// 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
*/

aimsClickPresent=true;

// Variabili per il menu flottante
var xm, ym;
var ob;
var over = false;
var sulMenu=false;

var isIE = document.all ? 1 : 0;
var isNav = (document.layers ? 1 : (document.all ? 0 : 1));
var isOpera = isIE && ((window.navigator.userAgent.indexOf("opera") != -1) || (window.navigator.userAgent.indexOf("Opera") != -1));
var isIEMac = isIE && ((window.navigator.userAgent.indexOf("mac") != -1) || (window.navigator.userAgent.indexOf("Mac") != -1));
var isW3C = document.getElementById ? 1 : 0;
// Fine variabili menu flottante



var onOVArea = false;

// Global vars to save mouse position
var mouseX=0;
var mouseY=0;
var x1=0;
var y1=0;
var x2=0;
var y2=0;
var zleft=0;
var zright=0;
var ztop=0;
var zbottom=0;

var totalMeasure=0;
var currentMeasure=0;
var lastTotMeasure=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 shapeSelectBuffer = false;

var panning=false;
var zooming=false;
var selectBox=false;
var blankImage = "images/map.gif";

var leftButton =1;
var rightButton = 2;
if (isNav) {
	leftButton = 1;
	rightButton = 3;
}




// Funzioni per il menu flottante
function IsIE()		{ return isIE;}
function IsNav()	{ return isNav;  }
function IsOpera()  { return isOpera;}
function IsIEMac()	{ return isIEMac;}
function IsW3C()	{ return isW3C; }

function cursorInMenu(){
	var obMenu = CSFindElementStyle(idMenu,0);
	var x = event.x;
	var y = event.y;
	var oxn = getNumberElementStyle(obMenu.left);
	var oyn = getNumberElementStyle(obMenu.top);
	if(x< oxn)
		return false;
	if(x> oxn + getNumberElementStyle(CSFindElementStyle(idMenu, 0).width))
		return false;
	if(y < oyn)
		return false;
	if(y > oyn + getNumberElementStyle(CSFindElementStyle(idMenu, 0).height))
		return false;

	return true;
}

function getNumberElementStyle(obj){
	var oyo = new String(obj);
	oyo = oyo.substring(0, oyo.length - 2);
	return new Number(oyo);
}

function CSFindElementStyle(n,ly) {
	if (IsW3C()) {
		return CSFindElement(n,ly).style;
	} else if (IsIE()) {
		return CSFindElement(n,ly).style;
	} else {
		return CSFindElement(n,ly);
	}
}

function CSFindElement(n,ly) {

	if (IsW3C()) {
		return document.getElementById(n);
	} else if (IsIE()) {
		return document.all(n);
	} else if (IsNav()) {
		var curDoc = ly ? ly.document : document;
		var elem = curDoc[n];

		if (elem) return elem;

		for (var i=0;i<curDoc.layers.length;i++) {
			elem = CSFindElement(n,curDoc.layers[i]);
			if (elem) return elem;
		}
		return elem;
	} else {
		return document[n];
	}

}

function CSSetStyleVis(s,v) {
	if (IsW3C()) {
		CSFindElementStyle(s, 0).visibility = (v == 0) ? 'hidden' : 'visible';
	} else if (IsIE()) {
		CSFindElementStyle(s, 0).visibility = (v == 0) ? 'hidden' : 'visible';
	} else {
		CSFindElementStyle(s, 0).visibility = (v == 0) ? 'hidden' : 'visible';
	}
}

// Mouse Down sul menu
// Imposta il focus sul menu e permette di trascinarlo
function MD(e) {

	if (over){

		if (IsIE()){
			ob = CSFindElement(idMenu,0);
			ob = ob.style;
			xm=event.x;
			ym=event.y;

		}else if (IsNav()) {
			ob = CSFindElement(idMenu,0);
			xm=e.layerX;
			ym=e.layerY;
			return false;
		}

	}
}

// Mouse Move sul menu
function MM(e) {

	if (ob) {
		if (IsIE() ){
			var oxn = getNumberElementStyle(ob.left);
			var oyn = getNumberElementStyle(ob.top);

			ob.top = oyn + (event.y - ym);
			ob.left = oxn + (event.x - xm);
			xm = event.x;
			ym = event.y;
			return false;

		}else if (IsNav()) {
			ob.style.top = e.pageY-ym;
			ob.style.left = e.pageX-xm;
		}
	}
}

// Mouse Up sul menu
// Se il menu viene spostato oltre i limiti del MapFrame, questo si riposiziona sul limite
function MU() {
	if(ob) 	{

		var nextCL = getNumberElementStyle(CSFindElementStyle(idMenu1, 0).left);
		var nextCT = getNumberElementStyle(CSFindElementStyle(idMenu1, 0).top);

		var nextOL= getNumberElementStyle(CSFindElementStyle(idMenu2, 0).left);
		var nextOT= getNumberElementStyle(CSFindElementStyle(idMenu2, 0).top);

		if(nextCL > maxX || nextOL > maxX) {
			CSFindElementStyle(idMenu2, 0).left = maxX;
			CSFindElementStyle(idMenu1, 0).left = maxX;
		}else if (nextCL < minX || nextOL < minX) {
			CSFindElementStyle(idMenu2, 0).left = minX;
			CSFindElementStyle(idMenu1, 0).left = minX;
		}

		if(nextCT > maxY || nextOT > maxY2 ){
			CSFindElementStyle(idMenu2, 0).top = maxY2;
			CSFindElementStyle(idMenu1, 0).top = maxY;
		}else if (nextCT < minY || nextOT < minY) {
			CSFindElementStyle(idMenu2, 0).top = minY;
			CSFindElementStyle(idMenu1, 0).top = minY;
		}

	}
	ob = null;
	over = false;
	NOSpostaMenu();
	sulMenu=false;

}

