// ----------------------------------------------------------------
//     				   ArcWeb Services Site Starter Client
// ----------------------------------------------------------------
// Purpose:  Manages "active" navigation functionality.  
//           + Manages map coordinate logic.
//           + Uses DHTML to produce rubber-banding effect 
//             when user zooms in or out.  This is handled
//             by the zoomBox class.
// ----------------------------------------------------------------
// Notes:		
// -------------------------------------------------------------------

// The following variables are global to the page.
	// These vars reference HTML tags in the page whose
	// values may be read or set dynamically.

    var m_radZoomIn;
    var m_radZoomOut;
    var m_radRecenter;
    var m_txtXCoord;
    var m_txtYCoord;
    var m_imgLoading;
    var m_imgMapCanvas;
    var m_imgZoomIn;
    var m_imgZoomOut;
    var m_imgPan;
    var m_imgIdentify;
    var m_imgStatewide;
    var m_divMapBorder;
    var m_divZoomBox;
	var m_divMapTools;
	var m_divNorth;
	var m_divSouth;
	var m_divEast;
	var m_divWest;
    var	m_hvMinX;
    var	m_hvMinY;
    var	m_hvMaxX;
    var	m_hvMaxY;
	var m_hvMapPage;
	var m_hvToolType;
	var m_cmd;
	var m_iWidth;
	var m_iHeight;
	var m_hvIdPage;
	var m_hvPrintTitle;
	var m_hvPrintType;
	var m_hvLegislator;
	var m_hvLegislativeDistrict;
	var eventX;
	var eventY;

	// Panning variables
	var x1;
	var y1;
	var x2;
	var y2;
	var panning = false;
	var newX;
	var newY;
	var hvPanMinX;
	var hvPanMinY;
	var hvPanMaxX;
	var hvPanMaxY;

	// These vars represent javascript classes that will
	// handle logic.
    var m_mapViewer;  // Object of type map.  Handles map coordinate management.

    var m_zbxZoom;      // Object of type zoombox.  Handles drawing the zoombox.  
				        // Operates entirely within page coordinates and has no 
				        // awareness of the map space or even the image that 
				        // represents the map on the page.

    var m_iToolMode;    // Indicates the currently selected tool, where
				        //				+ zoom in  = 1
				        //				+ recenter = 2	

	var m_sClientBrowserType;	// Possible values: "IE", "Netscape"
	var m_sClientPlatform;		// Possible values: "Windows","Mac"
    
    		           // The array that stores the level extent widths				  
	var m_ExtentWidths = new Array(0.005, 0.015, 0.035, 0.1, 0.3, 0.75, 2, 6, 10, 30, 110);

	var m_lTimerID = 1; // used only for hiding the "Wait" image for Netscape 6.x

	// Determine browser type and platform.

	window.name = "Main";
	
	if (navigator.appName.indexOf("Netscape")>=0) {
		m_sClientBrowserType = 'Netscape';
	} else if (navigator.appName.indexOf('Microsoft') >=0) {
		m_sClientBrowserType = 'IE';
	} else {
		//window.location = "unsupported.htm";
	}

	if (navigator.userAgent.indexOf("Win") >= 0) {
		m_sClientPlatform = 'Windows';
	} else if (navigator.platform.indexOf("Mac") >= 0) {
		m_sClientPlatform = 'Mac';
	} else {
		//window.location = "unsupported.htm";
	}
    


// *******************************************************************
// *********************** STARTUP FUNCTION **************************
// *******************************************************************


