// 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;

// 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 mustbeExact = LayerExactMatchesList.join();
         var fieldListString = LayerIDField[ActiveLayerIndex] + " " + LayerShapeField[ActiveLayerIndex];
         var elemCount = 0;
         for (var i=0;i<LayerFields.length;i++) {

          if (LayerFieldType[i].indexOf("12",0)!=-1) {  // is string
            if (mustbeExact.search(LayerName[ActiveLayerIndex]) != -1) {
                 theTempString = "(" + LayerFields[i] + " = '" + theValue + "')";                 
                 theTempString = "(" + LayerFields[i] + " =&apos;" + theValue + "&apos;)";
               }
               else {
                    theTempString = "UPPER(" + 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;
            }
          }else if (LayerFieldType[i].indexOf("-99",0)!=-1) { // is #id#
             theTempString = "(" + LayerFields[i] + " = '" + theValue + "')";                 
//             theNewQueryString = "(" + LayerFields[i] + " =&apos;" + theValue + "&apos;)";
             theNewQueryString = "(" + LayerFields[i] + " = " + theValue + ")";
alert(theTempString);
// NOTE FIRST LINE NOT USED
          }
        }
    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) {
    var theString = '<?xml version="1.0" encoding="UTF-8"?><ARCXML VERSION="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" geometry="false" envelope="true"';
    theString += ' compact="true" 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="' + limitRight + '" maxy="' + limitTop + '" minx="' + limitLeft + '" miny="' + 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;
}

