// aimsCustom.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*		dependent on aimsXML.js, ArcIMSparam.js, aimsCommon.js, aimsMap.js,
*		aimsLayers.js, aimsDHTML.js
*		aimsClick.js, aimsNavigation.js,
*/

// global variables
	var aimsCustomPresent4=false;
	var aimsCustomPresent5=false;
	var highlightedZip="";
	var theGlobalLat;
	var theGlobalLon;
	var deltaLat = 0.05;
	var deltaLon = 0.05;
			
	// change these to send XML response to custom function.
	// use numbers >= 1000 and match in useCustomFunction()
	// defaults are defined in aimsXML.js and use standard functions
	
	// xml response mode for selection
	selectXMLMode = 6;
	// xml response mode for identify
	identifyXMLMode = 7;
	// xml response mode for query
	queryXMLMode = 8;
	// xml response mode for find
	findXMLMode = 14;
	// xml response mode hyperlink
	hyperlinkXMLMode = 15;
	// xml response for zip code
//	zipXMLMode = 1006

// custom function for handling clicks 
// 		flow redirected here when
//		toolMode set to >=1000
function customMapTool(e) {
				if (toolMode == 1001) {
						// insert code here
						return false;
					
				}
				if (toolMode == 1002) {
					var theX = mouseX;
					var theY = mouseY;
					getMapXY(theX,theY);
					var u = Math.pow(10,numDecimals);
					var uX = parseInt(mapX * u + (5/10)) / u
					var uY= parseInt(mapY * u + (5/10)) / u
					var latLonDDStr = convertAlb(uX,uY);
					var ll = latLonDDStr.split("|");
					var LonDD = ll[0];
					var LatDD = ll[1];
					latlonHazardXY1(LatDD,LonDD);
				}

}

// send  XML response to custom function
//		flow  redirected here when
//		XMLMode >=1000
function useCustomFunction(theReply) {
	if (XMLMode==1001) {
		// insert code here
	} else if (XMLMode==1002) {
		// insert code here
	} else {
		alert(msgList[55] + XMLMode + msgList[56]);
	}
	hideLayer("LoadData");
}

// add custom stuff to Map XML request. . . between selection and geocode
function addCustomToMap1(){
	var customString = "";
	/*
		customString += '<LAYER type="ACETATE" name="theMode">\n';
		customString += '<OBJECT units="PIXEL">\n<TEXT coord="5,' + (iHeight-10) + '" label="This is a test">\n';
		customString += '<TEXTMARKERSYMBOL fontstyle="BOLD" fontsize="12" font="ARIAL" fontcolor="' + modeMapColor + '" ';
		customString += 'threed="TRUE" glowing="' + modeMapGlow + '" />\n</TEXT>\n</OBJECT>';
		customString += '\n</LAYER>\n';
	*/
	return customString;
}

// add custom stuff to Map XML request. . . between clickpoints and copyright
function addCustomToMap2(){
	var customString = "";
	
	return customString;
}

// add custom stuff to Map XML request. . . under modeOnMap
function addCustomToMap3(){
	var customString = "";
		/*
		customString += '<LAYER type="ACETATE" name="theMode">\n';
		customString += '<OBJECT units="PIXEL">\n<TEXT coords="5 ' + (iHeight-10) + '" label="This is a test">\n';
		customString += '<TEXTMARKERSYMBOL fontstyle="BOLD" fontsize="12" font="ARIAL" fontcolor="' + modeMapColor + '" ';
		customString += 'threed="TRUE" glowing="' + modeMapGlow + '" />\n</TEXT>\n</OBJECT>';
		customString += '\n</LAYER>\n';
		alert(customString);
		*/
	return customString;
}

