// (Modified) Javascript Force Numeric Input
// http://www.codeproject.com/KB/scripting/JavaScriptForceNumInput.aspx

function ForceNumericInput(This, AllowDot, AllowMinus,e){
	if(arguments.length == 1){
		var s = This.value;
		var i = s.lastIndexOf("-");
		
		if(i == -1)
		return;
		
		if(i != 0)
		This.value = s.substring(0,i)+s.substring(i+1);
		return;
	}
	
	if(window.event){ var code = event.keyCode; } else { var code = e.keyCode; }
	
	switch(code){
		case 8:		// Backspace
		case 37:		// Left-Arrow
		case 39:		// Right-Arrow
		case 46:		// Delete
		if(window.event) event.returnValue=true; 
		return;
	}
	
	// Negative Numbers
	if(code == 189){	// Minus Symbol
		if(AllowMinus == false){
			if(window.event){ event.returnValue=false; } else { StopEvent(e); }
			return;
		}
		var s = "ForceNumericInput(document.getElementById('"+This.id+"'),"+AllowMinus+","+AllowDot+","+e+")";
		setTimeout(s, 250);
		return;
	}
	
	// One Decimal Limit
	if(AllowDot && code == 190){
		if(This.value.indexOf(".") >= 0){
			if(window.event){ event.returnValue=false; } else { StopEvent(e); }
			return;
		}
		if(window.event) event.returnValue=true;
		return;
	}
	
	// Characters between 0 and 9
	if( (code >= 48 && code <= 57) || (code >= 96 && code <= 105) || (code == 9) || (code == 110) ){
		if(window.event) event.returnValue=true;
		return;
	}
	
	// Window Events
	if(window.event){ event.returnValue=false; } else { StopEvent(e); }
}


function StopEvent(pE){
	if (!pE)
	if (window.event) pE = window.event;
	else return;
	if (pE.cancelBubble != null) pE.cancelBubble = true;
	if (pE.stopPropagation) pE.stopPropagation();
	if (pE.preventDefault) pE.preventDefault();
	if (window.event) pE.returnValue = false;
	if (pE.cancel != null) pE.cancel = true;
}
