		var zoomBoxColor = "#FF0000";	// color of zoombox
		var iWidth = 0; 	// image width
		var iHeight = 0;	// image height
		var hspc = 0; 		// horizontal image offset
		var vspc = 0; 		// vertical image offset
		var ovBoxSize = 2; 	// Zoombox line width;
		
		var toolMode = 0;
		var zooming = false;
		var panning =false; // 
		// 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 zleft=0;
		var zright=0;
		var ztop=0;
		var zbottom=0;
		
		//change status bar
		
		var mapX=0;
		var mapY=0;
		var uX;
		var uY;
        
        var leftButton =1;
        var rightButton = 2;
        if (isNav) {
        	leftButton = 1;
        	rightButton = 3;
        }
        
        var printWin = null;
        var extWin = null;

		var statusbarD="DMS";
		

function startUpMF() {
	if (isNav) {
		document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
	} 
	document.onmousedown = mapTool;
	document.onmouseup = chkMouseUp;
	document.onmousemove = getMouse;
    //start map processing
    //alert("Loaded MapFrame");
    //parent.postFrame.document.location = "processMap.jsp?tool=0";
    showLayer("LoadMap");
    parent.postFrame.location = "post.html";  //onload of post.html will submit form to processMap.jsp
}



function sendRequest(){
    parent.loaded = false;
    showLayer("LoadMap");
	//getLayerString();
	//parent.postFrame.document.forms[0].activeLayer.value=activeLyrId;
	//alert(parent.postFrame.document.forms[0].layers.value);
	parent.postFrame.document.forms[0].submit();
}



//*************************************************************************************
//*************************************************************************************
// *****************************mapTool*********************************************	
function mapTool (e) {
    if(parent.loaded) {
    	var theButton= 0;
    	// get the button pushed... if right, ignore... let browser do the popup... it will anyway
    	if (isNav) {
    		theButton = e.which;
    	} else {
    		theButton =window.event.button;
    	}	
    	if (theButton==leftButton) {
    		getImageXY(e);
    		if ((mouseX>=0) && (mouseX<iWidth) && (mouseY>=0) && (mouseY<iHeight)) {
            	switch(toolMode) {
            	case 1:
            		//zoom box
            		//need so that the user doesn't have to click the zoom to the first time---
            		parent.postFrame.document.forms[0].tool.value=toolMode;
            		startZoomBox(e);
            		return false;
            	case 2:
            		//parent.postFrame.document.forms[0].tool.value='ZOOMOUT';
            		startZoomOutBox(e);
            		return false;
            	case 3:
            		return false;
            	case 4:
            		startPan(e);
            		return false;
            	case 5:
            		startRecenter(e);
            		return false;
            	case 6:
            		return false;
            	
            	default:
            		//startZoomBox(e);
            		return false;
            	}
            }
        }
        return false;
    }
}

