

// Send a GET_SERVICE_INFO request
function sendServiceXML(){
  startMapLoading();
  var theRequest = writeXML("Service");
  sendToServer(imsURL,theRequest,1);
}


// Send a request of map
function sendMapXML(){
  startMapLoading();
  var theRequest = writeXML("Map");
   //pass this function a URL to the map service, the request and a XML mode of 2
  sendToServer(imsURL,theRequest,2);
  
}


//Carica nell'applet un'immagine di attesa
function startMapLoading() {
  showRetrieveData();
}


//elimina l'immagine di attesa
function stopMapLoading() {
  hideRetrieveData();
}
  
 
function startDataLoading() {
  parent.textFrame.location = path +"dataWait.htm";
}

function writeXML(XMLType){
    var theProlog = '<?xml version="1.0" encoding="UTF-8" ?>\n';
    var theXML = theProlog;
    if (XMLType == "Service") {
  
      //Construct a GET_SERVICE_INFO request and put it in a variable called theXML
      theXML = theXML + '<ARCXML version="1.1">';
	  theXML = theXML + '<REQUEST>';
	  theXML = theXML + '<GET_SERVICE_INFO fields="false" envelope="true" renderer="false" extensions="false" />';
	  theXML = theXML + '</REQUEST>';
	  theXML = theXML + '</ARCXML>';

    }
  
  else if (XMLType == "Map") {
    
	//construct a GET_IMAGE request and put it in a variable called theXML 
    theXML = theXML + '<ARCXML version="1.1">';
	theXML = theXML + '<REQUEST>';
	theXML = theXML + '<GET_IMAGE>';
	theXML = theXML + '<PROPERTIES>';
	theXML = theXML + '<ENVELOPE minx="' + left + '" miny="' + bottom + '" maxx="' + right + '" maxy="' + top +'" />'; 
	theXML = theXML + '<IMAGESIZE width="'+larghezza+'" height="'+altezza+'"  />';
	if (strLyrVisibile!="") theXML += strLyrVisibile;
	theXML = theXML + '</PROPERTIES>';
	if (strPoly != "") theXML += strPoly;
	if (strSingleZoom != "") theXML += strSingleZoom;
	if (strStradario != "") theXML += strStradario;
	if (strPointMultipla != "") theXML += strPointMultipla;
	theXML = theXML + '</GET_IMAGE>';
	theXML = theXML + '</REQUEST>';
	theXML = theXML + '</ARCXML>';
	
	//alert(theXML);
  }
  
   else if (XMLType == "Polygon") {
  
     //construct a GET_FEAURES request and put it in a variable called theXML 
    theXML = theXML + '<ARCXML version="1.1">';
	theXML = theXML + '<REQUEST>';
	theXML = theXML + '<GET_FEATURES outputmode="xml" envelope="true" geometry="false" featurelimit="'+featureLimit+'" beginrecord="'+beginRecord+'" >';
	theXML = theXML + '<LAYER id="' + ID + '" />';
	theXML = theXML + '<SPATIALQUERY subfields="' + subfields + '" where="" jointables="" >'; 
	theXML = theXML + '<SPATIALFILTER relation="area_intersection" >';
	theXML = theXML + '<POLYGON>';
	theXML = theXML + '<RING>';
	
	for (var i=0;i<nPunti;i++)
			{     
							
				theXML = theXML + '<POINT x="' + clickPointX[i] + '" y="' + clickPointY[i] + '" />' ;
				
              }
    theXML = theXML + '<POINT x="' + clickPointX[0] + '" y="' + clickPointY[0] + '" />' ;
	
	theXML = theXML + '</RING>';
	theXML = theXML + '</POLYGON>';
	theXML = theXML + '</SPATIALFILTER>';
	theXML = theXML + '</SPATIALQUERY>';
	theXML = theXML + '</GET_FEATURES>';
	theXML = theXML + '</REQUEST>';
	theXML = theXML + '</ARCXML>';
	
	//beginRecord = 1;
	//alert(theXML);
	
	
} else if (XMLType == "stampaInScala") {
    //construct a GET_IMAGE request and put it in a variable called theXML 
	var centrox = (left+right)/2;
	var centroy = (bottom+top)/2; 
	var hightTemp = altezzaStampa/2*getMapScaleFactor();
	var widthTemp = larghezzaStampa/2*getMapScaleFactor();
	var leftSt = centrox-widthTemp;
	var rightSt = centrox+widthTemp;
	var topSt = centroy+widthTemp;
	var bottomSt = centroy-widthTemp;
	theXML = theXML + '<ARCXML version="1.1">';
	theXML = theXML + '<REQUEST>';
	theXML = theXML + '<GET_IMAGE>';
	theXML = theXML + '<PROPERTIES>';
	theXML = theXML + '<ENVELOPE minx="' + leftSt + '" miny="' + bottomSt + '" maxx="' + rightSt + '" maxy="' + topSt +'" />'; 
	theXML = theXML + '<IMAGESIZE width="'+larghezzaStampa+'" height="'+altezzaStampa+'"/>';
	if (strLyrVisibile!="") theXML += strLyrVisibile;
	theXML = theXML + '</PROPERTIES>';
	if (strPoly != "") theXML += strPoly;
	if (strSingleZoom != "") theXML += strSingleZoom;
	if (strPointMultipla != "") theXML += strPointMultipla;
	theXML = theXML + '</GET_IMAGE>';
	theXML = theXML + '</REQUEST>';
	theXML = theXML + '</ARCXML>';
	//alert(theXML);
	
  } 
    else if (XMLType == "Overview") {
  
    //construct a GET_IMAGE request and put it in a variable called theXML 
    theXML = theXML + '<ARCXML version="1.1">';
	theXML = theXML + '<REQUEST>';
	theXML = theXML + '<GET_IMAGE>';
	theXML = theXML + '<PROPERTIES>';
	theXML = theXML + '<ENVELOPE minx="' + ovStartLeft + '" miny="' + ovStartBottom + '" maxx="' + ovStartRight + '" maxy="' + ovStartTop +'" />'; 
	theXML = theXML + '<IMAGESIZE width="'+ovLarghezza+'" height="'+ovAltezza+'"  />';
	//theXML = theXML + '<BACKGROUND color="250,250,210" /> ';
    theXML = theXML + '</PROPERTIES>';
	theXML = theXML + '</GET_IMAGE>';
	theXML = theXML + '</REQUEST>';
	theXML = theXML + '</ARCXML>';
	//alert(theXML);
	
  }
  
  return theXML;	
}

