var newMinx = 0;
var newMiny = 0;
var newMaxx = 0;
var newMaxy = 0;
var boxMinx = 0;
var boxMiny = 0;
var boxMaxx = 0;
var boxMaxy = 0;
var maxx, maxy,minx,miny;
var mapClickAsRecenter = true;
var mapBoxAsZoom = true;
var allowRubberband = true;
var allowMapClick = true;
var state = "ZOOMIN";  // 
var curTool = "ZOOMIN"  // ZOOMIN, ZOOMOUT, PAN, SELECT, IDENTIFY, AREA

// Global vars to save mouse position
var mouseX=0;
var mouseY=0;
var x1=0;
var y1=0;
var x2=0;
var y2=0;
var zminx=0;
var zmaxx=0;
var zmaxy=0;
var zminy=0;

var mapX = 0; 
var mapY = 0; 
var zoomBoxWidth = 1;
var zoomBoxColor = "#FF0000";
var zooming=false;
var panning=false;
var bottomBorderHeight = 0;

//Arrays to hold x and y for polys
var xArr = new Array();
var yArr = new Array();
var xMapArr = new Array() ;
var yMapArr = new Array();

var toolObject;
var docID;
var cmdObject;

// setup test for Nav 4.0
var isIE = false;
var isNav = (navigator.appName.indexOf("Netscape")>=0);
var isNav4 = false;
var isIE4 = false;
var is5up = false;
var isMac = false;
var isWin = false;
var isIE6 = false;
var hspc;
var vspc;
var iWidth=500
var iHeight=400
var arIdentifyLayers = new Array();
if (isNav) {
	
	if (parseFloat(navigator.appVersion)<5) {
		isNav4=true;
		//alert("Netscape 4.x or older");
	} else {
		is5up = true;
	}
} else {
	isIE4=true;
	isIE=true;
	if (navigator.appVersion.indexOf("MSIE 5")>0) {
		isIE4 = false;
		is5up = true;
		isIE6 =false;
		//alert("IE5");
	} else if (navigator.appVersion.indexOf("MSIE 6")>0) {
		isIE4 = false;
		is5up = false;
		isIE6 = true;
	}
}	

if (navigator.userAgent.indexOf("Win") >= 0) 
	isWin = true;
else
	isMac = true;

//***********************************************	
//***************** FUNCTIONS *******************
//***********************************************

// check for mouseup
function chkMouseUp(e) { 
	if (zooming || panning) {
		if (mouseX<0)
		 	mouseX = 0;
		if (mouseX>iWidth)
			mouseX = iWidth;
		if (mouseY<0)
			mouseY = 0;
		if (mouseY>iHeight)
			mouseY = iHeight;
		mapTool(e);
	}
}


// get Image X Y
function getImageXY(e) {
	
	if (isNav) {
		mouseX=e.pageX;
		mouseY=e.pageY;
	} else if (is5up) {
		
		mouseX=event.clientX + document.body.scrollLeft;
		mouseY=event.clientY + document.body.scrollTop;
	} else {
		//We're in 6.0
		mouseX=event.clientX + document.documentElement.scrollLeft;
		mouseY=event.clientY + document.documentElement.scrollTop;
	}
	// subtract offsets from page left and right
	mouseX = mouseX-hspc;
	mouseY = mouseY-vspc;
}	


// convert mouse click xy's into map coordinates
function getMapXY(xIn,yIn) {

	mouseX = xIn;
	var pixelX = (maxx-minx) / iWidth;
	mapX = pixelX * mouseX + minx;
	mouseY = iHeight - yIn;
	var pixelY = (maxy-miny) / iHeight;
	mapY = pixelY * mouseY + miny;
	if (isNav){
		mapX = mapX+60.0;
		mapY = mapY-25.0;
	} else {
		mapX = mapX-57;
		mapY = mapY+40;
	}
}


