// CivXplorer Zoom To Landmark Layer

// Suzanne Fliege
// Red Horse Geographics, LLC

// contains functions for zooming to an area, defined by a map layer.
findbylandmarkAvailable = 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)

groupIndexCount = 0;
ZoomAreaInfoString[groupIndexCount] = "Zoom to Park"
ZoomAreaLayerID[groupIndexCount] = "optional-parks";
ZoomAreaFields[groupIndexCount] = new Array("PL_NAME");
ZoomAreaAliases[groupIndexCount] = new Array("Parks");
ZoomAreaIsString[groupIndexCount] = new Array(true);
ZoomAreaValues[groupIndexCount] = new Array();
ZoomAreaMakeVisible[groupIndexCount] = true;
ZoomAreaSaveSelect[groupIndexCount] = false;

groupIndexCount += 1;
ZoomAreaInfoString[groupIndexCount] = "Zoom to Bus Route"
ZoomAreaLayerID[groupIndexCount] = "grouped-busroute";
ZoomAreaFields[groupIndexCount] = new Array("NAME");
ZoomAreaAliases[groupIndexCount] = new Array("Bus Route Name");
ZoomAreaIsString[groupIndexCount] = new Array(true);
ZoomAreaValues[groupIndexCount] = new Array();
ZoomAreaMakeVisible[groupIndexCount] = true;
ZoomAreaSaveSelect[groupIndexCount] = false;

groupIndexCount += 1;
ZoomAreaInfoString[groupIndexCount] = "Zoom to Neighborhood"
ZoomAreaLayerID[groupIndexCount] = "overlay-neighborhood";
ZoomAreaFields[groupIndexCount] = new Array("NGHBR_NAME");
ZoomAreaAliases[groupIndexCount] = new Array("Neighborhood Name");
ZoomAreaIsString[groupIndexCount] = new Array(true);
ZoomAreaValues[groupIndexCount] = new Array();
ZoomAreaMakeVisible[groupIndexCount] = true;
ZoomAreaSaveSelect[groupIndexCount] = true;

groupIndexCount += 1;
ZoomAreaInfoString[groupIndexCount] = "Zoom to Historic Place"
ZoomAreaLayerID[groupIndexCount] = "optional-histplace";
ZoomAreaFields[groupIndexCount] = new Array("NAME");
ZoomAreaAliases[groupIndexCount] = new Array("Historic Place");
ZoomAreaIsString[groupIndexCount] = new Array(true);
ZoomAreaValues[groupIndexCount] = new Array();
ZoomAreaMakeVisible[groupIndexCount] = true;
ZoomAreaSaveSelect[groupIndexCount] = false;

groupIndexCount += 1;
ZoomAreaInfoString[groupIndexCount] = "Zoom to Local Historic District"
ZoomAreaLayerID[groupIndexCount] = "overlay-historic";
ZoomAreaFields[groupIndexCount] = new Array("DISTRICT");
ZoomAreaAliases[groupIndexCount] = new Array("Local Historic District");
ZoomAreaIsString[groupIndexCount] = new Array(true);
ZoomAreaValues[groupIndexCount] = new Array();
ZoomAreaMakeVisible[groupIndexCount] = true;
ZoomAreaSaveSelect[groupIndexCount] = false;

groupIndexCount += 1;
ZoomAreaInfoString[groupIndexCount] = "Zoom to National Historic District"
ZoomAreaLayerID[groupIndexCount] = "overlay-nathistdist";
ZoomAreaFields[groupIndexCount] = new Array("DISTRICT");
ZoomAreaAliases[groupIndexCount] = new Array("National Historic District");
ZoomAreaIsString[groupIndexCount] = new Array(true);
ZoomAreaValues[groupIndexCount] = new Array();
ZoomAreaMakeVisible[groupIndexCount] = true;
ZoomAreaSaveSelect[groupIndexCount] = false;

//******************************************************************************
// do not edit below here
//******************************************************************************
// program variables, don't need to specify
var ZoomAreaLayerIndex = new Array();
var ZoomAreaIndex = 0;
var isZoomAreaQuery = false;

function addZoomAreaItems() {
	var tempString = "";
	var IsOkToOpenForm = "false";
	for (var i=0;i<ZoomAreaInfoString.length;i++) {
		for  (var j=0;j<LayerID.length;j++) {
			if (ZoomAreaLayerID[i] == LayerID[j]) IsOkToOpenForm = LayerAvailable[j];
		}
		if (IsOkToOpenForm == "true") {
			tempString += '<option value="landmark:'+i+'">' + ZoomAreaInfoString[i] + '</option>';
			IsOkToOpenForm = "false";
		}
	}
	return tempString;
}

