// JavaScript Document


// Questa funzione apre una finestra popup con la foto passata come parametro
function PopIt (theIDFotoPubblicazione, windowwidth, windowheight)
{
	// Calcola la larghezza e l'altezza massima che potrà assumere la finestra in base alla risoluzione del monitor
	var screenW = 640, screenH = 480;
	if (parseInt(navigator.appVersion)>3)
	{
		screenW = screen.width;
		screenH = screen.height;
	}
	else if (navigator.appName == "Netscape" && parseInt(navigator.appVersion)==3 && navigator.javaEnabled()) 
	{
		var jToolkit = java.awt.Toolkit.getDefaultToolkit();
		var jScreenSize = jToolkit.getScreenSize();
		screenW = jScreenSize.width;
		screenH = jScreenSize.height;
	}
	
	// Se il monitor è più piccolo della finestra che si deve aprire allora la ridimensiona
	if ((screenW <= windowwidth) || (screenH <= windowheight))
	{
		windowwidth = (screenW -  20);
		windowheight = (screenH - 20);
		
		popup = window.open("fotopubblicazione_ita.php?idfotopubblicazione=" + theIDFotoPubblicazione,"popDialog","height=" + windowheight + ",width=" + windowwidth + ",scrollbars=yes");
	}
	else
	{
		popup = window.open("fotopubblicazione_ita.php?idfotopubblicazione=" + theIDFotoPubblicazione,"popDialog","height=" + windowheight + ",width=" + windowwidth + ",scrollbars=no");
	}
}
// fine PopIt


// Questa funzione apre una finestra popup con la presentazione di un autore passata come parametro
function AutorePopIt (theIDPubblicazione)
{
	popup = window.open("schedaautore_ita.php?idpubblicazione=" + theIDPubblicazione,"popDialog","height=400,width=400,scrollbars=yes");
}
// fine AutorePopIt


// Questa funzione tronca un numero con un certo numero di decimali
function troncaDecimali (theNumero, theDecimali)
{
	theNumero = Math.round(theNumero * Math.pow(10, theDecimali)) / Math.pow(10, theDecimali);
	
	return(theNumero);
}
// fine troncaDecimali


// Questa funzione apre una finestra popup con la foto passata come parametro
function PopItPhoto (titolopagina, urlfoto, windowwidth, windowheight)
{  
	var myHtml = "";
	
	myHtml = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" +
	"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" +
	"<head>\n" +
	"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n" +
	"<meta http-equiv=\"imagetoolbar\" content=\"no\" />" +
	"<title>" + titolopagina + "</title>\n" +
	"<style type=\"text/css\">\n" +
	"<!--\n" +
	"body {\n" +
	"	margin-left: 0px;\n" +
	"	margin-top: 0px;\n" +
	"	margin-right: 0px;\n" +
	"	margin-bottom: 0px;\n" +
	"}\n" +
	"div.divFotoPubblicazione {\n" +
	"	background-image: url(Ingrandimenti/" + urlfoto + ");\n" +
	"	background-repeat: no-repeat;\n" +
	"	background-position: 0px 0px;\n" +
	"	margin: 0px;\n" +
	"	padding: 0px;\n" +
	"	height: " + windowheight + "px;\n" +
	"	width: " + windowwidth + "px;\n" +
	"}\n" +
	"-->\n" +
	"</style>\n" +
	"<script src=\"script_popit_ing.js\" type=\"text/javascript\"></script>\n" +
	"</head>\n" +
	"<body>\n" +
	"<a href=\"javascript:window.close();\"><div class=\"divFotoPubblicazione\"><img src=\"Immagini/vuoto8x8.gif\" alt=\"" + titolopagina + "\" width=\"" + windowwidth + "\" height=\"" + windowheight + "\" hspace=\"0\" vspace=\"0\" border=\"0\" /></a></div>\n" +
	"</body>\n" +
	"</html>\n"; 
	
	popup = window.open("","popDialog","height="+windowheight+",width="+windowwidth+",scrollbars=no");
	popup.document.write(myHtml)  ;
	popup.document.close();
}
// fine PopItPhoto


