
resSelectFields="RES_CODE SHORT_NAME TYPE IUCN GAZ_DATE #ID# #SHAPE#";
theFieldNames = "RES_CODE,SHORT_NAME,TYPE,IUCN,GAZ_DATE";
var resAliasFieldName = theFieldNames.split(",");
				
theFieldAlias = "Code,Reserve Name,Reserve Type,IUCN Cat,Gazette Date";
var resAliasFieldAlias = theFieldAlias.split(",");

// write out XML request to identify features
function resWriteGetFeatures(west1,south1,east1,north1) {
	var useString = resWriteIdentifyXML(reserveLayer,reserveLayerType,resSelectFields,west1,south1,east1,north1,maxFeaturesReturned,useLimitExtent);
	hightlightedOne="";
	return useString;
}

//NPWS MES
// parse XML response for selected features
// modified for reserve query
function resDisplayData(theReply) {
	setLayerFields(reserveLayerIndex);
	alert("resDisplayData");
	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 tempString = "";
	var fCount = featureCount;
	selectCount = 0;
	var tempString="";
	var Win1 = parent.QueryFrame; //MES
	var theFrame = "parent.MapFrame";
	var selectPoints=new Array();
	if (featureCount > 0) {
		newSelectCount += 1;
		showSelectedData = true;
		if (showSelectedData) {
			Win1 = parent.QueryFrame;
			Win1.document.open();
			Win1.document.writeln('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">');
			Win1.document.writeln('<html><meta http-equiv="Content-Type" content="text/html; charset=' + charSet + '"><head><title>' + titleList[7] + '</title></head>');
			Win1.document.writeln('<body bgcolor="' + QueryFrameBackColor + '" text="Black" link="Blue" vlink="Blue" LEFTMARGIN=0 onload="window.focus()">');
		}
		endpos = 1;		
		for (var i=0;i<fCount;i++) {
			Win1.document.write('<FONT FACE="Arial,sans-serif" SIZE="-2"><b>Record ' + (i+1) + '</b></FONT>');
			Win1.document.writeln('<FONT FACE="Arial,sans-serif" SIZE="-1">');
			Win1.document.write('<TABLE WIDTH="100%">');
			if ((toolMode!=4) && (toolMode!=50)) {
				var theXYs = getEnvelopeXYs(theReply, endpos)
				selectLeft[selectCount] = theXYs[0];
				selectBottom[selectCount] = theXYs[1];
				selectRight[selectCount] = theXYs[2];
				selectTop[selectCount] = theXYs[3];
				endpos = xmlEndPos;
			}
			inData = parseRecordString(theReply, endpos);
			endpos = xmlEndPos;
			//selectedData = clearLeadingSpace(inData);
			selectedData = inData;	
			epos = theReply.indexOf("</FEATURE",endpos);
			if (showSelectedData) {
				var showHyper = false;	
				var fName1 = getFieldNames(selectedData);
				var fValue1 = getFieldValues(selectedData);
				selectPoints[selectCount] = getIdValue(fName1, fValue1);
				var idFieldNum = -1;
				var shapeFieldNum = -1;
				// hide ID column header
				for (var f=0;f<fName1.length;f++) {
					if (fName1[f]==LayerIDField[reserveLayerIndex]) idFieldNum = f;
				}
				// hide Shape column header
				for (var f=0;f<fName1.length;f++) {
					if (fName1[f]==LayerShapeField[reserveLayerIndex]) shapeFieldNum = f;
				}
				/*alert("reached field alias");
				theFieldNames = "RES_CODE,SHORT_NAME,TYPE,IUCN,GAZ_DATE";
				var resAliasFieldName = theFieldNames.split(",");
				
				theFieldAlias = "Code,Reserve Name,Reserve Type,IUCN Cat,Gazette Date";
				var resAliasFieldAlias = theFieldAlias.split(","); */
				
				for (var cnt=0;cnt<fName1.length;cnt++) {
					if ((cnt!=idFieldNum) && (cnt!=shapeFieldNum)) {
						if (isEven(cnt)) {
							//alert("Made it to true");
							Win1.document.write('<TR BGCOLOR="White">');
						} else {
							//alert("Made it to False");
							Win1.document.write('<TR BGCOLOR="Silver">');
						}
						
						var f2 = -1;
						if (useFieldAlias) {
							for (var f3=0;f3<resAliasFieldName.length;f3++) {
								if (resAliasFieldName[f3]==fName1[cnt]) f2 = f3;
							}
						}

						if (f2!=-1) {
							var theField = resAliasFieldAlias[f2];
						} else {
							var theField = fName1[cnt];
						}

						var s2 = -1;
						for (var s1=0;s1<LayerFields.length;s1++) {
							if (fName1[cnt]==LayerFields[s1]) s2=s1;
						}
						if (LayerFieldType[s2]=="91") { 
							if (isNaN(fValue1[cnt])) {
								var theDate = fValue1[cnt];
							} else {
								var theDateObj = new Date(parseFloat(fValue1[cnt]));
								var d = theDateObj.getDate();
								var day = (d < 10) ? '0' + d : d;
								var m = theDateObj.getMonth();
								var month = (m < 10) ? '0' + m : m;
								var y = theDateObj.getYear();
								var year = (y < 1000) ? 1900 + y : y;
								var theDate = day + "/" + month + "/" + year;
							}
							var theValue = theDate;
							theDate=null;
						} else {
							// put html entity for spaces so display is correct 
							var theValue = fValue1[cnt].replace(/ /g, "&nbsp;");
						}
					
						Win1.document.write('<TD><FONT FACE="Arial,sans-serif" SIZE="-1"><B>' + theField + '</B></FONT></TD>' + '<TD><FONT FACE="Arial,sans-serif" SIZE="-1">' + theValue + '</FONT></TD>');
				
						Win1.document.write('</TR>');
					}
				}
				fName1 = null;
				fValue1 = null;
			}
			
			selectCount += 1;
			Win1.document.writeln('</TABLE>');					
		}
		if (showSelectedData) {
			
			Win1.document.writeln('<FONT FACE="Arial" SIZE="-2">');
			if (toolMode!=25) {
				if (queryStartRecord>1) {
					var prevRecord = queryStartRecord - maxFeaturesReturned;
					if (prevRecord<1) prevRecord=1;
					Win1.document.write('<a href="javascript:' + theFrame + '.resGetMoreData(' + prevRecord + ')">' + msgList[61] + maxFeaturesReturned + msgList[62] + '</a>&nbsp;&nbsp;&nbsp;');
				}
				if (stillMore) {
					var nextRecord=featureCount + queryStartRecord;
					Win1.document.writeln('<a href="javascript:' + theFrame + '.resGetMoreData(' + nextRecord + ')">' + msgList[63] + '</a>');
				}
			}

			// NPWS Mod to add link to search for Reseve again
			var theImgSource=appDir + "images/find_1.gif"
			var theAspSource=appDir + "findRes.htm"
			Win1.document.writeln('</FONT>');
			Win1.document.writeln('<FONT FACE="Arial,sans-serif" SIZE="-1">');
			Win1.document.writeln('<BR>');
			Win1.document.write('<img src="' + theImgSource + '" width=22 height=22 hspace=0 ');
			Win1.document.writeln('vspace=0 border=0 name="Park" alt="Search for Reserve">'); 		
			Win1.document.writeln('<B><A HREF="' + theAspSource + '">Search for Reserve</A></B>');

			//Win1.document.writeln('</font></center></body></html>');
			Win1.document.writeln('</font></body></html>');
			Win1.document.close();
		}
		//alert("toolMode:" + toolMode);
		
		if ((queryStartRecord==1) && ((toolMode!=4) && (toolMode!=50)) && (toolMode!=25)) {
			if ((selectCount==1) && (zoomToSingleSelect)) {
				if ((selectCount==1) && (zoomToSingleSelect)) {
				var fWidth = selectRight[0] - selectLeft[0];
				var fHeight = selectTop[0] - selectBottom[0];
				var mWMargin = 0;
				var mHMargin = 0;
				mWMargin = fWidth * selectMargin;
				mHMargin = fHeight * selectMargin;
				}
				saveLastExtent();
				eLeft = selectLeft[0] - mWMargin;
				eRight = selectRight[0] + mWMargin;
				eTop = selectTop[0] + mHMargin;
				eBottom = selectBottom[0] - mHMargin;
					if (!zoomToFeature) {
					eLeft = lastLeft;
					eBottom = lastBottom;
					eRight = lastRight;
					eTop = lastTop;
					}
			}
			selectCount=0;
			
			if (highlightIdentify) {
				highlightedOne=LayerIDField[reserveLayerIndex] + " IN (" + selectPoints + ")";
				sendMapXML();
			}
		}

			
	} else {
		Win1 = parent.QueryFrame;
		Win1.document.open();

		Win1.document.writeln('<html><meta http-equiv="Content-Type" content="text/html; charset=' + charSet + '"><head>');
		Win1.document.writeln('	<title>' + titleList[8] + '</title>');
		Win1.document.writeln('</head>');
		Win1.document.writeln('<body BGCOLOR="White" TEXT="Black" LEFTMARGIN=0 TOPMARGIN=0>');
		Win1.document.writeln('<FONT FACE="Arial"><B>You are currently searching in the ' + LayerName[reserveLayerIndex] +' layer</B></font><FONT FACE="Arial" size="-2">');
		Win1.document.writeln('<br><br>' + msgList[64]);
		Win1.document.writeln('<br><br>Try zooming in more to find the reserve you are after');
		if (debugOn>0) {
			Win1.document.writeln('<p>' + msgList[65] + '<br>');
			Win1.document.writeln(untag(theReply));
		} else {
			if (theError!="") {
				Win1.document.writeln('<p>' + msgList[66] + '<br>');
				Win1.document.writeln(theError);
			}
		}

		//NPWS	
		var theAspSource=appDir + "SubMenu.htm";
		Win1.document.writeln('<BR>');
		Win1.document.writeln('<BR>');
		Win1.document.write('<FONT FACE="Arial" SIZE="-1">');
		Win1.document.writeln('<B><A HREF="' + theAspSource + '"> Return to Menu </A></B></FONT>');

		Win1.document.writeln('</FONT>');
		Win1.document.writeln('</body></html>');
		Win1.document.close();
	}
	if (toolMode==4) selectCount=0;
	Win1=null;
	hideRetrieveData();
}

