// Using the function
// OPTION 1: add the following function to the input box you wish to protect
// onKeyPress="restrictKeyStrokes(this,keybNumeric)"
// see relay/leadManager/oppProductList.cfm
//
// OPTION 2: create a function to add an event and then call the function in window.onload()
// void function setEvents() { document.all.txtYN.onkeypress = new Function('restrictKeyStrokes(this,keybYN)');}
//	<body onLoad="JavaScript:setEvents()">
//
// see authors article at http://builder.com.com/5100-6371-1044655.html
// WAB 14/03/2007 Andy noticed that z was not in the list of letters!

var keybYN = new keybEdit('yn','Valid values are \'Y\' or \'N\'.');
var keybNumeric = new keybEdit('01234567890','You can only enter numbers into this field.');
var keybAlpha = new keybEdit('abcdefghijklmnopqurstuvwxyz','You can only enter letters into this field.');
var keybAlphaNumeric = new keybEdit('abcdefghijklmnopqurstuvwxyz01234567890','You can only enter Alpha-numeric characters into this field.');
var keybDecimal = new keybEdit('01234567890.','Decimal input only i.e. No commas and . for a decimal point.');
var keybDate =  new keybEdit('01234567890/','Date input only');
var keybRelaySafe =  new keybEdit('abcdefghijklmnopqurstuvwxyz01234567890- ()','You can only enter Alpha-numeric characters, minus sign (-) into this field.');;
var keybCFVariable =  new keybEdit('abcdefghijklmnopqurstuvwxyz01234567890_','You can only enter Alpha-numeric characters & _ into this field.');;

var keybYNNM = new keybEdit('yn');
var keybNumericNM = new keybEdit('01234567890');
var keybAlphaNM = new keybEdit('abcdefghijklmnopqurstuvwxyz');
var keybAlphaNumericNM = new keybEdit('abcdefghijklmnopqurstuvwxyz01234567890');
var keybDecimalNM = new keybEdit('01234567890.');
var keybDateNM = new keybEdit('01234567890/');
var keybRelaySafeNM =  new keybEdit('abcdefghijklmnopqurstuvwxyz01234567890- ()');
var keybCFVariableNM =  new keybEdit('abcdefghijklmnopqurstuvwxyz01234567890_');


function keybEdit(strValid, strMsg) {
	/*	Function:		keybEdit
		Creation Date:	October 11, 2001
		Programmer:		Edmond Woychowsky
		Purpose:		The purpose of this function is to be a constructor for
						the keybEdit object.  keybEdit objects are used by the
						function editKeyBoard to determine which keystrokes are
						valid for form objects.  In addition, if an error occurs,
						they provide the error message.
						
						Please note that the strValid is converted to both
						upper and lower case by this constructor.  Also, that
						the error message is prefixed with 'Error:'.
						
						The properties for this object are the following:
							valid	=	Valid input characters
							message	=	Error message
							
						The methods for this object are the following:
							getValid()	=	Returns a string containing valid
											characters.
							getMessage()=	Returns a string containing the
											error message.

		Update Date:	Programmer:			Description:
	*/

	//	Variables
	var reWork = new RegExp('[a-z]','gi');		//	Regular expression\

	//	Properties
	if(reWork.test(strValid))
		this.valid = strValid.toLowerCase() + strValid.toUpperCase();
	else
		this.valid = strValid;

	if((strMsg == null) || (typeof(strMsg) == 'undefined'))
		this.message = '';
	else
		this.message = strMsg;

	//	Methods
	this.getValid = keybEditGetValid;
	this.getMessage = keybEditGetMessage;
	
	function keybEditGetValid() {
	/*	Function:		keybEdit
		Creation Date:	October 11, 2001
		Programmer:		Edmond Woychowsky
		Purpose:		The purpose of this function act as the getValid method
						for the keybEdit object.  Please note that most of the
						following logic is for handling numeric keypad input.

		Update Date:		Programmer:			Description:
	*/

		return this.valid.toString();
	}
	
	function keybEditGetMessage() {
	/*	Function:		keybEdit
		Creation Date:	October 11, 2001
		Programmer:		Edmond Woychowsky
		Purpose:		The purpose of this function act as the getMessage method
						for the keybEdit object.

		Update Date:	Programmer:			Description:
	*/
	
		return this.message;
	}
}




 function restrictKeyStrokes(event,objKeyb) {
	/*	Function:		editKeyBoard
		Creation Date:	October 11, 2001
		Programmer:		Edmond Woychowsky
		Purpose:		The purpose of this function is to edit edit keyboard input
						to determine if the keystrokes are valid.
	
		Update Date:		Programmer:			Description:
		June 2006 			WAB					modified to work with firefox, pass in event rather than this
							WAB					mods so that FF allows delete etc.
	*/
	strWork = objKeyb.getValid();
	strMsg = '';							// Error message
	blnValidChar = false;					// Valid character flag

	if (window.event) {
//		event = window.event
		keyCode = event.keyCode
		objForm = event.srcElement
	} else {
		keyCode = event.charCode
		objForm = event.target
		if (keyCode == 0) {	blnValidChar = true;}   // this seems to be the case of cursor keys etc. in FF
	}


			
	// Part 1: Validate input
	if(!blnValidChar)
		for(i=0;i < strWork.length;i++)
			if(keyCode == strWork.charCodeAt(i)) {
				blnValidChar = true;

				break;
			}

	// Part 2: Build error message
	if(!blnValidChar) {
		if(objKeyb.getMessage().toString().length != 0)
			alert('Error: ' + objKeyb.getMessage());
			
				// Clear invalid character
				if (window.event) {
					event.returnValue = false;
				} else {
					event.preventDefault();
				}
		 objForm.focus();						// Set focus
	}
}


