/* ***********************************************************
layer.js
Javascript for viewer.asp and find.asp pages 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 variables

var zMapLayersForm = new String("document.mapLayersForm.");

/* The onclick script for layer visiblity checkboxes that enforces the 
rule that only visible layers can be identified and show legends. 
Automatically checks 'off' layer identify checkbox when user checks 
of layer visiblity checkbox. */

function setVisible(n) {
	var val = new String("");
	var visiblename = new String("v" + n + "id");
    var legendname = new String("l" + n + "id");
  	var identifyname = new String("i" + n + "id");
	var selectname = new String("s" + n + "id");
	if (!document.getElementById(visiblename).checked) {
		if (document.getElementById(legendname)) {
			document.getElementById(legendname).checked = false;
		}
		if (document.getElementById(identifyname)) {
			document.getElementById(identifyname).checked = false;
		}	
		for (var i = 0; i < document.getElementsByName("s").length; i++) {
			val = document.getElementsByName("s")[i].id;
			if (val == selectname) {
				if (document.getElementsByName("s")[i].checked) {
					document.getElementsByName("s")[i].checked = false;
				}
			}
		}
	}
}

/* The onclick script for layer Legend checkboxes that enforces a rule that only
visible layers can show a legend. Checks on layer visibility checkbox when user 
checks on layer legend checkbox. */

function setVisibleLegend(n) {
	var visiblename = new String("v" + n + "id");
    var legendname = new String("l" + n + "id");
	if (document.getElementById(legendname).checked) {
		if (document.getElementById(visiblename)) {
			document.getElementById(visiblename).checked = true;
		}
	}
}

/* The onclick script for layer Identify checkboxes that enforces a rule that only
visible layers can be identified. Checks on layer visibility and show legend
checkboxes when user checks on layer identify checkbox. */

function setVisibleIdentify(n) {
	var visiblename = new String("v" + n + "id");
    var legendname = new String("l" + n + "id");
  	var identifyname = new String("i" + n + "id");
	if (document.getElementById(identifyname).checked) {
		if (document.getElementById(visiblename)) {
			document.getElementById(visiblename).checked = true;
		}
		if (document.getElementById(legendname)) {
			document.getElementById(legendname).checked = true;
		}
	}
}

/* The onclick script for layer Select radio buttons that enforces a rule
that only visible layers can be selected. Checks on layer visibility, show 
legend, and identify checkboxes when user checks on layer select checkbox. */

function setVisibleSelect(n) {
	var val = new String("")
	var visiblename = new String("v" + n + "id");
    var legendname = new String("l" + n + "id");
  	var identifyname = new String("i" + n + "id");
	var selectname = new String("s" + n + "id"); 
	for (var i = 0; i < document.getElementsByName("s").length; i++) {
		val = document.getElementsByName("s")[i].id;
		if (val == selectname) {
			if (document.getElementById(visiblename)) {
				document.getElementById(visiblename).checked = true;
			}
			if (document.getElementById(legendname)) {
				document.getElementById(legendname).checked = true;
			}
			if (document.getElementById(identifyname)) {
				document.getElementById(identifyname).checked = true;
			}
			break;
		}
	}
}

/* The onclick script for the Apply button associated with Map Layers option.
Turns layers on and off and updates the following form elements: visible, legend,
identify, identifyminscale, identifymaxscale, selectlayerid, selectminscale, 
and selectmaxscale. Map refreshed only if layer  visiblity changed. */

