// (c) 2005 Pine Grove Software, All rights reserved.
// This script MAY NOT be used without prior, written permission from the copyright holder.
// Contact: kthompson@pine-grove.com
// onKeypress event handler
function onlyNumbers(maskControl, e, precision)
{
	var key;
	var keychar;
	var reg;
	var isValid = false;
	var isSelected = false;
	var numText = "";
	var strText = maskControl.value;
	var pos = strText.indexOf(DPnt);
	var hasDecimal = (pos >= 0);

//	setCaretToEnd(maskControl);
    // sequence of test conditions import so that it works in Chrome
    if(e.which) {
		// for Mozilla Firefox, Netscape, Chrome see if a range is selected
		isSelected = ( ( maskControl.selectionEnd - maskControl.selectionStart ) != 0 );
		key = e.which; 
	}
	else if(window.event) {
		// for IE, see if a range is selected
		var rng = document.selection.createRange()
		isSelected = ( rng.text != "" );
		// for IE, e.keyCode or window.event.keyCode can be used
		key = e.keyCode; 
	}
	else {
		// no event raised in Mozilla/Firefox for l. cursor key "pgup", "pgdn", "home" or "end" keys
		// r. cursor key, #39, can be used to clear a selection without clearing the selected text itself
		if ( ( e.keyCode == 37 ) ||
			( e.keyCode == 33 ) || ( e.keyCode == 34 ) ||
			( e.keyCode == 35 ) || ( e.keyCode == 36 ) ) {
			return false;
		} else {
			return true;
		}
	}
	keychar = String.fromCharCode(key);
	reg = /\d/;

	// if a number, minus, backspace or decimal char (period), then we are good
	isValid = ( ( reg.test(keychar) ) || ( key == 45 ) || ( key == 8 ) || ( ( keychar == DPnt ) && ( !hasDecimal ) ) );
	if ( ( isValid ) && ( !isSelected ) && ( key != 8 ) && ( maskControl.value.length < maskControl.maxLength ) ) {
        // append the char just typed to the string about to be formatted
		numText = maskControl.value+keychar;
		numText = addSeparatorsNF( maskControl, numText, precision );
		if ( key == 45 ) {
            // if negative sign, clear entry so minus is always first char.
            maskControl.value = "";
        }
        else if ( numText.length > 1) {
        	// remove the char just typed - it will be added by system
			maskControl.value = numText.substr( 0, numText.length-1 );
		}
		else {
			// required to eliminate leading zeros
			maskControl.value = keychar;
			return false;
		}
	}
	return isValid;
};

// onKeyup event handler, presently, this event not needed
function FormatNum(maskControl, e) {
	var numText = "";
	var strText = maskControl.value;
	for (i = 0;	i < strText.length; i++) {
		if (strText.charAt(i) != Sep)
		numText = numText +	strText.charAt(i);
	}
	numText = addSeparatorsNF( maskControl, numText, 2 );
	maskControl.value = numText.substr( 0, numText.length-1 );
};

// onKeydown event handler
function NoCursorKeys(e)
{
	// this blocks left cursor key, "pgup", "pgdn", "home" or "end" keys
	// in IE but not Mozilla/Firefox
	// note: r. cursor key, #39, can be used to clear a selection without clearing the selected text itself
	if ( ( e.keyCode == 37 ) || 
		( e.keyCode == 33 ) || ( e.keyCode == 34 ) ||
		( e.keyCode == 35 ) || ( e.keyCode == 36 ) ) {
		return false;
	} else {
		return true;
	}
};

// when field receives focus, select the text
// this will force insertion point to right align in textbox
function Arrive(me, e)
{
	me.select();
};


// strip old formatting and add new formatting
// check for max length of numText
function addSeparatorsNF( ctrl, nStr, precision ) {
	var dpos = nStr.indexOf(DPnt);
	var rgx = /(\d+)(\d{3})/;
	var numText = "";

	// Integer portion cannot be too long
	if ( ( dpos == -1 ) && ( precision != 0 ) ) {	
		if ( nStr.length > ( ctrl.maxLength - precision - 1 ) ) {
		 return nStr.substr( 0, nStr.length - 1 );
		}
	}

	// remove formatting
	for (i = 0;	i < nStr.length; i++) {
		if (nStr.charAt(i) != Sep)
		numText += nStr.charAt(i);
	}

	if ( dpos == -1 ) {
		// remove possible leading zeros
		while ( ( numText.length > 1 ) && ( numText.substr( 0, 1 ) == "0" ) ) {
			numText = numText.substr( 1, numText.length-1 )
		}

		while ( rgx.test( numText ) ) {
			numText = numText.replace(rgx, "$1" + Sep + "$2");
		}
		return numText;
	} else {
		var n = numText.split(DPnt);
		while ( rgx.test( n[0] ) ) {
			n[0] = n[0].replace(rgx, "$1" + Sep + "$2");
		}
		if ( n[1].length <= precision ) {
			return n[0]+DPnt+n[1]; //.substr( 0, n[1].length-1 );
		} else {
			return n[0]+DPnt+n[1].substr( 0, precision );
		}
	}
};


function setCaretToEnd (control) {
  if (control.createTextRange) {
    var range = control.createTextRange();
    range.collapse(false);
    range.select();
  }
  else if (control.setSelectionRange) {
    control.focus();
    var length = control.value.length;
    control.setSelectionRange(length, length);
  }
};

function setCaretToStart (control) {
  if (control.createTextRange) {
    var range = control.createTextRange();
    range.collapse(true);
    range.select();
  }
  else if (control.setSelectionRange) {
    control.focus();
    control.setSelectionRange(0, 0);
  }
};

// OnBlur event handler
function CheckMaskFormat(maskControl, precision, lpad, rpad ) {
	var numText = maskControl.value;
	var dpos = numText.indexOf(DPnt);

	if ( numText.length	== 0 ) {
		numText = "0";
	}

	if ( ( dpos == -1 ) && ( precision != 0 ) ) {
		numText += DPnt;
		dpos = numText.length - 1;
	}

	while ( ( ( ( numText.length-1 ) - dpos ) < precision ) && ( precision > 0 ) ) {
		// we need to pad
		numText += "0";
	}
	if ( ( lpad != "" ) && ( numText.substr( 0, 1 ) != lpad ) && ( numText.length < maskControl.maxLength ) ) {
		numText = lpad + numText;
	}
	maskControl.value = addSeparatorsNF( maskControl, numText, precision );
	if ( ( rpad != "" ) && ( maskControl.value.substr( maskControl.value.length-1, 1 ) != rpad ) ) {
		maskControl.value = maskControl.value + rpad;
	}

	return true;
};

