// aimsQuery.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*		dependent on aimsXML.js, ArcIMSparam.js, aimsCommon.js, aimsMap.js, aimsIdentify.js, aimsSelect.js, and aimsLayers.js
*		To be interactive, dependent also on aimsDHTML.js, aimsClick.js, and aimsNavigation.js
*/

aimsQueryPresent=true;

var showSampleValues=false;

var storedQueryCount = 0;
var storedQueryName = new Array();
var storedQueryString = new Array();
var storedQueryVariable = new Array();
var storedQueryVarCount = new Array();
var storedQueryFieldList = new Array();
var storedQueryIndex = 0;

/* ******************
   Querying functions 
   ****************** */

//Attiva layer per query form
function setActiveLayer1(i) {
	//alert(ActiveLayerIndex + " - " + i);
	if(ActiveLayerIndex!=i) {
		ActiveLayer=LayerID[i];
		ActiveLayerType=LayerType[i];
		ActiveLayerIndex=i;
		selectLayer=ActiveLayer;
		selectType=ActiveLayerType;
		setActiveLayer(i);
		var isOk = checkHyperLinkLayer(i);
		if (toolMode==15) {
			if (!isOk) {
				currentHyperLinkLayer="";
				currentHyperLinkField="";
				currentHyperLinkPrefix="";
				currentHyperLinkSuffix="";
				alert(msgList[47]);
			}
		}
		//Aggiorna la legenda per visualizzare il nuovo layer attivo per le interrogazioni
		//con eccezione del tema relativo al quadro di unione
		//if ((hasTOC) && (!legendVisible) && (i!=IDQuadroUnione)) parent.TOCFrame.document.location = appDir + "toc.htm";
		if ((hasTOC) && (!legendVisible)) parent.TOCFrame.document.location = appDir + "toc.htm";
	}
}

// query form
function queryForm() {
	//if (checkIfActiveLayerAvailable()) {

		fieldIndex=0;
		showSampleValues=false;
		
		setLayerFields(ActiveLayerIndex);
		if (showSampleValues) {
			var theText = writeFieldSample(LayerFields[fieldIndex]);
			sendToServer(imsQueryURL,theText,40);
		} else {
			writeQueryForm();
		}
		
	//}
}

// query form per impianti
function queryForm1() {
	//if (checkIfActiveLayerAvailable()) {

		setActiveLayer1(IDImpiantiPunti);

		fieldIndex=0;
		showSampleValues=false;
		
		setLayerFields(ActiveLayerIndex);
		if (showSampleValues) {
			var theText = writeFieldSample(LayerFields[fieldIndex]);
			sendToServer(imsQueryURL,theText,40);
		} else {
			writeQueryForm1();
		}
	//}
}

// query form per linee
function queryForm2() {
	//if (checkIfActiveLayerAvailable()) {

		setActiveLayer1(IDLinee);

		fieldIndex=0;
		showSampleValues=false;
		
		setLayerFields(ActiveLayerIndex);
		if (showSampleValues) {
			var theText = writeFieldSample(LayerFields[fieldIndex]);
			sendToServer(imsQueryURL,theText,40);
		} else {
			writeQueryForm2();
		}
	//}
}

// Form per filtro sulla visualizzazione
function filterForm() {

	fieldIndex        = 0;
	showSampleValues  = false;
	selectData.length = 0;

	//Commentare le linee seguenti per non usare il tema degli impianti per 
	//trovare tutti i valori del campo "PROPRIETAR"
	setActiveLayer1(IDImpiantiPunti);
	setLayerFields(ActiveLayerIndex);
	for(var i=0; i<LayerFields.length; i++) {
		if(LayerFields[i]=='PROPRIETAR') {
			fieldIndex=i;
			showSampleValues=true;
		}
	}

	//Check se caricare i valori del campo selezionato o visualizzare il form		
	if (showSampleValues) {
		var theText = writeFieldSample(LayerFields[fieldIndex]);
		showRetrieveData();
		sendToServer(imsQueryURL,theText,41);
	} else {
		writeFilterForm();
	}
}

// processa la query in input sul layer corrente
function sendQueryString(newString) {
	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);
	sendToServer(imsQueryURL,theString,queryXMLMode);
}

// process filter
function sendFilterString(newString1,newString2,newString3,newString4) {
	newString1 = fixSingleQuotes(newString1);
	newString1 = swapQuotes(newString1);
	newString1 = makeXMLsafe(newString1);
	newString2 = fixSingleQuotes(newString2);
	newString2 = swapQuotes(newString2);
	newString2 = makeXMLsafe(newString2);
	newString3 = fixSingleQuotes(newString3);
	newString3 = swapQuotes(newString3);
	newString3 = makeXMLsafe(newString3);
	newString4 = fixSingleQuotes(newString4);
	newString4 = swapQuotes(newString4);
	newString4 = makeXMLsafe(newString4);
	setFilterString1 = newString1;
	setFilterString2 = newString2;
	setFilterString3 = newString3;
	setFilterString4 = newString4;
	selectCount=0;
	setQueryString=""
	selectionMode=1;
	selectData.length=0;
	LayerFields.length=0;
	LayerFieldType.length=0;
	LayerFieldCount=0;
	highlightedOne="";
	showBuffer=false;
	sendMapXML();
}

// write out XML request to query attributes
function writeQueryXML(queryString) {
	if (swapSelectFields) {
		selectFields=selFieldList[ActiveLayerIndex];
	}
	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" geometry="false" envelope="true"';
	theString += ' featurelimit="' + maxFeaturesReturned + '" beginrecord="' + queryStartRecord + '">\n';
	theString += '<LAYER id="' + ActiveLayer + '" />';
	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=ActiveLayer;
	selectType=ActiveLayerType;
	selectCount=0;
	hightlightedOne="";
	selectPoints.length=1;
	selectLeft.length=1;
	selectRight.length=1;
	selectTop.length=1;
	selectBottom.length=1;
	return theString;
}

