
//#######################	KEY CODES	#######################
// Allowed filters: 
// integers - only the digits 0-9
// fractions - integers plus decimal point "."

integers = new Array(10);
integers[0] = 48;
integers[1] = 49;
integers[2] = 50;
integers[3] = 51;
integers[4] = 52;
integers[5] = 53;
integers[6] = 54;
integers[7] = 55;
integers[8] = 56;
integers[9] = 57;

decimal_point = new Array(1);
decimal_point[0] = 46;

specials = new Array(10);
//specials[0] = 37;	//	Left arrow !!! In MSIE 7.0 overlaps with % keycode
//specials[1] = 39;	//	Right arrow	!!! In MSIE 7.0 overlaps with ' keycode
//specials[2] = 38;	//	Top arrow !!! In MSIE 7.0 overlaps with & keycode
//specials[3] = 40;	//	Bottom arrow !!! In MSIE 7.0 overlaps with ( keycode
specials[4] = 8;	//	Backspace
//specials[5] = 46;	//	Del !!! In Opera overlaps with decimal point keycode
//specials[6] = 36;	//	Home !!! In MSIE 7.0 overlaps with $ keycode 
//specials[7] = 35;	//	End	!!! In MSIE 7.0 overlaps with # keycode
//specials[8] = 45;	//	Insert !!! In MSIE 7.0 overlaps with - keycode
specials[9] = 9;	//	Tab



//#####################	FILTER FUNCTIONS ######################

ie = (document.all) ? 1 : 0;
n = !ie;

var focused_element;
var custom_filter_name;
var typing_filter_array = new Array();

function in_array(needle, haystack){
	for(i = 0; i < haystack.length; i++){
		if(needle == haystack[i]){
			return true;
		}
	}
	return false;
}

function setTypingFilter(elmnt, custom_filter){
	focused_element = elmnt;
	if(custom_filter == 'integers'){
		custom_filter_name = custom_filter;
		typing_filter_array = integers.concat(specials);
	} else if (custom_filter == 'fractions'){
		custom_filter_name = custom_filter;
		typing_filter_array = integers.concat(decimal_point, specials);
	} else {
		custom_filter_name = custom_filter;
		typing_filter_array = integers.concat(specials);
	}

	document.onkeypress = keyDown;

	if(n){
		document.captureEvents(Event.KEYPRESS);
	}

	elmnt.onblur = function(){
		document.onkeypress = function(){};
	}

}

function keyDown(e){
	var keycode;

	if(window.event)
		keycode = window.event.keyCode;
	else if(e)
		keycode = e.which;
	else
		return true;

	if(keycode){
		if(in_array(keycode, typing_filter_array)){
			if((custom_filter_name == 'fractions') && (in_array(keycode, decimal_point))){
				if(focused_element.value.indexOf('.', 0) >= 0){
					return false
				} else {
					return true;
				}
			} else {
				return true;
			}
			return true;
		} else {
			return false;
		}
	} else {
		return true;
	}
}

