function trim(val) {
	startposn = 0;
	while ((val.charAt(startposn) == " ") && (startposn < val.length)) {
		startposn++;
	}
	if (startposn == val.length) {
		val="";
	} else {
		val = val.substring(startposn,val.length);
		endposn = (val.length) - 1;
		while (val.charAt(endposn) == " ") {
			endposn--;
		}
		val = val.substring(0, endposn + 1);
	}
	return (val);
}

function empty(string) {
	return (trim(string).length == 0);
}

function jsleft(string, len) {
	return string.substr(0,len);
}

function jsright(string, len) {
	pos = string.length - len;
	return string.substr(pos);
}

function stripZero(cString) {
	//while (cString.substr(0,1) == "0")
	while (jsleft(cString,1) == "0")
		cString = cString.substr(1);
	return cString;
}
	
function FebDays(nYear) {
	if (nYear % 4 != 0)
		return 28;
	if (nYear % 400 == 0)
		return 29;
	if (nYear % 100 == 0)
		return 28;
	else
		return 29;
}

function isDate(cMonth, cDay, cYear) {
	var aMonths = new Array();
	aMonths[1] = 31;
	aMonths[2] = 28;
	aMonths[3] = 31;
	aMonths[4] = 30;
	aMonths[5] = 31;
	aMonths[6] = 30;
	aMonths[7] = 31;
	aMonths[8] = 31;
	aMonths[9] = 30;
	aMonths[10] = 31;
	aMonths[11] = 30;
	aMonths[12] = 31;
	
	nMonth = parseInt(stripZero(cMonth),10);
	nDay   = parseInt(stripZero(cDay),10);
	nYear  = parseInt(stripZero(cYear),10);
	
	// verify all are valid #s
	if (isNaN(nMonth) || isNaN(nDay) || isNaN(nYear))
		return false;
	
	// verify month in range
	if (nMonth < 1 || nMonth > 12)
		return false;
		
	// verify year in range
	if (nYear < 1800 || nYear > 9999)
		return false;

	// verify days in range for month	
	if (nMonth == 2) {
		if (nDay < 1 || nDay > FebDays(nYear))
			return false;
	} else {
		if (nDay < 1 || nDay > aMonths[nMonth])
			return false;
	}
	
	return true;
}

function vPhoneFocus(oField, cAreaCode) {
	if (empty(oField.value) && !empty(cAreaCode) && cAreaCode != "0")
		{
		oField.value = cAreaCode + "-";
		cTextRange = oField.createTextRange();
		cTextRange.collapse(false);
		cTextRange.select();
		}
}

function vPhoneBlur(oField, cAreaCode) {
	if (oField.readOnly || oField.disabled) return true; 
	
	cValue = oField.value;
	
	if (cValue == cAreaCode + '-') {
		oField.value = "";
		return;
	}
	
	if (empty(cValue))
		return;
	
	cDigits = "";
	for (i = 0; i < cValue.length; i++)
		if (cValue.charAt(i) != "-")
			cDigits += cValue.charAt(i);
	
	if (cDigits.length != 10) {
		alert("Please enter a full phone number (XXX-XXX-XXXX)");
		oField.focus();
	} else {
		oField.value = jsleft(cDigits,3) + "-" + cDigits.substr(3, 3) + "-" + jsright(cDigits,4);
	}
}
	
function vPhoneKey(oField) {
	cValid = "0123456789-";
	
	cChar = String.fromCharCode(event.keyCode);
	if (cValid.indexOf(cChar) == -1)
		event.returnValue = false;
}

function vDateBlur(oField) {	
	if (oField.readOnly || oField.disabled) return true;
	
	lOK = true;
	
	// check for empty input
	if (empty(oField.value))
		return true;
	
	// break date down and validate
	aDate = oField.value.split("/");
	switch(aDate.length) {
		case 1:
			// no slashes entered, so we infer their location
			switch(aDate[0].length) {
				case 4:
					aDate[2] = new Date().getFullYear().toString();
					aDate[1] = jsright(aDate[0],2);
					aDate[0] = jsleft(aDate[0],2);
					lOK = isDate(aDate[0],aDate[1],aDate[2]);
					break;
				case 6:
					nYear = parseInt(jsright(aDate[0],2),10);
					if (nYear >= 0 && nYear < 50) {
						nYear += 2000;
					} else {
						if (nYear < 100)
							nYear += 1900;
					}
					aDate[2] = nYear.toString();
					aDate[1] = aDate[0].substr(2,2);
					aDate[0] = jsleft(aDate[0],2);
					lOK = isDate(aDate[0],aDate[1],aDate[2]);

					break;
				case 8:
					aDate[2] = jsright(aDate[0],4);
					aDate[1] = aDate[0].substr(2,2);
					aDate[0] = jsleft(aDate[0],2);
					lOK = isDate(aDate[0],aDate[1],aDate[2]);
					break;
				default:
					lOK = false;
			}
			break;
		case 2:
			// only month and day entered, infer year
			aDate[2] = new Date().getFullYear().toString();
			lOK = isDate(aDate[0],aDate[1],aDate[2]);
			break;
		case 3:	
			// full date entered
			nYear = parseInt(aDate[2],10);
			
			if (nYear >= 0 && nYear < 50) {
				nYear += 2000;
			} else {
				if (nYear < 100)
					nYear += 1900;
			}
			aDate[2] = nYear.toString();
			lOK = isDate(aDate[0],aDate[1],aDate[2]);
			break;
		default:
			lOK = false;
	}

	// handle valid or invalid
	if (lOK) {
		oField.value = jsright("00" + aDate[0],2) + "/" + jsright("00" + aDate[1],2) + "/" + aDate[2];
	} else {
		alert("Invalid Date");
		oField.focus();
	}
}

