var bgError="#df2121";
var bgFocus="#CCCCCC";

function resetError(changeBackground, idcampo) {
	if (changeBackground) {
		field = document.getElementById(idcampo);
// 		field.style.background="";
		field.style.border="2px solid rgb(226, 226, 226)";
	}
	errorHTML = document.getElementById("ERROR_"+idcampo);
	errorHTML.innerHTML="";
	iconHTML = document.getElementById("ICON_"+idcampo);
	iconHTML.style.background='';
	errorHTML.style.height="1px";
}

function writeError(changeBackground, idcampo, errorMsg) {
	if (changeBackground) {
		field = document.getElementById(idcampo);
		
// 		field.style.background=bgError;
		field.style.border="2px solid #df2121";
	}
	errorHTML = document.getElementById("ERROR_"+idcampo);
	iconHTML = document.getElementById("ICON_"+idcampo);
	iconHTML.style.background='url(http://redi2go.gonet.it/layout/validate_ko.png) no-repeat';
	errorHTML.innerHTML=errorMsg;
	errorHTML.style.height="auto";
}

function writeFocus(changeBackground, idcampo, msg) {
	if (changeBackground) {
		field = document.getElementById(idcampo);
// 		field.style.background=bgFocus;
		field.style.border="2px solid #CCCCCC";
	}
	
	errorHTML = document.getElementById("ERROR_"+idcampo);
	errorHTML.innerHTML=msg;
	iconHTML = document.getElementById("ICON_"+idcampo);
	iconHTML.style.background='';
	
}