// add custom stuff to Map XML request. . . on top of everything
function addCustomToMap4(){
	var customString = "";
	if(highlightedZip!=""){
		customString +='<LAYER type="featureclass" name="Highlighted Feature" visible="true">\n';
		customString +='<DATASET fromlayer="4" />\n';
		customString +='<QUERY where="' + highlightedZip + '" />\n';
		customString +='<SIMPLERENDERER>\n';
		customString +='<SIMPLEMARKERSYMBOL color="0,0,0" outline="0,0,0" type="star" width="18" />\n';
		customString +='</SIMPLERENDERER>\n';
		customString +='</LAYER>\n';
	}
	return customString;
}

// add custom stuff to Map XML request. . . under modeOnMap
function addCustomToMap5(){
	var customString = "";
		
		customString += '<LAYER type="ACETATE" name="theHazLatLon">\n';
		customString += '<OBJECT units="DATABASE">\n<COORDSYS id="4008"/><POINT coords="' + theGlobalLon + ' ' + theGlobalLat + '">\n';
		customString += '<SIMPLEMARKERSYMBOL color="0,0,0" outline="255,0,0" type="star" width="18" />\n</POINT>\n</OBJECT>';
		customString += '\n</LAYER>\n';
//		alert(customString);
		
	return customString;
}


//send request for hazard by lat/lon
function getHazPoints(theLat, theLon) {
	aimsCustomPresent5 = true;
	theLon = Math.abs(theLon) * -1.0;
    theGlobalLat = theLat;
	theGlobalLon = theLon;
	zipXMLMode = 1007;
	selectCount=0;
	showBuffer=false;
	highlightedOne="";
	selectPoints.length=1;
	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 elemCount = 0;
	
	if (Math.round(parseFloat(theLat) * 20) == parseFloat(theLat) * 20){
		theLatBottom = parseFloat(theLat) - deltaLat / 2;
		theLatTop = parseFloat(theLat) + deltaLat / 2;
	}
	else{
		theLatBottom = parseFloat(theLat) - deltaLat;
		theLatTop = parseFloat(theLat) + deltaLat;
	}
	
	if (Math.round(parseFloat(theLon) * 20) == parseFloat(theLon) * 20){
		theLonLeft = parseFloat(theLon) - deltaLon / 2;
		theLonRight = parseFloat(theLon) + deltaLon / 2;
	}
	else{
	theLonLeft = parseFloat(theLon) - deltaLon;
	theLonRight = parseFloat(theLon) + deltaLon;
	}
	
	theLatLonEnvelopeString = 'minx="' + forceComma(theLonLeft) + '" ' + 'miny="' + forceComma(theLatBottom) + '" ' + 'maxx="' + forceComma(theLonRight) + '" ' + 'maxy="' + forceComma(theLatTop) + '"';
	theImsService = imsURL.substr(imsURL.indexOf("=")+1,8);
	theImsService = theImsService.toUpperCase();
	
	if (theLatLonEnvelopeString!="") {
		showRetrieveData();
		showBuffer=false;
//		theLatLonEnvelopeString=makeXMLsafe(theLatLonEnvelopeString);
//		alert(theLatLonEnvelopeString);
//		setQueryString = theNewQueryString;
		var theString = writeHazPointsRequest(theLatLonEnvelopeString);
//		alert(zipXMLMode);
		sendToServer(imsLatLonQueryURL,theString,zipXMLMode);
//		parent.MapFrame.showHighLight(0);
	
	} else {
		alert(msgList[80]);
	}
}

function writeHazPointsRequest(theEnvelope) {
	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" compact="false" geometry="true" envelope="false"';
	theString += ' featurelimit="' + maxFeaturesReturned + '" beginrecord="' + queryStartRecord + '">\n';
	theString += '<LAYER id="0" />\n';
	theString += '<SPATIALQUERY subfields="#SHAPE# ' + theImsService + ' ">\n';
		theString += '<SPATIALFILTER relation="envelope_intersection">\n';
		theString += '<ENVELOPE ' + theEnvelope + ' />\n';
		theString += '</SPATIALFILTER>\n';
	theString += '</SPATIALQUERY>\n';
	theString += '</GET_FEATURES>\n';
	theString += '</REQUEST>\n';
	theString += '</ARCXML>';
	selectLayer=ActiveLayer;
	selectType=ActiveLayerType;
	selectCount=0;
	hightlightedOne="";
	selectPoints.length=1;
	selectLeft.length=1;
	selectRight.length=1;
	selectTop.length=1;
	selectBottom.length=1;

	return theString;
}

