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 mapClickAsRecenter = false;
var mapBoxAsZoom = true;
var allowRubberband = true;
var allowMapClick = true;
var oldState 
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; //=13

// 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;

function selectBoxHide() {
 top.document.getElementById('dhtml_goodies_id1').style.visibility = "hidden";
}


function selectBox(cmd,MinX,MinY,MaxX,MaxY,X,Y) {
 top.document.getElementById('dhtml_goodies_id1').style.visibility = "visible";
 top.document.getElementById('selectBoxFrame').src = "/select_sheets.asp?cmd="+cmd+"&MinX="+MinX+"&MinY="+MinY+"&MaxX="+MaxX+"&MaxY="+MaxY+"&X="+X+"&Y="+Y;
}

function infoBox(X,Y) {
 top.document.getElementById('dhtml_goodies_id2').style.visibility = "visible";
 top.document.getElementById('customFrame').src = "/infobox.asp?X="+X+"&Y="+Y;
}




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);
	}
}

// check for chkKeyUp
function chkKeyUp(e) {

state = top.document.getElementById('CurrentState').value;

  var theKey = 0;
  // get the button pushed... if right, ignore... let browser do the popup... it will anyway
  if (isNav) {
    theKey = e.which;
  } else {
    theKey = window.event.keyCode;
  }

  //alert("key: " + theKey + "; clickType: " + clickType + "; clickCount: " + clickCount + "; toolMode: " + toolMode);
  if ( state.toUpperCase() == "DRAWLINE" || state.toUpperCase() == "DRAWPLOY" )
  {
    switch( theKey )
    {
    case 27: //Esc
		  top.document.location = "/hub/index.asp" + "?Cmd="+state+"&fn=1"
			return false;
      break;
    case 8: //BackSpace
      //deleteClick();
      break;
    case 46: //Delete
      //resetClick();
      break;
    default:
      break;
    }
  }
}

function customMapBox(_newMinx, _newMiny, _newMaxx, _newMaxy) {
}

function customMapClick(_newX, _newY) {
	state = top.document.getElementById('CurrentState').value;
	proceedShow(iWidth,iHeight);
	top.document.location = "/hub/index.asp" + "?Cmd="+state+"&Click.x="+_newX.toString()+"&Click.y="+_newY.toString();
}

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

//     if (state.toUpperCase() == "ZOOMIN") { mouseX = mouseX+17; mouseY = mouseY+17; }
//else if (state.toUpperCase() == "ZOOMOUT") { mouseX = mouseX+17; mouseY = mouseY+17; }
//else { mouseX = mouseX; mouseY = mouseY; }

	mouseX = mouseX; mouseY = mouseY; 

}	

// convert mouse click xy's into map coordinates
function getMapXY(xIn,yIn) {

	mouseX = xIn;
	var pixelX = (maxx-minx) / iWidth;
	mapX = pixelX * mouseX + minx;
	mapX = doRound100( mapX );
	mouseY = iHeight - yIn;
	var pixelY = (maxy-miny) / iHeight;
	mapY = pixelY * mouseY + miny;
	mapY = doRound100( mapY );
	//alert("mapx:" + mapX + " mapy:" + mapY );
}

