var newMinx = 0;
var newMiny = 0;
var newMaxx = 0;
var newMaxy = 0;
var changeClicked = 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 zminx=0;
var zmaxx=0;
var zmaxy=0;
var zminy=0;

var mapX = 0; 
var mapY = 0; 
var radiusFrom = "";

var zooming=false;
var panning=false;

// setup test for Nav
var isNav = (navigator.appName.indexOf("Netscape")>=0);
var isNav4 = false;
var isIE = false;
var isIE4 = false;
var is5up = false;
var isMac = false;
var isWin = false;
var isIE6 = false;


if (isNav) {
	
	if (parseFloat(navigator.appVersion)<5) {
		isNav4=true;
		//alert("Netscape 4.x or older");
	} else {
		is5up = true;
	}
} else {
	isIE4=true;
	isIE=true;
	if (navigator.appVersion.indexOf("MSIE 5")>0) {
		isIE4 = false;
		is5up = true;
		isIE6 =false;
		//alert("IE5");
	} else if (navigator.appVersion.indexOf("MSIE 6")>0) {
		isIE4 = false;
		is5up = false;
		isIE6 = true;
	}
}	

if (navigator.userAgent.indexOf("Win") >= 0) 
	isWin = true;
else
	isMac = true;

//***********************************************	
//***************** FUNCTIONS *******************
//***********************************************

// set zoom box settings
function setZoomBoxSettings() {
	
	document.onmousemove = getMouse;
	document.onmousedown = mapTool;
	document.onmouseup = chkMouseUp;	
	//alert(document.frmMain.tool.value);
}

// 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);
	}
}

// get Image X Y
function getImageXY(e) {

	if (isNav) {
		mouseX=e.pageX;
		mouseY=e.pageY;
	} else if (is5up) {
		
		mouseX=event.clientX + document.body.scrollLeft;
		mouseY=event.clientY + document.body.scrollTop;
	} else {
		//We're in 6.0
		mouseX=event.clientX + document.documentElement.scrollLeft;
		mouseY=event.clientY + document.documentElement.scrollTop;
	}
	// subtract offsets from page left and top
	
	mouseX = mouseX-hspc;  //hspc is horizontal distance of map image placement on page
	mouseY = mouseY-vspc;  //vspc is the vertical distance (px) of map image placement on page
}	


// check scrollbar click
function checkScrollbarClick(e){
	if (isNav) {
		cX=e.pageX;
		cY=e.pageY;
	} else {
		cX=event.clientX;
		cY=event.clientY;
	}
	if (isNav) {
		pW=window.innerWidth-16;
		pH=window.innerHeight-16;
	} else {
		pW=document.body.offsetWidth-20;
		pH=document.body.offsetHeight-20;
	}
	if((cX > pW)||(cY > pH)){		
		return false;		
	} else{				
		return true;
	}	
}


// convert mouse click xy's into map coordinates
function getMapXY(xIn,yIn) {
	mouseX = xIn;
	var pixelX = (maxx-minx) / iWidth;
	mapX = pixelX * mouseX + minx;
	mouseY = iHeight - yIn;
	var pixelY = (maxy-miny) / iHeight;
	mapY = pixelY * mouseY + miny;
	
}


// get the coords at mouse position
function getMouse(e) {
 	window.status="";
 	
	getImageXY(e);
	
	if (zooming) {
		if (mouseX<0)
		 	mouseX = 0;
		if (mouseX>iWidth)
			mouseX = iWidth;
		if (mouseY<0)
			mouseY = 0;
		if (mouseY>iHeight)
			mouseY = iHeight;
		x2=mouseX;
		y2=mouseY;
		setClip();
		return false;
	} else if (panning) {
		x2=mouseX;
		y2=mouseY;
		panMouse();	
		return false;
	} else 
    	return true;
	return true;
}

