// aimsSelect.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*       dependent on aimsXML.js, ArcIMSparam.js, aimsCommon.js, aimsMap.js, aimsIdentify, and aimsLayers.js
*       To be interactive, dependent also on aimsDHTML.js, aimsClick.js, and aimsNavigation.js
*/

aimsSelectPresent=true;

var selectData = new Array();
var selectLeft = new Array();
var selectRight = new Array();
var selectTop = new Array();
var selectBottom = new Array();
var selMaxEnvelope = new Array();

var selectLayer="";
var selectType = "";
var selectCount = 0;
var selectBlurb = "";
var selectEnvelope="";
var highlightedOne = "";
    //mode - 0=selection; 1=query
var queryMode=1;
    //mode - 1=query; 2=box,point; 3=line,polygon
var selectionMode=1;
var setQueryString="";

var shapeBufferDistance = 0;

// Added by cord to store the currently selected parcel - this is a comma-separated list of key PARCEL feature IDs
var currentlySelectedFeatureID="";
var currentSelectionState = 0;                  // Selection states:  0 = new select, 1 = add to select, 2 = remove from select
var highliteSelect = 0;                            // Whether to make a border around the selection set.  0 = yes.

/*
***************************************************************************************

Selection functions

***************************************************************************************
*/

// select feature
function select(e) {

    //getImageXY(e);
    getMapXY(mouseX,mouseY);
    searchTolerance = (xDistance/iWidth) * pixelTolerance;
    var tempWest = mapX - searchTolerance;
    var tempNorth = mapY + searchTolerance;
    var tempEast = mapX + searchTolerance;
    var tempSouth = mapY - searchTolerance;
    queryStartRecord=1;
    selectEnvelope='maxy="' + forceComma(tempNorth) + '" maxx="' + forceComma(tempEast) + '" miny="' + forceComma(tempSouth) + '" minx="' + forceComma(tempWest) + '"';
    selectionMode=2;
    showRetrieveData();
    var theString = writeGetFeatures2(tempWest,tempSouth,tempEast,tempNorth);
    sendToServer(imsQueryURL,theString,selectXMLMode);
}

// start select box display
// Replacing functionality of the Identify with the select by box -
// replace the first IF statement which required that the ActiveMapLayer be visible - to the parcel layer as the identify
function startSelectBox(e) {
    if (aimsDBPresent) {
        matchDBLinkLayer(dbLinkLayer);
    //if (checkIfActiveLayerAvailable()) {
        moveLayer("theMap",hspc,vspc);
        getImageXY(e);
        // keep it within the MapImage
        if ((mouseX<iWidth) && (mouseY<iHeight)) {
            if (selectBox) {
                stopSelectBox(e);
            } else {
                x1=mouseX;
                y1=mouseY
                x2=x1+1;
                y2=y1+1;
                zleft=x1;
                ztop=y1;
                zbottom=y1;
                zright=x1
                boxIt(x1,y1,x2,y2);
                selectBox=true;

                //if (isNav4) {
                    showLayer("zoomBoxTop");
                    showLayer("zoomBoxLeft");
                    showLayer("zoomBoxRight");
                    showLayer("zoomBoxBottom");
                //} else {
                //  showLayer("zoomBox");
                //}
            }
            highlightedOne="";
        }
        return false;
    }

}