// identify reserve
function resIdentify(e) {
	if (checkIfActiveLayerAvailable()) {
		//alert("Made it to NEW resIdentify in NPWS.js");
		highlightedOne="";
		var theX = mouseX;
		var theY = mouseY;
		getMapXY(theX,theY);
		searchTolerance = (xDistance/iWidth) * pixelTolerance;
		var tempWest = mapX - searchTolerance;
		var tempNorth = mapY + searchTolerance;
		var tempEast = mapX + searchTolerance;
		var tempSouth = mapY - searchTolerance;
		var theString = resWriteGetFeatures(tempWest,tempSouth,tempEast,tempNorth);
		showRetrieveData();
		sendToServer(imsQueryURL,theString,resIdentifyXMLMode);
	}
}



function resWriteIdentifyXML(theLayer,theLayerType,theFields,leftX,bottomY,rightX,topY,maxReturned,hasLimit) {
	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" envelope="true" geometry="false" featurelimit="' + maxReturned + '">\n';
	theString += '<LAYER id="' + theLayer + '" />';
	theString += '<SPATIALQUERY subfields="' + theFields + '">';
	theString += '<SPATIALFILTER relation="area_intersection" >';
	theString += '<ENVELOPE maxy="' + forceComma(topY) + '" maxx="' + forceComma(rightX) + '" miny="' + forceComma(bottomY) + '" minx="' + forceComma(leftX) + '" />';
	theString += '</SPATIALFILTER>';
	theString += '</SPATIALQUERY>';
	if (hasLimit) {
		// keep this within the limitExtent
		theString += '<SPATIALFILTER relation="area_intersection">\n';
		theString += '<ENVELOPE maxx="' + forceComma(limitRight) + '" maxy="' + forceComma(limitTop) + '" minx="' + forceComma(limitLeft) + '" miny="' + forceComma(limitBottom) + '" />\n';
		theString += '</SPATIALFILTER>\n';
	}
	theString += '</GET_FEATURES>';
	theString += '</REQUEST>';
	theString += '</ARCXML>';
	return theString;

}