/*
isValidValue ritorna false, se la validazione del campo non � riuscita. true, se tutto ok.
- text: isValidValue(idcampo, cannull, type, minsize, maxsize)
- int: isValidValue(idcampo, cannull, type, minvalue, maxvalue)
- float: isValidValue(idcampo, cannull, type, minvalue, maxvalue)
- email: isValidValue(idcampo, cannull, type)
*/
function isValidValue(idcampo, cannull, type, showError, changeBackground)
{
	//alert("isValidValue: "+idcampo);
	
	if (showError==undefined) showError=true;
	if (changeBackground==undefined) changeBackground=true;

	field = document.getElementById(idcampo);
	value = trim(field.value);
	if (showError) resetError(changeBackground, idcampo);
	
// 		prova loader
	iconHTML = document.getElementById("ICON_"+idcampo);
	if (cannull==false) iconHTML.style.background='url(http://redi2go.gonet.it/layout/validate_loader.gif) no-repeat';
// 		

	switch (type)
	{
		case "text":
		case "textarea":
		{
			minsize = arguments[5];
			maxsize = arguments[6];

			if ((value.length == 0) && (cannull == false))
			{
				if (showError)	writeError(changeBackground, idcampo,getGlobalLabel("validate_campoVuoto"));
// 				if (showError) writeError(changeBackground, idcampo,"il campo non pu&ograve; essere vuoto.");
				return false;
			}

			if ((minsize != 0) && (maxsize != 0))
			if ((value.length < minsize) || (value.length > maxsize))
			{
				return false;
			}
			iconHTML.style.background='url(http://redi2go.gonet.it/layout/validate_ok.png) no-repeat';
			return true;
		}break;

		case "int":
		{
			minvalue = arguments[5];
			maxvalue = arguments[6];
			if ((value.length == 0) && (cannull == false))
			{
				if (showError) writeError(changeBackground, idcampo,getGlobalLabel("validate_campoVuoto"));
				return false;
			}
			if ((value.length == 0) && (cannull == true)) 
			{
				iconHTML.style.background='url(http://redi2go.gonet.it/layout/validate_ok.png) no-repeat';
				return true;
			}
			if (!isInt(value))
			{
				if (showError) writeError(changeBackground, idcampo,getGlobalLabel("validate_numInt"));
				return false;
			}
			if ((minvalue != 0) && (maxvalue != 0))
			if ((value < minvalue) || (value > maxvalue))
			{
				if (showError) writeError(changeBackground, idcampo,getGlobalLabel("validate_valoreCompreso") + minvalue + getGlobalLabel("validate_valoreCompresoE") + maxvalue);
				return false;
			}
			iconHTML.style.background='url(http://redi2go.gonet.it/layout/validate_ok.png) no-repeat';
			return true;
		}break;

		case "telefono":
		{
			if ((value.length == 0) && (cannull == false))
			{
				if (showError) writeError(changeBackground, idcampo,getGlobalLabel("validate_campoVuoto"));
				return false;
			}
			if ((value.length == 0) && (cannull == true)) 
			{
				iconHTML.style.background='url(http://redi2go.gonet.it/layout/validate_ok.png) no-repeat';
				return true;
			}
			if (!isTelefono(value))
			{
				if (showError) writeError(changeBackground, idcampo,getGlobalLabel("validate_telefono"));
				return false;
			}
			iconHTML.style.background='url(http://redi2go.gonet.it/layout/validate_ok.png) no-repeat';
			return true;
		}break;



		case "int_id":
		{

			if ( (value == 0) && (cannull == false) )
			{
				if (showError) writeError(changeBackground, idcampo,getGlobalLabel("validate_occorreSelezionare"));
// 				if (showError) writeError(changeBackground, idcampo,"occorre selezionare un valore/oggetto");
				return false;
			}
			iconHTML.style.background='url(http://redi2go.gonet.it/layout/validate_ok.png) no-repeat';
			return true;
		}break;

		case "float":
		{
			minvalue = arguments[5];
			maxvalue = arguments[6];

			if ((value.length == 0) && (cannull == false))
			{
				if (showError) writeError(changeBackground, idcampo,getGlobalLabel("validate_campoVuoto"));
// 				if (showError) writeError(changeBackground, idcampo,"il campo non pu&ograve; essere vuoto.");
				return false;
			}

			if (!isFloat(value))
			{
				if (showError) writeError(changeBackground, idcampo,getGlobalLabel("validate_formatoFloat"));
// 				if (showError) writeError(changeBackground, idcampo,"il valore nel campo deve essere in formato 0000,00");
				return false;
			}

			if ((minvalue != 0) && (maxvalue != 0))
			if ((value < minvalue) || (value > maxvalue))
			{
				if (showError) writeError(changeBackground, idcampo,getGlobalLabel("validate_valoreCompreso") + minvalue + getGlobalLabel("validate_valoreCompresoE") + maxvalue);
				return false;
			}
			iconHTML.style.background='url(http://redi2go.gonet.it/layout/validate_ok.png) no-repeat';
			return true;
		}break;
		case "value":
		{
			minvalue = arguments[5];
			maxvalue = arguments[6];
			if ((value.length == 0) && (cannull == false))
			{
				if (showError) writeError(changeBackground, idcampo,getGlobalLabel("validate_campoVuoto"));
				return false;
			}
			if (!isValue(value))
			{
				if (showError) writeError(changeBackground, idcampo,getGlobalLabel("validate_formatoValue"));
				return false;
			}
			if ((minvalue != 0) && (maxvalue != 0))
			if ((value < minvalue) || (value > maxvalue))
			{
				if (showError) writeError(changeBackground, idcampo,getGlobalLabel("validate_valoreCompreso") + minvalue + getGlobalLabel("validate_valoreCompresoE") + maxvalue);
				return false;
			}
			iconHTML.style.background='url(http://redi2go.gonet.it/layout/validate_ok.png) no-repeat';
			return true;
		}break;


		case "date":
		{
			if (value.length == 0) {
				if (cannull == false) {
					if (showError) writeError(changeBackground, idcampo,getGlobalLabel("validate_campoVuoto"));
// 					if (showError) writeError(changeBackground, idcampo,"il campo non pu&ograve; essere vuoto.");
					return false;
					
				} else {
					iconHTML.style.background='url(http://redi2go.gonet.it/layout/validate_ok.png) no-repeat';
					return true;
				}
			}

			if (!verifyDateFormat(value,"it"))
			{
				if (showError) writeError(changeBackground, idcampo,getGlobalLabel("validate_formatoData"));
// 				if (showError) writeError(changeBackground, idcampo,"la data deve essere nel formato gg/mm/aaaa");
				return false;
			}
			iconHTML.style.background='url(http://redi2go.gonet.it/layout/validate_ok.png) no-repeat';
			return true;

		}break;

		case "time":
		{
			if (value.length == 0) {
				if (cannull == false) {
					if (showError) writeError(changeBackground, idcampo,getGlobalLabel("validate_campoVuoto"));
					return false;
					
				} else {
					iconHTML.style.background='url(http://redi2go.gonet.it/layout/validate_ok.png) no-repeat';
					return true;
				}
			}
			if (value.search(/^(?:[01]\d|2[0-3]|\d)\:(?:[0-4]\d|5[1-9])$/) != -1) 
				value+=":00"
			if (!verifyTimeFormat(value))
			{
				if (showError) writeError(changeBackground, idcampo,getGlobalLabel("validate_formatoTime"));
				return false;
			}
			iconHTML.style.background='url(http://redi2go.gonet.it/layout/validate_ok.png) no-repeat';
			return true;
		}break;


		case "codicefiscale":
		{
			cf = value.toUpperCase();

			var validi, i, s, set1, set2, setpari, setdisp;
			if ((cannull) && ( cf == '' ))
			{ 
				iconHTML.style.background='url(http://redi2go.gonet.it/layout/validate_ok.png) no-repeat';
				return true;
			}
			cf = cf.toUpperCase();
			if( cf.length != 16 ) {
				if (showError) writeError(changeBackground, idcampo,getGlobalLabel("validate_lungCF"));
				return false;
			}
			validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
			for( i = 0; i < 16; i++ ){
				if( validi.indexOf( cf.charAt(i) ) == -1 ) {
					if (showError) writeError(changeBackground, idcampo,getGlobalLabel("validate_CFcarattereInvalido") + cf.charAt(i) + getGlobalLabel("validate_CFcarattereInvalido2"));
					return false;
				}
			}
			set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
			set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
			setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
			setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
			s = 0;
			for( i = 1; i <= 13; i += 2 )
			s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
			for( i = 0; i <= 14; i += 2 )
			s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
			if( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) ) {
				if (showError) writeError(changeBackground, idcampo,getGlobalLabel("validate_CFnonCorretto"));
				return false;
			}
			iconHTML.style.background='url(http://redi2go.gonet.it/layout/validate_ok.png) no-repeat';
			return true;
		}break;

		case "email":
		{
			if ((value.length == 0) && (cannull)) 
			{
				iconHTML.style.background='url(http://redi2go.gonet.it/layout/validate_ok.png) no-repeat';
				return true;
			}

			if ((value.length == 0) && (cannull == false))
			{
				if (showError) writeError(changeBackground, idcampo,getGlobalLabel("validate_campoVuoto"));
// 				if (showError) writeError(changeBackground, idcampo,"il campo non pu&ograve; essere vuoto.");
				return false;
			}

			if (!isEmail(value))
			{
				if (showError) writeError(changeBackground, idcampo,getGlobalLabel("validate_EmailnonValida"));
// 				if (showError) writeError(changeBackground, idcampo,"il campo non contiene un indirizzo e-mail valido.");
				return false;
			}
			
			if (file_get_contents("rewriteURL.php?url=http://redigo.gonet.it/modules/validateMail.php?emailAddress="+value)==0) {
				if (showError) writeError(changeBackground, idcampo,getGlobalLabel("validate_EmailInesistente"));
// 				if (showError) writeError(changeBackground, idcampo,"l'indirizzo email indicato &egrave; inesistente.");
				return false;
			}
			iconHTML.style.background='url(http://redi2go.gonet.it/layout/validate_ok.png) no-repeat';
			return true;
		}break;

		case "ugualea":
		{
			id_campo_uguale = arguments[5];
			ugualea = trim(document.getElementById(id_campo_uguale).value);

			if ((ugualea.length == 0) && (cannull == false))
			{
				if (showError) writeError(changeBackground, idcampo,getGlobalLabel("validate_campoVuoto"));
// 				if (showError) writeError(changeBackground, idcampo,"il campo non pu&ograve; essere vuoto.");
				return false;
			}

			if (ugualea != 0)
			if ((ugualea.length != 0 ) && (ugualea != value))
			{
				if (showError) writeError(changeBackground, idcampo,getGlobalLabel("validate_PasswordnonCoincide"));
				return false;
			}
			iconHTML.style.background='url(http://redi2go.gonet.it/layout/validate_ok.png) no-repeat';
			return true;
		}break;

		default:
		{
			alert(idcampo);
			return false;
		}
	}

}

