
var g_aIgnore = Array(8,9,13,37,38,39,40,35,36,46);

var _dlg_close_warn=1; // for bg_popup.css function __dlg_close

function GetResultingValue(ptObject, strInsert) {
	//IE support
	var strPostValue = "";
	if (document.selection) {
		var trCaret = window.document.selection.createRange();
		var trPrefix = ptObject.createTextRange();
		
		var trSuffix = trPrefix.duplicate();
		
		trPrefix.setEndPoint("EndToStart", trCaret);
		
		trSuffix.setEndPoint("StartToEnd", trCaret);
		strPostValue = trPrefix.text + strInsert + trSuffix.text;
	}
	//MOZILLA/NETSCAPE support
	else if (ptObject.selectionStart || ptObject.selectionStart == '0') {
		var startPos = ptObject.selectionStart;
		var endPos = ptObject.selectionEnd;
		strPostValue = ptObject.value.substring(0, startPos) + strInsert + ptObject.value.substring(endPos, ptObject.value.length);
	} 
	//SAFARI support, 
	//I know this isn't quite right, but if anyone can get it to work let us know!!
	else if (window.getSelection) {
		strPostValue = ptObject.value + strInsert;
	}
	return strPostValue;
}

function IsNotNumber(strValue) {
	if (strValue == ".") return false;
	
	if (isNaN(parseFloat(strValue,10))) return true;
	
	if (strValue.match(/.*[\+\-]/) != null) return true;
	
	if (strValue.match(/[^0123456789\-\+\.]/) != null) return true;
	
	if (strValue.match(/.+\..+\./) != null) return true;
	
	return false;
}




<!-- Filter decimal_filter.js -->

function decimal_filter_class() {
	
	this.on_key_press = function(ptObject, evt) {
		evt = (evt) ? evt : event;
		sChar = (evt.charCode) ? evt.charCode : evt.keyCode;
		
		//This will allow backspace to work.
		for (var n =0; n < g_aIgnore.length; n++) {
			if (sChar == g_aIgnore[n]) return true;
		}
		var strNewVal = GetResultingValue(ptObject, String.fromCharCode(sChar));
		
		if (this.isValueIllegal(strNewVal)) {
			return false;
		}
		return true;
	}
	
	this.onBlur = function(ptObject) {
		var iDPPos = ptObject.value.indexOf(".");
		if (iDPPos == -1) return;
		
		var bValueChanged = false;
		
		if (iDPPos == ptObject.value.length -1) {
			ptObject.value = ptObject.value.substr(0, ptObject.value.length -1);
			bValueChanged = true;
		}
		
		if (iDPPos == 0) {
			var dNewValue = "0" + ptObject.value;
			ptObject.value = dNewValue;
			bValueChanged = true;
		}
		
		if (bValueChanged) ptObject.fireEvent("onchange");
	}
	
	this.onFocus = function(ptObject) {
		//Do nothing for decimal
	}
	
	this.onPaste = function(ptObject, evt) {
		var strNewVal = GetResultingValue(ptObject, String.fromCharCode(evt.charCode));
		alert(strNewVal);
		if (this.isValueIllegal(strNewVal)) {
			return false;
		}
		return true;
	}
	
	this.isValueIllegal = function(strValue) {
		bIsIllegal = IsNotNumber(strValue);
		if (bIsIllegal == false) {
			if (strValue.match(/\..*\./) != null) bIsIllegal = true;
		}
		return bIsIllegal;
	}
}

var decimal_filter = new decimal_filter_class();
<!-- Filter integer_filter.js -->

function integer_filter_class() {
	
	this.on_key_press = function(ptObject, evt) {
		//This will allow backspace to work.
		evt = (evt) ? evt : event;
		sChar = (evt.charCode) ? evt.charCode : evt.keyCode;
		for (var n =0; n < g_aIgnore.length; n++) {
			if (sChar == g_aIgnore[n]) return true;
		}
		var strNewVal = GetResultingValue(ptObject, String.fromCharCode(sChar));
		
		if (this.isValueIllegal(strNewVal)) {
			return false;
		}
		return true;
	}
	
	this.onBlur = function(ptObject) {
		//Do nothing for integer
	}
	
	this.onFocus = function(ptObject) {
		//Do nothing for integer
	}
	
	this.onPaste = function(ptObject, evt) {
		var strNewVal = GetResultingValue(ptObject, String.fromCharCode(evt.charCode));
		//alert(strNewVal);
		if (this.isValueIllegal(strNewVal)) {
			return false;
		}
		return true;
	}
	
	this.isValueIllegal = function(strValue) {
		var bIsIllegal = isNaN(parseInt(strValue, 10));
		if (bIsIllegal == false) {
			bIsIllegal = (strValue.match(/[^0-9]/) != null);
		}
		return bIsIllegal;
	}
}

