/* ***********************************************************
select.js
Javascript for viewer.asp page in Map Viewer
by Howie Sternberg (howies@snet.net)

Map Viewer is placed in the public domain and is "Freeware". 
Map Viewer may be freely used and redistributed, is provided 
"AS-IS" without warranty of any kind, and there is no technical 
support provided.
--------------------------------------------------------------
Dependencies - DHTMLmapviewer.js  
--------------------------------------------------------------
History - Sep 2004, Initial code for Map Viewer 2nd Edition
May 2005, Revised for Map Viewer 3rd Edition to only support  
		  W3C DOM compatible browsers 
************************************************************ */

// Global, reusable variable for holding a form value
var v = new String("")

// Array of images used for ascending sort links
var aImgAscend = new Array();
aImgAscend[0] = new Image(9,9);
aImgAscend[1] = new Image(9,9);
aImgAscend[0].src = "images/sortascending_0.gif";
aImgAscend[1].src = "images/sortascending_1.gif";

// Array of images used for descending sort links
var aImgDescend = new Array();
aImgDescend[0] = new Image(9,9);
aImgDescend[1] = new Image(9,9);
aImgDescend[0].src = "images/sortdescending_0.gif";
aImgDescend[1].src = "images/sortdescending_1.gif";

// Array of images used for descending sort links
var aImgText = new Array();
aImgText[0] = new Image(10,9);
aImgText[1] = new Image(10,9);
aImgText[0].src = "images/text_0.gif";
aImgText[1].src = "images/text_1.gif";

// Image onmouseout event handler for Sort Ascending link
function sortAscendOut(o) {
	o.src = aImgAscend[0].src;
}

// Image onmouseover event handler for Sort Ascending link
function sortAscendOver(o) {
	o.src = aImgAscend[1].src;
}

// Image onmouseout event handler for Sort Desending link
function sortDescendOut(o) {
	o.src = aImgDescend[0].src;
}

// Image onmouseover event handler for Sort Desending link
function sortDescendOver(o) {
	o.src = aImgDescend[1].src;
}

// Image onmouseout event handler for Label Map Feature link
function labelTextOut(o) {
	o.src = aImgText[0].src;
}

// Image onmouseover event handler for Label Map Feature link
function labelTextOver(o) {
	o.src = aImgText[1].src;
}

// Labels features
function labelSelection(f) {
	// show download message
	showDownloadMessage()
	// submit form
	objMapForm.reset();
	objMapForm.mapaction.value = "refreshmap";
	objMapForm.activelabelfield.value = f;
	objMapForm.submit();
}

// Stop Labeling features
function clearLabelSelection(f) {
	// show download message
	showDownloadMessage()
	// submit form
	objMapForm.reset();
	objMapForm.mapaction.value = "refreshmap";
	objMapForm.activelabelfield.value = "";
	objMapForm.submit();
}

// Sorts features in ascending or descending order
function sortSelection(f,o) {
	// show download message
	showDownloadMessage()
	// submit form
	objMapForm.reset();
	objMapForm.activesortfield.value = f;
	objMapForm.activesortorder.value = o;
	objMapForm.activelistbeginrecord.value = 1;
	objMapForm.submit();
}

/* Selects features begining at specified record number.
Used to navigate individual Page Results, BACK and NEXT pages. */

function listSelection(begin) {
	// show download message
	showDownloadMessage()
	// submit form
	objMapForm.reset();
	objMapForm.activelistbeginrecord.value = begin;
	objMapForm.submit();
}

/* Starts and Stops Auto Select function, which peforms a spatial
query of active layer features based on current map evelope.*/ 

function autoSelection(s) {
	// show download message
	showDownloadMessage()
	// submit form
	objMapForm.reset();
	objMapForm.mapoptionlevel2.value = "none";
	if (s == "on") {
		objMapForm.mapaction.value = "refreshmap";
		objMapForm.mapoptionlevel3.value = "autoselect";
		objMapForm.activefield.value = "";
		objMapForm.activefieldval.value = "";
		objMapForm.activefieldatatype.value = "";
		objMapForm.activeoperator.value = "";
		objMapForm.activelogicaloperator.value = "";		
		objMapForm.activefeaturelimit.value = "All";		
		objMapForm.activebeginrecord.value = 1;						
		objMapForm.activelistbeginrecord.value = 1;	
	} else {
		objMapForm.mapaction.value = "none";
		if (objMapForm.activefeaturecount.value == "0") {
			// no selection to preserve
			objMapForm.mapoptionlevel3.value = "none";
		} else {
			// preserve selection
			objMapForm.mapoptionlevel3.value = "select";
		}
	}			
	objMapForm.submit();
}	

/* Updates the layersvisible, layerslegend, layersidentify form elements so that a
a layerid that is programmatically made active by the setSelection() function is
by default made visible, appears in the legend, and can be identified (as if all
of these options where manually checked for the layer in the Layers form). */

