// aimsSelect.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*		dependent on aimsXML.js, ArcIMSparam.js, aimsCommon.js, aimsMap.js,
*		aimsLayers.js, aimsDHTML.js,
*		aimsClick.js, aimsNavigation.js,
*		aimsIdentify.js
*/
var aimsSelectPresent=true;

//var selectIds = new Array();
var selectData = new Array();
//var selectRecs = new Array();
var selectLeft = new Array();
var selectRight = new Array();
var selectTop = new Array();
var selectBottom = 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	selectDrawBoundary = "";
var allowDrawSelectBoundary = false;

//newly added since 3.1
var selMaxEnvelope = new Array();

/*
***************************************************************************************

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;
	/*
	if (showSelectedData) {
		if (useTextFrame) {
			parent.TextFrame.document.open();
			parent.TextFrame.document.writeln('<html>');
			parent.TextFrame.document.writeln('<body bgcolor="' + textFrameBackColor + '" text="Black" link="Blue" vlink="Blue" LEFTMARGIN=0>');
			parent.TextFrame.document.writeln('<FONT FACE="Arial" SIZE="-1"><b>' + LayerName[ActiveLayerIndex] + '</b><br>');
			parent.TextFrame.document.writeln('This may take considerable time to write out the data if many features have been selected. "');
			parent.TextFrame.document.writeln('</body></html>');
			parent.TextFrame.document.close();
		}
	}
	*/
	queryStartRecord=0;
	//selectDrawBoundary = tempWest + "," + tempNorth + "," + tempEast + "," + tempNorth + "," + tempEast + "," + tempSouth + "," + tempWest + "," + tempSouth + "," + tempWest + "," + tempNorth;
	selectDrawBoundary = tempWest + " " + tempNorth + " " + tempEast + " " + tempNorth + " " + tempEast + " " + tempSouth + "," + tempWest + "," + tempSouth + "," + tempWest + "," + tempNorth;
	selectEnvelope='maxy="' + tempNorth + '" maxx="' + tempEast + '" miny="' + tempSouth + '" minx="' + tempWest + '"';
	selectionMode=2;
	showRetrieveData();
	var theString = writeGetFeatures2(tempWest,tempSouth,tempEast,tempNorth);
	sendToServer(imsQueryURL,theString,selectXMLMode);
}

// start select box display
function startSelectBox(e) {
      
       var frmQuery = parent.parent.frames["query"];
       
       if (parseInt(frmQuery.getFeatureClass())==99)
       {
	   alert("請先選擇主題!");
	   return false;
       }
       frmQuery.setSearchType(2);
	
	
	moveLayer("theMap",hspc,vspc);
	getImageXY(e);
	// keep it within the MapImage
	if ((mouseX<iWidth) && (mouseY<iHeight)) {
		if (selectBox) {
			stopSelectBox(e);
		} else {
			x1=mouseX;
			y1=mouseY
			x2=x1+1;
			y2=y1+1;
			zleft=x1;
			ztop=y1;
			zbottom=y1;
			zright=x1
			boxIt(x1,y1,x2,y2);
			/*
			clipLayer("zoomBoxTop",x1,y1,x2,y2);
			clipLayer("zoomBoxLeft",x1,y1,x2,y2);
			clipLayer("zoomBoxRight",x1,y1,x2,y2);
			clipLayer("zoomBoxBottom",x1,y1,x2,y2);
			*/
			selectBox=true;
			showLayer("zoomBoxTop");
			showLayer("zoomBoxLeft");
			showLayer("zoomBoxRight");
			showLayer("zoomBoxBottom");
		}
		highlightedOne="";
	}
	return false;

}

