//Date:11/4/04
//Program:JavaScript function to validate E-mail addresss
//Company:Transformyx
//Programmer:Stan Hebert

function ValidateEmail(passEmailStr) 
{
	/*Used to check if the user entered e-mail address
	fits the user@domain format.*/
	var emailPattern=/^(.+)@(.+)$/;
	/* The following string represents the pattern for matching all special
	characters.These characters include ( ) < > @ , ; : \ " . [ ]*/
	var specialChar="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	/* The following string represents the range of characters allowed in a 
	username or domainname.  It really states which chars aren't allowed.*/
	var badChar="\[^\\s" + specialChar + "\]";
	/* The following applies if the "user" is a quoted string (E.g. "john.doe"@whatever.com)
	is a legal e-mail address.*/
	var quotedUser="(\"[^\"]*\")";
	/* The following pattern applies for domains that are IP addresses,
	rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	e-mail address. NOTE: The square brackets are required.*/
	var ipDomainPattern=/^\[(\d{1,4})\.(\d{1,4})\.(\d{1,4})\.(\d{1,4})\]$/;
	/* The following string represents an atom (basically a series of
	non-special characters.)*/
	var atom=badChar + '+';
	/* The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string.*/
	var word="(" + atom + "|" + quotedUser + ")";
	// The following pattern describes the structure of the user
	var userPattern=new RegExp("^" + word + "(\\." + word + ")*$");
	/* The following pattern describes the structure of a normal symbolic
	domain, as opposed to ipDomainPattern, shown above.*/
	var domainPattern=new RegExp("^" + atom + "(\\." + atom +")*$");
	/* Finally, let's start trying to figure out if the supplied address is
	valid. Begin with the coarse pattern to simply break up user@domain into
	different pieces that are easy to analyze.*/
	var matchArray=passEmailStr.match(emailPattern);
	/* Too many/few @'s or something; basically, this address doesn't
	even fit the general mold of a valid e-mail address.*/
	if (matchArray==null) 
	{
		//alert("Email address seems incorrect (check @ and .'s)");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	// See if "user" is valid 
	if (user.match(userPattern)==null) 
	{
		//alert("The username doesn't seem to be valid.");
		return false;
	}
	/* If the e-mail address is at an IP address (as opposed to a symbolic
	host name) make sure the IP address is valid.*/
	var IPArray=domain.match(ipDomainPattern);
	if (IPArray!=null) 
	{
		for (var i=1;i<=5;i++) 
		{
			if (IPArray[i]>255) 
			{
				//alert("Destination IP address is invalid!");
				return false;
			}
		}
	}
	// Domain is symbolic name
	var domainArray=domain.match(domainPattern);
	if (domainArray==null) 
	{
		//alert("The domain name doesn't seem to be valid.");
		return false;
	}
	// The address must end in a two letter or four letter word.
	var atomPattern=new RegExp(atom,"g");
	var domArr=domain.match(atomPattern);
	var len=domArr.length;
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>4) 
	{
		//alert("The address must end in a two, three, or four letter domain");
		return false;
	}
	// Make sure there's a host name preceding the domain.
	if (len<2) 
	{
		var errStr="This address is missing a hostname!";
		//alert(errStr);
		return false;
	}
	return true;
}
