function isValidEmail(Email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (Email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (Email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(Email)) {  // check to make sure all characters are valid
        return false;
    }
    if (Email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (Email.lastIndexOf(".") <= Email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (Email.indexOf("@") == Email.length) {  // @ must not be the last character
        return false;
    }
	
    return true;
}

function allValidChars(Email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < Email.length; i++) {
    var letter = Email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}
