//***********************************************//
//*** Script by <Stefano Roncari> - Alambitco ***//
//***********************************************//

var sendable = false;

function checkForm(name)
{
   var emailRequired = new Array("@",".");
   theForm = eval('document.' +name);

//*** controllo campi obbligatori ***//
   for (i=0; i<theForm.length; i++)
   {
       var tempFormObj=theForm.elements[i];
       nameLength = tempFormObj.name.length;
       shortFieldName=tempFormObj.name.substring(0,nameLength-3).toUpperCase();

       if (tempFormObj.name.substring(nameLength-3,nameLength)=="Req") // Se le ultime tre lettere sono "Req"
       {
            if ((tempFormObj.type == "text") && (tempFormObj.value == ""))
            {
               alert("Attenzione: il campo \""+shortFieldName+"\" e' obbligatorio.");
               tempFormObj.focus();
               return false;
            }
            
            if ((tempFormObj.type == "textarea") && (tempFormObj.value == ""))
            {
               alert("Attenzione: il campo \""+shortFieldName+"\" e' obbligatorio.");
               tempFormObj.focus();
               return false;
            }
            
            if ((tempFormObj.type == 'select-one') && (tempFormObj.name == 'chiReq') && (tempFormObj.selectedIndex == 0))
            {
               alert("Attenzione: e' necessario selezionare un destinatario");
               tempFormObj.focus();
               return false;
            }
       
//*** se "email" è obbligatorio: controllo caratteri speciali ***//
            if ((tempFormObj.name == "emailReq"))
            {
                for (x=0; x<emailRequired.length; x++)
                {
                    if (tempFormObj.value.indexOf(emailRequired[x]) == -1)
                    {
                       alert("Attenzione: l'indirizzo E-MAIL non e' corretto.");
                       tempFormObj.select();
                       tempFormObj.focus();
                       return false;
                    }
                }
            }
       }

//*** se "email" NON è obbligatorio: controllo caratteri speciali ***//
       else if ((tempFormObj.name == "email") && (tempFormObj.value != 0))
       {
           for (x=0; x<emailRequired.length; x++)
           {
               if (tempFormObj.value.indexOf(emailRequired[x]) == -1)
               {
                    alert("Attenzione: l'indirizzo E-MAIL non e' corretto.");
                    tempFormObj.select();
                    tempFormObj.focus();
                    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)
   {
      theForm.submit();
   }
}