// aimsSelect.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*		dependent on aimsXML.js, ArcIMSparam.js, aimsCommon.js, aimsMap.js, aimsIdentify, and aimsLayers.js
*		To be interactive, dependent also on aimsDHTML.js, aimsClick.js, and aimsNavigation.js
*/

aimsSelectPresent=true;

var selectData = new Array();
var selectLeft = new Array();
var selectRight = new Array();
var selectTop = new Array();
var selectBottom = new Array();
var selMaxEnvelope = new Array();

var selectLayer="";
var selectType = "";
var selectCount = 0;
var selectBlurb = "";
var selectEnvelope="";
//INIZIO Controllo Punto Contenuto: CPC
var cpc_csi_selectPoint="";
var currentTheX;
var currentTheY;
//FINE Controllo Punto Contenuto: CPC
var highlightedOne = "";
	//mode - 0=selection; 1=query
var queryMode=1;
	//mode - 1=query; 2=box,point; 3=line,polygon
var selectionMode=1;
var setQueryString="";

var shapeBufferDistance = 0;


/*
***************************************************************************************

Selection functions

***************************************************************************************
*/

// select feature
function select(e) {

	//getImageXY(e);
	getMapXY(mouseX,mouseY);
	searchTolerance = (xDistance/iWidth) * pixelTolerance;
	var tempWest = mapX - searchTolerance;
	var tempNorth = mapY + searchTolerance;
	var tempEast = mapX + searchTolerance;
	var tempSouth = mapY - searchTolerance;
	queryStartRecord=1;
	selectEnvelope='maxy="' + forceComma(tempNorth) + '" maxx="' + forceComma(tempEast) + '" miny="' + forceComma(tempSouth) + '" minx="' + forceComma(tempWest) + '"';
	selectionMode=2;
	showRetrieveData();
	var theString = writeGetFeatures2(tempWest,tempSouth,tempEast,tempNorth);
	sendToServer(imsQueryURL,theString,selectXMLMode);

}

//INIZIO Controllo Punto Contenuto: CPC
function cpc_csi_select() {

	//getImageXY(e);
	//getMapXY(mouseX,mouseY);
	var tempX = mapX;
	var tempY = mapY;
	queryStartRecord=1;
	cpc_csi_selectPoint='x="' + forceComma(tempX) + '" y="' + forceComma(tempY) + '"';
	//alert('cpc_csi_selectPoint='+cpc_csi_selectPoint);
	selectionMode=2;
	showRetrieveData();
	var theString = cpc_csi_writeGetFeatures2(cpc_csi_selectPoint);
	//alert('function select');
	sendToServer(imsQueryURL,theString,1002);

}
//FINE Controllo Punto Contenuto: CPC



// start select box display
function startSelectBox(e) {
	if (checkIfActiveLayerAvailable()) {
		moveLayer("theMap",hspc,vspc);
		getImageXY(e);
		// keep it within the MapImage
		if ((mouseX<iWidth) && (mouseY<iHeight)) {
			if (selectBox) {
				stopSelectBox(e);
			} else {
				x1=mouseX;
				y1=mouseY
				x2=x1+1;
				y2=y1+1;
				zleft=x1;
				ztop=y1;
				zbottom=y1;
				zright=x1
				boxIt(x1,y1,x2,y2);
				/*
				clipLayer("zoomBoxTop",x1,y1,x2,y2);
				clipLayer("zoomBoxLeft",x1,y1,x2,y2);
				clipLayer("zoomBoxRight",x1,y1,x2,y2);
				clipLayer("zoomBoxBottom",x1,y1,x2,y2);
				*/
				selectBox=true;

				//if (isNav4) {
					showLayer("zoomBoxTop");
					showLayer("zoomBoxLeft");
					showLayer("zoomBoxRight");
					showLayer("zoomBoxBottom");
				//} else {
				//	showLayer("zoomBox");
				//}
			}
			highlightedOne="";
		}
		return false;
	}

}

