<!--
//////////////////////////////////////////
//					//
// 	   Research and Markets		//
//  	        - SFF - 	 	//
//   	    24th March 2006		//
//					//
//     Form Error Messages / Handlers	//
//    	    V 1 - 24th Mar 06
//					//
//////////////////////////////////////////

//how does this work?
//
//concept:
//
//user clicks on a form element, the onfocus / onblur 
//javascript event passes the form value into the
//correct top level "[XXX]ErrorHandler" function which then
//passes it to the base "errorHandler" function.
//the base errorhandler function will display the error message
//in the DIV layer that has been passed into the function.
//if the validation function is not OK, it will display an error
//if the validation was not OK, and then the visitor changed their
//input the base function errorHandler, will then remove the original
//error message providing that the error message was the same.


//init
var eHUseSuccessMessage = true;
var eHSuccessMessage = "<font class='fgreen'>&nbsp;</font>";
var eHCaptureOldMessage = "";
var eHUseCounters = true;
var eHCounterSymbol = "<font class='errorMessage'>!</font>";
var eHCounterSep = "<font class='errorMessage'> </font>";


//system
var eHCounter = 0;
var eHCounterMax = 5;


//generic handlers

function checkNoHyperlinkHandler (formField) {
	errorHandler ("<font class='errorMessage'>Please remove hyperlinks from your message.</font>", "errorMessage", "containsNoHyperlink", formField.value);

}


function emailErrorHandler (formField) {
var chk = false;
chk = errorHandler ("<font class='errorMessage'>Please check that your email address is correct</font>", "errorEmail", "eH_str_chk_email", formField.value);

	if (chk) {
	errorHandler ("<font class='errorMessage'>If possible, please use your work email address</font>", "errorEmail", "str_chk_free_email", formField.value);
	}

}
//this is the same as above, but used on a page that has 2 email
//address fields in it, ie. basket_checkout.asp
function emailNewErrorHandler (formField) {
chk = errorHandler ("<font class='errorMessage'>Please check that your email address is correct</font>", "errorNewEmail", "eH_str_chk_email", formField.value);

	if (chk) {
	errorHandler ("<font class='errorMessage'>If possible, please use your work email address</font>", "errorNewEmail", "str_chk_free_email", formField.value);
	}
}
//this is for the unsubscribe page mostly, 
//as it does not check it they are using
//a free email account etc.
function emailErrorHandlerNoFreeChk (formField) {
var chk = false;
chk = errorHandler ("<font class='errorMessage'>Please check that your email address is correct</font>", "errorEmail", "eH_str_chk_email", formField.value);

}

function emailErrorFriendHandler (formField) {
chk = errorHandler ("<font class='errorMessage'>Please check that your friend\'s email address is correct</font>", "errorFriendEmail", "eH_str_chk_email", formField.value);
}

function phoneErrorHandler (formFieldValue) {
	
	if (formFieldValue.length < 6) {
	errorHandler ("<font class='errorMessage'>Your phone number must be longer than 6 characters</font>", "errorPhone", "generic_false", formFieldValue);
	return;
	} else {
	errorHandler ("<font class='errorMessage'>Your phone number must be longer than 6 characters</font>", "errorPhone", "generic_true", formFieldValue);
	
	}

	//first check for letters
	var chk = errorHandler ("<font class='errorMessage'>No letters are allowed in your phone number</font>", "errorPhone", "eH_str_no_letters", formFieldValue);

	//check a normal phone number
	if (chk) {
	
		var phoneValue = formFieldValue;

		//remove weird characters put in by database (hard coded)
		phoneValue = eH_str_reg_exp_replace(phoneValue, "\\/","");
		phoneValue = eH_str_reg_exp_replace(phoneValue, "#","");
		phoneValue = eH_str_reg_exp_replace(phoneValue, "\\\\","");
		phoneValue = eH_str_reg_exp_replace(phoneValue, ";","");
		phoneValue = eH_str_reg_exp_replace(phoneValue, "x","");
		phoneValue = eH_str_reg_exp_replace(phoneValue, "\\(","");
		phoneValue = eH_str_reg_exp_replace(phoneValue, "\\)","");
		phoneValue = eH_str_reg_exp_replace(phoneValue, " ","");
		phoneValue = eH_str_reg_exp_replace(phoneValue, "\\-","");
		phoneValue = eH_str_reg_exp_replace(phoneValue, "\\.","");
		phoneValue = eH_str_reg_exp_replace(phoneValue, "\\+","");

		if (phoneValue.length <= 4 || phoneValue != "") {
		return;
		}

	errorHandler ("<font class='errorMessage'>Please check that you entered your phone number correctly</font>", "errorPhone", "eH_chk_phone_number", phoneValue);
	}
}