//save map image to disk, similar to aimsPrint.js
// first get Map
function resSaveMap() {
	showRetrieveMap();
	var theString = writeXML();
	sendToServer(imsURL,theString,1011);
	theString=null;
	var Win1 = open("savemap.htm","SaveMapWindow","width=600,height=600,scrollbars=yes,resizable=yes");
}
// write the save page
function writeSavePage() {
	var Win1 = open("","SaveMapWindow");
	Win1.document.writeln('<html><meta http-equiv="Content-Type" content="text/html; charset=' + charSet + '"><head>');
	Win1.document.writeln('	<title>Save map image to disk</title>');
	Win1.document.writeln('</head>');
	Win1.document.writeln('<body BGCOLOR="White" TEXT="Black" LEFTMARGIN=0 TOPMARGIN=0>');
	Win1.document.writeln('<FONT FACE="Arial"><B>');
	Win1.document.writeln('<TABLE WIDTH="100%" BORDER="2" CELLSPACING="0" CELLPADDING="0" NOWRAP>');
	Win1.document.writeln('	<TR>');
	Win1.document.writeln('		<TH COLSPAN="2">To save the map, right click on the image and choose "Save Picture As" or drag and drop the map into a word document.</TH>');
	Win1.document.writeln('	</TR>');
	Win1.document.writeln('	<TR>');
	Win1.document.write('		<TD>');
	Win1.document.writeln('			<IMG SRC="' + saveMapURL + '" HSPACE=0 VSPACE=0 BORDER=0 ALT="">');
	Win1.document.writeln('		</TD>');
	Win1.document.writeln('	</TR>');
	Win1.document.writeln('</TABLE>');
	Win1.document.writeln('</body></html>');
	//Win1.document.close();
	Win1=null;
	saveMapURL=null;
	hideRetrieveMap();
}


