// JavaScript Document
function ctrl(emailStr) {
	var f = document.FormName
	if (f.msg_nome.value == "") {
		alert("E' necessario inserire un valore nel campo NOME.")
		f.msg_nome.focus();
		return false;
	}
	if (f.msg_telefono.value == "") {
		alert("E' necessario inserire un valore nel campo TELEFONO.")
		f.msg_telefono.focus();
		return false;
	}
	if (f.msg_azienda.value == "") {
		alert("E' necessario inserire un valore nel campo AZIENDA.")
		f.msg_azienda.focus();
		return false;
	}
	if (f.msg_azienda.value.indexOf("&") >= 0) {
		alert("Non è possibile inserire questo tipo di carattere: & nel campo NOME/AZIENDA.")
		f.msg_azienda.focus();
		return false;
	}

	// controllo approfondito campo mail
	
var emailPat=/^(.+)@(.+)$/

var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"

var validChars="\[^\\s" + specialChars + "\]"

var quotedUser="(\"[^\"]*\")"

var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/

var atom=validChars + '+'

var word="(" + atom + "|" + quotedUser + ")"

var userPat=new RegExp("^" + word + "(\\." + word + ")*$")

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

var matchArray=emailStr.match(emailPat)
if (matchArray==null) {

	alert("Il campo E-MAIL non è valido!.")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// controllo dell' user 
if (user.match(userPat)==null) {
    // user non valido
    alert("L' user della tua E-MAIL non è valido!.")
    return false
}

var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("La destinazione del tuo indirizzo IP non è valida!")
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("Il dominio della tua E-mail non è valido!.")
    return false
}

var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   alert("Il tuo indirizzo E-mail deve finire con tre lettere di dominio o due lettere per lo stato!")
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="Nel tuo indirro E-mail non è presente l'hostname!"
   alert(errStr)
   return false
}

	// fine controllo mail
	

if (f.trattamento.checked == false) {
alert("E' necessario spuntare il campo per il trattamento dei dati personali.")
return false;
}
return true;
}