// Removes leading whitespaces
function LTrim( value ) {
	if (value==undefined) return "";
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");

}

// Removes ending whitespaces
function RTrim( value ) {
	if (value==undefined) return "";
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");

}

// Removes leading and ending whitespaces
function trim( value ) {
	if (value==undefined) return "";
	return LTrim(RTrim(value));

}

function alphanumeric(alphane)
{
	var numaric = alphane;
	for(var j=0; j<numaric.length; j++)
	{
		var alphaa = numaric.charAt(j);
		var hh = alphaa.charCodeAt(0);
		if((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123) || (hh == 32))
		{
		}
		else	{
			return false;
		}
	}
	return true;
}

function isNumeric(x)
{
	var RegExp = /^(-)?(\d*)(\.?)(\d*)$/; // Note: this WILL allow a number that ends in a decimal: -452.

	var result = x.match(RegExp);

	if (result==null)
	result=false;

	return result;
}

function isInt (str)
{
	str="1"+str;
	
	var i = parseInt (str);

	if (isNaN (i))
	return false;

	i = i . toString ();
	if (i != str)
	return false;

	return true;
}

function isFloat(s) // #####,## 
{
	var n = trim(s);
	return n.length>0 && (/^((\d+(\,\d{0,2})?)|((\d*\,)?\d{0,2}))$/).test(n);
// 	return n.length>0 && (/^((\d(\,\d{0,2})?)|(((\d{0,3})|(\.\d{3}))+(\,\d{0,2})?))$/).test(n);
}