//NPWS MES
// parse XML response for selected features
// modified for reserve query
function resDisplayAttributeData(theReply) {
	alert("resDisplayAttributeData");
	setLayerFields(ActiveLayerIndex);
	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(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 tempString = "";
	var fCount = featureCount;
	//alert("displayAttributeData()[featurecount=" + featureCount + "]");
	selectCount = 0;
	var tempString="";
	//var Win1 = parent.QueryFrame; //MES
	var Win1 = parent.QueryFrame; //MES
	var theFrame = "parent.MapFrame";
	if (featureCount > 0) {
		newSelectCount += 1;
		showSelectedData = true;
		if (showSelectedData) {
			Win1 = parent.QueryFrame;

			// MES end
			Win1.document.open();
			Win1.document.writeln('<html><meta http-equiv="Content-Type" content="text/html; charset=' + charSet + '"><head><title>' + titleList[7] + '</title></head>');
			Win1.document.writeln('<body bgcolor="' + QueryFrameBackColor + '" text="Black" link="Blue" vlink="Blue" LEFTMARGIN=0 onload="window.focus()">');
			Win1.document.writeln('<TABLE WIDTH="100%">');
			Win1.document.writeln('<TR BGCOLOR="Gray">');
		}
		endpos = 1;
		
		for (var i=0;i<fCount;i++) {

			//alert(endpos);
			if ((toolMode!=4) && (toolMode!=50)) {
				var theXYs = getEnvelopeXYs(theReply, endpos)
				selectLeft[selectCount] = theXYs[0];
				selectBottom[selectCount] = theXYs[1];
				selectRight[selectCount] = theXYs[2];
				selectTop[selectCount] = theXYs[3];
				endpos = xmlEndPos;
			}
			//alert(endpos);
			inData = parseRecordString(theReply, endpos);
			//MES
			//alert("indata\n" + inData);

			endpos = xmlEndPos;
			//selectedData = clearLeadingSpace(inData);
			selectedData = 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[ActiveLayerIndex]) showHyper=true;
					}
				}
					
				var fName1 = getFieldNames(selectedData);
				//MES
				//alert("fName1\n" + fName1);

				var fValue1 = getFieldValues(selectedData);
				//MES
				//alert("fValue1\n" + fValue1);

				selectPoints[selectCount] = getIdValue(fName1, fValue1);
				
				var idFieldNum = -1;
				var shapeFieldNum = -1;
				if (hideIDFieldData) {
					// hide ID column header
					for (var f=0;f<fName1.length;f++) {
						if (fName1[f]==LayerIDField[ActiveLayerIndex]) idFieldNum = f;
					}
				}
				if (hideShapeFieldData) {
					// hide Shape column header
					for (var f=0;f<fName1.length;f++) {
						if (fName1[f]==LayerShapeField[ActiveLayerIndex]) shapeFieldNum = f;
					}
				}
				
				if (selectCount==0) {
					//Win1.document.write('<tr><th><FONT FACE="Arial" SIZE="-2">' + msgList[86] + '</FONT></a></th>');
					Win1.document.write('<th><FONT FACE="Arial" SIZE="-1">' + msgList[86] + '</FONT></a></th>');
					for (var f=0;f<fName1.length;f++) {
						if ((f!=idFieldNum) && (f!=shapeFieldNum)) {
							var f2 = -1;
							if (useFieldAlias) {
								for (var f3=0;f3<AliasFieldName.length;f3++) {
									//alert(AliasFieldName[f3] + " " + fName1[f]);
									if (AliasFieldName[f3]==fName1[f]) f2 = f3;
								}
							}


							if (f2!=-1) {
								Win1.document.write('<th><FONT FACE="Arial" SIZE="-1">' + AliasFieldAlias[f2] + '</FONT></a></th>');
							} else {
								Win1.document.write('<th><FONT FACE="Arial" SIZE="-1">' + fName1[f] + '</FONT></a></th>');
							}
						}
					}
					Win1.document.writeln('</tr>');
				}

				//MES							
				if (isEven(selectCount)) {
					//alert("Made it to true");
					Win1.document.writeln('<TR BGCOLOR="White">');
				} else {
					//alert("Made it to False");
					Win1.document.writeln('<TR BGCOLOR="Silver">');
				}

				//Win1.document.write('<tr><td>');						
				Win1.document.write('<td>');
				//if ((toolMode!=4) && (toolMode!=50)) Win1.document.write('<a href="javascript:' + theFrame + '.showHighlight(' + selectCount + ')">');
				Win1.document.write('<a href="javascript:' + theFrame + '.showHighlight(' + selectCount + ')">');

				Win1.document.write('<FONT FACE="Arial" SIZE="-1">' + (selectCount+queryStartRecord) + '</FONT>');
				if ((toolMode!=4) && (toolMode!=50)) Win1.document.write('</a>');
				Win1.document.writeln('</td>');
				for (var f=0;f<fName1.length;f++) {
	
					if ((f!=idFieldNum) && (f!=shapeFieldNum)) {
						Win1.document.write('<TD>');
						var isHyper=false;
						if (showHyper) {
							for (var s1=0;s1<hyperLinkFields.length;s1++) {
								if (hyperLinkFields[s1]==fName1[f]) {
									var theLinkURL = currentHyperLinkPrefix + fValue1[f] + currentHyperLinkSuffix;
									Win1.document.write('<a href="' + theLinkURL + '" target="_blank">');
									isHyper=true;
								}
							}
						}
						var s2 = -1;
						for (var s1=0;s1<LayerFields.length;s1++) {
							if (fName1[f]==LayerFields[s1]) s2=s1;
						}
						if (LayerFieldType[s2]=="91") {
							//alert(fName1[f]);
							if (isNaN(fValue1[f])) {
								var theDate = fValue1[f];
							} else {
								//var theDate = new Date(parseInt(fValue1[f]));
								var theDateObj = new Date(parseFloat(fValue1[f]));
								var d = theDateObj.toUTCString();
								var theDate = d.replace(/GMT|UTC/,"");
							}
							Win1.document.write('<FONT FACE="Arial" SIZE="-1">' + theDate + '</FONT>');
							theDate=null;
						} else {
							// put html entity for spaces so display is correct - browsers trim extra spaces
							tempString = fValue1[f].replace(/ /g, "&nbsp;");

							//MES
							//alert("aimsIdentify\n tempString = " + tempString + "\n SelectCount = " + selectCount + "\n i = " + i);

							Win1.document.write('<FONT FACE="Arial" SIZE="-1">' + tempString + '</FONT>');
							//Win1.document.write('<FONT FACE="Arial" SIZE="-1">' + fValue1[f] + '</FONT>');
						}
						if (isHyper) Win1.document.write('</a>');
						Win1.document.writeln('</td>');
					}
				}
				Win1.document.writeln('</tr>');
				fName1 = null;
				fValue1 = null;
				
			}
			selectCount += 1;
					
		}
		if (showSelectedData) {
			
			Win1.document.writeln('</table><FONT FACE="Arial" SIZE="-2">');
			if (toolMode!=25) {
				if (queryStartRecord>1) {
					var prevRecord = queryStartRecord - maxFeaturesReturned;
					if (prevRecord<1) prevRecord=1;
					Win1.document.write('<a href="javascript:' + theFrame + '.resGetMoreData(' + prevRecord + ')">' + msgList[61] + maxFeaturesReturned + msgList[62] + '</a>&nbsp;&nbsp;&nbsp;');
				}
				if (stillMore) {
					var nextRecord=featureCount + queryStartRecord;
					Win1.document.writeln('<a href="javascript:' + theFrame + '.resGetMoreData(' + nextRecord + ')">' + msgList[63] + '</a>');
				}
			}

			// NPWS Mod to add link to search for Reseve again
			var theImgSource=appDir + "images/find_1.gif";
			var theAspSource=appDir + "findRes.htm";
			Win1.document.writeln('</FONT>');
			Win1.document.writeln('<FONT FACE="Arial,sans-serif" SIZE="-1">');
			Win1.document.writeln('<BR>');
			Win1.document.write('<img src="' + theImgSource + '" width=22 height=22 hspace=0 ');
			Win1.document.writeln('vspace=0 border=0 name="Park" alt="Search for Reserve">'); 		
			Win1.document.writeln('<B><A HREF="' + theAspSource + '">Search for Reserve</A></B>');

			//Win1.document.writeln('</font></center></body></html>');
			Win1.document.writeln('</font></body></html>');
			Win1.document.close();
		}
		//alert("toolMode:" + toolMode);
		
		if ((queryStartRecord==1) && ((toolMode!=4) && (toolMode!=50)) && (toolMode!=25)) {
			if ((selectCount==1) && (zoomToSingleSelect)) {
				var fWidth = selectRight[0] - selectLeft[0];
				var fHeight = selectTop[0] - selectBottom[0];
				var mWMargin = 0;
				var mHMargin = 0;
				if (selectType=="point") {			
					mWMargin = fullWidth * selectPointMargin;
					mHMargin = fullHeight * selectPointMargin;
					if (mWMargin > xDistance/2) {
						mWMargin = xDistance/2;
						mHMargin = yDistance/2;
					}
				} else {
					mWMargin = fWidth * selectMargin;
					mHMargin = fHeight * selectMargin;
				}
				saveLastExtent();
				eLeft = selectLeft[0] - mWMargin;
				eRight = selectRight[0] + mWMargin;
				eTop = selectTop[0] + mHMargin;
				eBottom = selectBottom[0] - mHMargin;
			}

			sendMapXML();


		}

			
	} else {
		//alert(tempString);
		if ((useExternalWindow) || (!useQueryFrame)) {
			Win1 = window.open("","QueryWindow","width=575,height=200,scrollbars=yes,resizable=yes");
		} else {
			Win1 = parent.QueryFrame;
			Win1.document.open();
		}
		Win1.document.writeln('<html><meta http-equiv="Content-Type" content="text/html; charset=' + charSet + '"><head>');
		Win1.document.writeln('	<title>' + titleList[8] + '</title>');
		Win1.document.writeln('</head>');
		Win1.document.writeln('<body BGCOLOR="White" TEXT="Black" LEFTMARGIN=0 TOPMARGIN=0>');
		Win1.document.writeln('<FONT FACE="Arial"><B>You are currently searching in the ' + LayerName[ActiveLayerIndex] +' layer</B></font><FONT FACE="Arial" size="-2">');
		Win1.document.writeln('<br><br>' + msgList[64]);
		Win1.document.writeln('<br><br>Try entering just the first few letters to find the reserve you are after');
		if (debugOn>0) {
			Win1.document.writeln('<p>' + msgList[65] + '<br>');
			Win1.document.writeln(untag(theReply));
		} else {
			if (theError!="") {
				Win1.document.writeln('<p>' + msgList[66] + '<br>');
				Win1.document.writeln(theError);
			}
		}

		//NPWS	
		var theAspSource=appDir + "SubMenu.htm";
		Win1.document.writeln('<BR>');
		Win1.document.writeln('<BR>');
		Win1.document.write('<FONT FACE="Arial" SIZE="-1">');
		Win1.document.writeln('<B><A HREF="' + theAspSource + '"> Return to Menu </A></B></FONT>');

		Win1.document.writeln('</FONT>');
		Win1.document.writeln('</body></html>');
		Win1.document.close();
	}
	if (toolMode==4) selectCount=0;
	Win1=null;
	hideRetrieveData();
}