// Mouse Out sul menu
function MO() {
	if(ob) 	{
		var nextCL = getNumberElementStyle(CSFindElementStyle(idMenu1, 0).left);
		var nextCT = getNumberElementStyle(CSFindElementStyle(idMenu1, 0).top);
		var nextOL= getNumberElementStyle(CSFindElementStyle(idMenu2, 0).left);
		var nextOT= getNumberElementStyle(CSFindElementStyle(idMenu2, 0).top);
		var uscito = false;

		if(nextCL > maxX || nextOL > maxX) {
			CSFindElementStyle(idMenu2, 0).left = maxX;
			CSFindElementStyle(idMenu1, 0).left = maxX;
			uscito = true;
		}else if (nextCL < minX || nextOL < minX) {
			CSFindElementStyle(idMenu2, 0).left = minX;
			CSFindElementStyle(idMenu1, 0).left = minX;
			uscito = true;
		}

		if(nextCT > maxY || nextOT > maxY2 ){
			CSFindElementStyle(idMenu2, 0).top = maxY2;
			CSFindElementStyle(idMenu1, 0).top = maxY;
			uscito = true;
		}else if (nextCT < minY || nextOT < minY) {
			CSFindElementStyle(idMenu2, 0).top = minY;
			CSFindElementStyle(idMenu1, 0).top = minY;
			uscito = true;
		}

		if (uscito) {
			ob = null;
			over = false;
			NOSpostaMenu();
			sulMenu=false;
		}
	}
}

function ApriMenu() {

	var oyon = getNumberElementStyle(CSFindElementStyle(idMenu1, 0).top);
	var oxon = getNumberElementStyle(CSFindElementStyle(idMenu1, 0).left);

	CSFindElementStyle(idMenu2, 0).top = oyon;
	CSFindElementStyle(idMenu2, 0).left = oxon;
	idMenu = idMenu2;
	if (oyon > maxY - diffMenu2VsMenuH){
		var temp = maxY - diffMenu2VsMenuH;
		CSFindElementStyle(idMenu2, 0).top = temp;
		CSFindElementStyle(idMenu1, 0).top = temp;
	}

	CSSetStyleVis(idMenu1, 0);
	CSSetStyleVis(idMenu2, 1);
}

function ChiudiMenu() {
	var oyon = getNumberElementStyle(CSFindElementStyle(idMenu2, 0).top);
	var oxon = getNumberElementStyle(CSFindElementStyle(idMenu2, 0).left);

	idMenu = idMenu1;
	CSFindElementStyle(idMenu1, 0).top = oyon;
	CSFindElementStyle(idMenu1, 0).left = oxon;

	CSSetStyleVis(idMenu1, 1);
	CSSetStyleVis(idMenu2, 0);
}

// funzione di pressione del pulsante del mouse sul menu flottante
function SpostaMenu() {
	document.onmousedown = MD;
	document.onmousemove = MM;
	document.onmouseup = MU;
	document.onmouseout = MO;
}

// funzione di rilascio del pulsante del mouse sul menu flottante
function NOSpostaMenu() {
	reFocusMap();
}
// Fine funzioni per il menu flottante





/*  *****************************************************
*	Point click functions
* 	used by Measure and Select by Line/Polygon
*	*****************************************************
*/

// put a point at click and add to clickCount
function clickAddPoint() {
	var theX = mouseX;
	var theY = mouseY;
	getMapXY(theX,theY);
	//INIZIO Controllo Punto Contenuto: CPC
  currentTheX = theX;
  currentTheY = theY;
  if( (cpc_csi_useGeometricControl) && (toolMode==50)){
	   cpc_csi_select();
	} else{
	   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;
	   legendTemp=legendVisible;
	   legendVisible=false;
	   var theString = writeXML();

	   var theNum = 99;
	   //PER EDITING
	   showRetrieveMap();
	  //FINE EDITING
   	//alert('function clickAddPoint');
	  sendToServer(imsURL,theString,theNum);
 }
	//FINE Controllo Punto Contenuto: CPC
}


// 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;

	legendTemp=legendVisible;
	legendVisible=false;
	var theString = writeXML();
	var theNum = 99;
		//showRetrieveMap();
	sendToServer(imsURL,theString,theNum);

	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 (c1>0) {
		legendTemp=legendVisible;
		legendVisible=false;
		var theString = writeXML();
		var theNum = 99;
		sendToServer(imsURL,theString,theNum);
	}

}


//PER EDITING
function ed_resetANDclickAddPoint(){
   //reset dei clik
   var c1 = clickCount;
	clickCount=0;
	clickPointX.length=1;
	clickPointY.length=1;
	currentMeasure=0;
	totalMeasure=0;
	lastTotMeasure=0;
	clickMeasure.length=1;
	selectCount=0;

}
// FINE EDITING