// get the coords at mouse position
function getMouse(e) {
 
	window.status="";
	getImageXY(e);
	if (zooming) {
		if (mouseX<0)
		 	mouseX = 0;
		if (mouseX>iWidth)
			mouseX = iWidth;
		if (mouseY<0)
			mouseY = 0;
		if (mouseY>iHeight-bottomBorderHeight)
			mouseY = iHeight-bottomBorderHeight;
		x2=mouseX;
		y2=mouseY;
		window.status=mouseX+":"+mouseY; 
		setClip();
		return false;
	} else if (panning) {
		x2=mouseX;
		y2=mouseY;
		panMouse();	
		return false;
	} else 
    	return true;
	return true;
}


// hide zoom box
function hideZoomBox() {
	hideLayer(docID+"_zoomBox");
	//hideLayer(docID+"_zoomBoxLeft");
	//hideLayer(docID+"_zoomBoxRight");
	//hideLayer(docID+"_zoomBoxBottom");
}


// perform appropriate action with mapTool
function mapTool (e) {
	
	getImageXY(e);
	
	  // Deal with the possibility of an
	  // select, identify or area tool first, since it is a
	  // little different than the other
	  // states (doesn't require a mouse
	  // up event).  
	  	
	if ((curTool == "SELECT") && 
	    (mouseX >= 0) && (mouseX <= iWidth) && 
	    (mouseY >= 0) && (mouseY <= iHeight)){
			getMapXY(mouseX,mouseY);
			sendPoint(mapX,mapY, curTool);
			return false; // <-- false assures that the popup
			              //     box stays in front.
	}
	
	if ((curTool == "GEOM") && 
	    (mouseX >= 0) && (mouseX <= iWidth) && 
	    (mouseY >= 0) && (mouseY <= iHeight)){
			getMapXY(mouseX,mouseY);
			sendPoint(mapX,mapY, curTool);
			return false; // <-- false assures that the popup
			              //     box stays in front.
	}
	
	if ((curTool == "IDENTIFY") && 
	    (mouseX >= 0) && (mouseX <= iWidth) && 
	    (mouseY >= 0) && (mouseY <= iHeight)){
			getMapXY(mouseX,mouseY);
			sendPoint(mapX,mapY, curTool);
			return false; // <-- false assures that the popup
			              //     box stays in front.
	}
	
	if ((curTool == "AREA") && 
	    (mouseX >= 0) && (mouseX <= iWidth) && 
	    (mouseY >= 0) && (mouseY <= iHeight)){
			getMapXY(mouseX,mouseY);
			addAreaPoint(mapX,mapY);
			//addAreaImagePoint(mouseX,mouseY);
			return false; // <-- false assures that the popup
			              //     box stays in front.
	}
	
	if ((curTool == "ADDPOLY") && 
	    (mouseX >= 0) && (mouseX <= iWidth) && 
	    (mouseY >= 0) && (mouseY <= iHeight)){
			getMapXY(mouseX,mouseY);
			
			doPoly(mapX,mapY);
			//addAreaImagePoint(mouseX,mouseY);
			return false; // <-- false assures that the popup
			              //     box stays in front.
	}
	
	  // If we made it to here, it's a 
	  // navigational click and not an
	  // identify.
	
	if ((!zooming) && (!panning) && 
		(mouseX >= 0) && (mouseX <= iWidth) && 
		(mouseY >= 0) && (mouseY <= iHeight)) {
		if (curTool == "PAN")
			startPan(e);
		else 
			startZoomBox(e);
			
		return false;
	} else if (zooming) {
		getMouse(e);
		stopZoomBox(e);
	} else if (panning) {
		getMouse(e);
		stopPan(e);
	}
	return true;
}


