//################################################################
//#                                                              #
//# DEVELOPED BY:                                                #
//#                                                              #
//#                 Applied GIS, Inc.                            #
//#                 137 Jay Street                               #
//#                 Schenectady, NY 12305                        #
//#                 Phone: (518) 346-0942                        #
//#                 Fax: (518) 346-5322                          #
//################################################################
//#                                                              #
//# FILE:                                                        #
//#                                                              #
//#                                                              #
//#                                                              #
//# DESCRIPTION:                                                 #
//#                                                              #
//#                                                              #
//#                                                              #
//#                                                              #
//################################################################

var vi;


function checkval(string, fieldname)
{
		var ErrorNoVal = "Please enter a " + fieldname + " value";
		var ReturnVal = true;
		if (!string)
		{
			alert(ErrorNoVal);
			ReturnVal = false;
		}
	return ReturnVal;
}

function updateLayers(name,val,updatemap,val2)
{
	var fWeight = '';
	if (!(isNav4))
	{
		if(document.getElementById)
		{
			if (val == true)
			{
				fWeight = 'bold';	
			}
			else
			{
				fWeight = 'normal';
			}
		}
		obj = document.getElementsByTagName("font");
		obj[name].style.fontWeight = fWeight;			
	}
	if (updatemap)
	{
		parent.asp.location = 'UPDATE_LAYERS.ASP?' + name + '=' + val + '&LAYER_ID=' + val2;
		//parent.asp.location = 'UPDATE_MAP.ASP?' + name + '=' + val;
	}
}

function updateText(name,val,updatemap) 
{
	var fWeight = '';
	if (!(isNav4))
	{
		if(document.getElementById)
		{
			if (val == true)
			{
				fWeight = 'bold';	
			}
			else
			{
				fWeight = 'normal';
			}
		}
		obj = document.getElementsByTagName("font");
		obj[name].style.fontWeight = fWeight;			
	}
	if (updatemap)
	{
		parent.asp.location = 'UPDATE_MAP.ASP?' + name + '=' + val;
	}
}


function checkBrowser()
{
	if (browser == "Netscape") 
	{
		isNav = true;
		if (is_nav6up) 
		{
			itsOK = true;	
		} 
		else
		{
			itsOK = false;
		}
	} 
	else if (browser.indexOf("Microsoft") > -1) 
	{
		isIE = true;
		if (is_ie5_5up) 
		{
			itsOK = true;
		} 
		else 
		{
			itsOK = false;
		}
		//if (navigator.platform == "MacPPC") 
		//{
		//	itsOK = false;
		//}
	}
	else
	{
		itsOK = false;
	}
}

function saveMenu(SaveOption)
{
//#########################################################
//# FUNCTION: saveMenu()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################	var loc;
	if (SaveOption =='doc')
	{
		loc = '../save/save_rtf_doc.asp?f=' + SaveOption;
	}

	if (SaveOption =='csv')
	{
		loc = '../save/save_csv.asp?';
	}

	if (SaveOption =='rtf')
	{
		loc = '../save/save_rtf_doc.asp?f=' + SaveOption;
	}

	window.open(loc,'save','screenX=0,screenY=0,top=0,left=0,menubar=yes,resizable=yes,scrollbars=yes,width=720,height=500');
}

function switchTableColor(Input,pos)
{
//#########################################################
//# FUNCTION: switchTableColor()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
  if (isIE || isNav6)
  {
   for (var i=0; i< document.frm.length -1 ; i++)
	{
		if (i == pos)
		{
			if (isIE)
				eval("t"+i+".style.backgroundColor='" +bgcolor_on+"'");	
			else
				document.getElementById("t"+ i).style.backgroundColor=bgcolor_on;	
		}
		else
		{
			if (isIE)
				eval("t"+i+".style.backgroundColor='"+bgcolor_off+"'");
			else
				document.getElementById("t"+i).style.backgroundColor=bgcolor_off;
		}
  }
} 
}



function mWidth(frameloc)
//#########################################################
//# FUNCTION: mWidth()
//# DESCRIPTION: 
//# INPUT: 
//# blank
//# OUTPUT: 
//##########################################################
{
	var iw;
	if (frameloc == null)
	{
	if (window.innerWidth == null) 
			{
				iw = document.body.clientWidth;
			}
			 else 
			{
				iw = window.innerWidth;
			}
	}
	else
	{
	if (frameloc.window.innerWidth == null) 
			{
				iw = frameloc.document.body.clientWidth;
			}
			 else 
			{
				iw = frameloc.window.innerWidth;
			}
	}
    return iw;
}

