//------------------------------- COMMON
var basemessage = "- Selezionare un valore per i seguenti campi:<br/><br/>";
var message = "";
var error = "";
var errorcolor = "red";
var defaultcolor = "#0a50a1";

function ValidateEmail(emailaddr){
		var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/; 
		if (!email_reg_exp.test(emailaddr)) {
			return false;
		}else{
			return true
		} 
}

function getElementById(tagname){
	return document.getElementById(tagname);
}
//------------------------------- COMMON

//------------------------------- FORM SONDAGGIO PRIVATO
function HideIntroduzioneSondaggio(){
	
	document.getElementById("divsondaggio_0").style.display = "none";
	
}

function ShowSondaggio(){
	
	document.getElementById("divsondaggio").style.display = "inline";
	
}

function ValidatePrivateSondaggio(objform,numerodomande){
	
	objform = document.getElementById(objform);
	
	var selectedcounter = 0;
	//elementi del form corrente		
	for (var j=0;j <objform.elements.length;j++){
		
		if(objform.elements[j].type == "radio"){

			if (objform.elements[j].checked == true){
				//incrementa il contatore delle option selezionate
				selectedcounter = selectedcounter + 1;
			}			
			
		}
		
	}
	//verifica se mancano delle risposte
	if (selectedcounter != numerodomande){
			message = "Per inviare il sondaggio e' necessario selezionare tutte le risposte.";
			showPopupGenericError(message);
			return false;
	}else{
			if(confirm("Procedere con l'invio dei dati ?") == true){
				objform.submit();	
			}
	}
		
}
//controlla se la risposta è esatta
function CheckRisposta(thisradio,domandaid,rispostaesatta){
		
		//1:risposta esatta
		//0:risposta errata
		//-1: risposta libera
		
		var thediverrato;
		var thedivesatto;
		if (rispostaesatta == 1){
			//risposta esatta
			thediverrato = "errata_" + domandaid;
			document.getElementById(thediverrato).style.visibility = "hidden";
			
			thedivesatto = "esatta_" + domandaid;
			document.getElementById(thedivesatto).style.visibility = "visible";
		}else if(rispostaesatta == 0){
			//risposta errata
			thediverrato = "errata_" + domandaid;
			document.getElementById(thediverrato).style.visibility = "visible";
			
			thedivesatto = "esatta_" + domandaid;
			document.getElementById(thedivesatto).style.visibility = "hidden";
		}
		
}

//------------------------------- FORM SONDAGGIO PRIVATA

//------------------------------- FORM SONDAGGIO PUBBLICO
function ValidatePublicSondaggio(objform){
		
		var selectedone = false;
		//elementi del form corrente		
		for (var j=0;j <objform.elements.length;j++){
			
			if(objform.elements[j].type == "radio"){
				
				if (objform.elements[j].checked == true){
					selectedone = true;
				}
				
			}
			
		}
		//verifica se è stata selezionata una risposta
		if(selectedone == true){
			return true;
		}else{
			message = "Per inviare il sondaggio e' necessario selezionare una risposta.";
			showPopupGenericError(message);
			return false;
		}		

}
//------------------------------- FORM SONDAGGIO PUBBLICO

//------------------------------- FORM ISCRIZIONE NEWSLETTER
function ValidateNewsLetter(){
		
		var thisform = document.getElementById("newsletterform");						
		//nome
		if(thisform.firstname.value.length == 0){
			error = true;			
			getElementById("divfirstname_newsletter").style.color = errorcolor;
		}else{
			getElementById("divfirstname_newsletter").style.color = defaultcolor;
		}
		//cognome
		if(thisform.surname.value.length == 0){
			error = true;
			getElementById("divsurname_newsletter").style.color = errorcolor;
		}else{
			getElementById("divsurname_newsletter").style.color = defaultcolor;
		}
		//numerocard
		if(thisform.numerocard.value.length == 0){
			error = true;
			getElementById("divnumerocard_newsletter").style.color = errorcolor;
		}else{
			getElementById("divnumerocard_newsletter").style.color = defaultcolor;
		}		
		//email
		if(thisform.email.value.length == 0 || !ValidateEmail(thisform.email.value)){
			error = true;
			getElementById("divemail_newsletter").style.color = errorcolor;
		}else{			
			getElementById("divemail_newsletter").style.color = defaultcolor;
		}
		//pwd
		if(thisform.pwd.value.length == 0){
			error = true;
			getElementById("divpwd_newsletter").style.color = errorcolor;
		}else{
			getElementById("divpwd_newsletter").style.color = defaultcolor;
		}
		if(!error){
				//memorizza i dati
				var url = "";
				var reqbody = "";
				url = "ajax/registernewsletter.cfm?";
				reqbody = "nome=" + thisform.firstname.value;
				reqbody = reqbody + "&cognome=" + thisform.surname.value;
				reqbody = reqbody + "&numerocard=" + thisform.numerocard.value;
				reqbody = reqbody + "&email=" + thisform.email.value;
				reqbody = reqbody + "&pwd=" + thisform.pwd.value;
				
				xmlHttp=GetXmlHttpObject();									
				xmlHttp.onreadystatechange=ManageNewsLetter;						
				xmlHttp.open("POST",url,true);
				xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				xmlHttp.setRequestHeader("Content-length", reqbody.length);
				xmlHttp.setRequestHeader("Connection", "close");
				xmlHttp.send(reqbody);				
						
		}
		
}

