// COMMON JS CODE FOR FORM

var InternetExplorer=navigator.appName.indexOf("Microsoft")!=-1;
var NetscapeNavigator=navigator.appName.indexOf("Netscape")!=-1;
var browserVer=parseInt(navigator.appVersion);
var Win=navigator.userAgent.indexOf("Win")!=-1;	
var	NS4=(typeof document.layers!="undefined")?1:0;
var Gecko=navigator.userAgent.indexOf("Gecko")!=-1;
var	IE=((typeof document.all!="undefined") && (browserVer >= 4))?1:0;


//check for number
function validNum(val) { 
	if (browserVer>=4)		return !isNaN(parseInt(val));
	return true;
}

//check true false
//function checkTrue(obj,str,obblig) { 
	
	//len=obj.value;

	//if (len=='true'&&!obblig)	return true;

	//return false;
//}

//check number field
function checkNum(obj,min,max,str,dec,obblig) { 
	
	len=obj.value.length;

	if (len==0&&!obblig)	return true;
	
	if (len<=0)	{	
		alert("Devi inserire un valore nel campo '"+str+"'.");
		obj.focus();    	return false;  	}
	
	// NO DECIMAL
	if (!dec && (obj.value.indexOf(",")!=-1 || obj.value.indexOf(".")!=-1))	{	
		alert("Utilizzare solo numeri interi nel campo '"+str+"'.");
		obj.focus();    	return false;  	}

	// CHECK DECIMAL POINT
	if (dec && obj.value.indexOf(",")!=-1)	{	
		alert("Utilizzare il punto (.) per separare i decimali nel campo '"+str+"'.");
		obj.focus();    	return false;  	}

	if (!validNum(obj.value)) {	
		alert("Inserire solo valori numerici nel campo '"+str+"'.");
		obj.focus();    	return false;  	}

	if (obj.value<min)	{	
		alert("Il valore minimo per il campo '"+str+"' e' "+min+".");
		obj.focus();    	return false;  	}

	if (obj.value>max)	{	
		alert("Il valore massimo per il campo '"+str+"' e' "+max+".");
		obj.focus();    	return false;  	}

	return true;
}

// check string lenght
function checkLength(obj,max,str)
{
	len=obj.value.length;
	if (len>max)
	{
		alert("Il campo '"+str+"' e' troppo lungo.\nCi sono "+(len-max)+" caratteri in eccesso.");
		obj.focus();    	return false;
	}
	return true;
}


// check string min lenght
function checkMinLength(obj,min,str)
{
	if (obj.value.length<min)
	{
		alert("Occorre completare il campo '"+str+"'\n(minimo "+min+" caratteri).");
		obj.focus();    	return false;
	}
	return true;
}


// check email format
function checkMail (obj,nome,obblig) {
	
	val=obj.value;
	
	if (obblig&&val.length<6) {		
		alert("L'email "+nome+" e' troppo corta\n(es: nome@nomeweb.com)");
		obj.focus();    	return false;		}

	if (!obblig&&val=="")	return true;
	
	if (val.length<6) {
		alert("L'email "+nome+" e' troppo corta.\n(es: nome@nomeweb.com)");
		obj.focus();    	return false;  	}
	if (val.indexOf('.')==-1) {
		alert("L'email "+nome+" non e' valida.\n(es: nome@nomeweb.com)");
		obj.focus();    	return false;  	}
	if (val.indexOf('@')==-1) {
		alert("L'email "+nome+" non e' valida.\n(es: nome@nomeweb.com)");
		obj.focus();    	return false;  	}
	if (val.indexOf('@')!=val.lastIndexOf('@')) {
		alert("L'email "+nome+" non e' valida.\n(es: nome@nomeweb.com)");
		obj.focus();    	return false;  	}
	if (val.indexOf(':')!=-1) {
		alert("L'email "+nome+" non e' valida.\n(es: nome@nomeweb.com)");
		obj.focus();    	return false;  	}
	if (val.indexOf('"')!=-1) {
		alert("L'email "+nome+" non e' valida.\n(es: nome@nomeweb.com)");
		obj.focus();    	return false;  	}
	if (val.indexOf("'")!=-1) {
		alert("L'email "+nome+" non e' valida.\n(es: nome@nomeweb.com)");
		obj.focus();    	return false;  	}
	if (val.indexOf(' ')!=-1) {
		alert("L'email "+nome+" non e' valida.\nNon utilizzare spazi.\n(es: nome@nomeweb.com)");
		obj.focus();    	return false;  	}

	return true;	
}


