// 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 

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


// 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 pop-up
function queryForm2() {
        if (checkIfActiveLayerAvailable()) {
                fieldIndex=0;
                showSampleValues=false;
                
                setLayerFields(ActiveLayerIndex);
                if (showSampleValues) {
                        var theText = writeFieldSample(LayerFields[fieldIndex]);
                        sendToServer(imsQueryURL,theText,41);
                } else {
                        writeQueryForm2();
                }
        }
                
}

// find Form
function findForm() {
        if (checkIfActiveLayerAvailable()) {
                setLayerFields(ActiveLayerIndex);
                if (useTextFrame) {
                        parent.TextFrame.document.location= appDir + "findForm.htm";
                } else {
                        Win1 = open(appDir + "findForm.htm","QueryWindow","width=575,height=150,scrollbars=yes,resizable=yes");
                }
        }
}



// process query
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);
}

function sendQueryStringMode(newString, iXMLMode) {
	//alert("newString:   " + newString + "     iXMLMode:  " +  iXMLMode);
	//alert( "LayerName[ActiveLayerIndex]:   " + LayerName[ActiveLayerIndex]);
        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);
        //alert("theString    " + theString);
        //alert("imsQueryURL    " + imsQueryURL + "\ntheString:   " +  theString + "\niXMLMode:    " + iXMLMode + "\nLayerName[ActiveLayerIndex]:   " + LayerName[ActiveLayerIndex]);
        //alert("iXMLMode    " + iXMLMode);
        
        sendToServer(imsQueryURL,theString,iXMLMode);
}

function sendQueryStringMode2(newString, iXMLMode) {
        newString = fixSingleQuotes(newString);
        newString = swapQuotes(newString);
        newString = makeXMLsafe(newString);
        setQueryString=newString;
        var theString = writeQueryXML2(newString);
        sendToServer(imsQueryURL,theString,iXMLMode);
}

// 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 += '<SPATIALQUERY subfields="' + selectFields + '" where="' + queryString + '" />';
        }
        theString += '</GET_FEATURES>';
        theString += '</REQUEST>';
        theString += '</ARCXML>';
        selectLayer=ActiveLayer;
        selectType=ActiveLayerType;
        selectCount=0;
        hightlightedOne="";
        selectPoints.length=1;
        selectLeft.length=1;
        selectRight.length=1;
        selectTop.length=1;
        selectBottom.length=1;
        return theString;
        
}

function writeQueryXML2(queryString) {
	//alert("writeQueryXML2");
        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 += '<SPATIALQUERY subfields="' + selectFields + '" where="' + queryString + '" />';
        }
        theString += '</GET_FEATURES>';
        theString += '</REQUEST>';
        theString += '</ARCXML>';
        return theString;
        
}

// write out XML request to get field value samples
function writeFieldSample(theField) {
        //alert("writeFieldSample");
        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 + '" />';
        theString += '</GET_FEATURES>';
        theString += '</REQUEST>';
        theString += '</ARCXML>';
        selectLayer=ActiveLayer;
        selectType=ActiveLayerType;
        selectCount=0;
        hightlightedOne="";
        selectPoints.length=1;
        selectLeft.length=1;
        selectRight.length=1;
        selectTop.length=1;
        selectBottom.length=1;
        return theString;
}
// send find request
function getFind(theValue) {
        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;
        selectionMode=1;
        var theNewQueryString = "";
        var theTempString = "";
        var fieldListString = LayerIDField[ActiveLayerIndex] + " " + LayerShapeField[ActiveLayerIndex];
        var elemCount = 0;
        for (var i=0;i<LayerFields.length;i++) {
                if (LayerFieldType[i].indexOf("12",0)!=-1) {
                        theTempString = "(" + LayerFields[i] + " LIKE '%" + theValue + "%')";
                        if (elemCount>0) theTempString = " OR " + theTempString;
                        if (theNewQueryString.length + theTempString.length < 1024) {
                                theNewQueryString = theNewQueryString + theTempString;
                                fieldListString = fieldListString + " " + LayerFields[i];
                                elemCount += 1;
                        }
                        
                }
        }
        if (theNewQueryString!="") {
                showRetrieveData();
                showBuffer=false;
                theNewQueryString=makeXMLsafe(theNewQueryString);
                setQueryString = theNewQueryString;
                var theString = writeFindRequest(theNewQueryString,fieldListString);
                sendToServer(imsQueryURL,theString,findXMLMode);
        } else {
                alert(msgList[80]);
        }
}

// write out find form
function writeFindRequest(findQuery,fieldList) {
        //alert("writeFindRequest");
        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 + '" />\n';
        if (useLimitExtent) {
                // keep this within the limitExtent
                theString += '<SPATIALQUERY subfields="' + fieldList + '" where="' + findQuery + '" />';
                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="' + fieldList + '" where="' + findQuery + '" />';
        }
        theString += '</GET_FEATURES>\n';
        theString += '</REQUEST>\n';
        theString += '</ARCXML>';
        selectLayer=ActiveLayer;
        selectType=ActiveLayerType;
        selectCount=0;
        hightlightedOne="";
        selectPoints.length=1;
        selectLeft.length=1;
        selectRight.length=1;
        selectTop.length=1;
        selectBottom.length=1;
        return theString;

}


// parse layer field value samples
function parseFieldSamples(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="";
        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);
                        inData = theReply.substring(startpos,endpos);
                        selectData[i] = inData;

                }
        } else {
                selectData.length=0;
        }
        
}

