/*
 * $Header: /Engine/keypress.js   2   2005-11-10 11:37:57+01:00   mkorthout $
 * Created on:      29-Januari-2004
 * Original author: Eric Ruis
 *
 * Description:
 *	Implements functions to monitor and restrict the keys pressed in for example
 *	an input text box.
 */

var keyPressInput = new kp_setup();

// Constructor
function kp_setup() {
	this.integer = kp_integer;
	this.numeric = kp_numeric;
	this.maxlen = kp_maxlen;
	return this;
}

// returns the current selection within the document. If nothing is selected the method returns null.
function kp_getSel() {
	var txt = null;

	if (document.getSelection) txt = document.getSelection();
	else if (document.selection) txt = document.selection.createRange().text;

	if (txt != null && txt == "") return null;
	return txt;
}

// Determine whether the specified maxlen argument is a numeric
function kp_getMaxlen(maxlen) {
	if (maxlen != null) {
		// is maxlen a number?
		var sv = new String(maxlen);
		var re = new RegExp(/\d+/);
		var m = sv.match(re);

		// if the returned match equals the initial number, its contents must be numeric.
		if (m == null || String(m).length != sv.length)
			maxlen = null;
		else
			maxlen = parseInt(m);
	}

	return maxlen;
}

// key processing
function kp_integer(maxlen) {
	maxlen = kp_getMaxlen(maxlen);

	if (maxlen != null) {
		if (event.srcElement != null && event.srcElement.value != null) {
			// check against the maximum length
			var v = event.srcElement.value;
			if (v.length >= maxlen && kp_getSel() == null) {
				event.returnValue = false;
				return;
			}
		}
	}
	// check whether the input character is a number
	if ((event.keyCode < 48 || event.keyCode > 57))
		event.returnValue = false;
}

// check the maximum length of the input
function kp_maxlen(maxlen) {
	maxlen = kp_getMaxlen(maxlen);

	if (maxlen != null) {
		if (event.srcElement != null && event.srcElement.value != null) {
			// check against the maximum length
			var v = event.srcElement.value;
			if (v.length >= maxlen && kp_getSel() == null) {
				event.returnValue = false;
			}
		}
	}
}

// check whether the input is a float
function kp_numeric(maxlen) {
	var decimalSeparatorCode = g_decimalSeparator.charCodeAt(0);
	var thousandSeparatorCode = g_thousandSeparator.charCodeAt(0);

	// is maxlen really a number?
	maxlen = kp_getMaxlen(maxlen);

	// check the contents of the box and its length
	if (event.srcElement && event.srcElement.value) {
		var v = event.srcElement.value;

		// count thousand and decimal separators. These separators do not count as a character for the
		// maximum length restriction.
		if (maxlen != null) for (var i=0;i<v.length;i++) if (v.charAt(i)==g_thousandSeparator || v.charAt(i)==g_decimalSeparator) maxlen++;

		// check against the maximum length
		// In case kp_getSel returns a non-null selection, the new character will replace the selection
		if (maxlen != null && kp_getSel() == null && v.length >= maxlen) {
			event.returnValue = false;
		}
		// A thousand separator must appear before the decimal separator. Thousand separators appear between
		// groups of 3 digits.
		else if (event.keyCode == thousandSeparatorCode) {
			var ixp = v.lastIndexOf(g_thousandSeparator);
			var ixn = v.length;
			if (ixp != -1 && (ixn-ixp) != 4)
				event.returnValue = false;
		}
		// there can be only one decimal separator
		else if (event.keyCode == decimalSeparatorCode) {
			var ids = v.indexOf(g_decimalSeparator);
			if (ids != -1)
				event.returnValue = false;
		}
		// the input is not a thousand or a decimal separator thus must be a numeric character
		else if (event.keyCode < 48 || event.keyCode > 57) {
			event.returnValue = false;
		}
	}
	else {
		if (event.keyCode < 48 || event.keyCode > 57)
			event.returnValue = false;
	}
}
