// CivXplorer Zoom To Landmark Layer

// Suzanne Fliege
// PlanSight, LLC

// contains functions for zooming to an area, defined by a map layer.
zoom2areaAvailable = true;
var ZoomAreaInfoString = new Array(); // info string for user form
var ZoomAreaLayerID = new Array(); // map layer to zoom to
var ZoomAreaFields = new Array(); // field(s) to zoom query (one or many, include SDE prefixes!!) MAXIMUM 4 FIELDS!
var ZoomAreaAliases = new Array(); // field aliases for user to see (must correspond to ZoomAreaFields array)
var ZoomAreaIsString = new Array(); // whether the field is string or numeric (true if string)
var ZoomAreaValues = new Array(); // values for user to choose from (if left empty, then samples retrieved from layer data)
var ZoomAreaMakeVisible = new Array(); // whether to make layer visible when zoomed-to (true if you want it on)
var ZoomAreaSaveSelect = new Array(); // whether to select the feature that was zoomed-to (true to select, false to just zoom)

// variables containing Fields, Aliases, and Value Arrays
ZoomAreaInfoString[0] = "Zoom to Municipality"
ZoomAreaLayerID[0] = "grouped-municipal-municipality";
ZoomAreaFields[0] = new Array("NAME");
ZoomAreaAliases[0] = new Array("Municipality");
ZoomAreaIsString[0] = new Array(true);
ZoomAreaValues[0] = new Array();
ZoomAreaMakeVisible[0] = true;
ZoomAreaSaveSelect[0] = false;

ZoomAreaInfoString[1] = "Zoom to Village"
ZoomAreaLayerID[1] = "grouped-municipal-village";
ZoomAreaFields[1] = new Array("NAME");
ZoomAreaAliases[1] = new Array("Village");
ZoomAreaIsString[1] = new Array(true);
ZoomAreaValues[1] = new Array();
ZoomAreaMakeVisible[1] = true;
ZoomAreaSaveSelect[1] = false;

ZoomAreaInfoString[2] = "Zoom to Civil Township"
ZoomAreaLayerID[2] = "grouped-municipal-towncivil";
ZoomAreaFields[2] = new Array("CIVIL_NAME");
ZoomAreaAliases[2] = new Array("Civil Township");
ZoomAreaIsString[2] = new Array(true);
ZoomAreaValues[2] = new Array();
ZoomAreaMakeVisible[2] = true;
ZoomAreaSaveSelect[2] = true;

ZoomAreaInfoString[3] = "Find By Township"
ZoomAreaLayerID[3] = "grouped-plss-townrange";
ZoomAreaFields[3] = new Array("TWP_NUM","RANGE");
ZoomAreaAliases[3] = new Array("Township","Range");
ZoomAreaIsString[3] = new Array(true,true);
ZoomAreaValues[3] = new Array();
ZoomAreaValues[3][0] = "T146N!!T147N!!T148N!!T149N!!T150N!!T151N!!T152N!!T153N!!T154N!!T155N!!T156N!!T157N!!T158N";
ZoomAreaValues[3][1] = "R30W!!R31W!!R32W!!R33W!!R34W!!R35W!!R36W!!R37W!!R38W";
ZoomAreaMakeVisible[3] = false;
ZoomAreaSaveSelect[3] = true;

ZoomAreaInfoString[4] = "Find By Section"
ZoomAreaLayerID[4] = "grouped-plss-sections";
ZoomAreaFields[4] = new Array("TWP_NUM","RANGE","SEC_NUM");
ZoomAreaAliases[4] = new Array("Township","Range","Section");
ZoomAreaIsString[4] = new Array(true,true,false);
ZoomAreaValues[4] = new Array();
ZoomAreaValues[4][0] = "T146N!!T147N!!T148N!!T149N!!T150N!!T151N!!T152N!!T153N!!T154N!!T155N!!T156N!!T157N!!T158N";
ZoomAreaValues[4][1] = "R30W!!R31W!!R32W!!R33W!!R34W!!R35W!!R36W!!R37W!!R38W";
ZoomAreaValues[4][2] = "0!!1!!2!!3!!4!!5!!6!!7!!8!!9!!10!!11!!12!!13!!14!!15!!16!!17!!18!!19!!20!!21!!22!!23!!24!!25!!26!!27!!28!!29!!30!!31!!32!!33!!34!!35!!36";
ZoomAreaMakeVisible[4] = false;
ZoomAreaSaveSelect[4] = true;

