//==========================================================================
//Program name   ACTIVE.JS
//Original Programmers    Kris Estes/Terri Nelson
//Modified By    Brian Bond
//Date Created   2001?
//Last Updated   6-2-2008
//
//REVISIONS: 
// 5-27-04
// 7-23-04 = Added window status showing mouse location in state plane coordinates.
// 8-30-04 = Added Measure Tool Code Successfully
// 6-2-2008 =
//========================================================================== 
var newMinx = 0;
var newMiny = 0;
var newMaxx = 0;
var newMaxy = 0;
var mapClickAsRecenter = true;
var mapBoxAsZoom = true;
var allowRubberband = true;
var state = "ZoomIn";
// 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 = 2;
//SEARCHLITE var state = "zoom"; // "pan"
var zooming=false;
var panning=false;
var bottomBorderHeight = 13;
var hspc;
var vspc;
var measuring=false;
var getarea=false;
//These variables are used by the measure and area tools
var endx=0;
var endy=0;
var newX=0;
var newY=0;
var numendpoints=0;
var numpoints=0;
var totalpoints = 800;
//stores how many points go into each segment.
var numinseg=10;
//these varaibles are for the measure tool...
var line = new Array();
var dist;
//these varaibles are for the area tool...
var polygon = new Array();
var startx=0;
var starty=0;
var startover = false;
// setup test for Nav 4.0
var isIE = false;
var isNav = (navigator.appName.indexOf("Netscape")>=0);
var isNav4 = false;
var isIE4 = false;
var isIE5up = false;
var isIE6up = false;
if (isNav) {
	if (parseFloat(navigator.appVersion)<5) {
		isNav4=true;
	}
} else {
	isIE4=true;
	isIE=true;
	isIE6up=true;
	var ua = window.navigator.userAgent
	var msie = ua.indexOf ( "MSIE " )
	var resultx
	if ( msie > 0 )      // is Microsoft Internet Explorer; return version number
	   resultx = parseInt ( ua.substring ( msie+5, ua.indexOf ( ".", msie ) ) )
	if (resultx>4) {
		//browser is "MSIE 5"
		isIE4 = false;
		isIE5up = true;
		isIE6up = true;
	} else if (resultx<6) {
		//browser is "MSIE 6 or 7"
		isIE4 = false;
		isIE5up = false;
		isIE6up = true;
	}
}	
		
function isBrowser2() {
		//Platform 
		var Mac_OS = (navigator.userAgent.indexOf("mac") != -1) || (navigator.userAgent.indexOf("Mac") != -1); 
		var Linux_OS = (navigator.userAgent.indexOf("Linux") != -1); 
		var Win_OS = (navigator.userAgent.indexOf("Win") != -1); 
		//Browsers 
		var Nav4 = "false";
		var Opera = (navigator.userAgent.indexOf('Opera') != -1); 
		var MSIE = (navigator.userAgent.indexOf('MSIE') != -1); 
		var Moz = (navigator.userAgent.indexOf('Gecko') != -1); 
		var Safari = (navigator.userAgent.indexOf('Safari') != -1); 
		var NS = (navigator.userAgent.indexOf('Netscape') != -1 && navigator.userAgent.indexOf('Gecko') != -1); 
		if(document.layers){var Nav4="true";}; 
		var Konqueror = (navigator.userAgent.indexOf('Konqueror') != -1); 
		if(document.layers){var Nav4="true";}; 
		var Konqueror = (navigator.userAgent.indexOf('Konqueror') != -1);
		//Check browser if it is Internet Explorer in SP2. 
		g_fIsSP2 = (window.navigator.userAgent.indexOf("SV1") != -1);
		//Opera workaround - default install of opera identifies as both opera and msie 
		if (Opera && MSIE || Opera){var Opera="true"; MSIE="false";} 
		if (MSIE && !Opera){MSIE="true";}
		var browserversion="0";
		if (navigator.appVersion.indexOf("2.")!=-1) {browserversion="2"};
		if (navigator.appVersion.indexOf("3.")!=-1) {browserversion="3"};
		if (navigator.appVersion.indexOf("4.")!=-1) {browserversion="4"};
		if (navigator.appVersion.indexOf("5.")!=-1) {browserversion="5"};
		if (navigator.appVersion.indexOf("6.")!=-1) {browserversion="6"};
		if (navigator.appVersion.indexOf("76.")!=-1) {browserversion="7"};
}