function mHeight(frameloc)
//#########################################################
//# FUNCTION: mHeight()
//# DESCRIPTION: 
//# INPUT: 
//# blank
//# OUTPUT: Height available in frame
//##########################################################
{
	var ih;
	if (frameloc == null)
	{
		if (window.innerWidth == null) 
		{
		ih=document.body.clientHeight; 
		}
		 else 
		 {
			ih = window.innerHeight;
		 }
	}
	else
	{
		if (frameloc.window.innerWidth == null) 
		{
			ih=frameloc.document.body.clientHeight; 
		}
		 else 
		{
			ih = frameloc.window.innerHeight;
		}
	 }
	return ih;
}

function loadPage(pagename,pagelocation)
//#########################################################
//# FUNCTION: loadpage()
//# DESCRIPTION: Loads a page in a give frame
//# INPUT: Page to be loaded, location where page is to be loaded (parent.framename)
//# OUTPUT: 
//##########################################################
{
	if (pagename == null)
	{
		alert('ERROR:\nfunction loadpage()\nNo value given for first parameter ')
	}
	 else
	{	
		if (pagelocation == null)
		{
			document.location.href = pagename
		}
		 else
		{
			pagelocation.location.href =  pagename
		}
	}
} 



function Xcoord(e) 
{
//#########################################################
//# FUNCTION: Xcoord()
//# DESCRIPTION: get the mouseclick x
//# INPUT: javascript event
//# OUTPUT:  X coordinates of mouseclick on image
//##########################################################
	if (window.event) 
	{
		return window.event.clientX
	}
    else 
	{
       return e.pageX
	}
}

function Ycoord(e) 
//#########################################################
//# FUNCTION: Ycoord()
//# DESCRIPTION: get the y
//# INPUT: javascript event
//# OUTPUT: Y coordinate of mouseclick on image
//##########################################################
{      
   	if (window.event) 
		{
			return window.event.clientY;
		}
    else 
		{
			return e.pageY
		}	
}

function RemoveImage(windowName)
//#########################################################
//# FUNCTION: RemoveImage()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{
// if (!opener.closed)
 // {  
//	opener.parent.window_manage.location.href = '../window_manage.asp?item='+windowName+'&val=f';
 // }
}


function close_windows()
//#########################################################
//# FUNCTION: close_windows()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{

	if (iw)
	{
		if (!iw.closed)
		{
			iw.close();
		}
	}

	if (pt)
	{
		if (!pt.closed)
		{
			pt.close();
		}
	}

	if (vi)
	{
		if (!vi.closed)
		{
			vi.close();
		}
	}
	
		if (gc)
	{
		if (!gc.closed)
		{
			gc.close();
		}
	}
	
		if (mc)
	{
		if (!mc.closed)
		{
			mc.close();
		}
	}
	
		if (sw)
	{
		if (!sw.closed)
		{
			sw.close();
		}
	}
	
	
	if (qw)
	{
		if (!qw.closed)
		{
			qw.close();
		}
	}
	if (bw)
	{
		if (!bw.closed)
		{
			bw.close();
		}
	}
}


function switchImage(ImageName,ImagePath,ImageLocation)
//#########################################################
//# FUNCTION: switchImage()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{
		if (ImageLocation == null)
		{
			document.images[ImageName].src= ImagePath;
		}
		 else
		{
			ImageLocation.document.images[ImageName].src= ImagePath;
		}
}

function OpenResultsWin(record_count)
{
	var records_win;
	//records_win = window.open('show_records.asp','records_win');
	
	if (record_count == 0)
	{
	//records_win.close();
	}
	
	if (record_count !== 0)
	{
	//records_win = window.open('show_records.asp','records_win','screenX=0,screenY=0,top=0,left=0,menubar=no,resizable=yes,scrollbars=yes,width=600,height=450');
	parent.RESULTS.location.href='show_records.asp';
	}
}