var integer_filter = new integer_filter_class();


<!-- Filter captcha_filter.js -->

function captcha_filter_class() {
	
	this.on_key_press = function(ptObject, evt) {
		//This will allow backspace to work.
		evt = (evt) ? evt : event;
		sChar = (evt.charCode) ? evt.charCode : evt.keyCode;
		for (var n =0; n < g_aIgnore.length; n++) {
			if (sChar == g_aIgnore[n]) return true;
		}
		var strNewVal = GetResultingValue(ptObject, String.fromCharCode(sChar));
		
		if (this.isValueIllegal(strNewVal)) {
			alert('Please use only 0-9 and A-F. Lower case is ok.' + ' \n\nYou entered: "' + strNewVal + '"');
			return false;
		}
		return true;
	}
	
	this.onBlur = function(ptObject) {
		//Do nothing for integer
	}
	
	this.onFocus = function(ptObject) {
		//Do nothing for integer
	}
	
	this.onPaste = function(ptObject, evt) {
		var strNewVal = GetResultingValue(ptObject, String.fromCharCode(evt.charCode));
		//alert(strNewVal);
		if (this.isValueIllegal(strNewVal)) {
			alert('illegal paste ' + strNewVal);
			return false;
		}
		return true;
	}
	
	this.isValueIllegal = function(strValue) {
		//var bIsIllegal = isNaN(parseInt(strValue, 10));
		//if (bIsIllegal == false) {
			bIsIllegal = (strValue.match(/[^0-9A-Fa-f]/) != null);
		//}
		return bIsIllegal;
	}
}

var captcha_filter = new captcha_filter_class();


<!-- Filter money_filter.js -->

function money_filter_class() {
	
	this.on_key_press = function(ptObject, evt) {
		
		evt = (evt) ? evt : event;
		sChar = (evt.charCode) ? evt.charCode : evt.keyCode;
		
		for (var n =0; n < g_aIgnore.length; n++) {
			if (sChar == g_aIgnore[n]) return true;
		}
		
		var strOldValue = ptObject.value;
		var strNewValue = GetResultingValue(ptObject, String.fromCharCode(sChar));
		strNewValue = this.FormatUSCurrency(strNewValue, false);
		if (this.isValueIllegal(strNewValue)) return false;
		ptObject.value = strNewValue;
		
		this.SetCaretPosition(strOldValue, strNewValue,ptObject);
	
		return false;
	}
	
	this.onBlur = function(ptObject) {
		ptObject.value = this.FormatUSCurrency(ptObject.value, true);
		if (ptObject.value != ptObject.previousValue) ptObject.fireEvent("onchange");
	}
	
	this.onFocus = function(ptObject) {
		this.previousValue = ptObject.value
	}
	
	this.onPaste = function(ptObject, evt) {
		var strNewVal = GetResultingValue(ptObject, String.fromCharCode(evt.charCode));
		alert(strNewVal);
		if (this.isValueIllegal(strNewVal)) {
			return false;
		}
		return true;
	}
	
	this.isValueIllegal = function(strValue) {
		var bIsIllegal = false;
		var temp = strValue.replace(/,/g, "");
		if (strValue.match(/[^s]\$/)) bIsIllegal = true;
		else if (strValue.match(/\..*\./) != null) bIsIllegal = true;
		else if (strValue.match(/\.+\d{3}/) != null) bIsIllegal = true;
		else if (parseInt(temp.substr(1)) > 9999999999) bIsIllegal = true;
		else if (IsNotNumber(strValue.replace(/\$/g, "").replace(/,/g, "")) == true) bIsIllegal = true;
		
		return bIsIllegal;
	}
	
	
	this.FormatUSCurrency = function(strValue, bIncludeDP) {
		strValue = strValue.replace(/,/g, "");
		
		var iDPPosition = strValue.indexOf(".");
		if (iDPPosition == -1) iDPPosition = strValue.length;
		for (i = iDPPosition -3; i > 0; i -= 3) strValue = strValue.substr(0, i) + "," + strValue.substr(i);
	
		strValue = "$" + strValue.replace(/\$/g, "");
	
		strValue = strValue.replace("$,","$");
		
		if (bIncludeDP) {
			var iDP = strValue.length - strValue.indexOf(".");
			if (iDP > strValue.length) strValue += ".00";
			else if (iDP == 1) strValue += "00";
			else if (iDP == 2) strValue += "0";
			
			if (strValue == "$.00") strValue = "$0.00";
		}
		return strValue;
	}
	
	this.SetCaretPosition = function(strOld, strNew, ptObject) {
		var i = -1;
		strOld = strOld.replace(/,/g, "");
		strOld = strOld.replace(/\$/g, "");
		var strTemp = strNew.replace(/,/g, "");
		strTemp = strTemp.replace(/\$/g, "");
		var newCount = (((strTemp.length - strOld.length)<0)?1:(strTemp.length - strOld.length));
		var iInsertPoint = strNew.length;
		
		for (var x = 0; x < strNew.length; x++) {
			if ((strNew.substr(x,1) != "$") && (strNew.substr(x,1) != ",")) {
				i++;
				if (strNew.substr(x,1) != strOld.substr(i,1)) {
					iInsertPoint = x + newCount;
					break;
				}
			}
		}
		
		if (document.selection) {
			trCaret = ptObject.createTextRange();
			trCaret.collapse(true);
			trCaret.moveStart("character", iInsertPoint);
			trCaret.select();
		}
		else if (ptObject.selectionStart || ptObject.selectionStart == '0') {
			ptObject.selectionStart = iInsertPoint;
			ptObject.selectionEnd = iInsertPoint;
		}		
	}
	
	
}