function vDateKeyDown(oField) {
	if (!oField.readOnly) {
		if (event.keyCode == 38 || event.keyCode == 40) {
			//up, down move date backward, forward one day (or hold Ctrl to move one month)
			if (oField.value.length == 10){
				cFieldValue = oField.value;
				if (isDate(cFieldValue.substr(0, 2), cFieldValue.substr(3, 2), cFieldValue.substr(6, 4))) {
					if (event.keyCode == 38) {nIncrement = 1} else {nIncrement = -1}
					var myDate=new Date();
					if (event.ctrlKey){ 
						if (parseInt(cFieldValue.substr(0, 2)) == 12 && nIncrement == 1)
							myDate.setFullYear(parseInt(cFieldValue.substr(6, 4)) + 1, 0, cFieldValue.substr(3, 2));
						else if (parseInt(cFieldValue.substr(0, 2)) == 1 && nIncrement == -1) {
							myDate.setFullYear(cFieldValue.substr(6, 4) - 1, 11, cFieldValue.substr(3, 2));
						} else {
							myDate.setFullYear(cFieldValue.substr(6, 4), cFieldValue.substr(0, 2) - 1 + nIncrement, cFieldValue.substr(3, 2));
						}
					} else {
						myDate.setFullYear(cFieldValue.substr(6, 4), cFieldValue.substr(0, 2) - 1, cFieldValue.substr(3, 2));
						myDate.setDate(myDate.getDate() + nIncrement);
					}
					nMonth = myDate.getMonth() + 1;
					nDay = myDate.getDate();
					nYear = myDate.getYear();
					oField.value = jsright("00" + nMonth,2) + '/' + jsright("00" + nDay,2) + '/' + nYear;
					event.returnValue = false;
				} else {event.returnValue = false;}
			}
		}
	} else {event.returnValue = false;}
}

function vDateKey(oField) {
	var cValid = "0123456789/";
	cChar = String.fromCharCode(event.keyCode);
	if ( (cChar == "t" || cChar == "T") && !oField.readOnly ) { //if user presses T, then select today
		dToday = new Date();
		nMonth = dToday.getMonth()+1;
		nDay = dToday.getDate();
		nYear = dToday.getYear();
		oField.value = jsright("00" + nMonth,2) + '/' + jsright("00" + nDay,2) + '/' + nYear;
		event.returnValue = false;
	} 
	else if (cChar == "d" || cChar == "D" && !oField.readOnly ) {
		
		if (oField.value.length == 0 )
			dCurrentDate = new Date();
		else
			dCurrentDate = new Date(oField.value);
			
		dCurrentDate.setTime(dCurrentDate.getTime() + (1000*60*60*24));
		nMonth = dCurrentDate.getMonth() + 1;
		nDay = dCurrentDate.getDate();
		nYear = dCurrentDate.getYear();
		oField.value = jsright("00" + nMonth,2) + '/' + jsright("00" + nDay,2) + '/' + nYear;
		event.returnValue = false;
	} else if (cChar == "w" || cChar == "W" && !oField.readOnly ) {
		
		if (oField.value.length == 0 )
			dCurrentDate = new Date();
		else
			dCurrentDate = new Date(oField.value);
			
		dCurrentDate.setTime(dCurrentDate.getTime() + (1000*60*60*24*7));
		nMonth = dCurrentDate.getMonth() + 1;
		nDay = dCurrentDate.getDate();
		nYear = dCurrentDate.getYear();
		oField.value = jsright("00" + nMonth,2) + '/' + jsright("00" + nDay,2) + '/' + nYear;
		event.returnValue = false;
	} else if ((cChar == "m" || cChar == "M") && !oField.readOnly ) {
		
		if (oField.value.length == 0 )
			dCurrentDate = new Date();
		else
			dCurrentDate = new Date(oField.value);
			
		nMonth = dCurrentDate.getMonth() + 2;
		nDay = dCurrentDate.getDate();
		
		if (nMonth > 12)
		{
			nMonth = 1;
			nYear = dCurrentDate.getYear() + 1;
		}
		else
		{
			nYear = dCurrentDate.getYear();
		}
			
		oField.value = jsright("00" + nMonth,2) + '/' + jsright("00" + nDay,2) + '/' + nYear;
		event.returnValue = false;
	} else if ((cChar == "y" || cChar == "Y") && !oField.readOnly ) {
		
		if (oField.value.length == 0 )
			dCurrentDate = new Date();
		else
			dCurrentDate = new Date(oField.value);
			
		nMonth = dCurrentDate.getMonth() + 1;
		nDay = dCurrentDate.getDate();
		nYear = dCurrentDate.getYear() + 1;
			
		oField.value = jsright("00" + nMonth,2) + '/' + jsright("00" + nDay,2) + '/' + nYear;
		event.returnValue = false;
	} 
	else if (cValid.indexOf(cChar) == -1) {
		event.returnValue = false;
	}
}