function isBrowser() {
	isBrowser2();
	if (!MSIE)
		alert("It appears that you are NOT using an Internet Explorer or like browser.  Certain functions in this application may not work as well with your web browser.")
}
	
function popup() {
	 remote =  window.open("splashscreen.html","Splash","top=100,left=100,width=200,height=150,resizable=no");
	   if (remote.opener == null) remote.opener = window;
	 remote.opener.name = "opener";
	 return(true);
}
	
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);
	}
	if (measuring || getarea) {
		if (document.getElementById){
			if (document.getElementById('mapcontrol').tool.value == "measure"){
			line = AddtoLine(line, numendpoints - 1, mapX, mapY)
			} else {
				polygon = AddtoPoly(polygon, numendpoints - 1, mapX, mapY);
			}
			if (numendpoints >1)
				updateTotal(document.getElementById('mapcontrol').tool.value);
		}else{
			if (document.mapcontrol.tool.value == "measure"){
				line = AddtoLine(line, numendpoints - 1, mapX, mapY);
			} else {
				polygon = AddtoPoly(polygon, numendpoints - 1, mapX, mapY)
			}
			if (numendpoints >1)
				updateTotal(document.mapcontrol.tool.value);
		}
	}
}

function customMapBox(_newMinx, _newMiny, _newMaxx, _newMaxy) {
}

function customMapClick(_newX, _newY) {
}

function cancelRequest(){
	opener.document.loadmap.src = "images/loadMap_.gif"
	window.close();
}

function closeWindow(){
	if (isIE=true) {
		win = top;
    	// trick IE so you don't get the prompt to close 
    	win.opener = top;
    	win.close();
	}else{
    window.close();return false;
	}
}

function getImageXY(e) {
	if (isNav) {
		mouseX=e.pageX;
		mouseY=e.pageY;
	} else {
		mouseX=event.clientX + document.body.scrollLeft;
		mouseY=event.clientY + document.body.scrollTop;
	}
	mouseX = mouseX-hspc;
	mouseY = mouseY-vspc;
	pixelX = (maxx-minx) / iWidth;
	mapX = pixelX * mouseX + minx;
	var theY = iHeight - mouseY;
	pixelY = (maxy-miny) / iHeight;
	mapY = pixelY * theY + miny;
	
	var mouseString;
    if (mouseX<0 && mouseY<0 || mouseX>iWidth && mouseY>iHeight-bottomBorderHeight || mouseX<0 && mouseY<iWidth || mouseX>iWidth && mouseY<iWidth || mouseX>0 && mouseY>iWidth || mouseX<0 && mouseY>iWidth || mouseX<iWidth && mouseY<0) {
	} else {
		var u = Math.pow(10,4);
		var uX = parseInt(mapX * u + 0.5) / u
		var uY= parseInt(mapY * u + 0.5) / u
		mouseString = "State Plane: X " + uX + " , Y "  + uY + "";
		window.status = mouseString;
	}		
	
}	

// 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;
}

// get the coords at mouse position
function getMouse(e) {
	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;
		setClip();
		return false;
	} else if (panning) {
		x2=mouseX;
		y2=mouseY;
		panMouse();	
		return false;
	} else if (measuring){
		measureMove("measure");
	} else if (getarea){
		measureMove("area");
	} else 
    	return true;
}

function hideZoomBox() {
	hideLayer("zoomBoxTop");
	hideLayer("zoomBoxLeft");
	hideLayer("zoomBoxRight");
	hideLayer("zoomBoxBottom");
}

