function emailCheck (emailStr, warning) {
/* This pattern checks user@domain format, and is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* We don't want to allow the following characters ( ) < > @ , ; : \ " . [ ] '   */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* Range of characters allowed in a username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* If the "user" is a quoted string, anything goes.*/
var quotedUser="(\"[^\"]*\")"
/* If the domain is an IP addresses, rather than symbolic names.  
   E.g. joe@[123.124.233.4] is a legal email address. 
   NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* An atom (basically a series of non-special characters.) */
var atom=validChars + '+'
/* One word in the typical username. E.g. john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// Structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* Structure of a normal symbolic domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

/* Is supplied address is valid. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
	warning += " The Email address seems incorrect (check @ and .'s)"
	return warning
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    warning +=" The Email username doesn't seem to be valid."
    return warning
}

/* If an IP address, make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        warning +=" The Email destination IP address is invalid!"
		return warning
	    }
    }
    return warning;
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	warning +=" The Email domain name doesn't seem to be valid."
    return warning
}

/* Check that the domain ends in a three-letter word (like com, edu, gov) 
   or a two-letter word, representing country (uk, nl), 
   and that there's a hostname preceding the domain or country. */

/* Break up the domain to get a count of how many atoms it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   warning +=" The Email address must end in a three-letter domain, or two letter country."
   return warning
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   warning +=" The Email address is missing a hostname."
   return warning
}

return warning;
}
