﻿//**********************************************************************
//map.js includes all functions of page "Map.aspx" mainly functions here 
//control interaction with the map on clicking and dragging
//**********************************************************************


//Global Variables
var MapWidth;
var MapHeight;
var originalPosX;
var originalPosY;
var sWidth = screen.width;
var sHeight = screen.height;
var mUnits = "METERS";
var sUnits = "METERS";
var numDecimals;
var zooming= false;
var map;

//-------------------------------------------------------------------------------------

//Set MapSize and sets listeners on map
function LoadMap()
{
	
		
	
	//parent.document.frmMain.txtscale.value=document.frmMap.hid_scale.value;
	if (isIE)//case of IE
		{
		
		    document.getElementById("imgmapIE").style.display='block';
			map= document.getElementById("imgDiv");	
		}
		else//case netscae,mozilla, firefox, opera..
		{
		    document.getElementById("imgmapFF").style.display='block';
			map= document.getElementById("imgmapFF");	
		}
		
    map.onmousedown =  function(e) {handlerMD(e)} ; 
	map.onmouseup   =  function(e) {handlerMU(e)} ; 
	map.onmousemove =  function(e) {handlerMM(e)} ; 	
		
		
  //parent.document.frmMap.extent.value= document.getElementById("hid_minx").value + "!" + document.getElementById("hid_miny").value + "!" + document.getElementById("hid_maxx").value + "!" + document.getElementById("hid_maxy").value;

	//parent.document.frmMain.extent.value= document.getElementById("hid_minx").value + "!" + document.getElementById("hid_miny").value + "!" + document.getElementById("hid_maxx").value + "!" + document.getElementById("hid_maxy").value;
	//SetZoomValue(document.frmMap.hid_scale.value);
	MapWidth=document.getElementById("hid_width").value;
	MapHeight=document.getElementById("hid_height").value;				

	SetCursor();
	SetZoomLevels(document.getElementById("hid_ilevel").value);	
	
	
}
//**********************************************************************

//Submits the form
function Submit()
{

//alert("submit");
	//parent.document.getElementById("tooltipdiv").style.visibility= "hidden";
	document.forms["frmMap"].submit();
}
//**********************************************************************


function SetCursor()
{

	if (document.getElementById("hid_action").value =='zoomin')
	{	
		map.style.cursor= "url(../Images/Cursor/Pan.cur)";
	}
	if (document.getElementById("hid_action").value =='zoomout')
	{	
		map.style.cursor= "url(../Images/Cursor/zoom_out.cur)";
	}
	if (document.getElementById("hid_action").value =='pan')
	{	
		map.style.cursor= "url(../Images/Cursor/pan.cur)";
	}
	if (document.getElementById("hid_action").value =='measure')
	{	
		map.style.cursor= "crosshair";
	}
	if (document.getElementById("hid_action").value=='move')
	{
		map.style.cursor= "url(../Images/Cursor/Pan.cur)";
	}
	if (document.getElementById("hid_action").value=='identify')
	{
		//document.getElementById("hid_subaction").value = "" ;
		map.style.cursor="help";
	}
	if (document.getElementById("hid_action").value=='clickmap')
	{
		//document.getElementById("hid_subaction").value = "" ;
		map.style.cursor="arrow";
	}
}

//Changes the screen coordinates to map coordinates 
function getmapxy(xCoord,yCoord,updown)
{
	minx = parseFloat(document.getElementById("hid_minx").value);
	miny = parseFloat(document.getElementById("hid_miny").value);
	maxx = parseFloat(document.getElementById("hid_maxx").value);
	maxy = parseFloat(document.getElementById("hid_maxy").value);
	
	
	
	mouseX = xCoord;
	pixelX = (maxx-minx) / MapWidth;
	mapX = pixelX * mouseX + minx;
	mouseY = MapHeight - yCoord ;
	pixelY = (maxy-miny) / MapHeight;
	mapY = pixelY * mouseY + miny;
	if(updown == 'u')
	{
		document.getElementById("hid_mapxup").value = mapX;
		document.getElementById("hid_mapyup").value = mapY; 	
	}
	if(updown == 'd')
	{	        
		document.getElementById("hid_mapxdown").value = mapX  ;//+ 75;
		document.getElementById("hid_mapydown").value = mapY ;//+ 50;
	}
	if(updown == 'm')
	{
		moveX = mapX;
		moveY = mapY;
	}
}
//**********************************************************************