ZoomAreaInfoString[5] = "Find By Forty"
ZoomAreaLayerID[5] = "grouped-plss-forty";
ZoomAreaFields[5] = new Array("TWP_NUM","RANGE","SEC_NUM","FORTY");
ZoomAreaAliases[5] = new Array("Township","Range","Section","Forty");
ZoomAreaIsString[5] = new Array(true,true,false,true);
ZoomAreaValues[5] = new Array();
ZoomAreaValues[5][0] = "T146N!!T147N!!T148N!!T149N!!T150N!!T151N!!T152N!!T153N!!T154N!!T155N!!T156N!!T157N!!T158N";
ZoomAreaValues[5][1] = "R30W!!R31W!!R32W!!R33W!!R34W!!R35W!!R36W!!R37W!!R38W";
ZoomAreaValues[5][2] = "0!!1!!2!!3!!4!!5!!6!!7!!8!!9!!10!!11!!12!!13!!14!!15!!16!!17!!18!!19!!20!!21!!22!!23!!24!!25!!26!!27!!28!!29!!30!!31!!32!!33!!34!!35!!36";
ZoomAreaValues[5][3] = "GOVT LOT 1!!GOVT LOT 10!!GOVT LOT 11!!GOVT LOT 12!!GOVT LOT 13!!GOVT LOT 14!!GOVT LOT 15!!GOVT LOT 16!!GOVT LOT 17!!GOVT LOT 2!!GOVT LOT 3!!GOVT LOT 4!!GOVT LOT 5!!GOVT LOT 6!!GOVT LOT 7!!GOVT LOT 8!!GOVT LOT 9!!ISLAND!!NE1/4 NE1/4!!NE1/4 NW1/4!!NE1/4 SE1/4!!NE1/4 SW1/4!!NW1/4 NE1/4!!NW1/4 NW1/4!!NW1/4 SE1/4!!NW1/4 SW1/4!!SE1/4 NE1/4!!SE1/4 NW1/4!!SE1/4 SE1/4!!SE1/4 SW1/4!!SW1/4 NE1/4!!SW1/4 NW1/4!!SW1/4 SE1/4!!SW1/4 SW1/4!!UNKNOWN";
ZoomAreaMakeVisible[5] = false;
ZoomAreaSaveSelect[5] = true;

ZoomAreaInfoString[6] = "Find By Plat"
ZoomAreaLayerID[6] = "grouped-plat-plats";
ZoomAreaFields[6] = new Array("PLAT_NAME");
ZoomAreaAliases[6] = new Array("Plat");
ZoomAreaIsString[6] = new Array(true);
ZoomAreaValues[6] = new Array();
ZoomAreaMakeVisible[6] = false;
ZoomAreaSaveSelect[6] = true;

ZoomAreaInfoString[7] = "Find By Lake Name"
ZoomAreaLayerID[7] = "grouped-water-lakes";
ZoomAreaFields[7] = new Array("HYDRO_NAME");
ZoomAreaAliases[7] = new Array("Lake Name");
ZoomAreaIsString[7] = new Array(true);
ZoomAreaValues[7] = new Array();
ZoomAreaMakeVisible[7] = true;
ZoomAreaSaveSelect[7] = true;
//******************************************************************************
// this moduled uses custom function writeCustomFieldSample() to parse out unnamed lakes for samples!!!
//******************************************************************************


ZoomAreaInfoString[8] = "Find Bemidji Park"
ZoomAreaLayerID[8] = "optional-bemidjiparks";
ZoomAreaFields[8] = new Array("PARKNAME");
ZoomAreaAliases[8] = new Array("Park Name");
ZoomAreaIsString[8] = new Array(true);
ZoomAreaValues[8] = new Array();
ZoomAreaMakeVisible[8] = true;
ZoomAreaSaveSelect[8] = false;

ZoomAreaInfoString[9] = "Find County Park"
ZoomAreaLayerID[9] = "optional-countyparks";
ZoomAreaFields[9] = new Array("NAME");
ZoomAreaAliases[9] = new Array("Park Name");
ZoomAreaIsString[9] = new Array(true);
ZoomAreaValues[9] = new Array();
ZoomAreaMakeVisible[9] = true;
ZoomAreaSaveSelect[9] = false;

ZoomAreaInfoString[10] = "Find Cemetery"
ZoomAreaLayerID[10] = "grouped-landmark-cemetery";
ZoomAreaFields[10] = new Array("CEMETERY");
ZoomAreaAliases[10] = new Array("Cemetery Name");
ZoomAreaIsString[10] = new Array(true);
ZoomAreaValues[10] = new Array();
ZoomAreaMakeVisible[10] = false;
ZoomAreaSaveSelect[10] = true;

