var bgError="#df2121";
var bgFocus="#CCCCCC";

globalLabel=new Array();
globalLabel["it"]=new Array();
globalLabel["it"]["exit_confirm"]="sei sicuro di voler uscire? le modifiche non salvate verranno perse.";
globalLabel["it"]["reload_confirm"]="sei sicuro di voler ricaricare l'oggetto? le modifiche non salvate verranno perse.";
globalLabel["it"]["logout_confirm"]="sei sicuro di voler uscire dall'applicativo? le modifiche non salvate verranno perse.";
globalLabel["it"]["result_page"]="pagina dei risultati";
globalLabel["it"]["no_result"]="nessun elemento soddisfa la tua ricerca.";
globalLabel["it"]["grid_no_result"]="nessun elemento associato.";
globalLabel["it"]["export_csv"]="salva in csv";
globalLabel["it"]["result_page_risultati"]="risultati";
globalLabel["it"]["result_page_di"]="di";
globalLabel["it"]["result_page_button"]="strumenti";
globalLabel["it"]["create_new"]="crea nuovo oggetto";
globalLabel["it"]["validate_campoVuoto"]="il campo non pu&ograve; essere vuoto";
globalLabel["it"]["validate_numInt"]="sono consentiti solo valori numerici interi";
globalLabel["it"]["validate_valoreCompreso"]="il valore nel campo non deve essere compreso fra ";
globalLabel["it"]["validate_valoreCompresoE"]=" e ";
globalLabel["it"]["validate_occorreSelezionare"]="occorre selezionare un valore/oggetto";
globalLabel["it"]["validate_formatoFloat"]="il valore nel campo deve essere in formato 0000,00";
globalLabel["it"]["validate_formatoData"]="la data deve essere nel formato gg/mm/aaaa";
globalLabel["it"]["validate_formatoTime"]="l'ora deve essere nel formato 00:00";
globalLabel["it"]["validate_lungCF"]="la lunghezza non &egrave; corretta: il codice deve essere lungo esattamente 16 caratteri";
globalLabel["it"]["validate_CFcarattereInvalido"]="il codice fiscale contiene un carattere non valido.";
globalLabel["it"]["validate_CFcarattereInvalido2"]="'. i caratteri validi sono le lettere e le cifre";
globalLabel["it"]["validate_CFnonCorretto"]="il codice fiscale non &egrave; corretto";
globalLabel["it"]["validate_EmailnonValida"]="il campo non contiene un indirizzo e-mail valido";
globalLabel["it"]["validate_PasswordnonCoincide"]="la password non coincide";
globalLabel["it"]["validate_EmailInesistente"]="l'indirizzo email indicato &egrave; inesistente";
globalLabel["it"]["validate_telefono"]="in formato +## ###### / 00## ###### / +##.###### / 00##.######";
globalLabel["it"]["create_search"]="esegui una ricerca";
globalLabel["it"]["create_search_select_mode"]="seleziona una ricerca";
globalLabel["it"]["create_search_simple"]="ricerca semplice";
globalLabel["it"]["create_search_advanced"]="ricerca avanzata";
globalLabel["it"]["create_search_fast"]="ricerca rapida";
globalLabel["it"]["validate_Checkbox"]="il checkbox deve essere selezionato";
globalLabel["it"]["validate_RicontrollareForm"]="Attenzione! Alcune parti del modulo non sono state correttamente compilate: ricontrollare i dati inseriti.";
// globalLabel
function getGlobalLabel(key) {
	return globalLabel["it"][key];
}


function get_estensione(path) {
    posizione_punto=path.lastIndexOf(".");
	lunghezza_stringa=path.length;
	estensione=path.substring(posizione_punto+1,lunghezza_stringa);
	return estensione;
}

function controlla_estensione(path){

	if((get_estensione(path)!="pdf")&&(get_estensione(path)!="doc"))
	{
		writeError(true, 'Curriculum', "Il file deve avere estensione .pdf o .doc");
		return false
	}
	else
	{
		return true;
	}
}