// stop select box display..... select
function stopSelectBox(e) {
    selectBox=false;
	
		

    hideLayer("zoomBoxTop");
    hideLayer("zoomBoxLeft");
    hideLayer("zoomBoxRight");
    hideLayer("zoomBoxBottom");

    if ((zright <zleft+2) && (zbottom < ztop+2)) {

        //getMapXY(mouseX-pixelTolerance,mouseY-pixelTolerance);
        getMapXY(mouseX,mouseY);
        searchTolerance = (xDistance/iWidth) * pixelTolerance;
        tempLeft = mapX-searchTolerance;
        tempTop = mapY-searchTolerance;
        tempRight = mapX+searchTolerance;
        tempBottom = mapY+searchTolerance;

        //select(e);
    } else {
    // */
        pixelX = xDistance / iWidth;
        var theY = iHeight - ztop;
        pixelY = yDistance / iHeight;
        tempTop = pixelY * theY + eBottom;
        tempRight = pixelX * zright + eLeft;
        tempLeft = pixelX * zleft + eLeft;
        theY = iHeight - zbottom;
        pixelY = yDistance / iHeight;
        tempBottom = pixelY * theY + eBottom;
    }
        window.scrollTo(0,0);

    queryStartRecord=1;
    setQueryString="";
    var theString = writeGetFeatures2(tempLeft,tempBottom,tempRight,tempTop);
    selectEnvelope='maxy="' + forceComma(tempTop) + '" maxx="' + forceComma(tempRight) + '" miny="' + forceComma(tempBottom) + '" minx="' + forceComma(tempLeft) + '"';
    drawSelectBoundary=true;
    showBuffer=false; showAbutterAbutter=false;
    selectionMode=2;
    sendToServer(imsQueryURL,theString,selectXMLMode);
    return true;

}

// write out XML request to select features
function writeGetFeatures2(west1,south1,east1,north1) {
    if (swapSelectFields) {
        selectFields=selFieldList[ActiveLayerIndex];
        //alert(selectFields);
    }
    var theEnvelope = 'maxy="' + forceComma(north1) + '" maxx="' + forceComma(east1) + '" miny="' + forceComma(south1) + '" minx="' + forceComma(west1 )+ '"';
    var theString = writeEnvelopeXML(ActiveLayer,ActiveLayerType,selectFields,maxFeaturesReturned,queryStartRecord,theEnvelope,useLimitExtent);
    //alert(theString);
    selectLayer=ActiveLayer;
    selectType=ActiveLayerType;
    selectCount=0;
    hightlightedOne="";
    selectPoints.length=1;
    selectLeft.length=1;
    selectRight.length=1;
    selectTop.length=1;
    selectBottom.length=1;
    return theString;
}

function writeGetFeatures3() {
    if (swapSelectFields) {
        selectFields=selFieldList[ActiveLayerIndex];
    }
    var theString = writeEnvelopeXML(ActiveLayer,ActiveLayerType,selectFields,maxFeaturesReturned,queryStartRecord,selectEnvelope,useLimitExtent);
    //alert(theString);
    selectLayer=ActiveLayer;
    selectType=ActiveLayerType;
    selectCount=0;
    hightlightedOne="";
    selectPoints.length=1;
    selectLeft.length=1;
    selectRight.length=1;
    selectTop.length=1;
    selectBottom.length=1;
    return theString;
}

// generic envelope select xml write routine
function writeEnvelopeXML(theLayer,theLayerType,theFields,maxReturned,startRec,theEnvelope,hasLimit) {
    var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" envelope="true" geometry="false"';
    theString += ' featurelimit="' + maxReturned + '" beginrecord="' + startRec + '">\n';
    theString += '<LAYER id="' + theLayer + '" />';
    theString += '<SPATIALQUERY subfields="' + theFields + '">';
    //theString += '<SPATIALFILTER relation=envelope_intersection >';
    theString += '<SPATIALFILTER relation="area_intersection" >';
    theString += '<ENVELOPE ' + theEnvelope + ' />';
    theString += '</SPATIALFILTER>';
    if (hasLimit) {
        // keep this within the limitExtent
        theString += '<SPATIALFILTER relation="area_intersection">\n';
        theString += '<ENVELOPE maxx="' + forceComma(limitRight) + '" maxy="' + forceComma(limitTop) + '" minx="' + forceComma(limitLeft) + '" miny="' + forceComma(limitBottom) + '" />\n';
        theString += '</SPATIALFILTER>\n';
    }
    theString += '</SPATIALQUERY>';
    theString += '</GET_FEATURES>';
    theString += '</REQUEST>';
    theString += '</ARCXML>';
    return theString;

}