function startUp() {

// Purpose: Fires when the page first loads.  

		// Find the HTML tags that will be used throughout the
		// page.
	m_imgZoomIn = document.getElementById("imgZoomIn");
	m_imgZoomOut = document.getElementById("imgZoomOut");
	m_imgPan = document.getElementById("imgPan");
	m_imgIdentify = document.getElementById("imgIdentify");
	m_imgStatewide = document.getElementById("imgStatewide");
	m_radZoomIn = document.getElementById("radZoomIn");
	m_radZoomOut = document.getElementById("radZoomOut");
	m_radRecenter = document.getElementById("radRecenter");
	m_txtXCoord = document.getElementById("txtXCoord");
	m_txtYCoord = document.getElementById("txtYCoord");
	m_imgMapCanvas = document.getElementById("imgMapCanvas");
    m_imgLoading = document.getElementById("imgLoading");
	m_divZoomBox = document.getElementById("divZoomBox");
	m_divMapBorder = document.getElementById("divMapBorder");
  	m_divNorth = document.getElementById("divNorth");
  	m_divSouth = document.getElementById("divSouth");
  	m_divEast = document.getElementById("divEast");
  	m_divWest = document.getElementById("divWest");
  	m_divMapTools = document.getElementById("divMapTools");
	m_hvMinX = document.getElementById("hvMinX");
	m_hvMinY = document.getElementById("hvMinY");
	m_hvMaxX = document.getElementById("hvMaxX");
	m_hvMaxY = document.getElementById("hvMaxY");
	m_hvMapPage = document.getElementById("hvMapPage");
	m_hvIdPage = document.getElementById("hvIdPage");
	m_hvToolType = document.getElementById("hvToolType");
	m_cmd = document.getElementById("Cmd");
	m_iWidth = document.getElementById("iWidth");
	m_iHeight = document.getElementById("iHeight");
	m_hvPrintTitle = document.getElementById("hvPrintTitle");
	m_hvPrintType = document.getElementById("hvPrintType");
	m_hvLegislator = document.getElementById("hvLegislator");
	m_hvLegislativeDistrict = document.getElementById("hvLegislativeDistrict");
	hvPanMinX = document.getElementById("hvPanMinX");
	hvPanMinY = document.getElementById("hvPanMinY");
	hvPanMaxX = document.getElementById("hvPanMaxX");
	hvPanMaxY = document.getElementById("hvPanMaxY");
	
	// detect whether or not pan is being used
/*	if (location.search.length > 1){
		// check for the extended URL value
		var panloc = new String(window.location);
		// XMIN
		var panXMINArray = panloc.split("XMIN=");
		var panxmin = panXMINArray[1];
		panxmin = panxmin.replace('&','');
		panxmin = panxmin.substr(0, 15);
		hvPanMinX.value=panxmin;
		// YMIN
		var panYMINArray = panloc.split("YMIN=");
		var panymin = panYMINArray[1];
		panymin = panymin.replace('&','');
		panymin = panymin.substr(0, 15);
		hvPanMinY.value=panymin;
		
		// XMAX
		var panXMAXArray = panloc.split("XMAX=");
		var panxmax = panXMAXArray[1];
		panxmax = panxmax.replace('&','');
		panxmax = panxmax.substr(0, 15);
		hvPanMaxX.value=panxmax;
		
		// YMAX
		var panYMAXArray = panloc.split("YMAX=");
		var panymax = panYMAXArray[1];
		panymax = panymax.replace('&','');
		panymax = panymax.substr(0, 15);
		hvPanMaxY.value=panymax;
	}
*/
	// Create a map coordinate manager.
	if (hvPanMinX.value == "") {
	m_mapViewer = new map(m_imgMapCanvas.offsetLeft,
  						  m_imgMapCanvas.offsetTop,
						  m_imgMapCanvas.width,	
						  m_imgMapCanvas.height,
						  m_ExtentWidths,
						  new rect(m_hvMinX.value, m_hvMinY.value, m_hvMaxX.value, m_hvMaxY.value)
						  );
	}
	else
	{
	m_mapViewer = new map(m_imgMapCanvas.offsetLeft,
  						  m_imgMapCanvas.offsetTop,
						  m_imgMapCanvas.width,	
						  m_imgMapCanvas.height,
						  m_ExtentWidths,
						  new rect(hvPanMinX.value, hvPanMinY.value, hvPanMaxX.value, hvPanMaxY.value)
						  );
	}
	
	
	// Create a zoom box.
    m_zbxZoom = new zoomBox(m_divZoomBox);
   
        //Orient other page elements - Map Border, Map Tools, Loading Image, Border Navigation.
    posLoadingImage();
    posBorder();
    posBorderNavigation(); //must be called BEFORE posTools()
    posTools();  //must be called AFTER posBorderNavigation()  

		// Initialize the tool mode. 
	handleToolClick(returnActiveTool());
		
	    // Initialize the X,Y coordinate display.
	m_txtXCoord.value = "X:"
	m_txtYCoord.value = "Y:"

		// Assign custom event handlers to mousedown, mousemove, 
		// and mouseup.
	if (m_sClientBrowserType == 'Netscape') {
		document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
	}
	document.onmousedown = handleMouseDown;
	document.onmousemove = handleMouseMove;
	document.onmouseup = handleMouseUp;
	
	
	// Assign an event handler to the m_imgMapCanvas onload event.
	m_imgMapCanvas.onload = hideWaitImage;
	
	    //Request a map.
	submit();
}

function startList() {
if (document.all&&document.getElementById) {
navRoot = document.getElementById("nav");
for (i=0; i<navRoot.childNodes.length; i++) {
node = navRoot.childNodes[i];
if (node.nodeName=="LI") {
node.onmouseover=function() {
this.className+=" over";
  }
  node.onmouseout=function() {
  this.className=this.className.replace(" over", "");
   }
   }
  }
 }
}

// *******************************************************************
// *********************** EVENT HANDLERS  ***************************
// *******************************************************************