function ManageNewsLetter(){
		
		if(xmlHttp.readyState==4){		
			
			document.getElementById("divreplynewsletter").style.display = "inline";		
			document.getElementById("divreplynewsletter").innerHTML = xmlHttp.responseText;
			
			document.getElementById("divnewslt").style.display = "none";	
			
		}
		
}
//------------------------------- FORM ISCRIZIONE NEWSLETTER

//------------------------------- FORM REMIND PASSWORD UTENTI
function ValidateRemind(){
	
		var thisform = document.getElementById("remindform");						
		//nome
		if(thisform.firstname.value.length == 0){
			error = true;			
			getElementById("divfirstname_remind").style.color = errorcolor;
		}else{
			getElementById("divfirstname_remind").style.color = defaultcolor;
		}
		//cognome
		if(thisform.surname.value.length == 0){
			error = true;
			getElementById("divsurname_remind").style.color = errorcolor;
		}else{
			getElementById("divsurname_remind").style.color = defaultcolor;
		}
		//azienda		
		if(thisform.aziende.value.length == 0){
			error = true;
			getElementById("divaziende_remind").style.color = errorcolor;
		}else{
			getElementById("divaziende_remind").style.color = defaultcolor;
		}
		//codice
		if(thisform.codice.value.length == 0){
			error = true;
			getElementById("divcodice_remind").style.color = errorcolor;
		}else{
			getElementById("divcodice_remind").style.color = defaultcolor;
		}		
		//email
		if(thisform.email.value.length == 0 || !ValidateEmail(thisform.email.value)){
			error = true;
			getElementById("divemail_remind").style.color = errorcolor;
		}else{			
			getElementById("divemail_remind").style.color = defaultcolor;
		}		
		if(!error){
				//invia la mail ad un amico
				var url = "";
				var reqbody = "";
				url = "ajax/remindpwd.cfm?";
				reqbody = "nome=" + thisform.firstname.value;
				reqbody = reqbody + "&cognome=" + thisform.surname.value;
				reqbody = reqbody + "&aziende=" + thisform.aziende.value;
				reqbody = reqbody + "&codice=" + thisform.codice.value;
				reqbody = reqbody + "&email=" + thisform.email.value;
				
				xmlHttp=GetXmlHttpObject();									
				xmlHttp.onreadystatechange=ManageRemind;						
				xmlHttp.open("POST",url,true);
				xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				xmlHttp.setRequestHeader("Content-length", reqbody.length);
				xmlHttp.setRequestHeader("Connection", "close");
				xmlHttp.send(reqbody)
		}
	
}

function ManageRemind(){
	
	if(xmlHttp.readyState==4){
		
		document.getElementById("divreplyremind").style.display = "inline";		
		document.getElementById("divreplyremind").innerHTML = xmlHttp.responseText;
		
		document.getElementById("divremind").style.display = "none";		
			
	}
	
}
//------------------------------- FORM REMIND PASSWORD UTENTI