function faxErrorHandler (formFieldValue) {

	if (formFieldValue.length <= 1) {
	return;
	}
	
	if (formFieldValue.length > 0) {
	
		if (formFieldValue.length < 6) {
		errorHandler ("<font class='errorMessage'>Your fax number must be longer than 6 characters</font>", "errorFax", "generic_false", formFieldValue);
		return;
		} else {
		errorHandler ("<font class='errorMessage'>Your fax number must be longer than 6 characters</font>", "errorFax", "generic_true", formFieldValue);

		}

		//first check for letters
		var chk = errorHandler ("<font class='errorMessage'>No letters are allowed in your fax number</font>", "errorFax", "eH_str_no_letters", formFieldValue);

		//check a normal phone number
		if (chk) {

			var phoneValue = formFieldValue;

			//remove weird characters put in by database (hard coded)
			phoneValue = eH_str_reg_exp_replace(phoneValue, "\\/","");
			phoneValue = eH_str_reg_exp_replace(phoneValue, "#","");
			phoneValue = eH_str_reg_exp_replace(phoneValue, "\\\\","");
			phoneValue = eH_str_reg_exp_replace(phoneValue, ";","");
			phoneValue = eH_str_reg_exp_replace(phoneValue, "x","");
			phoneValue = eH_str_reg_exp_replace(phoneValue, "\\(","");
			phoneValue = eH_str_reg_exp_replace(phoneValue, "\\)","");
			phoneValue = eH_str_reg_exp_replace(phoneValue, " ","");
			phoneValue = eH_str_reg_exp_replace(phoneValue, "\\-","");
			phoneValue = eH_str_reg_exp_replace(phoneValue, "\\.","");
			phoneValue = eH_str_reg_exp_replace(phoneValue, "\\+","");

			if (phoneValue.length <= 4 || phoneValue != "") {
			return;
			}

		errorHandler ("<font class='errorMessage'>Please check that you entered your fax number correctly</font>", "errorFax", "eH_chk_phone_number", phoneValue);
		}
	} 
	
}


function ccCardErrorHandler (formFieldValue) {
var cardValue = formFieldValue.value;

	//remove weird characters put in by database (hard coded)
	cardValue = eH_str_reg_exp_replace(cardValue, "\\/","");
	cardValue = eH_str_reg_exp_replace(cardValue, "#","");
	cardValue = eH_str_reg_exp_replace(cardValue, "\\\\","");
	cardValue = eH_str_reg_exp_replace(cardValue, ";","");
	cardValue = eH_str_reg_exp_replace(cardValue, "x","");
	cardValue = eH_str_reg_exp_replace(cardValue, "\\(","");
	cardValue = eH_str_reg_exp_replace(cardValue, "\\)","");
	cardValue = eH_str_reg_exp_replace(cardValue, " ","");
	cardValue = eH_str_reg_exp_replace(cardValue, "\\-","");
	cardValue = eH_str_reg_exp_replace(cardValue, "\\.","");
	cardValue = eH_str_reg_exp_replace(cardValue, "\\+","");

errorHandler ("<font class='errorMessage'>Please check that your card number is correct</font>", "errorCardNumber", "eH_chk_phone_number", cardValue);
}

function passwordErrorHandler (formField, confirmField) {

	if (confirmField == "") {
	errorHandler ("<font class='errorMessage'>Your password cannot be blank</font>", "errorConfirm", "generic_false", formField.value);
	return;
	} else {
	errorHandler ("<font class='errorMessage'>Your password cannot be blank</font>", "errorConfirm", "generic_true", formField.value);

	}
	
	if (formField.value.length < 6) {
	errorHandler ("<font class='errorMessage'>Your password must be longer than 5 characters</font>", "errorConfirm", "generic_false", formField.value);
	return;
	} else {
	errorHandler ("<font class='errorMessage'>Your password must be longer than 5 characters</font>", "errorConfirm", "generic_true", formField.value);
	
	}
	
	if (formField.value != confirmField) {
	errorHandler ("<font class='errorMessage'>Your passwords do not match</font>", "errorConfirm", "generic_false", formField.value);

	} else {
	errorHandler ("<font class='errorMessage'>Your passwords do not match</font>", "errorConfirm", "generic_true", formField.value);
	
	}

}