function identify(e)
//#########################################################
//# FUNCTION: identify()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{
	iw=window.open('../identify/identify.asp?x1=' + mouseX + '&y1=' + mouseY,'iw','scrollbars=yes,resizable=yes,toolbar=yes,status=yes,width=400,height=300,screenX=0,screenY=0,top=0,left=0')
//=window.open('NA.ASP','iw','scrollbars=yes,width=200,height=200,screenX=0,screenY=0,top=0,left=0');
}

function remove_select(e) 
{
	parent.asp.location.href = '../select/remove_select.asp?x1=' + mouseX + '&y1=' + mouseY;
	parent.map.layerShow('LoadMap'); 
}


function select(e) 
{
	parent.RESULTS.location.href = '../select/select.asp?x1=' + mouseX + '&y1=' + mouseY;
	parent.map.layerShow('LoadMap'); 
}


function disp_coord(e) 
{
	parent.map.layerHide("measureBox");
	parent.asp.location.href = '../MCODE/MAP_COORDINATE.asp?ORIGIN=FROM_MAP&DD_LAT=' + mouseX + '&DD_LONG=' + mouseY;
	parent.map.layerShow('LoadMap'); 
	parent.map.layerShow('coordBox'); 
}

function select_box(e) 
{


	//parent.tools.createWindow('select_feature.asp?l='Tax Parcels'&t=select_box&x1=' + x1 + '&y1=' + y1 + '&x2='+x2+'&y2=' +y2,'no','no','yes','yes','yes','no',20,20,600,500,'query');
	parent.RESULTS.location.href = '../select/select_feature.asp?l='+activeLayer+'&t=select_box&x1=' + x1 + '&y1=' + y1 + '&x2='+x2+'&y2=' +y2;
	//parent.map.layerShow('LoadMap');
}

function select_line(e) {

	//parent.tools.createWindow('select_feature.asp?l='Tax Parcels'&t=select_line&x1=' + x1 + '&y1=' + y1 + '&x2='+x2+'&y2=' +y2,'no','no','yes','yes','yes','no',20,20,600,500,'query');
	parent.RESULTS.location.href = '../select/select_feature.asp?l='+activeLayer+'&t=select_line&x1=' + mouseX + '&y1=' + mouseY;
	//parent.map.layerShow('LoadMap');
}

function select_poly(e) {

	//parent.tools.createWindow('select_feature.asp?l='Tax Parcels'&t=select_line&x1=' + x1 + '&y1=' + y1 + '&x2='+x2+'&y2=' +y2,'no','no','yes','yes','yes','no',20,20,600,500,'query');
	parent.RESULTS.location.href = '../select/select_feature.asp?l='+activeLayer+'&t=select_poly&x1=' + mouseX + '&y1=' + mouseY;
	//parent.map.layerShow('LoadMap');
}



function zoomFullExtent() 
//#########################################################
//# FUNCTION: zoomFullExtent()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{
	layerShow('LoadMap'); 
	parent.asp.location='../pan_zoom/pan_zoom.asp?action=zoomFull';
}

function zoomin(e) 
//#########################################################
//# FUNCTION: zoomin()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{
	layerShow('LoadMap'); 
	parent.asp.location='../pan_zoom/pan_zoom.asp?action=zoomin&x1=' + mouseX + '&y1=' + mouseY + '&x2=0&y2=0';
}

function zoomin2(e) 
//#########################################################
//# FUNCTION: zoomin2()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{
	layerShow('LoadMap'); 
	parent.asp.location='../pan_zoom/pan_zoom.asp?action=zoomrect&x1=' + x1 + '&y1=' + y1 + '&x2=' + x2 + '&y2=' + y2;
}


function zoomout(e) 
//#########################################################
//# FUNCTION: zoomout()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{
	layerShow('LoadMap'); 
	parent.asp.location='../pan_zoom/pan_zoom.asp?action=zoomout&x1=' + mouseX + '&y1=' + mouseY + '&x2=0&y2=0';
}	

