/****************************************************************************************
Called by the map.asp page on load.  This function controls the dhtml start (making the user's
map movements tracked).  It also controls the initial load of the map and showing results
after a "get info" request.
****************************************************************************************/

function loadfunction(){
	//alert("loadFunction");
   	if (document.mapparams.started.value == ""){
		document.mapparams.started.value = "true";
		showRetrieveMap();
		document.mapparams.submit();
	}
	if (isNav) {
		document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
	} 
	document.onmousedown = mapTool;
	document.onmouseup = chkMouseUp;
	document.onmousemove = getMouse;
	document.ondblclick = submitOnDblClick;
	changeCursor();
	
	parent.Data.document.dataparams.CreateBuffer.value = document.mapparams.CreateBuffer.value;
	//alert("mapparams: " + document.mapparams.pins.value);
	//alert("dataDetail: " + document.mapparams.DataDetailVisible.value);
	if ((document.mapparams.pins.value != "") && (document.mapparams.DataDetailVisible.value == "")){
		//alert("dataDetail: " + document.mapparams.DataDetailVisible.value);
		//alert("loadFunction pins: " + document.mapparams.pins.value);
		//alert("pin: " + document.mapparams.pin.value);
		if (document.mapparams.pin.value > ""){
			parent.Data.document.dataparams.pin.value = document.mapparams.pin.value;
			parent.Data.document.dataparams.dataType.value = "parcel";
			parent.Data.document.dataparams.numpins.value = 1;
			
			//parent.Data.location = "data.asp?pin="+document.mapparams.pin.value+"&dataType=parcel&numpins=1&stylesize="+stylesize+"&CreateBuffer="+CreateBuffer;
		}else{
			//parent.Data.location = "data.asp?pins="+document.mapparams.pins.value+"&dataType=parcels&stylesize="+stylesize+"&CreateBuffer="+CreateBuffer;
			parent.Data.document.dataparams.pins.value = document.mapparams.pins.value;
			//alert("dataparams: " + parent.Data.document.dataparams.pins.value);
			parent.Data.document.dataparams.dataType.value = "parcels";
		}
		parent.Data.document.dataparams.submit();
		//alert("dataparams submit");
		if(parent.Tabs.document.tabparams){
			parent.Tabs.document.tabparams.currentsearch.value = "results";
			parent.Tabs.document.tabparams.currentindex.value = 3;
			parent.Tabs.document.tabparams.submit();
		}
	}
	
	// ESRI - 12/7/2005
	// This line was commented out when a "Multiple Select" tool was added to the web page.
	// We now need to keep the two tools separate.
	//if (document.mapparams.clickTool.value == "getinfo_multiple") document.mapparams.clickTool.value = "getinfo";
}
/****************************************************************************************
Called by header.asp when the user clicks on a tool function.  Changes the image on the
page, stores which tool is currently active in forma on the map page, and the header page,
and changes the cursor (calling a function in dhtml.js)
****************************************************************************************/
function switchtool(index,basename){
	//This line turns the currently active tool image off
	document.images[document.buttonparams.currentindex.value].src = "images/" + document.buttonparams.currenttool.value + "_off.gif";

	//these lines set the form inputs that store information about active image to match the new active tool
	document.buttonparams.currenttool.value = basename;
	document.buttonparams.currentindex.value = index;
	
	// ESRI - 1/7/04
	// IF PINS ARE PRESENT IN THE MAPPARAMS FORM, WE DO NOT WANT TO IMPLICITLY
	// HIGHLIGHT THOSE ON ANY MAP ACTION.  THIS BLOCK EXCLUDES ZOOMING AND PANNING.
	if (basename == "pan" || basename == "zoomin" || basename == "zoomout") {
		parent.Map.document.mapparams.mapType.value = "clicktools";
	}
	//alert(basename);
	//this line switches the source of the image at the "index" position in the images array (the one that was passed)
	document.images[index].src = "images/" + basename + "_on.gif";
	parent.Map.document.mapparams.clickTool.value = basename;
	//alert(parent.Map.document.mapparams.clickTool.value);
	parent.Map.changeCursor();
	
	// ESRI - 12/7/2005
	// Added this function call to give more info on the Multiple Select
	if (basename == "getinfo_multiple") toolDirections(basename);
	
}
/****************************************************************************************
called when the user clicks the buffer tool button on the interface
****************************************************************************************/
function startBuffer(){
	// ESRI - 12/7/2005
	// Added function call to clarify buffer tool
	toolDirections("buffer");
	if (parent.Map.document.mapparams.pin.value > "" || parent.Map.document.mapparams.pins.value > ""){
		var distance = prompt("Enter a buffer distance - in feet - between 1 and 1000", "300");
		if(isNaN(parseFloat(distance))){
			alert("The value entered must be numeric.  Try again.");
			distance = prompt("Enter a buffer distance - in feet - between 1 and 1000", "300");
		}else if(parseFloat(distance) > 1000 || parseFloat(distance) < 1){
			alert("The value entered must be between 1 and 1000.  Try again.");
			distance = prompt("Enter a buffer distance - in feet - between 1 and 1000", "300");
			if ((isNaN(parseFloat(distance))) ||(parseFloat(distance) > 1000 || parseFloat(distance) < 1)){
				return;
			}
		}
		//alert("startBuffer pins: " + parent.Map.document.mapparams.pins.value);
		//alert("startBuffer pin: " + parent.Map.document.mapparams.pin.value);
		parent.Map.document.mapparams.action.value = "buffer";
		parent.Map.document.mapparams.mapType.value="actiontools";
		parent.Map.document.mapparams.bufferdist.value=distance;
		parent.Map.showRetrieveMap();
		parent.Map.document.mapparams.submit();
		
	}else{
		alert("You must have selected parcels to perform a buffer.\nUse 'Info' tool to select parcels, when finished click\n'Buffer' button to resume.");
		switchtool(4, "getinfo");
	}
}

