var xmlHttp
  function registra()
  {
    form = document.form2;

     var nome = form.nome.value;
	 var pi = form.pi.value;  
	 var via = form.via.value;
	 var numciv = form.numciv.value;
	 var citta = form.citta.value; 
	 var cap = form.cap.value;
	 var provincia = form.prov.value;
	 var stato = form.stato.value;
	 var tel = form.tel.value;
	 var mail = form.mail.value;
	
		 

//--------------------------controllo inserimento nome

     if (nome=="")
     {
        alert("Campo nome obbligatorio.");
        form.nome.value=""
        form.nome.focus()
        return false;
      }
	  
//--------------------------controllo inserimento p IVA  
	  
	   if( pi=="")  alert("Campo partita IVA obbligatorio.");
	if( pi.length != 11 ){
		alert("La lunghezza della partita IVA non è\n" +
			"corretta: la partita IVA dovrebbe essere lunga\n" +
			"esattamente 11 caratteri.\n"); return;}
	validi = "0123456789";
	for( i = 0; i < 11; i++ ){
		if( validi.indexOf( pi.charAt(i) ) == -1 ){
			alert("La partita IVA contiene un carattere non valido `" +
				pi.charAt(i) + "'.\nI caratteri validi sono le cifre.\n");return;}
	}
	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) ){
		alert("La partita IVA non è valida:\n" +
			"il codice di controllo non corrisponde.\n");
	return;}
	   

// ----------------------- controllo checkbox

     if(!form.rivenditore.checked && !form.associazione.checked )
	 
        {
         alert("Per registrarsi al sito bisogna essere rivenditore o associazione!")
         return;
       }
	   
	    if(form.rivenditore.checked && form.associazione.checked )
	 
        {
         alert("Bisogna selezionare una sola tra le opzioni: Rivenditore o Associazione!")
         return;
       }
	   
if(form.rivenditore.checked){
	
	form.rivenditore.value = 1;
	
	}
	
if(form.associazione.checked){
	
	form.associazione.value = 1;
	
	}	


// ----------------------- controllo via
  
    viaRE =/[a-zA-Z]{2,20}$/   
    if ( !form.via.value.match(viaRE) )
      {
         alert("Il campo via contiene errori!")
         form.via.value=""
         form.via.focus()
         return;
       }