//*************************************************************************************
//*************************************************************************************
// *****************************chkMouseUp*********************************************
function chkMouseUp(e) {
    if(parent.loaded) {
    	if ((toolMode == 1) && (zooming)) {
    		stopZoomBox(e);
    			//alert(x1 + '\n' + y1 + '\n' + x2 + '\n' + y2);
    			parent.postFrame.document.forms[0].x1.value=x1;
    			parent.postFrame.document.forms[0].y1.value=y1;
    			parent.postFrame.document.forms[0].x2.value=x2;
    			parent.postFrame.document.forms[0].y2.value=y2;
    			parent.postFrame.document.forms[0].tool.value='1';
    			sendRequest();
    			
    		//parent.postFrame.document.forms[0].submit();
    	}
    	
    	if ((toolMode == 2) && (zooming)) {
    		stopZoomOutBox(e);
    		//alert(x1 + '\n' + y1 + '\n' + x2 + '\n' + y2);
    		//zooming=false;
    		
    			parent.postFrame.document.forms[0].x1.value=x1;
    			parent.postFrame.document.forms[0].y1.value=y1;
    			parent.postFrame.document.forms[0].x2.value=x2;
    			parent.postFrame.document.forms[0].y2.value=y2;
    			parent.postFrame.document.forms[0].tool.value='2';
    		
    		
    		sendRequest();
    	}
    	//pan
    	if ((toolMode == 4) && (panning)) {
    			stopPan(e);
    			
    		
    		    parent.postFrame.document.forms[0].x1.value=x1;
    			parent.postFrame.document.forms[0].y1.value=y1;
    			parent.postFrame.document.forms[0].x2.value=x2;
    			parent.postFrame.document.forms[0].y2.value=y2;
    			parent.postFrame.document.forms[0].tool.value='4';
    			
    			
    		sendRequest();
    		}
    	
    	//recenter, get_coord, or identify, or measure
    	if ((toolMode == 5) || (toolMode == 6)) {
    		if ((mouseX>iWidth) || (mouseY>iHeight) || (mouseX<=0) ||(mouseY<=0)) {
    			return false;
    		}
    		else{
    			
    			parent.postFrame.document.forms[0].x1.value=mouseX;
    			parent.postFrame.document.forms[0].y1.value=mouseY;
    			//parent.postFrame.document.forms[0].submit();
    			sendRequest();
    		}
    	}
    	//}
    	
    	return false;
    }
}

//*************************************************************************************
//*************************************************************************************
//***********************************getMouse*****************************************
// get the coords at mouse position
function getMouse(e) {
    if(parent.loaded) {
    	window.status="";
    	getImageXY(e);
        if ((mouseX>iWidth) || (mouseY>iHeight) || (mouseX<=0) || (mouseY<=0)) {
            chkMouseUp(e);
    	} else {
    		if (zooming) {
    			x2=mouseX;
    			y2=mouseY;
    			setClip();
    		} else if (panning) {
    			x2=mouseX;
    			y2=mouseY;
    			panMouse();	
    		}
    		var mouseString = "";
    		//mouseString += mouseX +","+mouseY;
            //if (zooming) mouseString += "ZoomBox: [" + (x1) + "," + (y1) + " x " + (x2) + "," + (y2) + "]    ";
    		//mouseString += "Coords: " + mouseX + " , " + mouseY;	
    		getMapunits(mouseX, mouseY);
            if (parent.postFrame.document.forms[0].coordSys.value == "26915") {
                ToGeographic(mapX, mapY, 15);
                //global var latlon[] set by ToGeographic() contains latitude(y) first and then longitude(x)
                mapY = latlon[0];
                mapX = latlon[1];
            }
    		var dLat = trunc(mapY,0);
    		var dLatMin = trunc((mapY - dLat)*60,0);
    		var dLatSec = trunc(((mapY - dLat)*60 - dLatMin)*60,0);
            //use tmpMapX so mapX stays unchanged
    		var tmpMapX = mapX * -1;
    		var dLon = trunc(tmpMapX,0);
    		var dLonMin = trunc((tmpMapX - dLon)*60,0);
    		var dLonSec = trunc(((tmpMapX - dLon)*60 - dLonMin)*60,0);
    		mouseString ="";
    		mouseString += 'Latitude: ' + dLat + "\u00B0" + ' ' + dLatMin + "\u0027" + ' ' + dLatSec + "\u0022"+" N  ";
    		mouseString += 'Longitude: ' + dLon + "\u00B0" + ' ' + dLonMin + "\u0027" + ' ' + dLonSec + "\u0022"+" W";
    		//mouseString += mouseX+","+mouseY+"/"+mapX + "," + mapY;
    		window.status = mouseString;
    	}
    	// next line needed for Mac
    	return false;
    }
}

