// aimsClick.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*		dependent on aimsXML.js, ArcIMSparam.js, aimsCommon.js, aimsMap.js,
*		aimsLayers.js, aimsDHTML.js
*		aimsNavigation.js
*/

var aimsClickPresent=true;

// Global vars to save mouse position
var mouseX=0;
var mouseY=0;
var x1=0;
var y1=0;
var x2=0;
var y2=0;
var zleft=0;
var zright=0;
var ztop=0;
var zbottom=0;

var totalMeasure=0;
var currentMeasure=0;
var lastTotMeasure=0;

// Var for Toggling Zooming to Selected Features - CVG
var ZoomToSelected=0;

// variables for interactive clicks
var clickCount = 0;
var clickPointX = new Array();
var clickPointY = new Array();
var clickMeasure = new Array();
	// type - 1=Measure; 2=SelectLine ; 3=SelectPolygon
var clickType = 1;
var flags = ["SelectPoint1","SelectPoint2","SelectPoint3","SelectPoint4","SelectPoint5","SelectPoint6","SelectPoint7","SelectPoint8","SelectPoint9","SelectPoint10"];


var panning=false;
var zooming=false;
var redlining=false;
var selectBox=false;
var blankImage = "images/map.gif";

//BusFinder....indicates which selection type will be activated when page loads
var busSelType = 0;
var customLeft = 0;		//The four envelope coordinates when an extent is programatically specified
var customBottom = 0;
var customRight = 0;
var customTop = 0;
var useCustomCoords = false;	//designates when a map refresh should zoom into a pre-defined extent
var showOneRoute = false;	//designates when the Bus Routes layer should only display one route
var busRoute = 100; 			//The one route to be displayed when 'showOneRoute' is true
var busIsUp = false;		//helps route the proper page into the TextFrame

//ParkFinder...indicates which selection type will be activated when page loads
var parkSelType = 0;
var parkIsUp = false;		//helps route the proper page into the TextFrame when returning after geocodes, etc.
var publicSelCleared = true;	//tracks if selections have been cleared from map so redundant map refreshes don't occur 

var leftButton = 1;
var rightButton = 2;
if (isNav) {
	leftButton = 1;
	rightButton = 3;
}

var waitingForRefresh = false;	///use this variable to redirect flow when a map had to be refreshed first


/*  *****************************************************
*	Point click functions
* 	used by Measure and Select by Line/Polygon
*	*****************************************************
*/

//similar to clickAddPoint() but displays a DHTML layer marker instead (much faster this way)
function clickSelectionFlag() {
	if (measureWindowUp) {
	//it'll only be false when the change units window is up
	var theX = mouseX;
	var theY = mouseY;
	getMapXY(theX,theY);
	clickPointX[clickCount]=mapX;
	clickPointY[clickCount]=mapY;
	clickCount += 1;
	var currentSelFlag = "";
	switch(clickCount) {
		case 1:
			currentSelFlag = "SelectPoint1";
			break;
		case 2:
			currentSelFlag = "SelectPoint2";
			break;
		case 3:
			currentSelFlag = "SelectPoint3";
			break;
		case 4:
			currentSelFlag = "SelectPoint4";
			break;
		case 5:
			currentSelFlag = "SelectPoint5";
			break;
		case 6:
			currentSelFlag = "SelectPoint6";
			break;
		case 7:
			currentSelFlag = "SelectPoint7";
			break;
		case 8:
			currentSelFlag = "SelectPoint8";
			break;
		case 9:
			currentSelFlag = "SelectPoint9";
			break;
		case 10:
			currentSelFlag = "SelectPoint10";
			break;
		default:
			alert("Only 10 selection points can be used");
			deleteClick();
			return;
	}
	selectCount=0;
	totalMeasure = totalMeasure + currentMeasure;
	var u = Math.pow(10,numDecimals);
	if (totalMeasure!=0) totalMeasure = parseInt(totalMeasure*u+0.5)/u;
	clickMeasure[clickCount]=totalMeasure;
	if (isNav5up) {
		Netclip(currentSelFlag,(theX-1),(theY-19),14,24);
	} else {
		clipLayer(currentSelFlag,(theX-1),(theY-19),14,24);
	}
	showLayer(currentSelFlag);
	if ((useExternalWindow) && (toolMode == 20)) {
		measureWindow.focus();
	}	
//	if (noOverlay) {
//		legendTemp=legendVisible;
//		legendVisible=false;
//			var theString = writeXML();
//			var theNum = 99;
//	} else {
//			var theString = writeOverlayXML();
//			var theNum = 901;
//	
//	}

	//showRetrieveMap();
//	sendToServer(imsURL,theString,theNum);
	}	
}

//special code to hide some or all selection points
function hideSelectionPoint(flag) {
	if (flag == "all") {
		for (var i=0;i<10;i++) {
			hideLayer(flags[i]);
		}
	} else {
		hideLayer(flag);
	}
}