// perform appropriate action with mapTool
function mapTool(e) {
	if(checkScrollbarClick(e))
	{
			
			
		curTool = document.frmMain.tool.value;		
		cmd = document.frmMain.cmd.value;
		getImageXY(e);
		// Deal with the possibility of a
		// select, identify or area tool first, since it is a little different than the other states
		// (doesn't require a mouse up event)
		//IDENTIFY
		if ((curTool == "identify") && 
			(mouseX >= 0) && (mouseX <= iWidth) && 
			(mouseY >= 0) && (mouseY <= iHeight)){
				//do identify
				getMapXY(mouseX,mouseY);
				sendIdentify(mapX,mapY);
				return true;	             
		}
		if ((curTool == "identifyBusiness") && 
			(mouseX >= 0) && (mouseX <= iWidth) && 
			(mouseY >= 0) && (mouseY <= iHeight)){
				//do identify
				getMapXY(mouseX,mouseY);
				sendIdentify(mapX,mapY);
				return true;	             
		}
		//RADIUS
		if ((curTool == "select") && 
			(mouseX >= 0) && (mouseX <= iWidth) && 
			(mouseY >= 0) && (mouseY <= iHeight))
		{		document.frmMain.cmd.value = "radius"; //must be set to radius for the DoRangeSearch in IMS.cs to run
				getMapXY(mouseX,mouseY);
				sendRadius(mapX,mapY);
				//changeRadiusType();
				return false;	             
		} 

		//ZOOMIN, ZOOMOUT, PAN
		
		if ((!zooming) && (!panning) && (mouseX >= 0) && (mouseX <= iWidth) && (mouseY >= 0) && (mouseY <= iHeight)) 
		{
			if (curTool == "pan") //start pan
			{
				startPan(e);
			}
			else if(curTool == "zoomin" || curTool == "zoomout") //start zoomin\out box
			{
				startZoomBox(e);				
			}			
			return false;
		} else if (zooming) {
			getMouse(e);
			stopZoomBox(e);
		} else if (panning) {
			getMouse(e);
			stopPan(e);
		}
		return true;
	}
}

// start zoom in.... box displayed
function startZoomBox(e) {
	getImageXY(e);	
	if ((mouseX<iWidth) && (mouseY<iHeight)) {
		if (!zooming) {
			x1=mouseX;
			y1=mouseY;
			x2=x1+1;
			y2=y1+1;
			zooming=true;
			moveLayer("MapZoomBox",hspc+x1,vspc+y1);
			resizeLayer("MapZoomBox", 1, 1);
			//clipLayer("MapZoomBox",x1,y1,x2,y2);
			showLayer("MapZoomBox");
			
		} else {
			if (zooming) {
				stopZoomBox(e);
			}
		}
	}
	return false;	
}