//display the lat/lon hazard value
function displayLatLonHazData(theReply) {
//	setLayerFields(ActiveLayerIndex);
//	alert(theReply);
	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;
	var theHazValue = 0;
	var fHazValues = new Array();
	var fHazX = new Array();
	var fHazY = new Array();
	var t = new Array();
	var u = new Array();
	var z = new Array();
		
	//alert("displayAttributeData()[featurecount=" + featureCount + "]");
	selectCount = 0;
	var tempString="";
	
	endpos = 1;
	theAttribute = imsURL.substr(imsURL.indexOf("=")+1,8);
	theAttributeStr = theAttribute + '="';
	theAttributeStr = theAttributeStr.toUpperCase();
			
	for (var i=0;i<fCount;i++) {
		var pos = theReply.indexOf(theAttributeStr, startpos);
		if (pos!=-1) {
			startpos = pos + 10;
			xmlEndPos = theReply.indexOf('" #SHAPE#',startpos);
			fHazValues[i] = theReply.substring(startpos,xmlEndPos);
		}
		var pos = theReply.indexOf('POINT x="',startpos);
		if (pos!=-1) {
			startpos = pos + 9;
			xmlEndPos = theReply.indexOf('" y="',startpos);
			fHazX[i] = Math.round(theReply.substring(startpos,xmlEndPos)*100)/100;
		}
		pos = xmlEndPos;
		if (pos!=-1) {
			startpos = pos + 5;
			xmlEndPos = theReply.indexOf('"/>',startpos);
			fHazY[i] = Math.round(theReply.substring(startpos,xmlEndPos)*100)/100;
		}		
        t[i] = 1 - (Math.abs(theGlobalLon - fHazX[i]) / deltaLon);
        u[i] = 1 - (Math.abs(theGlobalLat - fHazY[i]) / deltaLat);
		z[i] = t[i] * u[i] * fHazValues[i];
		theHazValue = theHazValue + z[i];
	}

	theHazValue = Math.round(theHazValue * 1000) / 1000;
	alert(theHazValue);
	hideRetrieveData();
	sendMapXML();
}


// send find request for zip code
function getFindZip(theValue) {
	aimsCustomPresent4 = true;
	zipXMLMode = 1006;
	selectCount=0;
	showBuffer=false;
	highlightedOne="";
	selectPoints.length=1;
	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 fieldListString = LayerIDField[ActiveLayerIndex] + " " + LayerShapeField[ActiveLayerIndex];
	var fieldListString = LayerIDField[3] + " " + LayerShapeField[3];
	var elemCount = 0;
	fieldListString = "#ID# #SHAPE# ZIP STATE PO_NAME PGA050 PGA100 PGA250 HZ1050 HZ1100 HZ1250 HZ3050 HZ3100 HZ3250 HZ5050 HZ5100 HZ5250";
//	alert(fieldListString);
	
	theNewQueryString = "(ZIP = '" + theValue + "')";

	if (theNewQueryString!="") {
		showRetrieveData();
		showBuffer=false;
		theNewQueryString=makeXMLsafe(theNewQueryString);
		setQueryString = theNewQueryString;
		var theString = writeFindZipRequest(theNewQueryString,fieldListString);
//		alert(zipXMLMode);
		sendToServer(imsQueryURL,theString,zipXMLMode);
//		parent.MapFrame.showHighLight(0);
	
	} else {
		alert(msgList[80]);
	}
}

// write out find form for zip code

