// aimsCustom.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*		dependent on aimsXML.js, ArcIMSparam.js, aimsCommon.js, aimsMap.js,
*		aimsLayers.js, aimsDHTML.js
*		aimsClick.js, aimsNavigation.js,
*/

// global variables
	aimsCustomPresent=true;
	// change these to send XML response to custom function.
	// use numbers >= 1000 and match in useCustomFunction()
	// defaults are defined in aimsXML.js and use standard functions
	
	// xml response mode for selection
	selectXMLMode = 6;
	// xml response mode for identify
	identifyXMLMode = 7;
	// xml response mode for query
	queryXMLMode = 8;
	// xml response mode for find
	findXMLMode = 14;
	// xml response mode hyperlink
	hyperlinkXMLMode = 15;
   //PER EDIITNG
	ed_selectXMLMode = 6000;
   //FINE EDITING

	//INIZIO LAYER QUERY: laq
	//Per evidenziare oggetti su un determinato
	//shape appartenente ad un elenco in 
	//laq_csi_GroupIdLayer[]
	laq_csi_useZoomGRPXMLMode = 7000;
	//FINE LAYER QUERY

// custom function for handling clicks 
// flow redirected here when
//	toolMode set to >=1000
function customMapTool(e) {
				if (toolMode == 1001) {
						// insert code here
						return false;
					
				}
				if (toolMode == 1002) {
						// insert code here

				}
				if (toolMode == 1070) {
					//Inizio MultiSelect : MUS
						//alert('MOUSE DOWN case:1070 MultiSelect');
						mus_csi_MakeRequest(e);
					//Fine MultiSelect

				}
			        if (toolMode == 1071) {
					//Inizio Carotaggio : CAR
						//alert('MOUSE DOWN case:1071 Carotaggio');
						car_csi_MakeRequest(e);
					//Fine Carotaggio

				}
				if (toolMode == 1080) {
					extServices(e);
				} 
}

// send  XML response to custom function
//		flow  redirected here when
//		XMLMode >=1000
function useCustomFunction(theReply) {

   //INIZIO DRV
   if (XMLMode==8000){      
   	replyArray[drv_csi_countLayersDrv] = theReply;      
	selectCount=justGetFeatureCount(theReply);      
	if(selectCount>0){      	
		fieldIndex=0;      	
		selectCount=0;      	
		showBuffer=false;         
		ActiveLayer=LayerID[drv_csi_countLayersDrv];         
		ActiveLayerType=LayerType[drv_csi_countLayersDrv];         
		ActiveLayerIndex=drv_csi_countLayersDrv;         
		setLayerFields(drv_csi_countLayersDrv);         
		if (useGeocode) {         	
			if (GCLayerCount>0) {         		
				var j =-1;         		
				for (var k=0;k<GCLayerId.length;k++) {         			
					if (ActiveLayer==GCLayerId[k]) j=k;         		
				}         		
				if (j!=-1) {         			
					GCActiveLayer=j;         		
				}         	
			}         
		}         
		//aggiorna l'envelope massimo...          
		getMaxXYDerivazione(theReply);                   
		showSelectedFeatures = true;	         
		var tmp1 = showSelectedFeatures;          
		var tmp2 = selectBlurb;         
		showSelectedFeatures = false;         
		selectBlurb = "";          
		stringForSelectDerivazione +=addSelectDerivazioneToMap(costrArrayGrPr("GROUP"),false,drv_csi_countLayersDrv,makeXMLsafe(highlightedOne));
		showSelectedFeatures = tmp1;         
		selectBlurb = tmp2;         
		aimsDerivazioneisPresent = true;      
	}
    drv_csi_countLayersDrv = ++drv_csi_countLayersDrv ;      
	setStartQuery();   
	//FINE DRV         
   	} else if (XMLMode==1001) {
		// insert code here
	} else if (XMLMode==1002) {
		// insert code here
		//INIZIO Controllo Punto Contenuto: CPC
		cpc_csi_IntersectionResult(theReply);
    //FINE Controllo Punto Contenuto: CPC		
	} 
	
	//INIZIO URBACOMU
 	  else if (XMLMode==1010) {
		// insert code here				
		//parsifico la risposta e poi inizio a fare la mappa
		parent.TopFrame.parseNomiComuni(theReply);
		//pezzo di codice che prima era in arcImsParam!!!
		startMap();
	} else if (XMLMode==1020) {
		// insert code here		
		//mi faccio dare le 4 coordinate nuove e rifaccio la mappa
		getXYsPerZoomComune(theReply);
		ucm_csi_bUse = true;
		//INIZIO BACO QDW
		parent.MapFrame.qdw_csi_isQuickDrawActive = false;
		//FINE BACO QDW
		sendMapXML();
	//FINE URBACOMU
	}	
	
	//PER EDITING
	  else if (XMLMode==6000) { 
		ed_AttributeOperation(theReply,0);
   } //FINE EDITING
     
     //INIZIO LAYER QUERY: laq 
     else if (XMLMode==7000) { 
		//Verifica della presenza di oggetti con id=x
		//passato via URL e presente in un singolo
		//layer tra uno o più layer indicati nell'array
		//laq_csi_GroupIdLayer[] nello specifico record indicato 
		//via URL dal comando GRP=x
		var laq_csi_countFeature = 0;
		laq_csi_countFeature = justGetFeatureCount(theReply);
		if (laq_csi_countFeature > 0)
			{
			//Sono state trovate delle feature all'interno
			//del layer attivo
			laq_csi_useZoomGRP = false;	
			}	
		if (laq_csi_idGRP == -1)
			{
			//Viene settata a false la variabile laq_csi_useZoomGRP dal momento 
			//che è stato interrogato l'ultimo layer indicato
			//dal record 
			laq_csi_useZoomGRP = false;
			}
			setStartQuery();
   } //FINE LAYER QUERY
     
     else {
		alert(msgList[55] + XMLMode + msgList[56]);
	}
	hideLayer("LoadData");
}