// write out xml request for selection by shape
function writeShapeSelect(theType) {
    if (swapSelectFields) {
        selectFields=selFieldList[ActiveLayerIndex];
    }
    var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" envelope="true" geometry="false"';
    theString += ' featurelimit="' + maxFeaturesReturned + '" beginrecord="' + queryStartRecord + '">\n';
    theString += '<LAYER id="' + ActiveLayer + '" />';
    theString += '<SPATIALQUERY subfields="' + selectFields + '">';
    theString += '<SPATIALFILTER relation="area_intersection" >';
	if ((shapeSelectBuffer) && (shapeBufferDistance>0)||((use_jsgraphics)&&(toolMode==22))) {
		// do a buffer around the shape before selecting
		
		theString += '<BUFFER distance="' + forceComma(shapeBufferDistance) + '" ';
		
		theString += ' bufferunits="' + ScaleBarUnits + '"';
		theString += ' />\n';
	}
    if (theType==1) {
        theString += '<POLYLINE>\n<PATH>\n';
    } else if (theType==2) {
        theString += '<POLYGON>\n<RING>\n';
    } else {
        theString += '<MULTIPOINT>\n';
    }
	if (theType==0) {
		if ((use_jsgraphics)&&(toolMode==22)) theString += '<POINT x="' + circleCenterX + '" y="' + circleCenterY + '" />\n';
		else theString += '<POINT x="' + clickPointX[clickCount-1] + '" y="' + clickPointY[clickCount-1] + '" />\n';
	} else {
        for (var i=0;i<clickCount;i++) {
            theString += '<POINT x="' + clickPointX[i] + '" y="' + clickPointY[i] + '" />\n';
        }
    }
    if (theType==0) {
        theString += '</MULTIPOINT>\n';
    } else if (theType==2) {
        theString += '</RING>\n</POLYGON>\n';
    } else {
        theString += '</PATH>\n</POLYLINE>\n';
    }
    theString += '</SPATIALFILTER>';
    if (useLimitExtent) {
        // keep this within the limitExtent
        theString += '<SPATIALFILTER relation="area_intersection">\n';
        theString += '<ENVELOPE maxx="' + forceComma(limitRight) + '" maxy="' + forceComma(limitTop) + '" minx="' + forceComma(limitLeft) + '" miny="' + forceComma(limitBottom) + '" />\n';
        theString += '</SPATIALFILTER>\n';
    }
    theString += '</SPATIALQUERY>';
    theString += '</GET_FEATURES>';
    theString += '</REQUEST>';
    theString += '</ARCXML>';
    selectLayer=ActiveLayer;
    selectType=ActiveLayerType;
    selectCount=0;
    hightlightedOne="";
    selectPoints.length=1;
    selectLeft.length=1;
    selectRight.length=1;
    selectTop.length=1;
    selectBottom.length=1;
    return theString;

}

// request a selection using a shape
function sendShapeSelect(theType) {
    queryStartRecord=1;
    selectionMode=3;
    showBuffer=false; showAbutterAbutter=false;
    if (theType==2) {
        if ((clickPointX[0]!=clickPointX[clickCount-1]) && (clickPointY[0]!=clickPointY[clickCount-1])) {
            clickPointX[clickCount]=clickPointX[0];
            clickPointY[clickCount]=clickPointY[0];
            clickCount=clickCount+1;
        }
    }
    showRetrieveData();
    var theString = writeShapeSelect(theType);
    sendToServer(imsQueryURL,theString,selectXMLMode);
}

// request more records to display
function getMoreData(startRecord) {
    var theString = "";
    queryStartRecord = startRecord;
    if (selectionMode==1) {
        //query
        theString = writeQueryXML(setQueryString);
    } else if (selectionMode==2) {
        theString = writeGetFeatures3();
    } else {
        var theType = clickType-1;
        theString = writeShapeSelect(theType);
    }
    showRetrieveData();
    sendToServer(imsQueryURL,theString,selectXMLMode);
}

