
var newMinx = 0;
var newMiny = 0;
var newMaxx = 0;
var newMaxy = 0;
var extMinx = 0;
var extMiny = 0;
var extMaxx = 0;
var extMaxy = 0;
var boxMinx = 0;
var boxMiny = 0;
var boxMaxx = 0;
var boxMaxy = 0;
//added ck 2003
var scaleXX = 0;
var scaleYY = 0;
     
var mapClickAsRecenter = true;
var mapBoxAsZoom = true;
var allowRubberband = false;
var allowMapClick = true;
//var state = "zoomin";  // zoomOut, pan

// 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 iwidth=600;
//var iheight=450;

// variables for measure tool
var positionX = 0;
var positionY = 0;
var clickCount = 0;
var clickPointX = new Array();
var clickPointY = new Array();
var clickMeasure = new Array();
	// type - 1=Measure; 2=SelectLine ; 3=SelectPolygon
var clickType = 1;
var numDecimals = 2;
var totalMeasure = 0;
var currentMeasure = 0;
var lastTotMeasure = 0;

var mapX = 0; 
var mapY = 0; 
var zoomBoxWidth = 1;

var sWindowStatus='';

//SEARCHLITE var state = "zoom"; // "pan"

var zooming=false;
var panning=false;
//var bottomBorderHeight = 13;
var bottomBorderHeight = 0;

// 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;

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) {
	if ((navigator.appVersion.indexOf("MSIE 5")>0) || (navigator.appVersion.indexOf("MSIE 6")>0)) {
		isIE4 = false;
		is5up = true;
		//alert("IE5");
	}
}	

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);
	}
}

function customMapBox(_newMinx, _newMiny, _newMaxx, _newMaxy) {

}

function customMapClick(_newX, _newY) {

}