//------------------------------- FORM REMIND PASSWORD DIPENDENTI
function ValidateRemindDip(){
	
		var thisform = document.getElementById("remindform");						
		//nome
		if(thisform.firstname_dip.value.length == 0){
			error = true;			
			getElementById("divfirstname_remind_dip").style.color = errorcolor;
		}else{
			getElementById("divfirstname_remind_dip").style.color = defaultcolor;
		}
		//cognome
		if(thisform.surname_dip.value.length == 0){
			error = true;
			getElementById("divsurname_remind_dip").style.color = errorcolor;
		}else{
			getElementById("divsurname_remind_dip").style.color = defaultcolor;
		}		
		//codice
		if(thisform.numcarta_dip.value.length == 0){
			error = true;
			getElementById("divcarta_remind_dip").style.color = errorcolor;
		}else{
			getElementById("divcarta_remind_dip").style.color = defaultcolor;
		}		
		//email
		if(thisform.email_dip.value.length == 0 || !ValidateEmail(thisform.email_dip.value)){
			error = true;
			getElementById("divemail_remind_dip").style.color = errorcolor;
		}else{			
			getElementById("divemail_remind_dip").style.color = defaultcolor;
		}		
		if(!error){
				//invia la mail ad un amico
				var url = "";
				var reqbody = "";
				url = "ajax/remindpwd_dip.cfm?";
				reqbody = "nome=" + thisform.firstname_dip.value;
				reqbody = reqbody + "&cognome=" + thisform.surname_dip.value;				
				reqbody = reqbody + "&numcarta=" + thisform.numcarta_dip.value;
				reqbody = reqbody + "&email=" + thisform.email_dip.value;
				
				xmlHttp=GetXmlHttpObject();									
				xmlHttp.onreadystatechange=ManageRemindDip;						
				xmlHttp.open("POST",url,true);
				xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				xmlHttp.setRequestHeader("Content-length", reqbody.length);
				xmlHttp.setRequestHeader("Connection", "close");
				xmlHttp.send(reqbody)
		}
	
}

function ManageRemindDip(){
	
	if(xmlHttp.readyState==4){
		
		document.getElementById("divreplyremind").style.display = "inline";		
		document.getElementById("divreplyremind").innerHTML = xmlHttp.responseText;
		
		document.getElementById("divremind").style.display = "none";		
			
	}
	
}
//------------------------------- FORM REMIND PASSWORD DIPENDENTI



//------------------------------- FORM ADERISCI AL CIRCUITO
function ValidateAdesioniForm(thisform){
		 
		 message = "";
		
		 //citta
		 if(thisform.citta.value.length == 0){
			 message = message + "- provincia<br/>";
		 }		
		 //categorie
		 if(thisform.categorie.value.length == 0){
			 message = message + "- categoria<br/>";
		 }
		 //ragione sociale
		 if(thisform.ragsoc.value.length == 0){
			 message = message + "- ragione sociale<br/>";
		 }		
		 //indirizzo
		 if(thisform.indirizzo.value.length == 0){
			 message = message + "- indirizzo<br/>";
		 }
		 //citta
		 if(thisform.prov.value.length == 0){
			 message = message + "- citta'<br/>";
		 }
		 //referente
		 if(thisform.referente.value.length == 0){
			 message = message + "- referente<br/>";
		 }
		 //telefono
		 if(thisform.tel.value.length == 0){
			 message = message + "- telefono<br/>";
		 }
		 //email
		 if(thisform.mail.value.length == 0){
			 message = message + "- indirizzo email<br/>";
		 }else{
			//verifica indirizzo email				
			if(!ValidateEmail(thisform.mail.value)){
				message = message + "- indirizzo email non corretto";	 
			}
		 }		 
		 if(message.length != 0){
			message = basemessage + message;			
			showPopupGenericError(message);
			return false;
		 }else{
			if (confirm("Procedere con l'invio dei dati ?") == true){
				thisform.process.value = "true"; 
				return true;	
			}else{
				return false;	
			}
		 }				 

}
//------------------------------- FORM ADERISCI AL CIRCUITO