function zoomout2(e) 
//#########################################################
//# FUNCTION: zoomout2()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{
	layerShow('LoadMap'); 
	var mWidth = getMapWidth();
	var mHeight = getMapHeight();
	var zWidth = Math.abs(zright-zleft);
	var zHeight = Math.abs(ztop-zbottom);
	var xRatio = mWidth / zWidth;
	var yRatio = mHeight / zHeight;
	x1 = 0 - xRatio * (zleft-0);
	x2 = mWidth + xRatio * (mWidth-zright);
	y1 = 0 - yRatio * (ztop-0);
	y2 = mHeight + yRatio * (mHeight-zbottom);
	parent.asp.location='../pan_zoom/pan_zoom.asp?action=zoomrect&x1=' + x1 + '&y1=' + y1 + '&x2=' + x2 + '&y2=' + y2;
}



// start pan.... image will move
function panStart(e) 
//#########################################################
//# FUNCTION: panstart()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{
	layerMove("theMap",hspc,vspc);
	getImageXY(e);
	if ((mouseX<iWidth) && (mouseY<iHeight)) {
		if (panning) {
			panStop(e);
		} else {
			x1=mouseX;
			y1=mouseY
			x2=x1+1;
			y2=y1+1;
			panning=true;
		}
	}
	return false;
}

// move map image with mouse
function panMouse() 
//#########################################################
//# FUNCTION: panMouse()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{
	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;
	}
	layerClip("theMap",cLeft,cTop,cRight,cBottom);
	layerMove("theMap",xMove+hspc,yMove+vspc);
}

// stop moving image.... pan 
function panStop(e)
//#########################################################
//# FUNCTION: panStop()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{
	layerShow('LoadMap'); 
	window.scrollTo(0,0);
	panning=false;
	layerHide("theMap");
	if ((Math.abs(x2-x1)<2) && (Math.abs(y2-y1)<2)) {
		pan(e);
	} else {
		parent.asp.location='../pan_zoom/pan_zoom.asp?action=pan&x1=' + x1 + '&y1=' + y1 + '&x2=' + x2 + '&y2=' + y2;
	}
	window.setTimeout('layerMove("theMap",hspc,vspc);',2000);
	//window.setTimeout('layerShow("theMap");',2500);
	window.setTimeout('layerClip("theMap",0,0,iWidth,iHeight);',2500);
	//window.setTimeout('layerHide("LoadMap");',2500);
	return true;
}

// pan to mouse click
function pan(e) 
//#########################################################
//# FUNCTION: pan()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{
	layerShow('LoadMap'); 
	parent.asp.location='../pan_zoom/pan_zoom.asp?action=center&x1=' + x2 + '&y1=' + y2 + '&x2=0&y2=0';
}	


// get the Map Image width
function getMapWidth () 
//#########################################################
//# FUNCTION: getMapWidth()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{
	var mapFrameWidth = window.innerWidth;
	if (mapFrameWidth == null)
	{
		mapFrameWidth = window.document.body.clientWidth;
	}
	return mapFrameWidth;
}

 //get the Map Image height
function getMapHeight () 
//#########################################################
//# FUNCTION: getMapHeight()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{
	var mapFrameHeight = window.innerHeight;
	if (mapFrameHeight == null)
	{
		mapFrameHeight = window.document.body.clientHeight;
	}
	return mapFrameHeight;
}

