
// 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 clickCount = 0;
var	clickPointX = new Array();
var clickPointY = new Array();
var clickMeas = new Array();
var clickType = 1;

//Mackay for markup
meas_Pts=0;
meas_Str="";
poly_Pts=0;
poly_Str="";


var panning=false;
var f73g=false;
var selectBox=false;
var blankImage = "images/map.gif";

var leftButton =1;
var rightButton = 2;
if (isNav) {
	leftButton = 1;
	rightButton = 3;
}

var theX=0;
var theY=0;

var selectShape=""; 

function f6(){ //stopMeasure
	if ((is5up) && (toolMode==20)){
			f88();
	}
	if ((is5up) && ((toolMode==12)||(toolMode==13))){
			f103();
			MCCselShape();    
	}

    //Mackay for markup 
	if ((is5up) && ((toolMode==1200)||(toolMode==13))){
		//alert("here");
		if(drawing){
			linepoints=0;
			if(currentshape=="shape"){
			  var tt=firstpoint.split(",");
			  var theX=tt[0];
			  var theY=tt[1];
			  f71(theX,theY);
			  var tt=draw_Points[drawCount];
			  var u = Math.pow(10,1);
		      var uX = Math.round(mapX * u) / u;
              var uY = Math.round(mapY * u) / u;
			  draw_Points[drawCount]=tt+";"+uX+","+uY; 
			  drawfirst=true;
			}
			//set wether we will dimension the object ie: length for lines, and area for polygons
			if((currentshape=="shape")||(currentshape=="line")){
				dimensioning[drawCount]=false;
				var d=parent.TOCFrame.document.getElementById('dim');
				if(d.checked){
					dimensioning[drawCount]=true;
                }
            }
			drawCount+=1;
			clear_Draw_Path();
			sendMapXML();
	    }  
	}

}