//keep track of currently selected tool, and display it to user
// set the imsMap cursor tool
function clickFunction (toolName) {

//Inizio NQB
   if ((toolName=="buffer")||(toolName=="editing")||(toolName=="editingTool")||(toolName=="ed_selectbox")||(toolName=="multiselect")||(toolName=="NewQueryBuilder")||(toolName=="Query")||(toolName=="identify")||(toolName=="identifyall")||(toolName=="selectbox")||(toolName=="selectbox")){
	nqb_csi_inuse=false;
	IDLayerCT="";
	CondizioneCT = "";
	QueryLayerAttivo = "";
   }
//Fine NQB


//Inizio QuickDraw : QDW
  // every toolbar click resets quickdraw
  if (qdw_csi_useQuickDrawImage) {
	  qdw_csi_isQuickDrawActive = false;
	   // with pan map moves and not quickdraw, so clear
	  if (toolName == "pan") document.QuickDrawImage.src = "images/pixel.gif";
  }
//Fine QuickDraw : QDW

	  //Inizio MultiSelect : MUS
    if (mus_csi_useMultiSelect){
	    if ((toolName!="buffer")&&(toolName!="ed_selectbox")&&(toolName!="editing")&&(toolName!="editingTool")&&(toolName!="print") && (toolName!="extract") && (toolName!="legend") && (toolName!="buffer") && (toolName!="zoomin") && (toolName!="zoomout") && (toolName!="zoomlast") && (toolName!="zoomactive") && (toolName!="fullextent") && (toolName!="pan") && (toolName!="multiselect") && (toolName!="SendMail")){
    		parent.MapFrame.mus_csi_ClearMultiSelectArray();
    		parent.MapFrame.clearSelection();
        if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
	    }
	    mus_csi_useClearSelection= false;
	  }
	  //Fine MultiSelect

	if (hasLayer("measureBox"))
		hideLayer("measureBox");
	switch(toolName) {
	// Zooming functions
	case "zoomin":
		// zoom in mode
		toolMode = 1;
		panning=false;
		selectBox=false;
		if (isIE)	{
			document.all.theTop.style.cursor = "crosshair";
			theCursor = document.all.theTop.style.cursor;
		}
		modeBlurb = modeList[0];
		//if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		break;
	case "zoomout":
		// zoom out mode
		toolMode = 2;
		panning=false;
		selectBox=false;
		if (isIE)	{
			document.all.theTop.style.cursor = "crosshair";
			theCursor = document.all.theTop.style.cursor;
		}
		modeBlurb = modeList[1];
		//if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		break;
	case "zoomlast":
		zoomBack();
		panning=false;
		zooming=false;
		selectBox=false;
		break;
	case "zoomactive":
		//alert(LayerExtent[ActiveLayerIndex]);
		var q = LayerExtent[ActiveLayerIndex].split("|");
		panning=false;
		zooming=false;
		selectBox=false;
		//zoomToEnvelope(parseFloat(q[0]),parseFloat(q[1]),parseFloat(q[2]),parseFloat(q[3]));

		var l = parseFloat(setDecimalString(q[0]));
		var b = parseFloat(setDecimalString(q[1]));
		var r = parseFloat(setDecimalString(q[2]));
		var t = parseFloat(setDecimalString(q[3]));
		var w = r-l;
		var h = t-b;
		// add a bit of a margin around the layer
		var wm = w * (5/100);
		var hm = h * (5/100);
		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
		toolMode = 3;

		zooming=false;
		selectBox=false;
		if (isIE)	{
			document.all.theTop.style.cursor = "move";
			theCursor = document.all.theTop.style.cursor;
		}
		modeBlurb = modeList[2];
		//if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		break;

	// Identify-Hyperlink functions
	case "identify":
		// identify mode - layer attributes - requires aimsIdentify.js
		panning=false;
		zooming=false;
		selectBox=false;
		shapeSelectBuffer = false;
		if (canQuery) {
			toolMode = 4;

			if (isIE)	{
				document.all.theTop.style.cursor = "crosshair";
				theCursor = document.all.theTop.style.cursor;
			}
			modeBlurb = modeList[3];
		} else {
			alert(msgList[46]);
		}
		//alert("Function Not Implemented");

		showGeocode=false;
		if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		hideLayer("measureBox");
		break;

	/****************
	 * ext_services *
	 ****************/
	 case "ext_services":
		//alert("ext");
		// identify mode - layer attributes - requires aimsIdentify.js
		panning=false;
		zooming=false;
		selectBox=false;
		shapeSelectBuffer = false;
		if (canQuery) {
			toolMode = 1080;

			if (isIE)	{
				document.all.theTop.style.cursor = "crosshair";
				theCursor = document.all.theTop.style.cursor;
			}
			modeBlurb = modeList[3];
		} else {
			alert(msgList[46]);
		}

		showGeocode=false;
		if (useTextFrame)parent.TextFrame.document.location= appDir + "text.htm";
		hideLayer("measureBox");
		break;
	/********************
	 * end ext_services *
	 ********************/
	 // Visualizza i limiti di scala
  	case "LimitiScala":
        	showLimiti();
        break;
	  // SaveImage function
  	case "saveImage":
   		sem_csi_flagUseSaveMap = true;
         	getSaveMap('SalvaMappa');
      		break;

	case "identifyall":
		// identify drill mode
		panning=false;
		zooming=false;
		selectBox=false;
		shapeSelectBuffer = false;
		toolMode = 5;
		if (canQuery) {
			if (isIE)	{
				document.all.theTop.style.cursor = "crosshair";
				theCursor = document.all.theTop.style.cursor;
			}
			//modeBlurb = modeList[19]; // identify all
			modeBlurb = modeList[20]; // identify visible features
			//modeBlurb = modeList[3]; // identify
		} else {
			alert(msgList[46]);
		}
		//alert("Function Not Implemented");
		showGeocode=false;
		drawSelectBoundary=false;
		if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		hideLayer("measureBox");
		break;
	case "hyperlink":
		// hyperlink mode - requires aimsIdentify.js
		var isOk = false;
		var j=-1;
		panning=false;
		zooming=false;
		selectBox=false;
		shapeSelectBuffer = false;
		toolMode = 15;
		modeBlurb = modeList[9];
		showGeocode=false;
		if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		hideLayer("measureBox");
		var isOk = checkHyperLinkLayer(ActiveLayerIndex)
		if (isOk) {
			if (canQuery) {


				if (isIE)	{
					document.all.theTop.style.cursor = "crosshair";
					theCursor = document.all.theTop.style.cursor;
				}

			} else {
				alert(msgList[46]);
			}
			//alert("Function Not Implemented");
		} else {
			currentHyperLinkLayer="";
			currentHyperLinkField="";
			alert(msgList[47]);

		}
		break;

	case "hyperlinkany":
		// hyperlink mode - requires aimsIdentify.js
		var j=-1;
		panning=false;
		zooming=false;
		selectBox=false;
		shapeSelectBuffer = false;
		toolMode = 30;
		modeBlurb = modeList[9];
		showGeocode=false;
		if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		hideLayer("measureBox");
			if (canQuery) {


				if (isIE)	{
					document.all.theTop.style.cursor = "crosshair";
					theCursor = document.all.theTop.style.cursor;
				}

			} else {
				alert(msgList[46]);
			}
			//alert("Function Not Implemented");
		break;

	// Measure-Unit function
	case "measure":

    //Inizio QuickDraw : QDW
	  if (qdw_csi_useQuickDrawImage) qdw_csi_isQuickDrawActive = true;
   //Fine QuickDraw : QDW
		panning=false;
		zooming=false;
		selectBox=false;
		shapeSelectBuffer = false;
		if (clickCount>0) {
			if (totalMeasure==0) resetClick();
		}
		toolMode = 20;
		if (isIE)	{
			document.all.theTop.style.cursor = "crosshair";
			theCursor = document.all.theTop.style.cursor;
		}
		modeBlurb = modeList[12];
		if (clickType==1) {
			//if (useTextFrame) parent.TextFrame.location= appDir + "measure.htm";
				showLayer("measureBox");
				updateMeasureBox();
		}
		showGeocode=false;
		break;

	case "setunits":
		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":
    //Inizio QuickDraw : QDW
    alert("clickFunction = Shape");
    if (qdw_csi_useQuickDrawImage) qdw_csi_isQuickDrawActive = true;
    //Fine QuickDraw : QDW
		panning=false;
		zooming=false;
		selectBox=false;
		shapeSelectBuffer = false;
		toolMode = 21;
		if (isIE)	{
			document.all.theTop.style.cursor = "crosshair";
			theCursor = document.all.theTop.style.cursor;
		}

		modeBlurb = modeList[13];

		showGeocode=false;

		hideLayer("measureBox");
		break;

	case "selectbox":
		panning=false;
		zooming=false;
		// select mode - requires aimsSelect.js
		if (canQuery) {
			toolMode = 10;
			queryTool=0;
			clickCount=0;
			showBuffer=false;
			if (isIE)	{
				document.all.theTop.style.cursor = "crosshair";
				theCursor = document.all.theTop.style.cursor;
			}

			modeBlurb = modeList[4];
		} else {
			alert(msgList[46]);
		}
		//alert("Function Not Implemented");
		showGeocode=false;
		if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		hideLayer("measureBox");
		break;

	//Inizio MultiSelect : MUS
	case "multiselect":
		//Le variabili booleane che seguono servono per tutti i tipi di selezione che necessitano 2 click
		//il pan --> punto di partenza e punto di arrivo
		//lo zoom --> i due vertici del rettangolo da zoommare
		//la selectbox --> i due vertici della CrossWindow
		//Se non settati a false vuole dire che non è stata ancora specificata la prima coordinata
		panning=false;
		zooming=false;
		selectbox=false;

		//Questa variabile booleana viene settata a TRUE solo quando ci troviamo nel caso di "BufferShape"
		shapeSelectBuffer = false;

		if (canQuery) {
			//Setto il toolmode a 1070 per indicare che l'operazione che sto eseguendo è di selezione multipla
			toolMode = 1070;
			//queryTool --> variabile ad oggi (13/02/03) mai utilizzata per una interrogazione
			queryTool=0;
			clickCount=0;
			showBuffer=false;
			if (isIE)	{
				document.all.theTop.style.cursor = "crosshair";
				theCursor = document.all.theTop.style.cursor;
			}

			modeBlurb = modeList[4];
		} else {
			alert(msgList[46]);
		}
		//alert("Function Not Implemented");
		showGeocode=false;

		//Questa operazione serve per ripulire la parte bassa dello schermo
		//if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		parent.TextFrame.document.location= appDir + "Bottoni_mus_csi.htm";
		hideLayer("measureBox");
		//Da questo momento in poi è sensibile ai click sulla mappa: mousemove, mousedown, mouseup
		break;
	//Fine MultiSelect

	//Inizio Carotaggio : CAR
	case "carotaggio":
		//Le variabili booleane che seguono servono per tutti i tipi di selezione che necessitano 2 click
		//il pan --> punto di partenza e punto di arrivo
		//lo zoom --> i due vertici del rettangolo da zoommare
		//la selectbox --> i due vertici della CrossWindow
		//Se non settati a false vuole dire che non è stata ancora specificata la prima coordinata
		panning=false;
		zooming=false;
		selectbox=false;

		//Questa variabile booleana viene settata a TRUE solo quando ci troviamo nel caso di "BufferShape"
		shapeSelectBuffer = false;

		if (canQuery) {
			//Setto il toolmode a 1071 per indicare che l'operazione che sto eseguendo è il carotaggio
			toolMode = 1071;
			//queryTool --> variabile ad oggi (13/02/03) mai utilizzata per una interrogazione
			queryTool=0;
			clickCount=0;
			showBuffer=false;
			if (isIE)	{
				document.all.theTop.style.cursor = "crosshair";
				theCursor = document.all.theTop.style.cursor;
			}

			modeBlurb = modeList[4];
		} else {
			alert(msgList[46]);
		}
		//alert("Function Not Implemented");
		showGeocode=false;

		//Questa operazione serve per ripulire la parte bassa dello schermo
		//if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		parent.TextFrame.document.location= appDir + "text.htm";
		hideLayer("measureBox");
		//Da questo momento in poi è sensibile ai click sulla mappa: mousemove, mousedown, mouseup
		break;
	//Fine Carotaggio

	case "selectpoint":
		panning=false;
		zooming=false;
		shapeSelectBuffer = false;
		// select mode - requires aimsSelect.js
		if (canQuery) {
			toolMode = 11;
			queryTool=0;
			clickCount=0;
			 resetClick();
			if (isIE)	{
				document.all.theTop.style.cursor = "hand";
				theCursor = document.all.theTop.style.cursor;
			}

			modeBlurb = modeList[5];
		} else {
			alert(msgList[46]);
		}
		//alert("Function Not Implemented");
		showGeocode=false;
		showBuffer=false;
		hideLayer("measureBox");
		break;

	case "selectline":
    //Inizio QuickDraw : QDW
	  if (qdw_csi_useQuickDrawImage) qdw_csi_isQuickDrawActive = true;
    //Fine QuickDraw : QDW
		panning=false;
		zooming=false;
		shapeSelectBuffer = false;
		// select mode - requires aimsSelect.js
		if (canQuery) {
			toolMode = 12;
			queryTool=0;
			if (isIE)	{
				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 = modeList[6];
		} else {
			alert(msgList[46]);
		}
		//alert("Function Not Implemented");
		showGeocode=false;
		showBuffer=false;
		break;

	case "selectpoly":
    //Inizio QuickDraw : QDW
	  if (qdw_csi_useQuickDrawImage) qdw_csi_isQuickDrawActive = true;
    //Fine QuickDraw : QDW
		panning=false;
		zooming=false;
		shapeSelectBuffer = false;
		// select mode - requires aimsSelect.js
		if (canQuery) {
			toolMode = 13;
			queryTool=0;
			if (isIE)	{
				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 = modeList[7];
		} else {
			alert(msgList[46]);
		}
		//alert("Function Not Implemented");
		showGeocode=false;
		showBuffer=false;
		break;

	case "selectshape":
          //Inizio QuickDraw : QDW
	  if (qdw_csi_useQuickDrawImage)
	    qdw_csi_isQuickDrawActive = true;
          //Fine QuickDraw : QDW
		panning=false;
		zooming=false;
		shapeSelectBuffer = false;
		// select mode - requires aimsSelect.js
		if (canQuery) {
			toolMode = 16;
			queryTool=0;
			if (isIE)	{
				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 = modeList[8];
		} else {
			alert(msgList[46]);
		}
		//alert("Function Not Implemented");
		showGeocode=false;
		showBuffer=false;
		break;
	///*
	case "buffershape":
		panning=false;
		zooming=false;

		// interactive shape buffer - not implemented
		if (canQuery) {
			toolMode = 17;
			//toolMode = 16;
			queryTool=0;
			shapeSelectBuffer = true;
			if (isIE)	{
				document.all.theTop.style.cursor = "crosshair";
				theCursor = document.all.theTop.style.cursor;
			}
			hideLayer("measureBox");
			if (useTextFrame) {
				parent.TextFrame.document.location= appDir + "shapeBuffer.htm";
			} else {
				Win1 = open("shapeBuffer.htm","QueryWindow","width=575,height=150,scrollbars=yes,resizable=yes");
			}
			modeBlurb = modeList[11];
		} else {
			alert(msgList[46]);
		}
		//alert("Function Not Implemented");
		showGeocode=false;
		showBuffer=false;
		break;
		//*/
	// Geocode Function
	case "geocode":
		panning=false;
		zooming=false;
		selectBox=false;
		shapeSelectBuffer = false;
		// geocode mode - requires aimsGeocode.js
		hideLayer("measureBox");
		modeBlurb = modeList[14];
		geocodeAppMode = "locate";
		setupGeocode();
		//parent.TextFrame.document.location= appDir + "addmatch.htm";
		break;

	// Query - Search - Find functions
	//if ((toolName=="attributesel") || (toolName=="query")) {
	case "query":
		// query mode - requires aimsQuery.js
		panning=false;
		zooming=false;
		selectBox=false;
		shapeSelectBuffer = false;
		queryStartRecord=1;
		//toolMode=
		queryTool=51;
		if (canQuery) {
			LayerFields.length=1;
			LayerFieldType.length=1;
			LayerFieldCount=0;
			toolMode=8;
			modeBlurb=modeList[15];
			fieldIndex=0;
			setQueryString="";
			hideLayer("measureBox");
			queryForm();
		} else {
			alert(msgList[46]);
		}
		showGeocode=false;
		showBuffer=false;
		break;
	//Inizio NQB
	case "NewQueryBuilder":
		// query mode - requires aimsQuery.js
		nqb_csi_inuse=true;
		panning=false;
		zooming=false;
		selectBox=false;
		shapeSelectBuffer = false;
		queryStartRecord=1;
		//toolMode=
		queryTool=51;
		if (canQuery) {
			LayerFields.length=1;
			LayerFieldType.length=1;
			LayerFieldCount=0;
			toolMode=8;
			modeBlurb=modeList[15];
			fieldIndex=0;
			setQueryString="";
			hideLayer("measureBox");
			NewQueryBuilderForm();
		} else {
			alert(msgList[46]);
		}
		showGeocode=false;
		showBuffer=false;
		break;
	//Fine NQB
	case "storedquery":
		// storedquery mode - requires aimsQuery.js
		panning=false;
		zooming=false;
		selectBox=false;
		shapeSelectBuffer = false;
		queryStartRecord=1;
		queryTool=1;
		toolMode=51;
		modeBlurb="Search";
		if (canQuery) {
			toolMode=51;
			modeBlurb=modeList[16];
			fieldIndex=0;
			setQueryString="";
			hideLayer("measureBox");
			getStoredQueries();
		} else {
			alert(msgList[46]);
		}
		showGeocode=false;
		showBuffer=false;
		break;

	case "find":
		//find
		toolMode=9;
		panning=false;
		zooming=false;
		selectBox=false;
		shapeSelectBuffer = false;
		queryStartRecord=1;
		queryTool=1;
		if (canQuery) {
			LayerFields.length=1;
			LayerFieldType.length=1;
			LayerFieldCount=0;

			fieldIndex=0;
			setQueryString="";
			hideLayer("measureBox");
			modeBlurb = modeList[17];
			findForm();
		} else {
			alert(msgList[46]);
		}
		showGeocode=false;
		showBuffer=false;
		break;

	case "clearsel":
		 clearSelection();
		 break;

	// Buffer function
	case "buffer":
		//buffer - requires aimsBuffer.js
		if (useBuffer) {
                        //Inizio MultiSelect : MUS
                        //	if (checkSelected()) {
			if ((checkSelected()) || (mus_csi_multiSelectDataArray.length>0)) {
                        //Fine MultiSelect
				toolMode = 25;
				shapeSelectBuffer = false;
				modeBlurb = modeList[18];
				writeBufferForm();
			} else {
				showBuffer=false;
				alert(msgList[48]);
			}
		} else {
			alert(msgList[49]);
		}
		break;

	case "options":
		writeOptionForm();
		break;

	// Print function
	case "print":
		 printIt();
		 break;

	// custom modes
	case "dbidentify":
		panning=false;
		zooming=false;
		selectBox=false;
		shapeSelectBuffer = false;
		// identify mode - requires custom db query - not in basic
		if (canQuery) {
			toolMode = 40;
			if (isIE)	{
				document.all.theTop.style.cursor = "hand";
				theCursor = document.all.theTop.style.cursor;
			}

			modeBlurb = modeList[3];
		} else {
			alert(msgList[46]);
		}
		//alert("Function Not Implemented");

		showGeocode=false;
		if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		hideLayer("measureBox");
		break;

	//PER EXTRACT
	case "extract":
		 ex_extractIt();
		 break;
	//FINE EXTRACT

	case "legend":
		if (aimsLegendPresent) {
			if (imsURL!="") {
				if (hasTOC) {
					if (legendVisible) {
						legendVisible=false;
					//writeLayerList();
						parent.TOCFrame.document.location=appDir+"toc.htm";
					} else {
						legendVisible=true;

						//INIZIO Help-Legenda statici: HLS
						if (hls_csi_useStaticLegendHtm){
							//alert('legend html');
							hls_csi_showLegendHTM();

						} else{

							if (hls_csi_useStaticLegendImg)
							{
								showLegend();
								//alert('showLegend()');

							} else {
								//alert('getLegend()');
								getLegend();
							}
						}
						//FINE Help-Legenda statici: HLS

					}

				} else {
					legendVisible=true;

					//INIZIO Help-Legenda statici: HLS
					if (hls_csi_useStaticLegendHtm){
						hls_csi_showLegendHTM();
					} else {
						if (hls_csi_useStaticLegendImg)
						{
							showLegend();
						} else {
							getLegend();
						}
					}
					//FINE Help-Legenda statici: HLS

				}
			} else {
				alert(msgList[45]);
			}
		} else {
			alert(msgList[50]);
		}
		break;

	case "layerlist":
		// put LayerList in separate window
		writeLayerListForm();
		break;
	//PER EDITING
   case "editing":
     //Inizio QuickDraw : QDW
    if (qdw_csi_useQuickDrawImage) qdw_csi_isQuickDrawActive = true;
    //Fine QuickDraw : QDW
		panning=false;
		zooming=false;
		shapeSelectBuffer = false;
		// select mode - requires aimsSelect.js
		if (canQuery) {
			toolMode = 50;
			queryTool=0;
			if (isIE)	{
				document.all.theTop.style.cursor = "crosshair";
				theCursor = document.all.theTop.style.cursor;
			}
			hideLayer("measureBox");

			if (useTextFrame) {
				parent.TextFrame.DesignEditingFrame.document.location= appDir + "DesignEditing.htm";
			} else {
				Win1 = open("DesignEditing.htm","QueryWindow","width=575,height=150,scrollbars=yes,resizable=yes");
			}

			modeBlurb = modeList[8];
		} else {
			alert(msgList[46]);
		}
		//alert("Function Not Implemented");
		showGeocode=false;
		showBuffer=false;
		break;

	case "editingTool":

	   panning=false;
		zooming=false;
		shapeSelectBuffer = false;
		if(selectCount>0) clearSelection();
		// editing mode - requires aimsEditing.js
		if (canQuery) {
			toolMode = 8;
			queryTool= 0;
			hideLayer("measureBox");
			if (useTextFrame) {
				parent.TextFrame.document.location= appDir + "editing.htm";
			} else {
				Win1 = open("editing.htm","EditingWindow","width=575,height=150,scrollbars=yes,resizable=yes");
			}
		} else {
			alert(msgList[46]);
		}
		//alert("Function Not Implemented");
		showGeocode=false;
		showBuffer=false;
		break;

	case "ed_selectbox":
	   //alert('ed_selectbox');
	   panning=false;
		zooming=false;
		// select mode - requires aimsSelect.js
		if (canQuery) {
			toolMode = 60;
			queryTool=0;
			clickCount=0;
			showBuffer=false;
			if (isIE)	{
				document.all.theTop.style.cursor = "crosshair";
				theCursor = document.all.theTop.style.cursor;
			}

			modeBlurb = modeList[4];
		} else {
			alert(msgList[46]);
		}
		//alert("Function Not Implemented");
		showGeocode=false;
		if (useTextFrame) parent.TextFrame.DesignEditingFrame.document.location= appDir + "blankEditing.htm";

		hideLayer("measureBox");
		break;

		//FINE EDITING
	case "address":
		panning=false;
		zooming=false;
		selectBox=false;
		shapeSelectBuffer = false;
		// geocode mode - requires aimsGeocode.js
		hideLayer("measureBox");
		modeBlurb = modeList[21];
		geocodeAppMode = "address";
		if (isIE)	{
			document.all.theTop.style.cursor = "hand";
			theCursor = document.all.theTop.style.cursor;
		}
		toolMode = 7;
		//parent.TextFrame.document.location= appDir + "addmatch.htm";
		break;
	case "route":
		// route requires aimsRoute.js and RouteServer extension
		panning=false;
		zooming=false;
		selectBox=false;
		shapeSelectBuffer = false;
		// geocode mode - requires aimsGeocode.js
		hideLayer("measureBox");
		modeBlurb = modeList[21];
		geocodeAppMode = "route";
		if (isIE)	{
			document.all.theTop.style.cursor = "hand";
			theCursor = document.all.theTop.style.cursor;
		}
		toolMode = 6;
		setupGeocode();

		break;
	//Inizio RicercaClasseTerritoriale : RCT
	case "RicercaClasseTerritoriale":
		//alert("qui");
		//if (useTextFrame) parent.TextFrame.document.location= appDir + "text_rct_csi.htm";
		parent.TextFrame.document.location= appDir + "text_rct_csi.htm";
	break;
	//Fine RicercaClasseTerritoriale : RCT
	//Inizio SendMail : SEM
	// SendMail function
	case "SendMail":
		sem_csi_flagUseSendMail = true;
		//Stampa a singola pop-up con titolo del layout definito
		//dalla variabile pml_csi_nomeLayoutStampa
		getPrintMap(sem_csi_nomeLayoutInvioMail)

		break;
	//Fine SendMail : SEM

	default:
		alert(msgList[51]);
	}
	modeName=modeBlurb;
	if (useModeFrame) {
		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);
	}
	//PER EDITING
	if ((toolMode == 60) && (selectBox)) {
		ed_stopSelectBox(e);
	}
   //FINE EDITING
	return false;

}

// perform appropriate action with mapTool
function mapTool (e) {
	if(cursorInMenu())return;
	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);


		// Controlla se spostare il menu flottante oppure eseguire le funzioni
		if (over) {

			SpostaMenu();

		} else {

			NOSpostaMenu();

			if ((mouseX>=0) && (mouseX<iWidth) && (mouseY>=0) && (mouseY<iHeight)) {
				//if ((!isNav) || (!is5up)) {
				if ((hasOVMap) && (ovIsVisible) && (mouseX<i2Width+ovBoxSize) && (mouseY<i2Height) && (ovMapIsLayer)) {
						ovMapClick(mouseX,mouseY);
						window.status = "On OV Map Area";
					//}
				} 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 5:
						// identify all
						identifyAll(e);
						break;

					// custom modes
					///*
					case 6:
						// route - requires custom route routine - not in default
							// requires aimsRoute.js, aimsGeocode.js
						//routeClick = routeClick + 1;
						//if (routeClick > 2) routeClick = 2;
						//setRouteXY()
						//writeRoutePage();
						address(e);
						break;
					//*/
					case 7:
						// address - requires custom reverseGeocode routine - not in default
							// requires aimsRoute.js, aimsGeocode.js
						address(e);
						break;
					//*/
					case 10:
						//select(e);
						startSelectBox(e);
						return false;
						break;
					case 11:
						//select point
						if (checkIfActiveLayerAvailable()) {
							select(e);
						}
						break;
					case 12:
						//select line
						if (checkIfActiveLayerAvailable()) {
							clickType=2;
							clickAddPoint();
							if (useTextFrame) {
								if (parent.TextFrame.document.title!==modeList[60]) {
									parent.TextFrame.document.location= appDir + "selectline.htm";
								}
							}
						}
						break;
					case 13:
						//select polygon
						if (checkIfActiveLayerAvailable()) {
							clickType=3;
							clickAddPoint();
							if (useTextFrame) {
								if (parent.TextFrame.document.title!==modeList[7]) {
									parent.TextFrame.document.location= appDir + "selectpoly.htm";
								}
							}
						}
						break;
					case 15:
						// hyperlink
						hyperLink(e);
						break;
					case 16:
						//select shape
						if (checkIfActiveLayerAvailable()) {
							clickType=2;
							clickAddPoint();
							if (useTextFrame) {
								if (parent.TextFrame.document.title!==modeList[8]) {
									parent.TextFrame.document.location= appDir + "select.htm";
								}
							}
						}
						break;
						///*
					case 17:
						//buffer shape -
						if (checkIfActiveLayerAvailable()) {
							clickType=2;
							clickAddPoint();
							if (useTextFrame) {
								if (parent.TextFrame.document.title!=modeList[11]) {
									parent.TextFrame.document.location= appDir + "shapeBuffer.htm";
								}
							}
						}
						break;
						//*/
					case 20:
						// measure
						clickType=1;
						clickAddPoint();
						break;
					case 21:
						// shape
						clickType=4;
						clickAddPoint();
						break;
					case 30:
						// hyperlink
						hyperLinkAny(e);
						break;
					case 40:
						// db identify - requires custom db query - not in default
						if (aimsDBPresent) {
							matchDBLinkLayer(dbLinkLayer);
							dbIdentify(e);
						}
						break;
					//PER EDITING
					case 50:
						 if (checkIfActiveLayerAvailable()) {
							if(ed_checkIfActiveLayerEditable()) {
								clickType=4;
								if(LayerType[ActiveLayerIndex]=="point"){
								   //cancello il punto precedente....
								   ed_resetANDclickAddPoint();
								}
								clickAddPoint();
								/*
								if (useTextFrame) {
									if (parent.TextFrame.document.title!==modeList[8]) {
										parent.TextFrame.document.location= appDir + "editing.htm";
									}
								}*/
							}
						 }
						break;
					case 60:
						ed_startSelectBox(e);
						return false;
						break;
					//FINE EDITING
					default:
						if (toolMode>=1000) {
							customMapTool(e);
						}
					}
				}
			}

		}
		//Fine controllo su menu flottante
	}
}