// stop select box display..... select
function stopSelectBox(e) {
	selectBox=false;
	var tempLeft=lastLeft;
	var tempRight=lastRight;
	var tempTop=lastTop;
	var tempBottom=lastBottom;
	if ((zright <zleft+2) && (ztop < zbottom+2)) {
	/*
		getMapXY(mouseX-pixelTolerance,mouseY-pixelTolerance);
		tempLeft = mapX;
		tempTop = mapY;
		getMapXY(mouseX+pixelTolerance,mouseY+pixelTolerance);
		tempRight = mapX;
		tempBottom = mapY;
	*/	
		
		//select(e);
	} else {
		pixelX = xDistance / iWidth;
		var theY = iHeight - ztop;
		pixelY = yDistance / iHeight;
		tempTop = pixelY * theY + bottom;
		tempRight = pixelX * zright + left;
		tempLeft = pixelX * zleft + left;
		theY = iHeight - zbottom;
		pixelY = yDistance / iHeight;
		tempBottom = pixelY * theY + bottom;
		window.scrollTo(0,0);
	}
	/*
	if ((showSelectedData) && (useTextFrame)) {
			parent.TextFrame.document.open();
			parent.TextFrame.document.writeln('<html>');
			parent.TextFrame.document.writeln('<body bgcolor="' + textFrameBackColor + '" text="Black" link="Blue" vlink="Blue" LEFTMARGIN=0>');
			parent.TextFrame.document.writeln('<FONT FACE="Arial" SIZE="-1"><b>' + LayerName[ActiveLayerIndex] + '</b><br>');
			parent.TextFrame.document.writeln('This may take considerable time to write out the data if many features have been selected. ');
			parent.TextFrame.document.writeln('</body></html>');
			parent.TextFrame.document.close();
			showRetrieveData();
	
	}
	*/
	queryStartRecord=0;
	var theString = writeGetFeatures2(tempLeft,tempBottom,tempRight,tempTop);
	//User-commented out
	selectDrawBoundary = tempLeft + "," + tempTop + "," + tempRight + "," + tempTop + "," + tempRight + "," + tempBottom + "," + tempLeft + "," + tempBottom + "," + tempLeft + "," + tempTop;
	//selectDrawBoundary = tempLeft + "," + tempTop + "," + tempRight + "," + tempTop + "," + tempRight + "," + tempBottom + "," + tempLeft + "," + tempBottom + "," + tempLeft + "," + tempTop;
	//selectEnvelope='north="' + tempTop + '" east="' + tempRight + '" south="' + tempBottom + '" west="' + tempLeft + '"';
	selectEnvelope='maxy="' + tempTop + '" maxx="' + tempRight + '" miny="' + tempBottom + '" minx="' + tempLeft + '"';
	drawSelectBoundary=true;
	showBuffer=false;
	selectionMode=2;
		hideLayer("zoomBoxTop");
		hideLayer("zoomBoxLeft");
		hideLayer("zoomBoxRight");
		hideLayer("zoomBoxBottom");
	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="' + north1 + '" maxx="' + east1 + '" miny="' + south1 + '" minx="' + west1 + '"';
	//User-Commented--var theString = writeEnvelopeXML(ActiveLayer,ActiveLayerType,selectFields,maxFeaturesReturned,queryStartRecord,theEnvelope,useLimitExtent);
	var theString = writeEnvelopeXML(ActiveLayer,ActiveLayerType,selectFields,'9999',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) {
	//User-Commented--var theString = '<ARCXML VERSION="1.0.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" envelope="true" geometry="false"';
	var theString = '<ARCXML VERSION="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" envelope="true" geometry="false"';
	theString += ' compact="true" featurelimit="' + maxReturned + '" beginrecord="' + startRec + '">\n';
	//User Added--Calculate the layer index 
	//var lidx = (layerCount-1-ActiveLayerIndex);
	
	theString += '<LAYER id="' + LayerID[ActiveLayerIndex] + '" ftype="' + theLayerType + '" />';
	theString += '<SPATIALQUERY subfields="' + theFields + " " + aField.toUpperCase() + '">';
	//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="' + limitRight + '" maxy="' + limitTop + '" minx="' + limitLeft + '" miny="' + 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.0.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" envelope="true" geometry="false"';
	var theString = '<ARCXML VERSION="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" envelope="true" geometry="false"';	
		
	//theString += ' compact="true" featurelimit="' + maxFeaturesReturned + '" beginrecord="' + queryStartRecord + '">\n';	
	theString += ' compact="true" featurelimit="' + '9999' + '" beginrecord="' + queryStartRecord + '">\n';
	
	//theString += '<LAYER id="' + ActiveLayer + '" ftype="' + ActiveLayerType + '" />';	
	//var lidx = (layerCount-1-ActiveLayerIndex);	
	theString += '<LAYER id="' + LayerID[ActiveLayerIndex] + '" ftype="' + ActiveLayerType + '" />';
		
	theString += '<SPATIALQUERY subfields="' + selectFields + " " + aField.toUpperCase() + '">';
	theString += '<SPATIALFILTER relation="area_intersection" >';
	if (theType==1) {
		theString += '<POLYLINE>\n<PATH>\n';
	} else {
		theString += '<POLYGON>\n<RING>\n';
	}
	for (var i=0;i<clickCount;i++) {
		theString += '<POINT x="' + clickPointX[i] + '" y="' + clickPointY[i] + '" />\n'; 
	}
	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="' + limitRight + '" maxy="' + limitTop + '" minx="' + limitLeft + '" miny="' + limitBottom + '" />\n';
		theString += '</SPATIALFILTER>\n';
	}
	theString += '</SPATIALQUERY>';
	theString += '</GET_FEATURES>';
	theString += '</REQUEST>';
	theString += '</ARCXML>';
	//alert(theString);
	selectLayer=ActiveLayer;
	selectType=ActiveLayerType;
	selectCount=0;
	hightlightedOne="";
	selectPoints.length=1;
	selectLeft.length=1;
	selectRight.length=1;
	selectTop.length=1;
	selectBottom.length=1;
	return theString;
	
}

