// 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] = "backgroundlayer";
ZoomAreaFields[0] = new Array("MUN_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 Section"
ZoomAreaLayerID[1] = "optional-pls";
ZoomAreaFields[1] = new Array("TOWN","RANGE","SECTION");
ZoomAreaAliases[1] = new Array("Township","Range","Section");
ZoomAreaIsString[1] = new Array(false,false,false);
ZoomAreaValues[1] = new Array();
ZoomAreaValues[1][0] = "17!!18!!19!!20";
ZoomAreaValues[1][1] = "18!!19!!20";
ZoomAreaValues[1][2] = "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!!37!!38!!39!!40!!41!!42!!43!!44!!45!!46!!47!!48!!49!!50!!51!!52!!53!!54!!55!!56!!57!!58!!59!!60!!61!!62!!63!!64!!65!!66!!67!!68!!69!!70!!71!!72!!73!!74!!75!!76!!77!!78!!79!!80!!81!!82!!83!!84!!85!!86!!87!!88!!89!!90!!91!!92!!93!!94!!95!!96!!97!!98!!99!!100!!101!!102!!103!!104!!105!!106!!107!!108!!109!!110!!111!!112!!113!!114!!115!!116!!117!!118!!119!!120!!121!!122!!123!!124!!125!!126!!127!!128!!129!!130!!131!!132!!133!!134!!135!!136!!137!!138!!139!!140!!141!!142!!143!!144!!145!!146!!147!!148!!149!!150!!151!!152!!153!!154!!155!!156!!157!!158!!159!!160!!161!!162!!163!!164!!165!!166!!167!!168!!169!!170!!171!!172!!173!!174!!175!!176!!177!!178!!179!!180!!181!!182!!183!!184!!185!!186!!187!!188!!189!!190!!191!!192!!193!!194!!195!!196!!197!!198!!199!!200!!201!!202!!203!!204!!205!!206!!207!!208!!209!!210!!211!!212!!213!!214!!215!!216!!217!!218!!219!!220!!221!!222!!223!!224!!225!!226!!227!!228!!229!!230!!231!!232!!233!!234!!235!!236!!237!!238!!239!!240!!241!!242!!243!!244!!245!!246!!247!!248!!249!!250!!251!!252!!253!!254!!255!!256!!257!!258!!259!!260!!261!!262!!263!!264!!265!!266!!267!!268!!269!!270!!271!!272!!273!!274!!275!!276!!277!!278!!279!!280!!281!!282!!283!!284!!285!!286!!287!!288!!289!!290!!291!!292!!293!!294!!295!!296!!297!!298!!299!!300!!301!!302!!303!!304!!305!!306!!307!!308!!309!!310!!311!!312!!313!!314!!315!!316!!317!!318!!319!!320!!321!!322!!323!!324!!325!!326!!327!!328!!329!!330!!331!!332!!333!!334!!335!!336!!337!!338!!339!!340!!341!!342!!343!!344!!345!!346!!347!!348!!349!!350!!351!!352!!353!!354!!355!!356!!357!!358!!359!!360!!361!!362!!363!!364!!365!!366!!367!!368!!369!!369.5!!370!!371!!372!!373!!374!!375!!376!!376.5!!377!!378!!379!!379.5!!380!!381!!382";
ZoomAreaMakeVisible[1] = true;
ZoomAreaSaveSelect[1] = true;

ZoomAreaInfoString[2] = "Zoom To Unincorporated Community"
ZoomAreaLayerID[2] = "optional-unincorp";
ZoomAreaFields[2] = new Array("NAME");
ZoomAreaAliases[2] = new Array("Community Name");
ZoomAreaIsString[2] = new Array(true);
ZoomAreaValues[2] = new Array();
ZoomAreaMakeVisible[2] = true;
ZoomAreaSaveSelect[2] = false;

ZoomAreaInfoString[3] = "Zoom To Facility"
ZoomAreaLayerID[3] = "optional-facilities";
ZoomAreaFields[3] = new Array("NAME");
ZoomAreaAliases[3] = new Array("Facility Name");
ZoomAreaIsString[3] = new Array(true);
ZoomAreaValues[3] = new Array();
ZoomAreaMakeVisible[3] = true;
ZoomAreaSaveSelect[3] = false;

ZoomAreaInfoString[4] = "Zoom To Park"
ZoomAreaLayerID[4] = "optional-parks";
ZoomAreaFields[4] = new Array("LABEL");
ZoomAreaAliases[4] = new Array("Parks");
ZoomAreaIsString[4] = new Array(true);
ZoomAreaValues[4] = new Array();
ZoomAreaMakeVisible[4] = true;
ZoomAreaSaveSelect[4] = false;


//******************************************************************************
// do not edit below here
//******************************************************************************
// program variables, don't need to specify
var ZoomAreaLayerIndex = new Array();
var ZoomAreaIndex = 0;
var isZoomAreaQuery = false;

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="ovInput">';
				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="ovInput">';
			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 = 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);
	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;
}