// Reserve find Form from aimsQuery.js
//NPWS MES
// Modified version of findForm function to hard code search for a Reserve Name
function resfindForm() {
	if (checkIfActiveLayerAvailable()) {
		setLayerFields(ActiveLayerIndex);
		//if (useQueryFrame) {
			//MES
			//parent.QueryFrame.document.location= appDir + "findForm.htm";
			parent.QueryFrame.document.location= appDir + "findRes.htm";
			
		//} else {
			//MES
			//Win1 = open(appDir + "findForm.htm","QueryWindow","width=575,height=150,scrollbars=yes,resizable=yes");
		//	parent.QueryFrame.document.location= appDir + "findRes.htm";
		//}
	}
}

// NPWS MES
// Modified writeQueryXML for Reserve Find from aimsQuery.js
// write out XML request to query attributes
function resWriteQueryXML(queryString) {
	if (swapSelectFields) {
		selectFields=selFieldList[ActiveLayerIndex];
	}

	//var fieldListString =  "SHORT_NAME RES_CODE FIND_NAME";

	var fieldListString =  "RES_CODE FIND_NAME";
	var idfieldListString = LayerIDField[ActiveLayerIndex] + " " + LayerShapeField[ActiveLayerIndex];
	var theFields = idfieldListString + " " + fieldListString;


	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" geometry="false" envelope="true"';
	theString += ' featurelimit="' + maxFeaturesReturned + '" beginrecord="' + queryStartRecord + '">\n';
	// MES theString += '<LAYER id="' + ActiveLayer + '" />';
	theString += '<LAYER id="' + 3 + '" />';
	if (useLimitExtent) {
		// keep this within the limitExtent
		theString += '<SPATIALQUERY subfields="' + theFields + '" where="' + queryString + '">';
		theString += '<SPATIALFILTER relation="area_intersection">\n';
		theString += '<ENVELOPE maxx="' + forceComma(limitRight) + '" maxy="' + forceComma(limitTop) + '" minx="' + forceComma(limitLeft) + '" miny="' + forceComma(limitBottom) + '" />\n';
		theString += '</SPATIALFILTER>\n';
		theString += '</SPATIALQUERY>\n';
	} else {
	//theString += '<QUERY subfields="' + theFields + '" where="' + queryString + '" />'; //MES
	theString += '<SPATIALQUERY subfields="' + theFields + '" where="' + queryString + '" />'; //MES
	}
	theString += '</GET_FEATURES>';
	theString += '</REQUEST>';
	theString += '</ARCXML>';
	//alert("writeQueryXML()\nQuery XML Request:\n" + 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;
	
}