// refresh map with hightlighted selection
function showHighlight(selNum) {
        highlightedOne = LayerIDField[ActiveLayerIndex] + " = " + selectPoints[selNum];
        //alert(selectLeft[selNum] + "," + selectRight[selNum]);
        var fWidth = selectRight[selNum] - selectLeft[selNum];
        var fHeight = selectTop[selNum] - selectBottom[selNum];
        var mWMargin = 0;
        var mHMargin = 0;
        if (selectType=="point") {
            mWMargin = fullWidth * selectPointMargin;
            mHMargin = fullHeight * selectPointMargin;
            if (mWMargin > xDistance/2) {
                mWMargin = xDistance/2;
                mHMargin = yDistance/2;
            }
        } else {
            mWMargin = fWidth * selectMargin;
            mHMargin = fHeight * selectMargin;
        }
        saveLastExtent();
        eLeft = selectLeft[selNum] - mWMargin;
        eRight = selectRight[selNum] + mWMargin;
        eTop = selectTop[selNum] + mHMargin;
        eBottom = selectBottom[selNum] - mHMargin;
        //alert(highlightedOne);
        legendTemp=legendVisible;
        legendVisible=false;
        layersVisible=false;
        sendMapXML();

}

// calculate max envelope for returned records
function calcSelectEnvelope() {
    //alert(selectCount);
    if (selectCount>0) {
        selMaxEnvelope[0] = selectLeft[0];
        selMaxEnvelope[1] = selectBottom[0];
        selMaxEnvelope[2] = selectRight[0];
        selMaxEnvelope[3] = selectTop[0];
        //alert("0 - " + selMaxEnvelope[0] + "," + selMaxEnvelope[1] + "," + selMaxEnvelope[2] + "," + selMaxEnvelope[3]);
        if (selectCount>1) {
            for (var i=1;i<selectCount;i++) {
                if (selectLeft[i]<selMaxEnvelope[0]) selMaxEnvelope[0] = selectLeft[i];
                if (selectBottom[i]<selMaxEnvelope[1]) selMaxEnvelope[1] = selectBottom[i];
                if (selectRight[i]>selMaxEnvelope[2]) selMaxEnvelope[2] = selectRight[i];
                if (selectTop[i]>selMaxEnvelope[3]) selMaxEnvelope[3] = selectTop[i];
                //alert(i + " - " + selMaxEnvelope[0] + "," + selMaxEnvelope[1] + "," + selMaxEnvelope[2] + "," + selMaxEnvelope[3]);
            }
        }
    }
}

function zoomToReturnedRecords() {
    calcSelectEnvelope();
    var fWidth = selMaxEnvelope[2] - selMaxEnvelope[0];
    var fHeight = selMaxEnvelope[3] - selMaxEnvelope[1];
    var mWMargin = 0;
    var mHMargin = 0;
    if ((fWidth==0) && (fHeight==0)) {
        mWMargin = fullWidth * selectPointMargin;
        mHMargin = fullHeight * selectPointMargin;
        if (mWMargin > xDistance/2) {
            mWMargin = xDistance/2;
            mHMargin = yDistance/2;
        }
    } else {
        mWMargin = fWidth * selectMargin;
        mHMargin = fHeight * selectMargin;
    }
    saveLastExtent();
    eLeft = selMaxEnvelope[0] - mWMargin;
    eRight = selMaxEnvelope[2] + mWMargin;
    eTop = selMaxEnvelope[3] + mHMargin;
    eBottom = selMaxEnvelope[1] - mHMargin;
    sendMapXML();
}

// clear current selection
function clearSelection() {
    var theCount = selectCount;
    var theHL = highlightedOne;
    selectCount=0;
    showBuffer=false; showAbutterAbutter=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;
	currentArea=0;
    selectBlurb="";
    sendMapXML();

}