function handleMouseDown(evt) {
	// Get event coordinates by browser-specific means.

	if (m_sClientBrowserType == 'Netscape') {
		eventX = evt.clientX;
		eventY = evt.clientY;
	} else {
		eventX = event.clientX + document.body.scrollLeft;
		eventY = event.clientY + document.body.scrollTop ;
	}

	/* 
		TO USE WITH DYNAMIC PAN
	*/
	x1 = eventX;
	y1 = eventY;
	
	// Check to see if click is over map
	if (!(m_mapViewer.isEventOverMap(eventX, eventY))) {	
		return;
	}
	switch(m_iToolMode) {
		case 1: // zoom in mode
			if ((m_sClientBrowserType == 'IE') && (m_sClientPlatform == 'Windows')) {
	  		// For IE, a call to "setCapture" is made to ensure that
				// new IE dragging events do not interfere with capturing 
				// the mousemove events -- effectively, this turns off 
				// drag and drop events temporarily.
				m_imgMapCanvas.setCapture(); 
			}
			m_zbxZoom.show();
			//alert(eventY);
			//alert(document.body.scrollTop);
			//alert(this.topMargin);
			m_zbxZoom.start(eventX, eventY);
			break;
		case 2: // recenter mode
  			var ptUserClick = m_mapViewer.toMapPoint(eventX, eventY);
			m_mapViewer.recenter(ptUserClick);
			//Request a new map.
			submit();
			break;
		case 3: // zoom out mode
			if ((m_sClientBrowserType == 'IE') && (m_sClientPlatform == 'Windows')) {
	  		// For IE, a call to "setCapture" is made to ensure that
				// new IE dragging events do not interfere with capturing 
				// the mousemove events -- effectively, this turns off 
				// drag and drop events temporarily.
				m_imgMapCanvas.setCapture(); 
			}
			m_zbxZoom.show();
			m_zbxZoom.start(eventX, eventY);
			break;
		case 4: // pan
			m_divMapBorder.panner = new Pan(m_imgMapCanvas);
			m_divMapBorder.panner.processEvent(event);
			break;
		case 5: // identify mode
  			var ptUserClick = m_mapViewer.toMapPoint(eventX, eventY);
			//m_mapViewer.recenter(ptUserClick);
			//Request a new map.
			submit();
			break;
	}
}

function handleMouseMove(evt) {
// Purpose:  Any time the mouse moves anywhere on the document, 
//           this function is given the opportunity to respond to 
//           the event.

	// Get event coordinates by browser-specific means.

	var eventX;
	var eventY;

	if (m_sClientBrowserType == 'Netscape') {
		eventX = evt.clientX;
		eventY = evt.clientY;
	} else {
		eventX = event.clientX + document.body.scrollLeft;
		eventY = event.clientY + document.body.scrollTop ;
	}

		// Rey - added keep track of secondary mouse location
	x2 = eventX;
	y2 = eventY;

    // If the mouse isn't currently over the map, we
    // have nothing to do.
	if (!(m_mapViewer.isEventOverMap(eventX, eventY))) {	
		return;
	} 		
		/* Rey - hide this 6/15/05
		reportCoords(m_mapViewer.toMapPoint(eventX, eventY)); // update the textbox to reflect
		*/																																						 // the current coordinates.
    // If the current tool is zoom in and a zoombox is in progress, update
    // the zoom box.
  if (m_iToolMode == 1 || m_iToolMode == 3) {
		if (m_zbxZoom.isInProgress) {
			m_zbxZoom.update(eventX, eventY);
		}
  }
  // Rey - added the "if (m_iToolMode == 4)"
  // Pan Mode
  if (m_iToolMode == 4) {
	if (m_divMapBorder.panner)
   {
      m_divMapBorder.panner.processEvent(event);
   }
	return false;
  }
}