function setMapLayers(f) {
	// show download message
	showDownloadMessage()
	// submit form
	var s = new String("none");
	var visiblename = new String("");
	var visible = new String("");
	var avisible = new Array();
	var legendname = new String("");
	var legend = new String("");
	var alegend = new Array();
	var identifyname = new String("");
	var identify = new String("");
	var aidentify = new Array();
	var layerid = new String("");
	var alayerid = new Array();
	var activelayerid = new String("");
	var previousactivelayerid = new String("");
	var mapoptionlevel3 = new String("");

	visible = objMapForm.layersvisible.value;
	avisible = visible.split("|");
	legend = objMapForm.layerslegend.value;
	alegend = legend.split("|");
	identify = objMapForm.layersidentify.value;
	aidentify = identify.split("|");
	layerid = objMapForm.servicelayerid.value;
	alayerid = layerid.split("|")
	activelayerid = objMapForm.activelayerid.value;
	previousactivelayerid = objMapForm.activelayeridprevious.value;
	mapoptionlevel3 = objMapForm.mapoptionlevel3.value;
	
	// evaluate checkboxes and radio buttons
	
	var val = new Number(0);
	for (var i = 0; i < f.length; i++) {
		var e = f.elements[i];
		elementid = e.id;
		if (e.type == "checkbox") {
			val = parseInt(e.name);
			visibleid = "v" + val + "id";
  			legendid = "l" + val + "id";
  			identifyid = "i" + val + "id";		
			if (visibleid == elementid) {
				if (e.checked) {
					if (avisible[val] == "false") {
						// window.alert("Layer turned on: " + alayerid[val])
						s = "refreshmap";     // refresh map at current extent
					}
					avisible[val] = true;
					
				} else {
					if (avisible[val] == "true") {
						// window.alert("Layer turned off: " + alayerid[val])
						s = "refreshmap"     // refresh map at current extent
					}
					avisible[val] = false;
				}
			} else if (legendid == elementid) {
     			if (e.checked) {
      				alegend[val] = true;
     			} else {
      				alegend[val] = false;
     			} 
   			} else if (identifyid == elementid) {
     			if (e.checked) {
      				aidentify[val] = true;
     			} else {
      				aidentify[val] = false;
     			} 
   			}							 		
		} else if (e.type == "radio") {
			//window.alert(val)
			selectid = "s" + val + "id";
    		if (selectid == elementid) {
				if (e.checked) {
					// if user checked on the layer, set activelayerid = radio button's value
					if (alayerid[val] != activelayerid) {
						activelayerid = alayerid[val];
						//window.alert("activelayerid on = " + activelayerid)
					  	if ((mapoptionlevel3 == "select") || (mapoptionlevel3 == "autoselect")) {
					   		// refresh map if active layerid is different and there is a selection to unselect... so set mapoptionlevel3 = "none"
       						s = "refreshmap";     
       						mapoptionlevel3 = "none";
						}
      				}
				} else {
					// if user checked off the layer that was previously active, set activelayerid = ""
					if (alayerid[val] == activelayerid) {
						//window.alert("activelayerid off =  " + activelayerid)
						activelayerid = ""
					  	if ((mapoptionlevel3 == "select") || (mapoptionlevel3 == "autoselect")) {
					   		// refresh map if active layerid is different and there is a selection to unselect... so set mapoptionlevel3 = "none"
       						s = "refreshmap";     
       						mapoptionlevel3 = "none";
						}
					}
				}
    		}
		}
 	}
	
	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, "|");

	// window.alert("Map action: " + s)
	objMapForm.reset();
	objMapForm.mapaction.value = s;
	objMapForm.mapoptionlevel2.value = "layers";
	objMapForm.mapoptionlevel3.value = mapoptionlevel3;
	objMapForm.maptool.value = zMapTool;
	objMapForm.mapimgx.value = zImgMinX;
	objMapForm.mapimgy.value = zImgMaxY;
	objMapForm.mapimgx2.value = zImgMaxX;
	objMapForm.mapimgy2.value = zImgMinY;
	objMapForm.layersvisible.value = visible;
	objMapForm.layerslegend.value = legend;
	objMapForm.layersidentify.value = identify;
	setIdentifyLayerScaleThresholds();
	if (activelayerid != previousactivelayerid) {
		objMapForm.activelayeridprevious.value = "-1";
		setActiveLayer(activelayerid);
		setActiveLayerScaleThreshold(activelayerid);
	}
	objMapEvent.cursor = "wait";
	objMapForm.submit();
}

/* Sets active layer variables to default variables when new layer becomes the active layer. */

function setActiveLayer(activelayerid) {
	objMapForm.activelayerid.value = activelayerid;
	objMapForm.activefclass.value = "";  
	objMapForm.activeminscale.value = 0;
	objMapForm.activemaxscale.value = 0;
	objMapForm.activefields.value = "";
	objMapForm.activefieldstype.value = "";
	objMapForm.activesortfield.value = "";
	objMapForm.activesortfielddatatype.value = "";
	objMapForm.activesortorder.value = "";
	objMapForm.activelabelfield.value = "";
	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;	
	objMapForm.activefilterminx.value = 0;
	objMapForm.activefilterminy.value = 0;
	objMapForm.activefiltermaxx.value = 0;
	objMapForm.activefiltermaxy.value = 0;
	objMapForm.activefeaturecount.value = 0;
}

/* Sets identifyminscale and identifymaxscale with the smallest minscale
and highest maxscale for the group of layers that can be currently identified. 
This scale range is used to enable and disable the Identify map tool. */

function setIdentifyLayerScaleThresholds() {
	var min = new Number(999999);
	var max = new Number(-1);	
	var min2 = new Number(999999);
	var max2 = new Number(-1);	

	var identify = new String("");
	var aidentify = new Array();
	var layerminscale = new String("");
	var alayerminscale = new Array();  
	var layermaxscale = new String("");
	var alayermaxscale = new Array();  

	identify = objMapForm.layersidentify.value;
	aidentify = identify.split("|");
	layerminscale = objMapForm.servicelayerminscale.value;
	alayerminscale = layerminscale.split("|");
	layermaxscale = objMapForm.servicelayermaxscale.value;
	alayermaxscale = layermaxscale.split("|");

	// get min max scale for all identify layers	
	for (var i = 0; i < aidentify.length; i++) {
		if (aidentify[i] == "true") {
			min2 = parseFloat(alayerminscale[i]);
			max2 = parseFloat(alayermaxscale[i]);
			min = Math.min(min,min2);
			max = Math.max(max,max2);
		}
	}
	
	// set form 
	objMapForm.identifyminscale.value = min;
	objMapForm.identifymaxscale.value = max;
}

/* Sets activeminscale and activemaxscale to the minscale and maxscale values 
for the currently active layer. This scale range is used to enable and disable
the Select Map Features and Auto Select tools. */

function setActiveLayerScaleThreshold(layerid) {
  if (layerid.length > 0) {
		var min = new String("");
		var max = new String("");	

		var layerids = new String("");
		var alayerids = new Array();
		var layerminscale = new String("");
		var alayerminscale = new Array();  
		var layermaxscale = new String("");
		var alayermaxscale = new Array();  

		layerids = objMapForm.servicelayerid.value;
		alayerids = layerids.split("|");
		layerminscale = objMapForm.servicelayerminscale.value;
		alayerminscale = layerminscale.split("|");
		layermaxscale = objMapForm.servicelayermaxscale.value;
		alayermaxscale = layermaxscale.split("|");

		// get min max scale for active layer	
		for (var i = 0; i < alayerids.length; i++) {
			if (layerid == alayerids[i]) {
				min = alayerminscale[i];
				max = alayermaxscale[i];
				break;
			}
		}
		// set form 
		objMapForm.activeminscale.value = min;
		objMapForm.activemaxscale.value = max;
	} 
}