function stopZoomBox(e) {
	hideLayer("MapZoomBox");
	zooming=false;
	curTool = document.frmMain.tool.value;
	var width = Math.abs(maxx - minx);
	var height = Math.abs(maxy - miny);
	var pixelX = width / iWidth;
	var pixelY = height / iHeight;
	var theminY = iHeight - zminy;
	var themaxY = iHeight - zmaxy;
	newMinx = pixelX * zminx + minx;
	newMiny = pixelY * theminY + miny;
	newMaxx = pixelX * zmaxx + minx;
	newMaxy = pixelY * themaxY + miny;
	// if the zoom box is too small
	if ((zmaxx <zminx+2) && (zmaxy < zminy+2)) {
		if (curTool == "zoomout") {
			newMinx = newMinx - width;
			newMiny = newMiny - height;
			newMaxx = newMaxx + width;
			newMaxy = newMaxy + height;
		} else {
			newMinx = newMinx - width / 4;
			newMiny = newMiny - height / 4;
			newMaxx = newMaxx + width / 4;
			newMaxy = newMaxy + height / 4;
		}
		refreshMap();
	// if the zoom box is not too small
	} else {
		if (curTool == "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();
	}
	return true;
}


// start pan.... image will move
function startPan(e) {
	moveLayer("theMap",hspc,vspc);
	getImageXY(e);
	// keep it within the MapImage
	if ((mouseX<iWidth) && (mouseY<iHeight)) {
		if (panning) {
			stopPan(e);
		} else {
			x1=mouseX;
			y1=mouseY
			x2=x1+1;
			y2=y1+1;
			panning=true;
		}
	}
	return false;
}


// stop moving image.... pan 
function stopPan(e) {
	window.scrollTo(0,0);
	panning=false;
	if ((Math.abs(x2-x1) < 2) && (Math.abs(y2-y1) < 2)) {
		// the move is too small
		getMapXY(mouseX,mouseY);
		var widthHalf = Math.abs(maxx - minx) / 2;
		var heightHalf = Math.abs(maxy - miny) / 2;
		newMinx = mapX - widthHalf;
		newMaxx = mapX + widthHalf;
		newMiny = mapY - heightHalf;
		newMaxy = mapY + heightHalf;
	} else  {
		var width = Math.abs(maxx - minx);
		var height = Math.abs(maxy - miny);
		var ixOffset = x2-x1;
		var iyOffset = y1-y2;
		pixelX = width / iWidth;
		pixelY = height / iHeight;
		var xOffset = pixelX * ixOffset;
		var yOffset = pixelY * iyOffset;
		newMinx = minx - xOffset;
		newMiny = miny - yOffset;
		newMaxx = maxx - xOffset;
		newMaxy = maxy - yOffset;
	}
	refreshMap();
	return true;
}


// move map image with mouse
function panMouse() {
	var xMove = x2-x1;
	var yMove = y2-y1;
	var cLeft = -xMove;
	var cTop = -yMove;
	var cRight = iWidth;
	var cBottom = iHeight;
	if (xMove>0) {
		cLeft = 0;
		cRight = iWidth - xMove;
	}
	if (yMove>0) {
		cTop = 0;
		cBottom = iHeight - yMove;
	}
	clipLayer("theMap",cLeft,cTop,cRight,cBottom);
	moveLayer("theMap",xMove+hspc,yMove+vspc);
	return false;
}


// refresh map
function refreshMap() {
  //document.frmMain.cmd.value = "REFRESH";
  document.frmMain.minX.value = newMinx.toString();
  document.frmMain.minY.value = newMiny.toString();
  document.frmMain.maxX.value = newMaxx.toString();
  document.frmMain.maxY.value = newMaxy.toString();
  submitMapRequest();
  showLayer("Splash");
}


// send identify
function sendIdentify(numMapX,numMapY) {
  document.frmMain.cmd.value = "";
  document.frmMain.identifyX.value = numMapX.toString();
  document.frmMain.identifyY.value = numMapY.toString();
  submitMapRequest();
  setZoomBoxSettings(); 

}

function checkRadVal()
{
	changeClicked = true;
	var report_other_text = findObj("reportradius_other");
	var radVal = document.frmMain.radius.value;
	
	radiusFrom = "Community";
	//alert(radVal);
	if(radVal != "County" && radVal != "15" && radVal != "30" && radVal != "60")
	{
		if(report_other_text != null)
		{
			if(parseInt(report_other_text.value) > 99)
			{
				alert("Please enter a whole number between 0 to 100.");
				report_other_text.value = "";
			}
			else if(report_other_text.value == "0" || report_other_text.value.indexOf("-") != -1)
			{
				alert("Please enter a whole number between 0 to 100.");
				report_other_text.value = "";
			}
			else if(isNaN(parseInt(report_other_text.value)))
			{
				alert("Please enter a whole number between 0 to 100.");
				report_other_text.value = "";
			}
			else
			{
				document.frmMain.radius.value = report_other_text.value;				
				communityReportChangeRadius();
			}
		}
		
		else
		{
			communityReportChangeRadius();
		}
	}
	else
	{
		communityReportChangeRadius();
	}
}


function communityReportChangeRadius()
{
    //they might be changing the radius from the Community Report.
    //in this case we need to add a marker through code.
    //GET X/Y OF COUNTY CENTROID
    countyXY = document.frmMain.reportxy.value;  

    if (countyXY != null && countyXY != "")
    {
       var found = countyXY.indexOf(",");  //found contains the position in the string of current ","
       if (found > 0)
       {
          x = countyXY.substring(0,found);
          found = found + 1;
          y = countyXY.substring(found);
          document.frmMain.selX.value = x;
          document.frmMain.selY.value = y;
 		  changeRadiusType();
       }
    }
}

//change radius type - maintain existing location
function changeRadiusType()
{  
  changeClicked = true;
  label = findObj("lbl_range_search_message");
  label.text = "";
  var toolRangeBox = document.getElementById("range");
  var reportRangeBox = document.getElementById("reportrange");  
    
  if(radiusFrom != "Community")
  {
	radiusFrom = "Marker";	
  }
 
  if(toolRangeBox.selectedIndex == 4)
  {
	 var radius_other_text = findObj("radius_other");
  	 document.frmMain.radius.value = radius_other_text.value;
  	
  	 document.frmMain.cmd.value = "radius";
  	 
  }
  
  if(reportRangeBox != null)
  {
	if(reportRangeBox.selectedIndex == 4)
	{
		var radius_other_text = findObj("reportradius_other");
  		document.frmMain.radius.value = radius_other_text.value;
  		document.frmMain.cmd.value = "radius";
  		
	}
  }
  
  //verify that there is a valid existing marker to change  
  
   x = document.frmMain.selX.value;
   y = document.frmMain.selY.value;
  
   radius = document.frmMain.radius.value;
   document.frmMain.cmd.value = "radius";
   
  if (x != "" &&  y != "") 
  {
	 sendRadius(x,y);
  }  
  else 
  {     
	markerXY = document.frmMain.markerList.value;
	if (markerXY != null && markerXY != "")
	{
		 var found = markerXY.indexOf(" ");  //found contains the position in the string of current ","
		 if (found > 0)
		 {
			x = markerXY.substring(0,found);
			found = found + 1;
			y = markerXY.substring(found);
			//document.frmMain.selX.value = x;
			//document.frmMain.selY.value = y;
			sendRadius(x,y);
		 }       
    } 
    else
    {
		alert("Please click on the map to create the search range.");   
	}
  }
  
}

// send radius
function sendRadius(numMapX,numMapY) {
  //document.frmMain.cmd.value = "radius";
 radius_other_text = findObj("radius_other");
 report_other_text = findObj("reportradius_other");	
 submitOk = true;
 
 
 var radVal = document.frmMain.radius.value;  
 
 if(radVal != "County" && radVal != "15" && radVal != "30" && radVal != "60" )
 {	
	
	if(radiusFrom == "Community")
	{
		//alert(report_other_text.value);
		radius_other_text.value = report_other_text.value;
	}
	else
	{		
		if(report_other_text != null)
		{
			report_other_text.value = radius_other_text.value;
		}
	}		
	
	if(parseInt(radius_other_text.value) > 99)
	{
		submitOk = false;
		alert("Please enter a whole number between 0 to 100.");
		radius_other_text.value = "";
	}
	else if(radius_other_text.value == "0" || radius_other_text.value.indexOf("-") != -1)
	{
		submitOk = false;
		alert("Please enter a whole number between 0 to 100.");
		radius_other_text.value = "";
	}
	else if(isNaN(parseInt(radius_other_text.value)))
	{
		submitOk = false;
		alert("Please enter a whole number between 0 to 100.");
		radius_other_text.value = "";
	}
	else
	{
		submitOk = true;
		
		document.frmMain.radius.value = radius_other_text.value;
		document.frmMain.cmd.value = "radius";
	}		
 }
 
  
  if(document.frmMain.radius.value == "")
  {
	alert("Please enter a whole number between 0 to 100.");
	radius_other_text.value = "";
	submitOk = false;	
  }
 
 
 
 if(submitOk && radiusFrom == "Marker" && document.frmMain.maintab.value.indexOf("prop") == -1)
 {
	x = numMapX.toString();
	y = numMapY.toString();
	document.frmMain.selX.value = x; 
	document.frmMain.selY.value = y;
	document.frmMain.reportxy.value = x + "," + y;	
	document.frmMain.cmd.value = "radius";	
	document.frmMain.tool.value = "Report_Radius";   
 }
 else if(submitOk && radiusFrom == "Marker" && document.frmMain.maintab.value.indexOf("prop") != -1)
 {
	x = numMapX.toString();
	y = numMapY.toString();
	document.frmMain.selX.value = x; 
	document.frmMain.selY.value = y;
	markerList = x + " " + y;
	document.frmMain.cmd.value = "radius";
	document.frmMain.markerList.value = markerList;
	doPropertyRangeSearch('both');
	submitOk = false;
 } 
 
 if(submitOk)
 {
	document.frmMain.cmd.value = "radius";
	x = numMapX.toString();
	y = numMapY.toString();
	document.frmMain.selX.value = x; 
	document.frmMain.selY.value = y;
	 //add marker at center of radius
     //MULTIPLE MARKERS: //markerList = document.frmMain.markerList.value + "," + x + " " + y;  // ',' separates pairs  " " separates coords
     //Changed to SINGLE MARKER:
	markerList = x + " " + y;
	document.frmMain.markerList.value = markerList;
	radiusFrom = "";
	submitMapRequest();
	showLayer("Splash");
  }  
}

function clearMarkers()
{
  document.frmMain.markerList.value = "";
  document.frmMain.selX.value = "";
  document.frmMain.selY.value = "";
  //clear selected counties too
  radius = document.frmMain.radius.value;
  if (radius == "County")
  {
      document.frmMain.selcounties.value = "";
  }
  else
  {
	document.frmMain.radius.value = "";
  }
  
  submitMapRequest();
  
}
// 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 {
		zminy=y2;
		zmaxy=y1;
	}
	if ((x1 != x2) && (y1 != y2)) {
		moveLayer("MapZoomBox", hspc+zminx, vspc+zmaxy);
		resizeLayer("MapZoomBox", Math.abs(zmaxx-zminx), Math.abs(zmaxy-zminy));
		//clipLayer("MapZoomBox",zminx,zminy,zmaxx,zmaxy);
		showLayer("MapZoomBox");
	}
}


