/* autore rosa
script con le funzioni per lo stradario
script con le funzioni per caricare la mappa
origine: e-utile */


var t;
if (opener) {
	if (opener.name=="MapFrame") {
		t = opener.parent.MapFrame;
	} else {
		t = opener;
	}
} else {
	if (parent.MapFrame) {
		t = parent.MapFrame;
	} else {
		t = document;
	}
}

	
//-----------------------------------------
// apre mappa per civico
//-----------------------------------------
function doMappa() {
	var theForm = document.forms[0];
	var theExt  = theForm.stCiv.value;
	//alert (theExt);
	if (theExt != "") {
		//Estrae i parametri: via, numero civico
		var theExtArray = theExt.split(",");
		var via, civ;
		via   = theExtArray[0];
		civ   = theExtArray[1];
		if (civ != "0000") {
		//Impostazioni per visualizzare il civico selezionato
		   t.setQueryCentra = "sit.CIVICI2000_tot.codvia=" + via + " and sit.CIVICI2000_tot.numero='" + civ + "'";
		   t.zl=t.idLcivico;
		   t.canSelectInvisible = true;
		} else {
		   //alert (via);
		   t.setQueryCentra = "sit.VIE2000.cod_via=" + via;
		   t.zl=t.idLvia;
		   t.canSelectInvisible = true;
		}    
	    var theString1 = t.writeQueryXMLEu(t.setQueryCentra,t.zl);
	    t.sendToServer(t.imsQueryURL,theString1,1030);
	}
}

//-----------------------------------------
// apre mappa per foglio
//-----------------------------------------
function doMappale() {
    var theForm = document.forms[0];
    var foglio  = theForm.stFoglio.value;
    //alert (codvia);
	parent.ricerche.location.href="catasto.asp?stFoglio=" + foglio;
}
//-----------------------------------------
// apre mappa per mappale
//-----------------------------------------
function doMappaCa() {
	var theForm = document.forms[0];
	var theExt  = theForm.stMap.value;
	if (theExt != "") {
		//Estrae i parametri: via, xmin, ymin, xmax, ymax
		var theExtArray = theExt.split(",");
		var map, fog
		fog   = theExtArray[0];
		map   = theExtArray[1];
	    t.setQueryCentra = "sit.Particelle_CAT.mappale='" + map + "' and sit.Particelle_CAT.foglio='" + fog + "'";
		t.zl=t.idLcata;
		t.canSelectInvisible = true;
	    var theString1 = t.writeQueryXMLEu(t.setQueryCentra,t.zl);
	    //alert (theString1);
	    t.sendToServer(t.imsQueryURL,theString1,1030);
	}
}

//-----------------------------------------
// apre mappa per cinema, museo, scuola, uff comunale, farmacia
//-----------------------------------------

function doRicerca() {
    var theForm = document.forms[0];
    var theExt  = theForm.stRicerca.value;
    if (theExt != "") {
        var theExtArray = theExt.split("|");
		var liv, val
		liv   = theExtArray[0];
		val   = t.fixSingleQuotes(theExtArray[1]); 
		val = val.replace("&", "&amp;");
		switch(liv.toLowerCase()) { 
		    case "cinema": 
			t.setQueryCentra = "sit.CINEMA2000.nome_cinem='" + val + "'";
			t.zl=t.idLcinema;
			t.canSelectInvisible = true;
			break;
			case "farmacie":
			t.setQueryCentra = "sit.FARMACIE2000.nome_farma='" + val + "'";
			t.zl=t.idLfarma;
			t.canSelectInvisible = true;
			break;
			case "musei":
			t.setQueryCentra = "sit.MUSEI2000.nome_musei='" + val + "'";
			t.zl=t.idLmusei;
			t.canSelectInvisible = true;
			break;
			case "scuole":
			t.setQueryCentra = "sit.SCUOLE2000.nome_scuol='" + val + "'";
			t.zl=t.idLscuole;
			t.canSelectInvisible = true;
			break;
			case "uffici comunali":
			t.setQueryCentra = "sit.UFFCOM2000.nome_comun='" + val + "'";
			t.zl=t.idLuff;
			t.canSelectInvisible = true;
			break;
			}
	t.toolMode = 1031     
    var theString1 = t.writeQueryXMLEu(t.setQueryCentra,t.zl);
	t.sendToServer(t.imsQueryURL,theString1,1031);
	}
}

