// Code by Andrea Moi (IUX) - Mind srl - http://wwmind.com 

// COMMON JS CODE FOR FORM

var InternetExplorer=navigator.appName.indexOf("Microsoft")!=-1;
var NetscapeNavigator=navigator.appName.indexOf("Netscape")!=-1;
var Gecko=navigator.appName.indexOf("Gecko")!=-1;
var browserVer=parseInt(navigator.appVersion);

//check for number
function validNum(val) { 
	if (browserVer>=4)		return !isNaN(parseInt(val));
	return true;
}

//check number field
function checkNum(obj,min,max,str,dec,obblig) { 
	
	var 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)
{
	var 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) {
	
	var 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) {
	
	var 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 windows (custom parameters)
function newWinCust(url,wname,wparam)		{
	// new windows
	var siteWindowVar=open(url,wname,"directories=no"+wparam);
	if (NetscapeNavigator||browserVer>=4)	{	siteWindowVar.focus();	siteWindowVar.focus();	}
}


// open a new popup window
function apri(url)		{
	// new windows
	newWinCust(url,"popupwin","toolbar=no,status=no,menubar=no,scrollbars=yes"
					+",resizable=no,location=no,width=550,height=320");
}

// open a windows (no menubar and location)
function newWinNoNav(url)		{
	// new windows
	newWinCust(url,"newwin","toolbar=yes,status=no,menubar=no,scrollbars=yes"
					+",resizable=yes,location=no,width=800,height=450");
}



// 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";
	}
}


// arrotonda un valore decimale
function roundVal(num,dec) {
	
	try {
		return	num.toFixed(dec);
	} catch (e) {
		return  num;
	}
}

// coma to point
function coma2point(val)
{
	try {
		return val.replace(",",".");
	} catch (e) {
		var num=""+val;
		var pos=num.indexOf(",");
		for (;pos!=-1;) {
			num=""+num.substring(0,pos)+"."+num.substring(pos+1,num.length);
			pos=num.indexOf(",");
		}
		return num;
	}
}

// point to coma
function point2coma(val)
{
	try {
		return val.replace(".",",");
	} catch (e) {
		var num=""+val;
		var pos=num.indexOf(".");
		for (;pos!=-1;) {
			num=""+num.substring(0,pos)+","+num.substring(pos+1,num.length);
			pos=num.indexOf(".");
		}
		return num;
	}
}