// add custom stuff to Map XML request. . . between selection and geocode
function addCustomToMap1(){
	var customString = "";
	/*
		customString += '<LAYER type="ACETATE" name="theMode">\n';
		customString += '<OBJECT units="PIXEL">\n<TEXT coord="5,' + (iHeight-10) + '" label="This is a test">\n';
		customString += '<TEXTMARKERSYMBOL fontstyle="BOLD" fontsize="12" font="ARIAL" fontcolor="' + modeMapColor + '" ';
		customString += 'threed="TRUE" glowing="' + modeMapGlow + '" />\n</TEXT>\n</OBJECT>';
		customString += '\n</LAYER>\n';
	*/
	return customString;
}

// add custom stuff to Map XML request. . . between clickpoints and copyright
function addCustomToMap2(){
	var customString = "";
	
	return customString;
}

// add custom stuff to Map XML request. . . under modeOnMap
function addCustomToMap3(){
	var customString = "";
		/*
		customString += '<LAYER type="ACETATE" name="theMode">\n';
		customString += '<OBJECT units="PIXEL">\n<TEXT coord="5,' + (iHeight-10) + '" label="This is a test">\n';
		customString += '<TEXTMARKERSYMBOL fontstyle="BOLD" fontsize="12" font="ARIAL" fontcolor="' + modeMapColor + '" ';
		customString += 'threed="TRUE" glowing="' + modeMapGlow + '" />\n</TEXT>\n</OBJECT>';
		customString += '\n</LAYER>\n';
		alert(customString);
		*/
	return customString;
}

// add custom stuff to Map XML request. . . on top of everything
function addCustomToMap4(){
	var customString = "";
	
	return customString;
}

// extract layers to download
function ex_extractIt() {
	hideLayer("measureBox");
	alert(msgList[51]);
}

//Inizio ZoomToScale : ZTS

//var screenWidthInches;   NON SERVE
var ppi; 
var screenWidthPixels = screen.width;

function promptRatio(){
	
	//In zts_csi_screenWidthInches è memorizzata la dimensione in 
	//pollici della larghezza del monitor
	ppi = screenWidthPixels/zts_csi_screenWidthInches;
	//parent.TextFrame.document.location= appDir + "ratioForm.htm";
	//	parent.ToolFrameZoom.document.location= appDir + "ratioForm.htm";

}

function calcNewScale(ratioValue, resolution){
		
	ppi = screenWidthPixels/resolution;
	
	newScaleFactor = ratioValue / (ppi * 39.3701);
	mapScaleFactor = newScaleFactor;
//alert(mapScaleFactor * ppi * 39.3701);
	muWidth = Math.abs(eLeft - eRight);
	muHeight = Math.abs(eBottom - eTop);

	newWidth = (newScaleFactor * iWidth);
	newHeight = (newScaleFactor * iHeight);

	changeWidth = (newWidth - muWidth)/2;
	changeHeight = (newHeight - muHeight)/2;
	
	//Viene richiamata la saveLastExtent() per memorizzare
   //le coordinate utilizzate poi dalla funzione back
   saveLastExtent();

	
	eBottom = eBottom - changeHeight;
	eTop = eTop + changeHeight;
	eLeft = eLeft - changeWidth;
	eRight = eRight + changeWidth;

   
	var theNewString = writeXML();
	sendToServer(imsURL, theNewString, 1);

}
//Fine ZoomToScale : ZTS
