// 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 getBufferedData = false;

/*
***************************************************************************************

Buffer functions 

***************************************************************************************
*/


function writeBufferForm() {
	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><HEAD><META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">');
	Win1.document.writeln('			<script language="javascript">');
	Win1.document.writeln('var t = ' + t);
	Win1.document.writeln('myunits = "' + ScaleBarUnits + '"');
	Win1.document.writeln('function doBuffer() {');
	Win1.document.writeln('	var f = document.forms[0];');		
	Win1.document.writeln('	t.bufferDistance = f.theDistance.value;');
//	Win1.document.writeln('	theIndex = f.theTarget.selectedIndex;');
//HARD CODED
	Win1.document.writeln('	theIndex = "' + acctTop + '";');
	Win1.document.writeln('	if (theIndex>0) {');
//	Win1.document.writeln('		t.bufferTargetLayer = f.theTarget.options[theIndex].text;');
	Win1.document.writeln('		t.bufferTargetLayer = "ACCOUNTS";');
//	Win1.document.writeln('		t.bufferTargetLayerIndex = parseInt(f.theTarget.options[theIndex].value);');
	Win1.document.writeln('		t.bufferTargetLayerIndex = parseInt(' + acctTop + ');');
	Win1.document.writeln('		t.getBufferedData = f.getData.checked;');
	Win1.document.writeln('		t.drawTargetLayer=true;');
	Win1.document.writeln('	} else {');
	Win1.document.writeln('		t.drawTargetLayer=false;');
	Win1.document.writeln('		t.getBufferedData = false;');
	Win1.document.writeln('	}');
	Win1.document.writeln('	if ((f.theDistance.value>5) && (myunits == "MILES"))');
	Win1.document.writeln('	  {');
	Win1.document.writeln('	    alert("Must be 5 miles or less");');
	Win1.document.writeln('		return;');
	Win1.document.writeln('	  }');
	Win1.document.writeln('	if ((f.theDistance.value>3000) && (myunits == "FEET"))');
	Win1.document.writeln('	  {');
	Win1.document.writeln('	    alert("Must be 3000 feet or less");');
	Win1.document.writeln('		return;');
	Win1.document.writeln('	  }');
	Win1.document.writeln('	if ((f.theDistance.value>1000) && (myunits == "METERS"))');
	Win1.document.writeln('	  {');
	Win1.document.writeln('	    alert("Must be 1000 meters or less");');
	Win1.document.writeln('		return;');
	Win1.document.writeln('	  }');
	Win1.document.writeln('	if ((f.theDistance.value>8) && (myunits == "KILOMETERS"))');
	Win1.document.writeln('	  {');
	Win1.document.writeln('	    alert("Must be 8 kilometers or less");');
	Win1.document.writeln('		return;');
	Win1.document.writeln('	  }');
	Win1.document.writeln('	t.bufferIt();');
	Win1.document.writeln('}');
	Win1.document.writeln('</script>');
	Win1.document.writeln('</head>');
	Win1.document.writeln('<body bgcolor="#D7EBFF" text="navy" leftmargin=0 topmargin=0 onload="window.focus()">');
	Win1.document.writeln('<div align="center"><form onsubmit="doBuffer(); return false;">');
	Win1.document.writeln('<table cellspacing="2" cellpadding="0" bgcolor="#D7EBFF" width=100%>');
	Win1.document.writeln('<tr><td align="CENTER"><FONT face=Arial><STRONG>Mailing Labels</STRONG></FONT></td></tr>');
	Win1.document.writeln('<tr><td align="CENTER">within a distance of</td></tr><tr><td align="CENTER"><input type="Text" name="theDistance" value="0" size="5"> ');
	Win1.document.writeln(ScaleBarUnits);
	Win1.document.writeln('</td></tr><tr><td align="CENTER">around the selected features of</td></tr> ');
	Win1.document.writeln('<tr><td align="CENTER">ACCOUNTS');
	Win1.document.writeln('</font></td></tr>');
	Win1.document.writeln('<tr><td align="CENTER">');
	Win1.document.writeln('<br><input type="Button" name="theButton" value="Create Labels" onclick="doBuffer()">');
	Win1.document.write('<br><INPUT TYPE="checkbox" NAME="getData" VALUE="Yes"');
	Win1.document.writeln('CHECKED><font face="Arial" size="-2">Mailing Labels/Owner Information</font>');
	Win1.document.writeln('</td></tr>');
	Win1.document.writeln('</table></form></div></body></html>');
	Win1.document.close();
}