function handleMouseUp(evt) {
	
	// WHAT TO DO DURING PAN

	if (m_iToolMode == 4)
		{
		// Rey - JUST PAN
		if (!(x1 == x2))
		{ 
			var zmaxy;
			if (y1 > y2)
			{
				zmaxy = y1;
			}
			else
			{
				zmaxy = y2;
			}
			var iWidth = m_mapViewer.getTagWidth();
			var iHeight = m_mapViewer.getTagHeight();
			var width = Math.abs(m_hvMaxX.value - m_hvMinX.value);
			var height = Math.abs(m_hvMaxY.value - m_hvMinY.value);
			var iXoffSet = x2 - x1;
			var iYoffSet = y1 - y2;
			var pixelX = width / iWidth;
			var pixelY = height / iHeight;
			var theY = iHeight - zmaxy;
			var xOffSet = pixelX * iXoffSet;
			var yOffSet = pixelY * iYoffSet;
			var newMaxX = m_hvMaxX.value - xOffSet;
			var newMaxY = m_hvMaxY.value - yOffSet;
			var newMinX = m_hvMinX.value - xOffSet;
			var newMinY = m_hvMinY.value - yOffSet;
			
			m_mapViewer.setExtent(new rect(newMinX,newMinY,newMaxX,newMaxY));
		   
			//m_imgMapCanvas.style.position = "absolute";
			m_imgMapCanvas.style.pixelleft = 110;
			m_imgMapCanvas.style.pixeltop = 0;
			m_imgMapCanvas.style.pixelwidth = m_mapViewer.getTagWidth();
			m_imgMapCanvas.style.pixelheight = m_mapViewer.getTagHeight();
		    
		    
			// Create a new map coordinate manager.
			submit();
		}
			
	}	
		// If a ZoomBox is not in progress, we've got nothing
		// to do.
	if (!(m_iToolMode == 1) && !(m_iToolMode == 3)) {

		return;
	}
	if (!(m_zbxZoom.isInProgress())) {
		return;
	}
		// Release the capture that was set in the mousedown
		// handler (again, this is something that we have to do because
		// of drag events in IE).
	if ((m_sClientBrowserType == 'IE') && (m_sClientPlatform == 'Windows')) {
		document.releaseCapture();
	}

		// Stop the current effect.
	m_zbxZoom.stop();

		// If the width of the zoom box is less than five pixels,
		// treat is as a single click and zoom in one map level.
		// Otherwise, set the extent to the zoom box.
	if ((Math.abs(m_zbxZoom.getStartX() - m_zbxZoom.getEndX()) < 5) && (Math.abs(m_zbxZoom.getStartY() - m_zbxZoom.getEndY()) < 5))  {	
		var ptUserClick = m_mapViewer.toMapPoint(m_zbxZoom.getStartX(), m_zbxZoom.getStartY());
		
		alert("Zoom area required. Click and drag the area to zoom into. \n Click on 'Help' to learn how to draw a zoom box.");
		return;
		/*
		if (m_mapViewer.getLevel() == 1) {   //if we reach the minimum zoom level, zoom a fixed amount.
		
		  m_mapViewer.zoom(1.0);
		} else {
			m_mapViewer.setLevel(m_mapViewer.getLevel() - 1);
     }
     */
		// Modified by Rey
	//	switch(m_iToolMode){
	//		case 1:
	//		m_mapViewer.recenter(ptUserClick);		
	//		m_mapViewer.zoom(0.5);
	//		m_imgLoading.style.visibility = 'visible';					
	//		break;
	//		case 3:
	//		m_mapViewer.recenter(ptUserClick);		
	//		m_mapViewer.zoom(-0.5);
			
	//		break;

	//	} 
		
  } else {
  		borderWidth = 0;
  		if (m_sClientBrowserType == 'Netscape') {
			borderWidth = 4; // 2 times border width
		}
	// Handle zoom in and out
		if (m_iToolMode == 1){
			var ptStart = m_mapViewer.toMapPoint(m_zbxZoom.getStartX()+borderWidth, m_zbxZoom.getStartY()+borderWidth);
			var ptEnd   = m_mapViewer.toMapPoint(m_zbxZoom.getEndX()+borderWidth, m_zbxZoom.getEndY()+borderWidth);
			m_mapViewer.setExtent(new rect(ptStart.x,ptStart.y,ptEnd.x,ptEnd.y));
		}
		else if (m_iToolMode == 3){
			var zoomOutFactor;
			zoomOutFactor = 300;
			var ptStart = m_mapViewer.toMapPoint(m_zbxZoom.getStartX()+(borderWidth-zoomOutFactor), m_zbxZoom.getStartY()+(borderWidth-zoomOutFactor));
			var ptEnd   = m_mapViewer.toMapPoint(m_zbxZoom.getEndX()+(borderWidth+zoomOutFactor), m_zbxZoom.getEndY()+(borderWidth+zoomOutFactor));
			m_mapViewer.setExtent(new rect(ptStart.x,ptStart.y,ptEnd.x,ptEnd.y));
		}
	}//Request a new map.
	showWaitImage();
	submit();
	
	/* Rey - USE THIS TO REDIRECT URL WHEN THEY HAVE SELECTED COORDINATES
	var urlSearchLength;
	urlSearchLength = location.search.length;
	if (urlSearchLength > 60) {
	 window.location.replace("default.aspx") ;
	}
	*/
}

function handleToolClick(iToolMode) {
	switch(iToolMode) {
		case 1:
			// zoom in mode
			m_imgMapCanvas.style.cursor = "crosshair";
			m_cmd.value="zoomin";
			m_hvToolType.value = 1;
			m_imgZoomIn.src="images/zoomin_on.gif";
			m_imgZoomOut.src="images/zoomout.gif";
			m_imgIdentify.src="images/indentify.gif";
			m_imgPan.src="images/pan.gif";
			m_imgStatewide.src="images/statewide.gif";
			break;
		case 2:
			// pan mode
			m_imgMapCanvas.style.cursor = "hand";
			m_hvToolType.value = 2;
			m_imgZoomOut.src="images/zoomout.gif";
			m_imgZoomIn.src="images/zoomin.gif";
			m_imgIdentify.src="images/indentify.gif";
			m_imgPan.src="images/pan_on.gif";
			m_imgStatewide.src="images/statewide.gif";			
			break;
		case 3:
			// zoom in mode
			m_imgMapCanvas.style.cursor = "crosshair";
			m_cmd.value="zoomout";
			m_hvToolType.value = 3;
			m_imgZoomOut.src="images/zoomout_on.gif";
			m_imgZoomIn.src="images/zoomin.gif";
			m_imgIdentify.src="images/indentify.gif";
			m_imgPan.src="images/pan.gif";
			m_imgStatewide.src="images/statewide.gif";
			break;
		case 4:
			// pan mode
			m_imgMapCanvas.style.cursor = "move";
			m_hvToolType.value = 4;
			m_imgZoomOut.src="images/zoomout.gif";
			m_imgZoomIn.src="images/zoomin.gif";
			m_imgIdentify.src="images/indentify.gif";
			m_imgPan.src="images/pan_on.gif";
			m_imgStatewide.src="images/statewide.gif";
			break;
		case 5:
			// identify mode
			m_imgMapCanvas.style.cursor = "help";
			m_cmd.value="identify";
			m_hvToolType.value = 5;
			m_imgZoomOut.src="images/zoomout.gif";
			m_imgZoomIn.src="images/zoomin.gif";
			m_imgIdentify.src="images/indentify_on.gif";
			m_imgPan.src="images/pan.gif";
			m_imgStatewide.src="images/statewide.gif";			
			break;
	}
	m_iToolMode = iToolMode;
}

