// aimsBuffer.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*		dependent on aimsXML.js, ArcIMSparam.js, aimsCommon.js, aimsMap.js, aimsIdentify.js, 
*			aimsSelect.js, aimsQuery.js, and aimsLayers.js
*		To be interactive, dependent also on aimsDHTML.js, aimsClick.js, and aimsNavigation.js
*/

aimsBufferPresent=true;

var drawTargetLayer = true;
var bufferTargetLayer = "";
var bufferTargetLayerIndex = 0;
var bufferDistance = 5.0;
var bufferSmoothEdges = 0.01;
var bufGetAttribute = false;
var getBufferedData = false;
var bufferWhereClause = "";			//where clause used if buffer is to be selective
var bufferGlobalEnvelope = false;		//whether a global envelope is requested.
	
/*
***************************************************************************************

Buffer functions 

***************************************************************************************
*/


function writeBufferForm() {
	if (useTextFrame) {
		parent.TextFrame.document.location = appDir + "buffer.htm";
		
	} else {
		var Win1 = open(appDir+"buffer.htm","QueryWindow","width=475,height=150,scrollbars=yes,resizable=yes");
		var t = "opener";
		if (parent.MapFrame!=null) t = "opener.parent.MapFrame";
	}
	Win1 = null;
}

// buffer around selected features
function bufferIt() {
	hideLayer("measureBox");
	//alert("Function not yet enabled.");
	showBuffer=true;
	if(getBufferedData) {
		bufGetAttribute = true;
		theString = writeQueryBufferXML();
		//theString = writeEnvelopeBufferXML();
		sendToServer(imsQueryURL,theString, 11);
	} else {
		sendMapXML();
	}
}

