/* function to calculate square footage */
function calcInvSqft(fldRootWd,fldRootHt,fldCalcTarget) {
	// specify the name of your form
	var thisForm = "editForm";
	// other vars
	var calcSqFt = 0;
	var widthFt  = fldRootWd+"Ft"; var widthIn  = fldRootWd+"In";
	var heightFt = fldRootHt+"Ft"; var heightIn = fldRootHt+"In";
	// initiate some vars
	var widthFtVal  = ""; var widthInVal  = ""; var widthNum  = "";
	var heightFtVal = ""; var heightInVal = ""; var heightNum = "";
	// populate fields with default values on page load
	with (document.forms[thisForm]) {
		// start with determining decimal value of width
		// alert("show: "+elements[widthFt].value.length+" and "+parseInt(elements[widthFt].value)+" and "+elements[widthIn].value.length+" and "+parseInt(elements[widthIn].value));
		if (elements[widthFt].value.length > 0) {
			widthFtVal = parseInt(elements[widthFt].value); elements[widthFt].value = parseInt(elements[widthFt].value);
		}
		if (elements[widthIn].value.length > 0) {
			widthInVal = parseInt(elements[widthIn].value); elements[widthIn].value = parseInt(elements[widthIn].value);
		}
		if (elements[heightFt].value.length > 0) {
			heightFtVal = parseInt(elements[heightFt].value); elements[heightFt].value = parseInt(elements[heightFt].value);
		}
		if (elements[heightIn].value.length > 0) {
			heightInVal = parseInt(elements[heightIn].value); elements[heightIn].value = parseInt(elements[heightIn].value);
		}
		/* alert ("show: "+parseInt(widthFtVal)+" plus "+widthInVal+" with "+parseInt(heightFtVal)+" plus "+heightInVal); */
		if (parseInt(widthFtVal) > 0 && widthFtVal > 0) {
			widthNum = widthFtVal; if (parseInt(widthInVal) > 0 && widthInVal > 0) { widthNum += (widthInVal / 12); }
		}
		// determine decimal value of height
		if (parseInt(heightFtVal) > 0 && heightFtVal > 0) {
			heightNum = heightFtVal; if (parseInt(heightInVal) > 0 && heightInVal > 0) { heightNum += (heightInVal / 12); }
		}
		// alert("show: '"+widthNum+"' and '"+heightNum+"'"); 
		// send square footage to targeted field
		if (widthNum > 0 && heightNum > 0) {
			calcSqFt = widthNum * heightNum;
			/* alert("Square Footage: "+calcSqFt); */
			elements[fldCalcTarget].value = calcSqFt.toFixed(2);
		}
	}
}

/* function to calculate total costs */
function calcInvCosts(fldRoot,numFlds,fldCalcTarget) {
	// specify the name of your form
	var thisForm = "editForm";
	// other vars
	var calcCost = 0;
	// populate fields with default values on page load
	with (document.forms[thisForm]) {
		for (i=1; i <= numFlds; i++) {
			currentField = fldRoot + i;
			// alert("show: form."+currentField); 
			if (elements[currentField].value.length > 0) {
				currentValue = parseFloat(elements[currentField].value)
				calcCost += currentValue;
				elements[currentField].value = currentValue.toFixed(2);
			}
		}
		// alert("Cost Total: "+calcCost); 
		elements[fldCalcTarget].value = calcCost.toFixed(2);
	}
}

/* call this function to automatically ensure a TIME field is converted to format: HH:MM */
function timeFormatValid(formfield) {
	var strval = formfield.value;
	var strvalmod = "";
	var strvalfinal = "";
	var strValidChars = "0123456789";
	var pos = strval.indexOf(":");
	if (strval.length > 0) { /* only act on field value if user has entered something */
		if (pos < 0) {
			if (strval.length == 1 || strval.length == 3) { strval = "0" + strval; }
			for (i=0; i < 4; i++) {
				if (strval.charAt(i) == "" || strValidChars.indexOf(strval.charAt(i)) == -1) { strvalmod += "0"; } else { strvalmod += strval.charAt(i); }
			}
			if (strvalmod.substring(0,2) > 23) { strvalfinal += "23"; } else { strvalfinal += strvalmod.substring(0,2); }
			strvalfinal += ":";
			if (strvalmod.substring(2,4) > 59) { strvalfinal += "59"; } else { strvalfinal += strvalmod.substring(2,4); }
		} else {
			if (strval.length == 2 || strval.length == 4) { strval = "0" + strval; }
			if (strval.substring(0,2) > 23) { strvalfinal += "23"; } else { strvalfinal += strval.substring(0,2); }
			strvalfinal += ":";
			if (strval.substring(3,5) > 59) { strvalfinal += "59"; } else { strvalfinal += strval.substring(3,5); }
		}
		formfield.value = strvalfinal;
	}
}

/* call this function to automatically ensure a DATE field is converted to format: MM/DD/YYYY */
function dateFormatValid(formfield) {
	var strval = formfield.value;
	var strvalmod = ""; var strvalfinal = ""; var strValidChars = "0123456789";
	var pos1 = -1;
	for (i=0; i <= strval.length; i++) {
		if (strValidChars.indexOf(strval.charAt(i)) == -1) { pos1 = 1; }
	}
	if (strval.length > 5 && pos1 < 0) { /* only act on field value if user has entered valid initial value */
		if (strval.length == 6) {
			strval = strval.substring(0,4) + "20" + strval.substring(4,6);
		}
		strvalmod = strval;
		if (strvalmod.substring(0,2) > 12) { strvalfinal += "12"; } else { strvalfinal += strvalmod.substring(0,2); }
		strvalfinal += "/";
		if (strvalmod.substring(2,4) > 31) { strvalfinal += "31"; } else { strvalfinal += strvalmod.substring(2,4); }
		strvalfinal += "/";
		strvalfinal += strvalmod.substring(4,8);
		formfield.value = strvalfinal;
	} else if (pos1 < 0) {
		formfield.value = "";
	}
}

// Autotab Begin
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input, len, e) {
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if (input.value.length >= len && !containsElement(filter,keyCode)) {
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input)+1) % input.form.length].focus();
		input.form[(getIndex(input)+1) % input.form.length].select();
	}
	function containsElement(arr, ele) {
		var found = false, index = 0;
		while (!found && index < arr.length)
			if (arr[index] == ele) found = true;
			else index++;
		return found;
	}
	function getIndex(input) {
		var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1)
			if (input.form[i] == input) index = i;
			else i++;
		return index;
	}
	return true;
}
// Autotab End