function setLayersForActiveLayer(layerid) {
	
	var visible = new String("");
	var avisible = new Array();
	var legend = new String("");
	var alegend = new Array();
	var identify = new String("");
	var aidentify = new Array();
	var layerids = new String("");
	var alayerids = new Array();
	
	visible = objMapForm.layersvisible.value;
	avisible = visible.split("|");
	legend = objMapForm.layerslegend.value;
	alegend = legend.split("|");
	identify = objMapForm.layersidentify.value;
	aidentify = identify.split("|");
	layerids = objMapForm.servicelayerid.value;
	alayerids = layerids.split("|")

	// check on visible, legend, identify	
	for (var i = 0; i < alayerids.length; i++) {
		if (layerid == alayerids[i]) {
			avisible[i] = true;
			alegend[i] = true;
			aidentify[i] = true;
			break;
		}
	}

	visible = avisible.toString();
	// replace "," with "|";
	var re = /,/gi;
	visible = visible.replace (re, "|");

	legend = alegend.toString();
	// replace "," with "|";
	var re = /,/gi;
	legend = legend.replace (re, "|");

	identify = aidentify.toString();
	// replace "," with "|";
	var re = /,/gi;
	identify = identify.replace (re, "|");

	objMapForm.layersvisible.value = visible;
	objMapForm.layerslegend.value = legend;
	objMapForm.layersidentify.value = identify;
}

// Selects and reselects features based on a query expression and/or rectangular spatial filter.
function setSelection(layerid, dospatial, donewquery, field, fieldval, fielddatatype, operator, logicaloperator, featurelimit, beginrecord, listfeaturelimit, listbeginrecord) {
	// show download message
	showDownloadMessage()
	// submit form
	var zContinue
	zContinue = Boolean(true);
	objMapForm.reset();
	objMapForm.mapaction.value = "zoomtoselected";
	objMapForm.propertyfeaturelimitlist.value = listfeaturelimit;
	objMapForm.activelayerid.value = layerid;
	objMapForm.activefeaturelimit.value = featurelimit;
	objMapForm.activebeginrecord.value = beginrecord;
	objMapForm.activelistbeginrecord.value = listbeginrecord;
	// do spatial query
	if (dospatial == "false") {	
		objMapForm.activefilterminx.value = 0;
		objMapForm.activefilterminy.value = 0;
		objMapForm.activefiltermaxx.value = 0;
		objMapForm.activefiltermaxy.value = 0;
	}
	// do new query (single field query)
	if (donewquery == "true") {	
		objMapForm.activefield.value = field;
		objMapForm.activefieldval.value = fieldval;
		objMapForm.activefieldatatype.value = fielddatatype;
		objMapForm.activeoperator.value = operator;
		objMapForm.activelogicaloperator.value = "";
	// compound query - (e.g. COUNTRY like 'United' and LANGUAGE = 'English')
	} else if (donewquery == "false") {	
		var prevsearchfield = new String("");
		var prevfield = new String("");
		var prevfieldval = new String("");
		var prevfielddatatype = new String("");
		var prevoperator = new String("");
		var prevlogicaloperator = new String("");
		prevsearchfield = objMapForm.searchfield.value;
		prevfield = objMapForm.activefield.value;
		prevfieldval = objMapForm.activefieldval.value;
		prevfielddatatype = objMapForm.activefieldatatype.value;
		prevoperator = objMapForm.activeoperator.value;
		prevlogicaloperator = objMapForm.activelogicaloperator.value;
		if ((prevfield.length) == 0) {
			objMapForm.activefield.value = field;
			objMapForm.activefieldval.value = fieldval;
			objMapForm.activefieldatatype.value = fielddatatype;
			objMapForm.activeoperator.value = operator;
			objMapForm.activelogicaloperator.value = logicaloperator;
		} else {
		if (prevsearchfield != field) {
				var aprevfield = new Array();
				aprevfield = prevfield.split("|");
				for (var i = 0; i < aprevfield.length; i++) {
					if (field == aprevfield[i]) {
						zContinue = Boolean(false);
						window.alert("Invalid field. Current selection is already based on a " + field + " value.");
						break;
					}
				}
			}
			if (zContinue) {
				v = prevfield + "|" + field;
				objMapForm.activefield.value = v;
				v = prevfieldval + "|" + fieldval;
				objMapForm.activefieldval.value = v;
				v = prevfielddatatype + "|" + fielddatatype;
				objMapForm.activefieldatatype.value = v;
				v = prevoperator + "|" + operator;
				objMapForm.activeoperator.value = v;
				v = prevlogicaloperator + "|" + logicaloperator;
				objMapForm.activelogicaloperator.value = v;
			}
		}
	}
	//window.alert(" operator - " + objMapForm.activeoperator.value") + "\n fieldval - " + objMapForm.activefieldval.value") + "\n field - " + objMapForm.activefield.value") + "\n logial - " + objMapForm.activelogicaloperator.value"))
	if (zContinue) {
		// Make adjustments to other form variables affected by active layer.
		setLayersForActiveLayer(layerid);
		setActiveLayerScaleThreshold(layerid);
		setIdentifyLayerScaleThresholds();	
		objMapForm.submit();
	}
}