function HTMLEntities(s, rev)
{
	var trTable=Array('¡', '&iexcl;', '¢', '&cent;', '£', '&pound;', '¤', '&curren;', '¥', '&yen;', '¦', '&brvbar;', '§', '&sect;', '¨', '&uml;', '©', '&copy;', 'ª', '&ordf;', '«', '&laquo;', '¬', '&not;', '­', '&shy;', '®', '&reg;', '¯', '&macr;', '°', '&deg;', '±', '&plusmn;', '²', '&sup2;', '³', '&sup3;', '´', '&acute;', 'µ', '&micro;', '¶', '&para;', '·', '&middot;', '¸', '&cedil;', '¹', '&sup1;', 'º', '&ordm;', '»', '&raquo;', '¼', '&frac14;', '½', '&frac12;', '¾', '&frac34;', '¿', '&iquest;', 'À', '&Agrave;', 'Á', '&Aacute;', 'Â', '&Acirc;', 'Ã', '&Atilde;', 'Ä', '&Auml;', 'Å', '&Aring;', 'Æ', '&AElig;', 'Ç', '&Ccedil;', 'È', '&Egrave;', 'É', '&Eacute;', 'Ê', '&Ecirc;', 'Ë', '&Euml;', 'Ì', '&Igrave;', 'Í', '&Iacute;', 'Î', '&Icirc;', 'Ï', '&Iuml;', 'Ð', '&ETH;', 'Ñ', '&Ntilde;', 'Ò', '&Ograve;', 'Ó', '&Oacute;', 'Ô', '&Ocirc;', 'Õ', '&Otilde;', 'Ö', '&Ouml;', '×', '&times;', 'Ø', '&Oslash;', 'Ù', '&Ugrave;', 'Ú', '&Uacute;', 'Û', '&Ucirc;', 'Ü', '&Uuml;', 'Ý', '&Yacute;', 'Þ', '&THORN;', 'ß', '&szlig;', 'à', '&agrave;', 'á', '&aacute;', 'â', '&acirc;', 'ã', '&atilde;', 'ä', '&auml;', 'å', '&aring;', 'æ', '&aelig;', 'ç', '&ccedil;', 'è', '&egrave;', 'é', '&eacute;', 'ê', '&ecirc;', 'ë', '&euml;', 'ì', '&igrave;', 'í', '&iacute;', 'î', '&icirc;', 'ï', '&iuml;', 'ð', '&eth;', 'ñ', '&ntilde;', 'ò', '&ograve;', 'ó', '&oacute;', 'ô', '&ocirc;', 'õ', '&otilde;', 'ö', '&ouml;', '÷', '&divide;', 'ø', '&oslash;', 'ù', '&ugrave;', 'ú', '&uacute;', 'û', '&ucirc;', 'ü', '&uuml;', 'ý', '&yacute;', 'þ', '&thorn;', 'ÿ', '&yuml;');

	if (rev)
	{
		for (i=0; i<trTable.length; i=i+2)
		{
			s=s.replace(eval('/'+trTable[i+1]+'/g'), trTable[i]);
		}
		s=s.replace(eval('/&quot;/g'), '"');
		s=s.replace(eval('/&lt;/g'), '<');
		s=s.replace(eval('/&gt;/g'), '>');
		s=s.replace(eval('/&amp;/g'), '&');
	}
	else
	{
		s=s.replace(eval('/&/g'), '&amp;');
		s=s.replace(eval('/"/g'), '&quot;');
		s=s.replace(eval('/</g'), '&lt;');
		s=s.replace(eval('/>/g'), '&gt;');
		for (i=0; i<trTable.length; i=i+2)
		{
			s=s.replace(eval('/'+trTable[i]+'/g'), trTable[i+1]);
		}
	}

	return s;
}

function checkFields(aForm)
{
	var i;

	for (i=0; i<aForm.length; i++)
	{
		if (aForm.elements[i].onchange)
		{
			if(!aForm.elements[i].onchange())
				{return false;}
		}
	}

	return true;
}

var DecSep=',';
var ThSep='.';

