function domandaValidator(theForm)
{
  if (theForm.domanda.value == "0")
  {
    alert("E' necessario scegliere una Domanda Segreta.");
    theForm.domanda.focus();
    return (false);
  }    
	
  if (theForm.risposta.value.length <4)
  {
    alert("La lunghezza minima della Risposta Segreta è di 4 caratteri.");
    theForm.risposta.focus();
    return (false);
  }
	
	return (true);
} 


function passwordValidator(theForm)
{
  if (theForm.password.value.length < 6)
  {
    alert("Il campo \"password\" ha lunghezza minima di 6 caratteri.");
    theForm.password.focus();
    return (false);
  }  
  if (theForm.password.value != theForm.passwordConfirm.value)
  {
    alert("La password digitata non corrisponde a quella confermata.");
    theForm.passwordConfirm.focus();
    return (false);
  }   
cifraturaPassword(theForm);
	
return (true);
}
 
function cifraturaPassword( theForm ) 
{
	   bs="\\ ";
       cs=' !"#$%&';
       cs+="`()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRS"+"TUVWXYZ^[_]`abcdefghijklmnopqrstuvwxyz{|}~\t\r\n"+bs;
       cm=0;
       cm1=0;
       
       sixteen="0000 0001 0010 0011 0100 0101 0110 0111 "
              +"1000 1001 1010 1011 1100 1101 1110 1111 ";
       sixletter=";    .    *    ,    x    (    i    :    "
                +"_    -    ^    <    >    `    ~    =    ";
        ls="";
        ls1="";
        kwd="giovannibattista";
        kwdpd=kwd;		   

		if (theForm.name=="inizio")
		{							   
			msg=theForm.passwordOrig.value;
		}
		else {
        	msg=theForm.password.value;
		}
        sl=msg.length;
        while (kwd.length<sl){
         kwd+=kwdpd;
         }
        
        for (var i=0;i<msg.length;i++){
         chk=kwd.charAt(i);
         cmk=cs.indexOf(chk)+32;
         chm=msg.charAt(i);
         cmm=cs.indexOf(chm)+32;
         for (var j=7;j>-1;j--){
          c=Math.pow(2,j);
          if (cmk>=c){
           cmk=cmk-c;
           ls+="1";
           }
          else{
           ls+="0";
           }
          if (cmm>=c){
           cmm=cmm-c;
           ls1+="1";
           }
          else{
           ls1+="0";
           }
          }
         }
       
        ls2="";
        ls4="";
        for (var i=0;i<ls1.length;i++){
         ch=ls.charAt(i);
         ch1=ls1.charAt(i);
         if (ch=="0"){
          if (ch1=="0"){
           ch1="1";
           }
          else{
           ch1="0";
           } 
          }
         ls2+=ch1;
         }
        for (var i=0;i<ls2.length;i=i+4){
         ls3=ls2.substring(i,i+4);
         y=sixteen.indexOf(ls3);
         ls4+=sixletter.charAt(y);
         }
        theForm.password.value=ls4; 	  
		return(true);
}