// add Draw Selected Features to Map XML request
function addSelectToMap(){


if (goTo) {
dbLinkLayer = zoomGoToName;
}else if (findStreet) {
dbLinkLayer = dbRoadLinkLayer;
}else if (idFeat) {
} else {
dbLinkLayer = "Parcels";
//dbLinkLayer = "Parcels Clear";
selectionMode = 1;
highlightedOne = 0;
}


//alert (" In aimsSelect dbLinklayer = " + dbLinkLayer);
matchDBLinkLayer(dbLinkLayer);

parent.MapFrame.goTo = false;
parent.MapFrame.idFeat = false;
parent.MapFrame.findStreet = false;




//alert ("Selection Mode : " + selectionMode);


    var selString = "";
    var spatialQueryString = "";
   // if (selectCount > 0)
   //    parent.Debugger.document.write("aimsSelect.addSelectToMap: Highlite: " + highlightedOne +"QueryString:" + setQueryString +" Sel Mode: " + selectionMode + "CT: " + clickType + "  Sel Count: " + selectCount + "<br>");

    var sFactor = (eRight-eLeft) / iWidth
    if (((sFactor>=LayerMinScale[ActiveLayerIndex]) && (sFactor<=LayerMaxScale[ActiveLayerIndex]) && (LayerVisible[ActiveLayerIndex]==1)) || (canSelectInvisible)) {
        //alert ("Select Count  :" + selectCount);
		//alert ("Show Selected Features  :" + showSelectedFeatures);
		if ((selectCount>0) && (showSelectedFeatures)) {
            selString +='<LAYER type="featureclass" name="Selected Features" visible="true">\n';
            selString +='<DATASET fromlayer="' + LayerID[ActiveLayerIndex] + '" />\n';
            if (selectionMode==1) {
			    //alert ("Sel Mode is 1");
                selString +='<SPATIALQUERY where="' + setQueryString + '"';
                if (useLimitExtent) {
                    // keep this within the limitExtent

                    selString +='>\n<SPATIALFILTER relation="area_intersection">\n';
                    selString +='<ENVELOPE maxx="' + forceComma(limitRight) + '" maxy="' + forceComma(limitTop) + '" minx="' + forceComma(limitLeft) + '" miny="' + forceComma(limitBottom) + '" />\n';
                    selString +='</SPATIALFILTER>\n';
                    selString +='</SPATIALQUERY>\n';
                } else {
                selString +='/>\n';
                }

            } else {
			   // alert ("In Else");
                spatialQueryString +='<SPATIALQUERY>\n';
                spatialQueryString +='<SPATIALFILTER relation="area_intersection" >\n';
                if (selectionMode==2) {
                    spatialQueryString +='<ENVELOPE ' + selectEnvelope + ' />\n';
                } else {
					if (((shapeSelectBuffer) && (shapeBufferDistance>0))||(toolMode==22)) {
                        spatialQueryString += '<BUFFER distance="' + forceComma(shapeBufferDistance) + '" ';
                        spatialQueryString += ' bufferunits="' + ScaleBarUnits + '"';
                        spatialQueryString += ' />\n';
                    }
                    if (clickType==2) {
                        spatialQueryString +='<POLYLINE>\n<PATH>\n';
                    } else if (clickType==3) {
                        spatialQueryString +='<POLYGON>\n<RING>\n';
                    } else {
                        spatialQueryString +='<MULTIPOINT>\n';
                    }
					if (clickType==1) {
						if ((use_jsgraphics)||(toolMode==22)) selString +='<POINT x="' + circleCenterX + '" y="' + circleCenterY + '" />\n';
						else selString +='<POINT x="' + clickPointX[clickCount-1] + '" y="' + clickPointY[clickCount-1] + '" />\n';
					} else {
                        for (var i=0;i<clickCount;i++) {
                            spatialQueryString +='<POINT x="' + clickPointX[i] + '" y="' + clickPointY[i] + '" />\n';
                        }
                    }
                    if (clickType==3) {
                        //selString +='<POINT x="' + clickPointX[0] + '" y="' + clickPointY[0] + '" />\n';
                        spatialQueryString +='</RING>\n</POLYGON>\n';
                    } else if (clickType==2) {
                        spatialQueryString +='</PATH>\n</POLYLINE>\n';
                    } else {
                        spatialQueryString +='</MULTIPOINT>\n';
                    }
                }
                spatialQueryString +='</SPATIALFILTER>\n';
                ///*
                if (useLimitExtent) {
                    // keep this within the limitExtent
                    spatialQueryString +='<SPATIALFILTER relation="area_intersection">\n';
                    spatialQueryString +='<ENVELOPE maxx="' + forceComma(limitRight) + '" maxy="' + forceComma(limitTop) + '" minx="' + forceComma(limitLeft) + '" miny="' + forceComma(limitBottom) + '" />\n';
                    spatialQueryString +='</SPATIALFILTER>\n';
                }
                //*/
                spatialQueryString +='</SPATIALQUERY>\n';
                selString += spatialQueryString;
            }
            selString +='<SIMPLERENDERER>\n';

            if (selectType=="point") {
                selString +='<SIMPLEMARKERSYMBOL color="' + selectColor + '" type="Circle" width="10" />\n';
            } else if (selectType=="line") {
                selString +='<SIMPLELINESYMBOL type="SOLID" color="' + selectColor + '" width="3" />\n';
            } else {
                //selString +='<SIMPLEPOLYGONSYMBOL FILLCOLOR="' + selectColor + '" FILLTYPE="Gray" ';
                selString +='<SIMPLEPOLYGONSYMBOL fillcolor="' + selectColor + '" filltype="solid" transparency="' + forceComma(transparentLevel) + '" boundarycolor="255,255,255" />\n';
            }
            selString +='</SIMPLERENDERER>\n';
            selString +='</LAYER>\n';
        }

        if (selectBlurb!="") {
		       
            // add SelectBlurb to Map XML request
            selString +='<LAYER type="featureclass" name="Selected Features" visible="true">\n';
            //selString +='<DATASET fromlayer="' + ActiveLayer + '" />\n';
            selString +='<DATASET fromlayer="' + LayerID[ActiveLayerIndex] + '" />\n';
            selString +='<QUERY where="' + selectBlurb + '" />\n';
            selString +='<SIMPLERENDERER>\n';

            if (selectType=="point") {
                selString +='<SIMPLEMARKERSYMBOL color="' + selectColor + '" type="Circle" width="10" />\n';
            } else if (selectType=="line") {
                selString +='<SIMPLELINESYMBOL type="SOLID" color="' + selectColor + '" width="3" />\n';
            } else {
                selString +='<SIMPLEPOLYGONSYMBOL fillcolor="' + selectColor + '" filltype="solid" transparency="' + forceComma(transparentLevel) + '" boundarycolor="255,255,255" />\n';
                }
            selString +='</SIMPLERENDERER>\n';
            selString +='</LAYER>\n';
        }

       // alert ("highlighted one: " + highlightedOne + "showSelectedFeatures: " + showSelectedFeatures);
	   
        // parent.Debugger.document.write("aimsSelect.addSelectToMap: Highlite: " + highlightedOne + " hS: "  + highliteSelect  + " sQs: " + setQueryString + "<br>");
        //if ((highlightedOne!="") || ((highliteSelect == 0) &&  ((selectCount>0) && (showSelectedFeatures)) )) {
        if ((highlightedOne!="")) {
            // add Draw Highlighed Feature to Map XML request
            selString +='<LAYER type="featureclass" name="Highlighted Feature" visible="true">\n';
            //selString +='<DATASET fromlayer="' + ActiveLayer + '" />\n';
            selString +='<DATASET fromlayer="' + LayerID[ActiveLayerIndex] + '" />\n';
//            if (highlightedOne!="")
//			
//                selString +='<QUERY where="' + highlightedOne + '" />\n';
//				
//            else if (setQueryString.length > 0) {
//			    
//                selString += '<QUERY where="' + setQueryString + '" />\n';
//            } else {
//			     
//                selString+= spatialQueryString;
//            }

            //SPECIAL CASE for street name search - patch for IMS 9.2
			// error when XML request contains character "<"
			if (highlightedOne != "" )
            {
                if (highlightedOne.indexOf("<") >= 0)
                    selString += '<QUERY where="' + setQueryString + '" />\n';
                else
                    selString +='<QUERY where="' + highlightedOne + '" />\n';
            }
			else
			{
			    selString +='<QUERY where="' + highlightedOne + '" />\n';
			}
            selString +='<SIMPLERENDERER>\n';
		

            if (selectType=="point") {
                selString +='<SIMPLEMARKERSYMBOL color="' + highlightColor + '" type="Circle" width="10" />\n';
            } else if (selectType=="line") {
                selString +='<SIMPLELINESYMBOL type="SOLID" color="' + highlightColor + '" width="3" />\n';
            } else {
                // BELOW IS THE CODE THAT MAKES THE HIGHLIGHT A RED OUTLINE
                selString +='<SIMPLEPOLYGONSYMBOL boundarytransparency="1.0" filltransparency="0.0" boundarywidth="4" boundarycaptype="round" boundarycolor="255,0,0" />\n';
            }
            selString +='</SIMPLERENDERER>\n';
            selString +='</LAYER>\n';
        }
    }
    //if (selString.length > 0)
   //parent.Debugger.document.write ("aimsSelect.addToSelect: " + highlightedOne + "<br>" + setQueryString + "<br>");
   //alert(selString);
    return selString;
}