function getNumber(S)
{
	var Sign;
	var SepPos;
	var SInt="";
	var SDec="";
	var DecLen;
	var DecWeight;
	var i;

	if (S.length == 0)
	{
		return NaN;
	}

	if (S.substr(0,1) == '-')
	{
		Sign=-1;
		S=S.substr(1);
	}
	else
		{Sign=1}

	if ((SepPos=S.indexOf(DecSep)) >= 0)
	{
		SInt=S.substr(0,SepPos);
		SDec=S.substr(SepPos+1);
		if (SInt == "")
			{SInt="0"}
	}
	else
	{
		SInt=S;
		SDec="";
	}

	while ((SepPos=SInt.indexOf(ThSep)) >= 0)
		{SInt=SInt.substr(0,SepPos)+SInt.substr(SepPos+1);}

	DecLen=SDec.length;
	DecWeight=1;
	for (i=1;i<=DecLen;i++)
		{DecWeight*=10;}

 	if (DecLen > 0)
 		{return(Sign*(parseFloat(SInt)+parseFloat(SDec/DecWeight)));}
 	else
 		{return(Sign*parseFloat(SInt));}
}

function putNumber(N, DecLen)
{
	var Sign="";
	var DecWeight;
	var SInt="";
	var SDec="";
	var i;

	if (N < 0)
	{
		Sign="-";
		N=-N;
	}
	else
		{Sign=""}
	DecWeight=1;
	for (i=1;i<=DecLen;i++)
		{DecWeight*=10}

	N+=0.5/DecWeight;
	var SInt=new String(Math.floor(N));
	var SDec=new String(Math.floor((N-Math.floor(N))*DecWeight));

	if (SInt.indexOf("E") >= 0 ||
		SDec.indexOf("E") >= 0)
		{return("!#")}

	for(i=SInt.length-3; i>0; i-=3)
		{SInt=SInt.substr(0,i)+ThSep+SInt.substr(i);}

	while (SDec.length < DecLen)
		{SDec='0'+SDec}

	if (SDec.length > 0 && DecLen > 0)
		{return(Sign+SInt+DecSep+SDec)}
	else
		{return(Sign+SInt)}
}

function checkNumber(NField, TotLen, DecLen, Min, Max)
{
	var SNum;
	var Num;
	var SepPos;
	var i;

	if (!DecLen)
		{DecLen=0;}

	SNum=NField.value;
	// Eliminazione segno
	if ((SepPos=SNum.indexOf("-")) >= 0)
		{SNum=SNum.substr(0,SepPos)+SNum.substr(SepPos+1);}
	// Eliminazione separatore decimale
	if ((SepPos=SNum.indexOf(DecSep)) >= 0)
	{
		if (DecLen == 0)
		// Il separatore decimale non e' ammesso
		{
			alert(BadCharMsg + "'" + DecSep + "'");
			NField.focus();
			return false;
		}

		SNum=SNum.substr(0,SepPos)+SNum.substr(SepPos+1);
	}
	// Eliminazione separatori delle migliaia
	while ((SepPos=SNum.indexOf(ThSep)) >= 0)
		{SNum=SNum.substr(0,SepPos)+SNum.substr(SepPos+1);}

	// Controllo formato
	if (SNum.length > TotLen)
	{
		alert(LenMsg);
		NField.focus();
		return false;
	}

	// Controllo presenza caratteri non numerici
	for (i=0; i<SNum.length; i++)
	{
		if (SNum.charCodeAt(i) < "0".charCodeAt(0) || SNum.charCodeAt(i) > "9".charCodeAt(0))
		{
			alert(BadCharMsg + "'" + SNum.charAt(i) + "'");
			NField.focus();
			return false;
		}
	}

	Num=getNumber(NField.value);

	if (isNaN(Num) && (!isNaN(Min) || !isNaN(Max)))
	{
		alert(NoNumMsg);
		NField.focus();
		return false;
	}
	else if (!isNaN(Num))
	{
		if (!isNaN(Min) && Num < Min)
		{
			alert(MinMsg + putNumber(Min, DecLen));
			NField.focus();
			return false;
		}
		if (!isNaN(Max) && Num > Max)
		{
			alert(MaxMsg + putNumber(Max, DecLen));
			NField.focus();
			return false;
		}

		NField.value=putNumber(Num, DecLen);
		return true;
	}
	else if (NField.value == 0)
	{
		return true;
	}
}

function checkString(SField, MinLen, MaxLen)
// Controllo campo alfanumerico.
// MinLen: lunghezza minima
// MaxLen: lunghezza massima
{
	// Controllo lunghezza
	if (SField.value.length < MinLen || SField.value.length > MaxLen)
	{
		alert(StrLenMsg + MinLen + ".." + MaxLen);
		SField.focus();
		return false;
	}

	return true;
}