// request a selection using a shape
function sendShapeSelect(theType) {
	queryStartRecord=0;
	selectionMode=3;
	showBuffer=false;
	if (theType==2) {
		if ((clickPointX[0]!=clickPointX[clickCount-1]) && (clickPointY[0]!=clickPointY[clickCount-1])) {
			clickPointX[clickCount]=clickPointX[0];
			clickPointY[clickCount]=clickPointY[0];
			clickCount=clickCount+1;
		}
	}
	showRetrieveData();
	var theString = writeShapeSelect(theType);
	sendToServer(imsQueryURL,theString,selectXMLMode);
}

// request more records to display
function getMoreData(startRecord) {
	var theString = "";
	queryStartRecord = startRecord;
	if (selectionMode==1) {
		//query
		theString = writeQueryXML(setQueryString);
	} else if (selectionMode==2) {
		theString = writeGetFeatures3();
	} else {
		var theType = clickType-1;
		theString = writeShapeSelect(theType);
	}
	showRetrieveData();
	sendToServer(imsQueryURL,theString,selectXMLMode);
}

// refresh map with hightlighted selection
function showHighlight(selNum) {
		
		highlightedOne = LayerIDField[ActiveLayerIndex] + " = " + selectPoints[selNum];
		//alert("showHighlight:" + highlightedOne + "," + 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;
		} else {
			mWMargin = fWidth * selectMargin;
			mHMargin = fHeight * selectMargin; 
		}
		saveLastExtent();
		left = selectLeft[selNum] - mWMargin;
		right = selectRight[selNum] + mWMargin;
		top = selectTop[selNum] + mHMargin;
		bottom = selectBottom[selNum] - mHMargin;
		//alert(highlightedOne);
			legendTemp=legendVisible;
			legendVisible=false;
			sendMapXML();
			
}

// refresh map with hightlighted selection
function showHighlight2(selNum,newSelCount) {
	if ((newSelCount!=newSelectCount) || (selNum>selectCount)) {
		alert("The selection set has been updated since this link was created. Please create a new selection set.");
	} else {
		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;
		} else {
			mWMargin = fWidth * selectMargin;
			mHMargin = fHeight * selectMargin; 
		}
		saveLastExtent();
		left = selectLeft[selNum] - mWMargin;
		right = selectRight[selNum] + mWMargin;
		top = selectTop[selNum] + mHMargin;
		bottom = selectBottom[selNum] - mHMargin;
		//alert(highlightedOne);
			legendTemp=legendVisible;
			legendVisible=false;
			sendMapXML();
			
	
	}
}

