var newMinx = 0;
var newMiny = 0;
var newMaxx = 0;
var newMaxy = 0;

var mapClickAsRecenter = true;
var mapBoxAsZoom = true;
var allowRubberband = true;
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 mapX = 0; 
var mapY = 0; 
var zoomBoxWidth = 1;

//SEARCHLITE var state = "zoom"; // "pan"
var zooming=false;
var panning=false;
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;

var SubtractMapWidth = 319;
var SubtractMapHeight = 200; 



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;
		//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) 
// Gets the Image xy in page units (a.k.a. pixels)
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;
	}
	//alert(mouseY);
	// 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 = Math.abs(maxx  -(minx)) / iWidth;
	mapX = pixelX * mouseX + minx;
	//mouseY = iHeight - yIn;
	var pixelY = Math.abs(maxy-(miny)) / iHeight;
	mapY = pixelY * (iHeight - yIn) + miny;

}

function mouseStatus(e){
	getImageXY(e);
	getMapXY(mouseX,mouseY);

	window.status="";
		// Brook: Testing some stuff.
	//getMapXY(window.event.clientX,window.event.clientY);
	
	window.status = " map X: " + mapX+ " map Y: " + mapY;
	//window.status = "X Coord: " + statusMapX + "  " + "Y Coord: " + statusMapY;


}

// get the coords at mouse position
function getMouse(e) {
	getImageXY(e);
	getMapXY(mouseX,mouseY);
	mouseStatus(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) {
		window.status="";
		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) {

	//alert(minx + " " +  miny  + " " +  maxx  + " " +  maxy);
	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);
			return false; // <-- false assures that the popup
			              //     box stays in front.
	}
	
		if ((state == "idcontrol") && 
	    (mouseX >= 0) && (mouseX <= iWidth) && 
	    (mouseY >= 0) && (mouseY <= iHeight)){
			getMapXY(mouseX,mouseY);
			sendIdControl(mapX,mapY);
			//sendIdentify(mapX,mapY);
			//alert(mapX + mapY);
			return false; // <-- false assures that the popup
		}	              //     box stays in front.
	
	if ((state=="label") &&
	    (mouseX >= 0) && (mouseX <= iWidth) && 
	    (mouseY >= 0) && (mouseY <= iHeight)){
	    	getMapXY(mouseX,mouseY);
	    	SetMapFormDimensions()
	    	//document.Form1.MapWidth.value =  top.document.body.offsetWidth-SubtractMapWidth;
			//document.Form1.MapHeight.value =  top.document.body.offsetHeight-SubtractMapHeight;
			document.Form1.labelx.value =  mapX;
			document.Form1.labely.value =  mapY;
				

		//if (window.opener && !window.opener.closed) {
			//		window.opener.close()
			//	}
			var w=window.open("MapLabel.aspx?mapX="+mapX+"&mapY="+mapY,'MapLabel','scrollbars=no,resizable=no,width=400,height=350');

		return false; 
	}
	// <-- false assures that the popup
	  // 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 == "zoomOut"){
			//alert("Current State = " + state);
			zoomOut();
			}
		else 
			//alert("mapTool function on Mouse Down! Zooming is: " + zooming);
			startZoomBox(e);
		return false;
	} else if (zooming) {
		//alert("mapTool function checking zooming on tha Mouse Up! Zooming is: " + 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) {

	// 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;
		mapClickAsRecenter = false;
		refreshMap(); 
	} else
		customMapClick(mapX,mapY);
}

// Zoom out on click event.
// Called by: Click event.
function zoomOut(e) {
			
		window.scrollTo(0,0);
		var widthInMapUnits = Math.abs(maxx - minx) * 2;
		var heightInMapUnits = Math.abs(maxy - miny) * 2;
		var pixelX = (maxx-minx) / iWidth;
		mapX = Math.round(pixelX * mouseX + minx);
		var tempY = iHeight - mouseY;
		var pixelY = (maxy-miny) / iHeight;
		mapY = Math.round(pixelY * mouseY + miny);
		getMapXY(mouseX,mouseY);

		newMaxy = mapY + heightInMapUnits;
		newMaxx = mapX + widthInMapUnits;
		newMinx = mapX - widthInMapUnits;
		newMiny = mapY - heightInMapUnits;
		//alert(newMaxx - newMinx);
		//alert(document.Form1.DefaultMapExtentMaxX.value - document.Form1.DefaultMapExtentMinX.value);
		//		if ((newMaxx - newMinx) > (document.Form1.DefaultMapExtentMaxX.value - document.Form1.DefaultMapExtentMinX.value)){
		//			newMinx = document.Form1.DefaultMapExtentMinX.value;
		//			newMiny = document.Form1.DefaultMapExtentMinY.value;
		//			newMaxx = document.Form1.DefaultMapExtentMaxX.value;
		//			newMaxy = document.Form1.DefaultMapExtentMaxY.value;	
		//		}
			refreshMap(); 
}