// move map image with mouse
function panMouse() {
	var xMove = x2-x1;
	var yMove = y2-y1;
	var cLeft = -xMove;
	var cTop = -yMove;
	var cRight = iWidth;
	var cBottom = iHeight;
	if (xMove>0) {
		cLeft = 0;
		cRight = iWidth - xMove;
	}
	if (yMove>0) {
		cTop = 0;
		cBottom = iHeight - yMove;
	}
	
	clipLayer(docID+"_theMap",cLeft,cTop,cRight,cBottom);
	
	if (xMove!=0)
		moveLayer(docID+"_theMap",xMove+hspc,yMove+vspc);
	
	return false;
}


// refresh map
function refreshMap() {
  hideZoomBox();
  //alert("Hello");
  
  cmdObject.value = state;
  var oldExtent = minx+":"+miny+":"+maxx+":"+maxy;
  document.getElementById(docID+"_txtPrevExtent").value=oldExtent;
  document.getElementById(docID+"_minx").value = newMinx.toString();
  document.getElementById(docID+"_miny").value = newMiny.toString();
  document.getElementById(docID+"_maxx").value = newMaxx.toString();
  document.getElementById(docID+"_maxy").value = newMaxy.toString();
  
  if (isNav)
	document.getElementById(docID+"_hiddenButton").click();
  else 
	document.getElementById(docID+"_imgMap").click();
  
  showLayer(docID+"_loadLayer");
}
function click(){
	return true;
}

// refresh map
function sendMap() {
  hideZoomBox();
   
  document.getElementById(docID+"_imgMap").click();
   showLayer(docID+"_loadLayer");
}


// send select
function sendPoint(numMapX,numMapY, cmd) {
	
	cmdObject.value = cmd;
	
    document.getElementById(docID+"_selX").value = numMapX.toString();
    document.getElementById(docID+"_selY").value = numMapY.toString();
    if(isNav) {
		document.getElementById(docID+"_hiddenButton").click();
    } else 
		document.getElementById(docID+"_imgMap").click();
   showLayer(docID+"_loadLayer");
}




// clip zoom box layer to mouse coords
function setClip() {	

	if (x1>x2) {
		zmaxx=x1;
		zminx=x2;
	} else {
		zminx=x1;
		zmaxx=x2;
	}
	if (y1>y2) {
		zminy=y1;
		zmaxy=y2;
		
	} else {
		zmaxy=y1;
		zminy=y2;
		
	}
	
	
	if ((x1 != x2) && (y1 != y2)) {
		
		moveLayer(docID+"_zoomBox", zminx+hspc, zmaxy+vspc);
		resizeLayer(docID+"_zoomBox", Math.abs(zmaxx-zminx), Math.abs(zmaxy-zminy));
		//clipLayer("zoomBox",zminx,zminy,zmaxx,zmaxy);
		showLayer(docID+"_zoomBox");
		//clipLayer(docID+"_zoomBox",x1,y1,wd,ht);
		//clipLayer(docID+"_zoomBoxLeft",zminx,zmaxy,zminx+zoomBoxWidth,zminy);
		//clipLayer(docID+"_zoomBoxRight",zmaxx-zoomBoxWidth,zmaxy,zmaxx,zminy);
		//clipLayer(docID+"_zoomBoxBottom",zminx,zminy-zoomBoxWidth,zmaxx,zminy);
	}
}


// set extent
function setExtent(_minx,_miny,_maxx,_maxy) {
	minx = _minx;
	miny = _miny;
	maxx = _maxx;
	maxy = _maxy;
}