// get the coords at mouse position
function getMouse(e) {

var mLat;
mLat = top.frames.InputFrame;

  var dmx, dmy;
  var dmxStr, dmyStr;
  var mScale;
  var hemStrLat, hemStrLong;
  var coordsArr; //= new Array(2);

	//window.status="";
	getImageXY(e);
	if ( (mouseX >= 0) && (mouseX <= iWidth) && (mouseY >= 0) && (mouseY <= iHeight))
	{
		getMapXY(mouseX,mouseY);
		coordsArr = Convert_Miller_To_Geodetic( mapX, mapY );
		dmx = coordsArr[0];
		dmy = coordsArr[1];
		mouseY = iHeight - mouseY; //reverse mouseY modified by getMapXY function
		if ( dmx >= 0 ) hemStrLong = " E"; else hemStrLong = " W";
		dmxStr = formatNumber(Math.abs(dmx),2);
		if ( dmy >= 0 ) hemStrLat = " N"; else hemStrLat = " S";
		dmyStr = formatNumber(Math.abs(dmy),2);
//		mScale = ((maxx - minx)/iWidth) * 96 * 12 * 2.54;
//if (mLat) {
//		top.frames.InputFrame.document.getElementById('mouseLat').innerHTML = dmyStr;
//		top.frames.InputFrame.document.getElementById('mouseLong').innerHTML = dmxStr;
//		top.frames.InputFrame.document.getElementById('mouseLatHem').innerHTML = hemStrLat;
//		top.frames.InputFrame.document.getElementById('mouseLongHem').innerHTML = hemStrLong;
//		}
//		document.getElementById('mouseScale').innerHTML = "1:" + Math.round(mScale);
if (document.getElementById('mouseLat')) {
		document.getElementById('mouseLat').innerHTML = dmyStr+hemStrLat;
		document.getElementById('mouseLong').innerHTML = dmxStr+hemStrLong;
		}

	}
	else {
if (mLat) {
//		top.frames.InputFrame.document.getElementById('mouseLat').innerHTML = "--.--";
//		top.frames.InputFrame.document.getElementById('mouseLong').innerHTML = "--.--";
//		top.frames.InputFrame.document.getElementById('mouseLatHem').innerHTML = "-";
//		top.frames.InputFrame.document.getElementById('mouseLongHem').innerHTML = "-";
		}
//		document.getElementById('mouseScale').innerHTML = "&nbsp;";
	}
	
	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) {

state = top.document.getElementById('CurrentState').value;

	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).  
	  	
	//----- identify
	if ((state.toUpperCase() == "SELECTINFO") && 
	    (mouseX >= 0) && (mouseX <= iWidth) && 
	    (mouseY >= 0) && (mouseY <= iHeight)){
			getMapXY(mouseX,mouseY);
			infoBox(mapX,mapY);
			return false; // <-- false assures that the popup
			              //     box stays in front.
	}

	//----- selectPoint
//	if ( ( (state.toUpperCase() == "SELECTPOINT") || (state.toUpperCase() == "SELECTMULTI") ) && 
	if ( ( state.toUpperCase() == "SELECTPOINT" ) && 
	    (mouseX >= 0) && (mouseX <= iWidth) && 
	    (mouseY >= 0) && (mouseY <= iHeight))
	{
    var evShift = 0;
    if (isNav)
      evShift = e.modifiers & Event.SHIFT_MASK;
    else
      evShift = window.event.shiftKey;
    var shftStr = "";
    if ( evShift )
    	shftStr = "1";
    else
    	shftStr = "0";
    	
		getMapXY(mouseX,mouseY);
		proceedShow(iWidth,iHeight);
		top.document.location = "/hub/index.asp" + "?Cmd="+state+"&X="+mapX.toString()+"&Y="+mapY.toString()+"&Sh="+shftStr;
		return false;
	}



	//----- selectMulti
	if ( (state.toUpperCase() == "____SELECTMULTI") && 
	    (mouseX >= 0) && (mouseX <= iWidth) && 
	    (mouseY >= 0) && (mouseY <= iHeight))
	{
    var evShift = 0;
    if (isNav)
      evShift = e.modifiers & Event.SHIFT_MASK;
    else
      evShift = window.event.shiftKey;
    var shftStr = "";
    if ( evShift )
    	shftStr = "1";
    else
    	shftStr = "0";
    	
		getMapXY(mouseX,mouseY);
		proceedShow(iWidth,iHeight);
		top.document.location = "/hub/index.asp" + "?Cmd="+state+"&X="+mapX.toString()+"&Y="+mapY.toString()+"&Sh="+shftStr;
		return false;
	}


	//----- drawPoint or drawLine or drawPoly
	if ( (state.toUpperCase() == "DRAWPOINT") || (state.toUpperCase() == "DRAWLINE") || (state.toUpperCase() == "DRAWPOLY") )
	  if ( (mouseX >= 0) && (mouseX <= iWidth) && (mouseY >= 0) && (mouseY <= iHeight) )
	  {
			getMapXY(mouseX,mouseY);
			proceedShow(iWidth,iHeight);
			top.document.location = "/hub/index.asp" + "?Cmd="+state+"&X="+mapX.toString()+"&Y="+mapY.toString()+"&fn=0";
			return false;
		}

	//----- drawText
	if ( (state.toUpperCase() == "DRAWTEXT") )
	  if ( (mouseX >= 0) && (mouseX <= iWidth) && (mouseY >= 0) && (mouseY <= iHeight) )
	  {
		  var f = top.frames.working_space.document.forms('theTextForm');
		  var tVal = f.textVal.value;
		  if ( tVal != "" )
		  {
				getMapXY(mouseX,mouseY);
				proceedShow(iWidth,iHeight);
				top.document.location = "/hub/index.asp" + "?Cmd="+state+"&X="+mapX.toString()+"&Y="+mapY.toString()+"&fn=0&tx="+tVal;
				return false;
			}
			else
			{
				alert("Please enter some text to draw!");
				return false;
			}
		}





	  // 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.toUpperCase() == "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("theMap",cLeft,cTop,cRight,cBottom);
	moveLayer("theMap",xMove,yMove);

	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;

		refreshMap(); 
	} else
		//customMapClick(mapX,mapY);
		customMapClick(mouseX,mouseY);
}