// add buffer stuff to Map XML request
function addBufferToMap() {
	var buffString = "";
	bufferSmoothEdges = bufferDistance * 0.01;
	//bufferSmoothEdges = mapScaleFactor;
	//if (bufferSmoothEdges>2) bufferSmoothEdges = 2;
	if (selectionMode==1) {
		if (drawTargetLayer) {
			//name below was "theBufferTarget"
			buffString += '<LAYER type="featureclass" name="Buffered Objects" visible="true">\n';
			//name below was "ActiveLayer"
			buffString += '<DATASET fromlayer="' + LayerID[ActiveLayerIndex] + '" />\n';
			buffString += '<SPATIALQUERY where="' + setQueryString + '" >\n';
			buffString += '<BUFFER distance="' + bufferDistance + '" ';

			buffString += 'smoothedges="' + bufferSmoothEdges + '"';
			buffString += ' bufferunits="' + ScaleBarUnits + '"';
			buffString += '>\n';
			if (useLimitExtent) {
				// keep this within the limitExtent
				buffString += '<SPATIALQUERY>\n';
				buffString += '<SPATIALFILTER relation="area_intersection">\n';
				buffString += '<ENVELOPE maxx="' + limitRight + '" maxy="' + limitTop + '" minx="' + limitLeft + '" miny="' + limitBottom + '" />\n';
				buffString += '</SPATIALFILTER>\n';
				buffString += '</SPATIALQUERY>\n';
			}
			//was name="bufferTargetLayer"
			buffString += '<TARGETLAYER id="' + LayerID[bufferTargetLayerIndex] + '" />\n';
			//Bob - new addition 6/11/02
			//if ((tableJoinString[bufferTargetLayerIndex] != undefined) || (layerTableString[bufferTargetLayerIndex] != undefined)) {
				//buffString += '<SPATIALQUERY where="' + tableJoinString[bufferTargetLayerIndex] + '" jointables = "' + layerTableString[bufferTargetLayerIndex] + '" />\n';
			//}
			//add a query if necessary
			if (bufferWhereClause != "") buffString += '<SPATIALQUERY where="' + bufferWhereClause + '" />\n'; 
			buffString += '</BUFFER>\n';
			buffString += '</SPATIALQUERY>\n';
			buffString += '<SIMPLERENDERER>\n';
			var tlType = LayerType[bufferTargetLayerIndex];
			if (tlType=="point") {
				buffString += '<SIMPLEMARKERSYMBOL color="' + highlightColor + '" type="Circle" size="10" />\n';
			} else if (tlType=="line") {
				buffString += '<SIMPLELINESYMBOL color="' + highlightColor + '" width="2" />\n';
			} else {
					buffString += '<SIMPLEPOLYGONSYMBOL fillcolor="' + highlightColor + '" filltype="solid" transparency="0.5" boundarycolor="255,255,255" />\n';
			}
		    buffString += '</SIMPLERENDERER>\n';
			buffString += '</LAYER>\n';
		}
		//name below was "theBufferPolygons"
		buffString += '<LAYER type="featureclass" name="Buffered Range" visible="true">\n';
		buffString += '<DATASET fromlayer="' + LayerID[ActiveLayerIndex] + '" />\n';
		buffString += '<SPATIALQUERY where="' + setQueryString + '" >\n';
		buffString += '<BUFFER distance="' + bufferDistance + '" smoothedges="' + bufferSmoothEdges + '" bufferunits="' + ScaleBarUnits + '" />\n';
		buffString += '</SPATIALQUERY>\n';
		buffString += '<SIMPLERENDERER>\n';
		buffString += '<SIMPLEPOLYGONSYMBOL fillcolor="100,100,100" filltype="solid" transparency="0.35" boundarycolor="255,255,255" />\n';
	    buffString += '</SIMPLERENDERER>\n';
		buffString += '</LAYER>\n';
	} else if ((selectionMode==2) ||(selectionMode==3)){
		if (drawTargetLayer) {
			//name was "theBufferTarget"
			buffString += '<LAYER type="featureclass" name="Buffered Objects" visible="true">\n';
			//fromLayer was "ActiveLayer"
			buffString += '<DATASET fromlayer="' + LayerID[ActiveLayerIndex] + '" />\n';
			buffString += '<SPATIALQUERY>\n';
			buffString += '<BUFFER distance="' + bufferDistance + '" smoothedges="' + bufferSmoothEdges + '" bufferunits="' + ScaleBarUnits + '">\n';
			//was name="bufferTargetLayer"
			buffString += '<TARGETLAYER id="' + LayerID[bufferTargetLayerIndex] + '" />\n';
			if ((tableJoinString[bufferTargetLayerIndex] != undefined) || (layerTableString[bufferTargetLayerIndex] != undefined)) {	
				buffString += '<SPATIALQUERY where="' + tableJoinString[bufferTargetLayerIndex] + '" jointables = "' + layerTableString[bufferTargetLayerIndex] + '" />\n';
			}
			//Bob - 6/11/02
			//alert ("Alpha");
			if (useLimitExtent) {
				// keep this within the limitExtent
				buffString += '<SPATIALQUERY>\n';
				buffString += '<SPATIALFILTER relation="area_intersection">\n';
				buffString += '<ENVELOPE maxx="' + limitRight + '" maxy="' + limitTop + '" minx="' + limitLeft + '" miny="' + limitBottom + '" />\n';
				buffString += '</SPATIALFILTER>\n';
				buffString += '</SPATIALQUERY>\n';
			}
			//alert("you are here");
			buffString += '</BUFFER>\n';
			// select rectangle or shape
			buffString += '<SPATIALFILTER relation="area_intersection">\n';
			if (selectionMode==2) {
				buffString += '<ENVELOPE ' + selectEnvelope + ' />\n';
			} else {
				if (clickType==2) {
					buffString += '<POLYLINE>\n<PATH>\n';
				} else if (clickType==3) {
					buffString += '<POLYGON>\n<RING>\n';
				} else {
					buffString += '<MULTIPOINT>\n';
				}
				for (var i=0;i<clickCount;i++) {
					buffString += '<POINT x="' + clickPointX[i] + '" y="' + clickPointY[i] + '" />\n'; 
				}
				if (clickType==3) {
					//buffString += '<POINT x="' + clickPointX[0] + '" y="' + clickPointY[0] + '" />\n';
					buffString += '</RING>\n</POLYGON>\n';
				} else if (clickType==2) {
					buffString += '</PATH>\n</POLYLINE>\n';
				} else {
					buffString += '</MULTIPOINT>\n';
				}
			}
			buffString += '</SPATIALFILTER>\n';
			buffString += '</SPATIALQUERY>\n';
			buffString += '<SIMPLERENDERER>\n';
			var tlType = LayerType[bufferTargetLayerIndex];
			if (tlType=="point") {
				buffString += '<SIMPLEMARKERSYMBOL color="' + highlightColor + '" type="Circle" size="10" />\n';
			} else if (tlType=="line") {
				buffString += '<SIMPLELINESYMBOL color="' + highlightColor + '" width="2" />\n';
			} else {
				buffString += '<SIMPLEPOLYGONSYMBOL fillcolor="' + highlightColor + '" filltype="solid" transparency="0.35" boundarycolor="255,255,255" />\n';
			}
		    buffString += '</SIMPLERENDERER>\n';
			buffString += '</LAYER>\n';
		}
  			buffString += '<LAYER type="featureclass" name="theBuffer" visible="true">\n';
    		buffString += '<DATASET fromlayer="' + LayerID[ActiveLayerIndex] + '" />\n';
    		buffString += '<SPATIALQUERY>\n';
      		buffString += '<BUFFER distance="' + bufferDistance + '" smoothedges="' + bufferSmoothEdges + '" bufferunits="' + ScaleBarUnits + '" />\n';
      		buffString += '<SPATIALFILTER relation="area_intersection">\n';
		if (selectionMode==2) {
			buffString += '<ENVELOPE ' + selectEnvelope + ' />\n';
		} else {
			if (clickType==2) {
				buffString += '<POLYLINE>\n<PATH>\n';
			} else if (clickType==3) {
				buffString += '<POLYGON>\n<RING>\n';
			} else {
				buffString += '<MULTIPOINT>\n';
			}
			for (var i=0;i<clickCount;i++) {
				buffString += '<POINT x="' + clickPointX[i] + '" y="' + clickPointY[i] + '" />\n'; 
			}
			if (clickType==3) {
				//buffString += '<POINT x="' + clickPointX[0] + '" y="' + clickPointY[0] + '" />\n';


				buffString += '</RING>\n</POLYGON>\n';
			} else if (clickType==2) {
				buffString += '</PATH>\n</POLYLINE>\n';
			} else {
				buffString += '</MULTIPOINT>\n';
			}
		}
		buffString += '</SPATIALFILTER>\n';
		if (useLimitExtent) {
			// keep this within the limitExtent
			
			buffString += '<SPATIALFILTER relation="area_intersection">\n';
			buffString += '<ENVELOPE maxx="' + limitRight + '" maxy="' + limitTop + '" minx="' + limitLeft + '" miny="' + limitBottom + '" />\n';
			buffString += '</SPATIALFILTER>\n';
			
		}
    		buffString += '</SPATIALQUERY>\n';
    		buffString += '<SIMPLERENDERER>\n';
      		buffString += '<SIMPLEPOLYGONSYMBOL fillcolor="100,100,100" filltype="solid" transparency="0.5" boundarycolor="255,255,255" />\n';
    		buffString += '</SIMPLERENDERER>\n';
  			buffString += '</LAYER>\n';
	} else {
	  // buffer a user shape
		// not implemented
	}
	//alert(buffString);
	return buffString;

}