//set loc box
function setLocBox(){
	fminX = 1343905;
	fminY = 460979;
	fmaxX = 1577358;
	fmaxY = 647741;
	var offX=3;
	var offY=3;
	mminX = document.getElementById(docID+'_minx').value;
	mminY = document.getElementById(docID+'_miny').value;
	mmaxX = document.getElementById(docID+'_maxx').value;
	mmaxY = document.getElementById(docID+'_maxy').value;
	
	pX1 = parseInt((mminX - fminX) / (fmaxX - fminX) * 143);
	pX2 = parseInt((mmaxX - fminX) / (fmaxX - fminX) * 143);
	pY1 = parseInt((fmaxY - mmaxY) / (fmaxY - fminY) * 117);
	pY2 = parseInt((fmaxY - mminY) / (fmaxY - fminY) * 117);
	
	
	if(pX2 - pX1 < 10){
		pX2 = pX1 + 10;
		offX=0
	}
	if(pY2 - pY1 < 10){
		pY2 = pY1 + 10;
		//offY=0
	}
	//OVMap
	ovImage = document.getElementById("imgOVMap");
	var ovl = getAbsPos(ovImage,"Left");
	var ovt =getAbsPos(ovImage,"Top");
	
	if (pX2-pX1>100){
		
		hideLayer("divLocBox");
		return;
	} else {
		
		moveLayer("divLocBox",ovl+offX,ovt-offY);
		clipLayer("divLocBox", pX1, pY1, pX2, pY2);
		showLayer("divLocBox");
	}	
	
	//alert(pX1 + ", " + pY1 + " : " + pX2 + ", " + pY2);
}



// set zoom box width	
function setZoomBoxWidth(size) {
	zoomBoxWidth = size;
}


// show zoom box
function showZoomBox() {
	
	showLayer(docID+"_zoomBox");
	//showLayer(docID+"_zoomBoxLeft");
	//showLayer(docID+"_zoomBoxRight");
	//showLayer(docID+"_zoomBoxBottom");
}


// start pan.... image will move
function startPan(e) {

	//moveLayer(docID+"_theMap",hspc,vspc);
	window.scrollTo(0,0);
	getImageXY(e);
	// keep it within the MapImage
	if ((mouseX<iWidth) && (mouseY<iHeight)) {
		if (panning) {
			stopPan(e);
		} else {
			x1=mouseX;
			y1=mouseY
			x2=x1+1;
			y2=y1+1;
			panning=true;
		}
	}
	return false;

}

// resize layer to wd,ht
function resizeLayer(name, wd, ht) {
	var layer = getLayer(name);		
	if (layer != null) {
	  	layer.width = wd + "px";
   		layer.height  = ht + "px";
	}
}

// start zoom in.... box displayed
function startZoomBox(e) {

	getImageXY(e);	

	if (!allowRubberband) {
		stopZoomBox(e);
	} else {	
		// keep it within the MapImage
		if ((mouseX<iWidth) && (mouseY<iHeight-bottomBorderHeight)) {
			if (!zooming) {
				x1=mouseX;
				y1=mouseY;
				x2=x1+1;
				y2=y1+1;
				zooming=true;
				moveLayer(docID+"_zoomBox",x1+hspc,(y1)+vspc);
				resizeLayer(docID+"_zoomBox", 1, 1);
				//clipLayer(docID+"_zoomBox",x1,y1,1,1);
				//clipLayer(docID+"_zoomBoxLeft",x1,y1,x2,y2);
				//clipLayer(docID+"_zoomBoxRight",x1,y1,x2,y2);
				//clipLayer(docID+"_zoomBoxBottom",x1,y1,x2,y2);
				showZoomBox();
			}
		} else {
			if (zooming) {
				stopZoomBox(e);
			}
		}
	}
	return false;	
}


// stop moving image.... pan 
function stopPan(e) {

	if ((Math.abs(x2-x1) < 2) && (Math.abs(y2-y1) < 2)) {
		// the move is too small
		recenter(e);
	} else  {
		window.scrollTo(0,0);
		panning=false;
		var width = Math.abs(maxx - minx);
		var height = Math.abs(maxy - miny);
		var tempLeft=minx;
		var tempRight=maxx;
		var tempTop=maxy;
		var tempBottom=miny;
		var ixOffset = x2-x1;
		var iyOffset = y1-y2;
		pixelX = width / iWidth;
		var theY = iHeight - zmaxy;
		pixelY = height / iHeight;
		var xOffset = pixelX * ixOffset;
		var yOffset = pixelY * iyOffset;
		newMaxy = maxy - yOffset;
		newMaxx = maxx - xOffset;
		newMinx = minx - xOffset;
		newMiny = miny - yOffset;

		// AP specific setting
		attributeUpdate = false;
		
		refreshMap();
	}
		
	return true;
	
}