//------------------------------- FORM CONTATTI
function ValidateContact(){
	    
		var thisform = document.getElementById("contactform");						
		error = false;
		//nome
		if(thisform.firstname.value.length == 0){
			error = true;			
			getElementById("divfirstname_contact").style.color = errorcolor;
		}else{
			getElementById("divfirstname_contact").style.color = defaultcolor;
		}
		//cognome
		if(thisform.surname.value.length == 0){
			error = true;			
			getElementById("divsurname_contact").style.color = errorcolor;
		}else{
			getElementById("divsurname_contact").style.color = defaultcolor;
		}
		//email
		if(thisform.email.value.length == 0 || !ValidateEmail(thisform.email.value)){
			error = true;
			getElementById("divemail_contact").style.color = errorcolor;
		}else{			
			getElementById("divemail_contact").style.color = defaultcolor;
		}		
		//campo di testo libero
		if(thisform.freetext.value.length == 0){
			error = true;			
			getElementById("divfreetext_contact").style.color = errorcolor;
		}else{
			getElementById("divfreetext_contact").style.color = defaultcolor;
		}
		//tipologia della richiesta
		if(thisform.tipologia.value.length == 0){
			error = true;			
			getElementById("divtipologia_contact").style.color = errorcolor;
		}else{
			getElementById("divtipologia_contact").style.color = defaultcolor;
		}		
		if(!error){
				//invia la mail
				var url = "";
				var reqbody = "";
				url = "ajax/contact.cfm";
				reqbody = "nome=" + thisform.firstname.value;
				reqbody = reqbody + "&cognome=" + thisform.surname.value;
				reqbody = reqbody + "&email=" + thisform.email.value;
				reqbody = reqbody + "&telefono=" + thisform.tel.value;
				reqbody = reqbody + "&freetext=" + thisform.freetext.value;
				reqbody = reqbody + "&tipologia=" + thisform.tipologia.value;
				
				xmlHttp=GetXmlHttpObject();									
				xmlHttp.onreadystatechange=ManageContact;						
				xmlHttp.open("POST",url,true);
				xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				xmlHttp.setRequestHeader("Content-length", reqbody.length);
				xmlHttp.setRequestHeader("Connection", "close");
				xmlHttp.send(reqbody)				
		}
		
}
function ManageContact(){
	
	if(xmlHttp.readyState==4){
		
		document.getElementById("divreplycontact").style.display = "inline";		
		document.getElementById("divreplycontact").innerHTML = xmlHttp.responseText;
		
		document.getElementById("divcontact").style.display = "none";		
			
	}
	
}
//------------------------------- FORM CONTATTI

//------------------------------- FORM INVIA AD UN AMICO
function ValidateFriendForm(){
		
		 message = "";
		 
		 var thisform = document.getElementById("friendform");						
		 
		 //nome mittente
		 if (thisform.sendername.value.length == 0){
			error = true;			
			getElementById("divnomemitt_friend").style.color = errorcolor;
		 }else{
			getElementById("divnomemitt_friend").style.color = defaultcolor;
		 }
		 //cognome mittente
		 if (thisform.sendersurname.value.length == 0){
			 error = true;
			 getElementById("divcognmitt_friend").style.color = errorcolor;
		 }else{
			getElementById("divcognmitt_friend").style.color = defaultcolor;
		 }
		 //email mittente
		 //if (thisform.senderemail.value.length == 0 || !ValidateEmail(thisform.senderemail.value)){
			 //error = true;
			 //getElementById("divemailmitt_friend").style.color = errorcolor;
		 //}else{
			 //getElementById("divemailmitt_friend").style.color = defaultcolor;
		 //}
		 
		 //nome destinatario
		 if (thisform.receivername.value.length == 0){
			 error = true;
			 getElementById("divnomedest_friend").style.color = errorcolor;
		 }else{
		 	 getElementById("divnomedest_friend").style.color = defaultcolor;
		 }
		 //cognome destinatario
		 if (thisform.receiversurname.value.length == 0){
			 error = true;
			 getElementById("divcogndest_friend").style.color = errorcolor;
		 }else{
			 getElementById("divcogndest_friend").style.color = defaultcolor; 	 
		 }
		 //email destinatario
		 if (thisform.receiveremail.value.length == 0 || !ValidateEmail(thisform.receiveremail.value)){
			 error = true;
			 getElementById("divemaildest_friend").style.color = errorcolor;
		 }else{
			 getElementById("divemaildest_friend").style.color = defaultcolor;
		 }
		 
		 if(!error){			 				
			//invia la mail ad un amico
			var url = "";
			var reqbody = "";
			url = "ajax/sendlinktoafriend.cfm";
			reqbody = "sendername=" + thisform.sendername.value;
			reqbody = reqbody + "&sendersurname=" + thisform.sendersurname.value;			
			reqbody = reqbody + "&receivername=" + thisform.receivername.value;
			reqbody = reqbody + "&receiversurname=" + thisform.receiversurname.value;
			reqbody = reqbody + "&receiveremail=" + thisform.receiveremail.value;
			reqbody = reqbody + "&message=" + thisform.message.value;
			
			xmlHttp=GetXmlHttpObject();									
			xmlHttp.onreadystatechange=ManageSendLinkToAFriend;						
			xmlHttp.open("POST",url,true);
			xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			xmlHttp.setRequestHeader("Content-length", reqbody.length);
		    xmlHttp.setRequestHeader("Connection", "close");
			xmlHttp.send(reqbody)			
		 }

}
function ManageSendLinkToAFriend(){
		
		if(xmlHttp.readyState==4){
					
			document.getElementById("divreplyfriend").style.display = "inline";		
			document.getElementById("divreplyfriend").innerHTML = xmlHttp.responseText;
		
			document.getElementById("divfriend").style.display = "none";	
			
		}
		
}
//------------------------------- FORM INVIA AD UN AMICO

