function isnumerictel(str)
   
   {
        var len= str.length;
        if (len==0)
        return false;
         //else
        var p=0;
        var ok= true;
        var ch= "";
         while (ok && p<len)
         {
               ch= str.charAt(p);
               if (('0'<=ch && ch<='9') || ch =='/' || ch =='-')
               p++;
         else
          ok= false;
         }
          return ok;
   } 


function test(src) 
{
	if (document.lettera.email.value == "") 
  	{
    		return true;
  	}
	else 
	{
  		var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
  		return regex.test(src);
	}
}


function isnumeric(str)
{
	var len= str.length;
  	if (len==0)
    	return false;
	var p=0;
  	var ok= true;
  	var ch= "";
  	while (ok && p<len)
  	{
    		ch= str.charAt(p);
    		if ('0'<=ch && ch<='9')
      			p++;
    		else
      			ok= false;
  	}
  	return ok;
}

//funzione trim che elimina gli spazi
  function trim(strText) {  
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);
    return strText;
  } 
  
//funzione che setta il focus sul primo campo
   function setfocus() {
   document.lettera.nome.focus();
   return;
  
 }
	
    
//*********Funzione per il controllo dell'obbligatorietà dei campi e sulle lunghezze
function cont()
{
  str = "";
  ok = true;

//controllo sul nome-------------------
  if ((document.lettera.nome.value=="") && ok)
  {
     ok = false;
     str = "Inserire il nome"
  } 
  else 
  {
      if ((document.lettera.nome.value.length > 100) && ok)
      {
      ok = false;
      str = "Il campo nome  contiene un numero elevato di caratteri"
      }

  }

//Fine controllo sul nome-------------------
//controllo sul cognome-------------------
  if ((document.lettera.cognome.value=="") && ok)
  {
     ok = false;
     str = "Inserire il cognome"
  } 
  else 
  {
      if ((document.lettera.cognome.value.length > 100) && ok)
      {
      ok = false;
      str = "Il campo cognome  contiene un numero elevato di caratteri"
      }

  }

//Fine controllo sul cognome-------------------
if ((!isnumeric(document.lettera.cap.value))&&(document.lettera.cap.value!="") && ok)
   {
      ok = false;
      str = "Inserire solo caratteri numerici per il Cap"
   }
   else 
   {
      if ((document.lettera.cap.value.length > 18) && ok)
      {
         ok = false;
         str = "Hai superato il numero di caratteri disponibili per il campo Cap"
      }
   } 
if ((!isnumerictel(document.lettera.tel.value))&&(document.lettera.tel.value!="") && ok)
   {
      ok = false;
      str = "Inserire solo caratteri numerici nel campo Telefono (Es:011/12345 o 011-12345)"
   }
   else 
   {
      if ((document.lettera.tel.value.length > 18) && ok)
      {
         ok = false;
         str = "Hai superato il numero di caratteri disponibili per il campo Telefono"
      }
   } 
   if ((!isnumerictel(document.lettera.fax.value))&&(document.lettera.fax.value!="") && ok)
   {
      ok = false;
      str = "Inserire solo caratteri numerici nel campo Fax (Es:011/12345 o 011-12345)"
   }
   else 
   {
      if ((document.lettera.fax.value.length > 18) && ok)
      {
         ok = false;
         str = "Hai superato il numero di caratteri disponibili per il campo Fax"
      }
   } 
//Controllo sul Campo Email-----------------
 document.lettera.email.value=trim(document.lettera.email.value);
	 if ((document.lettera.email.value=="") && ok)
   {
      ok = false;
      str = "Il Campo E-mail è obbligatorio"
   } 
   else 
   { 

if (!test(document.lettera.email.value) && ok)
   {
      ok = false;
      str = "Attenzione!I caratteri nel campo E-mail non sono corretti!"
   }
   else 
   {
      if ((document.lettera.email.value.length > 100) && ok)
      {
         ok = false;
         str = "Hai superato il numero di caratteri disponibili per il campo E-mail"
      }
   } 
}

//fine controllo sul campo email-------------------
return str;
} 

function controllo()
{ 

c = cont();
if (c=="")
{
 document.lettera.action="./spedizione.php";
 document.lettera.submit();
}
else
{
 alert(str)
}
}

function annulla()
{ 


 document.lettera.action="./index.php";
 document.lettera.submit();

}

function esci()
{
history.back();
}

     







 