// buffer around selected features
function bufferIt() {
	hideLayer("measureBox");
	showBuffer=true;
	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) {
			buffString += '<LAYER type="featureclass" name="theBufferTarget" visible="true">\n';
			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';
				buffString += '<TARGETLAYER name="' + bufferTargetLayer + '" />\n';
				buffString += '</BUFFER>\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';
		}
		buffString += '<LAYER type="featureclass" name="theBufferPolygons" visible="true">\n';
		buffString += '<DATASET fromlayer="' + LayerID[ActiveLayerIndex] + '" />\n';
		buffString += '<SPATIALQUERY where="' + setQueryString + '" >\n';
		buffString += '<BUFFER distance="' + bufferDistance + '" 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) {
			buffString += '<LAYER type="featureclass" name="theBufferTarget" visible="true">\n';
			buffString += '<DATASET fromlayer="' + LayerID[ActiveLayerIndex] + '" />\n';
			buffString += '<SPATIALQUERY>\n';
			buffString += '<BUFFER distance="' + bufferDistance + '" bufferunits="' + ScaleBarUnits + '">\n';
			buffString += '<TARGETLAYER name="' + bufferTargetLayer + '" />\n';
			if (useLimitExtent) {
				// keep this within the limitExtent
				buffString += '<SPATIALQUERY>\n';
				//NANCY
				if (bufferDistance==0) {
					buffString += '<SPATIALFILTER relation="envelope_intersection">\n';
				} else {
					buffString += '<SPATIALFILTER relation="area_intersection">\n';
				}
				//END
				buffString += '<ENVELOPE maxx="' + limitRight + '" maxy="' + limitTop + '" minx="' + limitLeft + '" miny="' + limitBottom + '" />\n';
				buffString += '</SPATIALFILTER>\n';
				buffString += '</SPATIALQUERY>\n';
			}
			buffString += '</BUFFER>\n';
			
			// select rectangle or shape
      		//NANCY
      		if (bufferDistance==0) {
				buffString += '<SPATIALFILTER relation="envelope_intersection">\n';
      		} else {
				buffString += '<SPATIALFILTER relation="area_intersection">\n';
      		}
      		//END
			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 + '" bufferunits="' + ScaleBarUnits + '" />\n';
      	if (bufferDistance==0) {
			buffString += '<SPATIALFILTER relation="envelope_intersection">\n';
      	} else {
			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
      		//NANCY
      		if (bufferDistance==0) {
				buffString += '<SPATIALFILTER relation="envelope_intersection">\n';
      		} else {
				buffString += '<SPATIALFILTER relation="area_intersection">\n';
      		}
      		//END
			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
	}
	return buffString;
}

function writeGetBufferedData() {
	var buffString = "";
	bufferSmoothEdges = bufferDistance * 0.01;
	switch(selectionMode) {
		case 1:
			buffString = writeQueryBufferXML();
			break
		case 2:
			buffString = writeEnvelopeBufferXML();
			break
		case 3:
			buffString = writeShapeBufferXML(clickType-1);
			break
	}
	return buffString;
}

function writeQueryBufferXML() {
	if (swapSelectFields) {
		selectFields=selFieldList[bufferTargetLayerIndex];
	}
	var targetLayerType = LayerType[bufferTargetLayerIndex];
	var theString = '<?xml version="1.0" encoding="UTF-8"?>\n<ARCXML version="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" geometry="false" envelope="true" compact="true"';
	theString += '>\n';
	theString += '<LAYER id="' + ActiveLayer + '" ftype="' + ActiveLayerType + '" />\n';
	theString += '<SPATIALQUERY where="' + setQueryString + '">\n';
	theString += '<BUFFER distance="' + bufferDistance + '" bufferunits="' + ScaleBarUnits + '" >\n'; 
	theString += '<TARGETLAYER id="' + LayerID[bufferTargetLayerIndex] + '"/>\n';
	theString += '<SPATIALQUERY subfields="' + selectFields + '" />\n';
	theString += '</BUFFER>\n';
	if (useLimitExtent) {
		// keep this within the limitExtent
   		//NANCY
   		if (bufferDistance==0) {
			buffString += '<SPATIALFILTER relation="envelope_intersection">\n';
   		} else {
			buffString += '<SPATIALFILTER relation="area_intersection">\n';
   		}
		theString += '<ENVELOPE maxx="' + limitRight + '" maxy="' + limitTop + '" minx="' + limitLeft + '" miny="' + limitBottom + '" />\n';
		theString += '</SPATIALFILTER>\n';
	}
	theString += '</SPATIALQUERY>\n';
	theString += '</GET_FEATURES>';
	theString += '</REQUEST>';
	theString += '</ARCXML>';
	return theString;
}

