function emailValid(email) {
  var result = true;

  // do basic check
  if (email == "" || email.indexOf(".") == -1 || email.indexOf("@") == -1 || email.indexOf(" ") != -1) {
    return false;
  }

  // the local part cannot begin or end with a "."
  regex=/(^\w{2,}\.?\w{2,})@/;
  _match = regex.exec(email);
  
  if (_match) {
    user = RegExp.$1;
  } else {
    return false;
  }
  
  // get the domain after the @ char
  // first take care of domain literals like @[19.25.0.1], however rare
  regex=/@(\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\])$/;
  _match = regex.exec(email);
  
  if (_match) {
    domain = RegExp.$1;
  } else {
    // the @ character followed by at least two chars that are not a period (.),
    // followed by a period, followed by zero or one instances of two or more
    // characters ending with a period, followed by two-three chars that are not periods
    regex=/@(\w{2,}\.(\w{2,}\.)?[a-zA-Z]{2,3})$/;
    _match = regex.exec(email);
    
    if (_match) {
      domain = RegExp.$1;
    } else {
      return false;
    }
  }
   
  return true;
}