//***********************************************//
//*** Script by <Stefano Roncari> - Alambitco ***//
//***********************************************//

var sendable = false;
var counter = 0;

campi = new Array
(
   new Array("uidReq","User ID"),
   new Array("passwordReq","Password"),
   new Array("password2Req","Conferma Password"),
   new Array("nomeReq","Nome"),
   new Array("cognomeReq","Cognome"),
   new Array("tel","Telefono"),
   new Array("emailReq","Email"),
   new Array("sessoReq","Sesso"),
   new Array("fasciaEtaReq","Fascia d'età"),
   new Array("professioneReq","Professione"),
   new Array("arcadiaCard","Codice Arcadia Card"),
   new Array("acconsentoReq","Autorizzazione al trattamento dei dati personali")
)

function checkForm()
{
   var emailRequired = new Array("@",".");

//*** controllo campi obbligatori ***//
   for (i=0; i<campi.length; i++)
   {
       tempFormObj = eval("document.registerForm." +campi[i][0]);
       nameLength = campi[i][0].length;
       if ((campi[i][0].indexOf("Req")!=-1) && ((tempFormObj.type == 'text') || (tempFormObj.type == 'password')) && (tempFormObj.value == ''))
       {
          if (campi[i][0] == "localitaReq" && campi[i][0] == "provinciaReq")
          {
             //
          }
          else
          {
            alert('Attenzione: il campo "'+campi[i][1]+'" è obbligatorio.');
            tempFormObj.focus();
            return false;
          }
       }

       else if ((campi[i][0] == "uidReq") && (tempFormObj.value.length <4))
       {
          alert("Attenzione: lo UserID deve essere composto da almeno 4 caratteri!");
          tempFormObj.select();
          tempFormObj.focus();
          return false;
       }
       
       else if ((campi[i][0] == "passwordReq") && (tempFormObj.value.length <4))
       {
          alert("Attenzione: la Password deve essere composta da almeno 4 caratteri!");
          tempFormObj.select();
          tempFormObj.focus();
          return false;
       }

       if ((campi[i][0] == "password2Req") && (tempFormObj.value != document.registerForm.passwordReq.value))
       {
          alert("Attenzione: le due password indicate non coincidono!");
          document.registerForm.passwordReq.select();
          document.registerForm.passwordReq.focus();
          return false;
       }
       
       if ((campi[i][0] == "emailReq"))
       {
           for (x=0; x<emailRequired.length; x++)
           {
               if (tempFormObj.value.indexOf(emailRequired[x]) == -1)
               {
                  alert("Attenzione: l'indirizzo E-MAIL non è corretto.");
                  tempFormObj.select();
                  tempFormObj.focus();
                  return false;
               }
           }
       }
       
       if (campi[i][0] == "sessoReq" && tempFormObj[0].status == false && tempFormObj[1].status == false)
       {
             alert('Attenzione: il campo "'+campi[i][1]+'" è obbligatorio.');
             tempFormObj[0].focus();
             return false;
       }
       
       if (((campi[i][0] == "fasciaEtaReq") || (campi[i][0] == "professioneReq")) && (tempFormObj.selectedIndex == 0))
       {
          alert('Attenzione: il campo "' +campi[i][1]+ '" è obbligatorio!');
          tempFormObj.focus();
          return false;
       }

       if ((campi[i][0] == "arcadiaCard") && (tempFormObj.value != ''))
       {
          if (document.registerForm.tel.value =='')
          {
             alert('Per offrire un servizio ottimale ai possessori di ArcadiaCard \nabbiamo bisogno anche del loro numero di telefono');
             document.registerForm.tel.focus();
             return false;
          }
             re = /^[A-Z]{1}[0-9]{5}$/g; // deve essere costituito da una lettera iniziale, seguita da 5 cifre.
             stringa=tempFormObj.value.toUpperCase();
             //alert(re.test(stringa));
             if (re.test(stringa) == false)
             {
                alert("Attenzione, il codice ArcadiaCard inserito non è valido.\nDeve essere composto da una lettera iniziale, seguita da 5 numeri.");
                tempFormObj.focus();
                tempFormObj.select();
                return false;
             }
       }

       if (campi[i][0] == "acconsentoReq" && tempFormObj[0].status == false)
       {
          alert("Attenzione: è necessario acconsentire al trattamento dei \ndati personali per poter procedere nella registrazione");
          return false;
       }
   }
   // questa variabile serve per far decidere alla funzione sendForm() se fare il submit oppure no.
   sendable = true;
}


// Non ho potuto fare il submit del Form direttamente qui sopra (nella funz. checkForm()) perché se nell'HRef metto solo la funzione checkForm (che fa da sola il submit),
// allora quando c'è un "Return false" il "false" viene tornato appunto nell'Href, nell'URL, sicché si apre una pagina vuota con scritto "False".
// Se invece nell'HRef richiamo un'altra funzione DOPO la checkForm() [ad es. href="checkForm();sendForm();"], allora il "false" viene tornato
// non all'Href, ma a Javascript.
// D'altra parte, non potevo richiamare la funzione del Submit nell'Onclick (che quindi non avrebbe dato problemi di "return false") perché IE
// permette di fare il submit via Js solo se lo si richiama direttamente nell'attributo Href (e non in un EventHandler).
function sendForm()
{
   if (sendable)
   {
      document.registerForm.submit();
   }
}