//******************************************************************************
// do not edit below here
//******************************************************************************
// program variables, don't need to specify
var ZoomAreaLayerIndex = new Array();
var ZoomAreaIndex = 0;
var isZoomAreaQuery = false;

// write out XML request to get field value samples
function writeCustomFieldSample(theLayer,theField,theLimit) {
	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" geometry="false" envelope="false" checkesc ="true"';
	theString += ' featurelimit="' + theLimit + '">\n';
	theString += '<LAYER id="' + theLayer + '" />';
	theString += '<QUERY subfields="' + theField + '"'; 
	if (theLayer == "grouped-water-lakes") { theString += ' where="HYDRO_NAME &lt;&gt; \'POND\' AND HYDRO_NAME &lt;&gt; \'UNNAMED\'"'; }
	theString += ' />';
	theString += '</GET_FEATURES>';
	theString += '</REQUEST>';
	theString += '</ARCXML>';
	sampleField = theField;
	return theString;
}


function addZoomAreaMenuItems() {
	for (var i=0;i<ZoomAreaInfoString.length;i++) {
		document.writeln(createMenuItem("menuItem_Zoom2Area"+i,"Zoom2Area(\'" + i + "\');",ZoomAreaInfoString[i]));
	}
	return false;
}

// writes specific form for zoom to landmark
function writeZoom2AreaForm() {
	var IsOkToOpenForm = "false";
	for  (var i=0;i<LayerID.length;i++) {
		if (ZoomAreaLayerID[ZoomAreaIndex] == LayerID[i]) IsOkToOpenForm = LayerAvailable[i];
	}
	if (IsOkToOpenForm == "true") {
		var theString = "";
		theString += '<form name="ZoomAreaStuff" onsubmit="doZoom2Area();return false;">';
		theString += '<table cellpadding="5" cellspacing="0" width="' + (sidebarWidth-10) + '">';
		theString += '<tr><td class="header1" width="100%" align="right"><a href="#" onmousedown="hideAttributeDisplay();"><img src="images/CXclose.gif" align="top"></a></td></tr>';
		theString += '<tr><td class="header1" align="center">' + ZoomAreaInfoString[ZoomAreaIndex] + '</td></tr>';
		theString += '</table>';
		// add in dropdown(s) for values
		theString += '<table cellpadding="5" cellspacing="0" width="100%">';
		if (ZoomAreaFields[ZoomAreaIndex].length > 1) {
			for (var j=0;j<ZoomAreaFields[ZoomAreaIndex].length;j++) {
				var tempAreaValues = ZoomAreaValues[ZoomAreaIndex][j].split("!!");
				theString += '<tr><td class="toolInfo1" align="left">';
				theString += '<select name="AreaName' + j + '" class="tocLayers">';
				theString += '<option selected value="">' + ZoomAreaAliases[ZoomAreaIndex][j];
				for (var i=0;i<tempAreaValues.length;i++) {
					theString += '<option value="' + tempAreaValues[i] + '">' + tempAreaValues[i];
				}
				theString += ' </select>';
				theString += '</td></tr>';
			}
		} else {
			var tempAreaValues = ZoomAreaValues[ZoomAreaIndex];
			theString += '<tr><td class="toolInfo1" align="left">';
			theString += '<select name="AreaName0" class="tocLayers">';
			theString += '<option selected value="">' + ZoomAreaAliases[ZoomAreaIndex];
			for (var i=0;i<tempAreaValues.length;i++) {
				theString += '<option value="' + tempAreaValues[i] + '">' + tempAreaValues[i];
			}
			theString += ' </select>';
			theString += '</td></tr>';
		}
		theString += '<tr><td align="left"><input class="toolInfo1" type="submit" value="Find It!" name="submit"></td></tr>'; 
		theString += '</table>';
		theString += '</form>';
		setupInputFormSidebar();
		updateContent("cxAttributeBottom",theString);
	} else {
		clickFunction("nothing");
		showSidebar("cxTOC");
		alert("Sorry, that layer is restricted and not available!");
	}
	return false;
}