function checkDT(DTField, CheckType)
// Controllo campo data e ora.
// CheckType: tipo di controllo (i valori possono essere concatenati)
//	'D' controllo data e campo obbligatorio
//	'd' controllo data
//	'T' controllo ora (senza secondi) e campo obbligatorio
//	't' controllo ora (senza secondi)
//	'S' controllo ora (con secondi) e campo obbligatorio
//	's' controllo ora (con secondi)
{
	var SepPos;
	var EndDay, EndMonth;
	var Day, Month, Year;
	var CurrentYear;
	var Today=new Date();

	var EndHour,EndMinute;
	var Hour, Minute, Secs;
	var CheckSecs;

	var DateField="";
	var TimeField="";
	var DTArray=new Array("","");

	if ((SepPos=DTField.value.indexOf(' ')) >= 0)
	{
		DTArray[0]=DTField.value.substr(0,SepPos);
		DTArray[1]=DTField.value.substr(SepPos+1);
	}
	else
		{DTArray[0]=DTField.value;}

	switch (CheckType)
	{
		case 'DS':
			CheckSecs=1;
		case 'DT':
			if (DTArray.length < 2)
			{
				alert(NoDTMsg);
				DTField.focus();
				return false;
			}
		case 'ds':
			CheckSecs=1;
		case 'dt':
			DateField=DTArray[0];
			TimeField=DTArray[1];
			break;
		case 'Ds':
			CheckSecs=1;
		case 'D':
		case 'Dt':
			if (DTArray[0] == "")
			{
				alert(NoDateMsg);
				DTField.focus();
				return false;
			}
		case 'd':
			DateField=DTArray[0];
			TimeField=DTArray[1];
			break;
		case 'dS':
			CheckSecs=1;
		case 'dT':
			if (DTArray[0] == "")
			{
				alert(NoTimeMsg);
				DTField.focus();
				return false;
			}
			if (DTArray[1] == "")
				{TimeField=DTArray[0];}
			else
			{
				DateField=DTArray[0];
				TimeField=DTArray[1];
			}
			break;
		case 'S':
			CheckSecs=1;
		case 'T':
			if (DTArray[0] == "")
			{
				alert(NoTimeMsg);
				DTField.focus();
				return false;
			}
			TimeField=DTArray[0];
			break;
		case 's':
			CheckSecs=1;
		case 't':
			TimeField=DTArray[0];
			break;
	}

	if (DateField != "")
	// Controllo della data
	{
		if (DateField.indexOf("/") >= 0)
		// E' stata indicata una sbarra
		{
			// Individuazione dei separatori
			EndDay=DateField.indexOf("/");
			EndMonth=DateField.indexOf("/",EndDay+1);
			if (EndMonth < 0 && DateField.length > EndDay)
			{
				EndMonth=DateField.length;
			}

			// Controllo del formato
			if (EndDay < 0 || EndMonth < 0)
			{
				alert(DateFormatMsg);
				DTField.focus();
				return false;
			}

			// Lettura di giorno, mese e anno
			Day=DateField.substring(0,EndDay);
			Month=DateField.substring(EndDay+1,EndMonth);
			Year=DateField.substring(EndMonth+1,DateField.length);
		}
		else
		{
			// Lettura di giorno, mese e anno
			Day=DateField.substring(0,2);
			Month=DateField.substring(2,4);
			Year=DateField.substring(4,DateField.length);
		}

		// Controllo validita' del giorno
		if (Day < 1 || Day > 31)
		{
			alert(DayMsg);
			DTField.focus();
			return false;
		}
		else
		{
			if (Day.length < 2)
				{Day='0'+Day;}
		}

		// Controllo validita' del mese
		if (Month < 1 || Month > 12)
		{
			alert(MonthMsg);
			DTField.focus();
			return false;
		}
		else
		{
			if (Month.length < 2)
				{Month='0'+Month;}
		}

		// Controllo della lunghezza dell'anno
		if (Year.length <= 2)
		{
			// Trasformazione dell'anno in formato a 4 cifre
			if (Today.getFullYear())
				{CurrentYear=""+Today.getFullYear()}
			else
				{CurrentYear=""+eval("1900 + "+Today.getFullYear())}

			Year=CurrentYear.substring(0,4-Year.length)+Year;
		}
	}

	if (TimeField != "")
	// Controllo dell'ora
	{
		if (TimeField.indexOf(":") >= 0)
		{
			// Individuazione dei separatori
			EndHour=TimeField.indexOf(":");
			EndMinute=TimeField.indexOf(":", EndHour+1);
			if (EndMinute < 0 && TimeField.length > EndHour)
			{
				EndMinute=TimeField.length;
			}

			// Controllo del formato
			if (EndHour < 0 || EndMinute < 0)
			{
				alert(TimeFormatMsg);
				DTField.focus();
				return false;
			}

			// Lettura di ora e minuti
			Hour=TimeField.substring(0,EndHour);
			Minute=TimeField.substring(EndHour+1,EndMinute);
			Secs=TimeField.substring(EndMinute+1,TimeField.length);
		}
		else
		{
			// Lettura di ora e minuti
			Hour=TimeField.substring(0,2);
			Minute=TimeField.substring(2,4);
			Secs=TimeField.substring(4,TimeField.length);
		}

		// Controllo validita' dell'ora
		if (Hour < 0 || Hour > 23)
		{
			alert(HourMsg);
			DTField.focus();
			return false;
		}
		else
		{
			if (Hour.length < 2)
				{Hour='0'+Hour;}
		}

		// Controllo validita' dei minuti
		if (Minute < 0 || Minute > 59)
		{
			alert(MinuteMsg);
			DTField.focus();
			return false;
		}
		else
		{
			if (Minute.length < 2)
				{Minute='0'+Minute;}

			if (Minute == '0')
				{Minute='00';}
		}

		if (CheckSecs)
		{
			// Controllo validita' dei secondi
			if (Secs < 0 || Secs > 59)
			{
				alert(SecsMsg);
				DTField.focus();
				return false;
			}
			else
			{
				if (Secs.length < 2)
					{Secs='0'+Secs;}

				if (Secs == '0')
					{Secs='00';}
			}
		}
	}

	if (DateField != "")
	{
		DTField.value=Day+"/"+Month+"/"+Year;
		if (TimeField != "")
		{
			if (CheckSecs)
			{
				DTField.value+=" "+Hour+":"+Minute+":"+Secs;
			}
			else
			{
				DTField.value+=" "+Hour+":"+Minute;
			}
		}
	}
	else
	{
		if (TimeField != "")
		{
			if (CheckSecs)
			{
				DTField.value=Hour+":"+Minute+":"+Secs;
			}
			else
			{
				DTField.value=Hour+":"+Minute;
			}
		}
	}

	return true;
}