//------------------------------- FORM SMARRIMENTO CARD
function ValidateLoseCard(thisform){
		
		message = "";
		
		var thisform = document.getElementById("looseform");						
		
		//numero card
		if(thisform.numerocard.value.length == 0){
			error = true;
			getElementById("divnumerocard_loose").style.color = errorcolor;			
		}else{
			getElementById("divnumerocard_loose").style.color = defaultcolor;
		}
		//password
		if(thisform.password.value.length == 0){
			error = true;
			getElementById("divpwd_loose").style.color = errorcolor;			
		}else{
			getElementById("divpwd_loose").style.color = defaultcolor;
		}
		
		if(!error){
			//memorizza i dati
			xmlHttp=GetXmlHttpObject();									
			xmlHttp.onreadystatechange=ManageRegisterLostCards;						
			xmlHttp.open("GET","ajax/registerlostcards.cfm?numerocard=" + thisform.numerocard.value + "&password=" + thisform.password.value,true);
			xmlHttp.send(null);			
		}		
}
function ManageRegisterLostCards(){
		 
		 if(xmlHttp.readyState==4){
			 
			 document.getElementById("divreplyloose").style.display = "inline";		
			 document.getElementById("divreplyloose").innerHTML = xmlHttp.responseText;
		
			 document.getElementById("divformlosecard").style.display = "none";	
			 
		 }
		
}
//------------------------------- FORM SMARRIMENTO CARD

//------------------------------- FORM CANCELLAZIONE CARD
function ValidateDeleteCard(thisform){
		
		message = "";
		
		var thisform = document.getElementById("deleteform");						
		
		//numero card
		if(thisform.numerocard.value.length == 0){
			error = true;
			getElementById("divnumerocard_delete").style.color = errorcolor;			
		}else{			
			getElementById("divnumerocard_delete").style.color = defaultcolor;
		}
		//password
		if(thisform.password.value.length == 0){
			error = true;
			getElementById("divpwd_delete").style.color = errorcolor;			
		}else{
			getElementById("divpwd_delete").style.color = defaultcolor;
		}
		
		if(!error){
			//memorizza i dati
			xmlHttp=GetXmlHttpObject();									
			xmlHttp.onreadystatechange=ManageDeleteCards;						
			xmlHttp.open("GET","ajax/deletecards.cfm?numerocard=" + thisform.numerocard.value + "&password=" + thisform.password.value,true);
			xmlHttp.send(null);			
		}		
}
function ManageDeleteCards(){
		 
		 if(xmlHttp.readyState==4){
			 
			 document.getElementById("divreplydelete").style.display = "inline";		
			 document.getElementById("divreplydelete").innerHTML = xmlHttp.responseText;
		
			 document.getElementById("divformdeletecard").style.display = "none";	
			 
		 }
		
}
//------------------------------- FORM CANCELLAZIONE CARD

//------------------------------- FORM MODIFICA
function ValidateModifica(){
		
		message = "";
		
		var thisform = document.getElementById("modificaform");						
		
		//numero card
		if(thisform.numerocard.value.length == 0){
			error = true;
			getElementById("divnumerocard_modifica").style.color = errorcolor;			
		}else{
			getElementById("divnumerocard_modifica").style.color = defaultcolor;
		}
		//password
		if(thisform.password.value.length == 0){
			error = true;
			getElementById("divpwd_modifica").style.color = errorcolor;			
		}else{
			getElementById("divpwd_modifica").style.color = defaultcolor;
		}
		
		if(!error){
			//memorizza i dati
			xmlHttp=GetXmlHttpObject();									
			xmlHttp.onreadystatechange=ManageModifica;						
			xmlHttp.open("GET","ajax/modificautente.cfm?numerocard=" + thisform.numerocard.value + "&password=" + thisform.password.value,true);
			xmlHttp.send(null);			
		}		
}
function ManageModifica(){
	
		if(xmlHttp.readyState==4){
			
			var jscode = xmlHttp.responseText;								
			
			//esegue il codice js inviato dalla pagina
			try{
				eval(jscode);
			}
			catch(err){		
				document.getElementById("divreplymodifica").style.display = "inline";		
				document.getElementById("divreplymodifica").innerHTML = xmlHttp.responseText;
		
				document.getElementById("divformmodifica").style.display = "none";						
			}
			
		}
	
}

