/*
	In Netscape 6/7 and Opera you can capture a keystroke, but you cannot cancel it by returning false.
	In Internet Explorer it is important that it is the keypress event and not the keydown event.
	On keydown the method String.fromCharCode does not translate the key to the right character.
	For example lowercase letters are translated to uppercase letters.
	In Opera prior to version 7 this method is buggy too.
*/
if (typeof jslibDevErrMsg != "string") {
	var jslibDevErrMsg = 'Illegal argument or assignment!';
}
/*	A function to associate a certain key combination with a handler/function.
	In the regular expression/pattern, matchObj, you specify the characters (usually only one) which should trigger the handler. */
function keyboardShortcut(eventObj, obj, matchObj, shift, ctrl, alt, handler) {
	var key = eventObj.keyCode;
	if (typeof key != "number") {
		key = eventObj.which;
		if (typeof key != "number") {
			return true;
		}
	}
	var shiftDown = false;
	var ctrlDown = false;
	var altDown = false;
	var metaDown = false;
	if (eventObj.modifiers != null) {
		if (eventObj.modifiers == 1) {
			altDown = true;
		}
		if (eventObj.modifiers == 2) {
			ctrlDown = true;
		}
/*		Alt GR is 3 - it is a combination of the ctrl key and the alt key held down simultaneously.
		In IE and Opera the ctrlKey property is true when Alt GR is held down. */
		if (eventObj.modifiers == 3) {
			altDown = true;
		}
		if (eventObj.modifiers == 4) {
			shiftDown = true;
		}
		if (eventObj.modifiers == 6) {
			ctrlDown = true;
			shiftDown = true;
		}
	} else {
		shiftDown = eventObj.shiftKey;
		ctrlDown = eventObj.ctrlKey;
		altDown = eventObj.altKey;
		metaDown = eventObj.metaKey;
	}
	if (shiftDown == shift && ctrlDown == ctrl && altDown == alt) {
		var regExp;
		if (typeof matchObj == "string" && matchObj != "") {
			regExp = new RegExp(matchObj, "i");
		}
		else if (matchObj != null && typeof matchObj.exec == "function") {
			regExp = matchObj;
			if (!regExp.ignoreCase) {
				regExp.compile(regExp.source,"i");
			}
		} else {
			return true;
		}
		if (typeof eventObj.charCode == "number" && eventObj.charCode != 0) {
			key = eventObj.charCode;
		}
		if (key != 0) {
			var ch = String.fromCharCode(key);
			var found = regExp.exec(ch);
			if (found != null) {
				return handler(eventObj,obj);
			}
		}
	}
	return true;
}

/*	A function to validate keystrokes and ensure form submittal on enter. Must be invoked on keypress.
	In the regular expression/pattern, matchObj, you specify the characters which are NOT allowed. */
function validKey(eventObj, fieldObj, matchObj, submit) {
	if (fieldObj == null || typeof fieldObj != "object" || typeof fieldObj.value != "string" || eventObj == null || typeof eventObj != "object" || eventObj.type.indexOf("key") == -1) {
		alert(jslibDevErrMsg);
		return true;
	}
	if (typeof submit != "boolean" || typeof doSubmit != "function") {
		submit = false;
	}
	var key = eventObj.keyCode;
	if (typeof key != "number") {
		key = eventObj.which;
		if (typeof key != "number") {
			return true;
		}
	}
	// it's assumed that backspace always should work - in Netscape 4+ it must be specified as a valid key and this is often forgotten
	if (key == 8 && document.layers) {
		return true;
	}
	// submit on enter
	if (key == 13) {
	/*	Submit on enter is built-in in Internet Explorer 4+, Opera 6+ and Netscape 6+ if (and only if) the form has a submit button.
		And also in Netscape 4 if the form only has one input field.
		But pressing enter on selection from auto-completion (not selecting from drop-down of previous typed sequences) may also submit! */
		if (submit) {
			doSubmit(fieldObj.form);
		}
		return false;
	}
	var regExp;
	if (typeof matchObj == "string" && matchObj != "") {
		regExp = new RegExp(matchObj);
	}
	else if (matchObj != null && typeof matchObj.exec == "function") {
		regExp = matchObj;
	} else {
		return true;
	}
	
/*	In Mozilla (and Netscape6 and 7) there is also a charCode property which is 0 for tab, arrows, enter etc.
	If these keys are not explicit specified in the pattern for the regular expression they're never allowed in these browsers.
	That's not necessary in other browsers and this incompatibility is unfortunately.
	It's then preferred that they're always allowed.
	Except for submit on enter which is only allowed if the submit argument is true. */
	if (typeof eventObj.charCode == "number") {
		key = eventObj.charCode;
	}
	if (key != 0) {
		var ch = String.fromCharCode(key);
		var found = regExp.exec(ch);
		if (found != null) {
		/*	NOTE: In Internet Explorer it is necessary to set the returnValue property
			of the event object. Otherwise another eventhandler for the same event may
			overrule the return value of this handler, when the event bubbles up. */
			eventObj.returnValue = false;
			return false;
		}
	}
	return true;
}

function addKeyPress(fieldObj, matchObj, submit) {
	if (typeof addEvent != "function") {
		alert("The function addEvent is not present.\nYou need to load the file JSObject.js!");
		return;
	}
	addEvent(fieldObj, "keypress", function(e, f) { return validKey(e, f, matchObj, submit); });
}

function addKeyboardShortcut(obj, matchObj, shift, ctrl, alt, handler) {
	if (typeof addEvent != "function") {
		alert("The function addEvent is not present.\nYou probably need to load the file JSObject.js!");
		return;
	}
	var doCapture = false;
	if (obj.captureEvents != null) {
		doCapture = true;
	}
	addEvent(obj, "keyup", function(e, o) { return keyboardShortcut(e, o, matchObj, shift, ctrl, alt, handler); }, doCapture);
}