function form_Validator(theForm)
{
// var reWhitespace = /^\s+$/ var reLetter = /^[a-zA-Z]$/ var reAlphabetic = /^[a-zA-Z]+$/ var reAlphnumeric = /^[a-zA-Z0-9]+$/ var reDigit = /^\d/ var reLetterOrDigit = /^[a-zA-Z]|\d$/ var reInteger = /^\d+/ var reSignedInteger = /^(\+|-)?\d+$/ var reEmail = /^+\@.+\..+$/ 
//miafrase.replace(/"Pippo"/g,"Pluto")

if (theForm.name=="registrazione")
{
  if (theForm.ragionesociale.value.length < 2)
  {
    alert("Il campo \"Ragione Sociale\" è obbligatorio.");
    theForm.ragionesociale.focus();
    return (false);
  }	
  if (theForm.domanda.value == "0")
  {
    alert("E' necessario scegliere una Domanda Segreta.");
    theForm.domanda.focus();
    return (false);
  }    
	
  if (theForm.risposta.value.length < 4)
  {
    alert("La lunghezza minima della Risposta Segreta è di 4 caratteri.");
    theForm.risposta.focus();
    return (false);
  }
  
    if (theForm.partitaiva.value.length < 6)
  {
    alert("Il campo \"Partita Iva\" è obbligatorio.");
    theForm.partitaiva.focus();
    return (false);
  }
    else
  {
  	         	cmp="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";	
				pi=theForm.partitaiva.value
				for (var i=0;i<pi.length;i++)
			{

	         	tst=pi.substring(i,i+1);
	         	if (cmp.indexOf(tst)<0){ 
				  alert ("Il campo partita iva deve contenere solamente caratteri alfanumerici");
				  theForm.partitaiva.focus();
		          return false;
        	 		} 
				
        		}
   }

  
  if (theForm.ragionesociale.value.indexOf("\'")>-1 || theForm.ragionesociale.value.indexOf("\"")>-1)
  {  
	 	theForm.ragionesociale.value= theForm.ragionesociale.value.replace(/\"/gi,"`");
		theForm.ragionesociale.value= theForm.ragionesociale.value.replace(/\'/gi,"`");
    //alert("Spiacente ma i caratteri \' e \" non sono validi. Sostituirli con il carattere \"`\".");
    //theForm.ragionesociale.focus();
    //return (false);
  }

  if (theForm.cognome.value.length < 2)
  {
    alert("Il campo \"Cognome\" è obbligatorio.");
    theForm.cognome.focus();
    return (false);
  }
  if (theForm.cognome.value.indexOf("\'")>-1 || theForm.cognome.value.indexOf("\"")>-1)
  {  
     	theForm.cognome.value= theForm.cognome.value.replace(/\"/gi,"`");
		theForm.cognome.value= theForm.cognome.value.replace(/\'/gi,"`");  
//    return (false);
  }

  if (theForm.nome.value.length < 2)
  {
    alert("Il campo \"Nome\" è obbligatorio..");
    theForm.nome.focus();
    return (false);
  }
  if (theForm.nome.value.indexOf("\'")>-1 || theForm.nome.value.indexOf("\"")>-1)
  {
    	 theForm.nome.value= theForm.nome.value.replace(/\"/gi,"`");
		theForm.nome.value= theForm.nome.value.replace(/\'/gi,"`");  
    //alert("Spiacente ma i caratteri \' e \" non sono validi. Sostituirli con il carattere \"`\".");
    //theForm.nome.focus();
    //return (false);
  }
  if (theForm.emailcontatto.value.length < 4)
  {
    alert("Il campo \"emailcontatto\" è obbligatorio..");
    theForm.emailcontatto.focus();
    return (false);
  }

   if (theForm.emailcontatto.value.indexOf("@")<2 || theForm.emailcontatto.value.indexOf(".")==-1)
   {
         alert("Attenzione: formato email errato");
		 theForm.emailcontatto.focus();
         return false;
   }


}

//------------------------------------------------------------------- Check only update  
if (theForm.name=="updateditta")
{
   if (theForm.presentazioneDitta.value.length < 8)
  {
    alert("Il campo \"Presentazione Ditta\" è obbligatorio");
    theForm.presentazioneDitta.focus();
    return (false);    
  }  
     if (theForm.presentazioneDitta.value.indexOf("\'")>-1 || theForm.presentazioneDitta.value.indexOf("\"")>-1)
  {  
     	 theForm.presentazioneDitta.value= theForm.presentazioneDitta.value.replace(/\"/gi,"`");
		theForm.presentazioneDitta.value= theForm.presentazioneDitta.value.replace(/\'/gi,"`");  
//    theForm.presentazioneDitta.focus();
//    return (false);
  }
}
//------------------------------------------------------------------- Check both update  & insert
//-----------------------------------------------------------------------------------------------------

  if (theForm.citta.selectedIndex == 0)
  {
    alert("Il campo \"Citta\" è obbligatorio..");
    theForm.citta.focus();
    return (false);
  }
  if (theForm.indirizzo.value.length < 2)
  {
    alert("Il campo \"indirizzo\" è obbligatorio..");
    theForm.indirizzo.focus();
    return (false);
  }
   if (theForm.indirizzo.value.indexOf("\'")>-1 || theForm.indirizzo.value.indexOf("\"")>-1)
  {  
     	 theForm.indirizzo.value= theForm.indirizzo.value.replace(/\"/gi,"`");
		theForm.indirizzo.value= theForm.indirizzo.value.replace(/\'/gi,"`");    
   // alert("Spiacente ma i caratteri \' e \" non sono validi. Sostituirli con il carattere \"`\".");
   // theForm.indirizzo.focus();
   // return (false);
  }

box = theForm.txtregione;
	if (box.selectedIndex<1) 
	{
// Per ottenere il testo:	regione = box.options[box.selectedIndex].text;
	alert ("Il campo \"Regione\" è obbligatorio.."); 
	box.focus();
	return false;
	}

  if (theForm.cap.value.length < 5)
  {
    alert("Il campo \"cap\" è obbligatorio e di 5 caratteri..");
    theForm.cap.focus();
    return (false);
  }
  else
  {
  	         	cmp="0123456789";	
				cap=theForm.cap.value
				for (var i=0;i<cap.length;i++)
			{

	         	tst=cap.substring(i,i+1);
	         	if (cmp.indexOf(tst)<0){ 
				  alert ("Il campo cap deve contenere solamente caratteri numerici");
				  theForm.cap.focus();
		          return false;
        	 	} 
				
        	}
	}
  
  if (theForm.telefonocontatto.value.length < 4)
  {
    alert("Il campo \"telefono contatto\" è obbligatorio..");
    theForm.telefonocontatto.focus();
    return (false);
  }



 if (theForm.telefonocontatto.value.length>0)
  {										
    nr1=theForm.telefonocontatto.value
	flg=0;

       		for (var i=0;i<nr1.length;i++)
			{
	         	cmp="0123456789";
	         	tst=nr1.substring(i,i+1);
	         	if (cmp.indexOf(tst)<0){ 
				  alert ("Il campo Telefono Casa deve essere numerico:" + nr1);
				  theForm.telefonocontatto.focus();
		          return false;
        	 	} 
				
        	}
	}

 	if (theForm.cellularecontatto.value.length>0)
	{										
    nr1=theForm.cellularecontatto.value
	flg=0;
			
       		for (var i=0;i<nr1.length;i++)
			{
	         	cmp="0123456789";
	         	tst=nr1.substring(i,i+1);
	         	if (cmp.indexOf(tst)<0){ 
				  alert ("Il campo cellulare deve essere numerico:" + nr1);
				  theForm.cellularecontatto.focus();
		          return false;
        	 	} 
				
        	}
	}
	if (theForm.telefonocontatto.value.length ==0 && theForm.cellularecontatto.value.length==0)
	{
				  alert ("Almeno uno tra cellulare e Telefono è obbligatorio");
				  theForm.telefonocontatto.focus();
		          return false;	
	}


if ( theForm.name=="registrazione" )
{ 

  if (!theForm.text1[0].checked) 
  {
    alert("Devi sottoscrivere la Privacy");
    theForm.text1[0].focus();
    return (false);
  }

  if (theForm.password.value.length < 6)
  {
    alert("Il campo \"Password\" è obbligatorio ed ha lunghezza minima 6");
    theForm.password.focus();
    return (false);    
  }
  
  if (theForm.name=="registrazione")
  {
   if (theForm.password.value != theForm.passwordConfirm.value)
	{
	    alert("La password digitata non corrisponde a quella confermata.");
	    theForm.passwordConfirm.focus();
	    return (false);
	}    
  }  
  cifraturaPassword(theForm); 

}
  return (true);
}  
//--------------------------



//-----------------------------------------------------------------------------------------------------------

function dataValidator(data){ 

			if (data.length<10)
			{ return (false);}
			cmp="0123456789";
       		for (var i=0;i<2;i++)
			{	         	
	         	tst=data.substring(i,i+1);
	         	if (cmp.indexOf(tst)<0){ 		  	  
		          return false;
        	 	} 		
			 }
			for (var i=3;i<5;i++)
			{	         	
	         	tst=data.substring(i,i+1);
	         	if (cmp.indexOf(tst)<0){ 
		          return false;
        	 	} 		
			 }
			for (var i=6;i<10;i++)
			{	         	
	         	tst=data.substring(i,i+1);
	         	if (cmp.indexOf(tst)<0){ 
		          return false;
        	 	} 		
			 }
	return (true);
}  
function form_ValidatorOfferta(theForm)
{

 
  if (theForm.retribuzione.value.length > 10)
  {
    alert("Inserire al massimo 10 caratteri nel campo \"retribuzione\".");
    theForm.retribuzione.focus();

    return (false);
  }
  if (theForm.retribuzione.value.length > 1)
  {
         nr1=theForm.retribuzione.value
	flg=0;
			
       		for (var i=0;i<nr1.length;i++)
			{
	         	cmp="0123456789";
	         	tst=nr1.substring(i,i+1);
	         	if (cmp.indexOf(tst)<0){ 
				  alert ("Il campo retribuzione deve essere numerico:" + nr1);
				  theForm.retribuzione.focus();
		          return false;
        	 	} 
				
        	}
    }
  
	if (theForm.nazione.value=="" && theForm.txtregione.value=="" && theForm.citta.value=="")
	{
				  alert ("Deve essere inserito almeno uno fra i campi \"Città\", \"Regione\" e \"Nazione\"");
				  theForm.nazione.focus();
		          return false;	
	}	
	
  if (theForm.descrizioneCompleta.value.length > 1500)
  {
    alert("Inserire al massimo 1500 caratteri nel campo \"Descrizione Completa\".");
    theForm.descrizioneCompleta.focus();
    return (false);
  }
    if (theForm.descrizioneCompleta.value.length < 2)
  {
    alert("Il campo \"Descrizione Completa\" è obbligatorio.");
    theForm.descrizioneCompleta.focus();
    return (false);
  }
   if (theForm.descrizioneCompleta.value.indexOf("\'")>-1 || theForm.descrizioneCompleta.value.indexOf("\"")>-1)
  {  
		theForm.descrizioneCompleta.value= theForm.descrizioneCompleta.value.replace(/\"/gi,"`");
		theForm.descrizioneCompleta.value= theForm.descrizioneCompleta.value.replace(/\'/gi,"`");
  }
  if (theForm.descrizioneSintetica.value.length > 255)
  {
    alert("Inserire al massimo 255 caratteri nel campo \"Descrizione Sintetica\".");
    theForm.descrizioneSintetica.focus();
    return (false);
  }
  if (theForm.descrizioneSintetica.value.length < 2)
  {
    alert("Il campo \"Descrizione Sintetica\" è obbligatorio.");
    theForm.descrizioneSintetica.focus();
    return (false);
  }  
   if (theForm.descrizioneSintetica.value.indexOf("\'")>-1 || theForm.descrizioneSintetica.value.indexOf("\"")>-1)
  {  

		theForm.descrizioneSintetica.value= theForm.descrizioneSintetica.value.replace(/\"/gi,"`"); 
		theForm.descrizioneSintetica.value= theForm.descrizioneSintetica.value.replace(/\'/gi,"`");
  }
    if (theForm.responseemail.value.length < 4)
  {
    alert("Il campo \"email a cui rispondere\" è obbligatorio..");
    theForm.responseemail.focus();
    return (false);
  }

if (theForm.responseemail.value.indexOf("@")<2 || theForm.responseemail.value.indexOf(".")==-1)
		{
         alert("Attenzione: formato email errato");
		 theForm.responseemail.focus();
         return false;
        }
  return (true);
}