// perform appropriate action with mapTool
function mapTool (e) {
	if (document.getElementById){
		state = document.getElementById('mapcontrol').action.value;
		state2 = document.getElementById('mapcontrol').task.value;
	}else if (document.layers) {
		state = document.mapcontrol.action.value;
		state2 = document.mapcontrol.task.value;
	}else if (document.all) {
		state = document.all.mapcontrol.action.value;
		state2 = document.all.mapcontrol.task.value;
	}
	getImageXY(e);
	if (startover == true){
		if (document.getElementById){
			resetMeasure(document.getElementById('mapcontrol').tool.value);
		}else if (document.layers) {
			resetMeasure(document.mapcontrol.tool.value);
		}else if (document.all) {
			resetMeasure(document.all.mapcontrol.tool.value);
		}	
	}
	  // "identify" doesn't require a mouse up event. 	
	if ((state == "Identify" || state == "LatLong" || state == "Quarter") && 
	    (mouseX >= 0) && (mouseX <= iWidth) && 
	    (mouseY >= 0) && (mouseY <= iHeight)){
			getMapXY(mouseX,mouseY);
			document.loadmap.src="images/loadMap.gif";
            document.mapcontrol.MapX.value = mapX.toString()						
            document.mapcontrol.MapY.value = mapY.toString()
            document.mapcontrol.submit()
			return false; // <-- false assures that the popup
			              //     box stays in front.
	}
	if (document.mapcontrol.tool.value == "measure" || state == "measure" || document.mapcontrol.tool.value == "area" || state == "area"){
		getImageXY(e);
		if (startover == true){
			if (document.getElementById){
				resetMeasure(document.getElementById('mapcontrol').tool.value);
			}else if (document.layers) {
				resetMeasure(document.mapcontrol.tool.value);
			}else if (document.all) {
				resetMeasure(document.all.mapcontrol.tool.value);
			}
		}
		if ((mouseX<iWidth) && (mouseY<iHeight) && (mouseX>0) && (mouseY>0)){
			//this is the simulation of a double click...
			if ((Math.abs(endx - (mouseX + 15)) <= 5) && (Math.abs(endy - (mouseY + 15)) <= 5)){
			//if ((Math.abs((endx+5) - mouseX) <= 10) && (Math.abs((endy+10) - mouseY) <= 10))
				getarea=false;
				measuring=false;
				startover=true;
				return;
			}
			//adjust the moving yellow end point so that it fits away from under the mouse...
			endx=mouseX - 5;
			endy=mouseY - 10;
			hideLayer("MeasureInst1");
			showLayer("MeasureInst2");
			//if run out of points (only set amount created), then reset line
			if (numendpoints == (totalpoints/numinseg)){
				numendpoints = 0;
				numpoints = 0;
			}
			showLayer("EndPoint"+(numendpoints + 1))
			moveLayer("EndPoint"+(numendpoints + 1),endx+hspc,endy+vspc);
			numendpoints = numendpoints + 1;
			//if startX is equal to zero, this is the first point, so set the starts so that they're remembered.
			if (startx == 0){
				startx = endx;
				starty = endy;
			}
			//here is when the area or length calculation is made.
			//increment the value of the item variable to reflect a point being added.
			getMapXY(mouseX,mouseY)
	
			if (document.mapcontrol.tool.value == "measure"){
				line = AddtoLine(line, numendpoints - 1, mapX, mapY)
			}else{
				polygon = AddtoPoly(polygon, numendpoints - 1, mapX, mapY)
			}
			if (numendpoints >1){
				updateTotal(document.mapcontrol.tool.value);
			}
			if (document.getElementById){
				startMeasureTools(e, document.getElementById('mapcontrol').tool.value);
			}else if (document.layers) {
				startMeasureTools(e, document.mapcontrol.tool.value);
			}else if (document.all) {
				startMeasureTools(e, document.all.mapcontrol.tool.value);
			}		
		}
	}
	// If we made it to here, it's a 
	// navigational click and not an
	// identify.
	if ((!zooming) && (!panning) && (!measuring) && (!getarea) && 
		(mouseX >= 0) && (mouseX <= iWidth) && 
		(mouseY >= 0) && (mouseY <= iHeight)) {
		if (state == "Pan"){
			document.loadmap.src='images/loadPanning.gif';
			startPan(e);
		}else{ 
		    document.loadmap.src='images/loadZooming.gif';
			startZoomBox(e);
		}
		return false;
	} else if (zooming) {
		getMouse(e);
		stopZoomBox(e);
	} else if (panning) {
		getMouse(e);
		stopPan(e);
	} else if (measuring) {
		getMouse(e);
	} else if (getarea) {
		getMouse(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("theMap",cLeft,cTop,cRight,cBottom);
	moveLayer("theMap",xMove+hspc,yMove+vspc);
	return false;
}

// recenter map is the default option
function recenter(e) {
	// otherwise we don't have these layers
	if (allowRubberband)
		hideZoomBox();	
	getMapXY(mouseX,mouseY);
	if (mapClickAsRecenter) {
		if (document.getElementById){
			document.getElementById('loadmap').src="images/loadData.gif";
			document.getElementById('mapcontrol').MapX.value = mapX;					
	        document.getElementById('mapcontrol').MapY.value = mapY;				   
	        document.getElementById('mapcontrol').submit();	
		}else if (document.layers) {
			document.loadmap.src="images/loadData.gif";
			document.mapcontrol.MapX.value = mapX;					
	        document.mapcontrol.MapY.value = mapY;			   
	        document.mapcontrol.submit();
     	}
	} else
		customMapClick(mapX,mapY);
}

//DOM COMPLIANT
function refreshAction() {
  hideZoomBox();
  if (document.getElementById){
	  document.getElementById('loadmap').src="images/loadData.gif"; 							
	  document.getElementById('mapcontrol').MExtMinX.value = newMinx.toString();						
	  document.getElementById('mapcontrol').MExtMinY.value = newMiny.toString();					
	  document.getElementById('mapcontrol').MExtMaxX.value = newMaxx.toString();
	  document.getElementById('mapcontrol').MExtMaxY.value = newMaxy.toString();
	  document.getElementById('mapcontrol').submit();
	}else if (document.layers) {
	  document.loadmap.src="images/loadData.gif"; 							
	  document.mapcontrol.MExtMinX.value = newMinx.toString();						
	  document.mapcontrol.MExtMinY.value = newMiny.toString();					
	  document.mapcontrol.MExtMaxX.value = newMaxx.toString();
	  document.mapcontrol.MExtMaxY.value = newMaxy.toString();
	  document.mapcontrol.submit();
  }
}


function sendIdentify(numMapX,numMapY) {
var objWin;
var sURL;
//note:
//to have the ID layer object highlighted, use the following function
//this will allow data to be written to right side of page and a map redraw
//opted not to use since this would slow client down waiting for image to download
  sURL = sIDProcessor+"?"+
  		 "X="+numMapX.toString()+"&"+
  		 "Y="+numMapY.toString();
		 		 
  objWin = window.open(sURL, "Identify", "height=480,width=240,scrollbars=yes,resizeable=yes");
  if (parseInt(navigator.appVersion) >=4) objWin.window.focus();
}


// Identify feature and then draw it
function refreshMap_ID(numMapX,numMapY) {
  hideZoomBox();
  document.loadmap.src="images/loadData.gif";               
  if (document.imageVis.imagery.checked){
  document.location = sMapProcessor+"?Cmd="+state+
                      "&imagery=on&MinX="+newMinx.toString()+
                      "&MinY="+newMiny.toString()+
                      "&MaxX="+newMaxx.toString()+
                      "&MaxY="+newMaxy.toString()+
                      "&X="+numMapX.toString()+
                      "&Y="+numMapY.toString()
  }else{
  document.location = sMapProcessor+"?Cmd="+state+
                      "&imagery=off&MinX="+newMinx.toString()+
                      "&MinY="+newMiny.toString()+
                      "&MaxX="+newMaxx.toString()+
                      "&MaxY="+newMaxy.toString()+
                      "&X="+numMapX.toString()+
                      "&Y="+numMapY.toString() 
                      }                               
}


// 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)) {
		clipLayer("zoomBoxTop",zminx,zmaxy,zmaxx,zmaxy+zoomBoxWidth);
		clipLayer("zoomBoxLeft",zminx,zmaxy,zminx+zoomBoxWidth,zminy);
		clipLayer("zoomBoxRight",zmaxx-zoomBoxWidth,zmaxy,zmaxx,zminy);
		clipLayer("zoomBoxBottom",zminx,zminy-zoomBoxWidth,zmaxx,zminy);
	}
}