function getImageXY(e) {
	//if (document.layers) {
	if (isNav) {
		mouseX=e.pageX;
		mouseY=e.pageY;
	} else {
		mouseX=event.clientX + document.body.scrollLeft;
		mouseY=event.clientY + document.body.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(state == "measure"){
		//calcDistance(mapX,mapY);
	}
}

//added*************************************************************************
// convert mouse click xy's into map coordinates
function getMeasureXY(xIn,yIn) {
	//mouseX = xIn;
	var pixelX = (maxx-minx) / iWidth;
	positionX = pixelX * xIn + minx;
	//mouseY = iHeight - yIn;
	var pixelY = (maxy-miny) / iHeight;
	positionY = pixelY * (iHeight - yIn) + miny;
	
	if(state == "measure"){
		//calcDistance(positionX,positionY);
	}
}
//*************************************************************************

// get the coords at mouse position
function getMouse(e) {
 
	window.status="";
	getImageXY(e);
	sWindowStatus = 'mouseX=' + mouseX + 'mouseY='+mouseY;
  //added***************************************************************************
  if ((mouseY > 0)&&(mouseX > 0)&&(mouseX < iWidth)&&(mouseY < iHeight-bottomBorderHeight)){ 
    getMeasureXY(mouseX,mouseY);
    var status = "Longitude= "+positionX+" Latitude= "+positionY; 
    //commented out by ck 2003 to not allow the window status
    //window.status=status;
    //added ck 2003 for id
    giveInfo(positionX,positionY);
    sWindowStatus += ' (In Map)';
    //alert("x" + mouseX + " y"+ mouseY);
  }else{
  		sWindowStatus += ' (Not In Map)';
	}
	//window.status=sWindowStatus;
  //********************************************************************************
	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 
    	return true;
	return true;
}

function hideZoomBox() {
	hideLayer("zoomBoxTop");
	hideLayer("zoomBoxLeft");
	hideLayer("zoomBoxRight");
	hideLayer("zoomBoxBottom");
}

// perform appropriate action with mapTool
function mapTool (e) {

	getImageXY(e);
	
	  // Deal with the possibility of an
	  // "identify" first, since it is a
	  // little different than the other
	  // states (doesn't require a mouse
	  // up event).  
	  	
	 if ((state == "identify") && 
	    (mouseX >= 0) && (mouseX <= iWidth) && 
	    (mouseY >= 0) && (mouseY <= iHeight)){
			getMapXY(mouseX,mouseY);
			//sendIdentify(mapX,mapY);      
			sendIdentify(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 (state == "pan"){
			startPan(e);
		}else if (state == "measure"){
			clickAddPoint();
		} 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("theMap",cLeft,cTop,cRight,cBottom);
	//moveLayer("theMap",xMove+hspc,yMove+vspc);

	return false;
}

// recenter map is the default option
function recenter(e) {
	

	if (allowRubberband)
  	hideZoomBox();    
	getMapXY(mouseX,mouseY); 
	if (mapClickAsRecenter) {  
	 if (event.button==1) {  
           if (state == "zoomIn") {       

		var widthHalf = Math.abs(maxx - minx) / 4;
		var heightHalf = Math.abs(maxy - miny) / 4;


		newMinx = minx;
		newMaxx = maxx;
		newMaxy = mapX;
		newMiny = mapY;



	    } else if (state == "zoomOut") {
		var widthHalf = Math.abs(maxx - minx) * 1;
		var heightHalf = Math.abs(maxy - miny) * 1;
 
		newMinx = minx;
		newMaxx = maxx;
		newMaxy = mapX;
		newMiny = mapY;   

	    } else if (state == "pan") {
		//var widthHalf = Math.abs(maxx - minx) / 1;

	    } else if ((state == "id") || (state == "buffer")) {
	    	//extMiny = newMaxy - 0.0001;*/
	    } 
		
	    refreshMap(); 
       }

	} else
		customMapClick(mapX,mapY);
}

function refreshMap() {
  hideZoomBox();
  var actionName;
  //added ck 2003 to disallow the page to read the mouses position in the scroll
  //menu to be interpretated by the map as a map click. Only seen in Netscape 5 >.  
  if(isNotDropDown){
  //end ck add
      showLayer("loadLayer");        
      actionName = state;
        if (state == "pan"){
        actionName = state;
        }
      if (state == "id") {
        document.location = sMapProcessor+"action="+actionName+
        "&idMinx="+extMinx.toString()+
        "&idMiny="+extMiny.toString()+
        "&idMaxx="+extMaxx.toString()+
        "&idMaxy="+extMaxy.toString()
  
      } else if (state == "buffer") {
        document.location = sMapProcessor+"action="+actionName+
        "&bufMinx="+extMinx.toString()+
        "&bufMiny="+extMiny.toString()+
        "&bufMaxx="+extMaxx.toString()+
        "&bufMaxy="+extMaxy.toString()
      } else {
        //document.location = sMapProcessor+"action="+actionName+
        //"&minx="+newMinx.toString()+
        //"&miny="+newMiny.toString()+
        //"&maxx="+newMaxx.toString()+
        //"&maxy="+newMaxy.toString()+
        //"&widthH="+scaleXX.toString()+    
        //"&heightH="+scaleYY.toString()
        //added ck for application handling of session variables
        setParameters("action="+actionName+
                      "&minx="+newMinx.toString()+
                      "&miny="+newMiny.toString()+
                      "&maxx="+newMaxx.toString()+
                      "&maxy="+newMaxy.toString()+
                      "&widthH="+scaleXX.toString()+    
                      "&heightH="+scaleYY.toString());
      }
     //alert (newMinx.toString()); 
     //alert (newMaxx.toString()); 
      //NOTE: calling hideLayer("loadLayer") causes
      //      the "Loading" image to disappear 
      //      immediately.

  //alert("adjusting...");
  //mouseX = mouseX + iWidth;
  //mouseY = mouseY + iHeight;      
  }
}

function sendIdentify(numMapX,numMapY) {
  
  //hideZoomBox();
  showLayer("loadLayer");
  var actionName;
    actionName = "id";
    if (state == "id"){
		actionName = state;
    }
  //alert(numMapX);
  //document.location = 
  /*document.location = sMapProcessor+"&action=id&x="+numMapX+"&"+"y="+numMapY+		
  		"&minx="+newMinx.toString()+
		"&miny="+newMiny.toString()+
		"&maxx="+newMaxx.toString()+
		"&maxy="+newMaxy.toString();*/
  hideLayer("loadLayer")
  //NOTE: calling hideLayer("loadLayer") causes
  //      the "Loading" image to disappear 
  //      immediately.


var objWin;
var sURL;
//getMapXY(numMapX,numMapY);
//alert(numMapY);
sURL = sIDProcessor+"&X=" + numMapX + "&Y=" + numMapY + "&minx=" + minx +"&miny="+miny+"&maxx="+maxx+"&maxy="+maxy;		
//alert (sURL);
objWin = window.open("loading.htm", "Identify", "height=240,width=700,scrollbars=yes,resizeable=yes");
if (parseInt(navigator.appVersion) >=4) objWin.window.focus();
objWin = window.open(sURL, "Identify", "height=240,width=700,scrollbars=yes,resizeable=yes");
if (parseInt(navigator.appVersion) >=4) objWin.window.focus();
  
}

// 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);
	}
}

function setExtent(_minx,_miny,_maxx,_maxy) {
	minx = _minx;
	miny = _miny;
	maxx = _maxx;
	maxy = _maxy;
}

function setState(newState) {
	state = newState;

	if (state == "zoomIn") {
		mapBoxAsZoom = true;
		setZoomBoxColor("#ff0000");
		setZoomBoxWidth(1);
	} else if (state == "zoomout") {
		mapBoxAsZoom = true;
		setZoomBoxColor("#ff0000");
		setZoomBoxWidth(1);
	} else if (state == "id") {
		mapBoxAsZoom = true;
		setZoomBoxColor("#ff0000");
		setZoomBoxWidth(1);
	} else if (state == "buffer") {
		mapBoxAsZoom = true;
		setZoomBoxColor("#ff0000");
		setZoomBoxWidth(1);
  //added**********************************************************************
	} else if (state == "measure") {
		mapBoxAsZoom = true;
		setZoomBoxColor("#ff0000");
		setZoomBoxWidth(1);
  //***************************************************************************    
	} else if (state == "pan") {
	} else if (state == "buffer") {
		mapBoxAsZoom = false;
	}
}

function setZoomBoxColor(color) {
	setLayerBackgroundColor("zoomBoxTop", color);
	setLayerBackgroundColor("zoomBoxLeft", color);
	setLayerBackgroundColor("zoomBoxRight", color);
	setLayerBackgroundColor("zoomBoxBottom", color);
}

function setZoomBoxSettings() {

	// Set up event capture for mouse movement
	if (isNav && is5up) {
		document.captureEvents(Event.MOUSEMOVE);
		document.captureEvents(Event.MOUSEDOWN);
		document.captureEvents(Event.MOUSEUP);
		document.onmousemove = getMouse;
		document.onmousedown = mapTool;
		document.onmouseup = chkMouseUp;
	} else if (isNav4) {
		// otherwise the buttons don't work
		getLayer("theTop").captureEvents(Event.MOUSEMOVE);
		getLayer("theTop").captureEvents(Event.MOUSEDOWN);
		getLayer("theTop").captureEvents(Event.MOUSEUP);
		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)) {
		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;
	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;

    //added ck 2003
    scaleXX = ((newMaxx + newMinx) / 2); 
    scaleYY = ((newMaxy + newMiny) / 2); 
    //alert(scaleX);
    //alert(scaleY);     
		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;
			} else if ((state == "id") || (state == "buffer")) {
				extMinx = newMinx;
				extMiny = newMiny;
				extMaxx = newMaxx;
				extMaxy = newMaxy;
				newMinx = minx;
				newMiny = miny;
				newMaxx = maxx;
				newMaxy = maxy;
			}
			refreshMap();
		} else
			customMapBox(newMinx, newMiny, newMaxx, newMaxy);
			
	}
	return true;
}