function getMapunits(mouseX, mouseY){
    //alert(parseInt(parent.postFrame.document.forms[0].lEnv_x1.value));
    var SLeft = -94.3393448520412;
    var SRight = -88.6488381433421;
    var STop = 33.0508901022964;
    var SBottom = 28.8064415689844;
    if(parent.postFrame.document.forms[0]!=null) {
        SLeft = parseFloat(parent.postFrame.document.forms[0].minx.value);
        SRight = parseFloat(parent.postFrame.document.forms[0].maxx.value);
        STop = parseFloat(parent.postFrame.document.forms[0].maxy.value);
        SBottom = parseFloat(parent.postFrame.document.forms[0].miny.value);
    }
    var pixelX = (SRight-SLeft) / iWidth;
    mapX = (pixelX * mouseX) + SLeft;
    var pixelY = (STop - SBottom) / iHeight;
    mapY = (pixelY * (iHeight - mouseY)) + SBottom;

    return true;
}

function trunc(a, b) {
 a *= Math.pow(10, b);
 a = Math.floor(a);
 a /=  Math.pow(10, b);
 return a;
}

function clickFunction(f){
    //parent.bottomFrame.document.location="bottomFrame.html";
    if(parent.loaded) {
		if (isIE)	{
				parent.mapFrame.document.all.map.style.cursor = "crosshair";
			}
		var pForm = parent.postFrame.document.forms[0];
		
		
		switch (f){
    		case 'zoomin':
    			toolMode=1;
    			pForm.tool.value=toolMode;
    			break;
    		case 'zoomout':
    			toolMode=2;
    			pForm.tool.value=toolMode;
    			break;
    		case 'fullextent':
    			pForm.tool.value=3;
    			sendRequest();
    			break;
    			// Changed pan to recenter
    		case 'pan':
    			toolMode=4;
    			zooming=false;
    			if (isIE)	{
    				parent.mapFrame.document.all.map.style.cursor = "move";
    			}
    			pForm.tool.value=toolMode;
    			
    			break;
    		case 'recenter':
    			toolMode=5;
    			pForm.tool.value=toolMode;
    			break;
    		
    		case 'setcoord':
    			//pForm.tool.value=6;
    			//sendRequest();
    			break;
    		
    		case 'clear':
    			//clear all acetate layers
    			pForm.tool.value=11;
    			sendRequest();
    			break;
    
    		case 'nyi':
    			alert('Tool not implemented...');
    			toolMode=1;
    			break;
    		case 'geocode':
    			//parent.bottomFrame.location = "geocodeForm.html";
    		break;
    		case 'showmap':
    		//alert(parent.postFrame.document.forms[0].showdoqq.value);
    		//parent.postFrame.document.forms[0].showdoqq.value="true";
    		//sendRequest();
    		break;
    	}
	}
}

function startRecenter(e){
	getImageXY(e);
}
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;
}

// stop moving image.... pan 
function stopPan(e) {
	window.scrollTo(0,0);
	panning=false;
	
	showLayer("theMap");
	return true;			
}

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;
}



// start zoom in.... box displayed
function startZoomBox(e) {
	moveLayer("map",hspc,vspc);
	getImageXY(e);	
	// keep it within the MapImage
	if ((mouseX<iWidth) && (mouseY<iHeight)) {
		if (zooming) {
			stopZoomBox(e);
		} else {
			//clear the box
			hideLayer("zoomBoxTop");
			hideLayer("zoomBoxLeft");
			hideLayer("zoomBoxRight");
			hideLayer("zoomBoxBottom");
			x1=mouseX;
			y1=mouseY;
			//x1=mouseX+hspc;
			//y1=mouseY+vspc;
			x2=x1+1;
			y2=y1+1;
			zleft=x1;
			ztop=y1;
			zbottom=y1;
			zright=x1;
			
			boxIt(x1,y1,x2,y2);
			zooming=true;
		}
	}
	return false;
}

// stop zoom box display... 
function stopZoomBox(e) {
	zooming=false;
	
	hideLayer("zoomBoxTop");
	hideLayer("zoomBoxLeft");
	hideLayer("zoomBoxRight");
	hideLayer("zoomBoxBottom");
	window.scrollTo(0,0);
	
	return true;
}