//DUPLICATE FUNCTION NAME FOUND ALSO IN SETEXTENT.INC
function setExtent(_minx,_miny,_maxx,_maxy) {
	minx = _minx;
	miny = _miny;
	maxx = _maxx;
	maxy = _maxy;
}


function setZoomBoxColor(color) {
	setLayerBackgroundColor("zoomBoxTop", color);
	setLayerBackgroundColor("zoomBoxLeft", color);
	setLayerBackgroundColor("zoomBoxRight", color);
	setLayerBackgroundColor("zoomBoxBottom", color);
}


function setZoomBoxSettings() {
state = "Zoom"
    if (state == "Pan") {
		if (document.getElementById){   // DOM3 = IE5, NS6
				document.getElementById('theTop').style.cursor = "move";
				document.getElementById('theMap').style.cursor = "move";
		}else if (document.layers){   // Netscape 4
				document.theTop.style.cursor = "move";
				document.theMap.style.cursor = "move";
		}else {  // IE 4
				document.all.theTop.style.cursor = "move";
				document.all.theMap.style.cursor = "move";
		}
	} else {
			if (document.getElementById){   // DOM3 = IE5, NS6
				document.getElementById('theTop').style.cursor = "crosshair";
				document.getElementById('theMap').style.cursor = "crosshair";
		}else 	if (document.layers) {  // Netscape 4
				document.theTop.style.cursor = "crosshair";
				document.theMap.style.cursor = "crosshair";
		}else  { // IE 4
				document.all.theTop.style.cursor = "crosshair";
				document.all.theMap.style.cursor = "crosshair";
		}
		if (isNav4) {
			getLayer("theTop").onmousemove = getMouse;
			getLayer("theTop").onmousedown = mapTool;
			getLayer("theTop").onmouseup = chkMouseUp;
		} else {
			document.onmousemove = getMouse;
			document.onmousedown = mapTool;
			document.onmouseup = chkMouseUp;
		}
	}
}
	
