aimsClickPresent=true;

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 panning=false;
var zooming=false;
var selectBox=false;
var selection=false;
var measure=false;
var insert=false;


var leftButton =1;
var rightButton = 2;

// determinano il browser e la versione
var isIE = (document.all?true:false);
var isNav = !isIE;
var isNav4 = false;
var isIE4 = false;
var is5up = false;
var isMoz = (((navigator.userAgent.indexOf("Gecko") >= 0) &&(navigator.userAgent.indexOf("Netscape") < 0))?true:false);
if (isNav) {
	leftButton = 1;
	rightButton = 3;
}
//alert(isMoz+', '+isNav+', ',+isIE+', '+isIE4);
var modeList = new Array();

modeList[0] = "Zoom In";
modeList[1] = "Zoom Out";
modeList[2] = "Pan";
modeList[3] = "Identify";
modeList[4] = "Select Rectangle";
modeList[5] = "Select Point";
modeList[6] = "Select Line";
modeList[7] = "Select Polygon";
modeList[8] = "Select Circle"; 
modeList[9] = "Measure";

var modeBlurb = modeList[0];

// Funzione per la personalizzazione della barra degli strumenti
function getNewRow(when,newRow) {
	var output = '';
	if(when == 1 && newRow == 'true') output = '<tr>';
	else if(when == 2 && newRow == 'true') newRow = 'false';
	else if(when == 1 && newRow == 'false');
	else if(when == 2 && newRow == 'false') {
		output = '</tr>';
		newRow = 'true';
	}
	return new Array(output,newRow);
}

// Funzione che mi restituisce il codice html relativo ad un pulsante vuoto
function getEmptyButton() {
	return '<td width="30" height="31"><a><img src="imgs/tool/_vuoto.gif" border="0" name="vuoto" alt=""/></a></td>';
}	

function setSearchFrame(chePagina){
	parent.SearchFrame.location.href=chePagina;
}

/**
 * Cancella i filtri impostati durante l'avvio del ctw. Ripristina quindi una 
 * situazione di avvio normale...
 */
function cancellaFiltri() {

	parent.MapFrame.showLayer("LoadMap");
		
	var theForm = parent.PostFrame.document.theForm;
		
	theForm.service_name.value="NavigaMappa";
	theForm.moveaction.value="cancella_filtri";
		
	theForm.target="EmptyFrame1";
		
	// imposta le dimensioni della mappa
	theForm.map_width.value = parent.MapFrame.mWidth;
	theForm.map_height.value = parent.MapFrame.mHeight;
		
	// salva le coordinate attuali
	theForm.lastMinx.value = theForm.minx.value;
	theForm.lastMiny.value = theForm.miny.value;
	theForm.lastMaxx.value = theForm.maxx.value;
	theForm.lastMaxy.value = theForm.maxy.value;

	// elimina i layers per disegnare
	theForm.create_layers.value = "no";
	theForm.submit();
		
	// imposta la tool bar a zoomin
	clickImage('zoomin','Tool');
	parent.MapFrame.clickFunction('zoomin');
}

function cancellaSelezione() {

	parent.MapFrame.showLayer("LoadMap");
		
	var theForm = parent.PostFrame.document.theForm;
		
	theForm.service_name.value="NavigaMappa";
	theForm.moveaction.value="cancella_selezione";
		
	theForm.target="EmptyFrame1";
		
	// imposta le dimensioni della mappa
	theForm.map_width.value = parent.MapFrame.mWidth;
	theForm.map_height.value = parent.MapFrame.mHeight;
		
	// salva le coordinate attuali
	theForm.lastMinx.value = theForm.minx.value;
	theForm.lastMiny.value = theForm.miny.value;
	theForm.lastMaxx.value = theForm.maxx.value;
	theForm.lastMaxy.value = theForm.maxy.value;

	// elimina i layers per disegnare
	theForm.create_layers.value = "no";
	theForm.submit();
		
	// imposta la tool bar a zoomin
	clickImage('zoomin','Tool');
	parent.MapFrame.clickFunction('zoomin');

	// ricrea il search frame
	//parent.PostFrame.document.frame.target="SearchFrame";
	//parent.PostFrame.document.frame.service_name.value="Search";
	//parent.PostFrame.document.frame.submit();
}