// check Url format
function checkUrl (obj,nome,obblig) {
	
	val=obj.value;
	
	if (obblig&&val.length<4) {		
		alert("L'indirizzo web "+nome+" e' troppo corto.");
		obj.focus();    	return false;		}

	if (!obblig&&val=="")	return true;
		
	if (val.length<4) {
		alert("L'indirizzo web "+nome+" e' troppo corto.");
		obj.focus();    	return false;  	}
	if (val.indexOf('.')==-1) {
		alert("L'indirizzo web "+nome+" non e' valido.");
		obj.focus();    	return false;  	}
	if (val.indexOf(':')!=-1&&val.indexOf(':')<8) {
		alert("L'indirizzo web "+nome+" non e' valido.\nNon utilizzare il prefisso http://");
		obj.focus();    	return false;  	}
	if (val.indexOf(' ')!=-1) {
		alert("L'indirizzo web "+nome+" non e' valido.\nNon utilizzare spazi.");
		obj.focus();    	return false;  	}
	

	return true;	
}



// open a new popup window
function apri(url)		{
	// new windows
	var siteWindowVar=open(url,"popupwin","toolbar=no,status=no,menubar=no,scrollbars=yes"
						+",resizable=no,location=no,directories=no,width=550,height=320");
	if (NetscapeNavigator||browserVer>=4)	{	siteWindowVar.focus();	siteWindowVar.focus();	}
}

// open a windows (no menubar and location)
function newWinNoNav(url)		{
	// new windows
	var siteWindowVar=open(url,"newwin","toolbar=yes,status=no,menubar=no,scrollbars=yes"
						+",resizable=yes,location=no,directories=no,width=800,height=450");
	if (NetscapeNavigator||browserVer>=4)	{	siteWindowVar.focus();	siteWindowVar.focus();	}
}
// open a windows (no menubar and location)
function apriPopUp(url,x,y)		{
	// new windows
	var siteWindowVar=open(url,"newwin","toolbar=no,status=no,menubar=no,scrollbars=yes"
						+",resizable=yes,location=no,directories=no,width="+x+",height="+y);
	if (NetscapeNavigator||browserVer>=4)	{	siteWindowVar.focus();	siteWindowVar.focus();	}

}

// open a windows (nothing and location)
function apriPopUp(url,x,y)		{
	// new windows
	var siteWindowVar=open(url,"newwin","toolbar=no,status=no,menubar=no,scrollbars=no"
						+",resizable=no,location=no,directories=no,width="+x+",height="+y);
	if (NetscapeNavigator||browserVer>=4)	{	siteWindowVar.focus();	siteWindowVar.focus();	}

}

// popup stato semaforo
function statoIB(desc)		{
	// new windows
	var siteWindowVar=open("/it/ibstatus.php?desc="+escape(desc),"ibstatus","toolbar=no,status=no,menubar=no,scrollbars=yes"
						+",resizable=yes,location=no,directories=no,width=500,height=250");
	if (NetscapeNavigator||browserVer>=4)	{	siteWindowVar.focus();	siteWindowVar.focus();	}
}


// get element by ID (with browser check)
function getObj(objID){
	if (!document.getElementById)	return null;	// not compatible
	//else
	return document.getElementById(objID);
}

// show and hide DIV
function alternateShow(divToShow,divToHide) {

	// hide
	if (divToHide!=null&&divToHide.style!=null) {
		divToHide.style.display = "none";
	}

	// show
	if (divToShow!=null&&divToShow.style!=null) {
		divToShow.style.display = "block";
	}
}