function refreshMap() {
state = top.document.getElementById('CurrentState').value;

  hideZoomBox();
  proceedShow(iWidth,iHeight);
  top.document.location = "/hub/index.asp" + "?Cmd="+state+
                      "&MinX="+newMinx.toString()+
                      "&MinY="+newMiny.toString()+
                      "&MaxX="+newMaxx.toString()+
                      "&MaxY="+newMaxy.toString();
  //NOTE: calling hideLayer("loadLayer") causes
  //      the "Loading" image to disappear 
  //      immediately.
}

function sendIdentify(numMapX,numMapY) {
	state = top.document.getElementById('CurrentState').value;
	top.frames.details_space.location = "identify_space.asp?Cmd="+state+"&X="+numMapX.toString()+"&Y="+numMapY.toString();
	return false;
}

function sendSelectDraw() {
	state = top.document.getElementById('CurrentState').value;
	showLayer("loadLayer");
	top.document.location = "/hub/index.asp" + "?Cmd="+state;
	return false;
}

function sendFullExtent() {
	state = top.document.getElementById('CurrentState').value;
	proceedShow(iWidth,iHeight);
	top.document.location = "/hub/index.asp" + "?Cmd="+state;
	return false;
}

// 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 pausecomp(Amount)
{
d = new Date() //today's date
while (1)
{
mill=new Date() // Date Now
diff = mill-d //difference in milliseconds
if( diff > Amount ) {break;}
}
}