function ValidateUpdateProfilo(objform){
		
		var numregexp = /^([0-9])/; 

		message = "";
		//nome
		//if(objform.nome.value.length == 0){
			//message = message + "- Inserire un valore nel campo Nome<br/>";
		//}		
		//cognome
		//if(objform.cognome.value.length == 0){
			//message = message + "- Inserire un valore nel campo Cognome<br/>";
		//}
		
		//codice fiscale
		//if(objform.codicefiscale.value.length == 0){
			//message = message + "- Inserire un valore nel campo Codice Fiscale<br/>";	
		//}else{
			//if(objform.codicefiscale.value.length != 16){
				//message = message + "- Inserire un Codice Fiscale valido<br/>";	
			//}	
		//}
		//ragione sociale & Partita iva
		//if(objform.ragionesociale.value.length != 0){
			
			//if(objform.partitaiva.value.length == 0){
				//message = message + "- Inserire un valore nel campo Partita Iva<br/>";	
			//}
			
		//}
		//if(objform.partitaiva.value.length != 0){
			
			//if(objform.ragionesociale.value.length == 0){
				//message = message + "- Inserire un valore nel campo Ragione Sociale<br/>";	
			//}
			
		//}
		
		//nome
		if($('#nome').val().length == 0){
			message = message + "- Inserire un valore nel campo Nome<br/>";
		}		
		//cognome
		if($('#cognome').val().length == 0){
			message = message + "- Inserire un valore nel campo Cognome<br/>";
		}
		//data di nascita
		if( $('#datanascita').val().length == 0 ){
			message = message + "- Inserire un valore nel campo Data di nascita<br/>";
		}else{
			//datanascita
var birthdatepattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
			
			var arr_bd = $('#datanascita').val().split("/");
			bd = arr_bd[1] + "/" + arr_bd[0] + "/" + arr_bd[2];
			if (bd.match(birthdatepattern) == null){
				message = message + "- Formato data di nascita non valido<br/>";
			}
			
		}
		//telefono
		if( $('#telfisso').val().length != 0 ){
			
			if( !numregexp.test($('#telfisso').val()) ){
				message = message + "- Il numero di telefono contiene caratteri non validi<br/>";
			}
			
		}
		//mobile
		if( $('#telmobile').val().length != 0 ){
			
			if( !numregexp.test($('#telmobile').val()) ){
				message = message + "- Il numero di cellulare contiene caratteri non validi<br/>";
			}
			
		}
		//email
		if( $('#useremail').val().length != 0 ){
			
			var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/; 
			if (!email_reg_exp.test( $('#useremail').val() )) {
				message = message + "- L'indirizzo email digitato non &egrave; valido\<br/>";	
			}
			
		}
		
		if( !document.getElementsByName("consensoprivacy")[0].checked ){
			message = message + "- Per procedere con l'invio dei dati devi dare il consenso";			
		}
		
		if (message.length != 0){
			showPopupGenericError(message);	
			return false;
		}else{
			return true;
		}
	
}
//------------------------------- FORM MODIFICA

//------------------------------- FORM DI LOGIN
function ValidateLogin(){
		
		message = "";		
		var thisform = document.getElementById("formlogin");						
		
		if(thisform.loginusername.value.length == 0){
			message = message + "- Inserire un valore nel campo Username<br/><br/>";
		}
		if(thisform.loginpwd.value.length == 0){
			message = message + "- Inserire un valore per il campo Password";
		}
		if(message.length > 0){			
			showPopupGenericError(message);
		}else{
			//effettua il login
			xmlHttp=GetXmlHttpObject();									
			xmlHttp.onreadystatechange=ManageLogin;						
			xmlHttp.open("GET","ajax/login.cfm?username=" + thisform.loginusername.value + "&password=" + thisform.loginpwd.value,true);
			xmlHttp.send(null);
		}
		
}
function ManageLogin(){
		
		if(xmlHttp.readyState==4){
		    
			Show_HideSelect();
		    var jscode = xmlHttp.responseText;								
			//esegue il codice js inviato dalla pagina
			try{
				eval(jscode);
			}
			catch(err){		
				showPopupGenericError("Siamo spiacenti, si è verificato un errore durante il login.");					
			}
			
		}
		
}
//------------------------------- FORM DI LOGIN
//------------------------------- LOGOUT
function ExecuteLogOut(){
		
		xmlHttp=GetXmlHttpObject();									
		xmlHttp.onreadystatechange=ManageLogOut;						
		xmlHttp.open("GET","ajax/logout.cfm",true);
		xmlHttp.send(null);
		
}
function ManageLogOut(){
		
		if(xmlHttp.readyState==4){
		   
		   var jscode = xmlHttp.responseText;								
			//esegue il codice js inviato dalla pagina
			try{
				eval(jscode);
			}
			catch(err){		
				showPopupGenericError("Siamo spiacenti, si è verificato un errore durante il logout.");					
			}
			
		}
		
}
//------------------------------- LOGOUT