// write out a query form
function writeQueryForm() {

	var startpos = 0;
	var endpos = 0;
	var SampleCount = selectData.length;
	var theIndex = fieldIndex;
	var sampleList = new Array();
	var qField = LayerFields[fieldIndex] + '="';
	var valueTitle = buttonList[2];
	var tempString = "";
	
	if (showSampleValues) {
		for (var i=0;i<SampleCount;i++) {
			startpos = selectData[i].indexOf(qField,0);
			if(startpos>=0) {
				startpos = startpos + qField.length;
				endpos = selectData[i].indexOf('"',startpos);
			}
			if (LayerFieldType[theIndex].indexOf("12",0)!=-1) {
				// a Character field
				if(startpos>=0) {
					var vData = makeXMLsafe(selectData[i].substring(startpos,endpos));
				} else {
					var vData = makeXMLsafe(selectData[i]);
				}
				//vData = fixSingleQuotes(vData);
				sampleList[i] = dQuote + vData + dQuote;
			} else if (LayerFieldType[theIndex]=="91") {
				// a Date field
				var theDateObj = new Date(parseFloat(selectData[i].substring(startpos,endpos)));
				var d = theDateObj.toUTCString();
				sampleList[i] = d.replace(/GMT|UTC/,"");
				theDateObj = null;
			} else {
				if(startpos>=0) {
					sampleList[i] = selectData[i].substring(startpos,endpos);
				} else {
					sampleList[i] = selectData[i];
				}
			}
		}
		valueTitle = buttonList[3];
	}
	
	if (useTextFrame) {
		var Win1 = parent.TextFrame;
		Win1.document.open();
		var t = "parent.MapFrame";
	} else {
		var Win1 = open("","Query","width=700,height=130,scrollbars=yes,resizable=yes");
		var t = "opener";
		if (parent.MapFrame!=null) t = "opener.parent.MapFrame";
	}
	
	Win1.document.writeln('<html><meta http-equiv="Content-Type" content="text/html; charset=' + charSet + '"><HEAD><link rel="stylesheet" type="text/css" href="' + appDir + 'css/style1.css">');
	Win1.document.writeln('<title>Interroga</title>');
	Win1.document.writeln('<script language="javascript">');
		
	Win1.document.writeln('			var t = ' + t );
	Win1.document.writeln('			var dQuote = \'"\';');
	Win1.document.writeln('			var lastExpr = "";');
	Win1.document.writeln('			var currExpr = "";');
	Win1.document.writeln('			var sampleList = new Array();');
	Win1.document.writeln('			var qField = "";');
	
	Win1.document.writeln('			function addString() {');
	Win1.document.writeln('				lastExpr = document.QueryStuff.QueryString.value;');
	Win1.document.writeln('				qField = document.QueryStuff.QueryField.options[document.QueryStuff.QueryField.selectedIndex].value;');
	Win1.document.writeln('				var qOperator = document.QueryStuff.QueryOperator.options[document.QueryStuff.QueryOperator.selectedIndex].value;');
	Win1.document.writeln('					var fNum = document.QueryStuff.QueryField.selectedIndex;');
	if (showSampleValues) {
		Win1.document.writeln('				qString = t.parseEntity(document.QueryStuff.QueryValue.options[document.QueryStuff.QueryValue.selectedIndex].value);');
	} else{
		Win1.document.writeln('					var qString = document.QueryStuff.QueryValue.value;');
		Win1.document.writeln('					if (t.LayerFieldType[fNum].indexOf("12",0)!=-1) {');
		Win1.document.writeln('						if (qString.indexOf(dQuote)==-1) {');
		Win1.document.writeln('							qString = dQuote + qString + dQuote;');
		Win1.document.writeln('						}');
		Win1.document.writeln('					}');
	}
	Win1.document.writeln('					if (t.LayerFieldType[fNum].indexOf("91",0)!=-1) {');
	Win1.document.writeln('						qString = t.formatDate(qString);');
	Win1.document.writeln('					}');
	Win1.document.write('					if ((t.LayerFieldType[fNum].indexOf("4",0)!=-1) || ');
	Win1.document.write('					(t.LayerFieldType[fNum].indexOf("8",0)!=-1) || ');
	Win1.document.write('					(t.LayerFieldType[fNum].indexOf("6",0)!=-1) || ');
	Win1.document.write('					(t.LayerFieldType[fNum].indexOf("5",0)!=-1)) {');
	Win1.document.writeln('						qString = t.convertDecimal(qString);');
	Win1.document.writeln('					}');
	Win1.document.writeln('				var qString2 = document.QueryStuff.QueryString.value;');
	Win1.document.writeln('				var theString =  qField + " " + qOperator + " " + qString;');
	Win1.document.writeln('				document.QueryStuff.QueryString.value = qString2 + theString;');
	Win1.document.writeln('				currExpr = document.QueryStuff.QueryString.value;');
	Win1.document.writeln('			}');
			
	Win1.document.writeln(' 		function sendQuery() {'); 
	Win1.document.writeln(' 			var theString = document.QueryStuff.QueryString.value;'); 
	Win1.document.writeln(' 			if (theString!="") {'); 
	Win1.document.writeln(' 				var ary = new Array("<",">","="," LIKE "," like "," IS "," is ");'); 
	Win1.document.writeln(' 				var va = 0;'); 
	Win1.document.writeln(' 				var arylen = ary.length;'); 
	Win1.document.writeln(' 				for (i=0;i<arylen;i++){'); 
	Win1.document.writeln(' 					a = ary[i]'); 
	Win1.document.writeln(' 					s = theString.search(a)'); 
	Win1.document.writeln(' 					va = va + s '); 
	Win1.document.writeln(' 				}'); 
	Win1.document.writeln(' 				if (va == -7){'); 
	Win1.document.writeln(' 					alert("' + msgList[109] + '");'); 
	Win1.document.writeln(' 				} else {'); 
	Win1.document.writeln(' 					sampleList=null;'); 
	Win1.document.writeln(' 					t.sendQueryString(theString);'); 
	Win1.document.writeln(' 				}'); 
	Win1.document.writeln(' 			} else {'); 
	Win1.document.writeln(' 				alert("' + msgList[110] + '");'); 
	Win1.document.writeln(' 			}'); 
	Win1.document.writeln(' 		}'); 
		
	Win1.document.writeln('			function addAnd() {');
	Win1.document.writeln('				lastExpr = document.QueryStuff.QueryString.value;');
	Win1.document.writeln('				var qString2 = document.QueryStuff.QueryString.value;');
	Win1.document.writeln('				document.QueryStuff.QueryString.value = qString2 + " AND ";');
	Win1.document.writeln('				currExpr = document.QueryStuff.QueryString.value;');
	Win1.document.writeln('			}');
			
	Win1.document.writeln('			function addOr() {');
	Win1.document.writeln('				lastExpr = document.QueryStuff.QueryString.value;');
	Win1.document.writeln('				var qString2 = document.QueryStuff.QueryString.value;');
	Win1.document.writeln('				document.QueryStuff.QueryString.value = qString2 + " OR ";');
	Win1.document.writeln('				currExpr = document.QueryStuff.QueryString.value;');
	Win1.document.writeln('			}');
	
	Win1.document.writeln('			function addNot() {');
	Win1.document.writeln('				lastExpr = document.QueryStuff.QueryString.value;');
	Win1.document.writeln('				var qString2 = document.QueryStuff.QueryString.value;');
	Win1.document.writeln('				document.QueryStuff.QueryString.value = qString2 + "NOT ";');
	Win1.document.writeln('				currExpr = document.QueryStuff.QueryString.value;');
	Win1.document.writeln('			}');
			
	Win1.document.writeln('			function addLeftPara() {');
	Win1.document.writeln('				lastExpr = document.QueryStuff.QueryString.value;');
	Win1.document.writeln('				var qString2 = document.QueryStuff.QueryString.value;');
	Win1.document.writeln('				document.QueryStuff.QueryString.value = qString2 + "(";');
	Win1.document.writeln('				currExpr = document.QueryStuff.QueryString.value;');
	Win1.document.writeln('			}');
	
	Win1.document.writeln('			function addRightPara() {');
	Win1.document.writeln('				lastExpr = document.QueryStuff.QueryString.value;');
	Win1.document.writeln('				var qString2 = document.QueryStuff.QueryString.value;');
	Win1.document.writeln('				document.QueryStuff.QueryString.value = qString2 + ")";');
	Win1.document.writeln('				currExpr = document.QueryStuff.QueryString.value;');
	Win1.document.writeln('			}');
	
	Win1.document.writeln('			function undoString() {');
	Win1.document.writeln('				currExpr = document.QueryStuff.QueryString.value;');
	Win1.document.writeln('				document.QueryStuff.QueryString.value = lastExpr;');
	Win1.document.writeln('				t.setQueryString = document.QueryStuff.QueryString.value;');
	Win1.document.writeln('				lastExpr = currExpr;');
	Win1.document.writeln('			}');
			
	Win1.document.writeln('			function clearString() {');
	Win1.document.writeln('				lastExpr = document.QueryStuff.QueryString.value;');
	Win1.document.writeln('				document.QueryStuff.QueryString.value = "";	');
	Win1.document.writeln('				t.setQueryString = "";');
	Win1.document.writeln('			}');
			
	Win1.document.writeln('			function setUp() {');
	Win1.document.writeln('				if (t.showSampleValues) {');
	Win1.document.writeln('					t.fieldIndex = document.QueryStuff.QueryField.selectedIndex;');
	Win1.document.writeln('					t.setQueryString = document.QueryStuff.QueryString.value;');
	Win1.document.writeln('					var theField = t.LayerFields[t.fieldIndex];');
	Win1.document.writeln('					//alert(theField);');
	Win1.document.writeln('					t.tempGetSamples(theField);');
	Win1.document.writeln('					t.writeQueryForm();');
	Win1.document.writeln('				}');
	Win1.document.writeln('			}');
		
	Win1.document.writeln('			function changeToSamples() {');	
	Win1.document.writeln('				t.fieldIndex = document.QueryStuff.QueryField.selectedIndex;');
	Win1.document.writeln('				t.setQueryString = document.QueryStuff.QueryString.value;');
	Win1.document.writeln('				t.tempGetSamples(t.LayerFields[t.fieldIndex]);');
	Win1.document.writeln('			}');
					
	Win1.document.writeln('		</script>');
	Win1.document.writeln('		</head>');
	
	Win1.document.writeln('<body link="Blue" vlink="Blue" alink="Blue" leftmargin=0 topmargin=0 bgcolor="#dfdfdf" onload="window.focus(); ' + t + '.queryOpen=true;" onunload="' + t + '.queryOpen=false;">');
	Win1.document.writeln('<center>');
	Win1.document.writeln('<form name="QueryStuff" onsubmit="sendQuery();return false;">');
	Win1.document.writeln('<table class=toc border="0" cellspacing="0" cellpadding="5" width=100%>');
	Win1.document.writeln('<tr><th align="center" height=25 BGCOLOR=#FF0000>Interroga</th></tr>');
	Win1.document.writeln('<tr><td align="center" height=5></td></tr>');
	Win1.document.writeln('</table>');
	Win1.document.writeln('<table class=toc border="0" cellspacing="0" cellpadding="2" width=100%>');
	Win1.document.writeln('<tr><td align="CENTER">' + msgList[72] + '</td>');
	Win1.document.writeln('	   <td align="CENTER">' + msgList[73] + '</td>');
	Win1.document.writeln('	   <td align="CENTER">' + valueTitle + '</td>');
	Win1.document.writeln('	   <td rowspan="2" align="CENTER" bgcolor="Silver">');
	Win1.document.writeln('			<input type="button" name="theAnd" value=" And " onclick="addAnd()">');
	Win1.document.writeln('			<input type="button" name="theOr" value=" Or " onclick="addOr()"><br>');
	Win1.document.writeln('			<input type="button" name="theNot" value=" Not " onclick="addNot()">');
	Win1.document.writeln('			<input type="button" name="theLeft" value="  (  " onclick="addLeftPara()">');
	Win1.document.writeln('			<input type="button" name="theRight" value="  )  " onclick="addRightPara()">');
	Win1.document.writeln('	   </td>');
	Win1.document.writeln('</tr>');
	Win1.document.writeln('<tr><td align="CENTER"><select name="QueryField" onchange="setUp();">');
	for (var i=0;i<LayerFieldCount;i++) {
		Win1.document.write('<option value="' + LayerFields[i] + '"');
		if (i==fieldIndex) Win1.document.write(' selected');
		// Put Field Alias in form if present
        if (useFieldAlias) {
			if (fieldAliasList[ActiveLayerIndex].length>0) {
	             var start=fieldAliasList[ActiveLayerIndex].indexOf(LayerFields[i]+':');
	             if (start == -1) {
		 			alias=LayerFields[i];
	             } else {
	                  start=start+LayerFields[i].length+1;
	                  var end=fieldAliasList[ActiveLayerIndex].indexOf("|",start);
	                  if (end == -1) end = fieldAliasList[ActiveLayerIndex].length;
	                  else end=end-start;
	                  alias=fieldAliasList[ActiveLayerIndex].substr(start,end);
	             }
	             Win1.document.writeln('>'+alias);
			}
        } else {
			Win1.document.writeln('>' + LayerFields[i]);
		}
		// end of Field Alias stuff
	}
	Win1.document.writeln('	   </select></td>');
	Win1.document.writeln('	   <td align="CENTER"><select name="QueryOperator">');
	Win1.document.writeln('			<OPTION selected value=" = ">=');
	Win1.document.writeln('			<option value=" < ">&lt;');
	Win1.document.writeln('			<option value=" > ">&gt;');
	Win1.document.writeln('			<option value=" <= ">&lt;=');
	Win1.document.writeln('			<option value=" >= ">&gt;=');
	Win1.document.writeln('			<option value=" LIKE ">LIKE');
	Win1.document.writeln('			</select></td>');
	Win1.document.writeln('	   <td align="CENTER">');
	if (showSampleValues) {
		Win1.document.writeln('		<select name="QueryValue">');
		for (var i=0;i<SampleCount;i++) {
			//Win1.document.writeln('<option value=\'' + sampleList[i] + '\'>' + unescape(sampleList[i]));
			tempString = parseEntity(sampleList[i]);
			tempString = tempString.replace(/ /g, "&nbsp;");
			Win1.document.writeln('<option value=\'' + sampleList[i] + '\'>' + tempString);
		}
		Win1.document.writeln('			</select>');
	} else {
		Win1.document.writeln('<input name="QueryValue" size="25" maxlength="1000" >');
		Win1.document.writeln('			&nbsp;&nbsp;<input type="button" name="makeList" value="' + buttonList[4] + '" onclick="changeToSamples()">');
		Win1.document.writeln('			</td>');
	}         
	Win1.document.writeln('	   </td>');
	Win1.document.writeln('</tr>');
	Win1.document.writeln('<tr><td colspan="4" align="center">');
	Win1.document.writeln('			<input type="button" name="addIt" value="' + buttonList[5] + '" onclick="addString()">');
	Win1.document.writeln('			<input type="Text" name="QueryString" size="50" maxlength="1024" value=\'' + setQueryString + '\'><br>');
	Win1.document.writeln('</td></tr>');
	Win1.document.writeln('<tr><td colspan="4" align="center">');
	Win1.document.writeln('			<input type="submit" value="' + buttonList[6] + '" name="submit">'); 
	//Win1.document.writeln('			<input type="button" name="UnDo" value="' + buttonList[7] + '" onclick="undoString()">');
	Win1.document.writeln('			<input type="button" name="Clear" value="' + buttonList[8] + '" onclick="clearString()">');
	Win1.document.writeln('</td></tr>');
	Win1.document.writeln('</table>');
	Win1.document.writeln('</center></FORM></body></html>');
	
	Win1.document.close();
	Win1=null;
	sampleList=null;
}