// put a point at click and add to clickCount
function clickAddPoint() {
	var theX = mouseX;
	var theY = mouseY;
	getMapXY(theX,theY);
	clickPointX[clickCount]=mapX;
	clickPointY[clickCount]=mapY;
	clickCount += 1;
	selectCount=0;
	totalMeasure = totalMeasure + currentMeasure;
		var u = Math.pow(10,numDecimals);
		if (totalMeasure!=0) totalMeasure = parseInt(totalMeasure*u+0.5)/u;

	clickMeasure[clickCount]=totalMeasure;
	if (noOverlay) {
		//alert(clickCount);
		legendTemp=legendVisible;
		legendVisible=false;
			var theString = writeXML();
			var theNum = 99;
	} else {
			var theString = writeOverlayXML();
			var theNum = 901;
	
	}

	//showRetrieveMap();
	sendToServer(imsURL,theString,theNum);

}

// zero out all clicks in clickCount
function resetClick() {
	var c1 = clickCount;
	clickCount=0;
	clickPointX.length=1;
	clickPointY.length=1;
	currentMeasure=0;
	totalMeasure=0;
	lastTotMeasure=0;
	clickMeasure.length=1;
	selectCount=0;
	hideSelectionPoint("all");
	
	if (noOverlay) {
		legendTemp=legendVisible;
		legendVisible=false;
		//var theString = writeXML();
		//var theNum = 99;
			//showRetrieveMap();
		//sendToServer(imsURL,theString,theNum);
	} else {
		document.theClickImage.src = blankImage;
	}
	if (toolMode==20) updateMeasureBox();

}	

// remove last click from clickCount
function deleteClick() {
	var c1 = clickCount;
	clickCount=clickCount-1;
	selectCount=0;
	if (clickCount<0) clickCount=0;
	if (clickCount>0) {
		totalMeasure = clickMeasure[clickCount]
		clickPointX.length=clickCount;
		clickPointY.length=clickCount;
		clickMeasure.length=clickCount;
		
	} else {
		totalMeasure=0;
		clickMeasure[0]=0;
	}
	currentMeasure=0;
	if ((clickCount>=0) && (clickCount < 10)) {
		//alert(flags[clickCount]);
		hideSelectionPoint(flags[clickCount]);
	}
	//if (c1>0) {
	//	if (noOverlay) {
	//		//alert(clickCount);
	//		legendTemp=legendVisible;
	//		legendVisible=false;
	//			var theString = writeXML();
	//			var theNum = 99;
	//	} else {
	//			var theString = writeOverlayXML();
	//			var theNum = 901;
	//	
	//	}
	//	sendToServer(imsURL,theString,theNum);
	//}
}