//-------- LAYER SUPPORT FUNCTIONS --------------

// atlas_common.js

// 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);
	    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) {
	  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) {
//		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";
	}
}

function clickFunction (toolName) {
	if (hasLayer("measureBox"))
		hideLayer("measureBox");
	switch(toolName) {
	// put a point at click and add to clickCount
	// Measure-Unit function
	case "measure":
		panning=false;
		zooming=false;
		selectBox=false;
		shapeSelectBuffer = false;
		if (clickCount>0) {
			if (totalMeasure==0) resetClick();
		}
		toolMode = 20;
		if (isIE)	{
			document.all.theTop.style.cursor = "crosshair";
			theCursor = document.all.theTop.style.cursor;
		}
		if (clickType==1) {
			//if (useTextFrame) parent.TextFrame.location= appDir + "measure.htm";        
        alert("help");
				//showLayer("measureBox");
				//updateMeasureBox();
		}
		break;
	}
}

function clickAddPoint() {
	var theX = mouseX;
	var theY = mouseY;
	getMapXY(theX,theY);
	clickPointX[clickCount]=mapX;
	clickPointY[clickCount]=mapY;
	clickCount += 1;
	selectCount=0;
	totalMeasure = totalMeasure + currentMeasure;
		//var u = Math.pow(10,numDecimals);
		//if (totalMeasure!=0) totalMeasure = parseInt(totalMeasure*u+0.5)/u;
    
	clickMeasure[clickCount]=totalMeasure;
  //added***********************************************************************
  addLine(theX,theY,hspc,vspc);
}
//resets the measure lines and measure form
function resetMeasure(){
  //resetClick();
  //newline();
}
// zero out all clicks in clickCount
function resetClick() {
	var c1 = clickCount;
	clickCount=0;
	clickPointX.length=1;
	clickPointY.length=1;
	currentMeasure=0;
	totalMeasure=0;
	lastTotMeasure=0;
	clickMeasure.length=1;
	selectCount=0;
	
	//legendTemp=legendVisible;
	//legendVisible=false;
	//var theString = writeXML();
	//var theNum = 99;
		//showRetrieveMap();
	//sendToServer(imsURL,theString,theNum);
		
	//updateMeasureBox();

}	

