function validemail(temail, doalert)
// this is a js file I include it this way in html or cfm docs
//		<script src="validateemail.js"></script>
// this function validates an Internet Email address by applying
// a subset of the rules specified by RFC's as they pertain to
// the formation of TLD's, Host Names.  Included within this are
// allowable characters and character juxtapositioning.
// 
// suggest ways to call this function are:
// 	from within the html form:
//			<input type="text" name="email" onclick="validemail(this, true)">
//		or
//			<form .... onsubmit="validemail(this.email.value)">
//		or
//		if the form calls a form-validator function e.g. onsubmit="validate-form(this)"...
//		then that function, e.g. form-validate(theForm) .. call something like this...
//			if(!validemail(theForm.email.value){
//				theForm.email.focus();
//				return (false);
//			}
//
//
//	returns true if a valid email or false if not valid
// 
// parameters are temail => the email address
//                doalert => either true (1) or false (0)
// if doalert is true then an alert message from the array
// aEmailLit will be raised.
// if doalert is false then it is up to the caller to raise
// any required alerts.

{
	var aEmailLit = new Array();
	aEmailLit['basic'] = "E-mail address is not valid!\n";	
	aEmailLit['missing'] = "E-mail address is required";
	aEmailLit['notvalid'] = "e-mail ddress format is not valid";
	aEmailLit['short'] = "e-mail ddress is too short to be valid";
	aEmailLit['recip'] = "e-mail Recipient contains invalid character or character sequence";
	aEmailLit['host'] = "e-mail Domain Name contains invalid character, or character sequence";
	aEmailLit['dom'] = "e-mail Domain (TLD), expected like \"com\", \"org\", \"net\", \"gov\", \"us\", \"uk\", ...";
	aEmailLit['hostshort'] = "e-mail Domain Name too few characters";
	aEmailLit['form'] = "e-mail address is not formed correctly";	
	
	var iI;
	var iMark;
	var sDom;
	var sHost;
	var sRecip;
	var iRootMark;
	var iAtMark;
	
//	temail = tF.email.value;
	if (temail.length == 0)
	{
		if (doalert)
			emailAlert('missing');
			
		return (false);
	}
	if (temail.length < 6)
	{
		if (doalert)
			emailAlert('short');		
		return (false);
	}
	iAtMark = temail.lastIndexOf('@');
	iRootMark = temail.lastIndexOf(".");
	if ((iRootMark - iAtMark) < 2){
		if (doalert)
			emailAlert('form');
		return (false);
	}
	if (temail.length == iRootMark){
		if (doalert)
			emailAlert('dom');
		return (false);
	}		
	sDom = temail.substr(iRootMark+1);
	if (sDom.length < 2 || sDom.length > 3) {
		if (doalert)
			emailAlert('dom');
		return (false);
	}

	var domChars = /[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]/;	
	
	if (!validAtom(sDom, domChars)) {
			if (doalert)
				emailAlert('dom');
			return (false);	
	}
	
	sHost = temail.substr(iAtMark+1, iRootMark - (iAtMark + 1));
	if (sHost.length == 1 && sHost != "Z" && sHost != "z") {
		if (doalert)
			emailAlert('hostshort');
		return (false);		
	}		
	
	if (sHost.charAt(0) == "-" || sHost.charAt(sHost.length - 1) == "-" 
		|| sHost.charAt(0) == "." || sHost.charAt(sHost.length - 1) == "."){
		if (doalert)
			emailAlert('host');
		return (false);		
	}
	for (var i = 0; i< sHost.length;i++){
		if (i != 0 && i != (sHost.length-1)
			&& sHost.charAt(i) == ".") {
			if (sHost.charAt(i+1) == "." || sHost.charAt(i-1) == "." ||
				sHost.charAt(i+1) == "-" || sHost.charAt(i-1) == "-"){
				if (doalert)
					emailAlert('host');
				return (false);
			}
		}
	}
	var domChars2 = /[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\-\.]/
	if (!validAtom(sHost, domChars2)){
		return (false);		
	}
	
	sRecip = temail.substr(0, iAtMark);
	sCh1 = sRecip.charAt(0);
	sCh2 = sRecip.charAt(sRecip.length - 1)
	
	if (sCh1.match("[@.-]") != null){
		if (doalert)
			emailAlert('recip');
		return (false);		
	}
	if (sCh2.match("[@.-]") != null){
		if (doalert)
			emailAlert('recip');
		return (false);		
	}
	var sPat = /@@|--|@-|-@|\.\.|-\.|\.-|@\.|\.@/i;
	if (sRecip.match(sPat)){
		if (doalert)
			emailAlert('recip');
		return (false);		
	}
	
	var sNamPat = /[\[\]\{\}\/~`!#$%^&\*\(\)_\+=\|\\;:"'<>\?,\f\n\r\t\v]/;
	var sChr;
	for (var i = 0;i<sRecip.length;i++){
		sChr = sRecip.charAt(i);
		if (sChr.match(sNamPat)){
			if (doalert)
				emailAlert('recip');
			return (false);
		}
	}			
	
		
return (true);  // fall though to here is good

// the following are nested functions within the validemail function and
// are only see within the scope of the function

	function validAtom(atom, valchars){
		sPat = valchars	
		for (var i=0;i<atom.length;i++){
			sChr = atom.charAt(i);
			if (!sChr.match(sPat)){
				return (false);
			}
		}
		return (true);
	}

					
			
	
	function validAtom2(atom, valchars){
	var bValid = true;
		for (var i=0; i<atom.length; i++){
			for(var j=0;j<valchars.length;j++){
				if (valchars.charAt(j) == atom.charAt(i)){
					break;
				}
				else if (valchars.length == (j+1)) {
					bValid = false;
				}
			}
		}
		if (!bValid)
			return (false);
		else
			return (true);
	}
	
	function emailAlert(ndx){
		alert(aEmailLit['basic'] + aEmailLit[ndx]);
		return;
	}	
	
}



NS