// stop zoom box display... zoom in
function stopZoomBox(e) {

	zooming=false;
	var width = Math.abs(maxx - minx);
	var height = Math.abs(maxy - miny);
	var pixelX = width / iWidth;
	var theY = iHeight - zmaxy;
	var pixelY = height / iHeight;
	
	newMaxy = pixelY * theY + miny;
	newMaxx = pixelX * zmaxx + minx;
	newMinx = pixelX * zminx + minx;
	theY = iHeight - zminy;
	pixelY = height / iHeight;
	newMiny = pixelY * theY + miny;

	if ((zmaxx <zminx+2) && (zmaxy < zminy+2)) {
		// if the zoom box is too small
		// recenter(e);
		if (curTool == "ZOOMOUT") {
			newMinx = newMinx - width;
			newMiny = newMiny - height;
			newMaxx = newMaxx + width;
			newMaxy = newMaxy + height;
		} else {
			newMinx = newMinx - width / 4;
			newMiny = newMiny - height / 4;
			newMaxx = newMaxx + width / 4;
			newMaxy = newMaxy + height / 4;
		}
		refreshMap();

	} else {
		
		if (mapBoxAsZoom) {
			if (curTool == "ZOOMOUT") {
				percentX = (maxx-minx)/(newMaxx-newMinx);
				percentY = (maxy-miny)/(newMaxy-newMiny);
				percent = (percentX+percentY)/2;
				
				widthH = (maxx-minx)/2;
				heightH = (maxy-miny)/2;
				cx = newMinx + widthH;
				cy = newMiny + heightH;
				
				newMinx = cx - percent * widthH;
				newMiny = cy - percent * heightH;
				newMaxx = cx + percent * widthH;
				newMaxy = cy + percent * heightH;
			}
			refreshMap();
		} else
			customMapBox(newMinx, newMiny, newMaxx, newMaxy);
			
	}
	return true;
}

// recenter map is the default option
function recenter(e) {

	// otherwise we don't have these layers
	if (allowRubberband)
		hideZoomBox();
		
	getMapXY(mouseX,mouseY);
	if (mapClickAsRecenter) {
		var widthHalf = Math.abs(maxx - minx) / 2;
		var heightHalf = Math.abs(maxy - miny) / 2;
		newMinx = mapX - widthHalf;
		newMaxx = mapX + widthHalf;
		newMaxy = mapY + heightHalf;
		newMiny = mapY - heightHalf;

		refreshMap(); 
	} else
		customMapClick(mapX,mapY);
}

//-------- LAYER SUPPORT FUNCTIONS --------------


// clip layer display to clipleft, cliptip, clipright, clipbottom
function clipLayer(name, clipleft, cliptop, clipright, clipbottom) {		
	var layer = getLayer(name);
		
	if (layer != null) {
  		if (isNav4) {
  			
			layer.clip.left   = clipleft+p;
			layer.clip.top    = cliptop;
		     layer.clip.right  = clipright;
			layer.clip.bottom = clipbottom;
		} else {
			
			layer.clip = 'rect(' + cliptop + 'px ' +  clipright + 'px ' + clipbottom + 'px ' + clipleft +'px)';
		}
	}
}


