
//Function that translates click on map image to map coordinates.
   function getMapXY(xIn,yIn) {

	var xDistance = Math.abs(left-right);  //difference in x coords (map units)
	var iWidth = larghezza; //width of image
	var eLeft = left; //left coord of map extent (map units)

	var yDistance = Math.abs(bottom-top); //difference in y coords (map units)
	var iHeight = altezza; // height of image
	var eBottom = bottom; ////left coord of map extent (map units)
	
	mouseX = xIn;
	mouseY = iHeight - yIn;
		
	pixelX = xDistance / iWidth;  //map units per pixel in the x direction
		
	mapX = pixelX * mouseX + eLeft; //location of x coord (map units)
		
	pixelY = yDistance / iHeight;  //map units per pixel in the y direction
	mapY = pixelY * mouseY + eBottom;  //location of y coord (map units)
   }

//estrae il nome dell'immagine da caricare
  function calcolapagina(indirizzo) {
  	var ilValore = "";
  	var sottostr = indirizzo;
  	var indice = 0;
  	while(sottostr.indexOf("/") != -1) {
		sottostr = sottostr.substr(sottostr.indexOf("/")+1,sottostr.length);
	}
    ilValore = sottostr;
  	return ilValore
  }

//Questa funzione serve per aggiornare l'immagine della mappa
  function richiama(theURL) {
	  var MSF = getMapScaleFactor();
	  document.applets.miaApplet.aggiornaImm(theURL,MSF); 
  }


//Extract the values of the envelope and insert them in theEnvelope
  function getEnvelopeXYs(theString) {
    var theEnvelope = new Array();
	theString = theString.toUpperCase();
	var tempString = "";
	var startpos;
	var endpos;
	//Get the start of the ENVELOPE element
	var pos = theString.indexOf("ENVELOPE",startpos);
	
	if (pos!=-1) {
		pos = pos + 8;
		//Extract left
		startpos = theString.indexOf("MINX=",pos);
		startpos += 6;
		var endpos = theString.indexOf(dQuote,startpos);
		tempString = theString.substring(startpos,endpos);
		theEnvelope[0] = parseFloat(tempString); 
		//Extract bottom
		startpos = theString.indexOf("MINY=",pos);
		startpos += 6;
		endpos = theString.indexOf(dQuote,startpos);
		tempString = theString.substring(startpos,endpos);
		theEnvelope[1] = parseFloat(tempString); 
		
		//Extract right
		startpos = theString.indexOf("MAXX=",pos);
		startpos += 6;
		endpos = theString.indexOf(dQuote,startpos);
		tempString = theString.substring(startpos,endpos);
		theEnvelope[2] = parseFloat(tempString); 
		
		//Extract top
		startpos = theString.indexOf("MAXY=",pos);
		startpos += 6;
		endpos = theString.indexOf(dQuote,startpos);
		tempString = theString.substring(startpos,endpos);
		theEnvelope[3] = parseFloat(tempString); 
	}
	return theEnvelope;
  }

//Extract the URL of the response image 
function getURL(theString) {
	var theURL = "";
	var startpos = 0;
	var endpos = 0;
	theString = theString.toUpperCase();
	
	//Get the start of the OUTPUT element
	var pos = theString.indexOf("OUTPUT");
	if (pos != -1) {
	
	    //extract the URL
		pos = pos + 6;
		startpos = theString.indexOf("URL=",pos);
		startpos += 5;
		endpos = theString.indexOf(dQuote,startpos);
		theURL = theString.substring(startpos,endpos);
		theURL = theURL.toLowerCase();
		
	}

	return theURL;

}

//Extract the number of features in the response
function justGetFeatureCount(theReply) {

	var theCount = 0;
	var pos = theReply.indexOf("<FEATURECOUNT");
	
	var startpos = theReply.indexOf("count",pos);
	startpos += 7;
	var endpos = theReply.indexOf('"',startpos);
	var countstr = theReply.substring(startpos,endpos);
		
	theCount = parseInt(countstr);	
	
	return theCount;
}


//Estrae informazioni per la scala
function get_scala(theR) {

    var pos = theR.indexOf("murlo_25k.tif"); // <----- Inserire il nome del layer per il calcolo della scala
    var startpos = theR.indexOf("minscale",pos);
	startpos += 10;
	var endpos = theR.indexOf('"',startpos);
	var countstr = theR.substring(startpos,endpos);
	fatt_scala = countstr;
}

// get directory path of URL
function getPath(theFullPath) {
	var theSlash = theFullPath.lastIndexOf("/");
	var theDir = theFullPath.substring(0,theSlash);
	if (theDir==null) theDir="";
	theDir = theDir + "/";
	return theDir;

} 

// estrae tutti gli ID del mapservice e li mette in layerID
function getIDs(theR){
 //alert(theR);
 var pos = "";
 var endpos = "";
 pos = theR.indexOf("<LAYERINFO",pos);
 pos = theR.indexOf("id=",pos);
 var i = 0;
 while (pos != -1) {
  pos += 4;
  endpos = theR.indexOf('"',pos);
  layerID[i] = theR.substring(pos,endpos);
  pos = theR.indexOf("id=",pos);
  i+=1;
 }
 
}  
// estrae tutti i nomi del mapservice e li mette in layerName
function getNames(theR){
 var pos = "";
 var endpos = "";
 pos = theR.indexOf("LAYERINFO",pos);
 pos = theR.indexOf("name=",pos);
 var i = 0;
 while (pos != -1) {
  pos += 6;
  endpos = theR.indexOf('"',pos);
  layerName[i] = theR.substring(pos,endpos);
  pos = theR.indexOf("name=",pos);
  i+=1;
 }
 
} 
 