//Action to do when mouse down over the map.
function handlerMD(e)
{
			
	if (isIE) e=event;
	
	//Xmd = XmdM =(isNav) ? e.pageX : e.clientX;
	//Ymd = YmdM =(isNav) ? e.pageY : e.clientY;
	
	 Xmd = (isNav) ? e.pageX : e.clientX;
	 Ymd = (isNav) ? e.pageY : e.clientY;
	
	 XmdM = (isNav) ? e.layerX : e.offsetX;
	 YmdM = (isNav) ? e.layerY : e.offsetY;	
	
	var rightclick;
	
	if (isNav) rightclick=(e.which==2);
	else rightclick=(e.button==2);

		
		xscroll = (isNav) ? 0 : document.body.scrollLeft;
		yscroll = (isNav) ? 0 : document.body.scrollTop;

		document.getElementById("hid_mousedownx").value = XmdM; 
		document.getElementById("hid_mousedowny").value = YmdM; 
		getmapxy(document.getElementById("hid_mousedownx").value,document.getElementById("hid_mousedowny").value,'d');
	
	if (rightclick==false)
	{
		//alert(document.getElementById("hid_action").value);

		if(document.getElementById("hid_action").value == "identify")
		{
		     //window.open("../MapTools/identify.aspx?xCoord=" + document.getElementById("hid_mapxdown").value+"&yCoord="+document.getElementById("hid_mapydown").value+"&scale="+document.frmMap.hid_scale.value,"Identify","left=0, top=0, scrollbars=1, resizable=no, width=400, height=200");
		     //newwindow=window.open("../MapTools/DescriptionReport.aspx?action=Identify&xCoord=" + document.getElementById("hid_mapxdown").value+"&yCoord="+document.getElementById("hid_mapydown").value+"&scale="+document.frmMap.hid_scale.value,"Identify","scrollbars=1, resizable=yes, width=600, height=450");
		   //if (window.focus) {newwindow.focus()}
	        //return false;

}
		
		if (document.getElementById("hid_action").value == "printlocation")
		{		
			window.open("printlocationmap.asp?xcoord=" + document.getElementById("hid_mapxdown").value + "&ycoord=" + document.getElementById("hid_mapydown").value ,"PrintLocation",'left=0; top=0; width=1000; height=600 ;resizable=no','True');
		}
		
		
		if (document.getElementById("hid_action").value=="move")
		{
		
		    map.style.cursor= "url(../Images/Cursor/PanMove.cur)";
			document.getElementById("hid_ispaning").value='y';
			document.getElementById("hid_panStartX").value=XmdM;
			document.getElementById("hid_panStartY").value=YmdM;	
			setOrigPos();		 	    			
		}
				
		if(document.getElementById("hid_action").value == "zoomin" || document.getElementById("hid_action").value =="zoomout")
		{
		    startZoomBox();
		}	

		if (document.getElementById("hid_action").value == "measure")
		{
			if (document.getElementById("hid_meastext2").value==0 && document.frmMap.hid_startmeasure.value == "start") 
			{
				document.getElementById("hid_action").value="";
				document.getElementById("hid_meastext2").value=0;
			}
			document.getElementById("hid_startmeasure").value = "start"; // to start calculated distance in meastext2 after (clicking on the map + mousemove) and not just on mouse move 
			document.getElementById("hid_drawxstrmeas").value =document.getElementById("hid_drawxstrmeas").value + ',' + document.getElementById("hid_mapxdown").value
			document.getElementById("hid_drawystrmeas").value =document.getElementById("hid_drawystrmeas").value + ',' + document.getElementById("hid_mapydown").value			

			var mvalue = parseFloat(document.getElementById("hid_meastext1").value) + parseFloat(document.getElementById("hid_meastext2").value);	
			document.getElementById("hid_meastexts").value= document.getElementById("hid_meastexts").value  + document.getElementById("hid_meastext2").value + ',';

			document.getElementById("meastext1").value = mvalue;
			document.getElementById("hid_meastext1").value = mvalue;
			document.getElementById("hid_meastext2").value = 0;
			document.forms["frmMap"].submit();
		}
		
		//add annotation
		if (document.getElementById("hid_action").value == "addannotation")
		{
		
			document.getElementById("lblannotation").innerHTML="Click To Enter Text";
			document.getElementById("annotationDIV").style.visibility = "visible";
			moveLayer2("annotationDIV",XmdM,YmdM);		
		}
		
		if(document.getElementById("hid_action").value == "clickmap")
			{
			CheckClickedFeature();
			}
		
		if (document.getElementById("hid_action").value == "" || document.getElementById("hid_action").value == "reset")
		{
		    SetAction('identify',0);//to be the default action
			//showLayer("HelpDiv");
	    }
		
		//end add annotation
		
		}//end of right click checking
	else
	{
	
	//setInterval("window.status='select option'",10);
	
			if(document.getElementById("hid_action").value == "clickmap")
			{
			CheckClickedFeature();
			}
			if (document.getElementById("hid_action").value == "" || document.getElementById("hid_action").value == "reset")
		    {
		    showLayer("HelpDiv");
			
			}
	

	}	
}
//**********************************************************************