// Create a DHTML layer
function createLayer(name, left, top, width, height, visible, content) {
	  var layer;
	  
	  if (isNav4) {
	    document.writeln('<layer name="' + name + '" left=' + left + ' top=' + top + ' width=' + width + ' height=' + height +  ' visibility=' + (visible ? '"show"' : '"hide"') +  '>');
	    document.writeln(content);
	    document.writeln('</layer>');
	    layer = getLayer(name);
	    layer.width = width;
	    layer.height = height;
	  } else {
	    document.writeln('<div id="' + name + '" style="position:absolute; overflow:none; left:' + left + 'px; top:' + top + 'px; width:' + width + 'px; height:' + height + 'px;' + ' visibility:' + (visible ? 'visible;' : 'hidden;') +  '">');
	    document.writeln(content);
	    document.writeln('</div>');
	  }
	  	//  clipLayer(name, 0, 0, width, height);
}

// get the layer object called "name"
function getLayer(name) {
	 //alert(name);
	 
	  var theObj = document.getElementById(name);
		return theObj.style
	  
	  if (isNav4)
	    return(document.layers[name]);
	  else if (isIE4) {

	  	if ( eval('document.all.' + name) != null) {
		    layer = eval('document.all.' + name + '.style');
		    return(layer);
		} else
			return(null);
	  } else if (is5up) {
	  //alert(name);
	 
		var theObj = document.getElementById(name);
		return theObj.style
	  } else
	    return(null);
}

// toggle layer to invisible
function hideLayer(name) {	
	
  	var layer = getLayer(name);		
  	
	if (layer != null) {
	 	if (isNav4)
    		layer.visibility = "hide";
		else
   		 layer.visibility = "hidden";
	}
}

// move layer to x,y
function moveLayer(name, x, y) {
	var layer = getLayer(name);		
	
	if (layer != null) {
	  	if (isNav4)
    		layer.moveTo(x, y);
	 	else if (isIE) {
    		layer.left = x + "px";
   			layer.top  = y + "px";
		} else {
			//layer.height = iHeight - y;
			//layer.width	= iWidth - x;
    		layer.left = x + "px";
   			layer.top  = y + "px";
	  	}
	}
}