// Questa funzione tronca il valore in euro alla seconda cifra decimale
function troncaEuro (theEuro)
{
	var myNumCifreDecimali = 2;	// Numero di cifre decimali che si desidera compaiano dopo la virgola
	
	theEuro = Math.round(theEuro * Math.pow(10, myNumCifreDecimali)) / Math.pow(10, myNumCifreDecimali);
	
	return(theEuro);
}
// fine troncaEuro


// Questa funzione aggiunge in fondo al valore in euro gli necessari zeri
function aggiungiDecimali (theEuro)
{
	var myEuro = troncaEuro(theEuro);
	
	var myStr = new String(myEuro);
	var myLength = 0;
	
	myLength = myStr.length;
	
	if (myStr.lastIndexOf(".") == -1)
	{
		myStr = (myStr + ".00");
	}
	else
	{
		if ((myLength - myStr.lastIndexOf(".")) == 2)
		{
			myStr = (myStr + "0");
		}
		else if ((myLength - myStr.lastIndexOf(".")) > 3)
		{
			myLength = (myStr.lastIndexOf(".") + 3);
			myStr = myStr.substring(0, myLength);
		}
	}
	
	if (myStr.charAt(0) == ".")
	{
		myStr = ("0" + myStr);
	}
	
	return myStr;
}
// fine aggiungiDecimali


// Questa funzione verifica se l'indirizzo di posta elettronica passato come
// parametro è valido
function isEmail (s)
{
	if (s == "")
	{
		return false;
	}
	
	// controlla se è uno spazio
	if (s == " ")
	{
		return false;
	}
	
	// ci deve essere almeno 1 carattere prima del carattere "@"
	// cosÏ la ricerca parte dalla posizione 1 della stringa (cioË il secondo carattere)
	var i = 1;
	var sLength = s.length;
	
	// cerca il carattere "@"
	while ((i < sLength) && (s.charAt(i) != "@"))
	{ i++
	}
	
	if ((i >= sLength) || (s.charAt(i) != "@")) return false;
	else i += 2;

	// cerca il carattere "."
	while ((i < sLength) && (s.charAt(i) != "."))
	{ i++
	}
	
	// ci devono essere almeno due caratteri dopo il "." per indicare il dominio di primo livello
	if ((i >= sLength - 2) || (s.charAt(i) != ".") || (s.indexOf(".@") > 0) || (s.indexOf("@.") > 0))
	{
		return false;
	}
	else
	{
		return true;
	}
}
// fine isEmail


// Questa funzione genera un codice di sicurezza casuale necessario per la validazione dei form
function CAPTCHA()
{
	var car, min, max, dif, lun, inc;
	car  = "abcdefghijklmnopqrstuvwxyz";
	car += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	car += "1234567890";
	min  = 5;
	max  = 5;
	dif  = max - min;
	lun  = Math.round((Math.random() * dif) + min);
	inc  = 0;
	cod  = "";
	
	while (inc < lun)
	{
		cod += car.charAt(Math.round(Math.random() * car.length));
		inc++;
	}
	
	return cod;
}


// Questa funzione consente di inserire solo dei numeri da 0 a 9 nel campo indicato come parametro
function soloNumeri (theField, theEvent, isDecimal)
{
	var key;
	var keychar;
	
	if (window.event)
		key = window.event.keyCode;
	else if (theEvent)
		key = theEvent.which;
	else
		return true;
	keychar = String.fromCharCode(key);

	// Consente l'uso dei tasti modificatori
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27))
		return true;

	// Fa passare i numeri da 0 a 9
	else if ((("0123456789").indexOf(keychar) > -1))
		return true;
	
	// Consente l'uso dei decimali
	else if ((isDecimal == true) && (keychar == "."))
	{
		theField.form.elements[isDecimal].focus();
		return true;
	}
	else
		return false;
}
// fine soloNumeri