function zoomParcel(cmdvalue) {
	showLayer("loadLayer");
	alert(cmdvalue);
	SetMapFormDimensions()
	document.Form1.FCommand.value = "ZOOMPARCEL&"+cmdvalue
	var theform = document.Form1;
	theform.submit();
	submitLayersForm();
	submitLegendForm();
}


function zoomHood(strHoodName){
  showLayer("loadLayer");

	SetMapFormDimensions()
	document.Form1.FCommand.value = "ZOOMHOOD&"+strHoodName

  var theform = document.Form1;
  theform.submit();
 submitLayersForm();
 submitLegendForm();
}

function zoomCity(strCityName){
  showLayer("loadLayer");

	SetMapFormDimensions()
	document.Form1.FCommand.value = "ZOOMCITY&"+strCityName

  var theform = document.Form1;
  theform.submit();
 submitLayersForm();
 submitLegendForm();
}

function zoomPrevious(){
//zoomToExtent(document.Form1.DefaultMapExtentMinX.value,document.Form1.DefaultMapExtentMinY.value,document.Form1.DefaultMapExtentMaxX.value,document.Form1.DefaultMapExtentMaxY.value)


	SetMapFormDimensions()
	document.Form1.FCommand.value = "ZOOMPREVIOUS"
	
  var theform = document.Form1;
  theform.submit();
   submitLayersForm();
   submitLegendForm();
  }
  
  
  function clearLabels(){

	SetMapFormDimensions()
	document.Form1.FCommand.value = "CLEARLABELS"
	
  var theform = document.Form1;
  theform.submit();

  }


function zoomNext(){

	SetMapFormDimensions()
	document.Form1.FCommand.value = "ZOOMNEXT"
	
  var theform = document.Form1;
  theform.submit();
    submitLayersForm();
    submitLegendForm();
  }
function zoomFull(){
//zoomToExtent(document.Form1.DefaultMapExtentMinX.value,document.Form1.DefaultMapExtentMinY.value,document.Form1.DefaultMapExtentMaxX.value,document.Form1.DefaultMapExtentMaxY.value)
  showLayer("loadLayer");
	
	SetMapFormDimensions()
	document.Form1.FCommand.value = "ZOOMFULL"

   var theform = document.Form1;
   theform.submit();
   submitLayersForm();
   submitLegendForm();
 
  }

function panFromSidebar(myDirection){

  showLayer("loadLayer");

	
SetMapFormDimensions()
	switch( myDirection )
	{
		case "N" :
			document.Form1.FCommand.value = "PANNORTH"
			break;
		case "S" :	
			document.Form1.FCommand.value = "PANSOUTH"
			break;
		case "E" :	
			document.Form1.FCommand.value = "PANEAST"
			break;
		case "W" :	
			document.Form1.FCommand.value = "PANWEST"
			break;
		case "NW" :	
			document.Form1.FCommand.value = "PANNW"
			break;
		case "NE" :	
			document.Form1.FCommand.value = "PANNE"
			break;
		case "SE" :	
			document.Form1.FCommand.value = "PANSE"
			break;
		case "SW" :	
			document.Form1.FCommand.value = "PANSW"
			break;
	}
	
	
	var theform = document.Form1;
	theform.submit();
	//submitLayersForm();
    //submitLegendForm(); 

  }

function SetMapFormDimensions(){

	document.Form1.MapWidth.value =  top.document.body.offsetWidth-SubtractMapWidth;
	document.Form1.MapHeight.value =  ((top.document.body.offsetHeight * 0.85) - 43)

}