function setState(newState) {

	oldState = top.document.getElementById('CurrentState').value.toUpperCase();
	state = newState;
	top.document.getElementById('CurrentState').value = state.toUpperCase();

//     if (state.toUpperCase() == "ZOOMIN") { top.frames.map_main.document.body.style.cursor = 'url(js/zoomin.cur)'; }
//else if (state.toUpperCase() == "ZOOMOUT") { top.frames.map_main.document.body.style.cursor = 'url(js/zoomin.cur)'; }
//else { top.frames.map_main.document.body.style.cursor = 'crosshair'; }


     if (state.toUpperCase() == "PAN") { top.frames.map_main.document.body.style.cursor = 'hand'; }
else { top.frames.map_main.document.body.style.cursor = 'crosshair'; }



  if (state.toUpperCase() == "ZOOMIN") {
		mapBoxAsZoom = true;
		setZoomBoxColor("#ff0000");
		setZoomBoxWidth(1);
	} else if (state.toUpperCase() == "ZOOMOUT") {
		mapBoxAsZoom = true;
		setZoomBoxColor("#ff0000");
		setZoomBoxWidth(1);
	} else if (state.toUpperCase() == "PAN") {
	} else if (state.toUpperCase() == "SELECTPOINT") {
	} else if (state.toUpperCase() == "SELECTRECT") {
		mapBoxAsZoom = true;
		setZoomBoxColor("#ff0000");
		setZoomBoxWidth(1);
	} else if (state.toUpperCase() == "SELECTMULTI") {
		mapBoxAsZoom = true;
		setZoomBoxColor("#ff0000");
		setZoomBoxWidth(1);
	}	else if (state.toUpperCase() == "SELECTDRAW") {
	}	else if (state.toUpperCase() == "DRAWLINE") {
	}	else if (state.toUpperCase() == "DRAWPOLY") {
	}	else if (state.toUpperCase() == "DRAWPOINT") {
	}	else if (state.toUpperCase() == "DRAWTEXT") {
	}
	
}

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.captureEvents(Event.KEYUP);
		document.onmousemove = getMouse;
		document.onmousedown = mapTool;
		document.onmouseup = chkMouseUp;
		document.onkeyup = chkKeyUp;
	} 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").captureEvents(Event.KEYUP);
		getLayer("theTop").onmousemove = getMouse;
		getLayer("theTop").onmousedown = mapTool;
		getLayer("theTop").onmouseup = chkMouseUp;
		getLayer("theTop").onkeyup = chkKeyUp;
	} else {
		document.onmousemove = getMouse;
		document.onmousedown = mapTool;
		document.onmouseup = chkMouseUp;
		document.onkeyup = chkKeyUp;
	}
}
	
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",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;

}

// 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 = doRound100( maxy - yOffset );
		newMaxx = doRound100( maxx - xOffset );
		newMinx = doRound100( minx - xOffset );
		newMiny = doRound100( miny - yOffset );

		// AP specific setting
		attributeUpdate = false;
		
		refreshMap();
	}
		
	return true;
	
}

// stop zoom box display... zoom in
function stopZoomBox(e) {

state = top.document.getElementById('CurrentState').value;
zooming = false;

//alert('1');

	if ( ((zmaxx < zminx+2) && (zmaxy < zminy+2)) && ((state.toUpperCase() == "ZOOMOUT") || (state.toUpperCase() == "ZOOMIN") || (state.toUpperCase() == "PAN")) ) {
//	if (1==2) {
		// if the zoom box is too small
//		alert('recenter');
		recenter(e);
	} else {

//		alert('start1');

		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.toUpperCase() == "SELECTRECT") || (state.toUpperCase() == "SELECTMULTI"))
			{
			  var evShift = 0;
			  if (isNav)
			    evShift = e.modifiers & Event.SHIFT_MASK;
			  else
			    evShift = window.event.shiftKey;
			  var shftStr = "";
			  if ( evShift )
			  	shftStr = "1";
			  else
			  	shftStr = "0";

			


			if ((newMinx < newMaxx) || (newMiny < newMaxy)) {
			selectBox("SELECTMULTI",newMinx.toString(),newMiny.toString(),newMaxx.toString(),newMaxy.toString(),"","");
			}
			else {
			getImageXY(e);
			getMapXY(mouseX,mouseY);
			selectBox("SELECTMULTI","","","","",mapX.toString(),mapY.toString());
			}
			
			  return false;
			}

			if (state.toUpperCase() == "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;
}

//-------- 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) + "px";
			layer.left	= (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) {
	  if (isNav4)
	    return(top.frames.map_main.document.layers[name]);
	  else if (isIE4) {
	  	if ( eval('top.frames.map_main.document.all.' + name) != null) {
		    layer = eval('top.frames.map_main.document.all.' + name + '.style');
		    return(layer);
		} else
			return(null);
	  } else if (is5up) {
		var theObj = top.frames.map_main.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";
	}
}