function controllaCampi(){
	
	validForm=true;
	if (!isValidValue("nome",false,"text")) validForm=false;
	if (!isValidValue("cognome",false,"text")) validForm=false;
	//if (!isValidValue("datan",false,"date")) validForm=false;
	//if (!isValidValue("indirizzo",false,"text")) validForm=false;
	//if (!isValidValue("citta",false,"text")) validForm=false;
	if (!isValidValue("telefono",false,"int")) validForm=false;
	if (!isValidValue("email",false,"email")) validForm=false;
	/*if (!isValidValue("arrivo",true,"date") validForm=false;	
	if (!isValidValue("partenza",true,"date") validForm=false;*/
	//if ((!isValidValue("curriculum",false,"text"))||(controlla_estensione(document.contattaci.Curriculum.value)==false))validForm=false;

	/*for (i=0;i<document.contattaci.accettoPrivacy.length;i++){
		if (document.contattaci.accettoPrivacy[i].checked) break;
	}
	accettoPrivacy_value=document.contattaci.accettoPrivacy[i].value;
	if (accettoPrivacy_value==0) {
		validForm=false;
		$("ERROR_notelegali").style.display="block";
		writeError(false, "notelegali", "Occorre accettare le note legali per effettuare l'invio");
	} else {
		$("ERROR_notelegali").style.display="none";
		resetError(false, "notelegali");
	}*/
	
	if (validForm) 
	{
		//alert("Tutto Ok");
		return true;
	}else{
		//alert("errore");
		return false;
	}
}
function resetError(changeBackground, idcampo) {
	if (changeBackground) {
		field = document.getElementById("contenitoreCampi_"+idcampo);
// 		field.style.background="";
		field.style.border="1px dotted transparent";
	}
	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("contenitoreCampi_"+idcampo);
// 		field.style.background=bgError;
		field.style.border="1px dotted #df2121";
	}
	errorHTML = document.getElementById("ERROR_"+idcampo);
	iconHTML = document.getElementById("ICON_"+idcampo);
	iconHTML.style.background='url(script/creacampi/simbolo_ko.png) no-repeat';
	errorHTML.innerHTML=errorMsg;
	errorHTML.style.height="auto";
}

function writeFocus(changeBackground, idcampo, msg) {
	if (changeBackground) {
		field = document.getElementById("contenitoreCampi_"+idcampo);
// 		field.style.background=bgFocus;
		field.style.border="1px dotted #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 e' riuscita. true, se tutto ok.
- text: isValidValue(..., minsize, maxsize)
- int: isValidValue(..., minvalue, maxvalue)
- float: isValidValue(..., minvalue, maxvalue)
*/
function isValidValue(idcampo, cannull, type, showError, changeBackground)
{
	//alert("isValidValue: "+idcampo);
	
	if (showError==undefined) showError=true;
	if (changeBackground==undefined) changeBackground=true;

	field = document.getElementById(idcampo);
	fieldBK = field;
	value = trim(field.value);
	// se è selezionata l'opzione che mostra l'errore
	if (showError) 
	{
		resetError(changeBackground, idcampo);
	}
// 		prova loader
	iconHTML = document.getElementById("ICON_"+idcampo);
	//controllo che sia consentito il valore nullo
	if (cannull==false) iconHTML.style.background='url(script/creacampi/loader.gif) no-repeat';
// 		

	switch (type)
	{
		case "check":
		{
			if (cannull == false) if (!fieldBK.checked) {
				if (showError)	writeError(changeBackground, idcampo,getGlobalLabel("validate_Checkbox"));
				return false;
			}
			iconHTML.style.background='url(script/creacampi/simbolo_ok.png) no-repeat';
			return true;
		}break;


		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(script/creacampi/simbolo_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(script/creacampi/simbolo_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(script/creacampi/simbolo_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(script/creacampi/simbolo_ok.png) no-repeat';
				return true;
			}
			if (!isTelefono(value))
			{
				if (showError) writeError(changeBackground, idcampo,getGlobalLabel("validate_telefono"));
				return false;
			}
			iconHTML.style.background='url(script/creacampi/simbolo_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(script/creacampi/simbolo_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(script/creacampi/simbolo_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(script/creacampi/simbolo_ok.png) no-repeat';
			return true;
		}break;


		case "date":
		{
			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 (!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(script/creacampi/simbolo_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(script/creacampi/simbolo_ok.png) no-repeat';
					return true;
				}
			}
			if (value.search(/^(?:[01]\d|2[0-3]|\d)\:(?:[0-5]\d|5[1-9])$/) != -1) 
				value+=":00"
			if (!verifyTimeFormat(value))
			{
				if (showError) writeError(changeBackground, idcampo,getGlobalLabel("validate_formatoTime"));
				return false;
			}
			iconHTML.style.background='url(script/creacampi/simbolo_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(script/creacampi/simbolo_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(script/creacampi/simbolo_ok.png) no-repeat';
			return true;
		}break;

		case "email":
		{
			if ((value.length == 0) && (cannull)) 
			{
				iconHTML.style.background='url(script/creacampi/simbolo_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(script/creacampi/simbolo_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(script/creacampi/simbolo_ok.png) no-repeat';
			return true;
		}break;

		default:
		{
			alert(idcampo);
			return false;
		}
	}
	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-5]\d|5[1-9])\:(?:[0-5]\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
	}
}