//keep track of currently selected tool, and display it to user
// set the imsMap cursor tool
function clickFunction (toolName) {
	if (hasLayer("measureBox")) hideLayer("measureBox");
	switch(toolName) {
	// Zooming functions
	case "zoomin":
		// zoom in mode
		if ((toolMode == 20) || (toolMode == 16)) resetClick();
		toolMode = 1;
		panning=false;
		selectBox=false;
		if (isIE)	{
			document.body.style.cursor="crosshair";
			//document.all.theTop.style.cursor = "crosshair";
			theCursor = document.all.theTop.style.cursor;
		}
		if (addRedlineReq) cancelRedline();
		modeBlurb = "Zoom In";
		if ((isPublic == false) && (isPartner == false)){
			if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		}
		break
	case "zoomout":
		// zoom out mode
		if ((toolMode == 20) || (toolMode == 16)) resetClick();

		toolMode = 2;
		panning=false;
		selectBox=false;
		if (addRedlineReq) cancelRedline();
		if (isIE)	{
			document.body.style.cursor="crosshair";
			//document.all.theTop.style.cursor = "crosshair";
			theCursor = document.all.theTop.style.cursor;
		}
		modeBlurb = "Zoom Out";
		if ((isPublic == false) && (isPartner == false)) {
			if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		}
		break
	case "zoomlast":
		if ((toolMode == 20) || (toolMode == 16)) resetClick();
		zoomBack();
		panning=false;
		zooming=false;
		if (addRedlineReq) cancelRedline();
		selectBox=false;
		break
	case "zoomactive":
		//alert(LayerExtent[ActiveLayerIndex]);
		var q = LayerExtent[ActiveLayerIndex].split(",");
		panning=false;
		zooming=false;
		redlining=false;
		selectBox=false;
		//zoomToEnvelope(parseFloat(q[0]),parseFloat(q[1]),parseFloat(q[2]),parseFloat(q[3]));
		
		var l = parseFloat(q[0]);
		var b = parseFloat(q[1]);
		var r = parseFloat(q[2]);
		var t = parseFloat(q[3]);
		var w = r-l;
		var h = t-b;
		// add a bit of a margin around the layer
		var wm = w * 0.05;
		var hm = h * 0.05;
		l = l - wm;
		r = r + wm;
		b = b - hm;
		t = t + hm;
		zoomToEnvelope(l,b,r,t);
		break
	case "fullextent":
		fullExtent();
		break
		
	// Pan functions
	case "pan":
		// pan mode
		if ((toolMode == 20) || (toolMode == 16)) resetClick();
		toolMode = 3;
		zooming=false;
		if (addRedlineReq) cancelRedline();
		selectBox=false;
		if (isIE)	{
			document.body.style.cursor="move";
			//document.all.theTop.style.cursor = "move";
			theCursor = document.all.theTop.style.cursor;
		}
		modeBlurb = "Pan";
		if ((isPublic == false) && (isPartner == false)) {
			if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		}
		break

	// Identify-Hyperlink functions
	case "identify":
		// identify mode - layer attributes - requires aimsIdentify.js
		if ((toolMode == 20) || (toolMode == 16)) resetClick();
		busIsUp = false;
		parkIsUp = false;
		panning=false;
		zooming=false;
		if (addRedlineReq) cancelRedline();
		selectBox=false;
		var Win1;
		toolMode = 4;
		modeBlurb = "Identify";
		if (publicSelCleared == false) {
			waitingForRefresh = true;
			clearSelection();	
		} else {
			if (canQuery) {
				if (useExternalWindow) {
					for (var f=0;f<identifyMapService.length;f++) {
						if (identifyMapService[f] == loadThisService) {
							if (identifyLayer[f].length == 1) {
								selectedIdentifyLayers = identifyLayer[f];
								selectedIdentifyReplies.length = 1;
							} else if (identifyMapService[f] == "KCRC_TruckOperator") {
								selectedIdentifyLayers = identifyLayer[f];
								selectedIdentifyReplies.length = 5 ;
							} else {
								Win1 = window.open("identify.htm","QueryWindow","left=120,top=400,width=450,height=200,scrollbars=yes,resizable=yes");

							}
						}
					}
				} else {
					Win1 = parent.TextFrame;
					Win1.document.location = appDir + "identify.htm";
				}
				if (isIE)	{
					document.body.style.cursor="hand";
					//document.all.theTop.style.cursor = "crosshair";
					theCursor = document.all.theTop.style.cursor;
				}
			} else {
				alert("Cannot query Service\nIdentify, Select, and Query functions are disabled.");
			}
		}
		hideLayer("measureBox");
		break

	case "hyperlink":
		// hyperlink mode - requires aimsIdentify.js
		var isOk = false;
		var j=-1;
		panning=false;
		zooming=false;
		if (addRedlineReq) cancelRedline();
		selectBox=false;
		toolMode = 15;
		modeBlurb = "HyperLink";
		if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		hideLayer("measureBox");
		var isOk = checkHyperLinkLayer(ActiveLayerIndex)
		if (isOk) {
			if (canQuery) {
				
				
				if (isIE)	{
					document.body.style.cursor="crosshair";
					//document.all.theTop.style.cursor = "crosshair";
					theCursor = document.all.theTop.style.cursor;
				}
				
			} else {
				alert("Cannot query Service\nIdentify, Select, and Query functions are disabled.");
			}
			//alert("Function Not Implemented");
		} else {
			currentHyperLinkLayer="";
			currentHyperLinkField="";
			alert("This layer does not have any HyperLinks.");

		}
		break
		
	// Measure-Unit function
	case "measure":
		panning=false;
		zooming=false;
		if (addRedlineReq) cancelRedline();
		selectBox=false;
		if (clickCount>0) {
			if (totalMeasure==0) resetClick();
		}
		toolMode = 20;
		if (isIE)	{
			document.body.style.cursor="crosshair";
			//document.all.theTop.style.cursor = "crosshair";
			theCursor = document.all.theTop.style.cursor;
		}
		modeBlurb = "Measure Distance";
		if (useExternalWindow) {
			measureWindow = window.open(appDir+"measure.htm","MeasureWindow","left=120,top=400,width=800,height=220,scrollbars=yes,resizable=yes");
			theFrame = "opener";
			if (parent.MapFrame!=null) theFrame = "opener.parent.MapFrame";
			measureWindowUp = true;
		} else {
			Win1 = parent.TextFrame;
			Win1.document.location = appDir + "measure.htm";
		}
		break
		
	case "setunits":
		if ((toolMode == 20) || (toolMode == 16)) resetClick();
		toolMode = 6;
		if (useTextFrame) {
			parent.TextFrame.location = "setUnits.htm";
		} else {
			window.open((appDir + "setUnits.htm"),"OptionWindow","width=575,height=120,scrollbars=yes,resizable=yes");
		}
		break
		
	// Graphic Selection functions
	case "shape":
		if ((toolMode == 20) || (toolMode == 16)) resetClick();
		panning=false;
		zooming=false;
		if (addRedlineReq) cancelRedline();
		selectBox=false;
		toolMode = 21;
		if (isIE)	{
			document.body.style.cursor="crosshair";
			//document.all.theTop.style.cursor = "crosshair";
			theCursor = document.all.theTop.style.cursor;

		}

		modeBlurb = "Create Shape Mode";
	
		hideLayer("measureBox");
		break
		
	case "selectbox":
		if ((toolMode == 20) || (toolMode == 16)) resetClick();
		panning=false;
		zooming=false;
		if (addRedlineReq) cancelRedline();
		// select mode - requires aimsSelect.js
		if (canQuery) {
			toolMode = 10;
			queryTool=0;
			clickCount=0;
			showBuffer=false;
			if (isIE)	{
				document.body.style.cursor="crosshair";
				//document.all.theTop.style.cursor = "crosshair";
				theCursor = document.all.theTop.style.cursor;
			}

			modeBlurb = "Select Rectangle";
		} else {
			alert("Cannot query Service\nIdentify, Select, and Query functions are disabled.");
		}
		//alert("Function Not Implemented");
		if (useExternalWindow) {
			if (isPartner) {
				Win1 = window.open(appDir+"selectpoly.htm","QueryWindow","left=120,top=400,width=700,height=150,scrollbars=yes,resizable=yes");
				theFrame = "opener";
				if (parent.MapFrame!=null) theFrame = "opener.parent.MapFrame";
			}
		} else {
			Win1 = parent.TextFrame;
			if (isPartner) parent.TextFrame.document.location= appDir + "selectpoly.htm"; 
			else Win1.document.location = appDir + "text.htm";
		}
		hideLayer("measureBox");
		break
		
	case "selectpoint":
		if ((toolMode == 20) || (toolMode == 16)) resetClick();
		panning=false;
		zooming=false;
		if (addRedlineReq) cancelRedline();
		// select mode - requires aimsSelect.js
		if (canQuery) {
			toolMode = 11;
			queryTool=0;
			clickCount=0;
			 resetClick();
			if (isIE)	{
				document.body.style.cursor="hand";
				//document.all.theTop.style.cursor = "hand";
				theCursor = document.all.theTop.style.cursor;
			}

			modeBlurb = "Point Select Mode";
		} else {
			alert("Cannot query Service\nIdentify, Select, and Query functions are disabled.");

		}
		//alert("Function Not Implemented");
		showBuffer=false;
		hideLayer("measureBox");
		break
		
	case "selectline":
		if ((toolMode == 20) || (toolMode == 16)) resetClick();
		panning=false;
		zooming=false;
		if (addRedlineReq) cancelRedline();
		// select mode - requires aimsSelect.js
		if (canQuery) {
			toolMode = 12;
			queryTool=0;
			if (isIE)	{


				document.body.style.cursor="crosshair";
				//document.all.theTop.style.cursor = "crosshair";
				theCursor = document.all.theTop.style.cursor;
			}



			hideLayer("measureBox");
			if (useTextFrame) {
				parent.TextFrame.document.location= appDir + "selectline.htm";
			} else {
				Win1 = open("selectline.htm","QueryWindow","width=575,height=150,scrollbars=yes,resizable=yes");
			}
			modeBlurb = "Select Line";
		} else {
			alert("Cannot query Service\nIdentify, Select, and Query functions are disabled.");
		}
		//alert("Function Not Implemented");
		showGeocode=false;
		showBuffer=false;
		break
		
	case "selectpoly":
		if ((toolMode == 20) || (toolMode == 16)) resetClick();
		panning=false;
		zooming=false;
		if (addRedlineReq) cancelRedline();
		// select mode - requires aimsSelect.js
		if (canQuery) {
			toolMode = 13;
			queryTool=0;
			if (isIE)	{
				document.body.style.cursor="crosshair";
				//document.all.theTop.style.cursor = "crosshair";
				theCursor = document.all.theTop.style.cursor;
			}
			hideLayer("measureBox");
			if (useTextFrame) {
				parent.TextFrame.document.location= appDir + "selectpoly.htm";
			} else {

				Win1 = open("selectpoly.htm","QueryWindow","width=575,height=150,scrollbars=yes,resizable=yes");
			}
			modeBlurb = "Select Polygon";
		} else {
			alert("Cannot query Service\nIdentify, Select, and Query functions are disabled.");
		}
		//alert("Function Not Implemented");
		showBuffer=false;
		break
		
	case "selectshape":
		if ((toolMode == 20) || (toolMode == 16)) resetClick();
		panning=false;
		zooming=false;
		if (addRedlineReq) cancelRedline();
		// select mode - requires aimsSelect.js
		if (canQuery) {
			toolMode = 16;
			queryTool=0;
			if (isIE)	{
				document.body.style.cursor="crosshair";
				//document.all.theTop.style.cursor = "crosshair";
				theCursor = document.all.theTop.style.cursor;
			}
			hideLayer("measureBox");
			if (useTextFrame) {
				parent.TextFrame.document.location= appDir + "select.htm";
			} else {
				Win1 = open("select.htm","QueryWindow","width=575,height=150,scrollbars=yes,resizable=yes");
			}
			modeBlurb = "Select Line/Polygon";
		} else {
			alert("Cannot query Service\nIdentify, Select, and Query functions are disabled.");
		}
		//alert("Function Not Implemented");
		showBuffer=false;
		break
	/*	
	case "buffershape":
		if ((toolMode == 20) || (toolMode == 16)) resetClick();
		panning=false;
		zooming=false;
		if (addRedlineReq) cancelRedline();
		// interactive shape buffer - not implemented
		if (canQuery) {
			toolMode = 17;

			queryTool=0;

			if (isIE)	{
				document.body.style.cursor="crosshair";
				//document.all.theTop.style.cursor = "crosshair";
				theCursor = document.all.theTop.style.cursor;
			}
			hideLayer("measureBox");
			if (useTextFrame) {
				parent.TextFrame.document.location= appDir + "buffershape.htm";
			} else {
				Win1 = open("buffershape.htm","QueryWindow","width=575,height=150,scrollbars=yes,resizable=yes");
			}
			modeBlurb = "Buffer Shape";
		} else {
			alert("Cannot query Service\nIdentify, Select, and Query functions are disabled.");
		}
		//alert("Function Not Implemented");
		showBuffer=false;
		break
		*/

	//Voter Find Function
	case "votingfind":
		if ((toolMode == 20) || (toolMode == 16)) resetClick();
		busIsUp = false;
		parkIsUp = false;
		toolMode = 35;
		panning=false;
		zooming=false;
		if (addRedlineReq) cancelRedline();
		selectBox=false;
		// geocode mode - requires aimsGeocode.js
		hideLayer("measureBox");
		modeBlurb = "Polling Search";
		votingReplies = ["","","","","","","",""];

		//parent.TextFrame.document.location= appDir + "pollingmatch.htm";
		if ((useExternalWindow) || (!useTextFrame)) {
			var Win1 = window.open("http://" + hostName + "/website/KCRC/pollingmatch.htm","QueryWindow","left=120,top=400,width=700,height=200,scrollbars=yes,resizable=yes");
		} else parent.TextFrame.document.location = "addmatch.htm";
		break

	// Geocode Function
	case "geocode":
		if ((toolMode == 20) || (toolMode == 16)) resetClick();
		busIsUp = false;
		parkIsUp = false;
		toolMode = 35;
		panning=false;
		zooming=false;
		if (addRedlineReq) cancelRedline();
		selectBox=false;
		// geocode mode - requires aimsGeocode.js
		hideLayer("measureBox");
		modeBlurb = "Locate Address";
		//parent.TextFrame.document.location= appDir + "addmatch.htm";
		if ((useExternalWindow) || (!useTextFrame)) {
			var Win1 = window.open("http://" + hostName + "/website/KCRC/addmatch.htm","QueryWindow","left=120,top=400,width=700,height=200,scrollbars=yes,resizable=yes");
		} else parent.TextFrame.document.location = "addmatch.htm";
		break

	// Bus Stop locator Function
	case "busfind":
		if ((toolMode == 20) || (toolMode == 16)) resetClick();
		if (isPublic) clearSelection();
		busIsUp = true;
		toolMode = 14;
		panning=false;
		zooming=false;

		if (addRedlineReq) cancelRedline();
		selectBox=false;
		hideLayer("measureBox");
		modeBlurb = "Locate Bus Stop/Route";

		if (useExternalWindow) {
			Win1 = window.open(appDir+"busmatch.htm","BusWindow","left=120,top=400,width=800,height=220,scrollbars=yes,resizable=yes");
			theFrame = "opener";
			if (parent.MapFrame!=null) theFrame = "opener.parent.MapFrame";
		} else {
			Win1 = parent.TextFrame;
			Win1.document.location = appDir + "busmatch.htm";
		}
		break

	// Geocode Function
	case "parkfind":
		if ((toolMode == 20) || (toolMode == 16)) resetClick();
		if (isPublic) clearSelection();
		parkisUp = true;

		toolMode = 18;
		panning=false;
		zooming=false;
		if (addRedlineReq) cancelRedline();
		selectBox=false;
		hideLayer("measureBox");
		modeBlurb = "Locate Park, Trail, or Bikeway";
		if (useExternalWindow) {
			Win1 = window.open(appDir+"parkmatch.htm","ParkWindow","left=120,top=400,width=700,height=220,scrollbars=yes,resizable=yes");
			theFrame = "opener";
			if (parent.MapFrame!=null) theFrame = "opener.parent.MapFrame";
		} else {
			Win1 = parent.TextFrame;
			Win1.document.location = appDir + "parkmatch.htm";
		}
		break

	// Geocode Function
	case "zoomcommunity":
		toolMode = 5;
		if (isPublic) clearSelection();
		panning=false;
		zooming=false;
		if (addRedlineReq) cancelRedline();
		selectBox=false;
		hideLayer("measureBox");
		modeBlurb = "Zoom to a Community";
		if (useExternalWindow) {
			Win1 = window.open(appDir+"communitymatch.htm","CommunityWindow","left=120,top=400,width=700,height=250,scrollbars=yes,resizable=yes");
			theFrame = "opener";
			if (parent.MapFrame!=null) theFrame = "opener.parent.MapFrame";
			//Win1.moveTo(150,400);
		} else {
			Win1 = parent.TextFrame;
			Win1.document.location = appDir + "communitymatch.htm";
		}
		break


	// Query - Search - Find functions
	//if ((toolName=="attributesel") || (toolName=="query")) {
	case "query":
		// query mode - requires aimsQuery.js
		if ((toolMode == 20) || (toolMode == 16)) resetClick();
		panning=false;
		zooming=false;
		if (addRedlineReq) cancelRedline();
		selectBox=false;
		queryStartRecord=0;

		ZoomToSelected=0;
		queryTool=51;
		if (canQuery) {	
			LayerFields.length=1;
			LayerFieldType.length=1;
			LayerFieldCount=0;
			toolMode=8;
			modeBlurb="Build Query";
			
			fieldIndex=0;
			setQueryString="";
			hideLayer("measureBox");
			queryForm();
		} else {
			alert("Cannot query Service\nIdentify, Select, and Query functions are disabled.");
		}
		showBuffer=false;
		break
		
	case "storedquery":
		// storedquery mode - requires aimsQuery.js
		if ((toolMode == 20) || (toolMode == 16)) resetClick();
		panning=false;
		zooming=false;
		if (addRedlineReq) cancelRedline();

		selectBox=false;
		queryStartRecord=0;
		queryTool=1;
		toolMode=51;
		modeBlurb="Search";
		if (canQuery) {	
			toolMode=51;
			modeBlurb="Search";
			fieldIndex=0;
			setQueryString="";
			hideLayer("measureBox");
			getStoredQueries();
		} else {
			alert("Cannot query Service\nIdentify, Select, and Query functions are disabled.");
		}
		showBuffer=false;
		break
		
	case "find":
		//find
		if ((toolMode == 20) || (toolMode == 16)) resetClick();

		toolMode=9;
		panning=false;
		zooming=false;
		if (addRedlineReq) cancelRedline();
		selectBox=false;
		queryStartRecord=0;
		queryTool=1;
		usingSearchName = false;
		if (canQuery) {	
			LayerFields.length=1;
			LayerFieldType.length=1;
			LayerFieldCount=0;	
			fieldIndex=0;
			setQueryString="";
			hideLayer("measureBox");
			modeBlurb = "Search for Value";
			findForm();
		} else {
			alert("Cannot query Service\nIdentify, Select, and Query functions are disabled.");
		}
		showBuffer=false;
		break

	case "standard query":
		//standard queries for realtors and such
		if ((toolMode == 20) || (toolMode == 16)) resetClick();
		toolMode = 7;
		panning=false;
		zooming=false;
		if (addRedlineReq) cancelRedline();
		selectBox=false;
		queryStartRecord=0;
		queryTool=1;
		usingSearchName = false;
		if (canQuery) {	
			LayerFields.length=1;
			LayerFieldType.length=1;
			LayerFieldCount=0;
			fieldIndex=0;
			setQueryString="";
			hideLayer("measureBox");
			modeBlurb = "Search for Value";
			if (useExternalWindow) {
				Win1 = window.open(appDir+"standardquery.htm","StandardQueryWindow","left=120,top=400,width=650,height=175,scrollbars=yes,resizable=yes");
				theFrame = "opener";
				if (parent.MapFrame!=null) theFrame = "opener.parent.MapFrame";
			} else {
				Win1 = parent.TextFrame;
				Win1.document.location = appDir + "standardquery.htm";
			}
		} else {
			alert("Cannot query Service\nIdentify, Select, and Query functions are disabled.");
		}
		showBuffer=false;
		break

	case "redline":
		//redline - annotate a map and send it out to be e-mailed
		if ((toolMode == 20) || (toolMode == 16)) resetClick();
		if (ovIsVisible) {
			toggleOVMap();
		}
		toolMode=30;
		panning=false;
		zooming=false;
		redlining=false;
		addRedlineReq=true;

		selectBox=false;
		modeBlurb = "Redline Tool";
		loadInterface();
		break;
		
	case "clearsel":
		 if ((toolMode == 20) || (toolMode == 16)) resetClick();
		 publicSelCleared = false;
		 clearSelection();
		 break;
		 
	// Buffer function
	case "buffer":
		//buffer - requires aimsBuffer.js
		if ((toolMode == 20) || (toolMode == 16)) resetClick();
		if (useBuffer) {
			if (checkSelected()) {
				if (possibleBuffer()) {
					toolMode = 25;
					modeBlurb ="Buffer";
					writeBufferForm();
				} else {
					alert("It is not possible to buffer around this layer.\nOnly layers of a special type can be buffered.\nIf you would like to know more, feel free to contact us.");
				}	
			} else {
				showBuffer=false;
				alert("No selected features to buffer");
			}
		} else {
			alert("Buffer Mode disabled");
		}
		break
		
	case "options":
		writeOptionForm();
		break
		
	// Print function	
	case "print":
		if ((toolMode == 20) || (toolMode == 16)) resetClick();
		 printIt();
		 break
		 
	// custom modes
	case "dbidentify":
		panning=false;
		zooming=false;
		if (addRedlineReq) cancelRedline();
		selectBox=false;
		// identify mode - requires custom db query - not in basic

		if (canQuery) {
			toolMode = 40;
			if (isIE)	{
				document.body.style.cursor="hand";
				//document.all.theTop.style.cursor = "hand";
				theCursor = document.all.theTop.style.cursor;
			}

			modeBlurb = "Identify";
		} else {
			alert("Cannot query Service\nIdentify, Select, and Query functions are disabled.");
		}


		//alert("Function Not Implemented");
		
		if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		hideLayer("measureBox");
		break
		
		
	case "extract":
		// extract mode
		if ((toolMode == 20) || (toolMode == 16)) resetClick();
		toolMode = 9;
		panning=false;
		selectBox=false;
		if (isIE)	{
			document.body.style.cursor="crosshair";
			//document.all.theTop.style.cursor = "crosshair";
			theCursor = document.all.theTop.style.cursor;
		}
		if (addRedlineReq) cancelRedline();
		modeBlurb = "Extract Data";
		if ((isPublic == false) && (isPartner == false)){
			if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		}
		break
		 
	case "legend":

		if (aimsLegendPresent) {
			if (imsURL!="") {
				if (hasTOC) {
					if (legendVisible) {
					
						legendVisible=false;
					//writeLayerList();
						parent.TOCFrame.document.location=appDir+"toc.htm";
					} else {
						legendVisible=true;
                                                //alert("Step 6");
						getNewLegend();
					}
				} else {
					legendVisible=true;
                                        //alert("Step 6");
					getNewLegend();
				}
			} else {
				alert("A MapService must be loaded");
			}
		} else {
			alert("Legend Library (aimsLegend.js) not loaded.");
		}
		break
		
	case "layerlist":
		// put LayerList in separate window
		writeLayerListForm();
		break
	
	case "REGISpage":
		var leave = confirm("You are now leaving the IMS work environment.\nAll your work to this point will be lost.\nClick OK to leave IMS.");
		if (leave) parent.document.location = "http://www.gvmc-regis.org";
		break
	case "IMSpage":
		var leave = confirm("You are now leaving the current work environment.\nAll your work to this point will be lost.\nClick OK to leave this environment.");
		if (leave) parent.document.location = "http://" + hostName + "/website/public/";
		break	
	case "RAPIDpage":
		var leave = confirm("You are now leaving the REGIS IMS work environment.\nAll your work to this point will be lost.\nClick OK to leave REGIS.");
		if (leave) parent.document.location = "http://www.ridetherapid.org";
		break
	case "KCRCpage":
		var leave = confirm("You are now leaving the REGIS IMS work environment.\nAll your work to this point will be lost.\nClick OK to leave REGIS.");
		if (leave) parent.document.location = "http://www.kentcountyroads.net";
		break
	default:
		alert("Function not enabled.");
	}
	//alert(modeBlurb);
	modeName=modeBlurb;
	if ((useModeFrame) && (parent.MapFrame)) {
		writeModeFrame(modeBlurb);
	} else if ((drawFloatingMode) && (modeLayerOn)) {
		writeModeLayers(modeBlurb);
	} else if ((modeRefreshMap) && (drawModeOnMap)) {
		//var theString = writeXML();
		sendMapXML();
	}
}