// stop select box display..... select
function stopSelectBox(e) {
	selectBox=false;
	/*
	var tempLeft=lastLeft;
	var tempRight=lastRight;
	var tempTop=lastTop;
	var tempBottom=lastBottom;
	*/
	//if (isNav4) {
		hideLayer("zoomBoxTop");
		hideLayer("zoomBoxLeft");
		hideLayer("zoomBoxRight");
		hideLayer("zoomBoxBottom");
	//} else {
	//	showLayer("zoomBox");
	//}
	// /*
	if ((zright <zleft+2) && (zbottom < ztop+2)) {

		//getMapXY(mouseX-pixelTolerance,mouseY-pixelTolerance);
		getMapXY(mouseX,mouseY);
		searchTolerance = (xDistance/iWidth) * pixelTolerance;
		tempLeft = mapX-searchTolerance;
		tempTop = mapY-searchTolerance;
		tempRight = mapX+searchTolerance;
		tempBottom = mapY+searchTolerance;

		//select(e);
	} else {
	// */
		pixelX = xDistance / iWidth;
		var theY = iHeight - ztop;
		pixelY = yDistance / iHeight;
		tempTop = pixelY * theY + eBottom;
		tempRight = pixelX * zright + eLeft;
		tempLeft = pixelX * zleft + eLeft;
		theY = iHeight - zbottom;
		pixelY = yDistance / iHeight;
		tempBottom = pixelY * theY + eBottom;
	}
		window.scrollTo(0,0);

	queryStartRecord=1;
	var theString = writeGetFeatures2(tempLeft,tempBottom,tempRight,tempTop);
	selectEnvelope='maxy="' + forceComma(tempTop) + '" maxx="' + forceComma(tempRight) + '" miny="' + forceComma(tempBottom) + '" minx="' + forceComma(tempLeft) + '"';
	drawSelectBoundary=true;
	showBuffer=false;
	selectionMode=2;
	//if (useTextFrame) parent.TextFrame.document.location = "text.htm";
	sendToServer(imsQueryURL,theString,selectXMLMode);

	return true;

}

// write out XML request to select features
function writeGetFeatures2(west1,south1,east1,north1) {
	if (swapSelectFields) {
		selectFields=selFieldList[ActiveLayerIndex];
		//alert(selectFields);
	}
	var theEnvelope = 'maxy="' + forceComma(north1) + '" maxx="' + forceComma(east1) + '" miny="' + forceComma(south1) + '" minx="' + forceComma(west1 )+ '"';
	var theString = writeEnvelopeXML(ActiveLayer,ActiveLayerType,selectFields,maxFeaturesReturned,queryStartRecord,theEnvelope,useLimitExtent);
	//alert(theString);
	selectLayer=ActiveLayer;
	selectType=ActiveLayerType;
	selectCount=0;
	hightlightedOne="";
	selectPoints.length=1;
	selectLeft.length=1;
	selectRight.length=1;
	selectTop.length=1;
	selectBottom.length=1;
	return theString;
}

//INIZIO Controllo Punto Contenuto: CPC
function cpc_csi_writeGetFeatures2(coord_point) {
   if (swapSelectFields) {
		//selectFields=selFieldList[s_idLayerPerEnvelope];
		selectFields="#SHAPE#";
		//alert(selectFields);
	}
	var theString = cpc_csi_writeEnvelopeXML(s_idLayerPerEnvelope,selectFields,maxFeaturesReturned,queryStartRecord,coord_point);
	//alert(theString);
	//selectLayer=ActiveLayer;
	//selectType=ActiveLayerType;
	selectCount=0;
	hightlightedOne="";
	selectPoints.length=1;
	selectLeft.length=1;
	selectRight.length=1;
	selectTop.length=1;
	selectBottom.length=1;
	return theString;
}
//FINE Controllo Punto Contenuto: CPC



function writeGetFeatures3() {
	if (swapSelectFields) {
		selectFields=selFieldList[ActiveLayerIndex];
	}
	var theString = writeEnvelopeXML(ActiveLayer,ActiveLayerType,selectFields,maxFeaturesReturned,queryStartRecord,selectEnvelope,useLimitExtent);
	//alert(theString);
	selectLayer=ActiveLayer;
	selectType=ActiveLayerType;
	selectCount=0;
	hightlightedOne="";
	selectPoints.length=1;
	selectLeft.length=1;
	selectRight.length=1;
	selectTop.length=1;
	selectBottom.length=1;
	return theString;
}