function startZoomOutBox(e) {
	moveLayer("map",hspc,vspc);
	//gets cursor location
	getImageXY(e);
	// keep it within the MapImage
	if ((mouseX<iWidth) && (mouseY<iHeight)) {
		
		if (zooming) {
			stopZoomOutBox(e);
		} else {
			x1=mouseX;
			y1=mouseY;
			x2=x1+1;
			y2=y1+1;
			zleft=x1;
			ztop=y1;
			zbottom=y1;
			zright=x1
			boxIt(x1,y1,x2,y2);
			/*
			clipLayer("zoomBoxTop",x1,y1,x2,y2);
			clipLayer("zoomBoxLeft",x1,y1,x2,y2);
			clipLayer("zoomBoxRight",x1,y1,x2,y2);
			clipLayer("zoomBoxBottom",x1,y1,x2,y2);
			*/
			zooming=true;
			//showLayer("zoomBoxTop");
			//showLayer("zoomBoxLeft");
			//showLayer("zoomBoxRight");
			//showLayer("zoomBoxBottom");
		}
	//} else {
	//	if (zooming) stopZoomOutBox(e);
	}
	return false;
	
}
function zoomout(e) {

	hideLayer("zoomBoxTop");
	hideLayer("zoomBoxLeft");
	hideLayer("zoomBoxRight");
	hideLayer("zoomBoxBottom");
	window.scrollTo(0,0);
	
	return true;
}

function stopZoomOutBox(e) {
	zooming=false;
	//alert('zright ' + zright + '\nzleft ' + zleft);
	//check for point click
	
	if ((zright <zleft+2) && (zbottom < ztop+2)) {
		zoomout(e);
	} else {

		window.scrollTo(0,0);
	
		parent.postFrame.document.forms[0].x1.value=zleft;
		parent.postFrame.document.forms[0].x2.value=zright;
		parent.postFrame.document.forms[0].y2.value=ztop;
		parent.postFrame.document.forms[0].y1.value=zbottom;
		
		hideLayer("zoomBoxTop");
		hideLayer("zoomBoxLeft");
		hideLayer("zoomBoxRight");
		hideLayer("zoomBoxBottom");
	
		return true;
	}
}

/*
***************************************************************************************

Navigation functions - used to resize zoom box
	Extended versions of these functions are used by the ArcIMS HTML Viewer - found in aimsNavigation.js

***************************************************************************************
*/

// get cursor location
function getImageXY(e) {
	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 top
	mouseX = mouseX-hspc;
	mouseY = mouseY-vspc;		
}	

		
// clip zoom box layer to mouse coords
function setClip() {	
	var tempX=x1;
	var tempY=y1;
	if (x1>x2) {
		zright=x1;
		zleft=x2;
	} else {
		zleft=x1;
		zright=x2;
	}
	if (y1>y2) {
		zbottom=y1;
		ztop=y2;
	} else {
		ztop=y1;
		zbottom=y2;
	}
	
	if ((x1 != x2) && (y1 != y2)) {
		boxIt(zleft,ztop,zright,zbottom);
	}
	//return false;
}

/*
***************************************************************************************

Click/Mouse functions - used to catch mouse button events
	Extended versions of these functions are used by the ArcIMS HTML Viewer - found in aimsClick.js

***************************************************************************************
*/


// pan using arrow buttons
function panButton(panType) {
    if(parent.loaded){
        var pForm = parent.postFrame.document.forms[0];
        pForm.tool.value="17";
        pForm.pandirection.value=panType;
        sendRequest();
    }
}


function submitGeocode() {
    if(parent.loaded){
        var gForm = parent.locateFrame.document.forms[0];
        var pForm = parent.postFrame.document.forms[0];
        // var gcMethod = gForm.gcMethod.value;
        // pForm.tool.value = "30";
        pForm.tool.value ="6";
        
        pForm.gcAddress.value = gForm.gcAddress.value;
        pForm.gcCrossStreet.value = gForm.gcCrossStreet.value;
        pForm.gcCity.value = gForm.gcCity.value;
        pForm.gcZip.value = gForm.gcZip.value;
       
        sendRequest();
    }
}

function zoomToPoint(stringIn) {
    var pForm = parent.postFrame.document.forms[0];
    pForm.tool.value = "31";
    pForm.zoomtoString.value = stringIn;
    pForm.submit();
    sendRequest();
}