//top level handler
//(text values for all parameters)
function errorHandler (errorMessage, errorTag, booleanFunction, formValue) {

var formattedValue = eH_str_reg_exp_replace(formValue, "'", "\\'");

eval ("var valueCheck = " + booleanFunction + "('"+formattedValue+"');");

	if (!valueCheck) {
	var counterSymbol = "";

		//add counter symbol
		if (errorMessage == eHCaptureOldMessage) {
		eHCounter++;

			if (eHCounter > eHCounterMax) {
			eHCounter = eHCounterMax;
			}

			for (var x = 1; x <= eHCounter; x++) {
			counterSymbol += eHCounterSymbol;
			}

		} else {
		eHCounter = 0;
		}


	if (document.getElementById(errorTag) != null) {
	document.getElementById(errorTag).innerHTML = counterSymbol + "" + eHCounterSep + errorMessage;
	}
	eHCaptureOldMessage = errorMessage;
	
	return false;

	} else {

		//only display "success" message if there was a problem before
		if (eHUseSuccessMessage && eHCaptureOldMessage == errorMessage) {
		document.getElementById(errorTag).innerHTML = eHSuccessMessage;
		}

	}

return true;
}

//////////////////////////
// VALIDATION FUNCTIONS //
//////////////////////////

//used for creating ad-hoc validation
function generic_false (str) {
return false;
}
function generic_true (str) {
return true;
}


function containsNoHyperlink(str)
{
	if(str.indexOf("<a href") != -1 || str.indexOf("http://") != -1)
		return false;
	return true;
}


function chk_passwords_match (pass_str, confirm_str) {

	if (pass_str != confirm_str) {
	return false;
	}

return true;
}

function eH_str_no_letters (str) {
var ps_pattern = /^.*[a-zA-Z].*$/;

	//this is reveresed logic
	if (ps_pattern.test(str) == true) {
	return false;
	}

return true;
}




//check for email
function eH_str_chk_email(str) {
var email = str;

//regular expression
//var ps_pattern = /^([\w\-~.]+\@([\w\-]+\.){1,}[a-zA-Z]{2,4})$/;
var ps_pattern = /^([a-zA-Z0-9_\.\-'])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var ps_bad_pattern = /^.*@.*@.*$/;

	//TEST
	if (email == "" || ps_pattern.test(email) == false || ps_bad_pattern.test(email) == true) {
	return false;
	}

return true;
}

//test a phone number (eH_chk_number alias)
function eH_chk_phone_number (phone_number,phone_length) {
return eH_chk_number(phone_number,7,20);
}

//////////////////////
// COMMON FUNCTIONS //
//////////////////////

//remove common symbols in number fileds
function eH_remove_number_symbols (str) {
str = str_reg_exp_replace(str,"[^0-9]","");
return str;
}


//test a normal number
function eH_chk_number (numberx,start_length,end_length) {
var num = new String(numberx);
var length_start = new String(start_length);
var length_end = new String(end_length);

	//default
	if (length_start == "" || isNaN(length_start)) {
	length_start = 1;
	}
	if (length_end == "" || isNaN(length_end)) {
	length_end= 100;
	}

//remove typical values before checking
//num = eH_remove_number_symbols(num);

//regular expression
eval("var pn_pattern = /^[0-9]{"+length_start+","+length_end+"}\$/;");

	//TEST
	if (num == "" || pn_pattern.test(num) == false ) {
	return false;
	}

return true;
}



//replace a symbol in a string (for backwards compatibility)
function eH_str_reg_exp_replace (str, find_value, replace_value) {
	if (str != "") {
	eval("var reg_expression = /"+find_value+"/g;");
	str = str.replace(reg_expression,replace_value);
	}
return str;
}


-->
























































































































































































































































































































































