function isValue(s) //da fare il tipo value 12.092,09  ( . separatore delle migliaia)
{
	var n = trim(s);
	return n.length>0 && (/^((\d(\,\d{0,2})?)|(((\d{0,3})|(\.\d{3}))+(\,\d{0,2})?))$/).test(n);
}


function isTelefono(s)
{
	var n = trim(s);
	return n.length>0 && (/^((00|\+)\d{2}[\. ]?)?(\d)+$/).test(n);
}


function isEmail(string)
{
	if (string.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) != -1)
	return true;
	else
	return false;
}

function verifyTimeFormat(value){
	if (value.search(/^(?:[01]\d|2[0-3]|\d)\:(?:[0-4]\d|5[1-9])\:(?:[0-4]\d|5[1-9])$/) != -1)
	return true;
	else
	return false;
}

function verifyDateFormat(DateString, DateFormat) {
	var match
	var tmpDate
	var validFormat = false
	
	try {
		match = DateString.match(/^(\d?\d)\D(\d?\d)\D(\d{2,4})$/)
	
		if (match != null) {
			if (DateFormat == "en") {
				tmpDate = new Date(match[3], match[1] - 1, match[2])
				validFormat = ((tmpDate.getMonth()==match[1]-1) && (tmpDate.getDate()==match[2]))
			} else if (DateFormat == "it"){
				tmpDate = new Date(match[3], match[2] - 1, match[1])
				validFormat = ((tmpDate.getMonth()==match[2]-1) && (tmpDate.getDate()==match[1]))
			}
		}
	}
	catch (e) {
		alert(e.message)
	}
	finally {
		return validFormat
	}
}