// check for mouseup

function chkMouseUp(e) { 
			
	if ((toolMode == 1) && (zooming)) {
			stopZoomBox(e);
		
	}
	if ((toolMode == 2) && (zooming)) {
			stopZoomOutBox(e);

	}
	if ((toolMode == 3) && (panning)) {
			stopPan(e);

	}
	if ((toolMode == 10) && (selectBox)) {
			stopSelectBox(e);
	}
	if ((toolMode == 30) && (redlining)) {
			stopRedlineDraw(e);
	}
		
	return false;
	
}

// perform appropriate action with mapTool
function mapTool(e) {
	var theButton= 0;
	// get the button pushed... if right, ignore... let browser do the popup... it will anyway
	if (isNav) {
		theButton = e.which;
	} else {
		theButton =window.event.button;
	}	
	if (theButton==leftButton) {
		getImageXY(e);
		if ((mouseX>=0) && (mouseX<iWidth) && (mouseY>=0) && (mouseY<iHeight)) {
			if ((hasOVMap) && (ovIsVisible) && (mouseX<i2Width+ovBoxSize) && (mouseY<i2Height) && (ovMapIsLayer)) {
				//ovMapClick(mouseX,mouseY);
			} else {
				//alert(mouseX + "," + mouseY);


				switch(toolMode) {
				case 1:
						startZoomBox(e);
						return false;
						break
					
				case 2:
						startZoomOutBox(e);
						return false;
						break
				case 3:
						startPan(e);
						return false;
						break

				case 4:
						identify(e);
						break
				case 30:
						startRedlineDraw(e)
						return false;
						break
				// custom modes
				/*
				case 5:
					// reverse geocode - requires custom reverse geocode routine - not in default
					
					doReverseGeocode(e);
					break
				case 6:
					// route - requires custom route routine - not in default
					routeClick = routeClick + 1;
					if (routeClick > 2) routeClick = 2;
					setRouteXY()

					writeRoutePage();
					break
				case 7:
					// proximity - requires custom proximity routine - not in default
					proxCount=0;
					proximitySearch(e);
					break
				
				*/

				case 10:
					//select(e);

						startSelectBox(e);
						return false;
						break
				case 11:
					//select point
					select(e);
					break
				case 12:
					//select line
					clickType=2;
					clickAddPoint();
					if (useTextFrame) {
						if (parent.TextFrame.document.title!="Select Line") {
							parent.TextFrame.document.location= appDir + "selectline.htm";
						}
					}
					break
				case 13:
					//select polygon
					clickType=3;
					clickAddPoint();
					if (useTextFrame) {
						if (parent.TextFrame.document.title!="Select Polygon") {
							parent.TextFrame.document.location= appDir + "selectpoly.htm";
						}
					}
					break
				case 15:
					// hyperlink
					hyperLink(e);
					break
				case 16:
					//select shape
					clickType=2;
					clickSelectionFlag();
					//if (useTextFrame) {
					//	if (parent.TextFrame.document.title!="Select Line/Shape") {
					//		parent.TextFrame.document.location= appDir + "select.htm";
					//	}
					//}
					break
					/* 
				case 17:
					//buffer shape - requires custom functions. . . not implemented
					clickType=6;
					clickAddPoint();
					if (useTextFrame) {
						if (parent.TextFrame.document.title!="Buffer Shape") {
							parent.TextFrame.document.location= appDir + "buffershape.htm";
						}
					}
					break
					*/
				case 20:
					// measure
					clickType=1;
					clickSelectionFlag();
					//clickAddPoint();
					break
				case 21:
					// shape
					clickType=4;
					clickAddPoint();
					break
				case 40:
					// db identify - requires custom db query - not in default
					if (aimsDBPresent) {
						matchDBLinkLayer(dbLinkLayer);
						dbIdentify(e);

					}

					break
				default:
					if (toolMode>=1000) {

						customMapTool(e);
					}
				}
			}

		}
	}
}