// zoomToExtent(Minimum X, Minimum Y, Maximum X, Maximum Y)
// Zooms the map to a specified extent as given in the function call. 
// NOTE: The coordinates are given in strings.
// Called by: ZoomFull function, but can be called by 
// any routine which passes in extent coordinates.
function zoomToExtent(FullMinX,FullMinY,FullMaxX,FullMaxY){
	//alert("MinX: " + FullMinX + "\nMinY: " + FullMinY + "\nMaxX: " + FullMaxX + "\nMaxY: " + FullMaxY);
   //if (FullMinX  > document.Form1.DefaultMapExtentMinX.value
   newMinx = FullMinX;
   newMiny = FullMinY;
   newMaxx = FullMaxX;
   newMaxy = FullMaxY;
   refreshMap();
	
}

function submitIdentifyForm(){
	var idIFrame = parent.document.getElementById('tabMap_frame1');
	if (idIFrame.contentDocument){
		idIFrame.contentDocument.forms[0].submit();
	}else{
		top.window.frames['tabMap_frame1'].document.forms[0].submit();
	}
}

function submitLayersForm(){
//Tests to see if the layers form is active. If it is null then do nothing
//it will load up the next time its clicked on

 
 if (top.window.frames['tabMap_frame2'].document.forms[0]!=null){
	top.window.frames['tabMap_frame2'].document.forms[0].submit();
	}
}

function submitLegendForm(){
//Tests to see if the legned form is active. If it is null then do nothing
//it will load up the next time its clicked on
 if (top.window.frames['tabMap_frame3'].document.forms[0]!=null){
	top.window.frames['tabMap_frame3'].document.forms[0].submit();
	}
}

function refreshMap() {
  hideZoomBox();
  showLayer("loadLayer");
//alert(newMaxy);
   document.Form1.MapZoomExtentMinX.value = newMinx.toString();
   document.Form1.MapZoomExtentMinY.value = newMiny.toString();
   document.Form1.MapZoomExtentMaxX.value = newMaxx.toString();
   document.Form1.MapZoomExtentMaxY.value = newMaxy.toString();
   SetMapFormDimensions();

	document.Form1.FCommand.value = "DYNAMIC"

	//alert(document.Form1.MapWidth.value+' '+document.Form1.MapHeight.value);
    
  var theform = document.Form1;
  theform.submit();
  //top.document.forms[0].submit();
  //Re-submits the layers form on each dynamic map redraw
  if (state != "pan"){

submitLayersForm();
submitLegendForm();

}
  //NOTE: calling hideLayer("loadLayer") causes
  //      the "Loading" image to disappear 
  //      immediately.
}


function sendIdControl(numMapX,numMapY) {

//alert(sURL);
top.window.frames['tabMap_frame1'].document.Control.xcoord.value=numMapX
top.window.frames['tabMap_frame1'].document.Control.ycoord.value=numMapY
top.window.frames['tabMap_frame1'].document.forms[0].submit();
 
 }

function sendIdentify(numMapX,numMapY) {
	var idIFrame = parent.document.getElementById('tabMap_frame1');
	if (idIFrame.contentDocument){
		//firefox and Safari:
		idDoc = idIFrame.contentDocument;
		idDoc.forms[0].xcoord.value=numMapX;
		idDoc.forms[0].ycoord.value=numMapY;
	}
	else {
		//IE version here.
		top.window.frames['tabMap_frame1'].document.Identify.xcoord.value=numMapX;
		top.window.frames['tabMap_frame1'].document.Identify.ycoord.value=numMapY;
	}
	submitIdentifyForm();
}

// 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);
	}
}

// Commented out by Brook on 10/27/2003
// I couldn't find anything that called this function.
//function setExtent(_minx,_miny,_maxx,_maxy) {
//	minx = _minx;
//	miny = _miny;
//	maxx = _maxx;
//	maxy = _maxy;
//}