//------------------------------- FORM DI LOGIN OPERATORE
function ExecuteLogOutOperatore(){
		
		xmlHttp=GetXmlHttpObject();									
		xmlHttp.onreadystatechange=ManageLogOutOperatore;						
		xmlHttp.open("GET","ajax/logoutoperatore.cfm",true);
		xmlHttp.send(null);
		
}
function ManageLogOutOperatore(){
		
		if(xmlHttp.readyState==4){
		   
		   var jscode = xmlHttp.responseText;								
			//esegue il codice js inviato dalla pagina
			try{
				eval(jscode);
			}
			catch(err){		
				showPopupGenericError("Siamo spiacenti, si è verificato un errore durante il logout.");					
			}
			
		}
		
}

function ValidateLoginOperatore(){
		
		message = "";		
		var thisform = document.getElementById("formloginoperatore");						
		
		if(thisform.usernameoperatore.value.length == 0){
			message = message + "- Inserire un valore nel campo Username<br/><br/>";
		}
		if(thisform.pwdoperatore.value.length == 0){
			message = message + "- Inserire un valore per il campo Password";
		}
		if(message.length > 0){			
			showPopupGenericError(message);
		}else{
			//effettua il login
			xmlHttp=GetXmlHttpObject();									
			xmlHttp.onreadystatechange=ManageLoginOperatore;						
			xmlHttp.open("GET","ajax/loginoperatore.cfm?username=" + thisform.usernameoperatore.value + "&password=" + thisform.pwdoperatore.value,true);
			xmlHttp.send(null);
		}
		
}
function ManageLoginOperatore(){
		
		if(xmlHttp.readyState==4){
		   
		   var jscode = xmlHttp.responseText;								
			//esegue il codice js inviato dalla pagina
			try{
				eval(jscode);
			}
			catch(err){		
				showPopupGenericError("Siamo spiacenti, si è verificato un errore durante il login.");					
			}
			
		}
		
}
//------------------------------- FORM DI LOGIN OPERATORE

//------------------------------- FORM DI DOMANDA A DINAMO
function ValidateFormDomanda(thisform){
	
	//testo libero
	if(thisform.domanda.value.length == 0){
		getElementById("divdomanda").style.color = errorcolor;			
		return false;
	}else{
		return true;
	}
	
}
//------------------------------- FORM DI DOMANDA A DINAMO


//------------------------------- FORM DI INSERIMENTO OFFERTA SPECIALE SHOPPING/SERVIZI/TEMPO LIBERO/RISTORAMTI
function ValidateInsOfferta(thisform){
	
	message = "";
	
	//abstract
	if(thisform.abstract.value.length == 0){
		message = message + "- Descrizione breve<br/>";
	}
	//descrizione
	if(thisform.descrizione.value.length == 0){
		message = message + "- Descrizione<br/>";
	}	
	//come catturare lo sconto
	if(thisform.comecatturare.value.length == 0){
		message = message + "- Come catturare lo sconto<br/>";
	}
	//periodo validita
	if(thisform.giorni_inizio.value.length == 0 || thisform.mesi_inizio.value.length == 0 || thisform.anni_inizio.value.length == 0 || thisform.giorni_fine.value.length == 0 || thisform.mesi_fine.value.length == 0 || thisform.anni_fine.value.length == 0){
		message = message + "- Periodo validit&agrave;<br/>";
	}
	//disponibilita
	if(thisform.disponibilita.value.length == 0){
		message = message + "- Disponibilit&agrave;<br/>";
	}	
	//sconto
	if(thisform.sconti.value.length == 0){
		message = message + "- Percentuale di sconto<br/>";
	}	
	//periodo di validita
	var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
	var datainizio;
	var datafine;
	if(thisform.giorni_inizio.value != "" || thisform.mesi_inizio.value != "" || thisform.anni_inizio.value != ""){		
		datainizio = thisform.mesi_inizio.value;
		datainizio = datainizio + "/" + thisform.giorni_inizio.value;
		datainizio = datainizio + "/" + thisform.anni_inizio.value;						
		if (datainizio.match(RegExPattern) == null){
			message = message + "- Data inizio offerta non valida<br><br>";
		}else{			
			datainizio = thisform.mesi_inizio.value + "/" + thisform.giorni_inizio.value + "/" + thisform.anni_inizio.value;
			getElementById("datainizio").value = datainizio;					
		}		
	}
	if(thisform.giorni_fine.value != "" || thisform.mesi_fine.value != "" || thisform.anni_fine.value != ""){		
		datafine = thisform.mesi_fine.value;
		datafine = datafine + "/" + thisform.giorni_fine.value;
		datafine = datafine + "/" + thisform.anni_fine.value;				
		if (datafine.match(RegExPattern) == null){
			message = message + "- Data fine offerta non valida<br><br>";
		}else{
			datafine = thisform.mesi_fine.value + "/" + thisform.giorni_fine.value + "/" + thisform.anni_fine.value;
			getElementById("datafine").value = datafine;		
		}		
	}
	//verifica che la data di fine validita sia superiore alla data odierna
	var d = new Date();
	var thisday = d.getDate();
	var thismonth = d.getMonth() + 1;
	var thisyear = d.getFullYear();
	if( (thisform.giorni_fine.value <= thisday) && (thisform.mesi_fine.value <= thismonth) && (thisform.anni_fine.value = thisyear) ){
		message = message + "- Data scadenza non valida";	
	}
	
	//vouchers
	if(thisform.voucher.checked == true){
		if(thisform.nvoucher.value == "" && thisform.vouchernolimit.checked == false){
			message = message + "- N. di voucher scaricabili <br/><br/>";
		}
		if (isNaN(thisform.nvoucher.value)){
			message = message + "- N. di voucher non valido<br/><br/>";
		}
	}
	
	if(message.length != 0){
		message = basemessage + message;
		showPopupGenericError(message);		
		return false;
	}else{
		if(confirm("Procedere con l'inserimento dell'offerta ?") == true){
			return true;
		}else{
			return false;
		}
	}
		
}