// replace layer's content with new content
function replaceLayerContent(name, content) {
	  if (isNav4) {
		    var layer = getLayer(name);
			if (layer != null) {
			    layer.document.open();
			    layer.document.writeln(content);
			    layer.document.close();
			}
	  }  else if (isIE) {
			if (eval("document.all." + name) != null) {
		  		content = content.replace(/\'/g,"\\'");
			    var str = "document.all." + name + ".innerHTML = '" + content + "'";
			    eval(str);
			}
	  }
}

// set layer background color
function setLayerBackgroundColor(name, color) {		
  	var layer = getLayer(name);		
	if (layer != null) {
	    if (isNav4) 
    		layer.bgColor = color;
		else 
    		layer.backgroundColor = color;
	}
}

// toggle layer to visible
function showLayer(name) {		
  	var layer = getLayer(name);		
  	
	if (layer != null) {
	  	if (isNav4)
    		layer.visibility = "show";
		else
   		 	layer.visibility = "visible";
	}
}


// set Tool Image
function setToolImage(dtool) {

	var wid = document.getElementById("tool"+dtool);
	
	var otherWids = document.getElementsByTagName("td");
	
	for (i=0;i<otherWids.length;i++){
		currWid = otherWids[i];
		if (currWid.id.indexOf("tool")==0)
			currWid.style.border="none";
			
	}
	
	wid.style.border="thin inset red";
}

// get the true offset of anything on NS4, IE4/5 &amp; NS6, even if it's in a table!
function getAbsX(elt) { return (elt.x) ? elt.x : getAbsPos(elt,"Left"); }
function getAbsY(elt) { return (elt.y) ? elt.y : getAbsPos(elt,"Top"); }
function getAbsPos(elt,which) {
 iPos = 0;
 while (elt != null) {
  iPos += elt["offset" + which];
  elt = elt.offsetParent;
 }
 return iPos;
}

function startUp(thisID) {
			docID= thisID;
			var mapLayer;
			
			mapImage = document.getElementById(docID+"_imgMap");
			var l = getAbsPos(mapImage,"Left");
			var t =getAbsPos(mapImage,"Top");
			hspc=l;
			vspc=t;
			moveLayer(docID+"_theTop",l,t);
			moveLayer(docID+"_zoomBox",l,t);
			
			var lLoad, tLoad;
			lLoad = l+(iWidth/2)-175;
			tLoad = t+(iHeight/2)-36.5;
			
			moveLayer(docID+"_loadLayer",lLoad,tLoad);
			showLayer(docID+"_loadLayer");
			
			//Get the identifiable layers
			
			var active = document.getElementById("ddlActive");
			
			for (i=0;i<active.length;i++)
			{
				
				arIdentifyLayers[i]=active.options[i].text+":"+active.options[i].value;
			}
		
			mapLayer=document.getElementById(docID+"_theTop");
			
			if (isNav4) {
				document.captureEvents(Event.MOUSEMOVE);
				document.captureEvents(Event.MOUSEDOWN);
				document.captureEvents(Event.MOUSEUP);
			
			} else {	
				mapLayer.onmousedown = mapTool;
				mapLayer.onmouseup = chkMouseUp;
				mapLayer.onmousemove = getMouse;
				
				mapLayer.cursor = "crosshair";
			}
			
			
			
			var frm = document.forms[0];
			
			toolObject = document.getElementById(docID+"_tool");
			cmdObject = document.getElementById(docID+"_cmd");
			cmd=cmdObject.value;
			tool = toolObject.value;
			if (tool=="")
				tool="ZOOMIN";
			setTool(tool);
			
			cmdObject = document.getElementById(docID+"_cmd");
			if (tool.length==0)
				tool="ZOOMIN";
			
				
			
			maxx = parseFloat(document.getElementById(docID+"_maxx").value);
			minx = parseFloat(document.getElementById(docID+"_minx").value);
			maxy = parseFloat(document.getElementById(docID+"_maxy").value);
			miny = parseFloat(document.getElementById(docID+"_miny").value);		
			
			hideLayer(docID+"_loadLayer");
			setLocBox();	
		
}

function setLayerVis(str){

	document.forms[0].strLayerVis.value=str;
}
function applyLayers() {
	var frm =document.forms[0];
	var layerVis=""; 
	var numLayers = parseInt(frm.mCount.value);
	var curCheck;

	for (i=1;i<=numLayers ;i++) {
		curCheck = eval("document.forms[0].Layer"+i+".checked");

		if (curCheck) {
			layerVis += "1";
		} else {
			layerVis += "0";
		}		
		
	}

	frm.strLayerVis.value=layerVis;
	sendMap();
	
}
//toggle layer legend
function toggleLegend(){
	var leg = getLayer("legendLayer");
	//var lay = getLayer("divLayers");
	
	if (leg.display=="none"){
		//lay.display="none";
		leg.display="inline";
	} else{
		//leg.display="none";
		lay.display="inline";
	}
	
}

//Show User Area
function showSection(secName){
	getLayer(secName).display="inline";
	

}

//Remove User Area
function removeSection(secName) {
	getLayer(secName).display="none";
	
}




//Bookmarks
function doBookmark() {
	var frm = document.forms[0];
	var newWin;
	var options = "height=400 width=450 resizable=yes menubar=no toolbar=no statusbar=no";
	var qstring,url;
	qstring="xmin="+minx+"&xmax="+maxx+"&ymin="+miny+"&ymax="+maxy;
	url = "support/aspx/bookMarks.aspx?"+qstring;
	window.open(url,"BookMarks",options);

}

//Polys
function doPoly(mX, mY){
	
	var d = document.getElementById("coordList");
	var strCoord="<ul>";
	var strDiv;
	var strInput="";
	xArr[xArr.length] = mX;
	yArr[yArr.length] = mY;
	var xDis, yDis;
	for (var i=0;i<xArr.length;i++){
		if (i>0 )
			strInput+=";";
		strCoord+="<li>X: ";
		strCoord+=xArr[i];
		strInput+=xArr[i];
		strCoord+=" Y: ";
		strCoord+=yArr[i];
		strInput+=",";
		strInput+=yArr[i];
		strCoord+="</li>";
		
	}
	strDiv = "<div style='position:absolute;top:";
		yDis = iHeight - mouseY;
		strDiv += yDis;
		strDiv +=";left:";
		xDis = mouseX;
		strDiv+=xDis;
		strDiv +=";width:5;height:5;background:red'>";
		strDiv+=xArr.length+"</div>";
		
	strCoord+="</ul>";
	
	d.innerHTML = strCoord;
	var bd=document.getElementById(docID+"_theMap");
	document.forms[0].polyString.value=strInput;
	bd.innerHTML+=strDiv;
}


//Show element
function showElement(el){

	document.getElementById(el).style.display="inline";
}

//Hide element
function hideElement(el){
	
	document.getElementById(el).style.display="none";
}

// set tool
function setTool(newTool) {
	//alert(document.forms[0].tool.value);
	curTool = newTool;
    state = "REFRESH";
    hideElement("divActive");
    hideElement("divBuffer");
   
    var wid = document.getElementById("ddlActive");
    var widsel = wid.selectedIndex;
    var currTxtt,currVal, currPos;
    
    //Reset active layer list
    if (wid.options.length<3){
		for (i=0;i<arIdentifyLayers.length;i++){
			
			currPos=arIdentifyLayers[i].indexOf(":");
			currTxt = arIdentifyLayers[i].substring(0,currPos);
			currVal = arIdentifyLayers[i].substring(currPos+1);
			wid.options[i]=new Option(currTxt,currVal);
			
		}
    }
   
    if (curTool == "ZOOMIN") {
		mapBoxAsZoom = true;
		//setZoomBoxColor("#ff0000");
		//setZoomBoxWidth(1);
	} else if (curTool == "ZOOMOUT") {
		mapBoxAsZoom = true;
		//setZoomBoxColor("#ff0000");
		//setZoomBoxWidth(1);
	} else if (curTool == "PAN") {
	} else if (curTool == "SELECT") {
		
			showElement("divBuffer");
			showElement("divActive");
			//Remove last value for Select
			wid.options[2]=null;
			wid.options[2] = null;
			
			if (document.getElementById('divRESULTS_mainDivVis')!= null)
				document.getElementById('divRESULTS_mainDivVis').value="inline";
	
		
			if (widsel<wid.options.length)
				wid.selectedIndex=widsel;

			
	} else if (curTool == "IDENTIFY") {
		showElement("divActive");
		
		 if (document.getElementById('divRESULTS_mainDivVis')!= null)
			document.getElementById('divRESULTS_mainDivVis').value="inline";
		if (widsel<wid.options.length)
			wid.selectedIndex=widsel;
	} else if (curTool == "ADDPOLY") {
		showSection("poly");
	}
	setToolImage(curTool);
	toolObject.value = curTool;
	cmdObject.value = state;
}

// set cmd
function setCmd(newCmd) {
		
    	state = newCmd;
    	cmdObject.value = state;
	sendMap();
}

// set Tool Image
function setToolImage(dtool) {
	
	var wid = document.getElementById("tool"+dtool);
	
	var otherWids = document.getElementsByTagName("div");
	
	for (i=0;i<otherWids.length;i++){
		currWid = otherWids[i];
		
		if (currWid.id.indexOf("tool")==0)
			currWid.className="tool"
			
	}
	
	wid.className="activeTool";
}

function toolOver(wid){
	if (wid.id=="tool"+curTool)
		return;
	wid.className="hoverTool";
}

function toolOut(wid){
	
	if (wid.id=="tool"+curTool)
		return;
	wid.className="tool";
}

function showLoad(){
	showLayer(docID+"_loadLayer");
}