﻿
//handle key press event for text boxes
function NumbersOnly_OnKeyDown(source, evt, wholeNumbersOnly) {
	var value = source.value;
	var decimals = value.split(".").length - 1;
	var dashes = value.split("-").length - 1;
	var charCode = (evt.which) ? evt.which : evt.keyCode
	if (wholeNumbersOnly) {
		if (charCode == 190 || charCode == 110) {
			//alert("Please enter whole numbers only");
			Event.stop(evt);
		}
	} 
	else {
		if ((charCode == 190 || charCode == 110) && decimals == 1) {
			//already a decimal
			Event.stop(evt);
		}
	}

	if ((charCode < 96 || charCode > 105) &&
		(charCode < 48 || charCode > 57) &&
		!(charCode > 36 && charCode < 41) &&
		!(charCode == 46) &&
		!(charCode == 8) &&
		!(charCode == 9) &&
		!(charCode == 35 || charCode == 36) &&
		!(charCode == 190 || charCode == 110)) {
		Event.stop(evt);
	} else if ((charCode > 47 && charCode < 58) && evt.shiftKey) {
		Event.stop(evt);
	}
	
	return true;
}

function Zip_OnKeyDown(source, evt) {
	var value = source.value;
	var charCode = (evt.which) ? evt.which : evt.keyCode
	if (charCode == 189 || charCode == 109) {
		//only allow one dash
		if (value.indexOf('-') > -1) {
			//already is one, disallow another
			Event.stop(evt);
		}
	}	else if ((charCode < 96 || charCode > 105) &&
		(charCode < 48 || charCode > 57) &&
		!(charCode > 36 && charCode < 41) &&
		!(charCode == 46) &&
		!(charCode == 9) &&
		!(charCode == 35 || charCode == 36) &&
		!(charCode == 8)) {
			Event.stop(evt);
		} else if ((charCode > 47 && charCode < 58) && evt.shiftKey) {
			Event.stop(evt);
		}

	return true;
}

//handle key press event for text boxes
function NumbersOnly_OnKeyPress(textBox, evt, wholeNumbersOnly, allowNegative, formatted) {
	var value = textBox.value;
	var decimals = value.split(".").length - 1;
	var dashes = value.split("-").length - 1;
	var charCode = (evt.which) ? evt.which : evt.keyCode

	//check wholenumbers and decimals
	if (wholeNumbersOnly) {
		//whole numbers only - kill decimals
		if (charCode == 46) {
			alert("Please enter whole numbers only");
			Event.stop(evt);
		}
	} else {
		//not whole numbers, allow one decimal
		if (charCode == 46 && decimals ==1) {
			Event.stop(evt);
		}
	}

	//allow only numbers and one decimal point and one dash - key up event removes dashes in incorrect location
	if ((charCode < 48 || charCode > 57) &&
		(charCode != 45 || (!(allowNegative) && charCode == 45) || (charCode == 45 && dashes == 1)) &&
		(charCode != 0 && charCode != 8)) {
		Event.stop(evt);
	}

	return true;
}

//handle key up event in text boxes
function NumbersOnly_OnKeyUp(textBox) {
	var value = textBox.value;
	var valueNoCommas = value.replace(/,/g, '');  //remove commas from the formatted string before processing
	var origValue = textBox.getAttribute("origValue");

	if ((valueNoCommas.length > 0)) {
		if (isNaN(valueNoCommas)) {
			var stripResult = stripDuplicateChars(value, '-', 0, 1); // strip excess minus signs
			textBox.value = stripResult.newString;
			setCaretToPosition(textBox, stripResult.caretPosition);
		}
		else {
			if (parseFloat(valueNoCommas) > 999999999.999) {
				textBox.value = origValue;
				alert("Input too large.  \n\nYour change has been undone.");
			}
			else if (parseFloat(valueNoCommas) < -999999999.999) {
				textBox.value = origValue;
				alert("Input too small.  \n\nYour change has been undone.");
			}
		}
	}
}


// --------------------------------------------------------------------
// accepts two strings (str) and (stripChar), and two integers (howMany) and (startIndex) 
// as arguments: the 'stripChar' character specified is allowed to 
// occur 'howMany' times in the 'str' string, counting from the starting 
// index 'startIndex'.
// ie: stripDuplicateChars("xxxYYYxxxYYY", "x", 2, 0) --> "xxYYYYYY"
//     stripDuplicateChars("xxxYYYxxxYYY", "Y", 1, 4) --> "xxxYYxxx"
// --------------------------------------------------------------------
// returns the modified string
// --------------------------------------------------------------------
function stripDuplicateChars(str, stripChar, howMany, startIndex) {
	var count = 0;
	var stripped = str.substring(0, startIndex);
	var chr;
	var caretPosition = str.length;

	for (var i = startIndex; i < str.length; i++) {
		chr = str.substring(i, i + 1);
		if (chr == stripChar) {
			if (count < howMany) { stripped = stripped + chr; }
			count++;
			caretPosition = i;
		}
		else { stripped = stripped + chr; }
	}
	return new stripResult(stripped, caretPosition);
}

function stripResult(newString, caretPosition) {
	this.newString = newString;
	this.caretPosition = caretPosition;
}

function setCaretToPosition(textBox, position) {
	if (textBox.setSelectionRange) {
		textBox.focus();
		textBox.setSelectionRange(position, position);
	}
	else if (textBox.createTextRange) {
		var selectRange = textBox.createTextRange();
		selectRange.move("character", position);
		selectRange.select();
	}
}






//----------------------------------------------------
//OLD FUNCTIONS
//----------------------------------------------------

function numbersOnlyUnformatted_KeyPress(el, evt) {
	var charCode = (evt.which) ? evt.which : evt.keyCode
	if (charCode == 46) {
		alert("Please enter whole numbers only");
	}
	if (charCode > 31 && !(charCode == 39 || charCode == 37) && charCode != 46 && (charCode < 48 || charCode > 57)) {
		Event.stop(evt);
	}
	return true;
}

function numbersOnlyFormatted_KeyPress(el, evt) {
	var charCode = (evt.which) ? evt.which : evt.keyCode
	alert(charCode);
	if (charCode == 46) {
		alert("Please enter whole numbers only");
	}
	if (charCode > 31 && !(charCode == 39 || charCode == 37) && charCode != 46 && (charCode < 48 || charCode > 57)) {
		Event.stop(evt);
	}
	return true;
}


function zip_KeyPress(el, evt) {
	var charCode = (evt.which) ? evt.which : evt.keyCode
	if (!(charCode == 39 || charCode == 37 || charCode == 46 || charCode == 45) && (charCode > 31 && (charCode < 48 || charCode > 57))) {
		Event.stop(evt);
	}
	if (charCode == 45) {
		//only allow one dash
		if (el.getValue().indexof('-') > -1) {
			//already is one, disallow another
			Event.stop(evt);
		}
	}
	return true;
}