function zoomToParish(parish) {
   if(parent.loaded){
    var pForm = parent.postFrame.document.forms[0];
    pForm.tool.value = "20";
    pForm.parish.value = parish;
    sendRequest();
   }
}

function closeWindow(windowIn) {
    if ((windowIn && windowIn.open) && !windowIn.closed) {
        windowIn.close();
    }
}

function closeWindows() {
    closeWindow(extWin);
    closeWindow(printWin);
}

function openExtWin(contentIn) {
    closeWindow(extWin);
    var winfeatures = 'scrollbars,resizable,screenX=20,screenY=20,top=20,left=20,width=300,height=400';
    extWin = window.open('','extWin',winfeatures);
    //var extWin = parent.TextFrame;
    extWin.document.open();
    extWin.document.write(contentIn);
    extWin.document.close();
    extWin.focus();
}

function openPrintWin() {
    if(parent.loaded){
        closeWindow(printWin);
        var winfeatures = 'menubar,scrollbars,resizable,screenX=20,screenY=20,top=20,left=20';
        var type = parent.postFrame.document.forms[0].showtype.value;
        var zoomtoString = parent.postFrame.document.forms[0].zoomtoString.value;
        var setCoords = parent.postFrame.document.forms[0].setCoords.value;
        var showFeatures = parent.postFrame.document.forms[0].showFeatures.value;
        var minx =  parent.postFrame.document.forms[0].minx.value;
        var maxx =  parent.postFrame.document.forms[0].maxx.value;
        var miny =  parent.postFrame.document.forms[0].miny.value;
        var maxy =  parent.postFrame.document.forms[0].maxy.value;
        var coordSys =  parent.postFrame.document.forms[0].coordSys.value;
        
        //The add ons are in case the session expires
        var sendString = "print.jsp?showtype="+type+"&showFeatures="+showFeatures+"&zoomtoString="+zoomtoString+"&setCoords="+setCoords+"&minx="+minx+"&maxx="+maxx+"&miny="+miny+"&maxy="+maxy+"&coordSys="+coordSys;
        
        
        printWin = window.open(sendString,'printWin',winfeatures);
        showLayer("LoadMap");
        window.focus();
        //printWin = window.open('printForm.html','printWin',winfeatures);
        //printWin.focus();
   }
}

function writeResultsFrame(contentIn) {
    var rFrame = parent.resultsFrame;
    rFrame.document.open();
    rFrame.document.write(contentIn);
    rFrame.document.close();
}

function tabFunction(type){
    if(parent.loaded){
    	if(type=="topo"){
            parent.toolFrame.showFeatures = false;
            parent.postFrame.document.forms[0].showFeatures.value = false; 
            parent.toolFrame.document.features.src = "../images/features.gif";
        }
    	if(type=="doqq") {
            parent.toolFrame.showFeatures = true;
            parent.postFrame.document.forms[0].showFeatures.value = true; 
            parent.toolFrame.document.features.src = "../images/features_f3.gif";
        }
        parent.postFrame.document.forms[0].showtype.value=type;
        parent.postFrame.document.forms[0].tool.value="999";
        sendRequest();
    }
}

function zoomLevel(level) {
    if(parent.loaded){
        var pForm = parent.postFrame.document.forms[0];
        pForm.tool.value="32";
        pForm.zoomlevel.value=level;
        sendRequest();
    }
}

function setCoord(inX,inY,coordchoice){
    if(parent.loaded){
        var rX = inX;
        var rY = inY;
        if(coordchoice=="1"){
            ToGeographic(inX, inY, 15);
            //global var latlon[] set by ToGeographic() contains latitude(y) first and then longitude(x)
            rY = latlon[0];
            rX = latlon[1];
        }
        parent.postFrame.document.forms[0].x1.value=rX;
        parent.postFrame.document.forms[0].y1.value=rY;
               
        parent.postFrame.document.forms[0].tool.value ="7";
        sendRequest();
    }
}