function ResizeMap(sSize) {

    if (((sSize == 'SMALL') && (m_imgMapCanvas.className == 'SmallMap')) || 
        ((sSize == 'LARGE') && (m_imgMapCanvas.className == 'LargeMap'))) {
        return;
    }
    
    switch(sSize) { 
        case 'SMALL':
        m_imgMapCanvas.className = 'SmallMap';
        break;
        case 'LARGE':
        m_imgMapCanvas.className = 'LargeMap';
        break;
        default: 
        return; // invalid sSize value
    }

        // Create a new map coordinate manager.
    m_mapViewer = new map(m_imgMapCanvas.offsetLeft,
  				    m_imgMapCanvas.offsetTop,
				    m_imgMapCanvas.width,	
				    m_imgMapCanvas.height,
					m_ExtentWidths,
				    m_mapViewer.getExtent()
				    );


        //Orient other page elements - MapSizeImage, Map Border, Map Tools, Loading Image.
    switch(sSize) { 
        case 'SMALL':
        updateImgMapSize('LARGE');
        break;
        case 'LARGE':
        updateImgMapSize('SMALL');
        break;
    }
    
    posLoadingImage();
    posBorder();
    posBorderNavigation(); //must be called BEFORE posTools()
    posTools();  //must be called AFTER posBorderNavigation()

    submit();
}

function ZoomToLevel(iLevel) {           
	m_mapViewer.setLevel(iLevel);
    //Request a new map.
    submit();
}

// *******************************************************************
// ********************** HELPER FUNCTIONS  **************************
// *******************************************************************

function hideWaitImage() {
    m_zbxZoom.hide();
    m_imgLoading.style.visibility = 'hidden';
}

function hideWaitImageForNetscape6() {
	if (m_imgMapCanvas.complete) {
		clearInterval(m_lTimerID);
		m_divZoomBox.style.visibility = 'hidden';
		m_imgLoading.style.visibility = 'hidden';
	}
}

function moveIn() {
    m_mapViewer.moveIn();
    submit();
}

function moveOut() {
    m_mapViewer.moveOut();
    submit();
}

function moveEast() {
    m_mapViewer.moveEast();
    submit();
}

function moveNorth() {
    m_mapViewer.moveNorth();
    submit();
}

function moveSouth() {
    m_mapViewer.moveSouth();
    submit();
}

function moveWest() {
    m_mapViewer.moveWest();
    submit();
}
function moveNorthEast() {
    m_mapViewer.moveNorthEast();
    submit();
}
function moveSouthEast() {
    m_mapViewer.moveSouthEast();
    submit();
}
function moveNorthWest() {
    m_mapViewer.moveNorthWest();
    submit();
}
function moveSouthWest() {
    m_mapViewer.moveSouthWest();
    submit();
}

function persistExtent() {
	m_hvMinX.value = m_mapViewer.getExtent().getLeft();
	m_hvMinY.value = m_mapViewer.getExtent().getBottom();
	m_hvMaxX.value = m_mapViewer.getExtent().getRight();
	m_hvMaxY.value = m_mapViewer.getExtent().getTop();
}

function posBorder() {

    if (m_sClientBrowserType == 'Netscape') { 
		m_divMapBorder.style.left = m_imgMapCanvas.offsetLeft - parseInt(m_divMapBorder.style.borderWidth.replace('px',''));
		m_divMapBorder.style.top = m_imgMapCanvas.offsetTop - parseInt(m_divMapBorder.style.borderWidth.replace('px',''));
	} else { // browser must be IE
		m_divMapBorder.style.left = m_imgMapCanvas.offsetLeft;
		m_divMapBorder.style.top = m_imgMapCanvas.offsetTop;
	}
		
    m_divMapBorder.style.width = m_imgMapCanvas.width;
    m_divMapBorder.style.height = m_imgMapCanvas.height;
    m_divMapBorder.style.visibility = "visible";

}

