﻿// called before calculation, stops calculation (returns false) if a non-changing key is hit (tab, arrows etc.)
function isValidKey(){
	var key = event.keyCode;
	return ((key >= 48 && key <= 57) && !(window.event.shiftKey) // (key >= 48 && key <= 57) = Top #'s
			|| (key >= 96 && key <= 105) // (key >=96 && key <=105) Keypad #'s
			|| key == 8					 // 8 BckSpce
			|| key == 46				 // Del 46
			|| key == 109 || key == 189	 // 109 -, 189 -
			|| key == 110 || key == 190  // 109 ., 189 .
			|| key == 0);
}

function checkKeyStroke(event){
	var key = event.keyCode;
	var curVal = (event.srcElement || event.target).value
	//alert(key);

	// only allow 1 minus sign and put at beginning, otherwise make positive
	if(key == 109 || key == 189){ // 109 -, 189 -
		if(document.selection.createRange().text != curVal){ 
			if(curVal.indexOf("-") < 0)
				(event.srcElement || event.target).value = "-" + curVal;
			else
				(event.srcElement || event.target).value = curVal.substring(1, curVal.length);
		}
		else
			return true;
	}
	
	// only allow one decimal point in field
	if(key == 110 || key == 190){ // 110 ., 190 .
		if(document.selection.createRange().text == curVal)
			return true;
		else if(curVal.indexOf(".") >= 0)
			return false;
	}
		
	return ((key >= 48 && key <= 57) && !(event.shiftKey) // (key >= 48 && key <= 57) = Top #'s
			|| (key >= 96 && key <= 105) // (key >=96 && key <=105) Keypad #'s
			|| key == 8 || key == 9		// 8 BckSpce, 9 Tab
			|| key == 46				// Del 46
			|| key == 37 || key == 39	// 37 Left Arrow, 39 Right Arrows
			|| key == 38 || key == 40	// 38 Up Arrow, 40 Down Arrows
			|| key == 36 || key == 35	// 36 Home, 35 End
			|| key == 45				// 45 Insert
			|| key == 110 || key == 190 // 109 ., 189 .
			|| event.ctrlKey);
}
var OnFocus_Value = "NoVal";
function OnFocus_SaveValue(obj)
{
	OnFocus_Value = obj.value;
}
function OnBlur_IsEqualValue(obj)
{
	return (OnFocus_Value == obj.value);
}

function checkKey(allowQuotes, allowNumbers, allowHyphen)
{
	return checkKey2(true, allowQuotes, allowNumbers, allowHyphen,
		false, false, true, true);
}

function checkKey2(allowAlf, allowQuotes, allowNumbers, allowHyphen,
	allowBrackets, allowCommas, allowSpaces, allowPoints)
{
	var key = event.keyCode;

	var isAlf = (key >= 65 /* a */ && key <= 90 /* z */);

	var isQuote = (key == 222) && !event.shiftKey;
	var isNumber = ((key >= 48 /* 0 */ && key <= 57 /* 9 */) ||
		(key >= 96 /* Keypad 0 */ && key <= 105 /* Keypad 9 */)) && !event.shiftKey;
	var isHyphen = (key == 109 || key == 189 /* - */) && !event.shiftKey;

	var isBracket = (key == 57 /* ( */ || key == 48 /* ) */) && event.shiftKey;
	var isComma = (key == 188 /* , */ || key == 186 /* ; */);
	var isSpace = (key == 32 /* space */);
	var isPoint = (key == 110 || key == 190 /* . */) && !event.shiftKey;

	return checkKey_IsSpecial()
		|| (allowAlf && isAlf)
		|| (allowQuotes && isQuote)
		|| (allowNumbers && isNumber)
		|| (allowHyphen && isHyphen)
		|| (allowBrackets && isBracket)
		|| (allowCommas && isComma)
		|| (allowSpaces && isSpace)
		|| (allowPoints && isPoint)
		;
}

function checkKey_IsSpecial()
{
	var key = event.keyCode;

	return (key == 8 /* backspace */ || key == 9 /* tab */
		|| key == 46 /* del */ || key == 37 /* left arrow */ || key == 39 /* right arrow*/
		|| key == 36 /* home */ || key == 35 /* end */ );
}