//Action to do when mouse moves over the map
function handlerMM(e)
{
	if (isIE) e=event;
	
	if (document.getElementById("hid_action").value == "" || document.getElementById("hid_action").value == "reset")
		{
		    SetAction('identify',0);//to be the default action
			//showLayer("HelpDiv");
	    }
	
	//Xmm = XmmM =(isNav) ? e.pageX : e.clientX;
	//Ymm = YmmM =(isNav) ? e.pageY : e.clientY;
	
	Xmm = (isNav) ? e.pageX : e.clientX;
	Ymm = (isNav) ? e.pageY : e.clientY;
	
	XmmM = (isNav) ? e.layerX : e.offsetX; 
	YmmM = (isNav) ? e.layerY : e.offsetY; 

	var rightclick;
	
	if (isNav) {rightclick=(e.which==2);}
	else {rightclick=(e.button==2);}
	
	if (rightclick==false)
	{
		
		getmapxy(XmmM,YmmM,'m');
		if (document.getElementById("hid_action").value != "measure")
		{
			window.status = "Image: X= " + XmmM + ", Y = " + YmmM + "" + " Map: X=" + moveX + " ,Y=" + moveY; 
		}
		if(zooming)
		{		
			getMouse();		
		}
		
		if (document.getElementById("hid_action").value == "move" && document.getElementById("hid_ispaning").value=='y')
		{
			var panStartX=document.getElementById("hid_panStartX").value;
			var panStartY=document.getElementById("hid_panStartY").value;
				//alert("0");		
			if ( (e.clientX <= originalPosX) || (e.clientX+1 >= originalPosX + MapWidth) || (e.clientY <= originalPosY) || (e.clientY+1 >= originalPosY + MapHeight))
			{
				handlerMU(e);
			}
			else
			{
					clip(Xmm,Ymm,panStartX,panStartY);
			}

		}
		
		if (document.getElementById("hid_action").value == "measure" && document.getElementById("hid_startmeasure").value == "start")
		{
			
			minx = parseFloat(document.getElementById("hid_minx").value);
			miny = parseFloat(document.getElementById("hid_miny").value);
			maxx = parseFloat(document.getElementById("hid_maxx").value);
			maxy = parseFloat(document.getElementById("hid_maxy").value);	
		
			mouseX = XmmM;
			pixelX = (maxx-minx) / MapWidth;
			mapX = pixelX * mouseX + minx;
			mouseY = MapHeight -YmmM;
			pixelY = (maxy-miny) / MapHeight;
			mapY = pixelY * mouseY + miny;		
			if(document.getElementById("hid_mapxdown").value != mapX )
			{			
				calcDistance(document.getElementById("hid_mapxdown").value,document.getElementById("hid_mapydown").value,mapX,mapY);
				document.frmMap.hid_meastext2.value = currentMeasure;
				//document.frmMap.hid_meastexts.value = document.frmMap.hid_meastexts.value + "," + currentMeasure;
				if (document.getElementById("meastype").value=="meters")
					{window.status =	"Distance=" + currentMeasure + " Meters";}
				else if(document.getElementById("meastype").value=="feet")
				{ window.status =	"Distance=" + currentMeasure  + " Feet";}
				else { window.status =	"Distance=" + currentMeasure  + " Kilometers";}
			}
		}
	}//end of rightclick checking	
}
//**********************************************************************
//Action to do when mouse up over the map
function handlerMU(e)
{

	if (isIE) e=event;
	
	
	//Xmu = XmuM =(isNav) ? e.pageX : e.clientX;
	//Ymu = YmuM =(isNav) ? e.pageY : e.clientY;
	
	Xmu = (isNav) ? e.pageX : e.clientX;
	Ymu = (isNav) ? e.pageY : e.clientY;
	
	XmuM = (isNav) ? e.layerX : e.offsetX ;
	YmuM = (isNav) ?  e.layerY: e.offsetY;

	var rightclick;
	
	if (isNav) rightclick=(e.which==2);
	else  rightclick=(e.button==2);
	
	if (rightclick==false)
	{
		xscroll = (isNav) ? 0 : document.body.scrollLeft;
		yscroll = (isNav) ? 0 : document.body.scrollTop;
		document.getElementById("hid_mouseupx").value = XmuM; //+xscroll;//Xmu-xFactor+xscroll;
		document.getElementById("hid_mouseupy").value = YmuM; //+yscroll;//Ymu-yFactor+yscroll;
		getmapxy(document.getElementById("hid_mouseupx").value,document.getElementById("hid_mouseupy").value,'u');
		
		if (document.getElementById("hid_action").value == "zoomin" || document.getElementById("hid_action").value == "zoomout")
		{	
		
		//alert("to mouse up");		
			chkMouseUp();	
			
			//check if user moved the mouse while zoomin or just clicked on the map
			//if user clicked on map <=> MD=MU then do fixed zoomin or fixed zoomout depending on the tool selected
			if (document.getElementById("hid_mousedownx").value == document.getElementById("hid_mouseupx").value && document.getElementById("hid_mousedowny").value == document.getElementById("hid_mouseupy").value)							
			{
			//alert("yes");
				if (document.getElementById("hid_action").value=="zoomin" )
					{
						
						document.getElementById("hid_action").value= "fixedzoomin";
					}
				else
					{
						document.getElementById("hid_action").value= "fixedzoomout";
					}
			}
			//end of checking
			
			Submit();
			showLayer("LoadMap");	
		}
		
		if (document.getElementById("hid_action").value=="pan")
		{
			document.getElementById("hid_ispaning").value='n';
		}
		
		if (document.getElementById("hid_action").value == "move")
		{
			getmapxy(e.clientX,e.clientY,"u");
			document.getElementById("hid_ispaning").value='n';
			hideLayer("imgDiv");
			Submit();
			showLayer("LoadMap");	
		}		
		if (document.getElementById("hid_action").value == "measure")
		{
			document.getElementById("hid_meastext2").value = 0;
		}			
	}//end of checking rightclick
}
//**********************************************************************