//keep track of currently selected tool, and display it to user
function clickFunction(toolName) 
//#########################################################
//# FUNCTION: clickFunction()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{
	measureClear();
	if (toolName == "zoomin") 
	{
		toolMode = 1;
		panning=false;
		selectBox=false;
		if (top.isIE) {document.all.theMap.style.cursor = "crosshair";}	
	}
	if (toolName == "zoomout") 
	{
		toolMode = 2;
		panning=false;
		selectBox=false;
		if (top.isIE) {document.all.theMap.style.cursor = "crosshair";}
	}
	if (toolName == "pan") 
	{
		toolMode = 3;
		dragging=false;
		selectBox=false;
		if (top.isIE) {document.all.theMap.style.cursor = "move";}
	}
	if (toolName == "identify") 
	{
		toolMode = 4;
		if (top.isIE) {document.all.theMap.style.cursor = "crosshair";}	
	}
	if (toolName == "viewimage") 
	{
		toolMode = 44;
	}
	if (toolName == "address") 
	{
		toolMode = 5;
		if (top.isIE) {document.all.theMap.style.cursor = "crosshair";}	
	}
	if (toolName == "select")
	{
		toolMode = 10;
		if (top.isIE) {document.all.theMap.style.cursor = "crosshair";}		
	}	
	if (toolName == "remove_select")
	{
		toolMode = 12;
		if (top.isIE) {document.all.theMap.style.cursor = "crosshair";}		
	}	
	if (toolName == "dispcoord")
	{
		toolMode = 13;
		parent.map.layerShow("coordBox");
		if (top.isIE) {document.all.theMap.style.cursor = "crosshair";}		
	}	
	if (toolName == "select_box")
	{
		toolMode = 200;
		dragging=false;
		selectBox=true;
		if (top.isIE) {document.all.theMap.style.cursor = "crosshair";}		
	}	
	if ((toolName == "select_line"))
	{
		toolMode = 201;
		if (top.isIE) {document.all.theMap.style.cursor = "crosshair";}	
	}
	if ((toolName == "select_poly"))
	{
		toolMode = 202;
		if (top.isIE) {document.all.theMap.style.cursor = "crosshair";}	
	}
	if (toolName == "measure") 
	{
		if (top.isIE) {document.all.theMap.style.cursor = "crosshair";}		
		measureStart0();
		toolMode = 21;
	}
	if (toolName == "search") 
	{
		sw = window.open('../query/QUERY_FRAME.asp','sw','resizable=yes,screenX=0,screenY=0,top=0,left=0,status=yes,menubar=yes,scrollbars=yes,width=600,height=500');
	}
	if (toolName == "query") 
	{
		toolMode = 201;
		dragging=false;
		selectBox=false;		
		panning=false;
		qw = window.open('../query/query.asp','qw','screenX=0,screenY=0,top=0,left=0,status=yes,scrollbars=yes,width=620,height=500');
	}
	if (toolName == "thematic_maps")
	{ tm=window.open('../thematic_map/thematic_map.asp','tw','scrollbars=yes,width=250,height=200,screenX=0,screenY=0,top=0,left=0');
	}
	if (toolName == "buffer")	
	{ bw=window.open('../buffer/buffer.asp','bw','resizable=yes,scrollbars=yes,width=600,height=450,screenX=0,screenY=0,top=0,left=0');
	}
	if (toolName == "add_rem_buffer")	
	{
		toolMode = 33;
		if (top.isIE) {document.all.theMap.style.cursor = "crosshair";}	
	}
	if (toolName == "add_rem_query")	
	{
		toolMode = 34;
		if (top.isIE) {document.all.theMap.style.cursor = "crosshair";}	
	}
	if (toolName == "zoomFull") 
	{
		zoomFullExtent();
	}
	if (toolName == "reset_map")
	{
		top.location.reload();
		//loadPage('RESET_MAP.asp', parent.asp);	
	}
	if (toolName == "clearAll")
	{
		loadPage('CLEAR.asp', parent.asp);	
	}
	if (toolName == "print_html") 
	{
		pt = window.open('../print/print.asp','pt','resizable=yes,screenX=0,screenY=0,top=0,left=0,status=yes,menubar=yes,scrollbars=yes,toolbar=yes,width=700,height=500');	
	}
	if (toolName == "print_pdf") 
	{
		pt = window.open('../PRINT/PRINT_PDF.asp','pt','resizable=yes,screenX=0,screenY=0,top=0,left=0,status=yes,menubar=yes,scrollbars=yes,toolbar=yes,width=500,height=400');	
	}
	if (toolName == "print_parcel") 
	{
		pt = window.open('../print/print_parcel.asp','pt','resizable=yes,screenX=0,screenY=0,top=0,left=0,status=yes,menubar=yes,scrollbars=yes,toolbar=yes,width=500,height=400');	
	}
	if (toolName == "IM_LINK") 
	{
    if (!window.il)
    {
    il = window.open('../print/IM_LINK.asp','il','resizable=yes,screenX=0,screenY=0,top=0,left=0,status=yes,menubar=yes,scrollbars=yes,toolbar=yes,width=500,height=400');	
    }
    else if (il.closed)
    {
    il = window.open('../print/IM_LINK.asp','il','resizable=yes,screenX=0,screenY=0,top=0,left=0,status=yes,menubar=yes,scrollbars=yes,toolbar=yes,width=500,height=400');	
    }
    else
    {
    il.open('../print/IM_LINK.asp','il','resizable=yes,screenX=0,screenY=0,top=0,left=0,status=yes,menubar=yes,scrollbars=yes,toolbar=yes,width=500,height=400');	
    }
	}
	if (toolName == "gcode") 
	{
		gc = window.open('../GCODE/GEOCODE_FORM.asp','gc','resizable=yes,creenX=0,screenY=0,top=0,left=0,status=yes,menubar=yes,scrollbars=yes,toolbar=yes,width=500,height=400');	
	}
	if (toolName == "mcode") 
	{
		mc = window.open('../MCODE/MAP_COORDINATE_FORM.asp','mc','resizable=yes,screenX=0,screenY=0,top=0,left=0,status=yes,menubar=yes,scrollbars=yes,toolbar=yes,width=700,height=500');	
		layerShow('coordBox');
	}
	if (toolName == "help")
	{
		h=window.open('../help/SDV_Help_System.htm','h');
	}
	if (toolName == "layerlist") 
	{
		TOCtoggle();
	}
}