// write out xml request for selection by shape
function writeShapeBufferXML(theType) {
	if (swapSelectFields) {
		selectFields=selFieldList[bufferTargetLayerIndex];
	}
	var theString = '<ARCXML VERSION="1.0.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" envelope="true" geometry=" false" compact="true"';
	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 + '" 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;
}


// generic envelope select xml write routine
function writeEnvelopeBufferXML() {
	if (swapSelectFields) {
		selectFields=selFieldList[bufferTargetLayerIndex];
	}
	var theString = '<ARCXML VERSION="1.0.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" envelope="true" geometry="false" compact="true"';
	theString += '>\n';
	theString += '<LAYER id="' + ActiveLayer + '" ftype="' + ActiveLayerType + '" />\n';
	theString += '<SPATIALQUERY>';
	theString += '<SPATIALFILTER relation="area_intersection" >\n';
	theString += '<ENVELOPE ' + selectEnvelope + ' />';
	theString += '</SPATIALFILTER>\n';
	if (useLimitExtent) {
		// keep this within the limitExtent
		//NANCY
		if (bufferDistance==0) {
			theString += '<SPATIALFILTER relation="envelope_intersection">\n';
		} else {
			theString += '<SPATIALFILTER relation="area_intersection">\n';
		}
		//END
		theString += '<ENVELOPE maxx="' + limitRight + '" maxy="' + limitTop + '" minx="' + limitLeft + '" miny="' + limitBottom + '" />\n';
		theString += '</SPATIALFILTER>\n';
	}
	theString += '<BUFFER distance="' + bufferDistance + '" bufferunits="' + ScaleBarUnits + '" >\n'; 
	theString += '<TARGETLAYER id="' + LayerID[bufferTargetLayerIndex] + '"/>\n';
	theString += '<SPATIALQUERY subfields="' + selectFields + '" />\n';
	theString += '</BUFFER>\n';
	theString += '</SPATIALQUERY>';
	theString += '</GET_FEATURES>';
	theString += '</REQUEST>';
	theString += '</ARCXML>';
	return theString;
}