function clickImage(cheImg,cheFrame) {

	// document dei pulsanti
	var cheDocument = parent.MapFrame.document;
	if(parent.PostFrame.clickedImg != ""){
		cheDocument.images[parent.PostFrame.clickedImg].src = "imgs/tool/" + parent.PostFrame.clickedImg + "_off.gif";
	}
	parent.PostFrame.clickedImg = cheImg;
	cheDocument.images[parent.PostFrame.clickedImg].src = "imgs/tool/" + parent.PostFrame.clickedImg + "_ok.gif";
}

function selectImage(cheImg,cheStato) {
	parent.MapFrame.document.images[cheImg].src = "imgs/tool/" + cheImg + "_" + cheStato + ".gif";
}

function onImage(cheImg,cheStato){
	if(cheImg != parent.PostFrame.clickedImg){
		parent.MapFrame.document.images[cheImg].src = "imgs/tool/" + cheImg + "_" + cheStato + ".gif";
	}
}

function onImageWin(cheImg,cheStato,cheId){
	var cheWin = parent.MapFrame.document.getElementById(cheId);
	if(cheWin.style.visibility == 'hidden') {
		parent.MapFrame.document.images[cheImg].src = "imgs/tool/" + cheImg + "_" + cheStato + ".gif";
	}
}

function checkWinCtrls(){
	var mappeWebIds = parent.PostFrame.document.mappeWebIds;
	var catNames = parent.PostFrame.document.catNames;
	if(mappeWebIds != undefined) {
		if(!mappeWebStatusBar.isStatusVisible()) parent.MapFrame.document.images["showRacc"].src = "imgs/tool/showRacc_off.gif";
	}
	if(catNames != undefined) {
		if(!pluginStatusBar.isStatusVisible()) parent.MapFrame.document.images["showPlug"].src = "imgs/tool/showPlug_off.gif";
	}
	if(!searchStatusBar.isStatusVisible()) parent.MapFrame.document.images["showRic"].src = "imgs/tool/showRic_off.gif";
	if(!overviewStatusBar.isStatusVisible()) parent.MapFrame.document.images["showOv"].src = "imgs/tool/showOv_off.gif";
	if(!tocStatusBar.isStatusVisible()) parent.MapFrame.document.images["showCarto"].src = "imgs/tool/showCarto_off.gif";
	if(!legendStatusBar.isStatusVisible()) parent.MapFrame.document.images["showLeg"].src = "imgs/tool/showLeg_off.gif";
	if(!urlStatusBar.isStatusVisible()) parent.MapFrame.document.images["buildURL"].src = "imgs/tool/buildURL_off.gif";
}

function onImageNoTool(cheImg,cheStato){
	document.images[cheImg].src = "imgs/" + cheImg + "_" + cheStato + ".gif";
}