// round to hundreds fruction
function doRound100(aVal)
{
	return Math.round(aVal*100) / 100.0;
}

function SetLegend(toolname) {


if (top.frames.menu_main.document.getElementById('toollegend') && top.frames.menu_main) {


 if (toolname.toUpperCase() == 'PRODUCTSELECT') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Select a Product to impose new layer';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Select Product';
 }

 if (toolname.toUpperCase() == 'PRODUCTRESULTS') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Work with selected products';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Product Results';
 }

 if (toolname.toUpperCase() == 'SAVEDDRAWING') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Save/Load custom Drawings';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Saved Drawings';
 }

 if (toolname.toUpperCase() == 'SAVEDSELECTION') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Save/Load custom Selections';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Saved Selections';
 }

 if (toolname.toUpperCase() == 'SAVEDVIEW') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Save/Load custom Views';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Saved Views';
 }

 if (toolname.toUpperCase() == 'SEARCH') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Search for geographic objects';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Search';
 }

 if (toolname.toUpperCase() == 'ZOOMCOUNTRY') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Zoom to selected Country';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Zoom to Country';
 }

 if (toolname.toUpperCase() == 'INPUTCOORDINATES') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Input exact coordinates for Place point/Drawing';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Input Coordinates';
 }

 if (toolname.toUpperCase() == 'INPUTTEXT') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Input custom Text';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Input Text';
 }

 if (toolname.toUpperCase() == 'MANUAL') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'User\'s Tutorial';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'User\'s Tutorial';
 }

 if (toolname.toUpperCase() == 'HELP') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'About Interactive Map Catalog';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'About IMC';
 }

 if (toolname.toUpperCase() == 'SAVEMAP') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Save current view to local file';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Save Current Map';
 }

 if (toolname.toUpperCase() == 'PRINTMAP') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Print current view';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Print Current Map';
 }




 if (toolname.toUpperCase() == 'PANNORTH') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Shifts map to the North on 1/3 screen size';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Pan North';
 }

 if (toolname.toUpperCase() == 'PANNORTHWEST') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Shifts map to the North-West on 1/3 screen size';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Pan North-West';
 }

 if (toolname.toUpperCase() == 'PANNORTHEAST') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Shifts map to the North-East on 1/3 screen size';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Pan North-East';
 }

 if (toolname.toUpperCase() == 'PANEAST') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Shifts map to the East on 1/3 screen size';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Pan East';
 }

 if (toolname.toUpperCase() == 'PANWEST') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Shifts map to the West on 1/3 screen size';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Pan West';
 }

 if (toolname.toUpperCase() == 'PANSOUTH') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Shifts map to the South on 1/3 screen size';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Pan South';
 }

 if (toolname.toUpperCase() == 'PANSOUTHEAST') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Shifts map to the South-East on 1/3 screen size';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Pan South-East';
 }

 if (toolname.toUpperCase() == 'PANSOUTHWEST') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Shifts map to the South-West on 1/3 screen size';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Pan South-West';
 }




 if (toolname.toUpperCase() == 'REFRESH') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Refresh current Map';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Refresh Map';
 }
 if (toolname.toUpperCase() == 'ZOOMIN') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Drag a rectangle to zoom in to a smaller area';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Zoom In';
 }
 if (toolname.toUpperCase() == 'ZOOMOUT') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Drag a rectangle to zoom out to a larger area';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Zoom Out';
 }
 if (toolname.toUpperCase() == 'PAN') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Drag the map in any direction';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Pan';
 }
 if (toolname.toUpperCase() == 'IDENTIFY') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Display the item metadata';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Display Info';
 }
 if (toolname.toUpperCase() == 'ZOOMBACK') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Display the previous map extents';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Zoom to previous';
 }
 if (toolname.toUpperCase() == 'ZOOMNEXT') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Display the next map extents';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Zoom forward';
 }
 if (toolname.toUpperCase() == 'FULLEXTENT') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Display the entire map';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Full Extent';
 }
 
 if (toolname.toUpperCase() == 'SELECTPOINT') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Add products by selected point';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Select by Point';
 }

 if (toolname.toUpperCase() == 'SELECTRECT') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Add products by selected rectangle';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Select by Rectangle';
 }

 if (toolname.toUpperCase() == 'SELECTMULTI') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Add products by selected point/rectangle';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Select Items';
 }

 if (toolname.toUpperCase() == 'SELECTDRAW') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Add products by overlapped with drawing';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Select by Drawing';
 }

 if (toolname.toUpperCase() == 'DRAWPOINT') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Click on the map to place a point';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Place Custom Point';
 }

 if (toolname.toUpperCase() == 'DRAWLINE') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Click on the map to draw a polyline';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Draw Custom Polyline';
 }

 if (toolname.toUpperCase() == 'DRAWPOLY') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Click on the map to draw a polygon';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Draw Custom Polygon';
 }

 if (toolname.toUpperCase() == 'DRAWTEXT') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Click on the map to place a text';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Place Custom Text';
 }
 
 if (toolname.toUpperCase() == 'CREATEBUFFER') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Click on the map to create buffer';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Create Buffer';
 }

 if (toolname.toUpperCase() == 'CLEAR') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Clear current selection';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Clear Selection';
 }

 if (toolname.toUpperCase() == 'CLEARDRAW') {
  top.frames.menu_main.document.getElementById('toollegend').innerHTML = 'Clear current Custom Drawing.';
  top.frames.menu_main.document.getElementById('toolname').innerHTML = 'Clear Custom Drawing';
 }

  top.frames.menu_main.document.getElementById('toollegend').innerHTML = '&nbsp;&nbsp;' + top.frames.menu_main.document.getElementById('toollegend').innerHTML;
  top.frames.menu_main.document.getElementById('toolname').innerHTML = top.frames.menu_main.document.getElementById('toolname').innerHTML + '&nbsp;&nbsp;';

 }	//exists


}