function sendToServer(URLString,theRequest,theType) {
    //alert(theRequest);
	//var holdTheReply=theRequest;
    //var debugWin=window.open("","","height=600,width=600,resizable=yes");
    //debugWin.document.write('request <br> <textarea  name="n" style="width=500;height=500"> + holdTheReply + '</textarea>');
	
	//set the XML Mode and send request to server
	XMLMode = theType;

	var thePostForm = parent.PostFrame.document.forms[0];
	thePostForm.action=URLString + "&Form=True";
	thePostForm.ArcXMLRequest.value=theRequest;
	thePostForm.JavaScriptFunction.value = "parent.MapFrame.processXML";
    
	thePostForm.submit();
	
}


function processXML(theReply){
    //alert(theReply);
	//var holdTheReply=theReply;
    //var debugWin=window.open("","","height=600,width=600,resizable=yes");
    //debugWin.document.write('reply <br> <textarea  name="n" style="width=500;height=500">' + holdTheReply + '</textarea>');
	
	var theError = getXMLErrorMessage(theReply);
	if (theError !="") {
		alert('ERROR!   '+theError);
		//parent.MapFrame.clearSelection();
		//delSelection();
		//controlla_fattore();
		//richiama("blank.gif");
	}else{
      switch(XMLMode) {

		//response is from a GET_SERVICE_INFO request
		case 1:
		  // inserisce nell'array layerID tutti gli ID del file axl
		  getIDs(theReply);
		  
		  // inserisce nell'array layerName tutti i nomi dei layers del file axl
		  getNames(theReply);
		  
		  // inserisce nell'array layerType tutti i tipi dei layers del file axl(point,polygon..)
		  getTypes(theReply);
		  
		  // setta i layer visibili sulla mappa, sul toc
		  setUpLayers();
		  
		  //Estrae informazioni per la scala
		  get_scala(theReply);
		  
		  //extract the initial envelope		
		  var theEnvelope = getEnvelopeXYs(theReply);
		  
		  //Get the envelope values from the array that is returned by the
		  // getEnvelopeXYs() function and put them in global variables. 
		  left = theEnvelope[0];
		  bottom = theEnvelope[1];
		  right = theEnvelope[2];
		  top = theEnvelope[3];

		  //store initial extent(to change the initial extent insert here the coords)
		  startLeft = theEnvelope[0];
		  startBottom = theEnvelope[1];
		  startRight = theEnvelope[2];
		  startTop = theEnvelope[3];
		  
		  saveLastExtent();
		  //create a map by calling the sendMapXML() function.
		  //sendMapXML();
		  if (progettisti == true){
			         //parent.TitleFrame.responsabili();
					 progettisti=false;
		  }	
		  controlla_fattore();
		break
		
		//response is from a GET_IMAGE request
	    case 2:
		  if(useZoomAllaScala) {
		    //risetto lo zoom_in dopo la scelta della scala
               parent.toolFrame.setToolPic('Zoom In');
               useZoomAllaScala=false;
		  }
		  var theURL = "";
		  
		    //extract the image URL
			theURL = getURL(theReply);
			immagineStampa = theURL;
			//estrazione dello sviluppo perch pu essere cambiato
					
		   var theEnvelope = getEnvelopeXYs(theReply);
		  
		  //Get the envelope values from the array that is returned by the
		  // getEnvelopeXYs() function and put them in global variables.  
		   left = theEnvelope[0];
		   bottom = theEnvelope[1];
		   right = theEnvelope[2];
		   top = theEnvelope[3];
		  
		  // variabili per l'estensione dell'overview
 		   ovLeft = left;
		   ovBottom = bottom;
	       ovRight = right;
		   ovTop = top;
		  
		  // saveLastExtent();
		  
		  //Passa l'immagine restituita dal server all'applet
		   //richiama(calcolapagina(theURL));
		   
		    if(toolMode == 4){
		   		parent.MapFrame.document.theImage.src = theURL;
				moveLayer("theMap",0,0);
				panning = false;
		   }else{
		   		//alert("theURL"+theURL);
				//alert("parent.MapFrame.document.theImage"+parent.MapFrame.document.theImage);
		   		parent.MapFrame.document.theImage.src = theURL;
		   }
			
		  //aggiorno la scala
		   fattore_attuale();
			  
			//Se  la prima volta disegno anche l'overview
			   
			   if (disegnaov) {
			    
			    //risetto lo zoom_in dopo la scelta della scala
                 parent.toolFrame.setToolPic('Zoom In');
                 //parent.toolFrame.chiamaRet(3);
				  // aggiorna i frames
			    //parent.selectFrame.location = path + "select.htm";
			    //parent.tocFrame.location = path + "toc.htm";
				
			    disegnaov = false;
				ovStartLeft = startLeft;
		        ovStartBottom = startBottom;
	            ovStartRight = startRight;
		        ovStartTop = startTop;
				ovAltezza = locHeight ;
				ovLarghezza = locWidth;
				var strOV = writeXML("Overview");
				sendToServer(ovURL,strOV,100)
				
			   } else {
			  
			  cambia1(left,bottom,right,top);
			
			  }
			  if(strNumeroStrada!="") {
		        var km = strNumeroStrada;
				km = km.replace(",",".");
				//alert("http://"+hostName+"/IntersezioneLayers/trovaKM.jsp?layer="+layerID[activeLayerIndex]+"&OBJID="+objIdStrada+"&m="+km);
				
				parent.JSPFrame.location = "http://"+hostName+"/IntersezioneLayers/trovaKM.jsp?layer="+layerID[activeLayerIndex]+"&OBJID="+objIdStrada+"&m="+km; 
			}
			
			  stopMapLoading();
	    break
		
		case 3:
		    //Visualizzazione dei risultati
		    var featureCount = justGetFeatureCount(theReply);
		    //alert(theReply);
			if(featureCount > 0) {
				//memorizzazione delle estensioni degli elementi trovati
				memEnvelope(theReply,featureCount);
				displayData(theReply,featureCount);
				
			}
			if (featureCount == 0)  {
				delSelection();
				controlla_fattore();
				var strSelz ="";
				if (usePoint) strSelz= "nel punto selezionato.";
				else strSelz="nell'area selezionata.";
				alert("Nessun elemento del tematismo '"+layerName[activeLayerIndex]+"'\n  presente "+strSelz);
				
			}
			//richiesta della nuova mappa 
		    if (layerHighlighted[activeLayerIndex] == true)  {
			      startMapLoading();
				  controlla_fattore();
		    }		  
						   
		break
		
		case 5:
		   
		    addMultipla(theReply);
		
		break
		case 6:
		    var theURL = "";
		    //Get the envelope values from the array that is returned by the
		    //getEnvelopeXYs() function and put them in global variables.  
		    /*var theEnvelope = getEnvelopeXYs(theReply);
		    left = theEnvelope[0];
		    bottom = theEnvelope[1];
		    right = theEnvelope[2];
		    top = theEnvelope[3];
			//aggiorno la scala
		    scalaStampa=printScala(getMapScaleFactorPrint());
			alert(scalaStampa);*/
		    theURL = getURL(theReply);
			immagineStampa = theURL;
		    window.open(path+"stampa.htm","PrintPage","");
	     break;

		case 7:
		    //strade
		    getTOPO_ROUTE(theReply);
		break
		case 8:
		
		    objIdStrada = getOBJID(theReply);
			objIdStradaUrb="";
			memEnvelope(theReply,1);
			
			visualizzaStrada(theReply);
			
		break
		
		case 9:
			
		    var featureCount = justGetFeatureCount(theReply);
			
			if(featureCount>0) {
					
				parent.JSPFrame.location = "http://"+hostName+"/IntersezioneLayers/connetti.jsp?layer="+layerID[activeLayerIndex]+"&OBJID="+objIdStrada+"&x="+mapX+"&y="+mapY;
				debugWindow("parent.JSPFrame.location " +parent.JSPFrame.location);
				}
		    else {
			 alert("Punto troppo distante dalla strada");//strade
			 inserisciMetrica(false,"",0,0)
			}
			stopMapLoading();
		   
		break   
		
		case 10:
		
		    objIdStradaUrb = getOBJID(theReply);
			objIdStrada="";
			//alert(theReply);
			//memEnvelope(theReply,1);
			memEnvelopeCivico(theReply,1);
			visualizzaStradaUrb(theReply);
			
			
		break
		case 11:
		    //Visualizzazione dei risultati
		    var featureCount = justGetFeatureCount(theReply);
		    //alert(theReply);
			//alert(theReply);
			if(featureCount > 0) {
				//memorizzazione delle estensioni degli elementi trovati
				recuperaCivico(theReply);
				//memEnvelope(theReply,1);
				memEnvelopeCivico(theReply,1);
				visualizzaCivico(theReply);
				stopMapLoading();
			}
			if (featureCount == 0)  {
				delSelection();
				//controlla_fattore();
				stopMapLoading();
				alert("Non è stato trovato alcun civico !");
			}
			stopMapLoading();
			   
		break
		
		case 12:
				endpos = 0;
				featureCount = justGetFeatureCount(theReply);
				if (featureCount > 0) 
				  for (var i=0; i<featureCount; i++) {
					inData = parseRecordString(theReply, endpos);
					endpos = xmlEndPos;
					values = getFieldValues(inData);
					if (values[0].substring(0,1) == "[")
					  TipsValues[TipsValues.length] = values[1];
					else
					  TipsValues[TipsValues.length] = values[0];
				  }  
				  fillTipsCoords(theReply);
		break
		case 100:
		    var  ind_ov = "";
			ind_ov = getURL(theReply);
			//parent.ovFrame.document.images[0].src = ind_ov;
			parent.MapFrame.document.ovImage.src = ind_ov;
		
		break
		
		}// end switch
	}//end if	
	
}