// temporarily change getSampleValues to true and load queryform
function tempGetSamples(theField) {
	showSampleValues=true;
	//if (useTextFrame) {
	//	var Win1 = parent.TextFrame;
	//} else {
	//	var Win1 = open("","QueryWindow","width=575,height=150,scrollbars=yes,resizable=yes");
	//}
	//Win1.document.open();
	//Win1.document.writeln('<html><meta http-equiv="Content-Type" content="text/html; charset=' + charSet + '"><HEAD>');
	//Win1.document.writeln('<body BGCOLOR="' + textFrameBackColor + '" TEXT="Black" size="-1">');
	//Win1.document.writeln('<div align="center"><font face="Arial"><b>');
	//Win1.document.writeln(msgList[74] + LayerName[ActiveLayerIndex] + '. . .</b></font><br>');
	//Win1.document.writeln('<font face="Arial" size="-1">' + msgList[75] + numberDataSamples + msgList[76] + '</font></div></body></html>');
	//Win1.document.close();
	//Win1=null;
	var theText = writeFieldSample(theField);
	sendToServer(imsQueryURL,theText,40);
}

// Form per query solo su impianti
function writeQueryForm1() {

	var startpos = 0;
	var endpos = 0;
	var SampleCount = selectData.length;
	var theIndex = fieldIndex;
	var sampleList = new Array();
	var qField = LayerFields[fieldIndex] + '="';
	var valueTitle = buttonList[2];
	var tempString = "";

	if (useRicFrame) {
		var Win1 = parent.RicFrame;
		Win1.document.open();
		var t = "parent.MapFrame";
	} else {
		var Win1 = open("","Ricerca","width=300,height=250,scrollbars=yes,resizable=yes");
		var t = "opener";
		if (parent.MapFrame!=null) t = "opener.parent.MapFrame";
	}
	
	Win1.document.writeln('<html><meta http-equiv="Content-Type" content="text/html; charset=' + charSet + '"><HEAD><link rel="stylesheet" type="text/css" href="' + appDir + 'css/style1.css">');
	Win1.document.writeln('<title>Ricerca per impianti</title>');
	Win1.document.writeln('<script language="javascript">');

	Win1.document.writeln('    var t = ' + t);
	Win1.document.writeln('    var dQuote = \'"\';');
	Win1.document.writeln('    var lastExpr = "";');
	Win1.document.writeln('    var currExpr = "";');

	Win1.document.writeln('    function addString() {');
	Win1.document.writeln('        lastExpr       = document.QueryStuff.QueryString.value;');
	Win1.document.writeln('        var qField     = document.QueryStuff.QueryField.options[document.QueryStuff.QueryField.selectedIndex].value;');
	Win1.document.writeln('        var qId        = document.QueryStuff.QueryId.value;');
	Win1.document.writeln('        var qNome      = document.QueryStuff.QueryNome.value;');
	Win1.document.writeln('        var qProvincia = document.QueryStuff.QueryProvincia.value;');
	Win1.document.writeln('        var qComune    = document.QueryStuff.QueryComune.value;');
	Win1.document.writeln('        var theString = "";');
	Win1.document.writeln('        if (qField != "") {');
	Win1.document.writeln('            theString = theString + "TEMA_IMP=" + dQuote + qField + dQuote;');
	Win1.document.writeln('        }');
	Win1.document.writeln('        if (qId != "") {');
	Win1.document.writeln('            if (theString != "") {');
	Win1.document.writeln('                theString = theString + " AND ";');
	Win1.document.writeln('            }');
	Win1.document.writeln('            qId        = dQuote + "%" + qId.toUpperCase() + "%" + dQuote;');
	Win1.document.writeln('            theString  = theString + "ID_UNIVOCO LIKE " + qId;');
	Win1.document.writeln('        }');
	Win1.document.writeln('        if (qNome != "") {');
	Win1.document.writeln('            if (theString != "") {');
	Win1.document.writeln('                theString = theString + " AND ";');
	Win1.document.writeln('            }');
	Win1.document.writeln('            qNome     = dQuote + "%" + qNome.toUpperCase() + "%" + dQuote;');
	Win1.document.writeln('            theString = theString + "NOME LIKE " + qNome;');
	Win1.document.writeln('        }');
	Win1.document.writeln('        if (qProvincia != "") {');
	Win1.document.writeln('            if (theString != "") {');
	Win1.document.writeln('                theString = theString + " AND ";');
	Win1.document.writeln('            }');
	Win1.document.writeln('            qProvincia = dQuote + qProvincia.toUpperCase() + dQuote;');
	Win1.document.writeln('            theString  = theString + "PROVINCIA=" + qProvincia;');
	Win1.document.writeln('        }');
	Win1.document.writeln('        if (qComune != "") {');
	Win1.document.writeln('            if (theString != "") {');
	Win1.document.writeln('                theString = theString + " AND ";');
	Win1.document.writeln('            }');
	Win1.document.writeln('            qComune   = dQuote + "%" + qComune.toUpperCase() + "%" + dQuote;');
	Win1.document.writeln('            theString = theString + "COMUNE LIKE " + qComune;');
	Win1.document.writeln('        }');
	Win1.document.writeln('        document.QueryStuff.QueryString.value = theString;');
	Win1.document.writeln('        currExpr = document.QueryStuff.QueryString.value;');
	Win1.document.writeln('    }');
			
	Win1.document.writeln('    function sendQuery() {'); 
	//Win1.document.writeln('        alert(document.QueryStuff.QueryString.value);');
	Win1.document.writeln('        addString();');
	//Win1.document.writeln('        alert(document.QueryStuff.QueryString.value);');
	Win1.document.writeln('        var theString = document.QueryStuff.QueryString.value;'); 
	Win1.document.writeln('        if (theString!="") {'); 
	Win1.document.writeln('            t.setActiveLayer1(t.IDImpiantiPunti);'); 
	Win1.document.writeln('            t.sendQueryString(theString);'); 
	//Win1.document.writeln('            t=null;'); 
	Win1.document.writeln('        } else {'); 
	Win1.document.writeln('            alert("' + msgList[110] + '");'); 
	Win1.document.writeln('        }'); 
	Win1.document.writeln('    }'); 
	
	Win1.document.writeln('</script>');
	Win1.document.writeln('</head>');
	Win1.document.writeln('<body link="Blue" vlink="Blue" alink="Blue" leftmargin=0 topmargin=0 bgcolor="#dfdfdf" onload="window.focus()">');
	//Win1.document.writeln('<body link="Blue" vlink="Blue" alink="Blue" leftmargin=0 topmargin=0 bgcolor="#dfdfdf" onload="window.focus(); ' + t + '.queryOpen=true;" onunload="' + t + '.queryOpen=false;">');
	Win1.document.writeln('<center>');
	Win1.document.writeln('<form name="QueryStuff" onsubmit="sendQuery();return false;">');
	Win1.document.writeln('<table class=toc border="0" cellspacing="0" cellpadding="2" width=100%>');
	Win1.document.writeln('<tr><th align="center" height=25 BGCOLOR=#FF0000>Ricerca per Impianti</th></tr>');
	Win1.document.writeln('<tr><td align="center" height=5></td></tr>');
	Win1.document.writeln('</table>');
	Win1.document.writeln('<table class=toc border="0" cellspacing="0" cellpadding="2" width=100%>');
	Win1.document.writeln('<tr>');
	Win1.document.writeln('    <td width=5></td>');
	Win1.document.writeln('    <td colspan=2 align="left">Tipo di impianto:</td>');
	Win1.document.writeln('    <td width=5></td>');
	Win1.document.writeln('</tr><tr>');
	Win1.document.writeln('    <td width=5></td>');
	Win1.document.writeln('    <td colspan=2 align="left">');
	Win1.document.writeln('        <select name="QueryField">');
	Win1.document.writeln('            <option value="" selected>');
	Win1.document.writeln('            <option value="1R" >Stazione 380 kV RTN');
	Win1.document.writeln('            <option value="2R" >Stazione 220 kV RTN');
	Win1.document.writeln('            <option value="3R" >Stazione 150 kV RTN');
	Win1.document.writeln('            <option value="4R" >Stazione 132 kV RTN');
	Win1.document.writeln('            <option value="N"  >Stazione non RTN o CP');
	Win1.document.writeln('            <option value="CU" >Cabina Utente');
	Win1.document.writeln('            <option value="EO" >Centrale Eolica');
	Win1.document.writeln('            <option value="GE" >Centrale Geotermoelettrica');
	Win1.document.writeln('            <option value="ID" >Centrale Idroelettrica');
	Win1.document.writeln('            <option value="TE" >Centrale Termoelettrica');
	Win1.document.writeln('        </select>');
	Win1.document.writeln('    </td>');
	Win1.document.writeln('    <td width=5></td>');
	Win1.document.writeln('</tr><tr>');
	Win1.document.writeln('    <td width=5></td>');
	Win1.document.writeln('    <td align="left">Codice:</td>');
	Win1.document.writeln('    <td align="left"><input name="QueryId" size="15" maxlength="1000"></td>');
	Win1.document.writeln('    <td width=5></td>');
	Win1.document.writeln('</tr><tr>');
	Win1.document.writeln('    <td width=5></td>');
	Win1.document.writeln('    <td align="left">Nome:</td>');
	Win1.document.writeln('    <td align="left"><input name="QueryNome" size="15" maxlength="1000"></td>');
	Win1.document.writeln('    <td width=5></td>');
	Win1.document.writeln('</tr><tr>');
	Win1.document.writeln('    <td width=5></td>');
	Win1.document.writeln('    <td align="left">Provincia:</td>');
	Win1.document.writeln('	   <td align="left"><input name="QueryProvincia" size="2" maxlength="2"></td>');
	Win1.document.writeln('    <td width=5></td>');
	Win1.document.writeln('</tr><tr>');
	Win1.document.writeln('    <td width=5></td>');
	Win1.document.writeln('    <td align="left">Comune:</td>');
	Win1.document.writeln('    <td align="left"><input name="QueryComune" size="15" maxlength="1000"></td>');
	Win1.document.writeln('    <td width=5></td>');
	Win1.document.writeln('</tr>');
	Win1.document.writeln('</table>');
	Win1.document.writeln('<table class=toc border="0" cellspacing="0" cellpadding="2" width=100%>');
	Win1.document.writeln('<tr><td align="center" height=5></td></tr>');
	Win1.document.writeln('<tr><td align="center">');
	Win1.document.writeln('        <input type="Hidden" name="QueryString" value=\'' + setQueryString + '\'>');
	Win1.document.writeln('        <input type="image"  src="./images/cerca.gif"  name="submit">'); 
	Win1.document.writeln('        <input type="image"  src="./images/azzera.gif" onclick="document.forms[0].reset(); return false;" name="reset">'); 
	Win1.document.writeln('</td></tr>');
	Win1.document.writeln('</table>');
	Win1.document.writeln('</font></center></FORM></body></html>');

	Win1.document.close();
	Win1=null;
	sampleList=null;
}