// writes specific form for zoom to landmark
function writeZoom2AreaForm() {
	var theString = "";
	theString += '<table width="100%" cellpadding="4" cellspacing="0">';
	theString += '<tr><td class="textHeader" align="center">' + ZoomAreaInfoString[ZoomAreaIndex] + '</td></tr>';
	theString += '<tr><td><table width="100%" cellpadding="2" cellspacing="0">';
	// add in dropdown(s) for values
	if (ZoomAreaFields[ZoomAreaIndex].length > 1) { // more than one field to search
		for (var j=0;j<ZoomAreaFields[ZoomAreaIndex].length;j++) {
			var tempAreaValues = ZoomAreaValues[ZoomAreaIndex][j].split("!!");
			theString += '<tr><td class="textBold2" align="center">' + ZoomAreaAliases[ZoomAreaIndex][j] + ':</td></tr>';
			theString += '<tr><td class="textBold2" align="center">';
			theString += '<select id="AreaName' + j + '" class="selectNormal2">';
			theString += '<option selected value="">' + ZoomAreaAliases[ZoomAreaIndex][j] + '</option>';
			for (var i=0;i<tempAreaValues.length;i++) {
				theString += '<option value="' + tempAreaValues[i] + '">' + tempAreaValues[i] + '</option>';
			}
			theString += ' </select>';
			theString += '</td></tr>';
		}
	} else { // single field search
		var tempAreaValues = ZoomAreaValues[ZoomAreaIndex];
		theString += '<tr><td class="textBold2" align="center">' + ZoomAreaAliases[ZoomAreaIndex] + ':</td></tr>';
		theString += '<tr><td class="textBold2" align="center">';
		theString += '<select id="AreaName0" class="selectNormal2">';
		theString += '<option selected value="">' + ZoomAreaAliases[ZoomAreaIndex] + '</option>';
		for (var i=0;i<tempAreaValues.length;i++) {
			theString += '<option value="' + tempAreaValues[i] + '">' + tempAreaValues[i] + '</option>';
		}
		theString += ' </select>';
		theString += '</td></tr>';
	}
	theString += '</table></td></tr>';
	theString += '<tr><td align="center"><input onclick="JavaScript:doZoom2Area();" type="button" value="Find It!" class="toolButtonSmallNormal" onmouseover="this.className=\'toolButtonSmallHighlight\';" onmouseout="this.className=\'toolButtonSmallNormal\';"/></td></tr>'; 
	theString += '</table>';
	updateContent('cxSearch',theString);
	return false;
}

// check that current zoom-to layer has a value list, retrieve samples if not
function Zoom2Area() {
	// 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] == "")) {
		ProtectorOn();
		// 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 = writeFieldSample(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);
	ProtectorOn();
	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() {
	workingParcelQueryInfoString = "";
	var theString = '';
	// field 0
	if (theCXForm.AreaName0.value != "") {
		if (ZoomAreaIsString[ZoomAreaIndex][0]) {
			theString += ' AND ' + ZoomAreaFields[ZoomAreaIndex][0] + ' LIKE ' + dQuote + theCXForm.AreaName0.value + dQuote;
		} else {
			theString += ' AND ' + ZoomAreaFields[ZoomAreaIndex][0] + ' = ' + theCXForm.AreaName0.value;
		}
	}
	// field 1, if exists
	if (theCXForm.AreaName1 != null) {
		if (theCXForm.AreaName1.value != "") {
			if (ZoomAreaIsString[ZoomAreaIndex][1]) {
				theString += ' AND ' + ZoomAreaFields[ZoomAreaIndex][1] + ' LIKE ' + dQuote + theCXForm.AreaName1.value + dQuote;
			} else {
				theString += ' AND ' + ZoomAreaFields[ZoomAreaIndex][1] + ' = ' + theCXForm.AreaName1.value;
			}
		}
	}
	// field 2, if exists
	if (theCXForm.AreaName2 != null) {
		if (theCXForm.AreaName2.value != "") {
			if (ZoomAreaIsString[ZoomAreaIndex][2]) {
				theString += ' AND ' + ZoomAreaFields[ZoomAreaIndex][2] + ' LIKE ' + dQuote + theCXForm.AreaName2.value + dQuote;
			} else {
				theString += ' AND ' + ZoomAreaFields[ZoomAreaIndex][2] + ' = ' + theCXForm.AreaName2.value;
			}
		}
	}
	// field 3, if exists
	if (theCXForm.AreaName3 != null) {
		if (theCXForm.AreaName3.value != "") {
			if (ZoomAreaIsString[ZoomAreaIndex][3]) {
				theString += ' AND ' + ZoomAreaFields[ZoomAreaIndex][3] + ' LIKE ' + dQuote + theCXForm.AreaName3.value + dQuote;
			} else {
				theString += ' AND ' + ZoomAreaFields[ZoomAreaIndex][3] + ' = ' + theCXForm.AreaName3.value;
			}
		}
	}
	if (theString == '') {	
		alert("You must specify an Area to Zoom To!");
		return false;
	} else {
		closePopupDisplay();
		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]);
							}
						}
					}
				}
			}
			createTOC();
		}
		clickFunction("nothing");
		sendAreaQueryString(theString);
	}
	return false;
}

// 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);
		displaySelectData(false);
	}
	isZoomAreaQuery = false;
	return false;
}