// estrae tutti i tipi dei layers del mapservice e li mette in layerType
function getTypes(theR){
 var pos = "";
 var endpos = "";
 pos = theR.indexOf("LAYERINFO",pos);
 pos = theR.indexOf("type=",pos);
 var i = 0;
 while (pos != -1) {
  pos += 6;
  endpos = theR.indexOf('"',pos);
  if ((theR.substring(pos,endpos))=="image"){
        layerType[i] = theR.substring(pos,endpos);
  }else{
        pos = theR.indexOf("FCLASS",pos);
		pos = theR.indexOf("type=",pos);
		pos += 6;
		endpos = theR.indexOf('"',pos);
		layerType[i] = theR.substring(pos,endpos);
  }
  pos = theR.indexOf("type=",pos);
  i+=1;
 }
 
}  
 
 
// parse out record data from XML stream
function parseRecordString(theReply, startpos) {
	var inData = "";
	var pos = theReply.indexOf("<FIELDS ",startpos);
	if (pos!=-1) {
		startpos = pos + 8;
		xmlEndPos = theReply.indexOf('" />',startpos);
		inData = theReply.substring(startpos,xmlEndPos);
	}
	return inData;
}

// get a list of field names from the returned record
function getFieldNames(recordString) {
	var theStuff = new String(recordString);				
	var theList = theStuff.split('" ');
	var fName1 = new Array();
	for (var f=0;f<theList.length;f++) {
		var v = theList[f].split('="');
		fName1[f] = v[0];
	} 
	return fName1;
}

// get a list field values from the returned record
function getFieldValues(recordString) {
	var theStuff = new String(recordString);				
	var theList = theStuff.split('" ');
	var fValue1 = new Array();
	for (var f=0;f<theList.length;f++) {
		var v = theList[f].split('="');
		if ((v[1]=="") || (v[1]==null)) v[1] = "&nbsp;";
		fValue1[f] = v[1];
	}
	return fValue1;

}

// just get the field value from the lists of fieldnames and fieldvalues
function getIdValue(fieldNameArray, fieldValueArray) {
	var theValue = 0;
	for (var f=0;f<fieldNameArray.length;f++) {
	    //alert('fieldNameArray[f]'+fieldNameArray[f]);
		
		if (fieldNameArray[f]=="#ID#"){
		    
			theValue = fieldValueArray[f];
		}else{
		    var endString = fieldNameArray[f].length;
		    if(fieldNameArray[f].substring(endString-8,endString)=="OBJECTID"){
		       theValue = fieldValueArray[f];
		    }
		}
	}
	return theValue;
}

// inserisce nell'array il campo #ID# se il layer è uno shape o il campo OBJECTID 
// se il layer è un sde layer
function getFieldID(fieldNameArray) {
	var theValue = "";
	for (var f=0;f<fieldNameArray.length;f++) {
	    if (fieldNameArray[f]=="#ID#"){
		    theValue = "#ID#";
		}else{
		   var endString = fieldNameArray[f].length;
		   if(fieldNameArray[f].substring(endString-8,endString)=="OBJECTID"){
		       theValue = "OBJECTID";
		   }
		}
	}
	return theValue;
}


// check if there is an error message in the response
function getXMLErrorMessage(theString) {
	var pos1 = 0;
	var pos2 = 0;
	var pos3 = 0;
	theError = "";
	pos3 = theString.indexOf("<ERROR");
	if (pos3!=-1) {
		pos1 = theString.indexOf(">",pos3);
		pos1 += 1;
		pos2 = theString.indexOf("</ERROR");
		theError = theString.substring(pos1,pos2)
	}
	return theError;

}

  
   
function cancella_selezione() {
	if (strPoly != "") {
		strPoly = "";
		strSingleZoom = "";
		sendMapXML();
	}


}

function richiamaStampaNewInScala(format){
  //definisco il formato ma non la scala a cui stampare
  switch(format){
    case "A4":
	 larghezzaStampa = 72/2.54*16*1.3;
	 altezzaStampa=72/2.54*16*1.3;
	break;
    case "A3":
      larghezzaStampa=larghezza;
  	  altezzaStampa=altezza;
	break;
    case "A0":
	  larghezzaStampa=larghezza;
      altezzaStampa=altezza;
    break;
  }
   
  var theRequest = writeXML("stampaInScala");
  sendToServer(imsURL,theRequest,6);
}

 //Function that translates x map coordinates to x map image .
function getX2px(xx) {
	var xDistance = Math.abs(left-right);  //difference in x coords (map units)
	var iWidth = larghezza; //width of image
	var eLeft = left; //left coord of map extent (map units)
		
	pixelX = xDistance / iWidth;  //map units per pixel in the x direction
	distx = xx-eLeft;
	pxX = distx/pixelX;
	pxX = parseInt(pxX);
	return pxX;
	}
  
    //Function that translates y map coordinates to y map image .
function getY2px(yy) {
    //alert("yy="+yy);
	var yDistance = Math.abs(bottom-top); //difference in y coords (map units)
	var iHeight = altezza; // height of image
	var eBottom = bottom; ////bottom coord of map extent (map units)
	pixelY = yDistance / iHeight;  //map units per pixel in the y direction	
	disty = yy-eBottom;
	//alert("bottom="+bottom+";top="+top+";disty="+disty);
	pxY = iHeight - disty/pixelY;//location of x coord (map units)
	//alert("pxY="+pxY);
	pxY = parseInt(pxY);
	return pxY;
	}