function writeGetBufferedData() {
	var buffString = "";
	bufferSmoothEdges = bufferDistance * 0.01;
	//alert(selectionMode);
	switch(selectionMode) {
		case 1:
			buffString = writeQueryBufferXML();
			break
		case 2:
			buffString = writeEnvelopeBufferXML();
			break
		case 3:
			buffString = writeShapeBufferXML(clickType-1);
			break
		//case 4:
			// buffer a user shape - not implemented
		//	bufferTargetLayerIndex=ActiveLayerIndex;
		//	buffString = writeUserShapeBufferXML(clickType);
		//	break
	}
	//alert(buffString);	
	return buffString;
}

function writeQueryBufferXML() {
	//alert("Write Query Buffer XML");
	if (((areaQuery) && (areaQueryStage == 2)) || toolMode == 25) {
		maxFeaturesReturned = 100;
		bufferGlobalEnvelope = true;
	}
	var theTargetID = LayerID[bufferTargetLayerIndex];
	ScaleBarUnits = ScaleBarUnits.toLowerCase();
	if (swapSelectFields) {
		selectFields=selFieldList[bufferTargetLayerIndex];
	}
	var targetLayerType = LayerType[bufferTargetLayerIndex];
	//line below used to be envelope=true and compact=true
	var theString = '<?xml version="1.0" encoding="UTF-8"?><ARCXML VERSION="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" geometry="false" envelope="true" compact="true"';
	theString += ' featurelimit="' + maxFeaturesReturned + '" beginrecord="' + queryStartRecord + '"';
	theString += ' globalenvelope = "'+bufferGlobalEnvelope+'"';
	theString += '>\n';
	//Bob omitted ftype= ActiveLayerType
	//theString += '<LAYER id="' + ActiveLayer + '" ftype="' + ActiveLayerType + '" />\n';
	theString += '<LAYER id="' + ActiveLayer + '" />\n';
	if (setQueryString == "") {
		theString += '<SPATIALQUERY >\n';
	} else {
		theString += '<SPATIALQUERY where="' + setQueryString + '">\n';
	}

	theString += '<BUFFER distance="' + bufferDistance + '" bufferunits="' + ScaleBarUnits + '" >\n';

	theString += '<TARGETLAYER id="' + LayerID[bufferTargetLayerIndex] + '"/>\n';
	//alert(bufferTargetLayerIndex + "     " + tableJoinString[bufferTargetLayerIndex] + "     " + layerTableString[bufferTargetLayerIndex]);
	if ((tableJoinString[bufferTargetLayerIndex] == undefined) || (layerTableString[bufferTargetLayerIndex] == undefined)) {
		if (areaQuery) theString += '<SPATIALQUERY subfields="'+selectFields+'" where="'+bufferWhereClause+'"/>';
		else theString += '<SPATIALQUERY subfields="' + selectFields + '" />\n';
		//theString += '<SPATIALQUERY subfields="#ALL#" />\n';
	} else {	
		theString += '<SPATIALQUERY subfields="' + selectFields + '" where="' + tableJoinString[bufferTargetLayerIndex] + '" jointables = "' + layerTableString[bufferTargetLayerIndex] + '" />\n';
		//theString += '<SPATIALQUERY subfields="#ALL#" />\n';
	}
	//theString += '</TARGETLAYER>\n';
	theString += '</BUFFER>\n';
	
	//Bob - envelope below used to be limitRight, limitLeft, etc.
	//useLimitExtent = true;
	if ((useLimitExtent) || (setQueryString == "")) {
		// keep this within the limitExtent
		
		theString += '<SPATIALFILTER relation="area_intersection">\n';
		theString += '<ENVELOPE maxx="' + right + '" maxy="' + top + '" minx="' + left + '" miny="' + bottom + '" />\n';
		theString += '</SPATIALFILTER>\n';
		
	}
	theString += '</SPATIALQUERY>\n';
	theString += '</GET_FEATURES>';
	theString += '</REQUEST>';
	theString += '</ARCXML>';
	//alert("writeQueryXML()\nQuery XML Request:\n" + theString);
	return theString;

}

