/* ValidateFields_eng
   Check if the required fields are filled and the format of the email
   ST 22071126

   The code for the mail validation is from:
   Sandeep V. Tamhankar (stamhankar - hotmail.com)
   The JavaScript Source!! http://javascript.internet.com
*/

function CheckRequired () {
var emptyFields=''
var errMsg="The following requiered fields are empty:\n\n"

if (document.form1.cognome.value=="") {
emptyFields = "   Second Name\n";
}
if (document.form1.nome.value=="") {
emptyFields = emptyFields + "   First Name\n";
}
if (document.form1.incarico.value=="") {
emptyFields = emptyFields + "   Position\n";
}
if (document.form1.istituzione.value=="") {
emptyFields = emptyFields + "   Institution\n";
}
if (document.form1.tel.value=="") {
emptyFields = emptyFields + "   Telephone\n";
}
if (document.form1.indirizzo.value=="") {
emptyFields = emptyFields + "   Street Address\n";
}
if (document.form1.cap.value=="") {
emptyFields = emptyFields + "   CAP/ZIP\n";
}
if (document.form1.citta.value=="") {
emptyFields = emptyFields + "   City\n";
}
if (document.form1.nazione.value=="") {
emptyFields = emptyFields + "   Country\n";
}
if (document.form1.email.value=="") {
emptyFields = emptyFields + "   Email\n";
}
if (emptyFields=='') {
   return emailCheck(document.form1.email.value);
} else {
   errMsg = errMsg + emptyFields;
   alert(errMsg);
   return false;
}

}

// mail validation
function emailCheck (emailStr) {

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) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	alert("Invalid Email address (check '@' and '.')")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

if (user.match(userPat)==null) {
    // user is not valid
    alert("Invalid Email address:\n\Check the 'username'.")
    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("Invalid Email address:\n\nDestination IP address is invalid!")
		return false
	    }
    }
    return true
}

var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("Invalid Email address:\n\nThere is no domain.")
    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>4) {
   // the address must end in a two letter or three letter word.
   alert("Invalid Email address:\n\nThe final part has more than 4 chars\nor less than 2.")
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="Invalid Email address:\n\nIt doesn't look like 'user@domain.com'"
   alert(errStr)
   return false
}

// If we've gotten this far, everything's valid!
return true;
}
//  End -->