/*
 * Adds the new IDs passed in to the list of currently selected IDs.  Checks to make sure that
 *  as it adds each ID, that it is not already in the list.
 *  @param newIDs The new IDs to add to the already selected list
 */
function addIDsToSelectedList(newIDs) {
    var ili = 0;
    var ilm = 0;
    var idExists = false;

    var splitCurrentIDs = currentlySelectedFeatureID.split(",");
    var splitNewIDs = newIDs.split(",");
    for (ili = 0; ili < splitNewIDs.length; ili ++) {
        for (ilm = 0; ilm < splitCurrentIDs.length; ilm++) {
            if (splitNewIDs[ili] == splitCurrentIDs[ilm]) {
                idExists = true;
                break
            }
        }
        if (! idExists)
            splitCurrentIDs[splitCurrentIDs.length] = splitNewIDs[ili];
        idExists = false;
    }
    currentlySelectedFeatureID = splitCurrentIDs.join(",");
    setQueryString = mapIDField + " IN (" + currentlySelectedFeatureID + ")";
    return 0;
}

/*
 *  Removes the listed IDs passed in from the list of currently selected IDs.
 *  @param removeIDs The new IDs to add to the already selected list
 */
function removeIDsFromSelectedList(removeIDs) {
    var ili = 0;
    var ilm = 0;
    var splitCurrentIDs = currentlySelectedFeatureID.split(",");
    var splitRemoveIDs = removeIDs.split(",");
    for (ili = 0; ili < splitRemoveIDs.length; ili ++) {
        for (ilm = 0; ilm < splitCurrentIDs.length; ilm++) {
            if (splitRemoveIDs[ili] == splitCurrentIDs[ilm]) {
                splitCurrentIDs[ilm] = "-999999";
                break
            }
        }
    }

    // Recreate list of IDs - those whose value is not -999999
    currentlySelectedFeatureID = "";
    for (ilm = 0; ilm < splitCurrentIDs.length; ilm++) {
        if (splitCurrentIDs[ilm] != "-999999")
            currentlySelectedFeatureID += splitCurrentIDs[ilm] + ","
    }

    currentlySelectedFeatureID = currentlySelectedFeatureID.substring(0,currentlySelectedFeatureID.length - 1);
    setQueryString = mapIDField + " IN (" + currentlySelectedFeatureID + ")";
    return 0;
}