function clickFunction(toolName) {
	
	switch(toolName) {
		
		case "cancellafiltri":
			cancellaFiltri();
		break
		
		case "resize":
	 	onResizeFromUser(true);
		break;
		
		case "wms":
	 	 var wmsLoader = parent.MapFrame.wmsLoader;
		// Visualizzo la finestra
		wmsLoader.showAll(true);
	     //La porto in alto
		parent.MapFrame.dd.elements.wmsLoader.maximizeZ();
		break;
		
		case "linknav3D":
		var theForm = parent.PostFrame.document.theForm;
		theForm.service_name.value = "linknav3D";
        theForm.service_action.value = "";
		theForm.target = "EmptyFrame1";
		theForm.submit();
		break;
	
		case "zoomin":
			toolMode = 1;
			panning=false;
			selectBox=false;
			modeBlurb = modeList[0];
			if(parent.PostFrame.document.theForm.create_layers.value == "si") parent.MapFrame.deleteLayers();
			parent.PostFrame.document.theForm.create_layers.value = "no";
			if(parent.MapFrame.dd.elements.supportStatusBarLayer != undefined) parent.MapFrame.dd.elements.supportStatusBarLayer.hide();
			break
	
		case "zoomout":
			toolMode = 2;
			panning=false;
			selectBox=false;
			modeBlurb = modeList[1];
			if(parent.PostFrame.document.theForm.create_layers.value == "si") parent.MapFrame.deleteLayers();
			parent.PostFrame.document.theForm.create_layers.value = "no";
			parent.MapFrame.dd.elements.supportStatusBarLayer.hide();
			break
	
		case "pan":
			toolMode = 3;
			zooming=false;
			selectBox=false;
			if(parent.PostFrame.document.theForm.create_layers.value == "si") parent.MapFrame.deleteLayers();
			parent.PostFrame.document.theForm.create_layers.value = "no";
			parent.MapFrame.dd.elements.supportStatusBarLayer.hide();
			break

		case "identify":
			toolMode = 4;
			panning=false;
			zooming=false;
			selectBox=false;
			if(parent.PostFrame.document.theForm.create_layers.value == "si") parent.MapFrame.deleteLayers();
			parent.MapFrame.dd.elements.supportStatusBarLayer.hide();
			break

		case "identifyWMS":
			toolMode = 97;
			panning=false;
			zooming=false;
			selectBox=false;
			break;


		case "zoomlast":
			toolMode = 5;
			submitData();
			panning=false;
			zooming=false;
			selectBox=false;
			insert=false;
			if(parent.PostFrame.document.theForm.create_layers.value == "si") parent.MapFrame.deleteLayers();
			parent.PostFrame.document.theForm.create_layers.value = "no";
			parent.MapFrame.dd.elements.supportStatusBarLayer.hide();
			break
	
		case "zoomactive":
			toolMode = 6;
			panning=false;
			zooming=false;
			selectBox=false;
			if(parent.PostFrame.document.theForm.create_layers.value == "si") parent.MapFrame.deleteLayers();
			parent.PostFrame.document.theForm.create_layers.value = "no";
			parent.MapFrame.dd.elements.supportStatusBarLayer.hide();
			submitData();
			break
	
		case "fullextent":
			toolMode = 7;
			fullExtent();
			if(parent.PostFrame.document.theForm.create_layers.value == "si") parent.MapFrame.deleteLayers();
			parent.MapFrame.dd.elements.supportStatusBarLayer.hide();
			break
			
		case "selectbox":
			toolMode = 8;
			panning=false;
			zooming=false;
			modeBlurb = modeList[4];
			if(parent.PostFrame.document.theForm.create_layers.value == "si") parent.MapFrame.deleteLayers();
			parent.PostFrame.document.theForm.create_layers.value = "no";
			parent.MapFrame.dd.elements.supportStatusBarLayer.hide();
			break
		
		case "logout":
			parent.PostFrame.document.theForm.service_name.value="logout";
			parent.PostFrame.document.theForm.target="_top";
			parent.PostFrame.document.theForm.submit();
			
		case "select line/polygon":
			parent.MapFrame.selectLinePolInit();
			toolMode = 9;
			panning = false;
			zooming = false;
			selection = true;
			isSelect = true;
			insert = false;
			modeBlurb = modeList[6];
			var theForm = parent.PostFrame.document.theForm;
			theForm.ricerca.value = false;
			var vUndefined;
			lastLayer = 0;
			arrPoint = new Array();	
			if (theForm.create_layers.value == "si") parent.MapFrame.createLayers();
			parent.PostFrame.document.frame.ricerca.value = "false";
			break;
			
		case "selectcircle":
			parent.MapFrame.selectCircleInit();
			toolMode = 10;
			panning = false;
			zooming = false;
			selection = true;
			insert = false;
			var theForm = parent.PostFrame.document.theForm;
			theForm.ricerca.value = false;
			isSelect = true;
			lastLayer = 0;	
			arrPoint = new Array();	
			modeBlurb = modeList[6];
			if (theForm.create_layers.value == "si") parent.MapFrame.createLayers();
			parent.PostFrame.document.frame.ricerca.value = "false";
			break;
	


          case "measuredist":
			var theForm = parent.PostFrame.document.theForm;
			var scala = new Number(theForm.scale.value);
			var scalaMax = new Number(parent.PostFrame.document.scalaMaxDisegno);
			if(scalaMax.valueOf()!=0 && scala.valueOf() > scalaMax.valueOf()) {
				alert("E' stata disabilitata la funzione di misurazione a scale inferiori a 1:" + scalaMax + ".");
				clickImage('zoomin','Tool');
				clickFunction('zoomin');
				break;
			} else {
				parent.MapFrame.measureDistInit();
			}
			toolMode = 11;
			panning = false;
			zooming = false;
			measure = true;
			isSelect = true;
			insert = false;
			theForm.ricerca.value = false;

			lastLayer = 0;
			arrPoint = new Array();
			modeBlurb = modeList[9];
			if (theForm.create_layers.value == "si") parent.MapFrame.createLayers();			
			parent.PostFrame.document.theForm.toolName.value = "measuredist";
			parent.PostFrame.document.frame.ricerca.value = "false";
			break;

		case "measurearea":
			var theForm = parent.PostFrame.document.theForm;
			var scala = new Number(theForm.scale.value);
			var scalaMax = new Number(parent.PostFrame.document.scalaMaxDisegno);
			if(scalaMax.valueOf()!=0 && scala.valueOf() > scalaMax.valueOf()) {
				alert("E' stata disabilitata la funzione di misurazione a scale inferiori a 1:"+scalaMax+".");
				clickImage('zoomin','Tool');
				clickFunction('zoomin');
				break;
			} else {
				parent.MapFrame.measureAreaInit();
			}
			toolMode = 12;
			panning = false;
			zooming = false;
			measure = true;
			isSelect = true;
			insert = false;
			lastLayer = 0;
			arrPoint = new Array();
			modeBlurb = modeList[9];

			if (theForm.create_layers.value == "si") parent.MapFrame.createLayers();
			parent.PostFrame.document.theForm.toolName.value = "measurearea";
			parent.PostFrame.document.frame.ricerca.value = "false";
			parent.PostFrame.document.theForm.ricerca.value = false;
			break;
			
		case "pluginidentify":
			toolMode = 13;
			panning=false;
			zooming=false;
			selectBox=false;
			insert=false;
			parent.MapFrame.dd.elements.supportStatusBarLayer.hide();
			break;

		case "inseriscinota":
			toolMode = 14;
			panning=false;
			zooming=false;
			measure=false;
			insert=true;
			isSelect = true;
			var theForm = parent.PostFrame.document.theForm;
			// accende il tema delle note
			refreshTema(theForm.nome_tema_note.value,true);
			// attiva il tema delle note
			layerAttivo(theForm.nome_tema_note.value);
			theForm.ricerca.value=false;
			if (parent.OptionFrame.arBt[1][0]==1)
				parent.OptionFrame.arBt[1][0]=0;
			FB_swapImage("img1",parent.OptionFrame.arBt[1][1]);
			if (flagLayer) {
				hideLayers();
			}
			lastLayer=0;
			arrPoint = new Array();
			modeBlurb = modeList[9];
			theForm.service_name.value="RefreshMap";
			if (theForm.create_layers.value == "si" && theForm.local.value=="0") {
				theForm.submit();
				theForm.local.value = "1";
			}
			parent.PostFrame.document.theForm.toolName.value="inseriscinota";
			parent.PostFrame.document.frame.ricerca.value="false";
			break;

		case "cancellanota":
			toolMode = 15;
			panning=false;
			zooming=false;
			measure=false;
			insert=true;
			isSelect = true;
			var theForm = parent.PostFrame.document.theForm;
			// accende il tema delle note
			refreshTema(theForm.nome_tema_note.value,true);
			// attiva il tema delle note
			layerAttivo(theForm.nome_tema_note.value);
			theForm.ricerca.value=false;
			if (parent.OptionFrame.arBt[1][0]==1)
				parent.OptionFrame.arBt[1][0]=0;
			FB_swapImage("img1",parent.OptionFrame.arBt[1][1]);
			if (flagLayer) {
				hideLayers();
			}
			lastLayer=0;
			arrPoint = new Array();
			modeBlurb = modeList[9];
			theForm.service_name.value="RefreshMap";
			if (theForm.create_layers.value == "si" && theForm.local.value=="0") {
				theForm.submit();
				theForm.local.value = "1";
			}
			parent.PostFrame.document.theForm.toolName.value="cancellanota";
			parent.PostFrame.document.frame.ricerca.value="false";
			break;

        case "cancellaselezione":
			cancellaSelezione();
			break

		case "print":
		
			var printform = parent.PostFrame.document.print;
			printform.scale.value = parent.MapFrame.document.getElementById('scaleValue').value;
			printform.service_name.value = "print";
            printform.service_action.value = "";
			printform.target = "EmptyFrame1";
			printform.gruppi.value = parent.PostFrame.document.theForm.gruppi.value;
			printform.submit();
			break
        case "printsession":
		
			parent.OptionFrame.FB_optionClick(1,false);
			var newWin=window.open('about:blank','PrintFormWindow','resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,top=50,left=150,width=800,height=600');
			newWin.focus();
			var printform = parent.PostFrame.document.print;
			printform.scale.value=parent.MapFrame.document.scaleForm.scale.value;
			printform.service_name.value="print";                        
            printform.service_action.value="printsession";
			printform.target="PrintFormWindow";
			printform.gruppi.value=parent.PostFrame.document.theForm.gruppi.value;
			printform.submit();
			break
			
		case "extract":
			var theForm = parent.PostFrame.document.extract;
			theForm.scale.value = parent.PostFrame.document.theForm.scale.value;
			theForm.gruppi.value = parent.PostFrame.document.theForm.gruppi.value;
            theForm.xml_view.value = "<request>" +
                                          "<request_from>browser</request_from>" +
                                          "<service_name>MapExtract_view</service_name>" +
                                          "<service_params>" +
                                              "<scala_attuale>" + theForm.scale.value + "</scala_attuale>" +
                                              theForm.gruppi.value +
                                          "</service_params>" +
                                       "</request>";
                             
			theForm.service_name.value = "ViewerXML";
			theForm.target = "EmptyFrame1";
			theForm.submit();
			
			if(isNav4 || isMoz){window.setTimeout("theForm.target=\"EmptyFrame1\"",50);}	
			else{theForm.target="EmptyFrame1";}
			break		 
	
		case "url":
		
			//Non e' possibile costruire tramite javascript la URL per riaprire la pagina corrente nello stato (zoom e layer accesi) in cui si trova
			//poiche' alcune informazioni (quale ad esempio "org") sono contenute nella sessione che e' accedibile solo lato server.
			
		   	//Sintassi url  http://<nome_server>:<porta>/clientweb/loginservlet?service_name=ApriViewer&org=01&ente=1&ente_str=01
    		//				&denom_ente=<denom_ente>&utente=<user>&pwd=<password>&scalaIngresso=<scala_ingresso>&pointCoords=<Xcoord@Ycoord>
    		//				&mappaweb=<mappa>
			var theForm = parent.PostFrame.document.theForm;
			parent.MapFrame.sendURLReq();
			if(isNav4 || isMoz){window.setTimeout("theForm.target=\"MapFrame\"",50);}	
			else{theForm.target="EmptyFrame1";}
			break;		 
	}
	modeName = modeBlurb;
	
	var theForm = parent.PostFrame.document.theForm;

	// cancella i disegni dalla mappa
	if (lastLayer!=0 && toolName!="print") {
		lastLayer = 0;
		var disegnoLayer = parent.MapFrame.document.getElementById('disegnoLayer');
		if(disegnoLayer.innerHTML != '') hideLayers();
		theForm.xml_points.value = "";
	}
	
	theForm.toolName.value = toolName;		
}