function setState(newState) {
	state = newState;
//alert(state);
	document.Form1.SelectedTool.value = newState
    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 (state == "zoomIn") {
	//alert(state);
		mapBoxAsZoom = true;
		setZoomBoxColor("#ff0000");
		setZoomBoxWidth(2);
	} else if (state == "zoomOut") {
		mapBoxAsZoom = false;
	//} else if (state == "pan") {
	//used to select some survey points
	} else if (state == "selectify") {
		//alert(state);
		mapBoxAsZoom = false;
		setZoomBoxColor("#7b68ee");
		setZoomBoxWidth(3);
	 
	}
	
}

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 zoom box display... zoom in
function stopZoomBox(e) {
	zooming=false;
	if ((zmaxx < zminx+4) && (zmaxy < zminy+4)) {
		// if the zoom box is smaller than 4 pixels,
		// assume the user just want to zoom click
		//alert("me");
		// and do the zoom click function.
		if (state == "selectify" ){
						getMapXY(mouseX,mouseY);
						//alert(mapX + " " + mapY);
						sendIdControl(mapX,mapY);
		} else{
		zoomInOnClick();
		}
	} else {
		var width = Math.abs(maxx - minx);
		var height = Math.abs(maxy - miny);
			
			// Brook: Testing some stuff.
			//getMapXY(window.event.clientX,window.event.clientY);
		
		// calculate the new MAXIMUM Y in Map Units based on the user's click.
		var theY = iHeight - zmaxy;
		var pixelY = height / iHeight;
		newMaxy = pixelY * theY + miny;

		// calculate the new MINIMUM Y in Map Units
		theY = iHeight - zminy;
		pixelY = height / iHeight;
		newMiny = pixelY * theY + miny;			
		
		// calculate the new MAXIMUM X in Map Units
		// iWidth is pixels
		//var theX = zmaxx;
		var pixelX = width / iWidth;
		newMaxx = pixelX * zmaxx + minx;
		
		// calculate the new MINIMUM X in Map Units
		newMinx = Math.round(pixelX * zminx) + minx;
	
			// Check to make sure the new zoom is not smaller than the preset zoomin limit.
			checkZoomInLimit(newMinx,newMiny,newMaxx,newMaxy)


			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;
				}
				refreshMap();
			} else if (state == "selectify" ){
				//need if the user is trying to select some survey poitns
				//this should only ever happen when using the Survey flavor of the system.
						if ((zmaxx < zminx+40) && (zmaxy < zminy+40)) {
			
						getMapXY(mouseX,mouseY);
						//alert(mapX + " " + mapY);
						sendIdControl(mapX,mapY);
			
					hideZoomBox();
					} else {
						top.window.frames['tabMap_frame1'].document.Control.minx.value=newMinx
						top.window.frames['tabMap_frame1'].document.Control.miny.value=newMiny
						top.window.frames['tabMap_frame1'].document.Control.maxx.value=newMaxx
						top.window.frames['tabMap_frame1'].document.Control.maxy.value=newMaxy
						top.window.frames['tabMap_frame1'].document.forms[0].submit();
				//refreshMap();
				hideZoomBox();
					}
			
			} else
				customMapBox(newMinx, newMiny, newMaxx, newMaxy);	
			}
	return true;
}

// 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 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;
	
}

// zoomInOnClick
//
// Zooms in on a mouse click event if the user didn't drag a box bigger than 4 pixels
// Called From: stopZoomBox(e)
function zoomInOnClick(){
	getMapXY(mouseX,mouseY);
	var widthHalf = Math.abs(maxx - minx) / 6;
	var heightHalf = Math.abs(maxy - miny) / 6;
	newMinx = mapX - widthHalf;
	newMaxx = mapX + widthHalf;
	newMaxy = mapY + heightHalf;
	newMiny = mapY - heightHalf;
		
	checkZoomInLimit(newMinx,newMiny,newMaxx,newMaxy)
	
	refreshMap(); 
}

