/*
***************************************************************************
*  This work is the copyright and intellectual property of Deborah Figg.  *
*  Reproduction by any means is strictly prohibited unless prior written  *
*  permission is obtained from the copyright holder.                      *
***************************************************************************
*/
function funEmailCheck(strEmail, booShowPolicy) {

	booShowPolicy = (booShowPolicy == null) ? (false) : (booShowPolicy);

	if  (strEmail == null || strEmail == '') {
		strGblErrorMsg = "\n- You must enter your email address.";
		return false;
	}
	
	var strEmailPat     = /^(.+)@(.+)$/
	var strSpecialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var strValidChars   = "\[^\\s" + strSpecialChars + "\]"
	var strQuotedUser   = "(\"[^\"]*\")"
	var strIpDomainPat  = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var strAtom         = strValidChars + '+'
	var strWord         = "(" + strAtom + "|" + strQuotedUser + ")"
	var strUserPat      = new RegExp("^" + strWord + "(\\." + strWord + ")*$")
	var strDomainPat    = new RegExp("^" + strAtom + "(\\." + strAtom +")*$")
	var aBadDomains     = new Array("yahoo","hotmail","msn","bigfoot","gmail","aol")
	var aMatches = strEmail.match(strEmailPat)
	if  (aMatches == null) {
		strGblErrorMsg = "\n- Email address seems incorrect (check '@' and '.'s)";
		return false;
	}

	var strUser   = aMatches[1]
	var strDomain = aMatches[2]
	if  (strUser.match(strUserPat) == null) {
		strGblErrorMsg = "\n- The Email username doesn't seem to be valid.";
		return false;
	}
	
	var aIpBits = strDomain.match(strIpDomainPat)
	if  (aIpBits != null) {
		  for (var i = 1; i <= 4; i++) {
			if  (aIpBits[i] > 255) {
				strGblErrorMsg = "\n- Destination IP address is invalid!";
			return false;
			}
		}
		return true
	}
	
	var aDomainBits = strDomain.match(strDomainPat)
	if  (aDomainBits == null) {
		strGblErrorMsg = "\n- The email domain name doesn't seem to be valid.";
		return false;
	}
	
	var strAtomPat = new RegExp(strAtom,"g")
	var aDomain    = strDomain.match(strAtomPat)
	var intLen     = aDomain.length
	if  (aDomain[intLen - 1].length < 2) {
		strGblErrorMsg = "\n- The last part of the address must be at least 2 characters.";
		return false;
	}
	
	if  (intLen < 2) {
		strGblErrorMsg = "\n- This address is missing a hostname!";
		return false;
	}
	
	for (var i = 0; i <= aBadDomains.length - 1; i++) {
		if  (strDomain.indexOf(aBadDomains[i]) != -1) {
			strGblErrorMsg = "\n- This email address is using one of our banned domains. Please use another one." + ((booShowPolicy) ? ("\n  See our Email Address Policy for further information.") : (""));
			return false;
		}
	}
	
	return true;
}
function funCheckNameEmail(objForm, booChkLastName, booShowPolicy) {
	// Check if an email address is valid

	var booReturn   = true;
	var strErrMsg   = '';
	strFirstName    = (objForm.FirstName    == null) ? (objForm.first_name) : (objForm.FirstName);
	strLastName     = (objForm.LastName     == null) ? (objForm.last_name)  : (objForm.LastName);
	strEmailAddress = (objForm.EmailAddress == null) ? (objForm.email)      : (objForm.EmailAddress);

	if  (booChkLastName == null || booChkLastName == undefined) {
		booChkLastName = true;
	}

	if  (strFirstName == null || strFirstName.value == '') {
		strErrMsg += "- You must enter your first name.";
		//alert("You must enter your first name.");
		strFirstName.focus();
		booReturn = false;
	}

	if  (booChkLastName) {
		if  (booChkLastName && strLastName == null || strLastName.value == '') {
			strErrMsg += "\n- You must enter your last name.";
			//alert("You must enter your last name.");
			strLastName.focus();
			booReturn = false;
		}
	}

	if  (!funEmailCheck(strEmailAddress.value, booShowPolicy)) {
		strErrMsg += strGblErrorMsg;
		strGblErrorMsg = '';
		strEmailAddress.focus();
		booReturn = false;
	}
	
	if  (booReturn) {
		objForm.submit();
		return true;
	} else {
		strErrMsg = (strErrMsg.substring(0, 1) == "\n") ? (strErrMsg.substring(1)) : (strErrMsg);
		alert("Please correct these errors, then re-submit the form:\n\n" + strErrMsg);
		return false;
	}
}	