// remove last click from clickCount
function deleteClick() {
	var c1 = clickCount;
	clickCount=clickCount-1;
	selectCount=0;
	if (clickCount<0) clickCount=0;
	if (clickCount>0) {
		totalMeasure = clickMeasure[clickCount]
		clickPointX.length=clickCount;
		clickPointY.length=clickCount;
		clickMeasure.length=clickCount;
		
	} else {
		totalMeasure=0;
		clickMeasure[0]=0;
	}
	currentMeasure=0;
	if (c1>0) {
		legendTemp=legendVisible;
		legendVisible=false;
		var theString = writeXML();
		var theNum = 99;
		sendToServer(imsURL,theString,theNum);
	}
	
}

function calcDistance(mX,mY) {
	// Note: decimal are not hard coded to allow use with locales using commas instead of points.
	if (clickCount>0) {
		var mUnits = "DEGREES";
		var mDistance = 0;
		var p = clickCount-1;
		var Lon1 = clickPointX[p] * Math.PI / 180;
		var Lon2 = mX * Math.PI / 180;
		var Lat1 = clickPointY[p] * Math.PI / 180;
		var Lat2 = mY * Math.PI / 180;
		var LonDist = Lon1-Lon2;
		var LatDist = Lat1-Lat2;
		//if (MapUnits=="DEGREES") {
			var A = Math.pow(Math.sin(LatDist / 2),2) + Math.cos(Lat1) * Math.cos(Lat2) * Math.pow(Math.sin(LonDist /2),2);
			//var A = Math.cos(Lat1) * Math.cos(Lat2) * Math.pow(Math.sin(LonDist /2),2);
			var C = 2 * Math.asin(Math.min(1, Math.sqrt(A)));
			var D = (3963 - 13 * Math.sin((Lat1 + Lat2) / 2)) * C
			mDistance = D * 5280;
			mUnits = "FEET";
		/*} else {
			var xD = Math.abs(mX - clickPointX[p]);
			var yD = Math.abs(mY - clickPointY[p]);
			mDistance = Math.sqrt(Math.pow(xD,2) + Math.pow(yD,2));
		}*/
		
		var theDist = convertUnits(mDistance,mUnits,"MILES");
		var u = Math.pow(10,numDecimals);
		currentMeasure = parseInt(theDist*u+(5/10))/u;
		
		//updateMeasureBox();
		
	}
}