// A new attempt at send Reserve find request
// based on a modified/simplified getFind function to hard code searching for a Reserve
// NPWS MES
function resFind(theValue) {
	//alert(theValue);
	selectCount=0;
	showBuffer=false;
	highlightedOne="";
	selectPoints.length=0;
	selectLeft.length=0;
	selectRight.length=0;
	selectTop.length=0;
	selectBottom.length=0;
	drawSelectBoundary=false;
	showGeocode=false;
	clickCount=0;
	totalMeasure=0;
	currentMeasure=0;
	selectionMode=1;
	var theNewQueryString = "";
	var theTempString = "";
	var idfieldListString = LayerIDField[ActiveLayerIndex] + " " + LayerShapeField[ActiveLayerIndex];
	var fieldListString =  "RES_CODE FIND_NAME"
	//var fieldListString =  "#ALL#"
	theValue = theValue.toUpperCase();
	theNewQueryString = "(" + "FIND_NAME" + " LIKE '" + theValue + "%')";
	fieldListString = idfieldListString + " " + fieldListString;

	if (theNewQueryString!="") {
		showRetrieveData();
		showBuffer=false;
		theNewQueryString=makeXMLsafe(theNewQueryString);
		setQueryString = theNewQueryString;
		var holdLayer = ActiveLayer;
		ActiveLayer = 3;
		var theString = writeFindRequest(theNewQueryString,fieldListString);
		ActiveLayer = holdLayer;
		//alert(theString + resFindXMLMode); //MES
		sendToServer(imsQueryURL,theString,resFindXMLMode);
	} else {
		alert(msgList[80]);
	}
}

function getASRImage(sessionId) {
	//alert(sessionId);
	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_IMAGE>\n<PROPERTIES>';
	theString += '<ENVELOPE minx="-180" miny="-90" maxx="180" maxy="90" />\n';
	theString +='<IMAGESIZE height="' + iHeight + '" width="' + iWidth + '" />\n</PROPERTIES>\n<WORKSPACES>';
	theString += '<SHAPEWORKSPACE name="' + sessionId + '" directory="D:\\Projects\\USA\\MapData" /></WORKSPACES>\n';
    theString += '<LAYER type="featureclass" name="ROADS2" visible="true" id="roads2">\n';
	theString += '<DATASET name="ROADS" type="line" workspace="' + sessionId + '" />\n';
	theString += '<SIMPLERENDERER>\n<SIMPLELINESYMBOL width="1" captype="round" color="227,127,27" />\n</SIMPLERENDERER>';
	theString += '</LAYER>\n</GET_IMAGE>\n</REQUEST>\n</ARCXML>';
	alert(theString);
	sendToServer(imsURL,theString,1);
	}