var money_filter = new money_filter_class();
<!-- Filter percent_filter.js -->

function percent_filter_class() {
	
	this.on_key_press = function(ptObject, evt) {
		evt = (evt) ? evt : event;
		sChar = (evt.charCode) ? evt.charCode : evt.keyCode;
		
		//This will allow backspace to work.
		for (var n =0; n < g_aIgnore.length; n++) {
			if (sChar == g_aIgnore[n]) return true;
		}
		var strNewVal = GetResultingValue(ptObject, String.fromCharCode(sChar));
		
		if (this.isValueIllegal(strNewVal)) {
			return false;
		}
		return true;
	}
	
	this.onBlur = function(ptObject) {
		ptObject.value = this.FormatPercent(ptObject.value, true);
		if (ptObject.value != ptObject.previousValue) ptObject.fireEvent("onchange");
	}
	
	this.onFocus = function(ptObject) {
		this.previousValue = ptObject.value
	}
	
	this.onPaste = function(ptObject, evt) {
		var strNewVal = GetResultingValue(ptObject, String.fromCharCode(evt.charCode));
		alert(strNewVal);
		if (this.isValueIllegal(strNewVal)) {
			return false;
		}
		return true;
	}
	
	this.isValueIllegal = function(strValue) {
		var bIsIllegal = false;
		
		if (strValue.match(/\%.*\%/) != null) bIsIllegal = true;
		else if (strValue.match(/\%.+/) != null) bIsIllegal = true;
		else if (strValue.match(/\..*\./) != null) bIsIllegal = true;
		else if (parseInt(strValue) > 999) bIsIllegal = true;
		else if (strValue.match(/\.+\d{5}/) != null) bIsIllegal = true;
		else if (IsNotNumber(strValue.replace("%", "").replace(" ", "")) == true) bIsIllegal = true;
		
		return bIsIllegal;
	}
	
	this.FormatPercent = function(strValue, bIncludeDP) {
		strValue = strValue.replace(/\%/g, "");
		if (strValue.length != 0) {
			while (strValue.charAt(0) == "0") {
				strValue = strValue.substr(1);
			}
			if (strValue.length == 0) strValue = "0";
			var iDP = strValue.length - strValue.indexOf(".");
			if (iDP == strValue.length) strValue = "0" + strValue;
			if (iDP > strValue.length) strValue += ".00";
			else if (iDP == 1) strValue += "00";
			else if (iDP == 2) strValue += "0";
			else if ((iDP > 2) && (iDP < strValue.length)) strValue =  strValue.substr(0,strValue.length - iDP+5);
			
			// Ensure number is postfixed
			strValue = strValue + " %";
		}
		return strValue;
	}
}

var percent_filter = new percent_filter_class();

