﻿function intInputInc(oInput, bShift, bAlt) {
	var iValue = parseInt(oInput.value);
	var iMaxValue = parseInt(oInput.getAttribute('max'));
	if (isNaN(iValue)) iValue = 0;
	if (bAlt && bShift) iValue += 1000;
		else if (bAlt) iValue += 100;
			else if (bShift) iValue += 10;
				else iValue += 1;
	if (iValue < 0) iValue = 0;
	if (iMaxValue && !isNaN(iMaxValue) && iValue > iMaxValue) iValue = iMaxValue;
	oInput.value = iValue;
	if (oInput.onchange) oInput.onchange();
}

function intInputDec(oInput, bShift, bAlt) {
	var iValue = parseInt(oInput.value);
	if (isNaN(iValue)) iValue = 0;
	if (bAlt && bShift) iValue -= 1000;
		else if (bAlt) iValue -= 100;
			else if (bShift) iValue -= 10;
				else iValue -= 1;
	if (iValue < 0) iValue = 0;
	oInput.value = iValue;
	if (oInput.onchange) oInput.onchange();
}

function improveInput(oInput) {
	if ( oInput.className.indexOf('int') >= 0 ) {
		oInput.onkeypress = inputKeyPress;
		oInput.oninput = inputKeyPress;
		/* up/down for MSIE */
		if ( document.all && !window.opera ) {
			oInput.onkeydown = function( evt ) {
				if ( event.keyCode == 38 ) { intInputInc( oInput, event.shiftKey, event.altKey ); return true; }
				if ( event.keyCode  == 40 ) { intInputDec( oInput, event.shiftKey, event.altKey ); return true; }
			}
		}
	}
}

var iIntInputTO = 0;
var eIntInput = null;
function setInputChangeTO(eInput) {
	if (iIntInputTO) { 
		clearTimeout(iIntInputTO);
		iIntInputTO = 0;
		eIntInput = null;
	}
	eIntInput = eInput;
	iIntInputTO = setTimeout(inputChange, 500);
}

function inputChange() {
	if (eIntInput && eIntInput.onchange) eIntInput.onchange();
	iIntInputTO = 0;
}


function inputKeyPress(evt) {
	var oEvent = window.event || evt;
	if (oEvent) {
		var oInput = this;
		var iKeyCode = oEvent.keyCode;
		var iCharCode = oEvent.charCode;
//		fadingAlert(iKeyCode + ' : ' + iCharCode);
		if (
			( iKeyCode >= 48 && iKeyCode <= 57 ) || /* numbers */
			( iCharCode && iCharCode >= 48 && iCharCode <= 57 ) /* numbers */
		) {
			setInputChangeTO(oInput);
			return true;
		}
		if ( iKeyCode == 13 || iKeyCode == 3 ) { if ( oInput.onchange ) oInput.onchange(); }
		if ( iKeyCode == 38 || iKeyCode == 63232 ) { intInputInc( oInput, oEvent.shiftKey, oEvent.altKey ); return true; }
		if ( iKeyCode == 40 || iKeyCode == 63233 ) { intInputDec( oInput, oEvent.shiftKey, oEvent.altKey ); return true; }
		if (
			iKeyCode == 8 || /* backspace */
			iKeyCode == 9 || /* tab */
			iKeyCode == 17 || /* ctrl */
			iKeyCode == 144 || /* num lock */
			iKeyCode == 35 || /* end */
			iKeyCode == 36 || /* home */
			iKeyCode == 37 || /* left arrow */
			iKeyCode == 39 || /* right arrow */

			/* mozilla */
			(iCharCode == 0 && iKeyCode == 46) || /* delete */
			
			/* safari */
			iKeyCode == 63234 || /* left arrow */
			iKeyCode == 63235 || /* right arrow */
			iKeyCode == 63272 /* forward delete */
		) {
			setInputChangeTO(oInput);
			return true;
		}
		if (
			(oEvent.metaKey || oEvent.ctrlKey) &&
			(
				(iKeyCode == 120 || iCharCode == 120) || /* cut */
				(iKeyCode ==  99 || iCharCode == 99) || /* copy */
				(iKeyCode == 118 || iCharCode == 118) /* paste */
			)
		) {
			setInputChangeTO(oInput);
			return true;
		}
		return false;
	} else return true;
}