// 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);
                        startpos = startpos + qField.length;
                        endpos = selectData[i].indexOf('"',startpos);
                        if (LayerFieldType[theIndex].indexOf("12",0)!=-1) {
                                // a Character field
                                var vData = makeXMLsafe(selectData[i].substring(startpos,endpos));
                                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 {
                                sampleList[i] = selectData[i].substring(startpos,endpos);
                        }
                }
                valueTitle = buttonList[3];
        }
        if (useTextFrame) {
                var Win1 = parent.TextFrame;
                Win1.document.open();
                var fMap = "parent.MapFrame";
                
        } else {
                var Win1 = open("","Recherche","width=575,height=300,scrollbars=yes,resizable=yes");
                var fMap;
                if (opener) {
                        if (opener.parent.parent.name=="MapFrame") {
                                fMap = "opener.parent.parent.parent.MapFrame";
                        } else {
                                fMap = "opener.parent.parent";
                        }
                } else {
                        
                        if (parent.parent.parent.parent.MapFrame) {
                                fMap = "parent.parent.parent.parent.MapFrame";
                        } else {
                                fMap="document";
                        }
                
                }
                //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>');
        Win1.document.writeln('<link rel="stylesheet" type="text/css" href="normal.css">');
        Win1.document.writeln('                 <script language="javascript">');
                //Win1.document.writeln('                 var fMap = ' + fMap);
                Win1.document.writeln('                 var fMap = parent.parent.parent.parent.MapFrame;');
                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('                 catTab = new Array("Activite culturelle","Administration","Arena","Berge","Bibliotheque","Bureau municipal de loisirs",');
		Win1.document.writeln('                 	"Caserne de pompier","Centre communautaire","Centre recreatif en general","Chambre de commerce",');
		Win1.document.writeln('                 	"Cosmodome","Cour municipale","Fourriere","Garderie","Hopital","Office municipal d\'habitation","Palais de justice",');
		Win1.document.writeln('                 	"Parc","Police","Protection incendie","Terrain de golf","Tourisme","Transport en commun");');
		Win1.document.writeln('                  var catprox = "";');
                
                
                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 = parent.parent.parent.parent.MapFrame.parseEntity(document.QueryStuff.QueryValue.options[document.QueryStuff.QueryValue.selectedIndex].value);');
                } else{
                        Win1.document.writeln('                                 var qString = document.QueryStuff.QueryValue.value;');
                        Win1.document.writeln('                                 if (parent.parent.parent.parent.MapFrame.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 (parent.parent.parent.parent.MapFrame.LayerFieldType[fNum].indexOf("91",0)!=-1) {');
                Win1.document.writeln('                                         qString = parent.parent.parent.parent.MapFrame.formatDate(qString);');
                Win1.document.writeln('                                 }');
                Win1.document.write('                                   if ((parent.parent.parent.parent.MapFrame.LayerFieldType[fNum].indexOf("4",0)!=-1) || ');
                Win1.document.write('                                   (parent.parent.parent.parent.MapFrame.LayerFieldType[fNum].indexOf("8",0)!=-1) || ');
                Win1.document.write('                                   (parent.parent.parent.parent.MapFrame.LayerFieldType[fNum].indexOf("6",0)!=-1) || ');
                Win1.document.write('                                   (parent.parent.parent.parent.MapFrame.LayerFieldType[fNum].indexOf("5",0)!=-1)) {');
                Win1.document.writeln('                                         qString = parent.parent.parent.parent.MapFrame.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('                                         parent.parent.parent.parent.MapFrame.sendQueryString(theString);'); 
                //Win1.document.writeln('                                         fMap=null;'); 
                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('                         parent.parent.parent.parent.MapFrame.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('                         parent.parent.parent.parent.MapFrame.setQueryString = "";');
                Win1.document.writeln('                 }');
                        
                Win1.document.writeln('                 function setUp() {');
                Win1.document.writeln('                         if (parent.parent.parent.parent.MapFrame.showSampleValues) {');
                Win1.document.writeln('                                 parent.parent.parent.parent.MapFrame.fieldIndex = document.QueryStuff.QueryField.selectedIndex;');
                Win1.document.writeln('                                 parent.parent.parent.parent.MapFrame.setQueryString = document.QueryStuff.QueryString.value;');
                Win1.document.writeln('                                 var theField = parent.parent.parent.parent.MapFrame.LayerFields[parent.parent.parent.parent.MapFrame.fieldIndex];');
                Win1.document.writeln('                                 //alert(theField);');
                Win1.document.writeln('                                 parent.parent.parent.parent.MapFrame.tempGetSamples(theField);');
                Win1.document.writeln('                                 parent.parent.parent.parent.MapFrame.writeQueryForm();');
                Win1.document.writeln('                         }');
                Win1.document.writeln('                 }');
                
                Win1.document.writeln('                 function changeToSamples() {'); 
                Win1.document.writeln('                         parent.parent.parent.parent.MapFrame.fieldIndex = document.QueryStuff.QueryField.selectedIndex;');
                Win1.document.writeln('                         parent.parent.parent.parent.MapFrame.setQueryString = document.QueryStuff.QueryString.value;');
                Win1.document.writeln('                         parent.parent.parent.parent.MapFrame.tempGetSamples(parent.parent.parent.parent.MapFrame.LayerFields[parent.parent.parent.parent.MapFrame.fieldIndex]);');
                Win1.document.writeln('                 }');
                
                Win1.document.writeln('			function voir_type(n){');
                Win1.document.writeln('				catprox = catTab[n - 1];');
                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 text="#FFFFCC" bgcolor="#1A599C" onload="window.focus(); ' + fMap + '.queryOpen=true;" onunload="' + fMap + '.queryOpen=false;">');
                Win1.document.writeln('                 <form name="QueryStuff" onsubmit="sendQuery();return false;">');
                Win1.document.writeln('                 <TABLE BGCOLOR="#1A599C" BORDER=0 CELLSPACING=0 CELLPADDING=0>');
                Win1.document.writeln('                 <TR>');
                Win1.document.writeln('                   <TD VALIGN=TOP align=left>');
                Win1.document.writeln('                 <font face="Arial" size="-3">');
                Win1.document.writeln('                 <table border="0">');
                Win1.document.writeln('                 <tr><td><b>Recherche d`un point d`intérêt</b></td></tr>');
                Win1.document.writeln('                 <tr><td align="left" bgcolor="' + textFrameBackColor + '"><font face="Arial" size="1"><b>' + msgList[72] + '</b></font></td>');
                Win1.document.writeln('                 </tr>');
                Win1.document.writeln('                 <tr><td align="left" bgcolor="' + textFrameBackColor + '">');
   
                Win1.document.writeln('<select size="1" name="categorie" onchange="voir_type(this.selectedIndex)">');
                Win1.document.writeln('<option>&lt; Sélection &gt;');
                Win1.document.writeln('<option>Activite culturelle');
                Win1.document.writeln('<option>Administration');
                Win1.document.writeln('<option>Arena');
                Win1.document.writeln('<option>Berge');
                Win1.document.writeln('<option>Bibliotheque');
                Win1.document.writeln('<option>Bureau municipal de loisirs');
                Win1.document.writeln('<option>Caserne de pompier');
                Win1.document.writeln('<option>Centre communautaire');
                Win1.document.writeln('<option>Centre recreatif en general');
                Win1.document.writeln('<option>Chambre de commerce');
                Win1.document.writeln('<option>Cosmodome');
                Win1.document.writeln('<option>Cour municipale');
                Win1.document.writeln('<option>Fourriere');
                Win1.document.writeln('<option>Garderie');
                Win1.document.writeln('<option>Hopital');
                Win1.document.writeln('<option>Office municipal d\'habitation');
                Win1.document.writeln('<option>Palais de justice');
                Win1.document.writeln('<option>Parc');
                Win1.document.writeln('<option>Police');
                Win1.document.writeln('<option>Protection incendie');
                Win1.document.writeln('<option>Terrain de golf');
                Win1.document.writeln('<option>Tourisme');
                Win1.document.writeln('<option>Transport en commun');
                Win1.document.writeln('</select>');
                
                        
                        /*Win1.document.writeln('                 <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) {
                                        alias=LayerFields[i];
                                        if (fieldAliasList[ActiveLayerIndex].length>0) {
                                             var start=fieldAliasList[ActiveLayerIndex].indexOf(LayerFields[i]+':');
                                             if (start != -1) {
                                                  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>');*/
                Win1.document.writeln('                 </td></tr></table>');
                //Win1.document.writeln('                 <table border="0"><tr><td align="left" bgcolor="' + textFrameBackColor + '"><font face="Arial" size="1"><b>' + msgList[73] + '</b></font></td>');
                //Win1.document.writeln('                 <td align="left" bgcolor="' + textFrameBackColor + '"><font face="Arial" size="1"><b>&nbsp;&nbsp;' + valueTitle + '</b></font></td>');
                //Win1.document.writeln('                 </tr></table>');
                //Win1.document.writeln('                 <table border="0"><tr><td align="left" bgcolor="' + textFrameBackColor + '">');
                //Win1.document.writeln('                 <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('                       <option value=" CN ">CN ');
               // Win1.document.writeln('                 </select>');
                     
                //Win1.document.writeln('                 </td><td align="CENTER" bgcolor="' + textFrameBackColor + '">');
                        
                if (showSampleValues) {
                        Win1.document.writeln('                 <select name="QueryValue">');
                                for (var i=0;i<SampleCount;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="10" maxlength="1000" >');
                        Win1.document.writeln('                   </td>');
                        Win1.document.writeln('                 </tr>');
                        Win1.document.writeln('<tr><td>');
                        
                        Win1.document.writeln('<a href="javascript:changeToSamples();"><img src="' + appDir + 'images/bouton_echantillon.gif" border=0></a>');          
                        //Win1.document.writeln('<input type="button" name="makeList" value="' + buttonList[4] + '" onclick="changeToSamples()">');
                        Win1.document.writeln('                 </td>');
                }       
                  
                Win1.document.writeln('                 </td>');
                Win1.document.writeln('                 </tr></table>');
                
                //Win1.document.writeln('                 <table border="0" bgcolor="#FFFFCC"><tr><td bgcolor="#1A599C">');
                
                /*Win1.document.writeln('                 <a href="javascript:addAnd();"><img src="' + appDir + 'images/bouton_et.gif" border=0></a>');
                Win1.document.writeln('                 <a href="javascript:addOr();"><img src="' + appDir + 'images/bouton_ou.gif" border=0></a>');
                Win1.document.writeln('                 <a href="javascript:addNot();"><img src="' + appDir + 'images/bouton_non.gif" border=0></a>');
                Win1.document.writeln('                 <a href="javascript:addLeftPara();"><img src="' + appDir + 'images/bouton_parent_droite.gif" border=0></a>');
                Win1.document.writeln('                 <a href="javascript:addRightPara();"><img src="' + appDir + 'images/bouton_parent_gauche.gif" border=0></a>');
                */
                //Win1.document.writeln('                       <input type="button" name="theAnd" value=" Et " onclick="addAnd()">');
                //Win1.document.writeln('                       <input type="button" name="theOr" value=" Ou " onclick="addOr()">');
                //Win1.document.writeln('                       <input type="button" name="theNot" value=" Non " 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('                 </table><table border="0">');
                
                Win1.document.writeln('                 <tr><td colspan="4" align="left" bgcolor="' + textFrameBackColor + '">');
                
                Win1.document.writeln('                 <a href="javascript:addString();"><img src="' + appDir + 'images/bouton_ajouter.gif" border=0></a>');
                //Win1.document.writeln('                 <input type="button" name="addIt" value="' + buttonList[5] + '" onclick="addString()">');
                Win1.document.writeln('                 <input type="Text" name="QueryString" size="30" maxlength="1024" value=\'' + setQueryString + '\'><br>');
                
                Win1.document.writeln('                 <a href="javascript:sendQuery();"><img src="' + appDir + 'images/bouton_executer.gif" border=0></a>');
                //Win1.document.writeln('                 <input type="submit" value="' + buttonList[6] + '" name="submit">'); 
                Win1.document.writeln('                 <a href="javascript:undoString();"><img src="' + appDir + 'images/bouton_annuler.gif" border=0></a>');
                Win1.document.writeln('                 <a href="javascript:clearString();"><img src="' + appDir + 'images/bouton_effacer.gif" border=0></a>');
                //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('                 </font>');*/
                
                
                Win1.document.writeln('                   </td>');
                Win1.document.writeln('                 </tr>');
                Win1.document.writeln('                 </table>');
                
                Win1.document.writeln('                 </FORM></body></html>');
        
                Win1.document.close();
                Win1=null;
                sampleList=null;
}

function ajouterQueryOperator (iInt) {
        var sOption="";
        for (var j=0; j<6; j++) {
                sOption+='<OPTION ';
                switch(j) {
                        case 0:
                                if (QueryOperator[iInt]==0) sOption += 'selected ';
                                sOption += 'value=" = ">=';
                                break;
                        case 1:
                                if (QueryOperator[iInt]==1) sOption += 'selected ';
                                sOption += 'value=" < ">&lt;';
                                break;
                        case 2:
                                if (QueryOperator[iInt]==2) sOption += 'selected ';
                                sOption += 'value=" > ">&gt;';
                                break;
                        case 3:
                                if (QueryOperator[iInt]==3) sOption += 'selected ';
                                sOption += 'value=" <= ">&lt;=';
                                break;
                        case 4:
                                if (QueryOperator[iInt]==4) sOption += 'selected ';
                                sOption += 'value=" >= ">&gt;=';
                                break;
                        case 5:
                                if (QueryOperator[iInt]==5) sOption += 'selected ';
                                sOption += 'value=" LIKE ">LIKE';
                                break;
                }
                sOption+="\r\n";
        }
        return(sOption);
}


// write out a query form pop up
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 (showSampleValues) {
                for (var i=0;i<SampleCount;i++) {
                        startpos = selectData[i].indexOf(qField,0);
                        startpos = startpos + qField.length;
                        endpos = selectData[i].indexOf('"',startpos);
                        if (LayerFieldType[theIndex].indexOf("12",0)!=-1) {
                                // a Character field
                                var vData = makeXMLsafe(selectData[i].substring(startpos,endpos));
                                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 {
                                sampleList[i] = selectData[i].substring(startpos,endpos);
                        }
                }
                valueTitle = buttonList[3];
        }
        
        if (useTextFrame) {
                var Win1 = parent.TextFrame;
                Win1.document.open();
                var fMap2 = "parent.MapFrame";
        } else {
                var Win1 = open("","Recherche","width=600,height=300,scrollbars=yes,resizable=yes");
                var fMap2;
                if (opener) {
                        if (opener.parent.parent.name=="MapFrame") {
                                fMap2 = "opener.parent.parent.parent.MapFrame";
                        } else {
                                fMap2 = "opener.parent.parent";
                        }
                } else {
                        
                        if (parent.parent.parent.parent.MapFrame) {
                                fMap2 = "parent.parent.parent.parent.MapFrame";
                        } else {
                                fMap2="document";
                        }
                
                }
                
                //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>');
        Win1.document.writeln('<link rel="stylesheet" type="text/css" href="normal.css">');
        Win1.document.writeln('                 <script language="javascript">');
                Win1.document.writeln('                 var fMap2 = parent.parent.parent.parent.MapFrame;');
                //Win1.document.writeln('                 var fMap2 = ' + fMap2);
                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('                 function addString() {');
                Win1.document.writeln('                         var qField = "";');
                Win1.document.writeln('                         var qOperator = "";');
                Win1.document.writeln('                         var qLineOperator = "";');
                Win1.document.writeln('                         var fNum = "";');
                Win1.document.writeln('                         var qString = "";');
                Win1.document.writeln('                         lastExpr = document.QueryStuff.QueryString.value;');
                Win1.document.writeln('                         document.QueryStuff.QueryString.value="";');
                
                //Première ligne de recherche
                Win1.document.writeln('                         qField = document.QueryStuff.QueryField.options[document.QueryStuff.QueryField.selectedIndex].value;');
                Win1.document.writeln('                         qOperator = document.QueryStuff.QueryOperator.options[document.QueryStuff.QueryOperator.selectedIndex].value;');
                Win1.document.writeln('                         fNum = document.QueryStuff.QueryField.selectedIndex;');
                if (showSampleValues) {
                        Win1.document.writeln('                         qString = fMap2.parseEntity(document.QueryStuff.QueryValue.options[document.QueryStuff.QueryValue.selectedIndex].value);');
                } else{
                        Win1.document.writeln('                         qString = document.QueryStuff.QueryValue.value;');
                        Win1.document.writeln('                         if (fMap2.LayerFieldType[fNum].indexOf("12",0)!=-1) {');
                        Win1.document.writeln('                                 if (qString.indexOf(dQuote)==-1) {');
                        Win1.document.writeln('                                    if (qOperator==" LIKE ") {');
                        Win1.document.writeln('                                         qString = dQuote + "%" + qString + "%" + dQuote;');
                        Win1.document.writeln('                                    } else {');
                        Win1.document.writeln('                                         qString = dQuote + qString + dQuote;');
                        Win1.document.writeln('                                    }');
                        Win1.document.writeln('                                 }');
                        Win1.document.writeln('                         }');
                }
                Win1.document.writeln('                                 if (fMap2.LayerFieldType[fNum].indexOf("91",0)!=-1) {');
                Win1.document.writeln('                                         qString = fMap2.formatDate(qString);');
                Win1.document.writeln('                                 }');
                Win1.document.write('                                   if ((fMap2.LayerFieldType[fNum].indexOf("4",0)!=-1) || ');
                Win1.document.write('                                   (fMap2.LayerFieldType[fNum].indexOf("8",0)!=-1) || ');
                Win1.document.write('                                   (fMap2.LayerFieldType[fNum].indexOf("6",0)!=-1) || ');
                Win1.document.write('                                   (fMap2.LayerFieldType[fNum].indexOf("5",0)!=-1)) {');
                Win1.document.writeln('                                         qString = fMap2.convertDecimal(qString);');
                Win1.document.writeln('                                 }');
                Win1.document.writeln('                                 if ((qString.length > 0) && (qString != \'""\'))');
                Win1.document.writeln('                                         document.QueryStuff.QueryString.value += "(" + qField + " " + qOperator + " " + qString + ")";');
                
                //Deuxième ligne de recherche
                Win1.document.writeln('                         qField = document.QueryStuff.QueryField2.options[document.QueryStuff.QueryField2.selectedIndex].value;');
                Win1.document.writeln('                         qOperator = document.QueryStuff.QueryOperator2.options[document.QueryStuff.QueryOperator2.selectedIndex].value;');
                Win1.document.writeln('                         qLineOperator = document.QueryStuff.QueryLineOperator.options[document.QueryStuff.QueryLineOperator.selectedIndex].value;');
                Win1.document.writeln('                         fNum = document.QueryStuff.QueryField2.selectedIndex;');
                Win1.document.writeln('                         qString = document.QueryStuff.QueryValue2.value;');
                Win1.document.writeln('                         if (fMap2.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 (fMap2.LayerFieldType[fNum].indexOf("91",0)!=-1) {');
                Win1.document.writeln('                                         qString = fMap2.formatDate(qString);');
                Win1.document.writeln('                                 }');
                Win1.document.write('                                   if ((fMap2.LayerFieldType[fNum].indexOf("4",0)!=-1) || ');
                Win1.document.write('                                   (fMap2.LayerFieldType[fNum].indexOf("8",0)!=-1) || ');
                Win1.document.write('                                   (fMap2.LayerFieldType[fNum].indexOf("6",0)!=-1) || ');
                Win1.document.write('                                   (fMap2.LayerFieldType[fNum].indexOf("5",0)!=-1)) {');
                Win1.document.writeln('                                         qString = fMap2.convertDecimal(qString);');
                Win1.document.writeln('                                 }');
                Win1.document.writeln('                                 if ((qString.length > 0) && (qString != \'""\'))');
                Win1.document.writeln('                                         document.QueryStuff.QueryString.value += qLineOperator + " (" + qField + " " + qOperator + " " + qString + ")";');
                
                //Troisième ligne de recherche
                Win1.document.writeln('                         qField = document.QueryStuff.QueryField3.options[document.QueryStuff.QueryField3.selectedIndex].value;');
                Win1.document.writeln('                         qOperator = document.QueryStuff.QueryOperator3.options[document.QueryStuff.QueryOperator3.selectedIndex].value;');
                Win1.document.writeln('                         qLineOperator = document.QueryStuff.QueryLineOperator2.options[document.QueryStuff.QueryLineOperator2.selectedIndex].value;');
                Win1.document.writeln('                         fNum = document.QueryStuff.QueryField3.selectedIndex;');
                Win1.document.writeln('                         qString = document.QueryStuff.QueryValue3.value;');
                Win1.document.writeln('                         if (fMap2.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 (fMap2.LayerFieldType[fNum].indexOf("91",0)!=-1) {');
                Win1.document.writeln('                                         qString = fMap2.formatDate(qString);');
                Win1.document.writeln('                                 }');
                Win1.document.write('                                   if ((fMap2.LayerFieldType[fNum].indexOf("4",0)!=-1) || ');
                Win1.document.write('                                   (fMap2.LayerFieldType[fNum].indexOf("8",0)!=-1) || ');
                Win1.document.write('                                   (fMap2.LayerFieldType[fNum].indexOf("6",0)!=-1) || ');
                Win1.document.write('                                   (fMap2.LayerFieldType[fNum].indexOf("5",0)!=-1)) {');
                Win1.document.writeln('                                         qString = fMap2.convertDecimal(qString);');
                Win1.document.writeln('                                 }');
                Win1.document.writeln('                                 if ((qString.length > 0) && (qString != \'""\'))');
                Win1.document.writeln('                                         document.QueryStuff.QueryString.value += qLineOperator + " (" + qField + " " + qOperator + " " + qString + ")";');
                
                //Quatrième ligne de recherche
                Win1.document.writeln('                         qField = document.QueryStuff.QueryField4.options[document.QueryStuff.QueryField4.selectedIndex].value;');
                Win1.document.writeln('                         qOperator = document.QueryStuff.QueryOperator4.options[document.QueryStuff.QueryOperator4.selectedIndex].value;');
                Win1.document.writeln('                         qLineOperator = document.QueryStuff.QueryLineOperator3.options[document.QueryStuff.QueryLineOperator3.selectedIndex].value;');
                Win1.document.writeln('                         fNum = document.QueryStuff.QueryField4.selectedIndex;');
                Win1.document.writeln('                         qString = document.QueryStuff.QueryValue4.value;');
                Win1.document.writeln('                         if (fMap2.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 (fMap2.LayerFieldType[fNum].indexOf("91",0)!=-1) {');
                Win1.document.writeln('                                         qString = fMap2.formatDate(qString);');
                Win1.document.writeln('                                 }');
                Win1.document.write('                                   if ((fMap2.LayerFieldType[fNum].indexOf("4",0)!=-1) || ');
                Win1.document.write('                                   (fMap2.LayerFieldType[fNum].indexOf("8",0)!=-1) || ');
                Win1.document.write('                                   (fMap2.LayerFieldType[fNum].indexOf("6",0)!=-1) || ');
                Win1.document.write('                                   (fMap2.LayerFieldType[fNum].indexOf("5",0)!=-1)) {');
                Win1.document.writeln('                                         qString = fMap2.convertDecimal(qString);');
                Win1.document.writeln('                                 }');
                Win1.document.writeln('                                 if ((qString.length > 0) && (qString != \'""\'))');
                Win1.document.writeln('                                         document.QueryStuff.QueryString.value += qLineOperator + " (" + qField + " " + qOperator + " " + qString + ")";');
                
                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('                                         alert(theString);'); 
                Win1.document.writeln('                                         parent.parent.parent.parent.MapFrame.sendQueryString(theString);'); 
                //Win1.document.writeln('                                         fMap2=null;'); 
                Win1.document.writeln('                                 }'); 
                Win1.document.writeln('                         } else {'); 
                Win1.document.writeln('                                 alert("' + msgList[110] + '");'); 
                Win1.document.writeln('                         }'); 
                Win1.document.writeln('                 }'); 
                
                Win1.document.writeln('                 function setUp(bRefresh) {');
                Win1.document.writeln('                                 parent.parent.parent.parent.MapFrame.fieldIndex1 = document.QueryStuff.QueryField.selectedIndex;');
                Win1.document.writeln('                                 parent.parent.parent.parent.MapFrame.fieldIndex2 = document.QueryStuff.QueryField2.selectedIndex;');
                Win1.document.writeln('                                 parent.parent.parent.parent.MapFrame.fieldIndex3 = document.QueryStuff.QueryField3.selectedIndex;');
                Win1.document.writeln('                                 parent.parent.parent.parent.MapFrame.fieldIndex4 = document.QueryStuff.QueryField4.selectedIndex;');
                Win1.document.writeln('                                 parent.parent.parent.parent.MapFrame.QueryOperator[0] = document.QueryStuff.QueryOperator.selectedIndex;');
                Win1.document.writeln('                                 parent.parent.parent.parent.MapFrame.QueryOperator[1] = document.QueryStuff.QueryOperator2.selectedIndex;');
                Win1.document.writeln('                                 parent.parent.parent.parent.MapFrame.QueryOperator[2] = document.QueryStuff.QueryOperator3.selectedIndex;');
                Win1.document.writeln('                                 parent.parent.parent.parent.MapFrame.QueryOperator[3] = document.QueryStuff.QueryOperator4.selectedIndex;');
                Win1.document.writeln('                                 parent.parent.parent.parent.MapFrame.QueryValue[0] = document.QueryStuff.QueryValue.value;');
                Win1.document.writeln('                                 parent.parent.parent.parent.MapFrame.QueryValue[1] = document.QueryStuff.QueryValue2.value;');
                Win1.document.writeln('                                 parent.parent.parent.parent.MapFrame.QueryValue[2] = document.QueryStuff.QueryValue3.value;');
                Win1.document.writeln('                                 parent.parent.parent.parent.MapFrame.QueryValue[3] = document.QueryStuff.QueryValue4.value;');
                Win1.document.writeln('                                 parent.parent.parent.parent.MapFrame.QueryLineOperator[0] = document.QueryStuff.QueryLineOperator.selectedIndex;');
                Win1.document.writeln('                                 parent.parent.parent.parent.MapFrame.QueryLineOperator[1] = document.QueryStuff.QueryLineOperator2.selectedIndex;');
                Win1.document.writeln('                                 parent.parent.parent.parent.MapFrame.QueryLineOperator[2] = document.QueryStuff.QueryLineOperator3.selectedIndex;');
//              Win1.document.writeln('                                 parent.parent.parent.parent.MapFrame.setQueryString = document.QueryStuff.QueryString.value;');
                Win1.document.writeln('                         if ((parent.parent.parent.parent.MapFrame.showSampleValues) && (bRefresh)) {');
                Win1.document.writeln('                                 var theField = fMap2.LayerFields[parent.parent.parent.parent.MapFrame.fieldIndex];');
//              Win1.document.writeln('                                 alert(theField);');
                Win1.document.writeln('                                 parent.parent.parent.parent.MapFrame.tempGetSamples2(theField);');
//              Win1.document.writeln('                                 fMap2.writeQueryForm2();');
                Win1.document.writeln('                         }');
                //Win1.document.writeln('                               addString();');                         
                Win1.document.writeln('                 }');
                
                Win1.document.writeln('                 function changeToSamples() {'); 
                Win1.document.writeln('                         parent.parent.parent.parent.MapFrame.fieldIndex1 = document.QueryStuff.QueryField.selectedIndex;');
                Win1.document.writeln('                         parent.parent.parent.parent.MapFrame.fieldIndex2 = document.QueryStuff.QueryField2.selectedIndex;');
                Win1.document.writeln('                         parent.parent.parent.parent.MapFrame.fieldIndex3 = document.QueryStuff.QueryField3.selectedIndex;');
                Win1.document.writeln('                         parent.parent.parent.parent.MapFrame.fieldIndex4 = document.QueryStuff.QueryField4.selectedIndex;');
                Win1.document.writeln('                         parent.parent.parent.parent.MapFrame.QueryOperator[0] = document.QueryStuff.QueryOperator.selectedIndex;');
                Win1.document.writeln('                         parent.parent.parent.parent.MapFrame.QueryOperator[1] = document.QueryStuff.QueryOperator2.selectedIndex;');
                Win1.document.writeln('                         parent.parent.parent.parent.MapFrame.QueryOperator[2] = document.QueryStuff.QueryOperator3.selectedIndex;');
                Win1.document.writeln('                         parent.parent.parent.parent.MapFrame.QueryOperator[3] = document.QueryStuff.QueryOperator4.selectedIndex;');
                Win1.document.writeln('                         parent.parent.parent.parent.MapFrame.QueryValue[0] = document.QueryStuff.QueryValue.value;');
                Win1.document.writeln('                         parent.parent.parent.parent.MapFrame.QueryValue[1] = document.QueryStuff.QueryValue2.value;');
                Win1.document.writeln('                         parent.parent.parent.parent.MapFrame.QueryValue[2] = document.QueryStuff.QueryValue3.value;');
                Win1.document.writeln('                         parent.parent.parent.parent.MapFrame.QueryValue[3] = document.QueryStuff.QueryValue4.value;');
                Win1.document.writeln('                         parent.parent.parent.parent.MapFrame.QueryLineOperator[0] = document.QueryStuff.QueryLineOperator.selectedIndex;');
                Win1.document.writeln('                         parent.parent.parent.parent.MapFrame.QueryLineOperator[1] = document.QueryStuff.QueryLineOperator2.selectedIndex;');
                Win1.document.writeln('                         parent.parent.parent.parent.MapFrame.QueryLineOperator[2] = document.QueryStuff.QueryLineOperator3.selectedIndex;');
                Win1.document.writeln('                         parent.parent.parent.parent.MapFrame.setQueryString = document.QueryStuff.QueryString.value;');
                Win1.document.writeln('                         parent.parent.parent.parent.MapFrame.tempGetSamples2(fMap2.LayerFields[fMap2.fieldIndex]);');
                Win1.document.writeln('                 }');
                Win1.document.writeln('                 function showTop() {'); 
                Win1.document.writeln('                         setTimeout("window.focus()",100);');
                Win1.document.writeln('                 }');
                                        
                Win1.document.writeln('         </script>');
                Win1.document.writeln('         </head>');
        
                Win1.document.writeln('         <body link="Blue" text="#FFFFCC" vlink="Blue" alink="Blue" leftmargin=0 topmargin=0 bgcolor="#1A599C" onload="' + fMap2 + '.queryOpen=true; showTop();" onunload="' + fMap2 + '.queryOpen=false;">');
                Win1.document.writeln('                 <form name="QueryStuff" onsubmit="sendQuery();return false;">');
                Win1.document.writeln('                 <TABLE BGCOLOR="#1A599C" BORDER=0 CELLSPACING=0 CELLPADDING=0>');
                Win1.document.writeln('                 <TR>');
                Win1.document.writeln('                   <TD VALIGN=TOP align=left>');
                Win1.document.writeln('                 <font face="Arial" size="-3">');
                Win1.document.writeln('                 <table border="0"');
                Win1.document.writeln('                 <tr><td><b>Recherche simple</b></td></tr>');
                Win1.document.writeln('                 <tr><td align="left" bgcolor="' + textFrameBackColor + '"><font face="Tahoma" size="-2"><b>' + msgList[72] + '</b></font></td>');
                Win1.document.writeln('                 </tr>');

                //Ligne numéro 1
                Win1.document.writeln('                 <tr><td align="left" bgcolor="' + textFrameBackColor + '">');
                        Win1.document.writeln('                 <select name="QueryField" onchange="setUp(true);">');
                        for (var i=0;i<LayerFieldCount;i++) {
                                Win1.document.write('<option value="' + LayerFields[i] + '"');
                                if (((i==fieldIndex) && (fieldIndex1 == 0)) || (i==fieldIndex1)) Win1.document.write(' selected');
                                // Put Field Alias in form if present
                                if (useFieldAlias) {
                                        alias=LayerFields[i];
                                        if (fieldAliasList.length>0)
                                                if (fieldAliasList[ActiveLayerIndex].length>0) {
                                                     var start=fieldAliasList[ActiveLayerIndex].indexOf(LayerFields[i]+':');
                                                     if (start != -1) {
                                                          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>');
                Win1.document.writeln('                 </td></tr></table><table border="0">');
                Win1.document.writeln('                 <tr><td align="left" bgcolor="' + textFrameBackColor + '"><font face="Tahoma" size="-2"><b>' + msgList[73] + '&nbsp;</b></font>');
                Win1.document.writeln('                 <font face="Tahoma" size="-2"><b>' + valueTitle + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></font>');
                Win1.document.writeln('                 <font face="Tahoma" size="-2"><b>' + msgList[123] + '</b></font></td>');
                Win1.document.writeln('                 </tr><tr></table><table border="0"><td align="left" bgcolor="' + textFrameBackColor + '">');
                Win1.document.writeln('                 <select name="QueryOperator" onchange="setUp(false);">');
                Win1.document.writeln(ajouterQueryOperator(0));
                Win1.document.writeln('                 </select>');
                     
                        
                if (showSampleValues) {
                        Win1.document.writeln('                 <select name="QueryValue">');
                                for (var i=0;i<SampleCount;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" value="' + QueryValue[0] + '" size="10" maxlength="1000" >');
                }         
                Win1.document.writeln('                 <select name="QueryLineOperator" onchange="setUp(false);">');
                
                Win1.document.writeln('                 <OPTION ');
                if (QueryLineOperator[0]==0) Win1.document.write('selected ');
                Win1.document.writeln('value="">');
                Win1.document.writeln('                 <OPTION ');
                if (QueryLineOperator[0]==1) Win1.document.write('selected ');
                Win1.document.writeln('value=" AND ">ET');
                Win1.document.write('                   <option ');
                if (QueryLineOperator[0]==2) Win1.document.write('selected ');
                Win1.document.writeln('value=" OR ">OU');
                
                Win1.document.writeln('                 </select>');
                Win1.document.writeln('                 </td><td align="CENTER" bgcolor="' + textFrameBackColor + '">');
                Win1.document.writeln('                 </td>');
                Win1.document.writeln('                 </tr>');

                //Ligne numéro 2
                Win1.document.writeln('                 <tr><td align="left" bgcolor="' + textFrameBackColor + '"><font face="Tahoma" size="-2"><b>' + msgList[72] + '</b></font></td>');
                Win1.document.writeln('                 </tr>');
                Win1.document.writeln('                 <tr><td align="left" bgcolor="' + textFrameBackColor + '">');
                        Win1.document.writeln('                 <select name="QueryField2" onchange="setUp(false);">');
                        for (var i=0;i<LayerFieldCount;i++) {
                                Win1.document.write('<option value="' + LayerFields[i] + '"');
                                if (i==fieldIndex2) Win1.document.write(' selected');
                                
                                // Put Field Alias in form if present
                                if (useFieldAlias) {
                                        alias=LayerFields[i];
                                        if (fieldAliasList.length>0)
                                                if (fieldAliasList[ActiveLayerIndex].length>0) {
                                                     var start=fieldAliasList[ActiveLayerIndex].indexOf(LayerFields[i]+':');
                                                     if (start != -1) {
                                                          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>');
                Win1.document.writeln('                 </td></tr></table><table border="0">');
                Win1.document.writeln('                 <tr><td align="left" bgcolor="' + textFrameBackColor + '"><font face="Tahoma" size="-2"><b>' + msgList[73] + '&nbsp;</b></font>');
                Win1.document.writeln('                 <font face="Tahoma" size="-2"><b>' + valueTitle + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></font>');
                Win1.document.writeln('                 <font face="Tahoma" size="-2"><b>' + msgList[123] + '</b></font></td>');
                Win1.document.writeln('                 </tr><tr></table><table border="0"><td align="left" bgcolor="' + textFrameBackColor + '">');
                Win1.document.writeln('                 <select name="QueryOperator2" onchange="setUp(false);">');
                Win1.document.writeln(ajouterQueryOperator(1));
                Win1.document.writeln('                 </select>');
                Win1.document.writeln('                 <input name="QueryValue2" value="' + QueryValue[1] + '" size="10" maxlength="1000" >');
                Win1.document.writeln('                 <select name="QueryLineOperator2" onchange="setUp(false);">');
                
                Win1.document.writeln('                 <OPTION ');
                if (QueryLineOperator[1]==0) Win1.document.write('selected ');
                Win1.document.writeln('value="">');
                Win1.document.writeln('                 <OPTION ');
                if (QueryLineOperator[1]==1) Win1.document.write('selected ');
                Win1.document.writeln('value=" AND ">ET');
                Win1.document.write('                   <option ');
                if (QueryLineOperator[1]==2) Win1.document.write('selected ');
                Win1.document.writeln('value=" OR ">OU');
                Win1.document.writeln('                 </select>');
                Win1.document.writeln('                 </td></tr>');
                
                //Ligne numéro 3
                Win1.document.writeln('                 <tr><td align="left" bgcolor="' + textFrameBackColor + '"><font face="Tahoma" size="-2"><b>' + msgList[72] + '</b></font></td>');
                Win1.document.writeln('                 </tr>');
                Win1.document.writeln('                 <tr><td align="left" bgcolor="' + textFrameBackColor + '">');
                        Win1.document.writeln('                 <select name="QueryField3" onchange="setUp(false);">');
                        for (var i=0;i<LayerFieldCount;i++) {
                                Win1.document.write('<option value="' + LayerFields[i] + '"');
                                if (i==fieldIndex3) Win1.document.write(' selected');
                                // Put Field Alias in form if present
                                if (useFieldAlias) {
                                        alias=LayerFields[i];
                                        if (fieldAliasList.length>0)
                                                if (fieldAliasList[ActiveLayerIndex].length>0) {
                                                     var start=fieldAliasList[ActiveLayerIndex].indexOf(LayerFields[i]+':');
                                                     if (start != -1) {
                                                          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>');
                Win1.document.writeln('                 </td></tr></table><table border="0">');
                Win1.document.writeln('                 <tr><td align="left" bgcolor="' + textFrameBackColor + '"><font face="Tahoma" size="-2"><b>' + msgList[73] + '&nbsp;</b></font>');
                Win1.document.writeln('                 <font face="Tahoma" size="-2"><b>' + valueTitle + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></font>');
                Win1.document.writeln('                 <font face="Tahoma" size="-2"><b>' + msgList[123] + '</b></font></td>');
                Win1.document.writeln('                 </tr><tr></table><table border="0"><td align="left" bgcolor="' + textFrameBackColor + '">');
                Win1.document.writeln('                 <select name="QueryOperator3" onchange="setUp(false);">');
                Win1.document.writeln(ajouterQueryOperator(2));
                Win1.document.writeln('                 </select>');
                Win1.document.writeln('                 <input name="QueryValue3" value="' + QueryValue[2] + '" size="10" maxlength="1000" >');
                Win1.document.writeln('                 <select name="QueryLineOperator3" onchange="setUp(false);">');
                Win1.document.writeln('                 <OPTION ');
                if (QueryLineOperator[2]==0) Win1.document.write('selected ');
                Win1.document.writeln('value="">');
                Win1.document.writeln('                 <OPTION ');
                if (QueryLineOperator[2]==1) Win1.document.write('selected ');
                Win1.document.writeln('value=" AND ">ET');
                Win1.document.write('                   <option ');
                if (QueryLineOperator[2]==2) Win1.document.write('selected ');
                Win1.document.writeln('value=" OR ">OU');
                Win1.document.writeln('                 </select>');
                Win1.document.writeln('                 </td></tr>');
                
                //Ligne numéro 4
                Win1.document.writeln('                 <tr><td align="left" bgcolor="' + textFrameBackColor + '"><font face="Tahoma" size="-2"><b>' + msgList[72] + '</b></font></td>');
                Win1.document.writeln('                 </tr>');
                Win1.document.writeln('                 <tr><td align="left" bgcolor="' + textFrameBackColor + '">');
                        Win1.document.writeln('                 <select name="QueryField4" onchange="setUp(false);">');
                        for (var i=0;i<LayerFieldCount;i++) {
                                Win1.document.write('<option value="' + LayerFields[i] + '"');
                                if (i==fieldIndex4) Win1.document.write(' selected');
                                
                                // Put Field Alias in form if present
                                if (useFieldAlias) {
                                        alias=LayerFields[i];
                                        if (fieldAliasList.length>0)
                                                if (fieldAliasList[ActiveLayerIndex].length>0) {
                                                     var start=fieldAliasList[ActiveLayerIndex].indexOf(LayerFields[i]+':');
                                                     if (start != -1) {
                                                          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>');
               Win1.document.writeln('                 </td></tr></table><table border="0">');
                Win1.document.writeln('                 <tr><td align="left" bgcolor="' + textFrameBackColor + '"><font face="Tahoma" size="-2"><b>' + msgList[73] + '&nbsp;</b></font>');
                Win1.document.writeln('                 <font face="Tahoma" size="-2"><b>' + valueTitle + '</b></font>');
                Win1.document.writeln('                 </tr><tr></table><table border="0"><td align="left" bgcolor="' + textFrameBackColor + '">');
                Win1.document.writeln('                 <select name="QueryOperator4" onchange="setUp(false);">');
                Win1.document.writeln(ajouterQueryOperator(3));
                Win1.document.writeln('                 </select>');
                Win1.document.writeln('                 <input name="QueryValue4" value="' + QueryValue[3] + '" size="10" maxlength="1000" >');
                Win1.document.writeln('                 </td></tr>');
                
                
                Win1.document.writeln('                 <tr><td align="center" bgcolor="' + textFrameBackColor + '">');
                Win1.document.writeln('                 <input type="hidden" name="QueryString" value=\'' + setQueryString + '\'>');
                
                //Win1.document.writeln('                 <a href="javascript:setUp(false);"><img src="' + appDir + 'images/bouton_executer.gif" border=0></a>'); 
                Win1.document.writeln('                 <input type="submit" onclick="setUp(false);addString();" value="' + buttonList[6] + '" name="submit">'); 
                Win1.document.writeln('                 </td></tr>');


                Win1.document.writeln('                 </table>');
                Win1.document.writeln('                 </font>');
                
                
                
                Win1.document.writeln('                   </td>');
                Win1.document.writeln('                 </tr>');
                Win1.document.writeln('                 </table>');
                Win1.document.writeln('                 </FORM></body></html>');
        
                Win1.document.close();
                Win1.focus();
                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] + parent.TopFrame.nomSousThemeLayer(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);

}

// temporarily change getSampleValues to true and load queryform2
function tempGetSamples2(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] + parent.TopFrame.nomSousThemeLayer(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,41);
}

// get the StoredQueries in the MapService
function getStoredQueries() {
        if (checkIfActiveLayerAvailable()) {
                var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_SERVICE_INFO renderer="false" extensions="true" fields="false" />\n';
                theString += '</REQUEST>\n</ARCXML>';
                sendToServer(imsQueryURL,theString,55);
        }
}

// get the StoredQueries in the MapService
function parseStoredQueries(theReply) {
        storedQueryCount=0;
        storedQueryName.length=1;
        storedQueryString.length=1;
        storedQueryVariable.length=1;
        storedQueryVarCount.length=1;
        storedQueryFieldList.length=1;
        storedQueryIndex=0;
        var endpos = 1;
        var pos = -1;
        var blurb = 'id="' + ActiveLayer;
        var startpos = theReply.indexOf("<LAYERINFO",1);
        var thePos = 1;
        while (thePos!=-1) {
                startpos = theReply.indexOf("<LAYERINFO",1);
                endpos = theReply.indexOf(">",startpos);
                thePos = theReply.indexOf(blurb,1);
                if (thePos!=-1) {
                        pos = thePos;
                        thePos=-1;
                } 
        }
        var sqpos = 0;
        var sqvpos = 0;
        var sqpos2 = 0;
        var vCount=0;
        if (pos!=-1) {
                var fieldCount = 0;
                epos = theReply.indexOf("</LAYERINFO>",pos);
                startpos=0;
                endpos=pos;
                sqpos = theReply.indexOf("<STOREDQUERY name=",endpos);
                sqpos2 = theReply.indexOf("</STOREDQUERY>",endpos);
                while ((epos>sqpos) && (sqpos!=-1)) {
                        
                        if (sqpos!=-1) {
                                startpos = sqpos + 19;
                                endpos = theReply.indexOf(dQuote, startpos);
                                blurb = theReply.substring(startpos,endpos);
                                //alert(blurb);
                                storedQueryName[storedQueryCount] = blurb;
                                
                                //startpos = theReply.indexOf("<QUERY where=",endpos);
                                var sp2 = theReply.indexOf("<QUERY ",endpos);
                                sp2 += 7;
                                startpos = theReply.indexOf("where=",sp2);
                                startpos += 7;
                                endpos = theReply.indexOf(dQuote, startpos);
                                blurb = theReply.substring(startpos,endpos);
                                storedQueryString[storedQueryCount] = blurb;
                                startpos = theReply.indexOf("subfields=",sp2); 
                                startpos += 11;
                                endpos = theReply.indexOf(dQuote, startpos);
                                storedQueryFieldList[storedQueryCount] = theReply.substring(startpos,endpos);
                                storedQueryVariable[storedQueryCount]="";                       
                                sqvpos = theReply.indexOf("<SQVAR",startpos);
                                vCount = 0;
                                while ((sqvpos!=-1) && (sqvpos<sqpos2)) {
                                        endpos = sqvpos;
                                        startpos = theReply.indexOf("name=",endpos);
                                        startpos += 6;
                                        endpos = theReply.indexOf(dQuote, startpos);
                                        blurb = theReply.substring(startpos,endpos);
                                        if (vCount > 0) blurb = "|" + blurb;
                                        storedQueryVariable[storedQueryCount] = storedQueryVariable[storedQueryCount] + blurb;
                                        vCount += 1;
                                        sqvpos = theReply.indexOf("<SQVAR",startpos);
                                }
                                storedQueryVarCount[storedQueryCount] = vCount;
                        
                                storedQueryCount += 1;
                        }
                        
                        
                        sqpos = theReply.indexOf("<STOREDQUERY name=",endpos);
                        sqpos2 = theReply.indexOf("</STOREDQUERY>",sqpos);
                }
        }
        blurb=null;
        if (storedQueryCount==0) {
                alert(parent.TopFrame.nomSousThemeLayer(LayerName[ActiveLayerIndex]) + msgList[77]);
        } else {
                storedQueryForm();
        }
}

// storedQuery Form
function storedQueryForm() {
        if (checkIfActiveLayerAvailable()) {
                writeStoredQueryForm(storedQueryIndex);
        }
}

function writeStoredQueryForm(theIndex) {
        storedQueryIndex=theIndex;
        if (storedQueryVarCount==0) {
                sendStoredQuery(theIndex,"");
        } else {
        
                if (useTextFrame) {
                        var Win1 = parent.TextFrame;
                        Win1.document.open();
                        var t = "parent.MapFrame";
                        
                } else {
                        var Win1 = open("","QueryWindow","width=575,height=150,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>');
                Win1.document.writeln('                 <script language="javascript">');
                Win1.document.writeln('var t = ' + t);
                
                Win1.document.writeln('function setStyle() {');
                Win1.document.writeln(' var f = document.forms[0];');
                Win1.document.writeln(' var theCount = t.storedQueryIndex;');
                Win1.document.writeln(' var okValue = 0;');
                Win1.document.writeln(' if (t.storedQueryVarCount[theCount]==1) {');
                Win1.document.writeln('         var theValue = f.theStyle.value;');
                Win1.document.writeln('         if (theValue!="") okValue = 1;');
                Win1.document.writeln(' } else {');
                Win1.document.writeln('         var theValue = f.theStyle[0].value;');
                Win1.document.writeln('         if (f.theStyle[0].value!="") okValue = 1;');
                Win1.document.writeln('         for (var i=1;i<t.storedQueryVarCount[theCount];i++) {');
                Win1.document.writeln('                 theValue = theValue + "|" + f.theStyle[i].value;');
                Win1.document.writeln('                 if (f.theStyle[i].value!="") okValue += 1;');
                Win1.document.writeln('         }');
                Win1.document.writeln(' }');
                Win1.document.writeln(' if (okValue==t.storedQueryVarCount[theCount]) {');
                Win1.document.writeln('         t.sendStoredQuery(theCount,theValue);');
                Win1.document.writeln(' } else {');
                Win1.document.writeln('         alert("Please enter appropriate value");');
                Win1.document.writeln(' }');
                Win1.document.writeln('}');
                Win1.document.writeln('function selectQuery() {');
                Win1.document.writeln(' var f = document.forms[0];');
                Win1.document.writeln(' var theIndex = f.theQuery.selectedIndex;');
                Win1.document.writeln(' t.writeStoredQueryForm(theIndex);');
                Win1.document.writeln('}');
                Win1.document.writeln('</script>');
                Win1.document.writeln('</head>');
                
                Win1.document.writeln('<body bgcolor="Black" text="White" topmargin=0 leftmargin=0 rightmargin=0 onload="window.focus()">');
                Win1.document.writeln('<div align="center">');
                Win1.document.writeln('<font face="Arial" size="-2"><b>');
                Win1.document.writeln('<form action="" name="theStoreQueryForm" onsubmit="setStyle(); return false;">');
                Win1.document.writeln('<table cellspacing="2" nowrap bgcolor="' + textFrameFormColor + '" width=100%>');
                Win1.document.writeln('<tr><th colspan="3" align="CENTER">');
                Win1.document.writeln('<font face="Arial" size="-1"><b>' + msgList[78] + '</b><br></font>');
                Win1.document.writeln('<font face="Arial" size="-2"><b>' + msgList[79] + '</b></font>');
                Win1.document.writeln('</th></tr>');
                Win1.document.writeln('<tr>');
                Win1.document.writeln('<td align="Left">');
                Win1.document.writeln('<font face="Arial" size="-1">' + parent.TopFrame.nomSousThemeLayer(LayerName[ActiveLayerIndex]));
                Win1.document.writeln('<select name="theQuery" onchange="selectQuery()">')
                for (var i=0;i<storedQueryCount;i++) {
                        Win1.document.write('   <option value=' + i);
                        if (i==storedQueryIndex) Win1.document.write(' selected');
                        Win1.document.writeln('>' + storedQueryName[i]);
                
                }
                Win1.document.writeln('</select>');
                Win1.document.writeln('</font></td>');
                Win1.document.writeln('<td align="RIGHT"><font face="Arial" size="-1">');
                for (var i=0;i<storedQueryVarCount[storedQueryIndex];i++) {
                        Win1.document.write(buttonList[2]);
                        if (storedQueryVarCount[storedQueryIndex]>1) Win1.document.write(' ' + (i+1));
                        Win1.document.writeln(':');
                        Win1.document.writeln('<input type=text name="theStyle" value=""><br>');
                }
                Win1.document.writeln('</font>');
                Win1.document.writeln('</td>');
                Win1.document.writeln('<td align="CENTER">');
                Win1.document.writeln('<input type="Button" name="setDU" value="' + buttonList[6] + '" onclick="setStyle()">  <br>');
                
                Win1.document.writeln('</td></tr></table></form>');
                Win1.document.writeln('</b></font></div>');
                Win1.document.writeln('</body></html>');
                Win1.document.close();
                Win1=null;
        
        }

}

// create the querystring for storedQuery and send it
function sendStoredQuery(theIndex,theValue) {
        showRetrieveData();
        var oldString = storedQueryString[theIndex];
        if (storedQueryVarCount[theIndex]>0) {
                var var1 = storedQueryVariable[theIndex].split("|");
                var var2 = theValue.split("|");
                for (var i=0;i<var1.length;i++) {
                        oldString = swapStuff(oldString,var1[i],var2[i]);
                }
        
        } else {
                oldString = swapStuff(oldString,storedQueryVariable[theIndex],theValue);
        }
        var newString = makeXMLsafe(oldString);
        setQueryString = newString;
        selectionMode=1;
        var theString = writeQueryXML(newString);
        sendToServer(imsQueryURL,theString,queryXMLMode);
}

// see if there are storedQueries (Search) 
function checkStoredQueries(theReply) {
        var startpos = theReply.indexOf("<STOREDQUERY",1);
        if (startpos==-1) {
                //no storedqueries. . . do not display button
                useStoredQuery=false;
        }
                
}

function writeStartQueryXML(queryString, idAndShapeOnly) {
	//alert("writeStartQueryXML");
        if (swapSelectFields) {
                selectFields=selFieldList[ActiveLayerIndex];
        }
        var theString = '<ARCXML VERSION="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" geometry="false" envelope="true"';
        theString += ' compact="true" featurelimit="' + maxFeaturesReturned + '" beginrecord="0">\n';
        theString += '<LAYER id="' + LayerID[ActiveLayerIndex] + '" />';
        if (idAndShapeOnly) {
                theString += '<QUERY subfields="#ID# #SHAPE#" where="' + queryString + '" />';
        } else {
                theString += '<QUERY subfields="' + selectFields + '" where="' + queryString + '" />';
        }
        theString += '</GET_FEATURES>';
        theString += '</REQUEST>';
        theString += '</ARCXML>';
        return theString;
        
}

function setStartQuery() {
        var theString = writeStartQueryXML(highlightedOne,true);
        sendToServer(imsQueryURL, theString, 20);
}

/*function parseStartQuery(theReply) {
         //alert(theReply);
         var theError = getXMLErrorMessage(theReply);
         if (theError=="") {
		if (queryZoom) {
		getXYs(theReply);
		startLeft = eLeft;
		startTop = eTop;
		startRight = eRight;
		startBottom = eBottom;
		var fWidth = eRight - eLeft;
		var fHeight = eTop - eBottom;
		var mWMargin = 0;
		var mHMargin = 0;
		//alert(LayerType[ActiveLayerIndex]);
		if (LayerType[ActiveLayerIndex]=="point") {	
			selectType="point";
			//alert("fullWidth=" + fullWidth + "\nfullHeight=" + fullHeight);
			//mWMargin = fullWidth * selectPointMargin;
			//mHMargin = fullHeight * selectPointMargin;
			mWMargin=40;
			mHMargin=40;
		
		} else {
			mWMargin = fWidth * selectMargin;
			mHMargin = fHeight * selectMargin; 
			if (mWMargin < 40) {mWMargin = 40}
			if (mHMargin < 40) {mHMargin = 40}
			//alert(selectMargin);
		}
		//alert("mWMargin=" + mWMargin + "\nmHMargin=" + mHMargin);
		eLeft = eLeft - mWMargin;
		eRight = eRight + mWMargin;
		eTop = eTop + mHMargin;
		eBottom = eBottom - mHMargin;
		startLeft = eLeft;
		startTop = eTop;
		startRight = eRight;
		startBottom = eBottom;
		parent.TOC2Frame.TOCFrame.afficherEtiquetteTheme('Voie de communication');
 	}
	// alert("ici");
	sendMapXML();  
	} else {
		alert(theError);
	}
}*/

function parseStartQuery(theReply) {
        var theError = getXMLErrorMessage(theReply);
        if (theError=="") {
                if (queryZoom) {
                        getXYs(theReply);
                        startLeft = eLeft;
                        startTop = eTop;
                        startRight = eRight;
                        startBottom = eBottom;
                        var fWidth = eRight - eLeft;
                        var fHeight = eTop - eBottom;
                        var mWMargin = 0;
                        var mHMargin = 0;
                        if (LayerType[ActiveLayerIndex]=="point") {     
                                selectType="point";
                                mWMargin = fullWidth * selectPointMargin;
                                mHMargin = fullHeight * selectPointMargin;
                        } else {
                                mWMargin = fWidth * selectMargin;
                                mHMargin = fHeight * selectMargin; 
                        }
                        eLeft = eLeft - mWMargin;
                        eRight = eRight + mWMargin;
                        eTop = eTop + mHMargin;
                        eBottom = eBottom - mHMargin;
                        startLeft = eLeft;
                        startTop = eTop;
                        startRight = eRight;
                        startBottom = eBottom;
                        
        
                }
                sendMapXML();
        } else {
                alert(theError);
        }
}