function writeFindZipRequest(findQuery,fieldList) {
	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" geometry="false" envelope="true"';
	theString += ' featurelimit="' + maxFeaturesReturned + '" beginrecord="' + queryStartRecord + '">\n';
	theString += '<LAYER id="4" />\n';
	if (useLimitExtent) {
		// keep this within the limitExtent
		theString += '<SPATIALQUERY subfields="' + fieldList + '" where="' + findQuery + '" />';
		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="' + fieldList + '" where="' + findQuery + '" />';
	}
	theString += '</GET_FEATURES>\n';
	theString += '</REQUEST>\n';
	theString += '</ARCXML>';
	//alert(theString);
	selectLayer=ActiveLayer;
	selectType=ActiveLayerType;
	selectCount=0;
	hightlightedOne="";
	selectPoints.length=1;
	selectLeft.length=1;
	selectRight.length=1;
	selectTop.length=1;
	selectBottom.length=1;

	return theString;
}

// parse XML response for selected features
function displayZipAttributeData(theReply) {
//	alert(theReply);
	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;
	var fZipAlias = new Array();
	fZipAlias[0] = "Zip code";
	fZipAlias[1] = "State";
	fZipAlias[2] = "Post office";
	fZipAlias[3] = "PGA 10% PE in 50 yrs";
	fZipAlias[4] = "PGA 5% PE in 50 yrs";
	fZipAlias[5] = "PGA 2% PE in 50 yrs";
	fZipAlias[6] = "1.0 sec 10% PE in 50 yrs";
	fZipAlias[7] = "1.0 sec 5% PE in 50 yrs";
	fZipAlias[8] = "1.0 sec 2% PE in 50 yrs";
	fZipAlias[9] = "0.3 sec 10% PE in 50 yrs";
	fZipAlias[10] = "0.3 sec 5% PE in 50 yrs";
	fZipAlias[11] = "0.3 sec 2% PE in 50 yrs";
	fZipAlias[12] = "0.2 sec 10% PE in 50 yrs";
	fZipAlias[13] = "0.2 sec 5% PE in 50 yrs";
	fZipAlias[14] = "0.2 sec 2% PE in 50 yrs";

	
	//alert("displayAttributeData()[featurecount=" + featureCount + "]");
	selectCount = 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><meta http-equiv="Content-Type" content="text/html; charset=' + charSet + '"><head><title>' + titleList[7] + '</title></head>');
			Win1.document.writeln('<body bgcolor="' + textFrameBackColor + '" text="Black" link="Blue" vlink="Gray" LEFTMARGIN=0 onload="window.focus()">');
			Win1.document.writeln('<center>');
//			Win1.document.writeln('<FONT FACE="Arial" SIZE="-1"><b>' + LayerName[3] + '</b></FONT>');
			Win1.document.writeln('<table border="1" cellspacing="0" cellpadding="2" nowrap bgcolor="' + tableBackColor + '">');
		}
		endpos = 1;
		
		for (var i=0;i<fCount;i++) {
			//alert(endpos);
			if (toolMode!=4) {
				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);
			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[3]) showHyper=true;
					}
				}
					
				var fName1 = getFieldNames(selectedData);
				var fValue1 = getFieldValues(selectedData);
				selectPoints[selectCount] = getIdValue(fName1, fValue1);
//				alert(selectPoints[selectCount]);
				
				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>');
					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++) {
									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">' + fZipAlias[f] + '</FONT></a></th>');
//								Win1.document.write('<th><FONT FACE="Arial" SIZE="-2">' + fName1[f] + '</FONT></a></th>');
							}
						}
					}
					Win1.document.writeln('</tr>');
				}
				Win1.document.write('<tr><td>');
				if (toolMode!=4) Win1.document.write('<a href="javascript:' + theFrame + '.showHighlight(' + selectCount + ')">');
				Win1.document.write('<FONT FACE="Arial" SIZE="-2">' + (selectCount+queryStartRecord) + '</FONT>');
				if (toolMode!=4) 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="-2">' + theDate + '</FONT>');
							theDate=null;
						} else {
							// put html entity for spaces so display is correct - browsers trim extra spaces
							tempString = fValue1[f].replace(/ /g, "&nbsp;");
							Win1.document.write('<FONT FACE="Arial" SIZE="-2">' + tempString + '</FONT>');
							//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;
				
			}
			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 + '.getMoreData(' + prevRecord + ')">' + msgList[61] + maxFeaturesReturned + msgList[62] + '</a>&nbsp;&nbsp;&nbsp;');
				}
				if (stillMore) {
					var nextRecord=featureCount + queryStartRecord;
					Win1.document.writeln('<a href="javascript:' + theFrame + '.getMoreData(' + nextRecord + ')">' + msgList[63] + '</a>');
				}
			}
			if ((toolMode!=4) && (toolMode!=25) && (selectCount>1)) {
				Win1.document.writeln('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b><a href="javascript:' + theFrame + '.zoomToReturnedRecords()">Zoom to these records</A></b>');
			}

			Win1.document.writeln('</font></center></body></html>');
			Win1.document.close();
		}