// Form per query solo su linee
function writeQueryForm2() {

	var startpos = 0;
	var endpos = 0;
	var SampleCount = selectData.length;
	var theIndex = fieldIndex;
	var sampleList = new Array();
	var qField = LayerFields[fieldIndex] + '="';
	var valueTitle = buttonList[2];
	var tempString = "";

	if (useRicFrame) {
		var Win1 = parent.RicFrame;
		Win1.document.open();
		var t = "parent.MapFrame";
	} else {
		var Win1 = open("","Ricerca","width=300,height=250,scrollbars=yes,resizable=yes");
		var t = "opener";
		if (parent.MapFrame!=null) t = "opener.parent.MapFrame";
	}
	
	Win1.document.writeln('<html><meta http-equiv="Content-Type" content="text/html; charset=' + charSet + '"><HEAD><link rel="stylesheet" type="text/css" href="' + appDir + 'css/style1.css">');
	Win1.document.writeln('<title>Ricerca per linee</title>');
	Win1.document.writeln('<script language="javascript">');

	Win1.document.writeln('    var t = ' + t);
	Win1.document.writeln('    var dQuote = \'"\';');
	Win1.document.writeln('    var lastExpr = "";');
	Win1.document.writeln('    var currExpr = "";');

	Win1.document.writeln('    function addString() {');
	Win1.document.writeln('        lastExpr       = document.QueryStuff.QueryString.value;');
	Win1.document.writeln('        var qField     = document.QueryStuff.QueryField.options[document.QueryStuff.QueryField.selectedIndex].value;');
	Win1.document.writeln('        var qId        = document.QueryStuff.QueryId.value;');
	Win1.document.writeln('        var qNome      = document.QueryStuff.QueryNome.value;');
	Win1.document.writeln('        var theString = "";');
	Win1.document.writeln('        if (qField != "") {');
	Win1.document.writeln('            theString = theString + "TEMA_LINEA=" + dQuote + qField + dQuote;');
	Win1.document.writeln('        }');
	Win1.document.writeln('        if (qId != "") {');
	Win1.document.writeln('            if (theString != "") {');
	Win1.document.writeln('                theString = theString + " AND ";');
	Win1.document.writeln('            }');
	Win1.document.writeln('            qId       = dQuote + "%" + qId.toUpperCase() + "%" + dQuote;');
	Win1.document.writeln('            theString = theString + "CODICE LIKE " + qId;');
	Win1.document.writeln('        }');
	Win1.document.writeln('        if (qNome != "") {');
	Win1.document.writeln('            if (theString != "") {');
	Win1.document.writeln('                theString = theString + " AND ";');
	Win1.document.writeln('            }');
	Win1.document.writeln('            qNome     = dQuote + "%" + qNome.toUpperCase() + "%" + dQuote;');
	Win1.document.writeln('            theString = theString + "((ESTREMO_FR LIKE " + qNome + ") OR (ESTREMO_TO LIKE " + qNome + "))";');
	Win1.document.writeln('        }');
	Win1.document.writeln('        document.QueryStuff.QueryString.value = theString;');
	Win1.document.writeln('        currExpr = document.QueryStuff.QueryString.value;');
	Win1.document.writeln('    }');

	Win1.document.writeln('    function sendQuery() {'); 
	//Win1.document.writeln('        alert(document.QueryStuff.QueryString.value);');
	Win1.document.writeln('        addString();');
	//Win1.document.writeln('        alert(document.QueryStuff.QueryString.value);');
	Win1.document.writeln('        var theString = document.QueryStuff.QueryString.value;'); 
	Win1.document.writeln('        if (theString!="") {'); 
	Win1.document.writeln('            t.setActiveLayer1(t.IDLinee);'); 
	Win1.document.writeln('            t.sendQueryString(theString);'); 
	//Win1.document.writeln('            t=null;'); 
	Win1.document.writeln('        } else {'); 
	Win1.document.writeln('            alert("' + msgList[110] + '");'); 
	Win1.document.writeln('        }'); 
	Win1.document.writeln('    }'); 
	
	Win1.document.writeln('</script>');
	Win1.document.writeln('</head>');
	Win1.document.writeln('<body link="Blue" vlink="Blue" alink="Blue" leftmargin=0 topmargin=0 bgcolor="#dfdfdf" onload="window.focus(); ' + t + '.queryOpen=true;" onunload="' + t + '.queryOpen=false;">');
	Win1.document.writeln('<center>');
	Win1.document.writeln('<form name="QueryStuff" onsubmit="sendQuery();return false;">');
	Win1.document.writeln('<table class=toc border="0" cellspacing="0" cellpadding="2" width=100%>');
	Win1.document.writeln('<tr><th align="center" height=25 BGCOLOR=#FF0000>Ricerca per Linee</th></tr>');
	Win1.document.writeln('<tr><td align="center" height=10></td></tr>');
	Win1.document.writeln('</table>');
	Win1.document.writeln('<table class=toc border="0" cellspacing="0" cellpadding="2" width=100%>');
	Win1.document.writeln('<tr>');
	Win1.document.writeln('    <td width=5></td>');
	Win1.document.writeln('    <td colspan=2 align="left">Tipo di linea:</td>');
	Win1.document.writeln('    <td width=5></td>');
	Win1.document.writeln('</tr><tr>');
	Win1.document.writeln('    <td width=5></td>');
	Win1.document.writeln('    <td colspan=2 align="left">');
	Win1.document.writeln('        <select name="QueryField">');
	Win1.document.writeln('            <option value="" selected>');
	Win1.document.writeln('            <option value="1NC">380 kV non RTN cavo');
	Win1.document.writeln('            <option value="1NA">380 kV non RTN aerea');
	Win1.document.writeln('            <option value="1RA">380 kV RTN aerea');
	Win1.document.writeln('            <option value="1RC">380 kV RTN cavo');
	Win1.document.writeln('            <option value="1RCcc">400 kV cc RTN');
	Win1.document.writeln('            <option value="1RM">380 kV RTN mista');
	Win1.document.writeln('            <option value="2NA">220 kV non RTN aerea');
	Win1.document.writeln('            <option value="2NC">220 kV non RTN cavo');
	Win1.document.writeln('            <option value="2RA">220 kV RTN aerea');
	Win1.document.writeln('            <option value="2RC">220 kV RTN cavo');
	Win1.document.writeln('            <option value="2RAcc">200 kV cc RTN aerea');
	Win1.document.writeln('            <option value="2RCcc">200 kV cc RTN cavo');
	Win1.document.writeln('            <option value="2RM">220 kV RTN mista');
	Win1.document.writeln('            <option value="3NA">150 kV non RTN aerea');
	Win1.document.writeln('            <option value="3NC">150 kV non RTN cavo');
	Win1.document.writeln('            <option value="3NM">150 kV non RTN mista');
	Win1.document.writeln('            <option value="3RA">150 kV RTN aerea');
	Win1.document.writeln('            <option value="3RC">150 kV RTN cavo');
	Win1.document.writeln('            <option value="3RM">150 kV RTN mista');
	Win1.document.writeln('            <option value="4NA">132 kV non RTN aerea');
	Win1.document.writeln('            <option value="4NC">132 kV non RTN cavo');
	Win1.document.writeln('            <option value="4NM">132 kV non RTN mista');
	Win1.document.writeln('            <option value="4RA">132 kV RTN aerea');
	Win1.document.writeln('            <option value="4RC">132 kV RTN cavo');
	Win1.document.writeln('            <option value="4RM">132 kV RTN mista');
	Win1.document.writeln('        </select>');
	Win1.document.writeln('    </td>');
	Win1.document.writeln('    <td width=5></td>');
	Win1.document.writeln('</tr><tr>');
	Win1.document.writeln('    <td width=5></td>');
	Win1.document.writeln('    <td align="left">Codice:</td>');
	Win1.document.writeln('    <td align="left"><input name="QueryId" size="15" maxlength="1000"></td>');
	Win1.document.writeln('    <td width=5></td>');
	Win1.document.writeln('</tr><tr>');
	Win1.document.writeln('    <td width=5></td>');
	Win1.document.writeln('    <td align="left">Nome:</td>');
	Win1.document.writeln('    <td align="left"><input name="QueryNome" size="15" maxlength="1000"></td>');
	Win1.document.writeln('    <td width=5></td>');
	Win1.document.writeln('</tr>');
	Win1.document.writeln('</table>');
	Win1.document.writeln('<table class=toc border="0" cellspacing="0" cellpadding="2" width=100%>');
	Win1.document.writeln('<tr><td align="center" height=10></td></tr>');
	Win1.document.writeln('<tr><td align="center">');
	Win1.document.writeln('        <input type="Hidden" name="QueryString" value=\'' + setQueryString + '\'>');
	Win1.document.writeln('        <input type="image"  src="./images/cerca.gif"  name="submit">'); 
	Win1.document.writeln('        <input type="image"  src="./images/azzera.gif" onclick="document.forms[0].reset(); return false;" name="reset">'); 
	Win1.document.writeln('</td></tr>');
	Win1.document.writeln('</table>');
	Win1.document.writeln('</font></center></FORM></body></html>');

	Win1.document.close();
	Win1=null;
	sampleList=null;
}