//Set position of image frame in main window
 function setOrigPos()
 {
 		var mapPos=map; //document.getElementById("imgDiv");
		originalPosX=findPosX(mapPos);	
		originalPosY=findPosY(mapPos);	
 }
 //**********************************************************************
 
 //Sets the measure type to feet or meters according to users preference
 function SetMeasureValue(type)
{
	if (document.getElementById("meastext1").value != ""){
		if (type=='m')
		{	
			if (document.frmMap.meastype.value=="feet") document.getElementById("meastext1").value=parseInt((document.getElementById("meastext1").value / 3.2808399) + 1);
			if (document.frmMap.meastype.value=="kilos") document.getElementById("meastext1").value=parseInt(document.getElementById("meastext1").value * 1000 );
			//document.frmMap.meastype.value="meters";
		}
		if (type=='f')
		{	
			if (document.frmMap.meastype.value=="meters") document.getElementById("meastext1").value=parseInt(document.getElementById("meastext1").value  * 3.2808399 );
			if (document.frmMap.meastype.value=="kilos") document.getElementById("meastext1").value=parseInt(document.getElementById("meastext1").value  * 1000 * 3.2808399) ;
			//document.frmMap.meastype.value="feet";
		}
		if (type=='k')
		{	
			if (document.frmMap.meastype.value=="meters") document.getElementById("meastext1").value=parseInt(document.getElementById("meastext1").value) / 1000 ;
			if (document.frmMap.meastype.value=="feet") document.getElementById("meastext1").value=parseInt((document.getElementById("meastext1").value / 3.2808399)+ 1)/1000 ;
			//document.frmMap.meastype.value="kilos";
		}
	}
	if (type=='m') document.frmMap.meastype.value="meters";
	if (type=='f') document.frmMap.meastype.value="feet";
	if (type=='k') document.frmMap.meastype.value="kilos";
	
	//document.getElementById("hid_action").value="";
}
 //**********************************************************************
  
  //function hides measure box from the top left of the map
  function HideMeasure()
  {
	hideLayer('measurediv');
	document.frmMap.hid_measurediv.value = "hidden";
  }
  //**********************************************************************
  
