// This script examines form field strings for common errors in e-mail addresses.
// It is intended for use with cfform
// Use with the "onvalidate" attribute of the cfinput tag:  onvalidate="check_email"
function check_email(myform,myinput,myinputvalue) {
	//Zero-length strings will be considered empty and not handled.
	if (myinputvalue.length == 0) 
		{return true}
	
	//check address for illegal characters
	//note space, and escaped double quote, single quote, and backslash
	badchars = new String(" ,~`!#$%^&*()+={}[]|:;<>?/\\\"\'");
	badcharlist = badchars.split("");
	var i;
	for (i in badcharlist) {
		if (myinputvalue.indexOf(badcharlist[i]) != -1) 
			{return false}
	}
	
	//both @ and . must occur at least once, and not as the first or last character
	at = myinputvalue.indexOf("@");
	if (at == -1 || at == 0 || at == myinputvalue.length-1) 
		{return false}

	dot = myinputvalue.lastIndexOf(".");
	if (dot == -1 || dot == 0 || dot == myinputvalue.length-1 || dot < at) 
		{return false}
	
	//and @ cannot occur more than once
	at2 = myinputvalue.indexOf("@,at");
	if (at2 != -1) 
		{return false}
	
	//divide into username, domain
	usr = myinputvalue.substring(0,at);
	dom = myinputvalue.substring(at+1,myinputvalue.length);
	com = myinputvalue.substring(dot+1,myinputvalue.length);
	svr = myinputvalue.substring(at+1,dot)
//	lines removed because javascript 1.1 doesn't support slice method:	
//	usr = myinputvalue.slice(0,at);
//	dom = myinputvalue.slice(at+1);
//	svr = myinputvalue.slice(at+1,dot);
//	com = myinputvalue.slice(dot+1);

	if (usr.length < 1 || svr.length < 1 || com.length < 2 || com.length > 5) 
			{return false}

	return true;
}