//		alert("toolMode:" + toolMode);
		
		if ((queryStartRecord==1) && (toolMode!=4) && (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;
			}
			highlightedZip = LayerIDField[4] + " = " + selectPoints[0];
			sendMapXML();
		}
					
	} 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><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>' + LayerName[3] +'</B></font><FONT FACE="Arial" size="-2">');
		Win1.document.writeln('<br>' + msgList[64]);
		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);
			}
		}
		Win1.document.writeln('</FONT>');
		Win1.document.writeln('</body></html>');
		Win1.document.close();
	}
	if (toolMode==4) selectCount=0;
	Win1=null;
	hideRetrieveData();
}

// extract layers to download
function extractIt() {
	hideLayer("measureBox");
	alert(msgList[51]);
}


// function for converting from Albers to Lat/Lon
function convertAlb(uX,uY) {
	a = 6378206.4;						// radius of the earth in meters, Clarke 1866 ellipsoid
	e = 0.0822715;						// eccentricity (e) of the earth
	ee = 0.0067686;						// e-squared
	lat0 = 0.0;							// latitude of origin in radians
	lat1 = 0.5148721;					// first standard parallel in radians
	lat2 = 0.7941248;					// second standard parallel in radians
	lon0 = -1.6580628;					// central meridian in radians
	
	n = 0.6029035;
	rho0 = 12288000.0;
	C = 1.3491594;
	
	rho = Math.sqrt(uX * uX + (rho0 - uY) * (rho0 - uY));
	q = (C - rho * rho * n * n / (a * a)) / n;
	
	Lat = Math.asin(q / 2);
	
	sinLat = Math.sin(Lat);
	LatNew1 = Math.pow((1 - ee * sinLat * sinLat),2) / (2 * Math.cos(Lat));
	LatNew2 = q / (1 - ee) - sinLat / (1 - ee * sinLat * sinLat);
	LatNew3 = 1 / (2 * e) * Math.log((1 - e * sinLat) / (1 + e * sinLat));
	LatNew = Lat + LatNew1 * (LatNew2 + LatNew3);
	
	Lat = LatNew;
	
	while (Math.abs(LatNew - Lat) > 0.000573) {
		sinLat = Math.sin(Lat);
		LatNew1 = Math.pow((1 - ee * sinLat * sinLat),2) / (2 * Math.cos(Lat));
		LatNew2 = q / (1 - ee) - sinLat / (1 - ee * sinLat * sinLat);
		LatNew3 = 1 / (2 * e) * Math.log((1 - e * sinLat) / (1 + e * sinLat));
		LatNew = Lat + LatNew1 * (LatNew2 + LatNew3);	
	}
	
	theta = Math.atan(uX / (rho0 - uY));
	Lon = lon0 + theta / n;
	
	Lat = Math.round(Lat * 57.29578 * 1000) / 1000;
	Lon = Math.round(Lon * 57.29578 * 1000) / 1000;
//	Lat = Lat.toPrecision(5);
//	Lon = Lon.toPrecision(6);
	
return Lon + "|" + Lat;
//return "110.0|110.0";
}