function SetLegendToState() {
if (top.document.getElementById('CurrentState')) {
 state = top.document.getElementById('CurrentState').value;
 SetLegend(state);
 }
}

function ShowState() {
state = top.document.getElementById('CurrentState').value;
 alert(state);
}

function Convert_Miller_To_Geodetic( Easting, Northing )
{
  var latitude, longitude;
  var retArray = new Array(2);
  var Ra = 6371007.1810824; /* Spherical Radius */
  var PI_OVER_2 = Math.PI / 2.0;
  var TWO_PI = 2.0 * Math.PI; 

  latitude = (5/2)*Math.atan(Math.exp(0.8*(Northing/Ra))) - (5/8)*Math.PI;
  longitude = Easting/Ra;

  if (latitude > PI_OVER_2)  /* force distorted values to 90, -90 degrees */
    latitude = PI_OVER_2;
  else if (latitude < -PI_OVER_2)
    latitude = -PI_OVER_2;

  /*if (longitude > Math.PI)
    longitude -= TWO_PI;
  if (longitude < -Math.PI)
    longitude += TWO_PI;*/

  if (longitude > Math.PI)  /* force distorted values to 180, -180 degrees */
    longitude = Math.PI;
  else if (longitude < -Math.PI)
    longitude = -Math.PI;

  retArray[0] = 180*longitude/Math.PI;
  retArray[1] = 180*latitude/Math.PI;
  return retArray;
}

function formatNumber( aValue, digitsAfterDecimal )
{
  var i;
  var tmpStr = "";
  var curDigit;
  var curDigitStr;
  var retValue = "";
  var theValue = Math.abs( aValue );
  var theSign;

  if (aValue < 0) theSign = -1; else theSign = 1; 
  for (i=digitsAfterDecimal; i>0; i--)
  {
  	curDigit = Math.floor( theValue * Math.pow( 10, i ) ) - ( Math.floor( theValue * Math.pow( 10, i-1 ) ) *10 );
  	if (curDigit == 0)
  		curDigitStr = "0";
  	else
  		curDigitStr = curDigit.toString();
  	tmpStr = curDigitStr.concat(tmpStr);
  }

  retValue = theSign*Math.floor( theValue ).toString() + ".";
  for (i=0; i< retValue.length; i++ )
  	retValue = retValue + tmpStr.charAt(i).toString();
  return retValue;
}