// write out xml request for selection by shape
function writeShapeBufferXML(theType) {
	if (swapSelectFields) {
		selectFields=selFieldList[bufferTargetLayerIndex];
	}
	var theString = '<?xml version="1.0" encoding="UTF-8"?><ARCXML VERSION="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" envelope="true" geometry=" false" compact="true"';
	//theString += ' featurelimit="' + maxFeaturesReturned + '" beginrecord="' + queryStartRecord + '"';
	theString += '>\n';
	theString += '<LAYER id="' + ActiveLayer + '" ftype="' + ActiveLayerType + '" />';
	theString += '<SPATIALQUERY>';
	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 += '<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 += '<BUFFER distance="' + bufferDistance + '" smoothedges="' + bufferSmoothEdges + '" bufferunits="' + ScaleBarUnits + '" >\n'; 
	theString += '<TARGETLAYER id="' + LayerID[bufferTargetLayerIndex] + '">\n';
	theString += '<SPATIALQUERY subfields="' + selectFields + '" />\n';
	theString += '</TARGETLAYER>\n';
	theString += '</SPATIALQUERY>\n';
	theString += '</BUFFER>\n';
	theString += '</GET_FEATURES>';
	theString += '</REQUEST>';
	theString += '</ARCXML>';
	//alert(theString);
	return theString;
	
}


// generic envelope select xml write routine
function writeEnvelopeBufferXML() {
	if (swapSelectFields) {
		selectFields=selFieldList[bufferTargetLayerIndex];
	}
	var theString = '<?xml version="1.0" encoding="UTF-8"?><ARCXML VERSION="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" envelope="true" geometry="false" compact="true"';
	//theString += ' featurelimit="' + maxReturned + '" beginrecord="' + startRec + '"';
	theString += '>\n';
	theString += '<LAYER id="' + ActiveLayer + '" ftype="' + ActiveLayerType + '" />\n';
	theString += '<SPATIALQUERY>';
	theString += '<SPATIALFILTER relation="area_intersection" >\n';
	//theString += '<ENVELOPE ' + selectEnvelope + ' />';
	theString += '<ENVELOPE maxx="' + right + '" maxy="' + top + '" minx="' + left + '" miny="' + bottom + '" />\n';
	theString += '</SPATIALFILTER>\n';
	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 += '<BUFFER distance="' + bufferDistance + '" smoothedges="' + bufferSmoothEdges + '" bufferunits="' + ScaleBarUnits + '" >\n'; 
	theString += '<TARGETLAYER id="' + LayerID[bufferTargetLayerIndex] + '"/>\n';
	theString += '<SPATIALQUERY subfields="' + selectFields + '" />\n';
	//theString += '</TARGETLAYER>\n';
	theString += '</BUFFER>\n';
	theString += '</SPATIALQUERY>';
	theString += '</GET_FEATURES>';
	theString += '</REQUEST>';
	theString += '</ARCXML>';
	return theString;

}