// generic envelope select xml write routine
function writeEnvelopeXML(theLayer,theLayerType,theFields,maxReturned,startRec,theEnvelope,hasLimit) {
	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" envelope="true" geometry="false" checkesc ="true"';
	theString += ' featurelimit="' + maxReturned + '" beginrecord="' + startRec + '">\n';
	theString += '<LAYER id="' + theLayer + '" />';

        //INIZIO Profilazione Utenti: PRU
	//	theString += '<SPATIALQUERY subfields="' + theFields + '"';
	if ( (useProfProgr) && (findElementInArray(costrArrayGrPr("GROUP"),LayerName[ActiveLayerIndex])==true) )
		//E' attiva la profilazione utente,
		//e l'attuale layer attivo è tra quelli su cui attivare la profilazione
		{
		theString += cr_useProfProgr(LayerName[ActiveLayerIndex],costrArrayGrPr("GROUP"),costrArrayGrPr("PROGR"),theFields);
		theString += '>\n';
		}
	else
		{
		//FINE Profilazione Utenti
	theString += '<SPATIALQUERY subfields="' + theFields + '">';
                //INIZIO Profilazione Utenti: pru
		}
                //FINE Profilazione Utenti

	//theString += '<SPATIALFILTER relation=envelope_intersection >';
	theString += '<SPATIALFILTER relation="area_intersection" >';
	theString += '<ENVELOPE ' + theEnvelope + ' />';
	theString += '</SPATIALFILTER>';
	if (hasLimit) {
		// keep this within the limitExtent
		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>';
	theString += '</GET_FEATURES>';
	theString += '</REQUEST>';
	theString += '</ARCXML>';
	return theString;

}


//INIZIO Controllo Punto Contenuto: CPC
// generic envelope select xml write routine
function cpc_csi_writeEnvelopeXML(theLayer,theFields,maxReturned,startRec,theMultiPoint) {

	//alert('xx='+LayerName[16]+' '+LayerName[14]);
	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" envelope="true" geometry="false" checkesc ="true"';
	theString += ' featurelimit="' + maxReturned + '" beginrecord="' + startRec + '">\n';

	theString += '<LAYER id="' + theLayer + '" />';
	theString += '<SPATIALQUERY subfields="' + theFields + '" where="'+s_queryPerEnvelope+'">';
  //theString += '<LAYER id="18" />';
	//theString += '<SPATIALQUERY subfields="#ALL#">';
   theString += '<SPATIALFILTER relation="area_intersection" >';
	theString += '<MULTIPOINT>';
	theString += '<POINT  ' + theMultiPoint + ' />';
	theString += '</MULTIPOINT>';
	if (""!=s_bufferValue && "0"!=s_bufferValue)
{
    //theString += '<BUFFER distance="'+s_bufferValue+'" bufferunits="Meters" />';
	  theString += '<BUFFER distance="'+s_bufferValue+'" bufferunits="'+MapUnits+'" />';
	}
	theString += '</SPATIALFILTER>';
	theString += '</SPATIALQUERY>';
	theString += '</GET_FEATURES>';
	theString += '</REQUEST>';
	theString += '</ARCXML>';
	return theString;

}
//FINE Controllo Punto Contenuto: CPC


// write out xml request for selection by shape
function writeShapeSelect(theType) {
	if (swapSelectFields) {
		selectFields=selFieldList[ActiveLayerIndex];
	}
	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" envelope="true" geometry="false" checkesc ="true"';
	theString += ' featurelimit="' + maxFeaturesReturned + '" beginrecord="' + queryStartRecord + '">\n';
	theString += '<LAYER id="' + ActiveLayer + '" />';
	theString += '<SPATIALQUERY subfields="' + selectFields + '">';
	theString += '<SPATIALFILTER relation="area_intersection" >';
	if ((shapeSelectBuffer) && (shapeBufferDistance>0)) {
	//if (shapeBufferDistance>0) {
		// do a buffer around the shape before selecting

		theString += '<BUFFER distance="' + forceComma(shapeBufferDistance) + '" ';

		theString += ' bufferunits="' + ScaleBarUnits + '"';
		theString += ' />\n';
	}
	if (theType==1) {
		theString += '<POLYLINE>\n<PATH>\n';
	} else if (theType==2) {
		theString += '<POLYGON>\n<RING>\n';
	} else {
		theString += '<MULTIPOINT>\n';
	}
	if (theType==0) {
		theString += '<POINT x="' + clickPointX[clickCount-1] + '" y="' + clickPointY[clickCount-1] + '" />\n';
	} else {
		for (var i=0;i<clickCount;i++) {
			theString += '<POINT x="' + clickPointX[i] + '" y="' + clickPointY[i] + '" />\n';
		}
	}
	if (theType==0) {
		theString += '</MULTIPOINT>\n';
	} else if (theType==2) {
		theString += '</RING>\n</POLYGON>\n';
	} else {
		theString += '</PATH>\n</POLYLINE>\n';
	}
	theString += '</SPATIALFILTER>';
	if (useLimitExtent) {
		// keep this within the limitExtent
		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>';
	theString += '</GET_FEATURES>';
	theString += '</REQUEST>';
	theString += '</ARCXML>';
	//alert(theString);
	selectLayer=ActiveLayer;
	selectType=ActiveLayerType;
	selectCount=0;
	hightlightedOne="";
	selectPoints.length=1;
	selectLeft.length=1;
	selectRight.length=1;
	selectTop.length=1;
	selectBottom.length=1;
	return theString;

}

// request a selection using a shape
function sendShapeSelect(theType) {
	queryStartRecord=1;
	selectionMode=3;
	showBuffer=false;
	if (theType==2) {
		if ((clickPointX[0]!=clickPointX[clickCount-1]) && (clickPointY[0]!=clickPointY[clickCount-1])) {
			clickPointX[clickCount]=clickPointX[0];
			clickPointY[clickCount]=clickPointY[0];
			clickCount=clickCount+1;
		}
	}
	showRetrieveData();
	var theString = writeShapeSelect(theType);
	sendToServer(imsQueryURL,theString,selectXMLMode);
}

// request more records to display
function getMoreData(startRecord) {
	var theString = "";
	queryStartRecord = startRecord;
	if (selectionMode==1) {
		//query
		theString = writeQueryXML(setQueryString);
	} else if (selectionMode==2) {
		theString = writeGetFeatures3();
	} else {
		var theType = clickType-1;
		theString = writeShapeSelect(theType);
	}
	showRetrieveData();
	sendToServer(imsQueryURL,theString,selectXMLMode);
}

// refresh map with hightlighted selection
function showHighlight(selNum) {
		highlightedOne = LayerIDField[ActiveLayerIndex] + " = " + selectPoints[selNum];
		//alert(selectLeft[selNum] + "," + selectRight[selNum]);
		var fWidth = selectRight[selNum] - selectLeft[selNum];
		var fHeight = selectTop[selNum] - selectBottom[selNum];
		var mWMargin = 0;
		var mHMargin = 0;
//alert("selectType = "+selectType);
		if (selectType=="point") {
			mWMargin = fullWidth * selectPointMargin;
			mHMargin = fullHeight * selectPointMargin;
			if (mWMargin > xDistance/2) {
				mWMargin = xDistance/2;
				mHMargin = yDistance/2;
			}
		} else {
			mWMargin = fWidth * selectMargin;
			mHMargin = fHeight * selectMargin;
		}
		saveLastExtent();
		eLeft = selectLeft[selNum] - mWMargin;
		eRight = selectRight[selNum] + mWMargin;
		eTop = selectTop[selNum] + mHMargin;
		eBottom = selectBottom[selNum] - mHMargin;
		//alert(eBottom);
		//alert("showHighlight "+eLeft+" "+eRight+" "+eTop+" "+eBottom);
			legendTemp=legendVisible;
			legendVisible=false;
			sendMapXML();

}

//Inizio Baco zoom to record su buffer
function showHighlightBuffer(selNum) {
//alert("LayerIDField[bufferTargetLayerIndex] = "+LayerIDField[bufferTargetLayerIndex]);
//alert("selectPoints[selNum] = "+selectPoints[selNum]);
//alert("selectPoints.length = "+selectPoints.length);
//alert("selectRight.length = "+selectRight.length);
//alert("selectLeft.length = "+selectLeft.length);
//alert("selectTop.length = "+selectTop.length);
//alert("selectBottom.length = "+selectBottom.length);
		highlightedOne = LayerIDField[bufferTargetLayerIndex] + " = " + selectPoints[selNum];
		//alert(selectLeft[selNum] + "," + selectRight[selNum]);
		var fWidth = selectRight[selNum] - selectLeft[selNum];
		var fHeight = selectTop[selNum] - selectBottom[selNum];
		var mWMargin = 0;
		var mHMargin = 0;
//alert("LayerType[bufferTargetLayerIndex] = "+LayerType[bufferTargetLayerIndex]);
		if (LayerType[bufferTargetLayerIndex]=="point") {
			mWMargin = fullWidth * selectPointMargin;
			mHMargin = fullHeight * selectPointMargin;
			if (mWMargin > xDistance/2) {
				mWMargin = xDistance/2;
				mHMargin = yDistance/2;
			}
		} else {
			mWMargin = fWidth * selectMargin;
			mHMargin = fHeight * selectMargin;
		}
		saveLastExtent();
		eLeft = selectLeft[selNum] - mWMargin;
		eRight = selectRight[selNum] + mWMargin;
		eTop = selectTop[selNum] + mHMargin;
		eBottom = selectBottom[selNum] - mHMargin;
		//alert(eBottom);
		//alert("showHighlight "+eLeft+" "+eRight+" "+eTop+" "+eBottom);
		legendTemp=legendVisible;
		legendVisible=false;
		sendMapXML();

}
//Fine Baco zoom to record su buffer

// calculate max envelope for returned records
function calcSelectEnvelope() {
	//alert("selectCount="+selectCount);
	if (selectCount>0) {
		selMaxEnvelope[0] = parseFloat(selectLeft[0]);
		selMaxEnvelope[1] = parseFloat(selectBottom[0]);
		selMaxEnvelope[2] = parseFloat(selectRight[0]);
		selMaxEnvelope[3] = parseFloat(selectTop[0]);
		//alert("0 - " + selMaxEnvelope[0] + "," + selMaxEnvelope[1] + "," + selMaxEnvelope[2] + "," + selMaxEnvelope[3]);
		if (selectCount>1) {
			for (var i=1;i<selectCount;i++) {
				if (parseFloat(selectLeft[i])<selMaxEnvelope[0]) selMaxEnvelope[0] = parseFloat(selectLeft[i]);
				if (parseFloat(selectBottom[i])<selMaxEnvelope[1]) selMaxEnvelope[1] = parseFloat(selectBottom[i]);
				if (parseFloat(selectRight[i])>selMaxEnvelope[2]) selMaxEnvelope[2] = parseFloat(selectRight[i]);
				if (parseFloat(selectTop[i])>selMaxEnvelope[3]) selMaxEnvelope[3] = parseFloat(selectTop[i]);
				//alert(i + " - " + selMaxEnvelope[0] + "," + selMaxEnvelope[1] + "," + selMaxEnvelope[2] + "," + selMaxEnvelope[3]);
			}
		}
	}
}

function zoomToReturnedRecords() {
	calcSelectEnvelope();
	var fWidth = selMaxEnvelope[2] - selMaxEnvelope[0];
	var fHeight = selMaxEnvelope[3] - selMaxEnvelope[1];
	var mWMargin = 0;
	var mHMargin = 0;
	if ((fWidth==0) && (fHeight==0)) {
		mWMargin = fullWidth * selectPointMargin;
		mHMargin = fullHeight * selectPointMargin;
		if (mWMargin > xDistance/2) {
			mWMargin = xDistance/2;
			mHMargin = yDistance/2;
		}
	} else {
		mWMargin = fWidth * selectMargin;
		mHMargin = fHeight * selectMargin;
	}
	saveLastExtent();
	eLeft = selMaxEnvelope[0] - mWMargin;
	eRight = selMaxEnvelope[2] + mWMargin;
	eTop = selMaxEnvelope[3] + mHMargin;
	eBottom = selMaxEnvelope[1] - mHMargin;
	//legendTemp=legendVisible;
	//legendVisible=false;
	sendMapXML();

}
// clear current selection
function clearSelection() {
//Inizio NQB
        nqb_csi_inuse=false;
	IDLayerCT="";
	CondizioneCT = "";
	QueryLayerAttivo = "";
//<Fine NQB
	var theCount = selectCount;
	var theHL = highlightedOne;
        //Inizio QuickDraw : QDW
        // not all tools obey the clickFunction rule, so code goes too)
        if (qdw_csi_useQuickDrawImage) {
            qdw_csi_isQuickDrawActive = false; // have to set off so main map gets visible layers drawn
            document.QuickDrawImage.src = "images/pixel.gif";
        }
        //Fine QuickDraw : QDW
	selectCount=0;
	showBuffer=false;
	highlightedOne="";
	selectPoints.length=0;
	selectLeft.length=0;
	selectRight.length=0;
	selectTop.length=0;
	selectBottom.length=0;
	drawSelectBoundary=false;
	showGeocode=false;
	clickCount=0;
	totalMeasure=0;
	currentMeasure=0;
	if (useTextFrame) {
		parent.TextFrame.document.open();
		parent.TextFrame.document.writeln('<meta http-equiv="Content-Type" content="text/html; charset=' + charSet + '">');
		parent.TextFrame.document.writeln('<head><link rel="stylesheet" href="'+pagStile+'" type="text/css"></head><html>');
		parent.TextFrame.document.writeln('<body bgcolor="White" link="Blue" vlink="Blue">');
		parent.TextFrame.document.writeln('<center><FONT SIZE="-1"><b>' + LayerName[ActiveLayerIndex] + '</b><br>');
		if (toolMode==20) {
			parent.TextFrame.document.writeln(msgList[70]);
			updateMeasureBox()
			}
		else
                        //INIZIO Profilazione Utenti: pru
			{
			//Profilazione utente in query Builder
			if ( top.MapFrame.useProfProgr != true)
                        //FINE Profilazione Utenti
			parent.TextFrame.document.writeln(msgList[71]);
                        //INIZIO Profilazione Utenti: pru
			}
		        //FINE Profilazione Utenti
		parent.TextFrame.document.writeln('</center></body></html>');
		parent.TextFrame.document.close()
	} else {
		if (toolMode==20) {
			showLayer("measureBox");
			updateMeasureBox()
			alert(msgList[70]);
		} else {
			alert(msgList[71]);
		}
	}


	//if ((theCount>0) || (theHL!="")) {
			//legendTemp=legendVisible;
			//legendVisible=false;
			selectBlurb="";
			//showLayer("LoadMap");
			//var theString = writeXML();
			//sendToServer(imsURL,theString,99);

			//Inizio MultiSelect :  MUS
			//commento la parte vecchia
			//sendMapXML();
			if (toolMode==1070){
				parent.TextFrame.document.location= appDir + "text.htm";
				parent.MapFrame.mus_csi_ClearMultiSelectArray();
				parent.MapFrame.mus_csi_useClearSelection=true;
			}else{
			        sendMapXML();
			}
			//Fine MultiSelect
	//}

	toolMode=1;

}

// add Draw Selected Features to Map XML request
//INIZIO Profilazione Utenti: PRU
//INIZIO DRV
//function addSelectToMap(arrayOfProgr,isGroup){
function addSelectToMap(arrayOfProgr,isGroup,sTheString){
//FINE DRV
  // Come parametri (prima non presenti) della funzione vengono passati
  // l'insieme dei progressivi associati agli oggetti da visualizzare
  // e relativi al layer attivo

  /*if (isGroup) alert("Ci sono dei Progressivi");
  else alert("non ci sono progressivi perchè il layer passati nei gruppi non corrispondono a quello attivo");
  */
  //FINE Profilazione Utenti
  var selString = "";
  var sFactor = (eRight-eLeft) / iWidth;
  if (((sFactor>=LayerMinScale[ActiveLayerIndex]) && (sFactor<=LayerMaxScale[ActiveLayerIndex]) && (LayerVisible[ActiveLayerIndex]==1)) || (canSelectInvisible)) {
    if ((selectCount>0) && (showSelectedFeatures)) {
	selString +='<LAYER type="featureclass" name="Oggetti selezionati" visible="true">\n';
      //selString +='<DATASET fromlayer="' + ActiveLayer + '" />\n';
      selString +='<DATASET fromlayer="' + LayerID[ActiveLayerIndex] + '" />\n';
      if (selectionMode==1) {
      	    if (nqb_csi_inuse){
                            	selString +='<SPATIALQUERY  where="' + QueryLayerAttivo;
                            	setQueryString=QueryLayerAttivo;
                            	selString +='"/>\n';
            }else{
        //INIZIO Profilazione Utenti: PRU
		        //alert("setQueryString "+setQueryString);
		        selString +='<SPATIALQUERY  where="' + setQueryString;
		        if (useLimitExtent) {
		          // keep this within the limitExtent
		          selString +='">\n<SPATIALFILTER relation="area_intersection">\n';
		          selString +='<ENVELOPE maxx="' + forceComma(limitRight) + '" maxy="' + forceComma(limitTop) + '" minx="' + forceComma(limitLeft) + '" miny="' + forceComma(limitBottom) + '" />\n';
		          selString +='</SPATIALFILTER>\n';
		          selString +='</SPATIALQUERY>\n';
		        } else {
		          selString +='"/>\n';
		        }
		        //FINE Profilazione Utenti: PRU
		        //Inizio NQB
	    }
			   //fine NQB
      } else {
        //INIZIO Profilazione Utenti: PRU
        selString +='<SPATIALQUERY ';
        //if ( (isGroup) && (useProfProgr) && (inizio==true))
        if ((isGroup) && (useProfProgr)) {//Vengono aggiunti in OR i progressivi passati per URL
          selString += ' where="' + campoQuery + '=' + arrayOfProgr[0];
          for (var indexProgr=1; indexProgr<arrayOfProgr.length; indexProgr++) {
            selString += ' OR ' + campoQuery + '=' + arrayOfProgr[indexProgr];
          }
          selString +='">\n';
        }	else {
          selString +='>\n';
        }
        //FINE Profilazione Utenti
        selString +='<SPATIALFILTER relation="area_intersection" >\n';
        if (selectionMode==2) {
          selString +='<ENVELOPE ' + selectEnvelope + ' />\n';
        } else {
          if ((shapeSelectBuffer) && (shapeBufferDistance>0)) {
            //if (shapeBufferDistance>0) {
            // do a buffer around the shape before selecting
            selString += '<BUFFER distance="' + forceComma(shapeBufferDistance) + '" ';
            selString += ' bufferunits="' + ScaleBarUnits + '"';
            selString += ' />\n';
          }
          if (clickType==2) {
            selString +='<POLYLINE>\n<PATH>\n';
          } else if (clickType==3) {
            selString +='<POLYGON>\n<RING>\n';
          } else {
            selString +='<MULTIPOINT>\n';
          }
          if (clickType==1) {
            selString +='<POINT x="' + clickPointX[clickCount-1] + '" y="' + clickPointY[clickCount-1] + '" />\n';
          } else {
            for (var i=0;i<clickCount;i++) {
            	selString +='<POINT x="' + clickPointX[i] + '" y="' + clickPointY[i] + '" />\n';
            }
          }
          if (clickType==3) {
            //selString +='<POINT x="' + clickPointX[0] + '" y="' + clickPointY[0] + '" />\n';
            selString +='</RING>\n</POLYGON>\n';
          } else if (clickType==2) {
            selString +='</PATH>\n</POLYLINE>\n';
          } else {
            selString +='</MULTIPOINT>\n';
          }
        }
        selString +='</SPATIALFILTER>\n';
        ///*
        if (useLimitExtent) {
          // keep this within the limitExtent
          selString +='<SPATIALFILTER relation="area_intersection">\n';
          selString +='<ENVELOPE maxx="' + forceComma(limitRight) + '" maxy="' + forceComma(limitTop) + '" minx="' + forceComma(limitLeft) + '" miny="' + forceComma(limitBottom) + '" />\n';
          selString +='</SPATIALFILTER>\n';
        }
        //*/
        selString +='</SPATIALQUERY>\n';
      }
      selString +='<SIMPLERENDERER>\n';

      if (selectType=="point") {
      //Inizio MultiSelect : MUS
      if (mus_csi_useMultiSelect) {
        if (mus_csi_multiSelectArraySelectPoint.length>0 && toolMode==1070){
          selString +='<SIMPLEMARKERSYMBOL color="' + selectColor + '" type="Circle" width="10" />\n';
        }else{
          selString +='<SIMPLEMARKERSYMBOL color="' + selectColor + '" type="Circle" width="10" />\n';
        }
      } else {
        selString +='<SIMPLEMARKERSYMBOL color="' + selectColor + '" type="Circle" width="10" />\n';
      }
      //Fine MultiSelect
    } else if (selectType=="line") {
      //Inizio MultiSelect :  MUS
      if (mus_csi_useMultiSelect) {
        if (mus_csi_multiSelectArraySelectPoint.length>0 && toolMode==1070){
          selString +='<SIMPLELINESYMBOL type="SOLID" color="' + selectColor + '" width="3" />\n';
        }else{
          selString +='<SIMPLELINESYMBOL type="SOLID" color="' + highlightColor + '" width="3" />\n';
        }
      } else {
        selString +='<SIMPLELINESYMBOL type="SOLID" color="' + selectColor + '" width="3" />\n';
      }
      //Fine MultiSelect
    } else {
      //selString +='<SIMPLEPOLYGONSYMBOL FILLCOLOR="' + selectColor + '" FILLTYPE="Gray" ';
      //Inizio MultiSelect : MUS
      if (mus_csi_useMultiSelect) {
        if (mus_csi_multiSelectArraySelectPoint.length>0 && toolMode==1070){
          selString +='<SIMPLEPOLYGONSYMBOL fillcolor="' + selectColor + '" filltype="solid" transparency="1,0" boundarycolor="255,255,255" />\n';
        }else{
          selString +='<SIMPLEPOLYGONSYMBOL fillcolor="' + highlightColor + '" filltype="solid" transparency="' + forceComma(transparentLevel) + '" boundarycolor="255,255,255" />\n';
        }
      } else {
        selString +='<SIMPLEPOLYGONSYMBOL fillcolor="' + selectColor + '" filltype="solid" transparency="' + forceComma(transparentLevel) + '" boundarycolor="255,255,255" />\n';
      }
      //Fine MultiSelect
    }
    selString +='</SIMPLERENDERER>\n';
    selString +='</LAYER>\n';
  }


  if (selectBlurb!="") {
    // add SelectBlurb to Map XML request
    selString +='<LAYER type="featureclass" name="Oggetti Selezionati" visible="true">\n';
    //selString +='<DATASET fromlayer="' + ActiveLayer + '" />\n';
    selString +='<DATASET fromlayer="' + LayerID[ActiveLayerIndex] + '" />\n';
    selString +='<QUERY where="' + selectBlurb + '" />\n';
    selString +='<SIMPLERENDERER>\n';

    if (selectType=="point") {
      selString +='<SIMPLEMARKERSYMBOL color="' + selectColor + '" type="Circle" width="10" />\n';
    } else if (selectType=="line") {
      selString +='<SIMPLELINESYMBOL type="SOLID" color="' + selectColor + '" width="3" />\n';
    } else {
      selString +='<SIMPLEPOLYGONSYMBOL fillcolor="' + selectColor + '" filltype="solid" transparency="' + forceComma(transparentLevel) + '" boundarycolor="255,255,255" />\n';
    }
    selString +='</SIMPLERENDERER>\n';
    selString +='</LAYER>\n';

  }

	//INIZIO DRV
	//if (highlightedOne!="") {
	var sStringCompare = '<LAYER type="featureclass" name="Oggetti evidenziati" visible="true">\n';
	sStringCompare +='<DATASET fromlayer="' + LayerID[ActiveLayerIndex] + '" />\n';
	sStringCompare = escape(sStringCompare);
	if ((highlightedOne!="") && (escape(sTheString).indexOf(sStringCompare)==-1)){
	//FINE DRV

//  if (highlightedOne!="") {
 //Inizio Stradario : STR
    //MESSO PERCHE' ALTRIMENTI NON SA CHE TIPO DI GEOM è QUELLA DEL LAYER ATTIVO
    selectType=ActiveLayerType;
		var selStringTemp ="";

		// add Draw Highlighed Feature to Map XML request
		selStringTemp +='<LAYER type="featureclass" name="Oggetti evidenziati" visible="true">\n';
		//selString +='<DATASET fromlayer="' + ActiveLayer + '" />\n';
		selStringTemp +='<DATASET fromlayer="' + LayerID[ActiveLayerIndex] + '" />\n';
		selStringTemp +='<QUERY where="' + highlightedOne + '" />\n';
		selStringTemp +='<SIMPLERENDERER>\n';
		if (selectType=="point") {
			selStringTemp +='<SIMPLEMARKERSYMBOL color="' + highlightColor + '" type="Circle" width="10" />\n';
		} else if (selectType=="line") {
			if (str_csi_bPercorsoSel && str_csi_oggDaSelezionare==""){
			  selStringTemp +='<SIMPLELINESYMBOL type="SOLID" color="' + str_csi_stradPercTematSpesso[str_csi_tematStra] + '" width="5" />\n';
			} else {
			  selStringTemp +='<SIMPLELINESYMBOL type="SOLID" color="' + highlightColor + '" width="3" />\n';
			}
      //selString +='<SIMPLELINESYMBOL type="SOLID" color="' + highlightColor + '" width="3" />\n';
		} else {
			//selString +='<SIMPLEPOLYGONSYMBOL FILLCOLOR="' + highlightColor + '" FILLTYPE="Gray" ';
			//selString +='<SIMPLEPOLYGONSYMBOL fillcolor="' + highlightColor + '" filltype="solid" transparency="' + forceComma(transparentLevel) + '" boundarycolor="255,255,255" />\n';
			if (str_csi_bPercorsoSel && str_csi_oggDaSelezionare==""){
			  selStringTemp +='<SIMPLEPOLYGONSYMBOL boundarytransparency="1,0" filltransparency="0,0"  boundarywidth="3" boundarycaptype="round" boundarycolor="'+str_csi_stradPercTematSpesso[str_csi_tematStra]+'" />\n';
			} else {
			  selStringTemp +='<SIMPLEPOLYGONSYMBOL fillcolor="' + highlightColor + '" filltype="solid" transparency="' + forceComma(transparentLevel) + '" boundarycolor="255,255,255" />\n';
			}

	  }
		selStringTemp +='</SIMPLERENDERER>\n';
		selStringTemp +='</LAYER>\n';
		//alert(selString);


		if (str_csi_bPercorsoSel && str_csi_oggDaSelezionare==""){
		  str_csi_oggDaSelezionare+=selStringTemp;
		  str_csi_SettaLayerStradario();
		  highlightedOne="";
		} else {
		  selString+=selStringTemp;
		}
//Fine Stradario : STR

  }
 }
 return selString;

}



//INIZIO Controllo Punto Contenuto: CPC
function cpc_csi_IntersectionResult(theReply){
   var has_featureCount = false;
   var theError = getXMLErrorMessage(theReply);
   var featureCount = justGetFeatureCount(theReply);

   if(featureCount>0){
      has_featureCount=true;
      alert ("ci sono feature");
      }
   else{
      has_featureCount=false;
       alert ("non ci sono feature");
        }
   if(has_featureCount){
    getMapXY(currentTheX,currentTheY);
    clickPointX[clickCount]=mapX;
   	clickPointY[clickCount]=mapY;
   	clickCount += 1;
   	selectCount=0;
   	totalMeasure = totalMeasure + currentMeasure;

   	clickMeasure[clickCount]=totalMeasure;
   	legendTemp=legendVisible;
   	legendVisible=false;
   	var theString = writeXML();

   	var theNum = 99;
   	//PER EDITING
   	showRetrieveMap();
   	//FINE EDITING
   	//alert('function clickAddPoint');
   	sendToServer(imsURL,theString,theNum);
   }
   else{
      alert("Attenzione Il punto inserito è fuori dall\'area di Editing abilitata!");
   }
}

//FINE Controllo Punto Contenuto: CPC