// mousemove handler
function mouseMove(e) 
//#########################################################
//# FUNCTION: mouseMove()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{
	window.status="";
	getImageXY(e);
	if ((mouseX>iWidth) || (mouseY>iHeight) || (mouseX<=0) ||(mouseY<=0)) {
		mouseIsDown=false;
		mouseUp(e);
	} else {
		if ((dragging) || (selectBox)) {
			x2=mouseX;
			y2=mouseY;
			layerSetClip();
		} else if (panning) {
			x2=mouseX;
			y2=mouseY;
			panMouse();	
		} else if (measuring) {
			x2=mouseX;
			y2=mouseY;	
			measureMove();
		}
	}
		return false;
}

// mouseup handler
function mouseUp(e) 
//#########################################################
//# FUNCTION: mouseUp()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{ 	
if (toolMode==1 || toolMode ==2 || toolMode==3 || toolMode ==11 || toolMode ==20)
	{

	//	layerShow('LoadMap');

	}
	mouseIsDown=false;
	if (dragging) {
		boxStop(e);
	}
	if ((toolMode == 3) && (panning)) {
		panStop(e);
	}
	return false;
}

// mousedown handler
function mouseDown(e) 
//#########################################################
//# FUNCTION: mouseDown()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{
	mouseIsDown=true;
	getImageXY(e);
	if ((mouseX>=0) && (mouseX<iWidth) && (mouseY>=0) && (mouseY<iHeight))
	{
		if (toolMode == 1) 
		{
			boxStart(e);
			return false;
		}
		
		if (toolMode == 2) 
		{
			boxStart(e);
			return false;
		}
		
		if (toolMode == 3) 
		{
			panStart(e);
			return false;
		}
		
		if (toolMode == 4) 
		{
			identify(e);
		}

	if (toolMode == 10) 
		{
			select();
		}
	if (toolMode == 33)
		{
		bw=window.open('../buffer/ADD_REM_BUFFER.ASP?x1=' + mouseX + '&y1=' + mouseY,'bw','resizable=yes,scrollbars=yes,width=600,height=450,screenX=0,screenY=0,top=0,left=0');
		}
		if (toolMode == 34)
		{
sw=window.open('../QUERY/ADD_REM_QUERY.ASP?x1=' + mouseX + '&y1=' + mouseY,'sw','resizable=yes,scrollbars=yes,width=600,height=450,screenX=0,screenY=0,top=0,left=0');
		}
		if (toolMode == 44)
		{
vi=window.open('../CORE/VIEW_IMAGE.ASP?x1=' + mouseX + '&y1=' + mouseY,'vi','resizable=yes,scrollbars=yes,status=yes,toolbar=yes,width=600,height=450,screenX=0,screenY=0,top=0,left=0');
			}
	if (toolMode == 12) 
		{
			remove_select();
		}
	if (toolMode == 13) 
		{
			disp_coord();
		}
		
		if (toolMode == 200) 
		{	
			boxStart(e);
			return false;
		}

		if (toolMode == 201) 
		{
			select_line();		
		}

		if (toolMode == 202) 
		{
			select_poly();		
		}



		if (toolMode ==20) 
		{
			measureClick(e);

		}
		
		if (toolMode ==0) 
		{
			alert("Please select a \"Map Tool\" to interact with the map.  Your current tool is \"" + parent.currenttool.operation + "\".");
			layerHide('LoadMap');
		}
	}

	return false;
}