// ----------------------- controllo città
  
    citRE =/^[òàùèìa-zA-Z''-'\s]{1,50}$/
    if ( !form.citta.value.match(citRE) )
      {
         alert("Il campo citta contiene errori!")
         form.citta.value=""
         form.citta.focus()
         return;
       }


// ----------------------- controllo cap
  
    capRE =/^[0-9]{4,20}$/  
    if ( !form.cap.value.match(capRE) )
      {
         alert("Il campo CAP contiene errori!")
         form.cap.value=""
         form.cap.focus()
         return;
       }



    
// ----------------------- controllo provincia
  
    proRE =/^[òàùèìa-zA-Z''-'\s]{1,50}$/
    if ( !form.prov.value.match(proRE) )
      {
         alert("Il campo provincia contiene errori!")
         form.prov.value=""
         form.prov.focus()
         return;
       }
	   
// ----------------------- controllo stato
  
    statRE =/^[òàùèìa-zA-Z''-'\.?\s]{1,50}$/
    if ( !form.stato.value.match(statRE) )
      {
         alert("Lo stato contiene errori!")
         form.stato.value=""
         form.stato.focus()
         return;
       }

//controllo telefono
   
  telRE =pre= /^\d+\.?\-?\/?\d*$/
    if ( !form.tel.value.match(telRE) )
     {
        alert("Il campo telefono contiene errori!");
        form.tel.value=""
        form.tel.focus()
        return false;
		
		
      }   
	  
//controllo mail

  
      var r = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
  
      if(!r.test(mail) ){
 
      alert("e-mail non valida!");
	  form.mail.value=""
      form.mail.focus()
      return false;
		
		
      }   

var nickname = form.us.value;
     
     


  if ((nickname == "") || (nickname == "undefined")) {
           alert("Campo username  obbligatorio.");
           form.us.value=""
           form.us.focus()
           return false;
        }


var password = form.pw.value;




          if ((password == "") || (password == "undefined")) {
           alert("Campo password obbligatorio.");
            form.pw.value=""
           form.pw.focus()
           return false;
            }


         
        if (password.length<8) {
           alert("Per ragioni di sicurezza,la password deve avere una lunghezza di almeno 8 caratteri alfanumerici.");
           form.pw.value=""
           form.pw.focus()
           return false;
        }



var conferma = form.conferma.value;
        if ((conferma == "") || (conferma == "undefined")) {
           alert("Campo conferma password obbligatorio.");
            form.conferma.value=""
           form.conferma.focus()
           return false;
        }




if (password != conferma) {
           alert("La password confermata risulta diversa da quella scelta; controllare.");
          form.conferma.value = ""
           form.conferma.focus()
           return false;
        }  



// ----------------------- controllo checkbox

     if(!form.agree.checked)
	 
        {
         alert("Per registrarsi al sito bisogna acconsentire al trattamento dei dati personali!")
         return;
       }
	   
	    if(!form.agree1.checked)
	 
        {
         alert("Per registrarsi al sito bisogna acconsentire alle condizioni di vendita!")
         return;
       }
	   


var scelta = confirm("I dati sono corretti. Confermi l'invio?")
   
if (scelta){

	
var url="../smarty/AggiungiUtente.php"
    var dati_post = "nome=" +  
                    escape( document.getElementById("nome").value )+  
                     "&pi=" +  
                   escape( document.getElementById("pi").value )+
                    "&via=" +  
                    escape( document.getElementById("via").value )+
                     "&numciv=" +  
                    escape( document.getElementById("numciv").value )+  
                     "&citta=" +  
                    escape( document.getElementById("citta").value )+   
                     "&cap=" +  
                    escape( document.getElementById("cap").value )+   
                     "&prov=" +  
                    escape( document.getElementById("prov").value )+   
                    "&stato=" +  
                    escape( document.getElementById("stato").value )+ 
                    "&tel=" +  
                    escape( document.getElementById("tel").value )+
                    "&mail=" +  
                    escape( document.getElementById("mail").value )+
                    "&us=" +  
                    escape( document.getElementById("us").value )+
                    "&pw=" +  
                    escape( document.getElementById("pw").value )+
                    "&rivenditore=" +  
                    escape( document.getElementById("rivenditore").value )+
                    "&associazione=" +  
                    escape( document.getElementById("associazione").value );   
               
      
     xmlHttp=GetXmlHttpObject(stateChanged)
     xmlHttp.open("POST", url , true)
     xmlHttp.setRequestHeader("content-type", "application/x-www-form-urlencoded")
     xmlHttp.setRequestHeader("connection", "close")
     xmlHttp.send(dati_post)

     }
}
function stateChanged() { 

   if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") { 
      document.getElementById("center_content").innerHTML = xmlHttp.responseText 
   } 

}
 

function GetXmlHttpObject(handler) { 
   
   var objXmlHttp=null
   if (navigator.userAgent.indexOf("Opera")>=0) {
      alert("This example doesn't work in Opera") 
      return 
   }
   if (navigator.userAgent.indexOf("MSIE")>=0) { 
      var strName="Msxml2.XMLHTTP"
      if (navigator.appVersion.indexOf("MSIE 5.5")>=0) {
         strName="Microsoft.XMLHTTP"
      } 
      try { 
         objXmlHttp=new ActiveXObject(strName)
         objXmlHttp.onreadystatechange=handler 
         return objXmlHttp
      } 
      catch(e) { 
         alert("Error. Scripting for ActiveX might be disabled") 
         return 
      } 
   } 
   if (navigator.userAgent.indexOf("Mozilla")>=0) {
      objXmlHttp=new XMLHttpRequest()
      objXmlHttp.onload=handler
      objXmlHttp.onerror=handler 
      return objXmlHttp
   }
} 