// update measureBox layer
function updateMeasureBox() {
	if (isNav4) {
		var theForm = document.layers["measureBox"].document.forms[0];
	} else {
		//var theForm = document.measureBox.forms[0];
		var theForm = document.forms[0];
	}
	var j = 1;
	for (var i=0;i<sUnitList.length;i++) {
		if (ScaleBarUnits==sUnitList[i]) j=i;
	}
		var u = Math.pow(10,numDecimals);
		var tMeas = 0;
		if (totalMeasure!=0) tMeas = parseInt(totalMeasure*u+0.5)/u;
	theForm.theMeasTotal.value = tMeas + " " + unitList[j];
	theForm.theMeasSegment.value = currentMeasure + " " + unitList[j];
	showLayer("measureBox");

}





// Restituisce il focus sulla mappa
function reFocusMap() {

	if (aimsNavigationPresent) {
		if (isNav4) {
			document.captureEvents(Event.MOUSEMOVE);
			document.captureEvents(Event.MOUSEDOWN);
			document.captureEvents(Event.MOUSEUP);
		}
		document.onmousemove = getMouse;
		document.onmousedown = mapTool;
		document.onmouseup = chkMouseUp;

	} else {

		usePan=false;
		usePanNorth=false;
		usePanWest=false;
		usePanEast=false;
		usePanSouth=false;
		useMeasure=false;
		useZoomIn=false;
		useZoomOut=false
		useZoomActive=false;
	}
}

// Apre la pagina di visualizzazione dei Limiti di Scala
function showLimiti(){
    var Win2 = open( doc_loc_sub +"LimitiScala.html","LimitiWindow","width=720,height=600,scrollbars=yes,resizable=yes");
    Win2.focus();
}