function emailCheck(emailStr) {
  var checkTLD=1; //verify TLD -> 0=no, 1=yes
  var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
  var emailPat=/^(.+)@(.+)$/; //verify user@domain pattern
  var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]"; //special chars to strip
  var validChars="\[^\\s" + specialChars + "\]"; //valid chars to NOT strip
  var quotedUser="(\"[^\"]*\")"; //pattern to apply in quoted string - special chars rule is void
  var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/; //allow ip addresses as domain
  var atom=validChars + '+'; //an atom (series of non-special chars
  var word="(" + atom + "|" + quotedUser + ")"; //representation of single word (mat.rosa@ becomes mat and rosa), quoted strings are single words
  var userPat=new RegExp("^" + word + "(\\." + word + ")*$"); //structure of the user
  var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$"); //structure of symbolic domain
  //begin testing
  var matchArray=emailStr.match(emailPat); //course pattern to break user@domain into pieces for analysis
  if (matchArray==null) {
    //if @ instances is 0 or >1 then invalid
    alert("Your email address seems incorrect (check @ and .'s)");
    return false;
  }
  var user=matchArray[1];
  var domain=matchArray[2];
  //check that only basic ASCII characters are in the strings (0-127)
  for (i=0; i<user.length; i++) {
    if (user.charCodeAt(i)>127) {
      alert("Your email address prefix (before @)\ncontains invalid characters.");
      return false;
    }
  }
  for (i=0; i<domain.length; i++) {
    if (domain.charCodeAt(i)>127) {
      alert("Your email address domain name contains\ninvalid characters.");
      return false;
    }
  }
  //check that "user" is valid 
  if (user.match(userPat)==null) {
    //user not valid
    alert("The email address prefix (before @) doesn't appear\nto be valid.");
    return false;
  }
  //check that ip address is valid if using ip (instead of symbolic hostname)
  var IPArray=domain.match(ipDomainPat);
  if (IPArray!=null) {
    for (var i=1;i<=4;i++) {
      if (IPArray[i]>255) {
        alert("Email address destination IP address is invalid!");
        return false;
      }
    }
    return true;
  }
  //symbolic hostname check [if not IP]
  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 in your email address does\nnot appear to be valid.");
      return false;
    }
  }
  //domain name appears valid, now check TLD as known (com, edu, gov) or 2 letter country code (us, uk, es)
  if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
    alert("The email address domain suffix must end in\na well-known domain or two letter country.");
    return false;
  }
  //make sure a host name precedes the domain
  if (len<2) {
    alert("Your email address is missing a domain.");
    return false;
  }
  //email validated
  return true;
}