// clear current selection
function clearSelection() {
	var theCount = selectCount;
	var theHL = highlightedOne;
	selectCount=0;
	showBuffer=false;
	highlightedOne="";
	selectPoints.length=1;
	selectLeft.length=1;
	selectRight.length=1;
	selectTop.length=1;
	selectBottom.length=1;
	drawSelectBoundary=false;
	if (useTextFrame) {
		parent.TextFrame.document.open();
		parent.TextFrame.document.writeln('<html>');
		parent.TextFrame.document.writeln('<body bgcolor="White" text="Black" link="Blue" vlink="Blue">');
		parent.TextFrame.document.writeln('<FONT FACE="Arial" SIZE="-1"><b>' + LayerName[ActiveLayerIndex] + '</b><br>');
		if (toolMode==20) {
			parent.TextFrame.document.writeln('Measure Totals cleared');
			updateMeasureBox()
			}
		else 
			parent.TextFrame.document.writeln('Selection cleared');
		parent.TextFrame.document.writeln('</body></html>');
		parent.TextFrame.document.close()
	} else {
		if (toolMode==20) {
			showLayer("measureBox");
			updateMeasureBox()
			alert("Measure Totals Cleared");
		} else {
			alert("Selection Cleared");
		}
	}

	
	showGeocode=false;
	clickCount=0;
	totalMeasure=0;
	currentMeasure=0;
	//if ((theCount>0) || (theHL!="")) {
			legendTemp=legendVisible;
			legendVisible=false;
			selectBlurb="";
			showLayer("LoadMap");
			var theString = writeXML();
			sendToServer(imsURL,theString,99);
	//}

}

