var emailLocalRegex = /^[\w!#\$%'’\*\+-\/=\?\^`{\|}~]+(\.[\w!#\$%’'\*\+-\/=\?\^`{\|}~]+)*$/;

var emailDomainRegex = /^(\b[-a-zA-Z0-9]+\b\.)+[a-zA-Z]{2,}$/;

function emailCheckImpl_old(email){

	var emailParts = email.split("@", 3);
	
	if(emailParts.length > 2){
		return "More than one '@'.";
	}
	if(emailParts.length < 2){
		return "Missing '@'.";
	}
	if(emailParts[0].match(emailLocalRegex) === null){
		return "Format error to the left of '@'.";
	}
	/*The above test doesn't catch adjacent periods or periods at the beginning
	  or end of the username (Javascript bug?), so do it here:
	 */
	 var length = emailParts[0].length;
	if(emailParts[0].indexOf("..") >= 0 || emailParts[0].substring(0, 1) === "." || emailParts[0].substring(length - 1, length) === "."){
		return "Format error to the left of '@'.";
	}
	if(emailParts[1].match(emailDomainRegex) === null){
		return "Format error to the right of '@'.";
	}
	//if we are here then the email address is properly formatted
	return "";
}

function emailCheckImpl(email){
	
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) {
		return "";
	}
	return "Invalid email address, please re-enter";
}

function emailCheckAlert(errMsgPrefix, emailElement) {
   var errMsg = emailCheckImpl(emailElement.value.trim());

   if(errMsg !== "") {
//      errMsg = errMsgPrefix + errMsg;
      emailElement.focus();
      alert(errMsg);
      return false;
   }
   return true;
}

