function VerificaEmail(percorso){
   var cont1=0;
   var cont2=0;
   var dom=""
   var mail=percorso.value;
   if (mail != ""){

      // controlla che vengano inseriti i caratteri @ e .
      if ((mail.indexOf('@', 0) == -1) || (mail.indexOf('.', 0) == -1)){
         alert("L'indirizzo e-mail è errato");
         percorso.select();
         percorso.focus();
         return false
      } else {
	  //controlla che dopo il carattere @ ci sia un punto
          for (var i=0; i<mail.length; i++){
            var ch=mail.substring(i, i+1);
	    //controlla che non vengano inseriti spazi
            if (ch == " "){cont2++}

            if (ch == "@"){
	       //controllo per verificare la presenza di almeno un carattere prima di @
	       if (i<1){cont2++}
               cont2++;
	       for (var n=i; n<mail.length; n++){
                 var ch2=mail.substring(n, n+1);
		 dom+=ch2;
                 if (ch2 == "."){
		    //almeno un carattere prima del punto
		    if (n==(i+1)){cont2++}
		    cont1=1;
		    dom="";
		 }
              }
            }
          }
          if ((cont1==1) && (cont2==1) && (dom.length>1) && (dom.length<=4)) {return true}
          else {
             alert("L'indirizzo e-mail è errato");
             percorso.select();
             percorso.focus();
             return false
          }
        }
     } else {
        alert("La scheda personale di registrazione non è corretta\ncompleta il campo Indirizzo e-mail");
        percorso.select();
        percorso.focus();
	return false
    }
}

function VerificaPrefisso(percorso){
   var pref=percorso.value;
   // controlla che la stringa non sia vuota
   if (pref == ""){
      alert("La scheda personale di registrazione non è corretta\ncompleta il campo Prefisso telefonico");
      percorso.select();
      percorso.focus();
      return false;
   }
   // controlla che la stringa sia di almeno 2 caratteri
   if ((pref.length < 2) || (pref.length >5)){
      alert("Prefisso errato");
      percorso.select();
      percorso.focus();
      return false;
   }
   // controlla che vengano inseriti solo numeri
   for (var i=0; i<pref.length; i++){
      var ch=pref.substring(i, i+1);
      if ((ch < "0") || (ch > "9")){
         alert("Prefisso errato");
         percorso.select();
         percorso.focus();
         return false;
      }
   }
   return true;
}

function VerificaTelefono(percorso){
   var tel=percorso.value;
   // controlla che la stringa non sia vuota
   if (tel == ""){
      alert("La scheda personale di registrazione non è corretta\ncompleta il campo Numero telefonico");
      percorso.select();
      percorso.focus();
      return false;

   }
   // controlla che la stringa sia maggiore di 3 caratteri e minore di 9
   if ((tel.length < 4) || (tel.length > 9)) {
      alert("Numero di telefono errato");
      percorso.select();
      percorso.focus();
      return false;
   }
   // controlla che vengano inseriti solo numeri
   for (var i=0; i<tel.length; i++){
      var ch=tel.substring(i, li+1);
      if ((ch < "0") || (ch > "9")){
         alert("Numero di telefono errato");
         percorso.select();
         percorso.focus();
         return false;
      }
   }
   return true;
}

function VerProv(percorso){
   Prov=percorso.value;
   if (Prov == ""){
      alert("Completa il campo Provincia");
      percorso.focus();
      return false;
   } else
   if (Prov.length != 2 ){
      alert("Inserire la sigla della provincia");
      percorso.select();
      percorso.focus();
      return false;
   } else {
      for (var i=0; i<Prov.length; i++){
         var ch=Prov.substring(i, i+1);
         if ((ch < "a") || (ch > "z")){
            if ((ch < "A") || (ch > "Z")){
              alert("La sigla della provincia è errata");
              percorso.select();
              percorso.focus();
              return false;
            }
         }
      }
      return true
   }

}

function VerCap(percorso){
   var Cap=percorso.value;
   // controlla che la stringa non sia vuota
   if (Cap == ""){
      alert("La scheda personale di registrazione non è corretta\ncompleta il campo CAP");
      percorso.select();
      percorso.focus();
      return false;
   }  else
   // controlla che la stringa sia di almeno 2 caratteri
   if ((Cap.length > 5 ) || (Cap.length < 4)){
      alert("Il CAP è errato");
      percorso.select();
      percorso.focus();
      return false;
   }
   // controlla che vengano inseriti solo numeri
   for (var i=0; i<Cap.length; i++){
      var ch=Cap.substring(i, i+1);
      if ((ch < "0") || (ch > "9")){
         alert("Il CAP deve contenere solo numeri");
         percorso.select();
         percorso.focus();
         return false;
      }
   }
   return true
}