// checkZoomInLimit(newMinX,newMinY,newMaxX,newMaxY)
// Called From: The zoomin functions zoomInOnClick() and stopZoomBox()
// NOTE the capitalization of the passed in coordinates: newMinX, newMinY, newMaxX, newMaxY
//      these are different from the coords used outside this function, which are not caps:
//      (newMinx,newMiny,newMaxx,newMaxy). These global variables are reset within this
//      function if the user is trying to zoom in closer than the preset held in the
//      variable "zoomInLimit".
function checkZoomInLimit(newMinX,newMinY,newMaxX,newMaxY) {
	var newWidth = Math.abs(newMaxX - newMinX);
	var newHeight = Math.abs(newMaxY - newMinY);
	//alert("Checking Zoom In Limit. \n\nNew Width: " + newWidth + "\nNew Height: " + newHeight + "\n\nNew Min X: " + newMinX + "\nNew Min Y: " + newMinY + "\nNew Max X: " + newMaxX + "\nNew Max Y: " + newMaxY);
		
	if (newWidth < zoomInLimit & newHeight < zoomInLimit){
		//alert("New zoom is too small." + "\n\nNew Min X: " + newMinx + "\nNew Min Y: " + newMiny+ "\nNew Max X: " + newMaxx + "\nNew Max Y: " + newMaxy);
			var limitDiffX = (zoomInLimit - newWidth) / 2;
			newMinx = newMinX - limitDiffX;
			newMaxx = newMaxX + limitDiffX;
			var limitDiffY = (zoomInLimit - newHeight) / 2;
			newMiny = newMinY - limitDiffX;
			newMaxy = newMaxY + limitDiffX;
		//	 newnewWidth = Math.abs(newMaxx - newMinx);
		//	 newnewHeight = Math.abs(newMaxy - newMiny);
			//alert("Zoom In Limit set. \n\nNew New Width: " + newnewWidth + "\nNew New Height: " + newnewHeight + "\n\nNew Min X: " + newMinx + "\nNew Min Y: " + newMiny+ "\nNew Max X: " + newMaxx + "\nNew Max Y: " + 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="z-index:5;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);
}

// 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) {
//alert(name);
  	//var layer = getLayer(name);		
	if (isIE){
		var layer = document.getElementById('theMap').style;
	}else if (isNav4){
		var layer = document.layers['theMap'];
	}
	
	if (layer != null) {
	  	if (isNav4){
		 	//alert("moveLayer as Nav Four. Name is: " + name);
    		layer.moveTo(x, y);}
	 	else if (isIE) {
       		layer.left = x + "px";
 		 	//alert("moveLayer as IE. Name is: " + name);
   			layer.top  = y + "px";
		} else {
		 	//alert("moveLayer as Else. Name is: " + name);
			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";
	}
}

//***********************************************	
//***************** Layer Function *******************
//***********************************************
//Support the layers functionality
//The layers tab sends the layers array along as a form variable. 
//When the refresh map button gets clicked the on/off layers are picked up from the tree form as below.
//these are passed into the form variable and submitted (postedback to the form)
//the jscript is dependent upon the object model of the infragistics control
//The DOLAYERS command below forces the DOCOMMAND function of the map form.
function ToggleCheck() {
		var NodeArray = ''
		var LayerString = ''
		var tree = igtree_getTreeById("treeLyrs");
		nodes = tree.getNodes();

		//Get the On Layers
		for (var i=0; i < nodes.length; i++){
			var kids = nodes[i].getChildNodes();
				for (var j=0; j < kids.length; j++){
					if(kids[j].getChecked() == true){
					 // LayerString+='&lt;LAYERDEF id="' + kids[j].getTag() + '" visible="true"/&gt;';

					  NodeArray = NodeArray + kids[j].getTag() + ",";
					
					}					
				}
			}
			//Get the off Layers
			NodeArray = NodeArray + 'OFF';
			for (var i=0; i < nodes.length; i++){
			var kids = nodes[i].getChildNodes();
				for (var j=0; j < kids.length; j++){
					if(kids[j].getChecked() == false){
					  //LayerString+='&lt;LAYERDEF id="' + kids[j].getTag() + '" visible="false" /&gt;';
					  NodeArray = NodeArray + kids[j].getTag() + ",";
					
					}					
				}
			}
		//alert(LayerString);	
			top.window.frames['mapframe'].document.Form1.MapWidth.value =  top.document.body.offsetWidth-SubtractMapWidth;
			top.window.frames['mapframe'].document.Form1.MapHeight.value =  ((top.document.body.offsetHeight * 0.85) - 43)
			top.window.frames['mapframe'].document.Form1.FCommand.value = "DOLAYERS&"+NodeArray
			var theform = top.window.frames['mapframe'].document.Form1;
			theform.submit();
			
}
		
		function UltraWebTree1_NodeClick(treeId, nodeId,button){
			 if(button == 2){
				igmenu_showMenu('UltraWebMenu1', event);
				return true;
			}
		}
