// 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 plotbuff = "";
var getBufferedData = false;
var buffer = true;
var dobuffer = false;
/*
***************************************************************************************

Buffer functions 

***************************************************************************************
*/

function doadjoiner() {	

	bufferDistance = 7;
	bufferTargetLayer = "Parcels";
	bufferTargetLayerIndex = dblayer;
	getBufferedData = 1;
	drawTargetLayer=true;
	bufferIt();
}


function writeBufferForm() {
	if (useTextFrame) {
		var Win1 = parent.TextFrame;
		Win1.document.open();
		var t = "parent.MapFrame";
		
	} else {
		var Win1 = open("","QueryWindow","width='100%',height=150,width=600,scrollbars=yes,resizable=yes");
			var t = "opener";
			if (parent.MapFrame!=null) t = "opener.parent.MapFrame";
	}
	
	Win1.document.writeln('<html><head>');
	Win1.document.writeln('			<script language="javascript">');
	Win1.document.writeln('var t = ' + t);
	Win1.document.writeln('function doBuffer() {');
	Win1.document.writeln('	var f = document.forms[0];');		
	Win1.document.writeln('	t.bufferDistance = f.theDistance.value;');
	Win1.document.writeln('	t.ScaleBarUnits = f.newDUnits.value;');
	// /*
	Win1.document.writeln('	theIndex = f.theTarget.selectedIndex;');
	Win1.document.writeln('	if (theIndex>0) {');
	Win1.document.writeln('		t.bufferTargetLayer = f.theTarget.options[theIndex].text;');
	Win1.document.writeln('		t.bufferTargetLayerIndex = parseInt(f.theTarget.options[theIndex].value);');
	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>0) {');
	Win1.document.writeln('		t.bufferIt();');
	Win1.document.writeln('		window.close();');
	Win1.document.writeln('	} else {');
	Win1.document.writeln('		alert("Distance must be greater than zero.");');
	Win1.document.writeln('	}');
	Win1.document.writeln('}');
	Win1.document.writeln('</script>');
	Win1.document.writeln('</head>');
	Win1.document.writeln('<body bgcolor="#6699CC" text="black" 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="#6699CC" text="black" width=100%>');
	Win1.document.writeln('<tr><td align="CENTER"><FONT face=Arial><STRONG>Buffer</STRONG></FONT></td></tr>');
	// /*
	Win1.document.writeln('<tr><td align="CENTER"><font face="Arial" size="-2">Highlight features from ');
	Win1.document.writeln('<SELECT  name=theTarget>');
	Win1.document.writeln('<option value=-1>No Layer');
	for (var i=0;i<layerCount;i++) {
		if (LayerType[i]!="image") {
			if(LayerName[i]=="Parcels"){
				Win1.document.writeln('<option value=' + i + ' selected>' + LayerName[i]);
			}else{
				Win1.document.writeln('<option value=' + i + '>' + LayerName[i]);
			}
		}
	}
	Win1.document.writeln('</SELECT>');
	Win1.document.writeln(' within a distance of&nbsp;<input type="Text" name="theDistance" value="300" size="5"> ');
	Win1.document.writeln('<select name="newDUnits">');
	Win1.document.write('<option value="FEET"');
	if (t.ScaleBarUnits=="FEET") Win1.document.write(' selected');
	Win1.document.write('>FEET');
	Win1.document.write('<option value="METERS"');
	if (t.ScaleBarUnits=="METERS") Win1.document.write(' selected');
	Win1.document.write('>METERS');				
	Win1.document.writeln('</select>');
	Win1.document.writeln(' around the selected features of ');
	Win1.document.writeln(LayerName[ActiveLayerIndex]);
	Win1.document.writeln('</font></td></tr>');
	Win1.document.writeln('<tr><td align="CENTER">');
	Win1.document.writeln('<input type="Button" name="theButton" value="Create Buffer" onclick="doBuffer()">');
	Win1.document.write('<INPUT TYPE="checkbox" NAME="getData" VALUE="Yes"');
	Win1.document.write(' CHECKED');
	Win1.document.writeln('><font face="Arial" size="-2">Display Attributes</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");
	//alert("Function not yet enabled.");
	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 += '</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 + '" 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) {
			buffString += '<LAYER type="featureclass" name="theBufferTarget" visible="true">\n';
			buffString += '<DATASET fromlayer="' + LayerID[ActiveLayerIndex] + '" />\n';
			buffString += '<SPATIALQUERY>\n';
			buffString += '<BUFFER distance="' + bufferDistance + '" smoothedges="' + bufferSmoothEdges + '" bufferunits="' + ScaleBarUnits + '">\n';
			buffString += '<TARGETLAYER name="' + bufferTargetLayer + '" />\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 += '</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);
	plotbuff = 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() {
	if (swapSelectFields) {
		selectFields=selFieldList[bufferTargetLayerIndex];
	}
	var targetLayerType = LayerType[bufferTargetLayerIndex];
	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 += '>\n';
	theString += '<LAYER id="' + ActiveLayer + '" ftype="' + ActiveLayerType + '" />\n';
	theString += '<SPATIALQUERY where="' + setQueryString + '">\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';
	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>\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 += '</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 = '<?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 += '</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 test = ""
	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 theFrame = "parent.MapFrame";
	var Win1 = parent.DBFrame.document;
	if (featureCount > 0) {
		newSelectCount += 1;
		endpos = 1;
		
		for (var i=0;i<fCount;i++) {

			inData = parseRecordString(theReply, endpos);
			endpos = xmlEndPos;
			selectedData = clearLeadingSpace(inData);
			epos = theReply.indexOf("</FEATURE",endpos);
				var tempActiveLayer = ActiveLayer;
				var tempActiveLayerIndex = ActiveLayerIndex;
				var tempActiveLayerType = ActiveLayerType;
				ActiveLayer = bufferTargetLayer;
				ActiveLayerIndex = bufferTargetLayerIndex;
				ActiveLayerType = LayerType[bufferTargetLayerIndex];	
				var fValue1 = getCommonFieldValues(selectedData);
				if (fValue1!="&nbsp;"){
				if (i+1>=fCount){
				test = test + fValue1;
				} else {
				test = test + fValue1 + "','";
				}
				}
				ActiveLayer = tempActiveLayer;
				ActiveLayerIndex = tempActiveLayerIndex;
				ActiveLayerType = tempActiveLayerType;	
		}
	}

	setLayerFields(ActiveLayerIndex);
	hideRetrieveData();
	buffer = true;
		theForm = Win1.forms[0];
		theForm.IDValue.value = test;
		ovBol = false;
	parent.TOCFrame.location = appDir+"arcims_process.asp";
}

