// CiviXplorer 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 City"
ZoomAreaLayerID[0] = "base-citylimits";
ZoomAreaFields[0] = new Array("CITY");
ZoomAreaAliases[0] = new Array("Municipality");
ZoomAreaIsString[0] = new Array(true);
ZoomAreaValues[0] = new Array();
ZoomAreaMakeVisible[0] = true;
ZoomAreaSaveSelect[0] = false;

ZoomAreaInfoString[1] = "Zoom to School"
ZoomAreaLayerID[1] = "optional-pubschl";
ZoomAreaFields[1] = new Array("SCHNAME");
ZoomAreaAliases[1] = new Array("Schools");
ZoomAreaIsString[1] = new Array(true);
ZoomAreaValues[1] = new Array();
ZoomAreaMakeVisible[1] = true;
ZoomAreaSaveSelect[1] = true;

ZoomAreaInfoString[2] = "Zoom to Zipcode"
ZoomAreaLayerID[2] = "overlay-zip";
ZoomAreaFields[2] = new Array("ZIP");
ZoomAreaAliases[2] = new Array("Zipcode");
ZoomAreaIsString[2] = new Array(false);
ZoomAreaValues[2] = new Array();
ZoomAreaMakeVisible[2] = true;
ZoomAreaSaveSelect[2] = 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 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 + '">';
			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);
	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 grouped 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) || (advancedGroupedLayersAvailable)) 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]];
				}
			}
		}
		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;
}