// check that current zoom-to layer has a value list, retrieve samples if not
function Zoom2Area(tmpIndex) {
	ZoomAreaIndex = tmpIndex;
	// check first time through for indexes
	if (ZoomAreaLayerIndex.length == 0) {
		for  (var i=0;i<LayerID.length;i++) {
			for (var j=0;j<ZoomAreaLayerID.length;j++) {
				if (ZoomAreaLayerID[j] == LayerID[i]) ZoomAreaLayerIndex[j] = i;
			}
		}
	}
	// check if the current zoom layer has a value list
	if ((ZoomAreaValues[ZoomAreaIndex][0] == null) || (ZoomAreaValues[ZoomAreaIndex][0] == "")) {
		document.getElementById('LoadData').style.visibility = "visible";
		// ask for user to wait
		setupInputFormSidebar();
		var theString = '';
		theString += '<table cellpadding="5" cellspacing="0" width="100%">';
		theString += '<tr><td class="header1" width="100%" align="right"><a href="#" onmousedown="hideAttributeDisplay();"><img src="images/CXclose.gif" align="top"></a></td></tr>';
		//theString += '<tr><td class="header1" align="center">Zoom Map To Area/Landmark</td></tr>';
		theString += '<tr><td class="header1" align="center">' + ZoomAreaInfoString[ZoomAreaIndex] + '</td></tr>';
		theString += '<tr><td class="toolInfo1" align="center">Retrieving List...</td></tr>';
		theString += '</table>';
		updateContent("cxAttributeBottom",theString);
		// write and send request for samples
		if (ZoomAreaFields[ZoomAreaIndex].length > 1) {
			var tempZoomFields = ZoomAreaFields[ZoomAreaIndex];
			sampleField = "";
			for (var i=0;i<tempZoomFields.length;i++) {
				sampleField += ZoomAreaFields[ZoomAreaIndex][i];
				if (i<tempZoomFields.length-1) sampleField += " ";
			}
		} else { // only one field to search
			sampleField = ZoomAreaFields[ZoomAreaIndex][0];
		}
		var sampleString = writeCustomFieldSample(ZoomAreaLayerID[ZoomAreaIndex],sampleField,5000);
		sendToServer(imsQueryURL,sampleString,41);
	} else {
		writeZoom2AreaForm();
	}
}

// process query specifically for twp search/zoom
function sendAreaQueryString(tempString) {
	var newString = tempString.substring(5,tempString.length);
	newString = fixSingleQuotes(newString);
	newString = swapQuotes(newString);
	newString = makeXMLsafe(newString);
	document.getElementById('LoadData').style.visibility = "visible";
	var theString = writeAreaQueryXML(newString);
	isZoomAreaQuery = true;
	sendToServer(imsQueryURL,theString,42);
}

// write out XML request to query twp
function writeAreaQueryXML(queryString) {
	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" geometry="false" envelope="true"';
	theString += ' featurelimit="1000" beginrecord="1">\n';
	theString += '<LAYER id="' + ZoomAreaLayerID[ZoomAreaIndex] + '" />';
	theString += '<SPATIALQUERY subfields="#ALL#" where="' + queryString + '" />';
	theString += '</GET_FEATURES>';
	theString += '</REQUEST>';
	theString += '</ARCXML>';
	return theString;
}

