function partitaIVA(sz_Codice)
   {
   var n_Val,n_Som1=0,n_Som2=0,lcv;
   if (sz_Codice.length!=11 || isNaN(parseFloat(sz_Codice)) || parseFloat(sz_Codice)<parseFloat(0))
      return false;
   
   for (lcv=0;lcv<9;lcv+=2)
   {
      n_Val=parseInt(sz_Codice.charAt(lcv));
      n_Som1+=n_Val;
      n_Val=parseInt(sz_Codice.charAt(lcv+1));
      n_Som1+=Math.floor(n_Val/5) + (n_Val<<1) % 10;
   }
   n_Som2 = 10 - (n_Som1 % 10);
   n_Val=parseInt(sz_Codice.charAt(10));
   if (n_Som2==n_Val)
      return true;
   return false;
   }
   
function controllaPIVA(pi){
	if( pi == '' )  return '';
	if( pi.length != 11 )
	        return "La lunghezza della partita IVA non è\n" +
	                "corretta: la partita IVA dovrebbe essere lunga\n" +
	                "esattamente 11 caratteri.\n";
	validi = "0123456789";
	for( i = 0; i < 11; i++ ){
	        if( validi.indexOf( pi.charAt(i) ) == -1 )
	                return "La partita IVA contiene un carattere non valido `" +
	                        pi.charAt(i) + "'.\nI caratteri validi sono le cifre.\n";
	     }
	s = 0;
	for( i = 0; i <= 9; i += 2 )
	        s += pi.charCodeAt(i) - '0'.charCodeAt(0);
	for( i = 1; i <= 9; i += 2 ){
	        c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
	        if( c > 9 )  c = c - 9;
	        s += c;
	}
	if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) )
	        return "La partita IVA non è valida:\n" +
	                "il codice di controllo non corrisponde.\n";
	return '';
	}

function is_null(chain) { 
	return (!chain || chain.length == 0 || chain == 'undefined') ? 1 : 0
}

function trim(stringa){
    while (stringa.substring(0,1) == ' '){
        stringa = stringa.substring(1, stringa.length);
    }
    while (stringa.substring(stringa.length-1, stringa.length) == ' '){
        stringa = stringa.substring(0,stringa.length-1);
    }
    return stringa;
}
function eCheckEMail(sn){
	var chain = trim(sn.value);
	var template = /^[a-z0-9\.\-_]+@[a-z0-9\.\-_]+\.[a-z]{2,3}$/i;
	var check = (is_null(chain)) ? 1 : template.test(chain);
	return(check);
}

function simp_validazione(theform){
	if (theform.mail.value == ""){
		alert('Inserire l\'indirizzo email');
		theform.mail.focus();
		return (false);
	}else if(!eCheckEMail(theform.mail)){
		alert('Indirizzo email non valido!');
		theform.mail.select();
		theform.mail.focus();
		return (false);				
	}
	return (true);
}

function validazione(theform)
		{
			if (theform.nome.value == "")
			{
				alert('Inserire il nome');
				theform.nome.focus();
				return (false);
			}
			if (theform.nome.value != "") {
				if (!theform.nome.value.match(/^[a-z\-\s]+$/i)) {
					alert("Formato nome non valido");
					theform.nome.focus()
					return false;
				}				
			}
			if (theform.cognome.value == "")
			{
				alert('Inserire il cognome');
				theform.cognome.focus();
				return (false);
			}
			if (theform.cognome.value != "") {
				if (!theform.cognome.value.match(/^[a-z\-\s]+$/i)) {
					alert("Formato cognome non valido");
					theform.cognome.focus()
					return false;
				}				
			}			
/*
//					alert(document.form1.settore.selectedIndex);
			var selObj = document.getElementById("firstlevel");
			val = selObj.options[selObj.selectedIndex].value;
			
			if (val == ''){
				alert('Selezionare una voce per il campo Area Funzionale.');
				selObj.focus();
				return (false);
			}else if (val == 'Altro'){
				var altroObj = document.getElementById('altro');
				altroval = altroObj.value;
				if(altroval == '' || altroval == 'Specificare una qualifica'){
					alert('Selezionare una voce per il campo Qualifica.');
					altroObj.focus();
					altroObj.select();
					return (false);
				}
			}else{
				var secondObj = document.getElementById("secondlevel");
				secondval = secondObj.options[secondObj.selectedIndex].value;
				if (secondval  == ''){
					alert('Selezionare una voce per il campo Qualifica.');
					secondObj.focus();
					return (false);
				}
			}


			if (theform.settore.selectedIndex == "0")
			{
				alert('Inserire il settore');
				theform.settore.focus();
				return (false);
			}

			if (theform.Ente.value == "")
			{
				alert('Inserire il nome della società');
				theform.Ente.focus();
				return (false);
			}
			
			if (theform.p_iva.value == "")
			{
				alert('Inserire la partita iva');
				theform.p_iva.focus();
				return (false);
			}
			else{
				if(theform.p_iva.value == "01230291005"){
				   alert("Partita IVA errata");
					theform.p_iva.focus();
				   return false;
				}
				if(theform.p_iva.value != "inserireiva"){
					var messaggio
					messaggio = controllaPIVA(theform.p_iva.value);
					if (messaggio!=""){
						alert("Partita Iva non valida\n"+messaggio);
						theform.p_iva.focus();
						return false;
					}					
				}
			}
			
			if (theform.telefono.value == ""){
				alert('Inserire il telefono');
				theform.telefono.focus();
				return (false);
			}
*/			
			if (theform.mail.value == ""){
				alert('Inserire l\'indirizzo email');
				theform.mail.focus();
				return (false);
			}else if(!eCheckEMail(theform.mail)){
				alert('Indirizzo email non valido!');
				theform.mail.select();
				theform.mail.focus();
				return (false);				
			}
			if (theform.Ente.value != "") {
				if (!theform.Ente.value.match(/^[a-z0-9\.\-\s]+$/i)) {
					alert("Formato Societa\'/Ente non valido");
					theform.Ente.focus()
					return false;
				}				
			}
			if (theform.indirizzo.value != "") {
				if (!theform.indirizzo.value.match(/^[a-z0-9,/\.\-\s]+$/i)) {
					alert("Formato indirizzo non valido");
					theform.indirizzo.focus()
					return false;
				}				
			}	
			if (theform.cap.value != "") {
				if (!theform.cap.value.match(/^[0-9]{5}$/i)) {
					alert("Formato cap non valido");
					theform.cap.focus()
					return false;
				}				
			}	
			if (theform.citta.value != "") {
				if (!theform.citta.value.match(/^[a-z\.\-\s]+$/i)) {
					alert("Formato Citta\' non valido");
					theform.citta.focus()
					return false;
				}				
			}	
			if (theform.telefono.value != "") {
				if (!theform.telefono.value.match(/^[\+0-9]+$/i)) {
					alert("Il campo 'Telefono' accetta solo numeri senza spazi");
					theform.telefono.focus()
					return false;
				}				
			}	
			if (theform.fax.value != "") {
				if (!theform.fax.value.match(/^[\+0-9]+$/i)) {
					alert("Il campo 'Fax' accetta solo numeri senza spazi");
					theform.fax.focus()
					return false;
				}				
			}	
			if (theform.legge.checked==false){
				alert('Consentire il trattamento dei dati');
				theform.legge.focus();
				return (false);
			}
			return (true);
		};