/****************************************************************************************
ESRI - 12/7/2005
This function was added to give additional information about the Multiple Select and 
Buffer Tools.  Mo thought the user needed clarification on these tools.
****************************************************************************************/
function toolDirections(toolName) {
	var helpString;
	
	if (toolName == "getinfo_multiple") {
		helpString = "Tool Directions\n\n";
		helpString += "To use this tool, click on the desired parcels while depressing the SHIFT key.\n";
		helpString += "Double-click on the last parcel to complete the selection.";
	} else if (toolName == "buffer") {
		helpString = "Tool Directions\n\n";
		helpString += "A buffer is used to select parcels around a selected parcel(s)\n";
		helpString += "based on a specified distance (in feet).  When the dialog box displays,\n";
		helpString += "enter the desired distance and click OK.  Your selected set will be displayed\n";
		helpString += "on the map and the attribute information for each parcel will be displayed under the Results tab."
	}
	
	alert(helpString);
}

/****************************************************************************************

****************************************************************************************/
function mapaction(basename){
	parent.Map.document.mapparams.action.value=basename;
	parent.Map.document.mapparams.mapType.value="actiontools"
	parent.Map.showRetrieveMap();
	parent.Map.document.mapparams.submit();
}
/****************************************************************************************

****************************************************************************************/
function switchsearch(index,basename, size){
	document.images[document.tabparams.currentindex.value].src = "images/" + document.tabparams.currentsearch.value + "_off_" + size + ".gif";
	document.tabparams.currentsearch.value = basename;
	document.tabparams.currentindex.value = index;
	document.images[index].src = "images/" + basename + "_on_" + size + ".gif";
	if(basename == "results") {
		parent.Data.document.dataparams.dataType.value="parcels";
	}else{
		parent.Data.document.dataparams.dataType.value="";
	}
	parent.Data.document.dataparams.currentsearch.value=basename;
	//parent.Data.document.dataparams.
	parent.Data.document.dataparams.submit();
}
/****************************************************************************************
Called by the "Run Search" button on the Subdivision/Pin search - data.asp
****************************************************************************************/
function checkSubPin(){
	if (document.subparams.parcelid.value > "" && document.subparams.subdivision.options[document.subparams.subdivision.selectedIndex].value > ""){
		alert("Choose either Parcel ID or Subdivision, not both.")
	}else{
		document.subparams.submit();
	}
}
/****************************************************************************************
Following functions called by data.asp
****************************************************************************************/
function openBuildingsPage(acctno){
	var buildingWin;
		buildingWin = window.open('improvements.asp?acctno='+acctno,'buildingWin','width=450,height=650,resizable,scrollbars');
		buildingWin.focus();
	}
	function openTaxPage(acctno){
		var taxWin;
		taxWin=window.open('taxes.asp?acctno='+acctno,'taxWin','width=350,height=375,resizable,scrollbars');
		taxWin.focus();
	}
	function detailedInfo(pin){
		document.dataparams.currentsearch.value="results";
		document.dataparams.dataType.value="parcel";
		document.dataparams.pin.value=pin;
		document.dataparams.submit();
		return true;
	}
	function backtoparcels(prevSearch){
		document.dataparams.currentsearch.value="results";
		// DATATYPE WAS ALWAYS SET TO PARCELS, WHICH EXECTUTED AN INCORRECT SQL STATEMENT
		// ON THE "RETURN TO PARCELS" QUERY.
		//document.dataparams.dataType.value="parcels";
		document.dataparams.dataType.value = prevSearch;
		document.dataparams.submit();
	}
	function checkRedirect(){
		//alert("redirect pins: " + document.dataparams.pins.value);
		//alert("redirect pin: " + document.dataparams.pin.value);
		//alert(document.dataparams.dataType.value);
		//alert(document.dataparams.currentsearch.value);
		var numpins = document.dataparams.numpins.value;
		numpins = parseFloat(numpins);
		if (document.dataparams.currentsearch.value == "results"){
			if (parent.Tabs.document.tabparams) {
				parent.Tabs.document.tabparams.currentsearch.value = "results";
				parent.Tabs.document.tabparams.currentindex.value = 3;
				parent.Tabs.document.tabparams.submit();
			}
			if (document.dataparams.dataType.value == "subdivision") {
				parent.Map.document.mapparams.mapType.value="highlight";
				parent.Map.document.mapparams.zoomType.value = document.dataparams.dataType.value;
				parent.Map.document.mapparams.subs.value = document.dataparams.subdivision.value;
				parent.Map.document.mapparams.pins.value = ""
				if ((parent.Data.document.dataparams.downLoad.value != "true") && (parent.Map.document.mapparams.CreateBuffer.value != "True")) {
					parent.Map.showRetrieveMap();
					parent.Map.document.mapparams.submit();
				}
			}
			//only display the selection on the map if the number of pins is less than the threshold
			else if (numpins < 50 || document.dataparams.dataType.value=="parcel") {
				
				parent.Map.document.mapparams.mapType.value="highlight";
				//alert(document.dataparams.pin.value);
				if (document.dataparams.dataType.value=="parcel" && document.dataparams.pin.value > ""){
					parent.Map.document.mapparams.pins.value= "'" + document.dataparams.pin.value + "'";
					// Used to set zoom level
					parent.Map.document.mapparams.zoomType.value = document.dataparams.dataType.value;
				
				}else{
					parent.Map.document.mapparams.pins.value=document.dataparams.pins.value;
					// Used to set zoom level
					parent.Map.document.mapparams.zoomType.value = document.dataparams.dataType.value;
				}
				
				//alert(document.dataparams.CreateBuffer.value);
				parent.Map.document.mapparams.CreateBuffer.value = document.dataparams.CreateBuffer.value; 
				parent.Map.document.mapparams.DataDetailVisible.value = "true";
					
				if ((parent.Data.document.dataparams.downLoad.value != "true") && (parent.Map.document.mapparams.CreateBuffer.value != "True")) {
					parent.Map.showRetrieveMap();
					parent.Map.document.mapparams.submit();
				}
			}else{
				alert("The selection returned " + numpins + " values.  This is too many to highlight on the map.\n  Please get information about individual parcels to view them on the map." )
				parent.Map.document.mapparams.DataDetailVisible.value = "true";
			}
		}
	}
	
	