//SetZoomLevels changes the image for the zoom level buttons (checks selected level)
function SetZoomLevels(value)
{
	ClearZoomLevels();
	if (document.getElementById("imgZoomLevel" + value)) document.getElementById("imgZoomLevel" + value).src="../images/ToolBar/OverZoomBar.gif";
}
//**********************************************************************

//ClearZoomLevels unchecks all zoomlevel buttons)
function ClearZoomLevels()
{
var i=1 ;
var zoomlevels=11;

	while (i <= zoomlevels)
	{
		if (document.getElementById("imgZoomLevel" + i)) document.getElementById("imgZoomLevel" + i).src="../images/ToolBar/ZoomBar.gif";
		i=i+1;
	}

}
 //**********************************************************************
  
  //function used to enter data in the annotation label when the label is clicked
  function EnterAnnot()
  {
	document.getElementById("lblannotation").innerHTML="<input id=txtannotation style='border-width:0' onkeydown='javascript:CheckEnter();'>";  
	document.getElementById("txtannotation").focus();
  }
   //**********************************************************************
      
   //function CheckEnter checks if user presses enter key in the text box for annotation
   function CheckEnter()
   {	
   		var check = IsEnterPressed();
		if (check==true)
		{	
			document.getElementById("hid_action").value="addannotation";
			document.frmMap.hid_annotation.value=document.getElementById("txtannotation").value;			
			document.frmMap.hid_annotationxdown.value=document.getElementById("hid_mapxdown").value;
			document.frmMap.hid_annotationydown.value=document.getElementById("hid_mapydown").value;
			Submit();
		}
   }
   
   //**********************************************************************
   
//---------------------------------------test functions for panning the map-----------------------//

function clip(Xend, Yend,panStartX,panStartY)
{
	var left,right,top,bottom;
	
	
	var Xvalue=0; 
	var Yvalue=0;
	Xvalue=Xend - panStartX;
	Yvalue=Yend - panStartY;
	if (Xvalue > 0)
	{
		left=0;
		right=MapWidth-Xvalue;
	}
	else 
	{
		left=0-Xvalue;
		right=MapWidth;
	}
	if (Yvalue>0)
	{
		top=0;
		bottom=MapHeight-Yvalue;
	}
	else 
	{
		top=0-Yvalue;
		bottom=MapHeight;
	}	
	
	clipmap(left,top,right,bottom);
	moveLayer2("imgDiv",Xvalue + originalPosX,Yvalue + originalPosY);
	
	
}
function clipmap(clipleft, cliptop, clipright, clipbottom)
{
	document.getElementById("imgDiv").style.clip= 'rect(' + cliptop + ' ' + clipright + ' ' + clipbottom + ' ' + clipleft + ')';
	
}


function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
		
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	
	return curtop;
}


// get the layer object called "name"
function getLayer2(name) {
	  if (isNav4)
	    return(document.layers[name]);
	  else if (isIE4) {
	  	if ( eval('document.all.' + name) != null) {
		    layer = eval('document.all.' + name + '.style');
		    return(layer);
		} else
			return(null);
	  } else if (is5up) {
	  var theObj = document.getElementById(name);
		return theObj.style
	  } else
	    return(null);
}

// move layer to x,y
function moveLayer2(name, x, y) {
  	var layer = getLayer2(name);  	
	if (layer != null) {
	  	if (isNav4)
    		layer.moveTo(x, y);
	 	else if (isIE) {
	 	
	 	
    		layer.left = x + "px";
   			layer.top  = y + "px";
		} else {
			layer.height = MapHeight - y;
			layer.width	= MapWidth - x;
    		layer.left = x + "px";
   			layer.top  = y + "px";
	  	}
	}
}