function getImageXY(e) 
//#########################################################
//# FUNCTION: getImageXY()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
  {
	if ((top.isNav) || (top.isNav6)) 
	  {
		mouseX = e.pageX;
		mouseY = e.pageY;
		altkey = e.modifiers&Event.ALT_MASK;
		ctrlkey = e.modifiers&Event.CONTROL_MASK;
		shiftkey = e.modifiers&Event.SHIFT_MASK;
	  } 
	else
	  {
		mouseX = event.clientX + document.body.scrollLeft;
		mouseY = event.clientY + document.body.scrollTop;
		altkey = event.altKey;
		ctrlkey = event.ctrlKey;
		shiftkey = event.shiftKey;
	  }
	mouseX = mouseX-hspc;
	mouseY = mouseY-vspc;

}	


function boxStart(e) 
//#########################################################
//# FUNCTION: boxStart()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{
	layerMove("theMap",hspc,vspc);
	getImageXY(e);	
	if ((mouseX<iWidth) && (mouseY<iHeight)) {
		if (dragging) {
			boxStop(e);
		} else {
			x1=mouseX;
			y1=mouseY
			x2=x1+1;
			y2=y1+1;
			layerSetClip();
			dragging=true;
			layerShow("zoomBoxTop");
			layerShow("zoomBoxLeft");
			layerShow("zoomBoxRight");
			layerShow("zoomBoxBottom");
		}
	}
	return false;
}

// stop drawing box display
function boxStop(e) 
//#########################################################
//# FUNCTION: boxStop()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{
	dragging=false;
	if ((zright <zleft+2) && (ztop < zbottom+2)) {
		if(toolMode==1) zoomin(e);
		if(toolMode==2) zoomout(e);
		if(toolMode==10) select(e);
		if(toolMode==12) remove_select(e);
		if(toolMode==200) select_box(e);
	} else {
		window.scrollTo(0,0);
		if(toolMode==1) zoomin2(e);
		if(toolMode==2) zoomout2(e);
		if(toolMode==10) select(e);
		if(toolMode==12) remove_select(e);
		if(toolMode==200) select_box(e);
	}
	layerHide("zoomBoxTop");
	layerHide("zoomBoxLeft");
	layerHide("zoomBoxRight");
	layerHide("zoomBoxBottom");
	return true;
}



function TOCDisplay() 
//#########################################################
//# FUNCTION: TOCDisplay()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{
	var win1 = window.open("writeTOC.asp","toc","width=250,height=300,scrollbars=yes,resizable=yes,toolbar=no");
}

function TOCtoggle() 
//#########################################################
//# FUNCTION: TOCtoggle()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{
	var curr = parent.layerlegend.location + ' ';
	var type = 0;
	if (curr.indexOf('display_layers.asp') > 0) {type = 1;}
	if (curr.indexOf('display_legend.asp') > 0) {type = 2;}
	if (type==1) {parent.layerlegend.location = 'layer_legend/display_legend.asp';}
	if (type==2) {parent.layerlegend.location = 'layer_legend/display_layers.asp';}
}


function measureStart0() 
//#########################################################
//# FUNCTION: measureStart0()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{
	parent.map.layerHide("coordform");
	parent.measure_config.location.href = '../measure/measure_config.asp?TOOL=MEASURE';
	parent.asp.location = '../measure/measure.asp';
}

function measureStart1() 
//#########################################################
//# FUNCTION: measureStart1()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{
	panning=false;
	dragging=false;
	selectBox=false;
	toolMode = 20;
	layerShow("measureBox");
}

function updateCoord(x,y) 
//#########################################################
//# FUNCTION: updateCoord(x,y)
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{

	if (isNav4) 
	{
		var theForm = document.layers["coordform"].document.forms[1];
	} 
	else 
	{
		var theForm = document.forms[1];
	}

	theForm.XCOORD.value = x;
	theForm.YCOORD.value = y;
	
}

function measureClick(e) 
//#########################################################
//# FUNCTION: measureClick()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{

	if (isNav4) 
	{
		var theForm = document.layers["measureBox"].document.forms[0];
	} 
	else 
	{
		var theForm = document.forms[0];
	}	



	measuring = true;
	getImageXY(e);
	x1 = mouseX;
	y1 = mouseY;
	parent.asp.location.href ='../measure/measure_draw.asp?x='+ mouseX+'&y='+mouseY;
	toMapPoint(x1,y1);
	mapAX = mapX;
	mapAY = mapY;
