
//**********************
// Validità Campo Empty
//**********************
function TestEmpty (Ctrl,msg)
 {if (Ctrl.value == "")
   {alert (msg);
    Ctrl.focus();
    return (false);
   }
  else
   return (true);   
 }

function TestEmail (Ctrl)
 {
  if (Ctrl.value == "")
   {alert ("Inserire l'e-mail!");
    Ctrl.focus();
    return (false);
   }
    
  //deve esserci una sola @
  pos=Ctrl.value.indexOf('@', 0)
  if (pos == 0)
   {alert ("Inserire correttamente l'e-mail!");
    Ctrl.focus();
    return (false);
   }

  if (pos == -1) 
   {alert ("Inserire correttamente l'e-mail!");
    Ctrl.focus();
    return (false);
   }
  else
   {if (Ctrl.value.indexOf('@', pos+1) != -1)
    {alert ("Inserire correttamente l'e-mail!");
     Ctrl.focus();
     return (false);
    }
   }

  //devono esserci almeno 4 caratteri dopo @
  if (Ctrl.value.substring(pos+1,Ctrl.value.length).length < 4)
   {alert ("Inserire correttamente l'e-mail!");
    Ctrl.focus();
    return (false);
   }

  //deve esserci almeno un . dopo @
  if (Ctrl.value.indexOf('.', pos+1) == -1)      
   {alert ("Inserire correttamente l'e-mail!");
    Ctrl.focus();
    return (false);
   }

  //devono esserci almeno 2 caratteri dopo .
  pos=Ctrl.value.indexOf('.', pos+1);
  if (Ctrl.value.substring(pos+1,Ctrl.value.length).length < 2)
   {alert ("Inserire correttamente l'e-mail!");
    Ctrl.focus();
    return (false);
   }

  //caratteri ammessi
  stremail=Ctrl.value.toUpperCase()
  for (var i = 0; i < stremail.length; i++)
   {if (".-_0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(stremail.charAt(i)) == -1) 
    {alert ("Inserire correttamente l'e-mail!");
     Ctrl.focus();
     return (false);
    }
   }
  //Tutto OK
  return (true);   
 }


// Validità trattamento
// =============
function TestTrattamento()
  {
   if (document.form1.autorizzo.checked) return (true)
     alert('Per inviare la richiesta è necessario acconsentire al trattamento dei dati personali!');
     document.form1.autorizzo.focus();
     return (false);
  }

function runSubmit()
  {
   //Eventuali test sull'input
	 
	 if (!TestEmpty(document.form1.azienda,'Il campo Ragione Sociale è obbligatorio !')) return;
     if (!TestEmpty(document.form1.localita,'Il campo Località è obbligatorio !')) return;
	 if (!TestEmail(document.form1.email)) return;
	 if (!TestEmpty(document.form1.messaggio,'Il campo Richiesta è obbligatorio !')) return;
	 if (!TestTrattamento()) return;
	 document.form1.submit();
  }