// add Draw Selected Features to Map XML request
function addSelectToMap(){
	var selString = "";
	if ((selectCount>0) && (showSelectedFeatures)) {
		selString +='<LAYER type="FeatureClass" name="Selected_Features" visible="true">\n';
		//selString +='<DATASET fromlayer="' + ActiveLayer + '" />\n';
		//selString +='<DATASET fromlayer="' + LayerName[ActiveLayerIndex] + '" />\n';
		selString +='<DATASET fromlayer="' + LayerID[ActiveLayerIndex] + '" />\n';
		if (selectionMode==1) {
			//selString +='<QUERY WHERE="' + setQueryString + '" />\n';
			/*
			if (useLimitExtent) {
				// keep this within the limitExtent
				selString +='<SPATIALQUERY  where="' + setQueryString + '" />';
				selString +='<SPATIALFILTER relation="area_intersection">\n';
				selString +='<ENVELOPE maxx="' + limitRight + '" maxy="' + limitTop + '" minx="' + limitLeft + '" miny="' + limitBottom + '" />\n';
				selString +='</SPATIALFILTER>\n';
				selString +='</SPATIALQUERY>\n';
			} else {
			*/
			selString +='<SPATIALQUERY where="' + setQueryString.toUpperCase() + '" />';
			//}
		
		} else {
			selString +='<SPATIALQUERY>\n';
			selString +='<SPATIALFILTER relation="area_intersection" >\n';
			if (selectionMode==2) {
				selString +='<ENVELOPE ' + selectEnvelope + ' />\n';
			} else {
				if (clickType==2) {
					selString +='<POLYLINE>\n<PATH>\n';
				} else if (clickType==3) {
					selString +='<POLYGON>\n<RING>\n';
				} else {
					selString +='<MULTIPOINT>\n';
				}
				for (var i=0;i<clickCount;i++) {
					selString +='<POINT x="' + clickPointX[i] + '" y="' + clickPointY[i] + '" />\n'; 
				}
				if (clickType==3) {
					//selString +='<POINT x="' + clickPointX[0] + '" y="' + clickPointY[0] + '" />\n';
					selString +='</RING>\n</POLYGON>\n';
				} else if (clickType==2) {
					selString +='</PATH>\n</POLYLINE>\n';
				} else {
					selString +='</MULTIPOINT>\n';
				}
			}
			selString +='</SPATIALFILTER>\n';
			/*
			if (useLimitExtent) {
				// keep this within the limitExtent
				selString +='<SPATIALFILTER relation="area_intersection">\n';
				selString +='<ENVELOPE maxx="' + limitRight + '" maxy="' + limitTop + '" minx="' + limitLeft + '" miny="' + limitBottom + '" />\n';
				selString +='</SPATIALFILTER>\n';
			} 
			*/
			selString +='</SPATIALQUERY>\n';
		}
		selString +='<SIMPLERENDERER>\n';
	
		if (selectType=="point") {
			selString +='<SIMPLEMARKERSYMBOL color="' + selectColor + '" type="Circle" size="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="' + transparentLevel + '" boundarycolor="255,255,255" />\n';
		}
		selString +='</SIMPLERENDERER>\n';
		selString +='</LAYER>\n';
		
	
	}
	
	if (allowDrawSelectBoundary) {
		if (drawSelectBoundary) {
			// add Draw Select Bounding Box to Map XML request
			selString +='<LAYER type="ACETATE" name="Selected_Boundary">\n';
			//selString +='<OBJECT UNITS="DATABASE">\n';
			//selString +='<POLYGON COORDS="' + selectDrawBoundary + '">\n';
			//	selString +='<SIMPLEPOLYGONSYMBOL FILLCOLOR="' + selectColor + '" FILLTYPE="Gray" ';
			//	selString +='BOUNDARYWIDTH="1" BOUNDARYCOLOR="' + selectColor + '" />\n';
			//selString +='</OBJECT>\n';
			selString +='<OBJECT units="DATABASE">\n';
			selString +='<LINE coords="' + selectDrawBoundary + '">\n';
			selString +='<SIMPLELINESYMBOL type="SOLID" color="' + clickMarkerColor + '" width="3" />\n</LINE>\n';
			selString +='</OBJECT>\n';
			selString +='<OBJECT units="DATABASE">\n';
			selString +='<LINE coords="' + selectDrawBoundary + '">\n';
			selString +='<SIMPLELINESYMBOL type="SOLID" color="255,255,255" width="1" />\n</LINE>\n';
			selString +='</OBJECT>\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="' + LayerName[ActiveLayerIndex] + '" />\n';
		selString +='<QUERY where="' + selectBlurb + '" />\n';
		selString +='<SIMPLERENDERER>\n';
	
		if (selectType=="point") {
			selString +='<SIMPLEMARKERSYMBOL color="' + selectColor + '" type="Circle" size="10" />\n';
		} else if (selectType=="line") {
			selString +='<SIMPLELINESYMBOL type="SOLID" color="' + selectColor + '" width="3" />\n';
		} else {
			selString +='<SIMPLEPOLYGONSYMBOL fillcolor="' + selectColor + '" filltype="solid" transparency="' + transparentLevel + '" boundarycolor="255,255,255" />\n';
			}
		selString +='</SIMPLERENDERER>\n';
		selString +='</LAYER>\n';
	
	}
	
	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';
		selString +='<QUERY where="' + highlightedOne + '" />\n';
		//selString +='<GROUPRENDERER>\n';
		//selString +='<SIMPLELABELRENDERER field="CNAME">\n';
		//selString +='<TEXTSYMBOL antialiasing="true" font="Yankai" fontstyle="regular" fontsize="14" />\n';
	        //selString +='</SIMPLELABELRENDERER>\n';
		selString +='<SIMPLERENDERER>\n';
		if (selectType=="point") {
		     //selString +='<SIMPLEMARKERSYMBOL color="' + highlightColor + '" type="Circle" size="10" />\n';
		     selString +='<SIMPLEMARKERSYMBOL color="' + highlightColor + '" type="Circle" size="10" />\n';
		} else if (selectType=="line") {
			selString +='<SIMPLELINESYMBOL type="SOLID" color="' + highlightColor + '" width="3" />\n';
		} else {
			//selString +='<SIMPLEPOLYGONSYMBOL FILLCOLOR="' + highlightColor + '" FILLTYPE="Gray" ';
			selString +='<SIMPLEPOLYGONSYMBOL fillcolor="' + highlightColor + '" filltype="solid" transparency="' + transparentLevel + '" boundarycolor="255,255,255" />\n';
		}
		selString +='</SIMPLERENDERER>\n';
		//selString +='</GROUPRENDERER\n>';
		selString +='</LAYER>\n';
		//alert(selString);
	}	
	
	return selString;
}


// 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;
	left = selMaxEnvelope[0] - mWMargin;
	right = selMaxEnvelope[2] + mWMargin;
	top = selMaxEnvelope[3] + mHMargin;
	bottom = selMaxEnvelope[1] - mHMargin;
	legendTemp=legendVisible;
	legendVisible=false;
	sendMapXML();
}