
/* ####### VARIABLE SANITY AND CONVERSION FUNCTIONS ####### */

function trimString(strIn) {
	if (/^\s/.test(strIn)) { strIn = strIn.replace(/^\s{1,}/, ""); }
	if (/\s$/.test(strIn)) { strIn = strIn.replace(/\s{1,}$/, ""); }
	return strIn;
}
function getIntegerString(strIn) {
	return strIn.replace(/[^0-9]/g, "");
}
function getInteger(vNum) {
	vNum = getIntegerString(vNum.toString());
	if (vNum == "") { vNum = 0; }
	return Number(vNum);
}
function getDecimalString(strIn) {
	strIn = strIn.replace(/[^0-9\.]/g, "");
	var iPoint = strIn.indexOf(".");
	if (iPoint > -1) {
		strIn = strIn.substring(0, iPoint + 1) + getIntegerString(strIn.substring((iPoint + 1), strIn.length));
	}
	return strIn;
}
function addCommasToNumString(strIn) {
	var arrTemp = strIn.split("");
	var i = strIn.length - 4;
	var iPoint = strIn.indexOf(".");
	if (iPoint > -1) { i -= (strIn.length - iPoint); }
	for (i; i >= 0; i-=3) { arrTemp[i] += ","; }
	return arrTemp.join("");
}

/* ####### FORM FIELD FILTERING FUNCTIONS ####### */

function filterInteger(hField) {
	hField.value = getIntegerString(hField.value);
}
function filterIntegerAddCommas(hField) {
	hField.value = addCommasToNumString(getIntegerString(hField.value));
}
function filterDecimal(hField) {
	hField.value = getDecimalString(hField.value);
}
function filterDecimalAddCommas(hField) {
	hField.value = addCommasToNumString(getDecimalString(hField.value));
}


/* ####### SPECIAL ####### */

function autoTab(hField, event) {
	// Automatically focuses the next form field if you reach the maxlength of a text input while typing.
	if ("0,8,9,16,17,18,38,39,40,46".indexOf(event.keyCode.toString()) != -1) { return; }
	if (hField.value.length < getInteger(hField.getAttribute("maxlength"))) { return; }
	var form = hField.form;
	for (var i=0; i < form.length; i++) {
		if ((hField == form[i]) && (i < form.length - 1)) { form[i+1].focus(); return; }
	}
}