function posBorderNavigation() {

    var borderOffset = 0;    
    if (m_sClientBrowserType == 'Netscape') {
		borderOffset = parseInt(m_divMapBorder.style.borderWidth.replace('px','')) 
	}
   
          //align the NORTH DIV       
    m_divNorth.style.top = m_imgMapCanvas.offsetTop - (parseInt(m_divNorth.style.height.replace('px',''))) - borderOffset;
    m_divNorth.style.left = m_imgMapCanvas.offsetLeft + (m_imgMapCanvas.width / 2)- (parseInt(m_divNorth.style.width.replace('px','')) / 2);    

         //align the SOUTH DIV
    m_divSouth.style.top = m_imgMapCanvas.offsetTop + m_imgMapCanvas.height + borderOffset;
    m_divSouth.style.left = m_imgMapCanvas.offsetLeft + (m_imgMapCanvas.width / 2)- (parseInt(m_divSouth.style.width.replace('px','')) / 2);   
  
         //align the EAST DIV       
    m_divEast.style.top = m_imgMapCanvas.offsetTop + ((m_imgMapCanvas.height / 2) - (parseInt(m_divEast.style.height.replace('px','')) / 2));
    m_divEast.style.left = m_imgMapCanvas.offsetLeft + m_imgMapCanvas.width + borderOffset;    

         //align the WEST DIV       
    m_divWest.style.top = m_imgMapCanvas.offsetTop + ((m_imgMapCanvas.height / 2) - (parseInt(m_divWest.style.height.replace('px','')) / 2));
    m_divWest.style.left = m_imgMapCanvas.offsetLeft - (parseInt(m_divWest.style.width.replace('px',''))) - borderOffset;   
}

function posLoadingImage() {
    m_imgLoading.style.top = (parseInt(m_imgMapCanvas.style.top.replace('px','')) + (m_imgMapCanvas.height / 2) - (m_imgLoading.height / 2) );
    m_imgLoading.style.left = (parseInt(m_imgMapCanvas.style.left.replace('px','')) + (m_imgMapCanvas.width / 2) - (m_imgLoading.width / 2) );
}

function posTools() {
    //m_divMapTools.style.top = parseInt(m_divSouth.style.top.replace('px','')) + parseInt(m_divSouth.style.height.replace('px',''));
    //m_divMapTools.style.width = m_imgMapCanvas.width;
}

function reportCoords(ptReport) {
	// Note: Use round() instead of toFixed(), since the latter isn't supported
  //       in IE Mac.
	m_txtXCoord.value = "X: "+(Math.round(ptReport.x*10000.0)/10000.0).toString();
	m_txtYCoord.value = "Y: "+(Math.round(ptReport.y*10000.0)/10000.0).toString();
}

/*
*******************	ORIGINAL ****************************
function returnActiveTool() {
	if (m_radZoomIn.checked) {
		return 1;
	} else if(m_radRecenter.checked) {
		return 2;
	}
}
*********************************************************
*/
function returnActiveTool() {
	// Maintain active tool
	var m_tool ;
	m_tool = m_hvToolType.value;
	//m_tool = m_hvTool.value;
	//alert(m_tool);

	if (m_tool == 1){
		return 1;
	}else if (m_tool == 2){
		return 2;
	}else if (m_tool == 3){
		return 3;
	}else if (m_tool == 4){
		return 4;
	}
	else if (m_tool == 5){
		return 5;
	}
	else {
		return 1;
	}
}

/* ******************************** ORIGINAL ***************************
function submit() {
	var sURL = m_hvMapPage.value+
		         "?XMIN="+m_mapViewer.getExtent().getLeft()+
				     "&YMIN="+m_mapViewer.getExtent().getBottom()+
						 "&XMAX="+m_mapViewer.getExtent().getRight()+
						 "&YMAX="+m_mapViewer.getExtent().getTop()+
						 "&WIDTH="+m_mapViewer.getTagWidth()+
						 "&HEIGHT="+m_mapViewer.getTagHeight();
  updateZoomLevel(m_mapViewer.getLevel());
	showWaitImage();

	if (navigator.userAgent.indexOf('Netscape6/6') > -1) {
		m_lTimerID = setInterval("hideWaitImageForNetscape6();",100);
	}
  m_imgMapCanvas.src = sURL;
  //Persist extent in Hidden variables
  persistExtent();	
}***************************************************************************
*/

