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;
}

function cancellaSelezione() {

	parent.MapFrame.showLayer("LoadMap");
		
	var theForm = parent.PostFrame.document.theForm;
		
	theForm.service_name.value="NavigaMappa";
	theForm.moveaction.value="cancella_selezione";
		
	theForm.target="MapFrame";
		
	// imposta le dimensioni della mappa
	var mapFrameWidth = (document.all?parent.MapFrame.document.body.clientWidth:parent.MapFrame.window.innerWidth);
	var mapFrameHeight = (document.all?parent.MapFrame.document.body.clientHeight:parent.MapFrame.window.innerHeight);			
	theForm.map_width.value=mapFrameWidth;
	theForm.map_height.value=mapFrameHeight;
		
	// 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 del pulsante appena cliccato
	var document;
	// document del pulsante cliccato precedentemente
	var oldDocument;
	var nomeOldFrame = parent.PostFrame.clickedFrame;
	if (cheFrame=="Tool") {
		document = parent.Tool.document;
	} else if (cheFrame=="Plugin"){
		document = parent.Plugin.document;
	}

	if (nomeOldFrame=="Tool") {
		oldDocument = parent.Tool.document;
	} else {
		oldDocument = parent.Plugin.document;
	}
	
	if(parent.PostFrame.clickedImg != ""){
		oldDocument.images[parent.PostFrame.clickedImg].src = "imgs/tool/" + parent.PostFrame.clickedImg + "_off.gif";
	}
	parent.PostFrame.clickedImg = cheImg;
	parent.PostFrame.clickedFrame = cheFrame;
	document.images[parent.PostFrame.clickedImg].src = "imgs/tool/" + parent.PostFrame.clickedImg + "_ok.gif";
	
}

function onImage(cheImg,cheStato){
	if(cheImg != parent.PostFrame.clickedImg){
		document.images[cheImg].src = "imgs/tool/" + cheImg + "_" + cheStato + ".gif";
	}
}