function getBufferAttributeData(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);
	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;
	var selectCount2 = 0;
	var tempString="";
	var Win1 = parent.TextFrame;
	var theFrame = "parent.MapFrame";
	if (featureCount > 0) {
		newSelectCount += 1;
		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('		<SCRIPT language=JavaScript>');
		Win1.document.writeln('		function BreakItUp() {');
		Win1.document.writeln('			var mystring = document.NANCYFORM.MYTEXT.value;');
//		Win1.document.writeln('			alert(mystring.length);');
		Win1.document.writeln('			if (mystring.length > 102399) {');
		Win1.document.writeln('				alert("Buffer area is too large to create labels or a text file.");');
		Win1.document.writeln('			} else {');
		Win1.document.writeln('				document.NANCYFORM.submit();');
		Win1.document.writeln('			}');


/*  this was an attempt to just get the account numbers and forward them upon submission:
		Win1.document.writeln('var mystring = document.theForm.BigTextArea.value;');
		Win1.document.writeln('var mylength = mystring.length;');
		Win1.document.writeln('var myindex = mystring.indexOf("<FEATURECOUNT count=", 1);');
		Win1.document.writeln('var endindex = mystring.indexOf("hasmore=",1);');
		Win1.document.writeln('endindex = endindex - 2;');
		Win1.document.writeln('var mynewindex = myindex + 21;');
		Win1.document.writeln('var fCount = mystring.substring(mynewindex,endindex);');
		Win1.document.writeln('for (var i=0;i<fCount;i++)');
 		Win1.document.writeln('{');
		Win1.document.writeln('	epos = mystring.indexOf(".ACCOUNT=\"",1);');
		Win1.document.writeln('	epos = epos + 10;');
		Win1.document.writeln('	account = mystring.substr(epos,14);');
		Win1.document.writeln('	chkAct = account.substr(0,4);');
		Win1.document.writeln('	if (!isNaN(chkAct))');
		Win1.document.writeln('		{');
		//Win1.document.writeln('			qryString = qryString + account + ",";');
		Win1.document.writeln('			actString = actString + account + "|";');
		Win1.document.writeln('		}');
		Win1.document.writeln('	mystring = mystring.substr(epos,mylength);');
		Win1.document.writeln('  }');
		Win1.document.writeln('mylength2 = qryString.length;');
		Win1.document.writeln('qryString = qryString.substring(0,mylength2 - 1);');
		Win1.document.writeln('qryString = qryString + ")))";');
		Win1.document.writeln('alert(qryString);');
*/
/*  this was an attempt to use the code provided on the web for overflow error:

		Win1.document.writeln('			var FormLimit = 1000;');
		Win1.document.writeln('			var TempVar = new String;');
		Win1.document.writeln('			var mycount = 1;');
		
		Win1.document.writeln('			TempVar = document.theForm.BigTextArea.value');
		Win1.document.writeln('			if (TempVar.length > FormLimit)');
		Win1.document.writeln('			{');
		Win1.document.writeln('				document.theForm.BigTextArea.value = TempVar.substr(0,FormLimit);');
		Win1.document.writeln('				while (TempVar.length > 0)');
		Win1.document.writeln('				{');
		Win1.document.writeln('					TempVar = TempVar.substr(FormLimit,FormLimit * mycount);');
		Win1.document.writeln('					mycount++;');
				
		Win1.document.writeln('					var objTEXTAREA = document.createElement("TEXTAREA");');
		Win1.document.writeln('					objTEXTAREA.name = "BigTextArea";');
		Win1.document.writeln('					objTEXTAREA.value = TempVar;');
		Win1.document.writeln('					document.theForm.appendChild(objTEXTAREA);');
						
		Win1.document.writeln('		alert(TempVar);');
		Win1.document.writeln('				}');
		Win1.document.writeln('			}');

		Win1.document.writeln('			document.theForm.submit();');
*/
		Win1.document.writeln('		}');
		Win1.document.writeln('		</SCRIPT>');
		
		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++) {
			inData = parseRecordString(theReply, endpos);
			endpos = xmlEndPos;
			selectedData = clearLeadingSpace(inData);
			
			epos = theReply.indexOf("</FEATURE",endpos);
			if (showSelectedData) {
				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;	
				
				if (selectCount2==0) {
					Win1.document.write('<tr><th><FONT FACE="Arial" SIZE="-2">Rec</FONT></a></th>');
					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('<FORM NAME="NANCYFORM" METHOD="POST" ACTION="../gis/buffer.asp">');
		Win1.document.writeln('<INPUT TYPE="HIDDEN" NAME="MYTEXT" VALUE=\'' + theReply + '\'><BR>');
//		Win1.document.writeln('<FORM NAME="theForm" METHOD=post ACTION="../gis/buffer.asp">');
//		Win1.document.writeln('<Textarea rows=3 cols=100 name="BigTextArea" id="BigTextArea">' + theReply + '</Textarea><BR>');
//		Win1.document.writeln('<Textarea rows=3 cols=100 name="BigTextArea" id="BigTextArea">theReply</Textarea><BR>');

		Win1.document.writeln('<SCRIPT LANGUAGE="JAVASCRIPT">');
		Win1.document.writeln('BreakItUp();');
//		Win1.document.writeln('document.NANCYFORM.submit();');
		Win1.document.writeln('</SCRIPT>');
		
		Win1.document.writeln('</table>');		
		Win1.document.writeln('</font></center></body></html>');
		Win1.document.close();
		
			
	} else {
		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();
	}
	Win1=null;
	setLayerFields(ActiveLayerIndex);
	hideRetrieveData();
}