/*
function submit() {

	m_hvMinX.value = m_mapViewer.getExtent().getLeft();
	m_hvMinY.value = m_mapViewer.getExtent().getBottom();
	m_hvMaxX.value = m_mapViewer.getExtent().getRight();
	m_hvMaxY.value = m_mapViewer.getExtent().getTop();

	if (m_iToolMode == 4)
	{
	//Panning
	var sURL = m_hvMapPage.value+
					"?XMIN="+m_mapViewer.getExtent().getLeft()+
						"&YMIN="+m_mapViewer.getExtent().getBottom()+
							"&XMAX="+m_mapViewer.getExtent().getRight()+
							"&YMAX="+m_mapViewer.getExtent().getTop()+
							"&WIDTH="+m_mapViewer.getTagWidth()+
							"&HEIGHT="+m_mapViewer.getTagHeight();
							//document.location = sURL;	
	}
	else if ((m_iToolMode == 1) || (m_iToolMode == 2)|| (m_iToolMode == 3))
	{
	//ZoomIn, ZoomOut, Recenter (respectively)
		var sURL = m_hvMapPage.value+
					"?XMIN="+m_mapViewer.getExtent().getLeft()+
						"&YMIN="+m_mapViewer.getExtent().getBottom()+
							"&XMAX="+m_mapViewer.getExtent().getRight()+
							"&YMAX="+m_mapViewer.getExtent().getTop()+
							"&WIDTH="+m_mapViewer.getTagWidth()+
							"&HEIGHT="+m_mapViewer.getTagHeight();
	}
	else 
	{
		// Identify
		var idURL = m_hvIdPage.value;
		var ptUserClick = m_mapViewer.toMapPoint(x2, y2);
		var sURL =	         "?XMIN="+m_mapViewer.getExtent().getLeft()+
						"&YMIN="+m_mapViewer.getExtent().getBottom()+
							"&XMAX="+m_mapViewer.getExtent().getRight()+
							"&YMAX="+m_mapViewer.getExtent().getTop()+
							"&WIDTH="+m_mapViewer.getTagWidth()+
							"&HEIGHT="+m_mapViewer.getTagHeight()+
							"&CTRX="+ptUserClick.x+
							"&CTRY="+ptUserClick.y;
	}
	// Force submission in order to create the MapTips information.
	if (x2 < (m_mapViewer.getTagWidth()) && !(m_iToolMode == 5)){
	// Identify tool
		frmViewer.submit();
	}
	else{
	}
	// Keep from drawing zoom levels
	updateZoomLevel(m_mapViewer.getLevel());
		showWaitImage();
		if (navigator.userAgent.indexOf('Netscape6/6') > -1) {
			m_lTimerID = setInterval("hideWaitImageForNetscape6();",100);
		}

	if (m_iToolMode == 5){
	// Identify tool
		m_imgMapCanvas.src = m_hvMapPage.value+sURL;
		openBrWindow(idURL+sURL,'idResults','toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=500,height=500');
	}
	else {	
		m_imgMapCanvas.src = sURL;
	}
	//Persist extent in Hidden variables
	persistExtent();		 
}
*/


function submit() {
	/* SET LAYER 9 VISIBLE WHEN USING THE MAP
	SetVisible('9Layer');
	*/
	
    if (m_iToolMode == 4)
	{
	//Panning
	var sURL = "default.aspx"+
					"?XMIN="+m_mapViewer.getExtent().getLeft()+
						"&YMIN="+m_mapViewer.getExtent().getBottom()+
							"&XMAX="+m_mapViewer.getExtent().getRight()+
							"&YMAX="+m_mapViewer.getExtent().getTop()+
							"&WIDTH="+m_mapViewer.getTagWidth()+
							"&HEIGHT="+m_mapViewer.getTagHeight();
							document.location = sURL;	
	}
	// IDENTIFY
	if (m_iToolMode == 5)
	{
		var idURL = m_hvIdPage.value;
		var ptUserClick = m_mapViewer.toMapPoint(x2, y2);
		var idURL2 = "?XMIN="+m_mapViewer.getExtent().getLeft()+
				     "&YMIN="+m_mapViewer.getExtent().getBottom()+
						 "&XMAX="+m_mapViewer.getExtent().getRight()+
						 "&YMAX="+m_mapViewer.getExtent().getTop()+
						 "&WIDTH="+m_mapViewer.getTagWidth()+
						 "&HEIGHT="+m_mapViewer.getTagHeight()+
						 "&CTRX="+ptUserClick.x+
						 "&CTRY="+ptUserClick.y;
		var tl = "&tl=id";
		var sURL = m_hvMapPage.value+idURL2+tl;
		         
	}
	else 
	{
	
	if ((m_mapViewer.getExtent().getRight() - m_mapViewer.getExtent().getLeft() > 5519) ) {
	//alert((m_mapViewer.getExtent().getRight() - m_mapViewer.getExtent().getLeft()));
	var sURL = m_hvMapPage.value+
		         "?XMIN="+m_mapViewer.getExtent().getLeft()+
				     "&YMIN="+m_mapViewer.getExtent().getBottom()+
						 "&XMAX="+m_mapViewer.getExtent().getRight()+
						 "&YMAX="+m_mapViewer.getExtent().getTop()+
						 "&WIDTH="+m_mapViewer.getTagWidth()+
						 "&HEIGHT="+m_mapViewer.getTagHeight()+
						 "&mt="+m_hvToolType.value;
	}	
	else
	{
		//alert((m_mapViewer.getExtent().getRight() - m_mapViewer.getExtent().getLeft()));
		var xCtr = m_mapViewer.getExtent().getLeft() + ((m_mapViewer.getExtent().getRight() - m_mapViewer.getExtent().getLeft())) / 2;
        var yCtr = m_mapViewer.getExtent().getBottom() + ((m_mapViewer.getExtent().getTop() - m_mapViewer.getExtent().getBottom())) / 2;
		
        var NewminX = xCtr - 1500.5;
		var NewminY = yCtr - 1500.5;
        var NewmaxX = xCtr + 1500.5;
        var NewmaxY = yCtr + 1500.5;
        if (m_iToolMode == 2) {
			var ptUserClick = m_mapViewer.toMapPoint(x2, y2);
		}
		else {
			var ptUserClick = m_mapViewer.toMapPoint(m_mapViewer.getTagWidth(),m_mapViewer.getTagHeight());
        }
        
		var sURL = m_hvMapPage.value+
		         "?XMIN="+NewminX+
				     "&YMIN="+NewminY+
						 "&XMAX="+NewmaxX+
						 "&YMAX="+NewmaxY+
						 "&WIDTH="+m_mapViewer.getTagWidth()+
						 "&HEIGHT="+m_mapViewer.getTagHeight()+
						 "&mt="+m_hvToolType.value+
						 "&CTRX="+ptUserClick.x+
						 "&CTRY="+ptUserClick.y;
		
	}
	
	
	}
	
  /*
   COMMENT OUT SINCE WE ARE NOT USING ZOOM LEVELS 		
	BY : REY DE CASTRO 
  updateZoomLevel(m_mapViewer.getLevel());
  */
   
	showWaitImage();
	

	if (navigator.userAgent.indexOf('Netscape6/6') > -1) {
		m_lTimerID = setInterval("hideWaitImageForNetscape6();",100);
	}
	// MAKE SURE THAT THE EVENT CLICK IS ON THE MAP & USE THE EVENTX TO CONFIRM CLICK EVENT
	if (m_iToolMode == 5 && ((m_mapViewer.isEventOverMap(eventX, eventY))) && (eventX)){

// Identify tool
	m_imgMapCanvas.src = sURL;
	//alert(idURL + sURL);
	
	openBrWindow(idURL+idURL2,'idResults','toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=350,height=500');
  }
	else {
	m_imgMapCanvas.src = sURL;
	}
   	
 //Persist extent in Hidden variables
 
  persistExtent();	


}