function RTrim(VALUE){
var w_space = String.fromCharCode(32);
var v_length = VALUE.length;
var strTemp = "";
if(v_length < 0){
return"";
}
var iTemp = v_length -1;

while(iTemp > -1){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(0,iTemp +1);
break;
}
iTemp = iTemp-1;

} //End While
return strTemp;

} //End Function

function LTrim(VALUE){
var w_space = String.fromCharCode(32);
if(v_length < 1){
return"";
}
var v_length = VALUE.length;
var strTemp = "";

var iTemp = 0;

while(iTemp < v_length){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(iTemp,v_length);
break;
}
iTemp = iTemp + 1;
} //End While
return strTemp;
} //End Function

function LeftTrim(){
if(CheckEmpty(document.theForm.LeftTrim)){
return;
}
document.theForm.LeftTrim.value = LTrim(document.theForm.LeftTrim.value);
}

function RightTrim(){
if(CheckEmpty(document.theForm.RightTrim)){
return;
}
document.theForm.RightTrim.value = RTrim(document.theForm.RightTrim.value);
}

function Trim(TRIM_VALUE){
	if(TRIM_VALUE.length < 1){
		return"";
	}
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	if(TRIM_VALUE==""){
		return "";
	}
	else{
		return TRIM_VALUE;
	}
}

function Controlla() {

var re_emptyall = new RegExp("^[ ]*$");
var re_not_number = new RegExp("[^0-9]");

alert1 = "Devi inserire il nome";
alert2 = "Devi inserire il cognome";
alert3 = "Devi inserire il prefisso";
alert4 = "Devi inserire il telefono";
alert5 = "Devi inserire la richiesta";
alert6 = "Devi inserire l'indirizzo e-mail";
alert7 = "Devi acconsentire all'informativa sulla privacy";
alert8 = "La richiesta non puo' superare i 1000 caratteri";


alertMailing = "Per iscriverti alla newsletter\n devi inserire l'indirizzo e-mail";

	// Controllo il campo nome
	if ( (document.forms[1].nome.value == "") || (re_emptyall.test(document.forms[1].nome.value)) ) {
		alert(alert1);
		document.forms[1].nome.focus();
		return false;
	} 

	// Controllo il cognome
	if ( (document.forms[1].cognome.value == "") || (re_emptyall.test(document.forms[1].cognome.value)) ){
		alert(alert2);
		document.forms[1].cognome.focus();
		return false;
	}

	// Controllo il CAP
	if (document.forms[1].cap.value != "") {
		if (! VerCap(document.forms[1].cap) )  {
			document.forms[1].cap.focus();
			return false;
		}
	}
	
	// Controllo il campo del prefisso
	if ( (document.forms[1].pref.value == "") || (re_emptyall.test(document.forms[1].pref.value)) ) {
		alert(alert3);
		document.forms[1].pref.focus();
		return false;
	}

	// Controllo il campo del telefono
	if ( (document.forms[1].tel.value == "") || (re_emptyall.test(document.forms[1].tel.value)) ) {
		alert(alert4);
		document.forms[1].tel.focus();
		return false;
	}

	// Controllo il campo e-mail
	if ( (document.forms[1].email.value == "") || (re_emptyall.test(document.forms[1].email.value)) ) {
		alert(alert6);
		document.forms[1].email.focus();
		return false;
	}

	// Controllo il campo della richiesta
	if ( (document.forms[1].richta.value == "") || (Trim(document.forms[1].richta.value) == "scrivi qua la tua richiesta") || (re_emptyall.test(document.forms[1].richta.value)) ) {
		alert(alert5);
		document.forms[1].richta.focus();
		return false;
	}
	
	if (document.forms[1].richta.value.length > 1000) {
		alert(alert8);
		document.forms[1].richta.focus();
		return false;
	}
	// Se ho segnato anche il checkbox della mailing list.. 
	// devo controllare che il campo mail sia compilato
	if (document.forms[1].mailing.checked == 1)	{
		if ( (document.forms[1].email.value == "") || (re_emptyall.test(document.forms[1].email.value)) ) {
			alert(alertMailing);
			document.forms[1].email.focus();
			return false;
		}
	
	} // end if check mailing list
	
	if (! VerificaEmail(document.forms[1].email)) {
		return false;
	}
		
	if (document.forms[1].accetto.checked != 1) {
		alert(alert7);
		return false;
	}
	
}