// Form per filtro sulla visualizzazione
function writeFilterForm() {

	var startpos = 0;
	var endpos = 0;
	var SampleCount = selectData.length;
	var theIndex = fieldIndex;
	var sampleList = new Array();
	var qField = LayerFields[fieldIndex] + '="';
	var valueTitle = buttonList[2];
	var tempString = "";

	if (useRicFrame) {
		var Win1 = parent.RicFrame;
		Win1.document.open();
		var t = "parent.MapFrame";
	} else {
		var Win1 = open("","Filtra","width=300,height=250,scrollbars=yes,resizable=yes");
		var t = "opener";
		if (parent.MapFrame!=null) t = "opener.parent.MapFrame";
	}
	
	Win1.document.writeln('<html><meta http-equiv="Content-Type" content="text/html; charset=' + charSet + '"><HEAD><link rel="stylesheet" type="text/css" href="' + appDir + 'css/style1.css">');
	Win1.document.writeln('<title>Filtra visualizzazione</title>');
	Win1.document.writeln('<script language="javascript">');

	Win1.document.writeln('    var t = ' + t);
	Win1.document.writeln('    var dQuote = \'"\';');
	Win1.document.writeln('    var lastExpr = "";');
	Win1.document.writeln('    var currExpr = "";');

	Win1.document.writeln('    function resetProp1() {'); 
	if(selectData.length > 0) Win1.document.writeln('        document.FilterStuff.FiltraProp1.selectedIndex = 0;');
	Win1.document.writeln('    }');

	Win1.document.writeln('    function resetProp2() {'); 
	if(selectData.length > 0) Win1.document.writeln('        document.FilterStuff.FiltraProp2.value = "";');
	Win1.document.writeln('    }');

	Win1.document.writeln('    function addString() {');
	if(selectData.length > 0) {
		Win1.document.writeln('        var qProp1     = document.FilterStuff.FiltraProp1.options[document.FilterStuff.FiltraProp1.selectedIndex].value;');
	} else {
		Win1.document.writeln('        var qProp1     = "";');
	}
	Win1.document.writeln('        var qProp2     = document.FilterStuff.FiltraProp2.value;');
	Win1.document.writeln('        var qRtn       = document.FilterStuff.FiltraRtn.options[document.FilterStuff.FiltraRtn.selectedIndex].value;');
	Win1.document.writeln('        var qImpianti  = document.FilterStuff.FiltraImpianti.checked;');
	Win1.document.writeln('        var qLinee     = document.FilterStuff.FiltraLinee.checked;');
	Win1.document.writeln('        var qTens1     = "";');
	Win1.document.writeln('        var qTens2     = "";');
	Win1.document.writeln('        var theString1 = "";');
	Win1.document.writeln('        var theString2 = "";');
	Win1.document.writeln('        var theString3 = "";');
	Win1.document.writeln('        var theString4 = "";');
	Win1.document.writeln('        if (qProp1 != "") {');
	Win1.document.writeln('		       var qProp = qProp1;');
	Win1.document.writeln('		   } else {');
	Win1.document.writeln('		       var qProp = qProp2;');
	Win1.document.writeln('		   }');
	Win1.document.writeln('        if (qProp != "") {');
	Win1.document.writeln('            qProp = dQuote + "%" + qProp.toUpperCase() + "%" + dQuote;');
	Win1.document.writeln('		       if (qImpianti) theString1 = theString1 + "PROPRIETAR like " + qProp;');
	Win1.document.writeln('		       if (qLinee) {');
	Win1.document.writeln('			       theString2 = theString2 + "PROPRIETAR like " + qProp;');
	Win1.document.writeln('			       theString3 = theString3 + "(PROPRIETA1 like " + qProp + " OR PROPRIETA2 like " + qProp + ")";');
	Win1.document.writeln('			       theString4 = theString4 + "PROPRIETAR like " + qProp;');
	Win1.document.writeln('            }');
	Win1.document.writeln('        }');
	Win1.document.writeln('        if (qRtn != "") {');
	Win1.document.writeln('            qRtn = dQuote + qRtn.toUpperCase() + dQuote;');
	Win1.document.writeln('		       if (qImpianti) {');
	Win1.document.writeln('                if (theString1 != "") theString1 = theString1 + " AND ";');
	Win1.document.writeln('                theString1 = theString1 + "RTN=" + qRtn;');
	Win1.document.writeln('            }');
	Win1.document.writeln('		       if (qLinee) {');
	Win1.document.writeln('                if (theString2 != "") theString2 = theString2 + " AND ";');
	Win1.document.writeln('                if (theString3 != "") theString3 = theString3 + " AND ";');
	Win1.document.writeln('                if (theString4 != "") theString4 = theString4 + " AND ";');
	Win1.document.writeln('			       theString2 = theString2 + "RTN=" + qRtn;');
	Win1.document.writeln('			       theString3 = theString3 + "(RTN1=" + qRtn + " OR RTN2=" + qRtn + ")";');
	Win1.document.writeln('			       theString4 = theString4 + "RTN=" + qRtn;');
	Win1.document.writeln('            }');
	Win1.document.writeln('        }');
	
	//Filtro sulla tensione con le seguenti note:
	//- per le linee usa il campo TENSIONE di tipo testo
	//- per gli impianti usa il campo TENSIONE di tipo testo che puo' contenere 
	//  piu' livelli di tensione separati da "," (es: "132,220,380")
	Win1.document.writeln('        for (var i=0; i< document.FilterStuff.FiltraTens.options.length; i++) {');
    Win1.document.writeln('            if(document.FilterStuff.FiltraTens.options(i).selected) {');
	Win1.document.writeln('                 qTens = document.FilterStuff.FiltraTens.options[i].value;');
	Win1.document.writeln('                 if (qTens != "") {');
	Win1.document.writeln('					    if(qTens1 != "") qTens1 = qTens1 + " OR ";');
	Win1.document.writeln('						if(qTens2 != "") qTens2 = qTens2 + " OR ";');
	Win1.document.writeln('                     qTens1 = qTens1 + "TENSIONE like " + dQuote + "%" + qTens + "%" + dQuote;');
	Win1.document.writeln('                     qTens2 = qTens2 + "TENSIONE=" + dQuote + qTens + dQuote;');
	Win1.document.writeln('                 }');
	Win1.document.writeln('            }');
	Win1.document.writeln('        }');
	Win1.document.writeln('        if(qTens1 != "") {;');
	Win1.document.writeln('		       if (qImpianti) {');
	Win1.document.writeln('                if (theString1 != "") theString1 = theString1 + " AND ";');
	Win1.document.writeln('                theString1 = theString1 + "(" + qTens1 + ")";');
	Win1.document.writeln('            }');
	Win1.document.writeln('		       if (qLinee) {');
	Win1.document.writeln('                if (theString2 != "") theString2 = theString2 + " AND ";');
	Win1.document.writeln('                if (theString3 != "") theString3 = theString3 + " AND ";');
	Win1.document.writeln('                if (theString4 != "") theString4 = theString4 + " AND ";');
	Win1.document.writeln('		       	   theString2 = theString2 + "(" + qTens2 + ")";');
	Win1.document.writeln('		       	   theString3 = theString3 + "(" + qTens2 + ")";');
	Win1.document.writeln('		           theString4 = theString4 + "(" + qTens2 + ")";');
	Win1.document.writeln('            }');
	Win1.document.writeln('        }');
	
	Win1.document.writeln('        document.FilterStuff.FilterString1.value = theString1;');
	Win1.document.writeln('        document.FilterStuff.FilterString2.value = theString2;');
	Win1.document.writeln('        document.FilterStuff.FilterString3.value = theString3;');
	Win1.document.writeln('        document.FilterStuff.FilterString4.value = theString4;');
	Win1.document.writeln('    }');

	Win1.document.writeln('    function sendFilter() {'); 
	Win1.document.writeln('        addString();');
	//Win1.document.writeln('        alert(document.FilterStuff.FilterString1.value);');
	//Win1.document.writeln('        alert(document.FilterStuff.FilterString2.value);');
	//Win1.document.writeln('        alert(document.FilterStuff.FilterString3.value);');
	//Win1.document.writeln('        alert(document.FilterStuff.FilterString4.value);');
	Win1.document.writeln('        var theString1 = document.FilterStuff.FilterString1.value;'); 
	Win1.document.writeln('        var theString2 = document.FilterStuff.FilterString2.value;'); 
	Win1.document.writeln('        var theString3 = document.FilterStuff.FilterString3.value;'); 
	Win1.document.writeln('        var theString4 = document.FilterStuff.FilterString4.value;'); 
	Win1.document.writeln('        if ((theString1!="") || (theString2!="") || (theString3!="") || (theString4!="")) {'); 
	Win1.document.writeln('            t.sendFilterString(theString1,theString2,theString3,theString4);'); 
	//Win1.document.writeln('            t=null;'); 
	Win1.document.writeln('        } else {'); 
	Win1.document.writeln('            alert("' + msgList[110] + '");'); 
	Win1.document.writeln('        }'); 
	Win1.document.writeln('    }'); 
	
	Win1.document.writeln('</script>');
	Win1.document.writeln('</head>');
	Win1.document.writeln('<body link="Blue" vlink="Blue" alink="Blue" leftmargin=0 topmargin=0 bgcolor="#dfdfdf" onload="window.focus(); ' + t + '.queryOpen=true;" onunload="' + t + '.queryOpen=false;">');
	Win1.document.writeln('<center>');
	Win1.document.writeln('<form name="FilterStuff" onsubmit="sendFilter();return false;">');
	Win1.document.writeln('<table class=toc border="0" cellspacing="0" cellpadding="2" width=100%>');
	Win1.document.writeln('<tr><th align="center" height=25 BGCOLOR=#FF0000>Filtra visualizzazione</th></tr>');
	Win1.document.writeln('<tr><td align="center" height=10></td></tr>');
	Win1.document.writeln('</table>');
	Win1.document.writeln('<table class=toc border="0" cellspacing="0" cellpadding="2" width=100%>');
	
	if(selectData.length > 0) {
		Win1.document.writeln('<tr>');
		Win1.document.writeln('    <td width=5></td>');
		Win1.document.writeln('    <td align="left">Proprietà:</td>');
		Win1.document.writeln('    <td align="left">');
		Win1.document.writeln('        <select size=1 name="FiltraProp1" style="width:130" onchange="resetProp2()">');
		Win1.document.writeln('            <option value="" selected>');
		for(var k=0; k<selectData.length; k++) {
			Win1.document.writeln('        <option value="' + selectData[k] + '">' + selectData[k]);
		}
		Win1.document.writeln('        </select>');
		Win1.document.writeln('    </td>');
		Win1.document.writeln('    <td width=5></td>');
		Win1.document.writeln('</tr>');
	}
	
	Win1.document.writeln('<tr>');
	Win1.document.writeln('    <td width=5></td>');
	Win1.document.writeln('    <td align="left">Proprietà:</td>');
	Win1.document.writeln('    <td align="left"><input name="FiltraProp2" size="20" maxlength="1000" onkeypress="resetProp1()"></td>');
	Win1.document.writeln('    <td width=5></td>');
	Win1.document.writeln('</tr><tr>');
	Win1.document.writeln('    <td width=5></td>');
	Win1.document.writeln('    <td align="left">RTN:</td>');
	Win1.document.writeln('    <td align="left">');
	Win1.document.writeln('        <select name="FiltraRtn" style="width:80" >');
	Win1.document.writeln('            <option value="" selected>');
	Win1.document.writeln('            <option value="SI" >Si');
	Win1.document.writeln('            <option value="NO" >No');
	Win1.document.writeln('        </select>');
	Win1.document.writeln('    </td>');
	Win1.document.writeln('    <td width=5></td>');
	Win1.document.writeln('</tr><tr>');
	Win1.document.writeln('    <td width=5></td>');
	Win1.document.writeln('    <td align="left">Tensione:</td>');
	Win1.document.writeln('    <td align="left">');
	Win1.document.writeln('        <select size=3 MULTIPLE name="FiltraTens" style="width:80" >');
	Win1.document.writeln('            <option value="132"   >132 kV');
	Win1.document.writeln('            <option value="150"   >150 kV');
	Win1.document.writeln('            <option value="220"   >220 kV');
	Win1.document.writeln('            <option value="380"   >380 kV');
	Win1.document.writeln('            <option value="200cc" >200 kV cc');
	Win1.document.writeln('            <option value="400cc" >400 kV cc');
	Win1.document.writeln('        </select>');
	Win1.document.writeln('    </td>');
	Win1.document.writeln('    <td width=5></td>');
	Win1.document.writeln('</tr><tr>');
	Win1.document.writeln('    <td width=5></td>');

	Win1.document.writeln('    <td colspan=2 align="middle">');
	Win1.document.writeln('    <input checked type="Checkbox" name="FiltraImpianti">Impianti');
	Win1.document.writeln('    &nbsp;&nbsp;');
	Win1.document.writeln('    <input checked type="Checkbox" name="FiltraLinee">Linee');
	Win1.document.writeln('    </td>');

	//Win1.document.writeln('    <td colspan=2 align="left"><input checked type="Checkbox" name="FiltraImpianti">Applica filtro agli impianti</td>');
	//Win1.document.writeln('    <td width=5></td>');
	//Win1.document.writeln('</tr><tr>');
	//Win1.document.writeln('    <td width=5></td>');
	//Win1.document.writeln('    <td colspan=2 align="left"><input checked type="Checkbox" name="FiltraLinee">Applica filtro alle linee</td>');

	Win1.document.writeln('    <td width=5></td>');
	Win1.document.writeln('</tr>');
	Win1.document.writeln('</table>');
	Win1.document.writeln('<table class=toc border="0" cellspacing="0" cellpadding="2" width=100%>');
	Win1.document.writeln('<tr><td align="center" height=10></td></tr>');
	Win1.document.writeln('<tr><td align="center">');
	Win1.document.writeln('        <input type="Hidden" name="FilterString1" value=\'' + setFilterString1 + '\'>');
	Win1.document.writeln('        <input type="Hidden" name="FilterString2" value=\'' + setFilterString2 + '\'>');
	Win1.document.writeln('        <input type="Hidden" name="FilterString3" value=\'' + setFilterString3 + '\'>');
	Win1.document.writeln('        <input type="Hidden" name="FilterString4" value=\'' + setFilterString4 + '\'>');
	Win1.document.writeln('        <input type="image"  src="' + appDir + 'images/filtra.gif"  name="submit">'); 
	Win1.document.writeln('        <input type="image"  src="' + appDir + 'images/azzera.gif" onclick="document.forms[0].reset(); return false;" name="reset">'); 
	Win1.document.writeln('</td></tr>');
	Win1.document.writeln('</table>');
	Win1.document.writeln('</font></center></FORM></body></html>');

	Win1.document.close();
	Win1=null;
	sampleList=null;
}

