// 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
	aimsCustomPresent=true;
	// 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;




// custom function for handling clicks 
// 		flow redirected here when
//		toolMode set to >=1000
function customMapTool(e) {
				if (toolMode == 1001) {
					//alert("Made it to AimsCustom");
							
				}
				if (toolMode == 1002) {
			                latlon();

				}

}

// send  XML response to custom function
//		flow  redirected here when
//		XMLMode >=1000
function useCustomFunction(theReply) {
	if (XMLMode==1002) {
		var theEnvelope = getEnvelopeXYs(theReply);
		var llminx = theEnvelope[0];
		var llminy = theEnvelope[1];
		var llmaxx = theEnvelope[2];
		var llmaxy = theEnvelope[3];
		var lat = (llmaxy+llminy)/2;
		var lon = (llmaxx+llminx)/2;
		lat = Math.round(lat*1000000)/1000000;
		lon = Math.round(lon*1000000)/1000000;
		var spx = Math.round(mapX*1000000)/1000000;
		var spy = Math.round(mapY*1000000)/1000000;
		theLon=lon;
		theLat=lat;
		parent.MapFrame.llform.Latitude.value = theLat;
		parent.MapFrame.llform.Longitude.value = theLon;
		showLatLon=true;
		sendMapXML();
		
	} else if (XMLMode==1003) {
		var theEnvelope = getEnvelopeXYs(theReply);
		var llpointx = theEnvelope[0];
		var llpointy = theEnvelope[1];
                zoomToPoint(llpointx,llpointy,0)
	} else if (XMLMode==1005) {
	        	var fcount = justGetFeatureCount(theReply);
			var startpos = 0;
			var i = 1;
			
			if (fcount > 1)
			{
			var matchTableHTML = '<HTML><HEAD></HEAD><BODY><TABLE width=\'100%\' name=\'rstable\' id=\'rstable\' cols=2><B><TR><TH>ADDRESS</TH><TH>CITY</TH></TR></B>';
				for (i = 0; i < fcount; i++)
				{
				var x = justGetValue(theReply,'minx="','"',startpos);
				var y = justGetValue(theReply,'miny="','"',startpos);
				var addr = justGetValue(theReply,'ADDRESS="','"',startpos);
				var zone = justGetValue(theReply,'ZONEA="','"',startpos);
				matchTableHTML = matchTableHTML + '<TR><TD><FONT FACE=\'Arial,sans-serif\' SIZE=\'0\'><a href="#A" onclick="parent.MapFrame.dbIdentifyAddress(' + x + ',' + y + ')">' + addr + '</a></FONT></TD><TD><FONT FACE=\'Arial,sans-serif\' SIZE=\'0\'>' + zone + '</FONT></TD></TR>';
				startpos = theReply.indexOf("</FEATURE>",startpos) + 10;
				}
			matchTableHTML = matchTableHTML + '</TABLE></BODY></HTML>';
			parent.TextFrame.document.body.innerHTML = matchTableHTML;
			sortTable(0, parent.TextFrame.rstable);
			}
			
			
			if (fcount == 1)
			{
				var x = parent.MapFrame.justGetValue(theReply,'minx="','"',startpos);
				var y = parent.MapFrame.justGetValue(theReply,'miny="','"',startpos);
				var addr = parent.MapFrame.justGetValue(theReply,'ADDRESS="','"',startpos);
		                parent.MapFrame.dbIdentifyAddress(x,y);
			}
			
			
			if (fcount == 0)
			{
		                alert("The E-911 Address was not found. \n \nTry typing the House Number ONLY and search again.");
	                }
	        
	        
        } else {
		alert(msgList[55] + XMLMode + msgList[56]);
	}
	hideLayer("LoadData");
}

// add custom stuff to Map XML request. . . between selection and geocode
function addCustomToMap1(){
	var customString = "";
	//alert(showLatLon);
	          
	if(showLatLon){
		var llcoords=theLon+' '+theLat;
		var llcoordslbl='LON='+theLon+' '+'LAT='+theLat;
		//alert(llcoords);
		customString += '<LAYER type="ACETATE" name="theMode">\n';
		//customString += '<OBJECT units="database">\n<COORDSYS id="4326" />\n<POINT coords="-79.53000 34.85360">\n';
		customString += '<OBJECT units="database">\n<COORDSYS id="4326" />\n<POINT coords="'+llcoords+'">\n';
		customString += '<SIMPLEMARKERSYMBOL color="0,0,0" width="15" antialiasing="true" outline="0,255,255"/>" ';
		customString += '</POINT>\n</OBJECT>';
		
		customString += '<OBJECT units="database">\n<COORDSYS id="4326" />\n<TEXT coords="'+llcoords+'" label="'+llcoordslbl+'">\n';
		customString += '<TEXTMARKERSYMBOL font="Arial" fontsize="10" color="red" glowing="0,255,255"/>" ';
		customString += '</TEXT>\n</OBJECT>';
		
		customString += '\n</LAYER>\n';
		return customString;
	}
	else{
		//alert("Nothing to show");
			customString = "";
			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="scalebar" visible="true" id="acetate">\n';
//		customString += '<OBJECT units="PIXEL">';
//		customString += '<SCALEBAR fontcolor="0,0,0" coords="2 2" barcolor="255,255,255" fontsize="12" screenlength="98" barwidth="3" mapunits="feet" scaleunits="feet" antialiasing="true" mode="cartesian" />';
//		customString += '</OBJECT>';
//		customString += '</LAYER>';
//		//alert(customString);
		return customString;
 }

// add custom stuff to Map XML request. . . on top of everything
function addCustomToMap4(){
	var customString = "";
	
	return customString;
}

// extract layers to download
function extractIt() {
	hideLayer("measureBox");
	alert(msgList[51]);
}