function setZoomBoxWidth(size) {
	zoomBoxWidth = size;
}

function showZoomBox() {
	showLayer("zoomBoxTop");
	showLayer("zoomBoxLeft");
	showLayer("zoomBoxRight");
	showLayer("zoomBoxBottom");
}

// start pan.... image will move
function startPan(e) {
	moveLayer("theMap",hspc,vspc);
	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;
}

// 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;
				clipLayer("zoomBoxTop",x1,y1,x2,y2);
				clipLayer("zoomBoxLeft",x1,y1,x2,y2);
				clipLayer("zoomBoxRight",x1,y1,x2,y2);
				clipLayer("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;
		refreshAction();
	}	
	return true;
	
}

// stop zoom box display... zoom in
function stopZoomBox(e) {
	zooming=false;
	if ((zmaxx <zminx+2) && (zmaxy < zminy+2)) {
		// if the zoom box is too small
		recenter(e);
	} else {
		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 (mapBoxAsZoom) {
			if (state == "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;
			}
			refreshAction();
		} else
			customMapBox(newMinx, newMiny, newMaxx, newMaxy);
	}
	return true;
}

//start measure or area.  Set of point layers will follow the cursor
function startMeasureTools(e, tool){
	var i;
	getImageXY(e);
	if ((mouseX<iWidth) && (mouseY<iHeight)) {
		if (tool == "measure"){
			measuring=true;
		}else{
			getarea=true;
		}
		//envoke the measure points dynamically.
		for(i=(numpoints + 1); i <= (numinseg + numpoints); i++){
			showLayer("MeasurePoint"+i);
			moveLayer("MeasurePoint"+i,endx,endy);
		}
		numpoints = numpoints + numinseg;
		//create the additional drag line for area from the end of the layer list.
		//if (tool == "area"){
		//	for(i=totalpoints-numinseg; i < totalpoints; i++){
		//	showLayer("MeasurePoint"+i);
		//	moveLayer("MeasurePoint"+i,endx,endy);
		//	}
		//}
	}
	return false;
}
function stopMeasure(e){
	getImageXY(e);
	if ((mouseX<iWidth) && (mouseY<iHeight)) {
		measuring=false;
	}
	return false;
}