// write out XML request to get field value samples
function writeFieldSample(theField) {
	var theFields = theField;
	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" geometry="false" envelope="false"';
	theString += ' featurelimit="' + numberDataSamples + '">\n';
	theString += '<LAYER id="' + ActiveLayer + '" />';
	//theString += '<QUERY subfields="' + theFields + '" where="' + LayerIDField[ActiveLayerIndex] + ' GT 0" />';
	theString += '<QUERY subfields="' + theFields + '" />';
	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;
	//alert(theString);
	return theString;
}

// parse layer field value samples
function parseFieldSamples(theReply) {
	//alert("Reply Length: " + theReply.length);
	//alert(theReply);
	var fList="";
	var lpos1 = 1;
	var lpos2 = 1;
	var epos = 1;
	var spos = 1;
	var morePoints=true;
	var moreFeatures=true;
	var pos = 0;
	var startpos = 0;
	var endpos = 0;
	var featureCount = justGetFeatureCount(theReply);
	var tempString="";
	var trovato = false;
	var conta = 0;
	if (featureCount > 0) {
		//selectData.length=featureCount;
		for (var i=0;i<featureCount;i++) {
			pos      = theReply.indexOf("<FIELDS ",endpos);
			startpos = pos + 8;
			endpos   = theReply.indexOf(' />',startpos);
			pos      = theReply.indexOf("=",startpos);
			inData   = theReply.substring(pos+2,endpos-1);
			if(inData != "") {
				trovato  = false;
				for (var k=0;k<selectData.length;k++) {
					if(inData == selectData[k]) {
						trovato = true;
						break;
					}
				}
				if(trovato == false) {
					selectData[conta] = inData;
					conta = conta + 1;
				}			
			}
			//inData = fixSingleQuotes(inData);
			//selectData[i] = clearLeadingSpace(inData);
			//selectData[i] = escape(inData);
		}
	} else {
		selectData.length=0;
	}
	if(selectData.length > 0) selectData.sort();
}
