// usage: <form name=blah onsubmit="return checkEmail(this.form.emailfield.value)">

function checkEmail(emailStr){
	emailPat=/^(.+)@(.+)$/
	specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	validChars="\[^\\s" + specialChars + "\]"
	quotedUser="(\"[^\"]*\")"
	ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	atom=validChars + '+'
	word="(" + atom + "|" + quotedUser + ")"
	userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	matchArray=emailStr.match(emailPat)
	if (matchArray==null){
		showError("Your e-mail address seems incorrect (check @ and .'s)");
		return false;
	}
	user=matchArray[1]
	domain=matchArray[2]
	if (user.match(userPat)==null){
		showError("The username doesn't seem to be valid.");
		return false
	}
	IPArray=domain.match(ipDomainPat)
	if (IPArray!=null){
		for (i=1;i<=4;i++){
			if (IPArray[i]>255){
				showError("Destination IP address is invalid!");
				return false;
			}
		}
	}
	domainArray=domain.match(domainPat)
	if (domainArray==null){
		showError("Your domain name doesn't seem to be valid.");
		return false;
	}
	atomPat=new RegExp(atom,"g");
	domArr=domain.match(atomPat);
	len=domArr.length;
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>4){
		showError("Your e-mail address must end in a two to four-letter domain.");
		return false;
	}
	if (len<2){
		errStr="Your address is missing a hostname!";
		showError(errStr);
		return false;
	}
	return true;
}

function showError(what){
	document.getElementById('error_message').innerHTML="<p>"+what+"</p>";
}