//DOM COMPLIANT
//called by the mouse move event if the tool is measure or area.
function measureMove(tool){
	var m=1;
	if ((mouseX<iWidth) && (mouseY<iHeight) && (mouseX>0) && (mouseY>0)){
		newX = mouseX - 5;
		newY = mouseY - 10;
		//The first point moves with the mouse.  The other 9 points move in between the first point and the mouse.
		for(i=((numpoints - numinseg) + 1); i <= numpoints; i++){
			moveLayer("MeasurePoint"+i, endx + ((newX - endx)*(m/numinseg))+hspc, endy + ((newY - endy)*(m/numinseg))+vspc)
			m++
		}	
		//if this is the area tool, also move the end of the layer list with the points.
		if (tool == "area"){
			m=1;
			for(i=totalpoints-numinseg; i < totalpoints; i++){
				moveLayer("MeasurePoint"+i,startx + ((newX - startx)*(m/numinseg)), starty + ((newY - starty)*(m/numinseg)))
				m++
			}
		}
		getMapXY(newX,newY);
		if (tool == "measure"){
			line = AddtoLine(line,numendpoints, mapX, mapY);
			if (document.getElementById){ 
				document.getElementById('measureparams').SegLength.value = Math.abs( roundTo(GetLineLength(line, numendpoints, numendpoints - 1),0) );
			}else{
				document.measureparams.SegLength.value = Math.abs( roundTo(GetLineLength(line, numendpoints, numendpoints - 1),0) );	
			}
		}else{
			polygon = AddtoPoly(polygon,numendpoints, mapX, mapY);
			AreaSqFt = Math.abs( roundTo(PolygonArea(polygon, numendpoints + 1),0) );
			Acre = roundTo(AreaSqFt/43560,1);
			AreaSqMi = Acre/640;
			if (document.getElementById){ 
				document.getElementById('measureparams').SegLength.value=Math.abs( roundTo(GetLineLength(polygon, numendpoints, numendpoints - 1),0) );
				document.getElementById('measureparams').Area.value=AreaSqFt;
				document.getElementById('measureparams').Acres.value=Acre;
			}else{
				document.measureparams.SegLength.value=Math.abs( roundTo(GetLineLength(polygon, numendpoints, numendpoints - 1),0) );
				document.measureparams.Area.value=AreaSqFt;
				document.measureparams.Acres.value=Acre;
			}
		}
	}
}


//DOM COMPLIANT CODE
//used to reset the area function in the case that the user has double clicked and started over
//again.
function resetMeasure(tool){
	polygon = new Array();
	line = new Array();
	startx=0;
	starty=0;
	endx=0;
	endy=0;
	newX=0;
	newY=0;
	startover = false;
	hideLayer("MeasureInst2");
	showLayer("MeasureInst1");
	for(i=1; i <= numpoints; i++){
		hideLayer("MeasurePoint"+i);
	}
	for(i=1; i <= numendpoints; i++){
		hideLayer("EndPoint"+i);
	}
	numendpoints = 0;
	numpoints = 0;
	if (document.getElementById){
		document.measureparams.TotalLength.value = 0;
		if(tool == "measure"){
			document.getElementById('measureparams').TotalLengthMi.value = 0;
			document.getElementById('measureparams').SegLength.value = 0;
		}
		if(tool == "area"){
			document.getElementById('measureparams').Area.value = 0;
			document.getElementById('measureparams').Acres.value = 0;
			document.getElementById('measureparams').SegLength.value = 0;
		}
	}else if (document.layers) {
		document.measureparams.TotalLength.value= 0;
		if(tool == "measure"){
			document.measureparams.TotalLengthMi.value= 0;
			document.measureparams.SegLength.value = 0;
		}
		if(tool == "area"){
			document.measureparams.Area.value = 0;
			document.measureparams.Acres.value = 0;
			document.measureparams.SegLength.value = 0;
		}
	}else if (document.all) {
		document.all('mapcontrol').tool.value;
		if (tool == "measure"){
			document.all('measureparams').TotalLengthMi.value = 0;
			document.all('measureparams').SegLength.value = 0;
		}
		if (tool == "area"){
			document.all('measureparams').Area.value = 0;
			document.all('measureparams').Acres.value = 0;
			document.all('measureparams').SegLength.value = 0;
		}
	}		
}

//DOM Compliant
function updateTotal(tool){
//forms in layers don't work the same in Netscape Nav below 6...
		if (tool == "measure"){
			if (document.getElementById){
				document.getElementById('measureparams').TotalLength.value=Math.abs( roundTo(GetLineLength(line, numendpoints - 1, 0),0) );
				document.getElementById('measureparams').TotalLengthMi.value=Math.abs( roundTo((GetLineLength(line, numendpoints - 1, 0)/5280),1) );
			}else if (document.layers){
				document.measureparams.TotalLength.value=Math.abs( roundTo(GetLineLength(line, numendpoints - 1, 0),0) );
				document.measureparams.TotalLengthMi.value=Math.abs( roundTo((GetLineLength(line, numendpoints - 1, 0)/5280),1) );
			}else if (document.all){
				document.all('measureparams').TotalLength.value=Math.abs( roundTo(GetLineLength(line, numendpoints - 1, 0),0) );
				document.all('measureparams').TotalLengthMi.value=Math.abs( roundTo((GetLineLength(line, numendpoints - 1, 0)/5280),1) );
			}
		}else{
			if (document.getElementById){
				document.getElementById('measureparams').TotalLength.value=Math.abs( roundTo(GetPolyPerimeter(polygon, numendpoints),0) );
			}else if (document.layers){
				document.measureparams.TotalLength.value=Math.abs( roundTo(GetPolyPerimeter(polygon, numendpoints),0) );
			}else if (document.all) {
				document.all('measureparams').TotalLength.value=Math.abs( roundTo(GetPolyPerimeter(polygon, numendpoints),0) );
			}
		}	
		
}

