// Text Input limiters

// Returns true if the length of the text in 
// the source element of the event plus the 
// current key typed is less than 'maxChars'.
// Intended for use in 'onkeydown' event.

function limitLength(event, maxChars)
{
	// Allow special keys
	if (limitSpecial(event)) return true;

	var keycode = (event.keyCode) ? event.keyCode : event.charCode;
	var source = (event.srcElement) ? event.srcElement : event.target;

	var value = source.value + String.fromCharCode(keycode);
	if (value.length > maxChars) return false;
	
	return true;
}


function jumpAtLength(event, maxChars, elemID)
{
	if (!limitLength(event, maxChars))
	{
		var next = document.getElementById(elemID);
		if (next != null) next.focus();
	} 
}

// Returns true if a control key is typed
// Intended for use in 'onkeydown' event.
function limitSpecial(event)
{
	var keycode = (event.keyCode) ? event.keyCode : event.charCode;

	// Allow Backspace
	if (keycode == 8) return true;
	// Allow Tab
	if (keycode == 9) return true;
	// Allow Enter
	if (keycode == 13) return true;
	// Allow Delete
	if (keycode == 46) return true;
	// Allow Left Arrow
	if (keycode == 37) return true;
	// Allow Right Arrow
	if (keycode == 39) return true;
	
	return false;
}

// Returns true if a numeric character is typed
// Intended for use in 'onkeydown' event.
function limitNumeric(event)
{
	var keycode = (event.keyCode) ? event.keyCode : event.charCode;
	var shifted = event.shiftKey;
	//alert("keycode= " + keycode);
	
	// Allow Numbers
	if (keycode >= 48 && keycode <= 57 && !shifted) return true;
	// Allow Keypad Numbers
	if (keycode >= 96 && keycode <= 105) return true;
	// Allow Control Keys
	if (limitSpecial(event)) return true;
	
	return false;
}

// 3/27/07 tm written
// 8/28/07 tm rewritten to not allow "." and to work on safari.
// this function is designed to replace limitNumeric() because it also works in Opera...
// the important difference is that it is called by the onKeypress instead of the onKeydown event.
function limitNumericOnKeypress(e)
{
            var key;
	    var isCtrl = false;
	    var keychar;
	    var reg;

	    if (window.event) {
		    key = e.keyCode;
		    isCtrl = window.event.ctrlKey
	    } else if (e.which) {
		    key = e.which;
		    isCtrl = e.ctrlKey;
	    }
    	
	    if (isNaN(key)) return true;
    	
	    keychar = String.fromCharCode(key);
	    // check for backspace or delete, or if Ctrl was pressed
	    if (key == 0 || key == 13 || key == 8 || isCtrl) {
		    return true;
	    }

	    reg = /\d/;
	    var isFirstN = false;
	    var isFirstD = false;
    	
	    return isFirstN || isFirstD || reg.test(keychar);
}

// Returns true if a numeric or decimal character is typed
// Intended for use in 'onkeydown' event.
function limitDecimal(event)
{
	var keycode = (event.keyCode) ? event.keyCode : event.charCode;
	var source = (event.srcElement) ? event.srcElement : event.target;
	//alert("keycode= " + keycode);
	
	// Allow Numbers
	if (limitNumeric(event)) return true;
	// Allow Decimal
	if ((keycode == 190 || keycode == 110) && source.value.indexOf('.') == -1) return true;
	
	return false;
}

// Returns true if an alphabetic character is typed
// Intended for use in 'onkeydown' event.
function limitAlpha(event)
{
	var keycode = (event.keyCode) ? event.keyCode : event.charCode;
	//alert("keycode= " + keycode);
	
	// Allow letters
	if (keycode >= 65 && keycode <= 90) return true;
	// Allow Control Keys
	if (limitSpecial(event)) return true;
	
	return false;
}

// Returns true if a numeric or alphabetic character is typed
// Intended for use in 'onkeydown' event.
function limitAlphaNumeric(event)
{
	if (limitAlpha(event)) return true;
	if (limitNumeric(event)) return true;
	
	return false;
}