//-------- LAYER SUPPORT FUNCTIONS --------------

// get the layer object called "name"
function getLayer(name) {
	var theObj = document.getElementById(name);
	if(theObj != null){
		return(theObj.style);
	} else {
		if ( eval('document.all.' + name) != null) {
		    layer = eval('document.all.' + name + '.style');
		    return(layer);
		} else {
			return(null);
		}
	
	}
}

// clip layer display to clipleft, cliptip, clipright, clipbottom
function clipLayer(name, clipleft, cliptop, clipright, clipbottom) {		
	var layer = getLayer(name);		
	if (layer != null) {
  		layer.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
	}
}

// toggle layer to invisible
function hideLayer(name) {		
  	var layer = getLayer(name);		
	if (layer != null) {
		layer.visibility = "hidden";
	}
}

// toggle layer to visible
function showLayer(name) {		
  	var layer = getLayer(name);		
	if (layer != null) {
	  	layer.visibility = "visible";
	}
}

// move layer to x,y
function moveLayer(name, x, y) {
	var layer = getLayer(name);		
	if (layer != null) {
	  	layer.left = x + "px";
   		layer.top  = y + "px";
	}
}

// resize layer to wd,ht
function resizeLayer(name, wd, ht) {
	var layer = getLayer(name);		
	if (layer != null) {
	  	layer.width = wd + "px";
   		layer.height  = ht + "px";
	}
}


// replace layer content
function replaceLayerContent(name, content) {
	if (document.getElementById(name) != null) {
		document.getElementById(name).innerHTML = content;
	}
}