// ESRI - 12/7/2005
// The Print functionality was changed to add a "Print Map" button.  Previously the user could
// only print if a selection set was present (the Print Button was displayed on the Results tab).
// This Print button was changed to "Print Results" and a "Print Map" button was added to just
// print the map.

/****************************************************************************************
Called from data.asp by the "PrintResults" button on the results tab (after a successful query)
****************************************************************************************/
	function openPrintResults(pins, dataDetailed){
		var mapheight = parent.Map.document.mapparams.mapheight.value;
		var mapwidth = parent.Map.document.mapparams.mapwidth.value;
		var xmin = parent.Map.document.mapparams.xmin.value;
		var xmax = parent.Map.document.mapparams.xmax.value;
		var ymin = parent.Map.document.mapparams.ymin.value;
		var ymax = parent.Map.document.mapparams.ymax.value;
		var varString = "mapheight="+mapheight+"&mapwidth="+mapwidth
		varString = varString + "&xmin="+xmin+"&xmax="+xmax
		varString = varString + "&ymin="+ymin+"&ymax="+ymax
		varString = varString + "&pins="+pins+"&dataDetailed="+dataDetailed
		if (dataDetailed == true){
			alert("After page opens, choose Page Setup from the File menu and change setting to landscape.\nChoose Print from the File menu to print.");
		}else{
			alert("After page opens, choose Page Setup from the File menu and change setting to portrait.\nChoose Print from the File menu to print.");
		}
		printWin = window.open('print.asp?'+varString,'printResultsWin','width=780,height=650,resizable,scrollbars,menubar,');
	}
	
