////////////////////////////////////////////////////////////////////////////////
//
//	file:		modules.js
//
//	in:			-
//	out:		-
//
//	purpose:	Repository for JavaScript functions used within the application.
//
////////////////////////////////////////////////////////////////////////////////
//
//	history:
//
//	Created new.
//	Julie Collins, RDI 2001
//
////////////////////////////////////////////////////////////////////////////////

var curResultsPage = 1;
var curOnSitePage = 1;
var eventX;
var eventY;
var intID;

function onClick (color) {
 // tech serv added comments here
 //green means submit or enter was pushed
 //red means reset was pushed
	if (color=="green") {
		document.all.btnSearch.src = "images/submitbutton.GIF";
	} else if (color=="red") {
		document.all.btnReset.src = "images/resetbutton.GIF";
	}

	var string = "onClick2('"+color+"')";  //validate form fields - TS comments
	clearTimeout();
	setTimeout(string,30);
	// launch function to resend refresh command if necessary - 4/17/02 jac
	string = "refreshTimer()";
	intID = setInterval(string,1000);
}

function refreshTimer() {
// fn added 4/17/02 jac
	if (top.refreshCounter == 0 && top.refreshFlag == "yes") {
//		top.resultsframe.results.location = "results.cfm?t=<cfoutput>#TimeFormat(now(),'hhmmss')#</cfoutput>";
//		top.onsiteframe.onsite.location = "onsite.cfm?t=<cfoutput>#TimeFormat(now(),'hhmmss')#</cfoutput>";
		var dtNow = new Date();
		top.resultsframe.results.location = "results.cfm?t="+dtNow;
		top.onsiteframe.onsite.location = "onsite.cfm?t="+dtNow;
		//top.resultsframe.results.location = "results.cfm";
		//top.onsiteframe.onsite.location = "onsite.cfm";
	} else {
		// refresh accomplished, so reset flags and jump out of the interval loop
		top.refreshStatus = 0;  
		top.refreshFlag = "no"; 
		clearInterval(intID);
	}
}

