// The JavaScript Source http://javascript.internet.com
// V1.1.3: Sandeep V. Tamhankar
function emailCheck (emailStr) {
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	
	/* check user@domain format */
	var emailPat=/^(.+)@(.+)$/;
	/* no special characters: ( ) < > @ , ; : \ " . [ ] */
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	/* range of characters */
	var validChars="\[^\\s" + specialChars + "\]";
	/* if the "user" is a quoted string */
	var quotedUser="(\"[^\"]*\")";
	/*  IP addresses, @[123.124.233.4] is legal - square brackets are required. */
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	/* atom (basically a series of non-special characters.) */
	var atom=validChars + '+';
	/* 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 + ")";
	// describes the structure of the user
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	/* structure of a normal symbolic domain */
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	/* break up user@domain */
	var matchArray=emailStr.match(emailPat);
	
	if (matchArray==null) {
		/* Too many/few @'s or something - not a valid e-mail address. */
		alert("Email address seems incorrect (check @ and .'s)");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	
	// check basic ASCII chars(0-127).
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
		alert("Ths username contains invalid characters.");
		return false;
	   }
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
		alert("Ths domain name contains invalid characters.");
		return false;
		}
	}
	
	// See if "user" is valid 
	if (user.match(userPat)==null) {
		// user is not valid
		alert("The username doesn't seem to be valid.");
		return false;
	}
	
	/* make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		// is IP address
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
			alert("Destination IP address is invalid!");
			return false;
		   }
		}
		return true;
	}
	
	// Domain is symbolic name.  Check if it's valid.
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
		alert("The domain name does not seem to be valid.");
		return false;
		}
	}
	
	/* check domain ends in a (com, edu, gov)(uk, nl) */
	if (checkTLD && domArr[domArr.length-1].length!=2 && 
		domArr[domArr.length-1].search(knownDomsPat)==-1) {
		alert("The address must end in a well-known domain or two letter " + "country.");
		return false;
	}
	
	// check host name preceding the domain.
	if (len<2) {
		alert("This address is missing a hostname!");
		return false;
	}
	
	// valid
	return true;
}