function sendPan(PanMethod) {
 state = top.document.getElementById('CurrentState').value;
 proceedShow(iWidth,iHeight);
 top.document.location = "/hub/index.asp" + '?cmd=' + PanMethod + '&state=' + state;
}

function hlPanOff(PanMethod) {
if (PanMethod == 'PANNORTHWEST' ) {
 document.getElementById('11').style.background = '#CFE4DC';
 document.getElementById('12').style.background = '#CFE4DC';
 document.getElementById('13').style.background = '#CFE4DC';
 }
if (PanMethod == 'PANNORTH' ) {
 document.getElementById('2').style.background = '#CFE4DC';
 }
if (PanMethod == 'PANNORTHEAST' ) {
 document.getElementById('31').style.background = '#CFE4DC';
 document.getElementById('32').style.background = '#CFE4DC';
 document.getElementById('33').style.background = '#CFE4DC';
 }
if (PanMethod == 'PANWEST' ) {
 document.getElementById('4').style.background = '#CFE4DC';
 }
if (PanMethod == 'PANEAST' ) {
 document.getElementById('5').style.background = '#CFE4DC';
 }
if (PanMethod == 'PANSOUTHWEST' ) {
 document.getElementById('61').style.background = '#CFE4DC';
 document.getElementById('62').style.background = '#CFE4DC';
 document.getElementById('63').style.background = '#CFE4DC';
 }
if (PanMethod == 'PANSOUTH' ) {
 document.getElementById('7').style.background = '#CFE4DC';
 }
if (PanMethod == 'PANSOUTHEAST' ) {
 document.getElementById('81').style.background = '#CFE4DC';
 document.getElementById('82').style.background = '#CFE4DC';
 document.getElementById('83').style.background = '#CFE4DC';
 }
}

function hlPanOn(PanMethod) {
if (PanMethod == 'PANNORTHWEST' ) {
 document.getElementById('11').style.background = '#FFFFFF';
 document.getElementById('12').style.background = '#FFFFFF';
 document.getElementById('13').style.background = '#FFFFFF';
 }
if (PanMethod == 'PANNORTH' ) {
 document.getElementById('2').style.background = '#FFFFFF';
 }
if (PanMethod == 'PANNORTHEAST' ) {
 document.getElementById('31').style.background = '#FFFFFF';
 document.getElementById('32').style.background = '#FFFFFF';
 document.getElementById('33').style.background = '#FFFFFF';
 }
if (PanMethod == 'PANWEST' ) {
 document.getElementById('4').style.background = '#FFFFFF';
 }
if (PanMethod == 'PANEAST' ) {
 document.getElementById('5').style.background = '#FFFFFF';
 }
if (PanMethod == 'PANSOUTHWEST' ) {
 document.getElementById('61').style.background = '#FFFFFF';
 document.getElementById('62').style.background = '#FFFFFF';
 document.getElementById('63').style.background = '#FFFFFF';
 }
if (PanMethod == 'PANSOUTH' ) {
 document.getElementById('7').style.background = '#FFFFFF';
 }
if (PanMethod == 'PANSOUTHEAST' ) {
 document.getElementById('81').style.background = '#FFFFFF';
 document.getElementById('82').style.background = '#FFFFFF';
 document.getElementById('83').style.background = '#FFFFFF';
 }
}


function productDetails(ObjectID, TABLEPRODUCT) {
  top.frames.details_space.document.location = 'products_details.asp?ObjectID='+ObjectID+'&TABLEPRODUCT='+TABLEPRODUCT;
}



