
//-------------------------------------------------------------------------------------------------------//

function ValidLength(str,number)
{
  if (str < number)
    return false;
  return true;
}

function ValidChars(CheckStr,chars)
{
  for (i = 0;  i < CheckStr.length;  i++)
  {
    ch = CheckStr.charAt(i);
    for (j = 0;  j < chars.length;  j++)
      if (ch == chars.charAt(j))
        break;
      if (j == chars.length)
        return false;
  }
  return true;
}  

function ValidString(CheckStr,RepeatNum)
{
  if (CheckStr.length >= RepeatNum)
  {
    for (k = 0; k < CheckStr.length - 2; k++)
    {
      l = k+1;
      if  (CheckStr.charAt(k) == CheckStr.charAt(l))
      {
        m = l+1;
        if (CheckStr.charAt(k) == CheckStr.charAt(m))
        {
          return false;
          break;
        }
        else
        {
          k++;
        };
      }  
    }
  }
  return true;
}

  
function ValidVowel(checkStr)
{
  checkVowel = "AEIOUYaeiouy";
  for (z = 0; z < checkStr.length; z++)
  {
    for (a = 0; a < checkVowel.length; a++)
    {
      if (checkStr.charAt(z) == checkVowel.charAt(a))
      {  
        return true;
      } 
    }
  }
  return false;
}

function ValidNumber (checkStr)
{
  checkNumber = "0123456789";
  for (z = 0; z < checkStr.length; z++)
  {
    for (a = 0; a < checkNumber.length; a++)
    {
      if (checkStr.charAt(z) == checkNumber.charAt(a))
      {  
        return true;
      } 
    }
  }
  return false;
}  



//--------------Main Procedure------------------------------------------------------------------------------//


function Valid(theForm)
{

  /*------------------ Begin Nome e Cognome Validation ----------------*/
  
  var ValidNameChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-@()[]?!ì^ùàòè._:;,<>=&%$£/|*\ 1234567890 ";
  var FormLName = theForm.cognome.value;
  if (!ValidLength(FormLName.length, 1))
  {
    alert ("Inserisci il Cognome.");
    theForm.cognome.focus();
    return false;
  }
  if (!ValidChars(FormLName, ValidNameChars))
  {
    alert("Il cognome non è valido!");
    theForm.cognome.focus();
    return false;
  }
  if (!ValidString(FormLName,3))
  {
    alert("Il cognome non è valido!");
    theForm.cognome.focus();
    return false;
  }
  if (!ValidVowel(FormLName))
  {
    alert("Il cognome non è valido!");
    theForm.cognome.focus();
    return false;
  }
  var FormLName = theForm.nome.value;
  if (!ValidLength(FormLName.length, 1))
  {
    alert ("Inserisci il Nome.");
    theForm.nome.focus();
    return false;
  }
  if (!ValidChars(FormLName, ValidNameChars))
  {
    alert("Il Nome non è valido!");
    theForm.nome.focus();
    return false;
  }
  if (!ValidString(FormLName,3))
  {
    alert("Il Nome non è valido.");
    theForm.nome.focus();
    return false;
  }
  if (!ValidVowel(FormLName))
  {
    alert("Il Nome non è valido.");
    theForm.nome.focus();
    return false;
  } 

  /*--------------------- End Nome e Cognome Validation ----------------------/

  
  /-----------------------Begin Indirizzo Validation -------------------*/

  var FormAddr1 = theForm.indirizzo.value ;
  
  if (!ValidLength(FormAddr1.length, 5))
  {
    alert ("Inserisci un indirizzo valido.");
    theForm.indirizzo.focus();
    return false;
  }  
  
  var FormAddr2 = theForm.citta.value ;
  
  if (!ValidLength(FormAddr2.length, 1))
  {
    alert ("Inserisci una città valida.");
    theForm.citta.focus();
    return false;
  }    
  
  var FormAddr3 = theForm.provincia.value ;
  
  if (!ValidLength(FormAddr3.length, 2))
  {
    alert ("Inserisci una provincia valida.");
    theForm.provincia.focus();
    return false;
  }    
 
  /*------------------ End Indirizzo Validation --------------------/


/------------------ Begin Telefono Validation -------*/

  var FormTel = theForm.telefono.value
  var telNums = "0123456789";
    
if (FormTel.length == 0)
{
    alert("Inserisci il numero di telefono!");
    theForm.telefono.focus();
    return false;
}

    if (!ValidChars(FormTel,telNums))
    {
      alert("Inserisci un Numero di Telefono Valido!");
      theForm.telefono.focus();
      return false;
    }


  /*-------------- End Telefono Validation ---------------*/


  var FormLnote = theForm.note.value;
  if (!ValidLength(FormLnote.length, 1))
  {
    alert ("Inserisci la tua richiesta.");
    theForm.note.focus();
    return false;
  }

if (FormLnote.length > 400)
{
    alert("La richiesta può contenere al massimo 400 caratteri.");
    theForm.note.focus();
    return false;
}

  return true;  
}  

//-->