function checkRadio(aRadio)
{
	if (aRadio.type == "radio")
	{
		var checked=false;
		var radios=eval("document."+aRadio.form.name+"."+aRadio.name);

		for (var i=0; i<radios.length; i++)
		{
			if (radios[i].checked)
			{
				checked=true;
				break;
			}
		}

		if (!checked)
		{
			alert(NoCheckedMsg);
			radios[0].focus();
			return false;
		}
	}

	return true;
}

function checkSelect(aSelect)
{
	if (aSelect.selectedIndex == 0)
	{
		alert(NoCheckedMsg);
		aSelect.focus();
		return false;
	}

	return true;
}

function openWin(URL,Name,Features)
{
	var openWin=window.open(URL,Name,Features);
	openWin.focus();
}

function resizeWin(width, height)
{
/*
	var winWidth, winHeight;
	var widthStep, heightStep;

	if (parseInt(navigator.appVersion) > 3 )
	{
		if (navigator.appName.indexOf("Netscape") != -1)
		{
			winWidth=window.innerWidth;
			winHeight=window.innerHeight-46;
		}
		if (navigator.appName.indexOf("Microsoft") != -1)
		{
			winWidth=document.body.offsetWidth;
			winHeight=document.body.offsetHeight;
		}
	}

	widthStep=(width-winWidth)/50;
	heightStep=(height-winHeight)/50;

	for (i=0; i<50; i++)
	{
		window.resizeBy(widthStep, heightStep);
	}
*/

	window.resizeTo(width, height);
}

function openImg(ImgURL,Name,Title,Features,Width,Height)
{
	var openWin=window.open("",Name,Features);
	openWin.document.open();
	openWin.document.write("<html>\n<head>\n<title>"+Title+"</title>\n<style type=\"text/css\">\nbody {margin:0}\n</style>\n</head>\n<body>\n");
	openWin.document.write("<img src=\""+ ImgURL +"\" width=\""+ Width +"\" height=\""+ Height +"\" alt=\"\" border=\"0\">");
	openWin.document.write("</body></html>");
	openWin.document.write();
	openWin.document.close();
	openWin.focus();
}

function mailto(usr, dom, tit, txt)
{
	if (tit == '')
	{
		tit=usr + '@' + dom;
	}
	if (txt == '')
	{
		txt=usr + '@' + dom;
	}
	document.write('<a href="mailto');
	document.write(':' + usr + '@');
	document.write(dom + '" title="' + tit + '">' + txt + '</a>');
}

