function chk(dt) {
    control=true
    if (dt.action[1].checked == false) {
        if (dt.nick.value == "") {alert('Inserire un NOME');control=false; return;}
		if (dt.cognome.value == "") {alert('Inserire un COGNOME');control=false; return;}	
	}
	if (dt.mail.value == "") {alert('Inserire una MAIL');control=false; return;}
	if (!validitaEmail(dt.mail)) {control=false; return;}
	if (dt.action[1].checked == false)
	{
		if (dt.sesso[0].checked == false && dt.sesso[1].checked == false) {alert('Scegliere un sesso!');control=false; return;}
		if ((dt.cell.value != "") && (dt.cell.value != "39")){
					if (!validitaCell(dt.cell.value)) {control=false; return;}
		}
		if ((dt.eta.value!=""))
		{
				if (IsNumeric(dt.eta.value)==false){alert("L'eta deve contenere solo numeri!");control = false;return;}
		}
		if ((dt.prov.value!=""))
		{
				if (IsText(dt.prov.value)==false){alert("La provincia deve contenere solo lettere!");control = false;return;}
		}
		if ((dt.cap.value!=""))
		{
				if (IsNumeric(dt.cap.value)==false){alert("Il CAP deve contenere solo numeri!");control = false;return;}
		}
	   if ((dt.telefono.value!=""))
		{
				if (IsNumeric(dt.telefono.value)==false){alert("Il numero di telefono deve contenere solo numeri!");control = false;return;}
		}
		if ((dt.fax.value!=""))
		{
				if (IsNumeric(dt.fax.value)==false){alert("Il fax deve contenere solo numeri!");control = false;return;}
		}
	}
    if (control) {
       if (!chkR) {alert('Leggi l\'informativa Privacy!');control=false; return;}
       else {
        dt.mail.value = stripBlanks(dt.mail.value)    
        document.DATI.submit();
       }
    }
	else return;  //if control is false 
}

function validitaCell(cell){
	control = true;
	if ((cell.slice(0,2)!= "39")||(cell.length!=12))
	{
				alert("Dopo il 39 per l'Italia, inserire il prefisso (es.349)\n poi il numero di cellulare (es. 393491234567");
		control = false;
		return control;
	}
	if (IsNumeric(cell)==false)
		{
			alert("Il cellulare deve contenere solo numeri!");
			control = false;
		}
	return control;
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }

function IsText(sText)
{
   var ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
   var IsText=true;
   var Char;
   for (i = 0; i < sText.length && IsText == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsText = false;
         }
      }
   return IsText;
 }

function validitaEmail(mail)
{
  contr = true;
  var strEmail = '';
  strEmail = String(mail.value).toLowerCase();
  strEmail = stripBlanks(strEmail);
  vir=strEmail.indexOf(',')
  if (vir != -1) {
 		alert( 'Indirizzo e-Mail non valido:\n hai inserito una virgola \n Carattere non valido' );
        contr = false;
        return contr;
  }
  pos = strEmail.indexOf( '@' )
  if( pos != -1)
  {
    pos2 = strEmail.indexOf('.', pos)
	if (pos2 == -1) {
    		alert( 'Indirizzo e-Mail non valido:\manca il punto dopo la chiocciola' );
    		contr = false;
    	}
  }
  else if(pos == -1){
    alert( 'Indirizzo e-Mail non valido:\n manca @' );
    contr = false;
  }
  return contr;
}
  function stripBlanks(str)
  {
    // elimina tutti i caratteri blank dall'inizio della stringa
    while ( str.charAt(0) == ' ' )
    str = str.substr(1);
    // elimina tutti caratteri blank dalla fine della stringa
    while ( str.charAt( str.length-1 ) == ' ' )
    str = str.substr ( 0,str.length-1);
    

        p='';
        q=str.length
        for (x=0; x<q; x++) {
            z='';
            i=str.indexOf(' ');
            if (i >= 0) {
                z=z+str.substring(0,i);
                a=str.substring(i,i+1);
                    str = str.substring(str.indexOf(' ')+1, q);
                    p=p+z
            }
            else {
                p=p+str
                break;
            }
        }
      return p;
  }