/*
***************************************************************************************
These are the functions that handle client side measuring and area calculations.
***************************************************************************************
*/
//function for defining points.
function point(x,y){
	this.x = x;
	this.y = y;
}
//function for adding points to lines
function AddtoLine(line,item, x, y){
	line[item] = new point(x, y);
	return line;
}
//function for adding points to polygons
function AddtoPoly(polygon,item, x, y){
	polygon[item] = new point(x, y);
	return polygon;
}
//function for finding the area of a polygon, providing the polygon array (made of points)
//and the number of points.
function PolygonArea(polygon, N){
	var i=0;
	var j;
	var area = 0;
	for (i=0; i < N ; i++) {
		j = (i + 1) % N;
		area += polygon[i].x * polygon[j].y;
      	area -= polygon[i].y * polygon[j].x;
	}
	area /= 2;
	return(area < 0 ? -area : area);
}
//function for finding the perimeter of a polygon, providing the polygon array (made of points)
//and the number of points. 
function GetPolyPerimeter(polygon, N){
	var i=0;
	var j;
	var length=0;
	if (N == 2){
		length += Math.sqrt(((polygon[1].x-polygon[0].x)*(polygon[1].x-polygon[0].x)) + ((polygon[1].y-polygon[0].y)*(polygon[1].y-polygon[0].y)))
	}else{
		for (i=0; i < N ; i++) {
			j = (i + 1) % N;
			length += Math.sqrt(((polygon[i].x-polygon[j].x)*(polygon[i].x-polygon[j].x)) + ((polygon[i].y-polygon[j].y)*(polygon[i].y-polygon[j].y)))
		}
	}
	return length;
}
//function for finding the length of any sement of a line, including the entire line.
//pass in the line (array of points), the number of points in the array, and which point to
//start on.
function GetLineLength(line, N, start){
	var i=0;
	var j;
	var length=0;
	for (i=start; i < N ; i++) {
		j = (i + 1)
		length += Math.sqrt(((line[i].x-line[j].x)*(line[i].x-line[j].x)) + ((line[i].y-line[j].y)*(line[i].y-line[j].y)))
		//alert(line[i].x)
	}
	return length;
}

function roundTo(num,pow){
  num *= Math.pow(10,pow);
  num = (Math.round(num)/Math.pow(10,pow))+ "" ;
  if(num.indexOf(".") == -1)
    num += "." ;
  while(num.length - num.indexOf(".") - 1 < pow)
    num += "0" ;
  return num ;
}

//-------- 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;
			layer.clip.top    = cliptop;
		    layer.clip.right  = clipright;
			layer.clip.bottom = clipbottom;
		} else if (isIE) {
			layer.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
	    } else {
     		layer.height = clipbottom - cliptop;
			layer.width	= clipright - clipleft;
			layer.top	= (cliptop+vspc) + "px";
			layer.left	= (clipleft+hspc) + "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);
	    if (layer != null){
	    	layer.width = width;
	    	layer.height = height;
   		}
	} else {
		document.writeln('<div id="' + name + '" style="position:absolute;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);
}

//DOM Compliant
// get the layer object called "name"
function getLayer(name) {
	if (document.getElementById) {
		var theObj = document.getElementById(name);
		if (eval(theObj) != null){
			return(theObj.style);
		} else
			return(null);
	} else if (document.layers) { 
		if (eval('document.layers' + name) != null){
		    return(document.layers(name).style);
		}else
		 	return(null);
	} else {
		if ( eval('document.all.' + name) != null) {
			return(document.all.name.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";
	}
}