function showWaitImage() {
	m_imgLoading.style.visibility = 'visible';
}

function openBrWindow(theURL,winName,features) { 
  //window.open(theURL,winName,features);
    window.showModalDialog(theURL,"","dialogHeight: 600px; dialogWidth: 350px; dialogTop: 10px; dialogLeft: 10px; edge: Raised; center: Yes; resizable: Yes; status: Yes;");
}

function updateImgMapSize(sSize) {
    m_imgMapSize = document.getElementById("imgMapSize");
    switch(sSize) { 
        case 'SMALL':
            m_imgMapSize.name = 'SMALL';
            m_imgMapSize.src= 'images/smallmap.gif';
        break;
        case 'LARGE':
            m_imgMapSize.name = 'LARGE';
            m_imgMapSize.src= 'images/largemap.gif';
        break;
    }
}

function updateZoomLevel(inLevel) {
        var nLevels = m_ExtentWidths.length;
		for (i=1;i<nLevels+1;i++) {
			document.getElementById('imgZoomLevel' + i).src = 'images/tick.gif';
			if (i==inLevel) {
			    document.getElementById('imgZoomLevel' + i).src = 'images/tick_selected.gif';
			}
		}
}

// Functions from the original app
// Create a DHTML layer
function createLayer(name, left, top, width, height, visible, content) {
	  var layer;
	  if (m_sClientBrowserType == 'Netscape') {
	    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);
}

// clip layer display to clipleft, cliptip, clipright, clipbottom
function clipLayer(name, clipleft, cliptop, clipright, clipbottom) {		
	//var layer = getLayer(name);		
	//if (layer != null) {
	//alert("clipping");
  		if (m_sClientBrowserType == 'Netscape') {
			m_imgMapCanvas.clip.left   = clipleft;
			m_imgMapCanvas.clip.top    = cliptop;
		    m_imgMapCanvas.clip.right  = clipright;
			m_imgMapCanvas.clip.bottom = clipbottom;
		} else if (m_sClientBrowserType == 'IE') {
			m_imgMapCanvas.style.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
	    } else {
     		m_imgMapCanvas.height = clipbottom - cliptop;
			m_imgMapCanvas.width	= clipright - clipleft;
			m_imgMapCanvas.top	= (cliptop+vspc) + "px";
			m_imgMapCanvas.left	= (clipleft+hspc) + "px";
		}
	//}
}

// move layer to x,y
function moveLayer(name, x, y) {
	//alert("movelayer");		
	  	if (m_sClientBrowserType == 'Netscape') {
    		//layer.moveTo(x, y);
    		m_imgMapCanvas.style.left = x;
    		m_imgMapCanvas.style.top = y;
    	}	
	 	else if (m_sClientBrowserType == 'IE') {
    		m_imgMapCanvas.style.pixelLeft = x;
   			m_imgMapCanvas.style.pixelTop  = y;
		} else {
			m_imgMapCanvas.height = iHeight - y;
			m_imgMapCanvas.width	= iWidth - x;
    		m_imgMapCanvas.left = x + "px";
   			m_imgMapCanvas.top  = y + "px";
	  	}
}
