// 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>";
}

function frm_val(frm){
	// requires array of field names, arr_required
	//
	if(!frm)frm=document.sForm;
	firsterror='';
	delim='';
	for(i=0;i<arr_required.length;i++){
		field=document.getElementsByName(arr_required[i])[0];
		if(field.value){
			field.style.backgroundColor='white';
		}else{
			field.style.backgroundColor=errorColor;
			firsterror=(firsterror>''?firsterror:arr_required[i]);
		}
	}
	if(firsterror>''){
		showError("Required information is missing");
		document.getElementsByName(firsterror)[0].focus();
		return false;
	}
	if(!checkEmail(frm.email.value)){
		frm.email.style.backgroundColor=errorColor;
		frm.email.focus();
		return false;
	}
	return true;
}