var unitText = "";

if (mapUnits == "M")
{
	unitText = " Miles";
}
if (mapUnits == "F")
{
	unitText = " Feet";
}

	if (measureAdd == 0 && (mDistance ==0)) 
	{
		theForm.theMeasTotal.value = 0 + unitText;
	}
	else
	{
		if (firstTime == true)
		{	
			measureAdd = mDistance;
			if (measureAdd > 0)
			{
				if (mapUnits == "M")
				{
					theForm.theMeasTotal.value = ConvertToDecimalPlace(measureAdd) + unitText;
				}
				else
				{
					theForm.theMeasTotal.value = measureAdd + unitText;
				}
			}			
			firstTime = false;
		}
		else
		{
			measureAdd = measureAdd + mDistance;
			if (measureAdd > 0)
			{
				if (mapUnits == "M")
				{
					theForm.theMeasTotal.value = ConvertToDecimalPlace(measureAdd) + unitText;
				}
				else
				{
					theForm.theMeasTotal.value = measureAdd + unitText;
				}	
			}
		}

	}


}

function toMapPoint(x,y) 
//#########################################################
//# FUNCTION: toMapPoint()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{
	mapX = mapOX + (x * mapPixX);
	mapY = mapOY + (y * mapPixY);
}

function measureMove() 
//#########################################################
//# FUNCTION: measureMove()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{
	toMapPoint(x2,y2);
	mapBX = mapX;
	mapBY = mapY;
	mDistance = 0;
	var numDecimals = 2;
	var unitText = "";
	if (mapUnits=="D") {
		var Lon1 = mapAX * Math.PI / 180;
		var Lat1 = mapAY * Math.PI / 180;
		var Lon2 = mapBX * Math.PI / 180;
		var Lat2 = mapBY * Math.PI / 180;
		var LonDist = (Lon2-Lon1);	//dx
		var LatDist = (Lat2-Lat1);	//dy
		var A = Math.sin(LatDist/2) * Math.sin(LatDist/2);
		var B = Math.cos(Lat2) * Math.cos(Lat1);
		var C = Math.sin(LonDist/2) * Math.sin(LonDist/2);
		var D = Math.sqrt(A + B * C);
		mDistance = Math.asin(D) * 2 * 3956.73;
	}
	if (mapUnits=="M") 
	{
		var xD = Math.abs(mapBX - mapAX);
		var yD = Math.abs(mapBY - mapAY);
		var D = Math.sqrt(Math.pow(xD,2) + Math.pow(yD,2));
		mDistance = D / 5280;
		mDistance = ConvertToDecimalPlace(mDistance);//Math.round();
		unitText = " Miles";
	}
	if (mapUnits=="F") 
	{
		var xD = Math.abs(mapBX - mapAX);
		var yD = Math.abs(mapBY - mapAY);
		var D = Math.sqrt(Math.pow(xD,2) + Math.pow(yD,2));
		mDistance = Math.round(D); 
		unitText = " Feet";
	}
	var u = Math.pow(10,numDecimals);
	mDistance = parseInt(mDistance*u+0.5)/u;	
	
	if (isNav4) 
	{
		var theForm = document.layers["measureBox"].document.forms[0];
	} 
	else 
	{
		var theForm = document.forms[0];
	}	

	layerShow("measureBox");
	if (mDistance > 0)
	{
		theForm.theMeasSegment.value = mDistance + unitText;
	}
}

function measureClear() 
//#########################################################
//# FUNCTION: measureClear()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{
	measuring = false;
	layerHide("measureBox");
}


function ConvertToDecimalPlace(n) 
//#########################################################
//# FUNCTION: ConvertToDecimalPlace()
//# DESCRIPTION: 
//# INPUT: 
//# OUTPUT: 
//##########################################################
{
//alert('inConverToDecimal');
  var s = "" + Math.round(n * 100) / 100;
  var i = s.indexOf('.')
  if (i < 0) return s + ".00"
  var t = s.substring(0, i + 1) + s.substring(i + 1, i + 3)
  if (i + 2 == s.length) t += "0"
  return t
}