function getBufferAttributeData(theReply) {
	//alert(theReply);
	setLayerFields(bufferTargetLayerIndex);
	var theError = getXMLErrorMessage(theReply);
	var fList="";
	var lpos1 = 1;
	var lpos2 = 1;
	var epos = 1;
	var spos = 1;
	var morePoints=true;
	var moreFeatures=true;
	var featureCount = justGetFeatureCount(theReply);
	//alert("Features Returned: " + featureCount);
	var pos = 0;
	var startpos = 0;
	var endpos = xmlEndPos;
	var stillMore = false
	pos = theReply.indexOf('hasmore="true"',endpos);
	if (pos!=-1) stillMore = true;
	pos=0;
	var tempCount = 0;
	var selectedData="";
	var inData="";
	var xStr="";
	var yStr="";
	var eNorth="";
	var eSouth="";
	var eWest="";
	var eEast="";
	var fCount = featureCount;
	//alert("displayAttributeData()[featurecount=" + featureCount + "]");
	var selectCount2 = 0;
	var tempString="";
	var Win1 = parent.TextFrame;
	var theFrame = "parent.MapFrame";
	if (featureCount > 0) {
		newSelectCount += 1;
		//if (showSelectedData) {
			//alert("displayAttributeData()[opening html page]");
			if ((useExternalWindow) || (!useTextFrame)) {
				Win1 = window.open("","QueryWindow","width=575,height=120,scrollbars=yes,resizable=yes");
				theFrame = "opener";
				if (parent.MapFrame!=null) theFrame = "opener.parent.MapFrame";
			} else {
				Win1 = parent.TextFrame;
				Win1.document.open();
			}
			Win1.document.open();
			Win1.document.writeln('<html><head><title>Query/Selection Results</title></head>');
			Win1.document.writeln('<body bgcolor="' + textFrameBackColor + '" text="Black" link="Blue" vlink="Gray" LEFTMARGIN=0 onload="window.focus()">');
			Win1.document.writeln('<center><FONT FACE="Arial" SIZE="-1"><b>' + LayerName[bufferTargetLayerIndex] + '</b>');
			Win1.document.writeln('<table border="1" cellspacing="0" cellpadding="2" nowrap bgcolor="' + tableBackColor + '">');
		//}
		endpos = 1;
		
		for (var i=0;i<fCount;i++) {
			//alert(endpos);
			inData = parseRecordString(theReply, endpos);
			endpos = xmlEndPos;
			selectedData = clearLeadingSpace(inData);
			
			epos = theReply.indexOf("</FEATURE",endpos);
			if (showSelectedData) {
				//alert("2\n" + selectedData);
				var showHyper = false;
				if (hyperLinkLayers!=null) {
					for (var s1=0;s1<hyperLinkLayers.length;s1++) {
						if (hyperLinkLayers[s1] == LayerName[bufferTargetLayerIndex]) showHyper=true;
					}
				}
				var tempActiveLayer = ActiveLayer;
				var tempActiveLayerIndex = ActiveLayerIndex;
				var tempActiveLayerType = ActiveLayerType;
				ActiveLayer = bufferTargetLayer;
				ActiveLayerIndex = bufferTargetLayerIndex;
				ActiveLayerType = LayerType[bufferTargetLayerIndex];	
				var fName1 = getFieldNames(selectedData);
				var fValue1 = getFieldValues(selectedData);
				ActiveLayer = tempActiveLayer;
				ActiveLayerIndex = tempActiveLayerIndex;
				ActiveLayerType = tempActiveLayerType;	
				//selectPoints[selectCount] = getIdValue(fName1, fValue1);
				
				if (selectCount2==0) {
					Win1.document.write('<tr><th><FONT FACE="Arial" SIZE="-2">Rec</FONT></a></th>');
					alert(fName1);
					alert(AliasFieldName);
					for (var f=0;f<fName1.length;f++) {
						var f2 = -1;
						if (useFieldAlias) {
							for (var f3=0;f3<AliasFieldName.length;f3++) {
								if (AliasFieldName[f3]==fName1[f]) f2 = f3;
							}
						}
						if (f2!=-1) {
							Win1.document.write('<th><FONT FACE="Arial" SIZE="-2">' + AliasFieldAlias[f2] + '</FONT></a></th>');
						} else {
							Win1.document.write('<th><FONT FACE="Arial" SIZE="-2">' + fName1[f] + '</FONT></a></th>');
						}
					}
					Win1.document.writeln('</tr>');
				}
				Win1.document.write('<tr><td>');
				Win1.document.write('<FONT FACE="Arial" SIZE="-2">' + (selectCount2+queryStartRecord+1) + '</FONT>');
				Win1.document.writeln('</td>');
				for (var f=0;f<fName1.length;f++) {	
					Win1.document.write('<TD>');
					var isHyper=false;
					if (showHyper) {
						for (var s1=0;s1<hyperLinkFields.length;s1++) {
							if (hyperLinkFields[s1]==fName1[f]) {
								Win1.document.write('<a href="' + fValue1[f] + '" target="_blank">');
								isHyper=true;
							}
						}
					}
					var s2 = -1;
					for (var s1=0;s1<LayerFields.length;s1++) {
						if (fName1[f]==LayerFields[s1]) s2=s1;
					}
					//if (s2!=-1) {
						if (LayerFieldType[s2]=="91") {
							//alert(fName1[f]);
							if (isNaN(fValue1[f])) {
								var theDate = fValue1[f];
							} else {
								var theDate = new Date(parseInt(fValue1[f]));
							}
							Win1.document.write('<FONT FACE="Arial" SIZE="-2">' + theDate + '</FONT>');
							theDate=null;
						} else {
							Win1.document.write('<FONT FACE="Arial" SIZE="-2">' + fValue1[f] + '</FONT>');
						}
					//}
					if (isHyper) Win1.document.write('</a>');
					Win1.document.writeln('</td>');
				}
				Win1.document.writeln('</tr>');
				fName1 = null;
				fValue1 = null;
				
			}
			selectCount2 += 1;
		}
		
		Win1.document.writeln('</table>');		
		Win1.document.writeln('</font></center></body></html>');
		Win1.document.close();
		
			
	} else {
		//alert(tempString);
		if ((useExternalWindow) || (!useTextFrame)) {
			Win1 = window.open("","QueryWindow","width=575,height=120,scrollbars=yes,resizable=yes");
		} else {
			Win1 = parent.TextFrame;
			Win1.document.open();
		}
		Win1.document.writeln('<html><head>');
		Win1.document.writeln('	<title>Select Results</title>');
		Win1.document.writeln('</head>');
		Win1.document.writeln('<body BGCOLOR="White" TEXT="Black" LEFTMARGIN=0 TOPMARGIN=0>');
		Win1.document.writeln('<FONT FACE="Arial"><B>' + LayerName[bufferTargetLayerIndex] +'</B></font><FONT FACE="Arial" size="-2">');
		Win1.document.writeln('<br>No Features Found.');
		if (debugOn>0) {

			Win1.document.writeln('<p>Returned ArcXML Response:<br>');
			Win1.document.writeln(untag(theReply));
		} else {
			if (theError!="") {
				Win1.document.writeln('<p>Server returned:<br>');
				Win1.document.writeln(theError);
			}
		}
		Win1.document.writeln('</FONT>');
		Win1.document.writeln('</body></html>');
		Win1.document.close();
	}
	//if ((toolMode == 4) || (toolMode == 9))  selectCount=0;
	Win1=null;
	setLayerFields(ActiveLayerIndex);
	hideRetrieveData();
}

function possibleBuffer() {
	//used to determine if the active layer can be buffered around (only shapefiles can be)
	//the bufferLayers array is declared in aimsfields.js in the individual website directories
	var isOK = false;
	var currLayer = ActiveLayer;
	//alert(bufferLayers.length);
	for (var i = 0;i<bufferLayers.length;i++) {
		//alert(bufferLayers[i]);
		if (currLayer == bufferLayers[i]) isOK = true;
	}
	return isOK;
}