function vNumBlur(oField, nWidth, nDec, lLead, lNegative, lAllowBlank, lBlankZero) {
	if (oField.readOnly || oField.disabled) return true;
	if (nDec == 0)
		cBefore = nWidth.toString();
	else
		//cBefore = (nWidth - (nDec + 1)).toString();
		cBefore = (nWidth - nDec).toString(); //don't count "." against total # of digits -dbb 12/14/07
	
	cDec = nDec.toString();
	if (lNegative)
		cMask = "^(-)?\\d{0," + cBefore + "}(\\.\\d{0," + cDec + "})?$";
	else
		cMask = "^\\d{0," + cBefore + "}(\\.\\d{0," + cDec + "})?$";
	
	oRegExp = new RegExp(cMask);
	if (!oRegExp.test(oField.value)) {
		alert("Invalid format: Use " + nWidth.toString() + " digits with " + 
		                               nDec.toString() + " decimal places");
		oField.focus();
	} else {
		if (oField.value !== "" || !lAllowBlank)
			oField.value = formatNumber(oField.value, nDec, lLead, lAllowBlank, lBlankZero);
	}
}

function vNumKey(oField, cNegative) {
	if (cNegative == "1")
		cValid = "0123456789.-";
	else
		cValid = "0123456789.";
		
	cChar = String.fromCharCode(event.keyCode);
	if (cValid.indexOf(cChar) == -1)
		event.returnValue = false;
}

function formatNumber(nNum, nDec, lLeading, lAllowBlank, lBlankZero) {
	cRet = "";
	cPre = "0";
	cPost = "";

	cNum = nNum.toString();
	
	if (cNum.indexOf(".") == -1) {
		cPre = cNum;
	} else {
		cPre = cNum.substr(0, cNum.indexOf("."));
		cPost = cNum.substr(cNum.indexOf(".") + 1);
	}
	
	if (!lBlankZero) {
		if (cPre.valueOf() == "") {
			if (lLeading)
				cRet = "0";
			else
				cRet = "";
		} else {
			cRet = cPre;
		}		
	} else {
		if (cPre.valueOf() == 0 && cPre.substr(0,1) != "-") {
			if (lLeading)
				cRet = "0";
			else
				cRet = "";
		} else {
			cRet = cPre;
		}
	}
	
	if (cPost.length > nDec)
		cPost = cPost.substr(cPost, nDec);
	
	if (!(lAllowBlank && cRet.valueOf() == "" && cPost.valueOf() == ""))
		 {		
		for (i = cPost.length; i < nDec; i++)
			cPost = cPost + "0";

		if (nDec > 0)
			cRet = cRet + "." + cPost;
		}
	
	return cRet;
}