function clickFunction(toolName) {
	
	switch(toolName) {
	
		case "zoomin":
			toolMode = 1;
			panning=false;
			selectBox=false;
			modeBlurb = modeList[0];
			parent.PostFrame.document.theForm.create_layers.value = "no";
			break
	
		case "zoomout":
			toolMode = 2;
			panning=false;
			selectBox=false;
			modeBlurb = modeList[1];
			parent.PostFrame.document.theForm.create_layers.value = "no";
			break
	
		case "pan":
			toolMode = 3;
			zooming=false;
			selectBox=false;
			parent.PostFrame.document.theForm.create_layers.value = "no";
			break

		case "identify":
			toolMode = 4;
			panning=false;
			zooming=false;
			selectBox=false;
			break

		case "zoomlast":
			toolMode = 5;
			submitData();
			panning=false;
			zooming=false;
			selectBox=false;
			insert=false;
			parent.PostFrame.document.theForm.create_layers.value = "no";
			break
	
		case "zoomactive":
			toolMode = 6;
			panning=false;
			zooming=false;
			selectBox=false;
			parent.PostFrame.document.theForm.create_layers.value = "no";
			submitData();
			break
	
		case "fullextent":
			toolMode = 7;
			fullExtent();
			break
			
		case "selectbox":
			toolMode = 8;
			panning=false;
			zooming=false;
			modeBlurb = modeList[4];
			parent.PostFrame.document.theForm.create_layers.value = "no";
			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":
			toolMode = 9;
			panning=false;
			zooming=false;
			selection=true;
			isSelect = true;
			insert=false;
			modeBlurb = modeList[6];
			if (parent.OptionFrame.arBt[1][0]==1)
				parent.OptionFrame.arBt[1][0]=0;
			FB_swapImage("img1",parent.OptionFrame.arBt[1][1]);
			parent.PostFrame.document.theForm.ricerca.value=false;
			var vUndefined;
			if (flagLayer) {
				hideLayers();
			}
			lastLayer=0;
			arrPoint = new Array();	
			var theForm = parent.PostFrame.document.theForm;
			theForm.service_name.value="RefreshMap";
			if (theForm.create_layers.value == "si" && theForm.local.value=="0") {
				theForm.submit();
				theForm.local.value = "1";	
			}
			parent.PostFrame.document.frame.ricerca.value="false";
			break;
			
		case "selectcircle":
			toolMode = 10;
			panning=false;
			zooming=false;
			selection=true;
			insert=false;
			var theForm = parent.PostFrame.document.theForm;
			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]);
			isSelect = true;
			if (flagLayer) {
				hideLayers();
			}
			lastLayer=0;	
			arrPoint = new Array();	
			modeBlurb = modeList[6];
			theForm.service_name.value="RefreshMap";
			if (theForm.create_layers.value == "si" && theForm.local.value=="0") {
				theForm.submit();
				theForm.local.value = "1";
			}
			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+".");
				parent.Tool.clickImage('zoomin','Tool');
				clickFunction('zoomin');
				break;
			} else {
				setSearchFrame('measuredist.htm');
			}
			toolMode = 11;
			panning=false;
			zooming=false;
			measure=true;
			isSelect = true;
			insert=false;
			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.target = "MapFrame";
				theForm.submit();
				theForm.local.value = "1";
			}
			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+".");
				parent.Tool.clickImage('zoomin','Tool');
				clickFunction('zoomin');
				break;
			} else {
				setSearchFrame('measurearea.htm');
			}
			toolMode = 12;
			panning=false;
			zooming=false;
			measure=true;
			isSelect = true;
			insert=false;
			if (flagLayer) {
				hideLayers();
			}
			lastLayer=0;
			arrPoint = new Array();
			modeBlurb = modeList[9];
			theForm.service_name.value="RefreshMap";
			if (parent.OptionFrame.arBt[1][0]==1)
				parent.OptionFrame.arBt[1][0]=0;
			FB_swapImage("img1",parent.OptionFrame.arBt[1][1]);
			if (theForm.create_layers.value == "si" && theForm.local.value=="0") {
				theForm.target = "MapFrame";
				theForm.submit();
				theForm.local.value = "1";
			}
			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;
			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":
		
			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.ScaleFrame.document.cheForm.scale.value;
			printform.service_name.value="print";
                        printform.service_action.value="";
			printform.target="PrintFormWindow";
			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.ScaleFrame.document.cheForm.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>";
                                       
			var newWin = new Object();
            newWin = window.open('about:blank','extract','resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,top=50,left=150,width=800,height=600');
            //newWin = window.open('about:blank','extract','');
			theForm.service_name.value="ViewerXML";
			theForm.target="extract";
               newWin.focus();
			theForm.submit();
			
			if(isNav4 || isMoz){window.setTimeout("theForm.target=\"MapFrame\"",50);}	
			else{theForm.target="MapFrame";}
			break		 
	
	}
	modeName=modeBlurb;
	
	// salvo nella form nascosta l'azione della toolbar
	// solo se l'azione non rifa' una submit
	var theForm = parent.PostFrame.document.theForm;
	if (toolName!="select line/polygon" 
		&& toolName!="selectcircle" 
			&& toolName!="measuredist" 
				&& toolName!="measurearea"
					&& toolName!="inseriscinota"
						&& toolName!="cancellanota") {
		isSelect = false;
	}

	// cancella i disegni dalla mappa
	if (lastLayer!=0 && toolName!="print") {
		lastLayer = 0;
		hideLayers();
		theForm.xml_points.value = "";
	}

	if (document.images.length>0 && toolName!="selectcircle")
		document.images["theImage"].alt = "";
		
	if (toolName=="zoomin" 
		|| toolName=="zoomout" 
			|| toolName=="pan" 
				|| toolName=="selectbox" 
					|| toolName=="identify" 
						|| toolName=="selectcircle" 
							|| toolName=="select line/polygon"
								|| toolName=="pluginidentify")
		theForm.toolName.value=toolName;
	
	// ricreazione del search frame e impostazione del pulsante "attiva frame ricerca"
	if (toolName=="zoomin" 
		|| toolName=="zoomout" 
			|| toolName=="pan" 
				|| toolName=="selectbox" 
					|| toolName=="identify") {
		if (selection || measure || insert) {
			selection=false;
			measure=false;
			insert=false;
			parent.PostFrame.document.frame.target="SearchFrame";
			parent.PostFrame.document.frame.service_name.value="Search";
			parent.PostFrame.document.frame.submit();
			if (parent.OptionFrame.arBt[1][0]==0)
				parent.OptionFrame.arBt[1][0]=1;
			FB_swapImage("img1",parent.OptionFrame.arBt[1][3]);
			parent.PostFrame.document.frame.ricerca.value="true";
		}
	}
		
}