// update measureBox layer
function updateMeasureBox() {
	//if (isPartner) {
		//alert("update measure box");
		if ((useExternalWindow) && (measureWindowUp) && (measureWindow != null)) {
			var theSegmentBox = measureWindow.document.forms[0].segment;
			var theTotalBox = measureWindow.document.forms[0].total;
			theSegmentBox.value = currentMeasure;
			theTotalBox.value = totalMeasure;
		} else {
			var theSegmentBox = parent.TextFrame.document.forms[0].segment;
			var theTotalBox = parent.TextFrame.document.forms[0].total;
			theSegmentBox.value = currentMeasure;
			theTotalBox.value = totalMeasure;
		}
	//} else {
	//	if (isNav4) {
	//		var theForm = document.layers["measureBox"].document.forms[0];
	//	} else {
	//		//var theForm = document.measureBox.forms[0];
	//		var theForm = document.forms[0];
	//	}
	//	theForm.theMeasTotal.value = totalMeasure + " " + ScaleBarUnits;
	//	theForm.theMeasSegment.value = currentMeasure + " " + ScaleBarUnits;
	//	showLayer("measureBox");
	//}

}


function writeOverlayXML() {
	var theString = '<?xml version="1.0" encoding="UTF-8"?><ARCXML VERSION="1.1">\n<REQUEST>\n<GET_IMAGE>\n<PROPERTIES>\n<EXTENT minx="' + left + '" miny="' + bottom + '" maxx="' + right + '" maxy="' + top + '" />\n';
	theString += '<IMAGESIZE height="' + iHeight + '" width="' + iWidth + '" />\n';
	// tell the server that the layers are to be invisible
	theString += '<LAYERLIST >\n';
	for (var i=0;i<layerCount;i++) {
		theString += '<LAYERDEF name="' + LayerName[i] + '" visible="false" />\n';
	}
	theString += '</LAYERLIST>\n';
	// map background color
	theString += '<BACKGROUND color="255,255,254" transcolor="255,255,254" />\n';
	var theImage = theServiceName + "_" + hostName.toUpperCase() + (Math.round(Math.random()*100000000)).toString() + "." + theImageType;
	var thePath = theImagePath + theImage;
	var theURL = theImageURLPath + theImage;
	theString += '<OUTPUT name="' + thePath + '" url="' + theURL + '" ';
	theString += 'type="' + theImageType + '" />\n';
	theString += '</PROPERTIES>\n';
	if (clickCount>0) {
		theString += '<LAYER type="ACETATE" name="allTheClicks">\n';
		if (clickCount>1) {
			theString += '<OBJECT units="DATABASE">\n<LINE coords="' + clickPointX[0] + " " + clickPointY[0];
			for (var i=1;i<clickCount;i++) {
				theString += ","  + clickPointX[i] + "," + clickPointY[i]; 
			}
			theString += '" >\n';
			theString += '<SIMPLELINESYMBOL type="SOLID" color="' + clickMarkerColor;
			theString += '" width="3" />\n</LINE>\n</OBJECT>\n'; 

			theString += '<OBJECT units="DATABASE">\n<LINE coords="' + clickPointX[0] + " " + clickPointY[0];
			for (var i=1;i<clickCount;i++) {
				theString += ","  + clickPointX[i] + "," + clickPointY[i]; 

			}
			theString += '" >\n';
			theString += '<SIMPLELINESYMBOL type="SOLID" color="255,255,255" width="1" />\n</LINE>\n</OBJECT>\n'; 
 		}
		for (var i=0;i<clickCount;i++) {
			theString += '<OBJECT units="DATABASE">\n<POINT coords="' + clickPointX[i] + ',' + clickPointY[i] + '">\n';
			theString += '<SIMPLEMARKERSYMBOL  type="' + clickMarkerType + '"';
			theString += ' color="' + clickMarkerColor + '" size="' + clickMarkerSize + '" />\n</POINT>\n</OBJECT>\n';
			}
		theString += '</LAYER>\n';
	}
	theString += '</GET_IMAGE>\n</REQUEST>\n</ARCXML>';
	return theString;

}