// convert the amounts to new units
function convertUnits(theDist1,mUnits,sUnits) {
	// Note: decimal are not hard coded to allow use with locales using commas instead of points.	
	var theDist = parseFloat(theDist1);
	var mDistance = theDist;
	//alert(theDist);
	if (mUnits == "FEET") {
		if (sUnits=="MILES") {
			mDistance = theDist / 5280;
		} else if (sUnits == "METERS") {
			mDistance = theDist * (3048/10000);
		} else if (sUnits == "KILOMETERS") {
			mDistance = theDist * (3048/10000000);
		}
	} else {
		if (sUnits=="MILES") {
			mDistance = theDist * (6213711922/10000000000000);
		} else if (sUnits == "FEET") {
			mDistance = theDist * (3280839895/1000000000);
		} else if (sUnits == "KILOMETERS") {
			mDistance = theDist / 1000;
		}
	}
	var u = Math.pow(10,numDecimals);
	//alert(u);
	if (!isNav) mDistance = parseInt(mDistance * u + (5/10)) / u
	//alert(mDistance);
	return mDistance;
}

// update Measure Box
function updateMeasureBox() {

	//urlStr = new String(parent.infoFrame.location);
	//slashIdx = urlStr.lastIndexOf("/") + 1;
	//searchUrl = urlStr.substr(slashIdx,(urlStr.length - 1));
	//if(searchUrl=="measure.jsp"){
		if (isNav4) {
			//var theForm = parent.infoFrame.document.meas;
      var theForm = document.meas;
		} else {
			//var theForm = document.measureBox.forms[0];
			var theForm = document.meas;
		}
		/*var j = 1;
		for (var i=0;i<sUnitList.length;i++) {
			if (ScaleBarUnits==sUnitList[i]) j=i;
		}*/
			var u = Math.pow(10,numDecimals);
			var tMeas = 0;
			if (totalMeasure!=0) tMeas = parseInt(totalMeasure*u+0.5)/u;
		theForm.theMeasTotal.value = tMeas + currentMeasure + " miles";
		theForm.theMeasSegment.value = currentMeasure + " miles";
    showLayer("measureBox");
	//}
}

// clear measure box
function clearMeasureBox(){
	//urlStr = new String(parent.infoFrame.location);
	//slashIdx = urlStr.lastIndexOf("/") + 1;
	//searchUrl = urlStr.substr(slashIdx,(urlStr.length - 1));
	//if(searchUrl=="measure.jsp"){
		if (isNav4) {
			//var theForm = parent.infoFrame.document.meas;
      var theForm = document.meas;
		} else {
			//var theForm = document.measureBox.forms[0];
			var theForm = document.meas;
		}
		theForm.theMeasTotal.value = 0;
		theForm.theMeasSegment.value = 0;
	//}
}
	