// NPWS MES
// request more records to display in Reserve Find
function resGetMoreData(startRecord) {
	var theString = "";
	queryStartRecord = startRecord;
	if (selectionMode==1) {
		//query
		theString = resWriteQueryXML(setQueryString);
		//MES
		//alert("Set Query String = " + setQueryString + "\n theString = " + theString + "/n Select XML Mode = " + selectXMLMode);
	} else if (selectionMode==2) {
		theString = writeGetFeatures3();
	} else {
		var theType = clickType-1;
		theString = writeShapeSelect(theType);
	}
	showRetrieveData();
	sendToServer(imsQueryURL,theString,resFindXMLMode);
}




    //WMS-CSGI

    //*********************************************************************
    //*	START of Code to Add WMS Background and Overlay Layers            *
    //* The OGCNumBackgrounds is used to tell the system how many URLs    *
    //* are defined in the following array.  The URLs are stored as       *
    //* strings with the complete OGC connection information minus the    *
    //* BBOX=-130,20,-90,40&WIDTH=400&HEIGHT=400& section which will be   *
    //* appended when the layer is made and updated when the extents      *
    //* change.  Changing OGCNumBackgrounds to 0 will make the system     *
    //* ignore the backgrounds, so you do not need to delete the URLs.    *
    //*********************************************************************

    var OGCNumBackgrounds = 4;
    //THe URL of the OGC map background minus the BBOX and WIDTH/HEIGHT.  (Array is 0 to OGCNumBackgroungs - 1)
    var OGCBackgroundURL = new Array();
    OGCBackgroundURL[0] = 'http://it.dlwc.nsw.gov.au/cgi-bin/get.pl?WMTVER=1.0.0&SRS=epsg%3A4202&REQUEST=map&LAYERS=nsw_landsat2000&FORMAT=GIF&EXCEPTIONS=WMS_XML&STYLES=no+style';
    OGCBackgroundURL[1] = 'http://is.dlwc.nsw.gov.au/cgi-bin/wms_wrapper.pl?WMTVER=0.1&SRS=4202&REQUEST=map&FORMAT=GIF&TRANSPARENT=true&EXCEPTIONS=INIMAGE&LAYERS=PRAIN&STYLES=4326%2Bhttp%3A%2F%2Fviz.globe.gov%2Fviz-bin%2Fwmt.cgi%3F%2BREFERENCE';
    OGCBackgroundURL[2] = 'http://is.dlwc.nsw.gov.au/cgi-bin/fornet3/dlwc.pl?object=ds&ds.method=getimage&ds.getimage.format=GIF&ds.getimage.layer=auslig_postcode&ds.getimage.context.projection=AGD66%2FGeographic&ds.getimage.filter=&ds.getimage.symicon=&ds.getimage.symstyle=no+style&ds.getimage.label=';
    OGCBackgroundURL[3] = 'http://is.dlwc.nsw.gov.au/cgi-bin/fornet3/auslig/auslig.pl?object=ds&ds.method=getimage&ds.getimage.format=GIF&ds.getimage.layer=auslig_builtarea&ds.getimage.context.projection=AGD66%2FGeographic&ds.getimage.filter=&ds.getimage.symicon=&ds.getimage.symstyle=no+style&ds.getimage.label=';

    //Name is the displayed name of this layer in the toc.htm code, it is also put in the alt tag of the image
    var OGCBackgroundName = new Array();
    OGCBackgroundName[0] = 'NSW LandSat 2000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp';
    OGCBackgroundName[1] = 'Daily Rainfall - NASA';
    OGCBackgroundName[2] = 'Post Code Boundaries';
    OGCBackgroundName[3] = 'Builtup areas - 1:1M';

    //Visible is used to determine if a layer should be shown, again used by toc.htm (and initial creation)
    //To save time on initial load set them all to false
    var OGCBackgroundVisible = new Array();
    OGCBackgroundVisible[0] = false;
    OGCBackgroundVisible[1] = false;
    OGCBackgroundVisible[2] = false;
    OGCBackgroundVisible[3] = false;

    var WMSAcetateVisible = new Array();
    WMSAcetateVisible[0] = false;
    WMSAcetateVisible[1] = false;
    WMSAcetateVisible[2] = false;
    WMSAcetateVisible[3] = false;

    var isCSGI = new Array();
    isCSGI[0] = false;
    isCSGI[1] = false;
    isCSGI[2] = true;
    isCSGI[3] = true;


    //Acetate is used draw layer on top of background and IMS images. Need to make sure that
    //the image is gif or png otherwise it won't be transparent 
 /*
    var WMSNumAcetates = 4;

	
    //THe URL of the WMS map acetate the BBOX and WIDTH/HEIGHT.  (Array is 0 to WMSNumAcetates - 1)
    var WMSAcetateURL = new Array();
    WMSAcetateURL[0] = 'http://it.dlwc.nsw.gov.au/cgi-bin/get.pl?WMTVER=1.0.0&SRS=epsg%3A4202&REQUEST=map&LAYERS=nsw_landsat2000&FORMAT=GIF&EXCEPTIONS=WMS_XML&STYLES=no+style';
    WMSAcetateURL[1] = 'http://is.dlwc.nsw.gov.au/cgi-bin/wms_wrapper.pl?WMTVER=0.1&SRS=4202&REQUEST=map&FORMAT=GIF&TRANSPARENT=true&EXCEPTIONS=INIMAGE&LAYERS=PRAIN&STYLES=4326%2Bhttp%3A%2F%2Fviz.globe.gov%2Fviz-bin%2Fwmt.cgi%3F%2BREFERENCE';



    var WMSAcetateName = new Array();
    WMSAcetateName[0] = 'NSW LandSat 2000';
    WMSAcetateName[1] = 'Daily Rainfall - NASA';
    //WMSAcetateName[2] = false;
    //WMSAcetateName[3] = false;
*/    
    //Function to update the background layers - called from sendtoserver in aimsxml.js
    //The code was taken from aimsdhtml.js, calling the function from here to change the 
    //content gave object errors.
    function UpdateBackgrounds() {
	//alert("UpdateBackgrounds Called!!!");
/*
    	for (var i=0;i<OGCNumBackgrounds;i++) {
        var BackgroundMapURL = OGCBackgroundURL[i] + '&WIDTH=' + mWidth + '&HEIGHT=' + mHeight + '&BBOX=' + eLeft + ',' + eBottom + ',' + eRight + ',' + eTop;                                                         
        var str = 'document.BackgroundMap' + i + '.src = "' + BackgroundMapURL + '"';
        eval(str);
      }
*/

	//alert("eLeft = " + eLeft + "/n startLeft = " + startLeft);

     	if (enableWMS) {
           	for (var i=0;i<OGCNumBackgrounds;i++) {
           		//alert("isCSGI = " + isCSGI[i]);
			if (isCSGI[i]) {
         			var BackgroundMapURL = OGCBackgroundURL[i] + '&ds.getimage.size=' + mWidth + ',' + mHeight + '&ds.getimage.context.extent=' + eLeft + ',' + eBottom + ',' + eRight + ',' + eTop;                                                         
        			var str = 'document.BackgroundMap' + i + '.src = "' + BackgroundMapURL + '"';
        			eval(str);
			}  else {
          			var BackgroundMapURL = OGCBackgroundURL[i] + '&WIDTH=' + mWidth + '&HEIGHT=' + mHeight + '&BBOX=' + eLeft + ',' + eBottom + ',' + eRight + ',' + eTop;
         			var str = 'document.BackgroundMap' + i + '.src = "' + BackgroundMapURL + '"';
        			eval(str);
			}
            }
           	for (var i=0;i<OGCNumBackgrounds;i++) {
           		//alert("isCSGI = " + isCSGI[i]);
			if (isCSGI[i]) {
         			var AcetateURL = OGCBackgroundURL[i] + '&ds.getimage.size=' + mWidth + ',' + mHeight + '&ds.getimage.context.extent=' + eLeft + ',' + eBottom + ',' + eRight + ',' + eTop;                                                         
        			var str = 'document.AcetateMap' + i + '.src = "' + AcetateURL + '"';
        			eval(str);
			}  else {
          			var AcetateURL = OGCBackgroundURL[i] + '&WIDTH=' + mWidth + '&HEIGHT=' + mHeight + '&BBOX=' + eLeft + ',' + eBottom + ',' + eRight + ',' + eTop;
         			var str = 'document.AcetateMap' + i + '.src = "' + AcetateURL + '"';
        			eval(str);
			}
            }
	}
      return true;
    }

    /*

    function UpdateAcetates() {
	//alert("UpdateAcetates Called!!!");
    	for (var i=0;i<WMSNumAcetates;i++) {
        var AcetateURL = WMSAcetateURL[i] + '&WIDTH=' + mWidth + '&HEIGHT=' + mHeight + '&BBOX=' + eLeft + ',' + eBottom + ',' + eRight + ',' + eTop;                                                         
        var str = 'document.AcetateMap' + i + '.src = "' + AcetateURL + '"';
        eval(str);
      }
      return true;
    }	

    */

    //Function to change layer visibility - called from toc.htm (Layer List)
    function ChangeBackground(i) {
      if (OGCBackgroundVisible[i]) { 
        hideLayer('theBackground' + i);
        OGCBackgroundVisible[i] = false;
      } else {
        showLayer('theBackground' + i);
        OGCBackgroundVisible[i] = true;
      }
      return true;
    }

    //Function to change layer WMS Layer position if Acetate=true layer is on top otherwise it is background
    function ChangeAcetate(i) {
      if (WMSAcetateVisible[i]) { 
        hideLayer('theAcetate' + i);
        WMSAcetateVisible[i] = false;
      } else {
        showLayer('theAcetate' + i);
        WMSAcetateVisible[i] = true;
      }
      return true;
    }
    //*********************************************************************
    //*			  END of Code to Add OGC Background Layers                    *
    //*********************************************************************



