// JavaScript Document
function checkform ( form )
{

var emailStr=form.email.value;

var checkTLD=1;

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

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);

// Check basic formatting

if (matchArray==null) {
	alert("Your email address seems incorrect (check @ and .'s)");
    form.email.focus();
return false;
}

var user=matchArray[1];
var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
	alert("Your email address contains invalid characters.");
    form.email.focus();
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
	alert("Your email address contains invalid characters.");
	form.email.focus();
return false;
   }
}

// See if "user" is valid 

if (user.match(userPat)==null) {

// user is not valid

	alert("Your email address is invalid - check the part before the @ symbol.");
    form.email.focus();	
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("Your email address is invalid - destination IP address is invalid!");
    form.email.focus();
return false;
   }
}
return true;
}

// Domain is symbolic name.  Check if it's valid.
 
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
	alert("Your email address does not seem to be valid.");
    form.email.focus();	
return false;
   }
}

if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
	alert("Your email address is invalid: it must end in a well-known domain or two letter country.");
	form.email.focus();
return false;
}

// Make sure there's a host name preceding the domain.

if (len<2) {
	alert("Your email address is invalid: it is missing a hostname (the bit after the @ symbol)");
    form.email.focus();
return false;
}

  // This just checks that the form has been filled out correctly
  // There is a different JS routine to check the email address is valid
    
  if (form.name.value == "") {
    alert( "Please enter your name." );
    form.name.focus();
    return false ;
  }  
  
  if (form.email.value == "") {
    alert( "Please enter your email address." );
    form.email.focus();
    return false ;
  }
  
  if (form.telday.value == "") {
    alert( "Please enter a daytime telephone number." );
    form.telday.focus();
    return false ;
  }

  if (form.televe.value == "") {
    alert( "Please enter a evening telephone number." );
    form.televe.focus();
    return false ;
  }
  
  if (!(form.spamtotaluser.value == form.spamtotalactual.value)) {
    alert( "Please enter the correct answer to the question." );
    form.spamtotaluser.focus();
    return false ;
   }
  // ** END **
  return true ;
}