function f7() { //clickAddPoint
	if (is5up){
		clickMeas();
	}
	theX = mouseX;
	theY = mouseY;
	f71(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;

	clickMeas[clickCount]=totalMeasure;

	legendTemp=legendVisible;
	legendVisible=false;

	if (is5up==false){
		shwRetrMap();
		writeXMLASP(99);
	}

}

function f8() { //resetClick
	var c1 = clickCount;
	clickCount=0;
	clickPointX.length=1;
	clickPointY.length=1;
	currentMeasure=0;
	totalMeasure=0;
	lastTotMeasure=0;
	clickMeas.length=1;
	selectCount=0;
	

	legendTemp=legendVisible;
	legendVisible=false;

	writeXMLASP(99);
		

	if (toolMode==20) f13();

}	

function f9() { //deleteClick
alert("in aimsclick - deleteclick");
	var c1 = clickCount;
	clickCount=clickCount-1;
	selectCount=0;
	if (clickCount<0) clickCount=0;
	if (clickCount>0) {
		totalMeasure = clickMeas[clickCount];
		clickPointX.length=clickCount;
		clickPointY.length=clickCount;
		clickMeas.length=clickCount;
		
	} else {
		totalMeasure=0;
		clickMeas[0]=0;
	}
	currentMeasure=0;
	if (c1>0) {

		legendTemp=legendVisible;
		legendVisible=false;

		writeXMLASP(99);
	}
	
}


function f10 (toolName) { //clickFunction
    if (isIE)	{
		document.all.theImage.style.cursor = "default";
		theCursor = document.all.theImage.style.cursor;
	}
	if (hasLayer("measureBox")){
		hideLayer("measureBox");
		if (is5up) f103();
	}

	//Mackay for markup
	if(drawing){
		clear_Draw_Path();
        draw_first=true;
		drawing=false;
		var wasDrawing = true;
		draw_Points[drawCount]="";
		linepoints=0;
		//*******************
		drawing=false;
		parent.RefreshFrame.document.all("Tabs").src="images/tab_layers.gif";
		//parent.RefreshFrame.document.changeTab("Layers");
		parent.TOCFrame.document.location=appDir+"toc.htm";
		//parent.TOCFrame.updateLayers();
	}

	switch(toolName) {
	// f73g functions
	case "f73":
		// zoom in mode
		toolMode = 1;
		panning=false;
		selectBox=false;
        if (isIE)	{
    		document.all.theImage.style.cursor = "crosshair";
		    theCursor = document.all.theImage.style.cursor;
	    }

		break;
	case "f74":
		// zoom out mode
		toolMode = 2;
		panning=false;
		selectBox=false;
        if (isIE)	{
    		document.all.theImage.style.cursor = "crosshair";
		    theCursor = document.all.theImage.style.cursor;
	    }


		break;
	case "zoomlast":
		zoomBack();
		panning=false;
		f73g=false;
		selectBox=false;
		break;
	case "f52":
		f52();
		break;
		
	// Pan functions
	case "pan":
		toolMode = 3;
        if (isIE)	{
    		document.all.theImage.style.cursor = "hand";
		    theCursor = document.all.theImage.style.cursor;
	    }

		f73g=false;
		selectBox=false;

		break;

	case "identify":
		panning=false;
		f73g=false;
		selectBox=false;
        if (isIE)	{
    		document.all.theImage.style.cursor = "crosshair";
		    theCursor = document.all.theImage.style.cursor;
	    }

		if (canQuery) {
			toolMode = 4;
			
		} else {
			alert("Cannot query Service\nIdentify, Select, and Query functions are disabled.");
		}
			
		
		hideLayer("measureBox");

		break;


	case "measure":
		panning=false;
		f73g=false;
		selectBox=false;
		if (clickCount>0) {
			if (totalMeasure==0) f8();
		}
		toolMode = 20;

		if (clickType==1) {
				showLayer("measureBox");
				f13();
		}

		break;
		
		
	case "selectbox":
		panning=false;
		f73g=false;
		if (canQuery) {
			toolMode = 10;
			clickCount=0;
		} else {
			alert("Cannot query Service\nIdentify, Select, and Query functions are disabled.");
		}

		hideLayer("measureBox");
		break;
		
// New for MiMAPS September 2005. Dan Clark ESRI Australia
	case "selectpoly":
		panning=false;
		f73g=false;
		//showLayer(selectLine);
		if (canQuery) {
			toolMode = 13;
			clickCount=0;
			clickPointX.length=0;
			clickPointY.length=0;
			parent.ResultsFrame.document.location= appDir + "select.htm";
		} else {
			alert("Cannot query Service\nIdentify, Select, and Query functions are disabled.");
		}

		hideLayer("measureBox");  
		break
		
	case "selectline":
		panning=false;
		f73g=false;
		//showLayer(selectLine);
		if (canQuery) {
			toolMode = 12;
			clickCount=0;
			clickPointX.length=0;
			clickPointY.length=0;
			parent.ResultsFrame.document.location= appDir + "select.htm";
		} else {
			alert("Cannot query Service\nIdentify, Select, and Query functions are disabled.");
		}

		hideLayer("measureBox");
		break		
//End new for MiMAPS
	
	case "legend":
			if (imsURL!="") {
				//Mackay for markup
				if (wasDrawing) parent.TOCFrame.document.location=appDir+"toc.htm";
				drawing=false;
				wasDrawing=false;
				parent.TOCFrame.scrollTo(0,0);
				legendVisible=true;
				f48();
			} else {
				alert("A MapService must be loaded");
			}

		break;
	case "layers":
			if (imsURL!="") {
				legendVisible=false;
				if (wasDrawing){
					parent.TOCFrame.document.location=appDir+"toc.htm";
				}else{
					var LegendDIV = parent.TOCFrame.document.getElementById("LEGEND");
					var TOCDIV = parent.TOCFrame.document.getElementById("TOC");
					
					TOCDIV.style.visibility = "visible";
					LegendDIV.style.visibility = "hidden";
				}
				wasDrawing=false;
			} else {
				alert("A MapService must be loaded");
			}

		break;	
//Mackay for markup
	case "markup":
		
	if (isIE)	{
		document.all.theImage.style.cursor = "crosshair";
		theCursor = document.all.theImage.style.cursor;
	}

		legendVisible=false;
        parent.ToolFrame.revertToolPic();
		toolMode = 1200;   
		drawing=true;
		var LegendDIV = parent.TOCFrame.document.getElementById("LEGEND");
		var TOCDIV = parent.TOCFrame.document.getElementById("TOC");
						
		TOCDIV.style.visibility = "hidden";
		LegendDIV.style.visibility = "hidden";
		parent.TOCFrame.document.location="http://www.mimapsmackay.com.au/draw.htm";   //appDir+
		break;
		
	case "adjoin":
		if(f42()){
		  adjoin();
	    }
		else alert("There is no selection from the Land layer.")
	    break;
	case "addtext":
		f2();
	
		break;
	case "f5":
		break;

    case "get_from_geac":
        parent.ResultsFrame.location="http://www.mimapsmackay.com.au/website/"+app_dir+"/asp/GEAC1.asp"; 
	    break
    case "send_to_geac":
        if(last_property_str!=""){ 
           parent.ASPFrame.location="http://www.mimapsmackay.com.au/website/"+app_dir+"/asp/send_property1.asp?property="+last_property_str;
		}else{
           alert("You must select a property to use this function");  
        }
		break
	case "IPA":
		panning=false;
		f73g=false;
		selectBox=false;
        if (isIE)	{
    		document.all.theImage.style.cursor = "crosshair";
		    theCursor = document.all.theImage.style.cursor;
	    }

		if (canQuery) {
			toolMode = 1004;
			
		} else {
			alert("Cannot query Service\nIdentify, Select, and Query functions are disabled.");
		}
			
		
		hideLayer("measureBox");

		break;

	case "project_point":
		panning=false;
		f73g=false;
		selectBox=false;
        if (isIE)	{
    		document.all.theImage.style.cursor = "crosshair";
		    theCursor = document.all.theImage.style.cursor;
	    }

		if (canQuery) {
			toolMode = 2060;
			
		} else {
			alert("Cannot query Service\nIdentify, Select, and Query functions are disabled.");
		}
		hideLayer("measureBox");
		break;



	default:
		alert("Function not enabled.");
	}

}

function f11(e) { //chkMouseUp
			
	if ((toolMode == 1) && (f73g)) {
			f77(e);
		
	}
	if ((toolMode == 2) && (f73g)) {
			f79(e);
	}
	if ((toolMode == 3) && (panning)) {
			f82(e);

	}
	if ((toolMode == 10) && (selectBox)) {
			f85(e);
	}
		
	return false;
	
}

function f12 (e) { //mapTool

	if (!parent.finishedLoading)
		return;
	if (parent.MenuFrame.menEvent)
		return;
	if (hasf1)
		return;

	var theButton= 0;
	if (isNav) {
		theButton = e.which;
	} else {
		theButton =window.event.button;
	}	
	if (theButton==leftButton) {
		f72(e);
		if ((mouseX>=0) && (mouseX<iWidth) && (mouseY>=0) && (mouseY<iHeight)) {
			
				switch(toolMode) {
				case 1:
						f76(e);
						return false;
						break;
					
				case 2:
						f78(e);
						return false;
						break;
				case 3:
						f81(e);
						return false;
						break;

				case 4:
					identify(e);
					break;
	
				case 10:
					//Select Box
					setQueryString="";
					f84(e);
					break;
				case 11:
					//select point
					select(e);
					break;
// New for MiMAPS September 2005. Dan Clark ESRI Australia
				case 12:
					//select line
						clickType=2;
						f7();
					break;
				case 13:
					//select polygon
						clickType=3;
						f7();
					break;
// End new for MiMAPS
				case 20:
					// measure
					clickType=1;
					f7();
					break;
//ppunter september 2005 to support IPA functionality
				case 1004:
					ipa(e);
					break;

				case 2000:
					f3(e);
					break;

				//Mackay for markup
				  case 1200:   
					draw(e);
					break;

				  case 1201:   
					drawPoint(e);
					break;

				  case 1126:   
					var str=parent.TOCFrame.document.getElementById('thetext').value;
					if(str==""){
					  alert("You MUST enter a label value");
					  return
					}
					labeltex=str;
					currLabelColor = currentcolor;
					adduserlab(e);
					break;
				  //end //Mackay for markup

            
				  case 2060:   
					Project_Point(e);
					break;




				default:

				}
			
		}
	}
}

function f13() { //updateMeasureBox
	if (isNav4) {
		var theForm = document.layers["measureBox"].document.forms[0];
	} else {
		var theForm = document.forms[0];
	}
	theForm.theMeasTotal.value = totalMeasure + " " + ScaleBarUnits;
	theForm.theMeasSegment.value = currentMeasure + " " + ScaleBarUnits;
	showLayer("measureBox");

}