function vTimeBlur(oField, cAmPm) {
	if (oField.readOnly || oField.disabled) return true; 
	cTime = oField.value;
	if (empty(cTime)) {
		oAmPm = document.all(cAmPm);
		setSelect(oAmPm, "AM");
		return;
	}
	
	// added to support military (24-hour) time
	// if user enters exactly 4 digits with no colon, assume military time
	// use the first 2 digits to determine AM or PM  -jek 12/23/08
	lMilitary = false;
	cMilAmPm  = "AM";
	
	nColon = cTime.indexOf(":");
	
	if (nColon == -1) {
		if (cTime.length == 4) {
			nHour = jsleft(cTime,2);
			nMinute = cTime.substr(2);
			lMilitary = true;
		} 
		
		if (cTime.length == 3) {
			nHour = jsleft(cTime,1);
			nMinute = cTime.substr(1);
		}
			
		if (cTime.length == 1 || cTime.length == 2 || cTime.length == 5) {
			nHour = parseInt(cTime,10);
			nMinute = 0;
		}
	} else {
		nHour = jsleft(cTime, nColon);
		nMinute = cTime.substr(nColon+1);
	}
	
	if (lMilitary) {
		if (nHour == 0) {
			nHour = 12;
			cMilAmPm = "AM";
		} else if (nHour == 12) {
			cMilAmPm = "PM"
		} else if (nHour > 12) {
			nHour = nHour - 12;
			cMilAmPm = "PM"
		}
	}

	if (nHour < 1 || nHour > 12 || nMinute < 0 || nMinute > 59) {
		alert("Invalid Time");
		oField.focus();
	} else {
		oField.value = jsright("00" + nHour,2) + ":" + jsright("00" + nMinute,2);
		if (!empty(cAmPm)) {
			oAmPm = document.all(cAmPm);
			
			// Added for Anderson to default 5 or 6 to AM.  MH 3-16-06
			// Added so 5 defaults to PM for AnytimeEndTime on ServiceSetups. -VB 10-10-2006
			if (typeof cCustomFlag == "undefined")
				cCustomFlag = "";
				
			if ((cCustomFlag == "ANDERSON" || cCustomFlag == "RMTERMITE") && oField.name != "AnytimeEndTime" ) { 
				nHr = 5;
			} else if (cCustomFlag == "TITAN334182") {
				nHr = 6;
			} else {
				nHr = 7;
			}

			if (lMilitary)
				setSelect(oAmPm, cMilAmPm);
			else if (nHour == 12 || nHour < nHr)
				setSelect(oAmPm, "PM");
			else
				setSelect(oAmPm, "AM");
		}
	}
}

function vTimeKeyDown(oField, cAmPm) {
	if (!oField.readOnly) {
		if (event.keyCode == 38 || event.keyCode == 40) {
			//up, down move minute backward, forward one day (or hold Ctrl to move one hour)
			cFieldValue = oField.value;
			
			if (cFieldValue.length == 4 || cFieldValue.length == 5){
				if (cFieldValue.length == 4)
					cFieldValue = "0" + cFieldValue;
				
				oAmPm = document.all(cAmPm);
				
				cHours = cFieldValue.substr(0, 2);
				cMinutes = cFieldValue.substr(3, 2);
				
				if (isTime(cHours, cMinutes)) {
					if (event.keyCode == 38) {nIncrement = 1} else {nIncrement = -1}
					
					var myTime = new Date();
					myTime.setHours(cHours);
					myTime.setMinutes(cMinutes);
					
					dNewTime = myTime;
					
					if (event.ctrlKey)
						dNewTime.setHours(dNewTime.getHours() + nIncrement);
					else
						dNewTime.setMinutes(dNewTime.getMinutes() + nIncrement);
					
					if (dNewTime.getHours() == 13)
						dNewTime.setHours(1);
					else if (dNewTime.getHours() == 0)
						dNewTime.setHours(12);
					else if ((dNewTime.getHours() == 12 && cHours !== "12") || (dNewTime.getHours() == 11 && cHours == "12")) {
						if (oAmPm !== null) {
							if (oAmPm.value == 'AM')
								setSelect(oAmPm, "PM");
							else
								setSelect(oAmPm, "AM");
						}
					}
					
					oField.value = jsright("00" + dNewTime.getHours(),2) + ':' + jsright("00" + dNewTime.getMinutes(),2);
					event.returnValue = false;
				} else {event.returnValue = false;}
			}
		}
	} else {event.returnValue = false;}
}

function vTimeKey(oField, cAmPm) {
	var cValid = "0123456789:";
	cChar = String.fromCharCode(event.keyCode);	
	if ( (cChar == "t" || cChar == "T") && !oField.readOnly ) { //if user presses T, then select today
		//respect employee's AdjustTime setting -dbb 6/23/08
		//dNow = new Date();
		dNow = AdjustTime(new Date());
		//
		nHour = dNow.getHours();
		nMin = dNow.getMinutes();
		
		oAmPm = document.all(cAmPm);
		if (nHour <= 12) {
			setSelect(oAmPm, "AM");
		} else {
			nHour = nHour - 12;
			setSelect(oAmPm, "PM");
		}
		oField.value = jsright("00" + nHour,2) + ':' + jsright("00" + nMin,2);
		event.returnValue = false;
	} else if (cValid.indexOf(cChar) == -1) {
		event.returnValue = false;
	}
}

function setSelect(oSelect, cValue) {
	for (nXYZ = 0; nXYZ < oSelect.length; nXYZ++) {
		cOption = oSelect.item(nXYZ).value;
		if (cOption == cValue) 
			oSelect.selectedIndex = nXYZ;
	}
}