/***********************************************************************************************
Called from header.asp by the "PrintMap" button on the toolbar.  This function only prints the map(s)
****************************************************************************************************/
	function openPrintMap(){
		var mapheight = parent.Map.document.mapparams.mapheight.value;
		var mapwidth = parent.Map.document.mapparams.mapwidth.value;
		var xmin = parent.Map.document.mapparams.xmin.value;
		var xmax = parent.Map.document.mapparams.xmax.value;
		var ymin = parent.Map.document.mapparams.ymin.value;
		var ymax = parent.Map.document.mapparams.ymax.value;
		var varString = "mapheight="+mapheight+"&mapwidth="+mapwidth
		varString = varString + "&xmin="+xmin+"&xmax="+xmax
		varString = varString + "&ymin="+ymin+"&ymax="+ymax
		varString = varString + "&mapOnly=true";
		//if (dataDetailed == true){
			//alert("After page opens, choose Page Setup from the File menu and change setting to landscape.\nChoose Print from the File menu to print.");
		//}else{
			//alert("After page opens, choose Page Setup from the File menu and change setting to portrait.\nChoose Print from the File menu to print.");
		//}
		printWin = window.open('print.asp?'+varString,'printMapWin','width=780,height=650,resizable,scrollbars,menubar,');
	}
	
	
/****************************************************************************************
Called from data.asp by the "Download" button on the results tab (after a successful query)
****************************************************************************************/
	function openDownload(pins){
		//alert(pins);
		
		// Set download value in data.asp to true and submit form, which reruns query and results and creates text file for download
		parent.Data.document.dataparams.downLoad.value = "true";
		parent.Data.document.dataparams.submit();
		//downloadWin = window.open('download.asp?'+varString,'buildingWin','width=200,height=200,resizable,scrollbars,menubar,');

	}