// send user zoom-to request
function doZoom2Area() {
	var theString = '';
	// field 0
	if (document.ZoomAreaStuff.AreaName0.value != "") {
		if (ZoomAreaIsString[ZoomAreaIndex][0]) {
			theString += ' AND ' + ZoomAreaFields[ZoomAreaIndex][0] + ' LIKE ' + dQuote + document.ZoomAreaStuff.AreaName0.value + dQuote;
		} else {
			theString += ' AND ' + ZoomAreaFields[ZoomAreaIndex][0] + ' = ' + document.ZoomAreaStuff.AreaName0.value;
		}
	}
	// field 1, if exists
	if (document.ZoomAreaStuff.AreaName1 != null) {
		if (document.ZoomAreaStuff.AreaName1.value != "") {
			if (ZoomAreaIsString[ZoomAreaIndex][1]) {
				theString += ' AND ' + ZoomAreaFields[ZoomAreaIndex][1] + ' LIKE ' + dQuote + document.ZoomAreaStuff.AreaName1.value + dQuote;
			} else {
				theString += ' AND ' + ZoomAreaFields[ZoomAreaIndex][1] + ' = ' + document.ZoomAreaStuff.AreaName1.value;
			}
		}
	}
	// field 2, if exists
	if (document.ZoomAreaStuff.AreaName2 != null) {
		if (document.ZoomAreaStuff.AreaName2.value != "") {
			if (ZoomAreaIsString[ZoomAreaIndex][2]) {
				theString += ' AND ' + ZoomAreaFields[ZoomAreaIndex][2] + ' LIKE ' + dQuote + document.ZoomAreaStuff.AreaName2.value + dQuote;
			} else {
				theString += ' AND ' + ZoomAreaFields[ZoomAreaIndex][2] + ' = ' + document.ZoomAreaStuff.AreaName2.value;
			}
		}
	}
	// field 3, if exists
	if (document.ZoomAreaStuff.AreaName3 != null) {
		if (document.ZoomAreaStuff.AreaName3.value != "") {
			if (ZoomAreaIsString[ZoomAreaIndex][3]) {
				theString += ' AND ' + ZoomAreaFields[ZoomAreaIndex][3] + ' LIKE ' + dQuote + document.ZoomAreaStuff.AreaName3.value + dQuote;
			} else {
				theString += ' AND ' + ZoomAreaFields[ZoomAreaIndex][3] + ' = ' + document.ZoomAreaStuff.AreaName3.value;
			}
		}
	}
	if (theString == '') {	
		alert("You must specify an Area to Zoom To!");
		return false;
	} else {
		if (ZoomAreaMakeVisible[ZoomAreaIndex]) {
			// base or restricted layers are not changed!!
			if (LayerTypeUser[ZoomAreaLayerIndex[ZoomAreaIndex]] == "optional") {
				LayerVisible[ZoomAreaLayerIndex[ZoomAreaIndex]] = "true";  	
			} else if ((LayerTypeUser[ZoomAreaLayerIndex[ZoomAreaIndex]] == "overlay") || (LayerTypeUser[ZoomAreaLayerIndex[ZoomAreaIndex]] == "orthoimg")) {
				// reset all overlays
				var tempIndex = -1;
				for (var i=0;i<tocOverlayList.length;i++) {
					if (!isNaN(tocOverlayList[i][3])) LayerVisible[tocOverlayList[i][3]] = "false";
					if (tocOverlayList[i][1] == LayerID[ZoomAreaLayerIndex[ZoomAreaIndex]]) tempIndex = i;
				}
				if (groupedlayersAvailable) TurnGroupOverlaysOff();
				// set selected overlay visible
				if (tempIndex > -1) { // is single overlay or orthoimg
					LayerVisible[ZoomAreaLayerIndex[ZoomAreaIndex]] = "true";
					tocOverlayCurrent = LayerID[ZoomAreaLayerIndex[ZoomAreaIndex]];
					tocOverlayCurrentType = LayerTypeUser[ZoomAreaLayerIndex[ZoomAreaIndex]];
				}
			} else if (LayerTypeUser[ZoomAreaLayerIndex[ZoomAreaIndex]] == "grouped") {
				if (groupedlayersAvailable) {
					for (var j=0;j<groupedIDs.length;j++) {
						for (var k=0;k<groupedIDs[j].length;k++) {
							if (groupedIDs[j][k] == LayerID[ZoomAreaLayerIndex[ZoomAreaIndex]]) {
								groupedExpand[j] = true;
								groupedLayerVisible[j][k] = "true";
								for (var i=0;i<tocOptionalList.length;i++) {
									if (tocOptionalList[i][1] == groupedID[j]) tocOptionalList[i][3] = "true";
								}
								if (groupedType[j] == "subgroup") {
									for (var m=0;m<groupedIDs.length;m++) {
										for (var n=0;n<groupedIDs[m].length;n++) {
											if (groupedIDs[m][n] == groupedID[j]) {
												groupedExpand[m] = true;
												groupedVisible[m] = true;
												for (var i=0;i<tocOptionalList.length;i++) {
													if (tocOptionalList[i][1] == groupedID[m]) tocOptionalList[i][3] = "true";
												}
											}
										}
									}
								}
								TurnGroupOptionalOn(groupedID[j]);
							}
						}
					}
				}
			}
		}
		clickFunction("nothing");
		createTOC();
		showSidebar("cxTOC");
		sendAreaQueryString(theString);
	}
}

// check if layer has fieldNameList, and save selection if it does
function checkZoomAreaSave(theReply) {
	if (LayerSelectInfoIndex[ZoomAreaLayerIndex[ZoomAreaIndex]] != -1) {
		setActiveLayer(ZoomAreaLayerID[ZoomAreaIndex],false,false);
		saveToCurrentSelection(theReply);
	}
	isZoomAreaQuery = false;
	return false;
}