function onClick2(color) {
	var dateTo = "";
	var dateFrom = "";
	var strErr = "";

	clearTimeout();
	if (color=="green") {
		// validate valuation range (valmin & valmax are text inputs on form...check to see if they have valid values numeric/empty)
		var varTest1 = document.all.frmQuery.valmin.value;
		var varTest2 = document.all.frmQuery.valmax.value;
		if ((isNaN(varTest1) && varTest1.length > 0) || (isNaN(varTest2) && varTest2.length > 0)) {
			strErr = strErr + "Valuation range fields must be either numeric or empty. ";
		} 
		// validate taxid
		var taxid = document.all.frmQuery.taxid.value;	
		if ((isNaN(taxid) || taxid.length > 13)) {
			strErr = strErr + "Tax/Parcel IDs must be numeric w/13 digits or less. ";
		} 
		// validate "Date To"
		var strMo = document.all.frmQuery.appto1.value;
		var strDay = document.all.frmQuery.appto2.value;
		var strYr = document.all.frmQuery.appto3.value;

		if ((strMo.length < 1 || strDay.length < 1 || strYr.length < 2) && (strMo.length > 0 || strDay.length > 0 || strYr.length > 0)) {
			strErr = strErr + "'Application Date to' is invalid. ";
		} else if (!(strMo.length < 1 && strDay.length < 1 && strYr.length < 1)) {
			if (!isNaN(strMo) && parseFloat(strMo) <= 12) {
				if (strMo.length == 1) {
					if (strMo == "0") {
						strErr = strErr + "'Date from' month is invalid.";
					} else {
						document.all.frmQuery.appto1.value = "0" + strMo;
					}
				} else if (strMo.length > 2) {
					strErr = strErr + "'Date to' month is invalid.";
				}
			} else if (strMo.length > 0) {
				strErr = strErr + "'Date to' month is invalid.";
			}
			if (!isNaN(strDay) && parseFloat(strDay) <= 31) {
				if (strDay.length == 1) {
					if (strDay == "0") {
						strErr = strErr + "'Date from' day is invalid.";
					} else {
						document.all.frmQuery.appto2.value = "0" + strDay;
					}
				} else if (strDay.length > 2) {
					strErr = strErr + "'Date to' day is invalid.";
				}
			} else if (strDay.length > 0) {
				strErr = strErr + "'Date to' day is invalid.";
			}
			if (!isNaN(strYr)) {
				if (strYr.length == 2) {
					document.all.frmQuery.appto3.value = "20" + strYr;
				} else if (strYr.length == 3 || strYr.length > 4) {
					strErr = strErr + "'Date to' year is invalid.";
				}
			} else if (strYr.length > 0) {
				strErr = strErr + "'Date to' year is invalid.";
			}
			dateTo = document.all.frmQuery.appto1.value + "/" + document.all.frmQuery.appto2.value + "/" + document.all.frmQuery.appto3.value;
		} 
		// validate "Date From"
		strMo = document.all.frmQuery.appfrom1.value;
		strDay = document.all.frmQuery.appfrom2.value;
		strYr = document.all.frmQuery.appfrom3.value;
		if ((strMo.length < 1 || strDay.length < 1 || strYr.length < 2) && (strMo.length > 0 || strDay.length > 0 || strYr.length > 0)) {
			strErr = strErr + "'Application Date to' is invalid. ";
		} else if (!(strMo.length < 1 && strDay.length < 1 && strYr.length < 1)) {
			if (!isNaN(strMo) && parseFloat(strMo) <= 12) {
				if (strMo.length == 1) {
					if (strMo == "0") {
						strErr = strErr + "'Date from' month is invalid.";
					} else {
						document.all.frmQuery.appfrom1.value = "0" + strMo;
					}
				} else if (strMo.length > 2) {
					strErr = strErr + "'Date from' month is invalid.";
				}
			} else if (strMo.length > 0) {
				strErr = strErr + "'Date from' month is invalid.";
			}
			if (!isNaN(strDay) && parseFloat(strDay) <= 31) {
				if (strDay.length == 1) {
					if (strDay == "0") {
						strErr = strErr + "'Date from' day is invalid.";
					} else {
						document.all.frmQuery.appfrom2.value = "0" + strDay;
					}
				} else if (strDay.length > 2) {
					strErr = strErr + "'Date from' day is invalid.";
				}
			} else if (strDay.length > 0) {
				strErr = strErr + "'Date from' day is invalid.";
			} 
			if (!isNaN(strYr)) {
				if (strYr.length == 2) {
					document.all.frmQuery.appfrom3.value = "20" + strYr;
				} else if (strYr.length == 1 || strYr.length == 3 || strYr.length > 4) {
					strErr = strErr + "'Date from' year is invalid.";
				}
			} else if (strYr.length > 0) {
				strErr = strErr + "'Date from' year is invalid.";
			}
			dateFrom = document.all.frmQuery.appfrom1.value + "/" + document.all.frmQuery.appfrom2.value + "/" + document.all.frmQuery.appfrom3.value;
		} 
		// complete submission
		if (strErr.length == 0) {
			try {
				top.resultsframe.results.LoadingBP.style.visibility = "visible";
			} 
			catch (e) { strErr = strErr; }
			try {
				top.onsiteframe.onsite.LoadingOS.style.visibility = "visible";
			}
			catch (e) { strErr = strErr; }
			top.refreshStatus = 0;   // reset the refresh counter - 4/17/02 jac
			top.refreshFlag = "yes"; // there is an active query, so monitor refresh status of results.cfm - 4/17/02 jac
			document.all.btnSearch.src = "images/submitbuttonactive.GIF";
			document.all.frmQuery.appto.value = dateTo;
			document.all.frmQuery.appfrom.value = dateFrom;
			document.all.frmQuery.submitted.value = "yes";
			document.all.frmQuery.submit();
//			top.resultsframe.results.location = "results.cfm?t=<cfoutput>#TimeFormat(now(),'hhmmss')#</cfoutput>";
//			top.onsiteframe.onsite.location = "onsite.cfm?t=<cfoutput>#TimeFormat(now(),'hhmmss')#</cfoutput>";

//  NSP & KLR moved below 2 lines to query.cfm after session vars set because of refresh problem...6/25/02
		//top.resultsframe.results.location = "results.cfm";
		//top.onsiteframe.onsite.location = "onsite.cfm";
		} else {
			alert(strErr);
		}
	} else if (color=="red") {
		document.all.btnReset.src = "images/resetbuttonactive.GIF";
		top.queryframe.query.location = "query.cfm";
		top.refreshStatus = 0;  // reset the refresh counter - 4/17/02 jac
		top.refreshFlag = "no";  // no active query, so no requirement to refresh - 4/17/02 jac
//		top.resultsframe.results.location = "results.cfm?t=<cfoutput>#TimeFormat(now(),'hhmmss')#</cfoutput>";
//		top.onsiteframe.onsite.location = "onsite.cfm?t=<cfoutput>#TimeFormat(now(),'hhmmss')#</cfoutput>";
		top.resultsframe.results.location = "results.cfm";
		top.onsiteframe.onsite.location = "onsite.cfm";
	}
}

function checkForEnter(evt) {
	evt = (evt) ? evt : event;
	var charCode = (evt.which) ? evt.which : evt.keyCode;
	if (charCode == 13) {
		onClick('green');
	}
}

function tabClick(context) {
	if (context == 'bp') {
		if (eventY >= 0 && eventY < 24) {
			if (eventX >= 206 && eventX <= 314) {
				// clicked on-site permit tab
				top.resultframeset.cols = "300px,0,*";
			}
		}
	} else {
		if (eventY >= 0 && eventY < 24) {
			if (eventX >= 30 && eventX <= 194) {
				// clicked building permit tab
				top.resultframeset.cols = "300px,*,0";
			}
		} 
	} 
}