//-------------------------------------------------------------------------------------
// formattazione risposta di ArcIMS per attributi (quando si vuole un livello definito)
//-------------------------------------------------------------------------------------
function writeQueryXMLEu(queryString,zl) {
    //alert (zl);
	if (swapSelectFields) {
		selectFields=selFieldList[zl];
	}
	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" geometry="false" envelope="true" checkesc ="true"';
	theString += ' featurelimit="' + maxFeaturesReturned + '" beginrecord="' + queryStartRecord + '">\n';
	theString += '<LAYER id="' + zl + '" />';
	if (useLimitExtent) {
		// keep this within the limitExtent
		theString += '<SPATIALQUERY subfields="' + selectFields + '" where="' + queryString + '">';
		theString += '<SPATIALFILTER relation="area_intersection">\n';
		theString += '<ENVELOPE maxx="' + forceComma(limitRight) + '" maxy="' + forceComma(limitTop) + '" minx="' + forceComma(limitLeft) + '" miny="' + forceComma(limitBottom) + '" />\n';
		theString += '</SPATIALFILTER>\n';
		theString += '</SPATIALQUERY>\n';
	} else {
	//theString += '<QUERY subfields="' + selectFields + '" where="' + queryString + '" />';
	theString += '<SPATIALQUERY subfields="' + selectFields + '" where="' + queryString + '" />';
	}
	theString += '</GET_FEATURES>';
	theString += '</REQUEST>';
	theString += '</ARCXML>';
	//alert("writeQueryXML()\nQuery XML Request:\n" + theString);
	selectLayer=LayerIdentifyAll[zl];
	selectType=LayerType[zl];
	selectCount=0;
	hightlightedOne="";
	selectPoints.length=1;
	selectLeft.length=1;
	selectRight.length=1;
	selectTop.length=1;
	selectBottom.length=1;
	//alertRosa
	//alert (theString);
	return theString;
}


//-----------------------------------------
// centra 
//-----------------------------------------
function centraEut(theReply,XMLMode) {
    var xc, yc, x1, x2, y1, y2, dx, dy
	//alert("inizio centra" + theReply);
	var featureCount = justGetFeatureCount(theReply);
	var inData = theReply;
    var inDataString
    var epos;
	if (featureCount > 0) {
	//alert(selectType);
	//alert(inData);
	//alert(t.zl);
	  for (var i=0;i<featureCount;i++){
	      
        var theExtArray =  getEnvelopeXYs(inData,1);
        if (i == 0) {
		   x1 = parseFloat(theExtArray[0]);
		   y1 = parseFloat(theExtArray[1]);
		   x2 = parseFloat(theExtArray[2]);
		   y2 = parseFloat(theExtArray[3]);
		}    
		
		if (parseFloat(theExtArray[0])<x1) { x1 = parseFloat(theExtArray[0])};
		if (parseFloat(theExtArray[1])<y1) { y1 = parseFloat(theExtArray[1])};
		if (parseFloat(theExtArray[2])>x2) { x2 = parseFloat(theExtArray[2])};
		if (parseFloat(theExtArray[3])>y2) { y2 = parseFloat(theExtArray[3])};
		//taglia la stringa
	    epos = inData.indexOf("</FEATURE>",1) + 10;
	    inDataString = inData.substr(epos);
	    inData = inDataString;
		
	  }
	  //Aggiusta l'extent in funzione delle dimensioni dell'immagine   
	  if (selectType=="point") {
			   scala = 500;
			   var dx = 50/2;
			   var dy = 50/2;
			   x1 = x1 - dx;
			   x2 = x2 + dx;
			   y1 = y1 - dy;
			   y2 = y2 + dy;
	   } else {
       
				dx = (x2 - x1) / t.iWidth;
				dy = (y2 - y1) / t.iHeight;
				if (dx > dy) {
					yc = (y1 + y2) / 2;
					y1 = yc - (t.iHeight*dx)/2;
					y2 = yc + (t.iHeight*dx)/2;
				} else {
					xc = (x1 + x2) / 2;
					x1 = xc - (t.iWidth*dy)/2;
					x2 = xc + (t.iWidth*dy)/2;
				}
	   }	
    
    t.saveLastExtent();
	t.eLeft   = x1;
	t.eRight  = x2;
	t.eTop    = y1;
	t.eBottom = y2;	
	//alert (t.eLeft);
	//alert (t.eRight);
	//alert (t.eTop);
	//alert (t.eBottom);
	
	t.setActiveLayer(t.zl);
	t.setQueryString = t.setQueryCentra 
	t.selectionMode = 1;
	t.selectCount = 1;
    t.sendMapXML();
    
   
    } else	{
		alert ("Oggetto non trovato");
	} 
}

//-----------------------------------------
// apre tabella degli attributi 
//-----------------------------------------
function EsendQueryString(newString) {
	//if (LayerIDField[ActiveLayerIndex]!="#ID#") {
		//newString = fixSingleQuotes(newString);
		newString = swapQuotes(newString);
	//}
	newString = makeXMLsafe(newString);
	setQueryString=newString;
	selectionMode=1;
	selectData.length=0;
	LayerFields.length=0;
	LayerFieldType.length=0;
	LayerFieldCount=0;
	highlightedOne="";
	showBuffer=false;
	showRetrieveData();
	var theString = writeQueryXML(newString);
	//alert (theString);
	//alert (queryXMLMode);
	sendToServer(imsQueryURL,theString,queryXMLMode);
}