//validazione vouchers
function fixVouchers(){
	
	thischeckbox = getElementById("vouchernolimit");
	if(thischeckbox.checked == true){
		getElementById("nvoucher").value = "";
	}
	
	thisinput = getElementById("nvoucher");
	if(thisinput.value != ""){
		thischeckbox = getElementById("vouchernolimit");
		thischeckbox.checked = false;
	}	
	
}

function checkVouchers(){
	
	thisinput = getElementById("nvoucher");
	if(thisinput.value != ""){
		thischeckbox = getElementById("vouchernolimit");
		thischeckbox.checked = false;
	}	

}

//------------------------------- FORM DI INSERIMENTO OFFERTA SPECIALE SHOPPING/SERVIZI/TEMPO LIBERO/RISTORAMTI

//------------------------------- FORM DI MODIFICA DATI ESERCENTE 
function ValidateEsercenteForm(thisform){
	
	message = "";
	
	//nome negozio
	if(thisform.nomenegozio.value.length == 0){
		message = message + "- Nome negozio<br/>"; 
	}
	//indirizzo
	if(thisform.indirizzo.value.length == 0){
		message = message + "- Indirizzo<br/>"; 
	}
	//civico
	if(thisform.civico.value.length == 0){
		message = message + "- Numero civico<br/>"; 
	}
	//cap
	if(thisform.cap.value.length == 0){
		message = message + "- Cap<br/>"; 
	}
	//provincia (citta)
	if(thisform.provincia.value.length == 0){
		message = message + "- Provincia<br/>"; 
	}
	//comune
	if(thisform.comune.value.length == 0){
		message = message + "- Comune<br/>"; 
	}	
	//referente delle promozioni
	//cognome
	if(thisform.cognome_rp.value.length == 0){
		message = message + "- Cognome <br/>";
	}
	//nome
	if(thisform.nome_rp.value.length == 0){
		message = message + "- Nome <br/>";
	}
	//telefono
	if(thisform.telefono_rp.value.length == 0){
		message = message + "- Telefono <br/>";
	}
	//indirizzo
	//if(thisform.indirizzo_rp.value.length == 0){
	//	message = message + "- Indirizzo completo<br/>";
	//}
	//privacy
	//if( !document.getElementsByName("consensoprivacy")[0].checked ){
		//message = message + "- Per procedere con l'invio dei dati devi dare il consenso";			
	//}
	
	//privacy 
	if( !document.getElementsByName("consensoprivacy")[0].checked ){
		message = message + "- Per inviare i dati bisogna esprimere il proprio consenso<br/>";
	}
	
	if(message.length != 0){
		message = basemessage + message;
		showPopupGenericError(message);
		return false;
	}else{
		if(confirm("Procedere con l'invio dei dati ?") == true){
			return true;
		}else{
			return false;
		}
	}		
}
//------------------------------- FORM DI MODIFICA DATI ESERCENTE 

