// aimsGeocode.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
*/

aimsGeocodePresent=true;

var GCLayers = new Array();
var GCLayerId = new Array();
var GCLayerStyle = new Array();
var GCActiveLayer=0;
var GCidCount=0;
var GCid = new Array();
var GClabel = new Array();
var GCdesc = new Array();
var GCvalue = new Array();
var GCpointX = new Array();
var GCpointY = new Array();
var GCpointCount = 0;
var GCscore = new Array();
var GCaddress = new Array();

var GCLayerCount=0;

var SdcGeocodeStyle = "sdcgeocode";
var geocodeAppMode = "locate"; // default mode - other modes (route, address) require RouteServer Extension
var usingGeocode=0; //to identify if using geocoding, set initial = 0
var t;
		if (opener) {
			if (opener.name=="MapFrame") {
				t = opener.parent.MapFrame;
			} else {
				t = opener;
			}
		} else {

			if (parent.MapFrame) {
				t = parent.MapFrame;
			} else {
				t=document;
			}
		}

if (imsGeocodeURL=="") {
	imsGeocodeURL= imsURL + "&CustomService=SDCGeocode";

}

/*
***************************************************************************************

Geocoding functions 

***************************************************************************************
*/

// set up geocode
function setupGeocode() {
		getGeocodeParams();	
}

// get a list of geocoding layers
function getGeocodeLayers() {
	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_SERVICE_INFO />\n';
	theString += '</REQUEST>\n</ARCXML>';
	sendToServer(imsGeocodeURL,theString,20);
	
}

// get the parameters for geocoding the layer
function getGeocodeParams() {
	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_SERVICE_INFO />\n';
	theString += '</REQUEST>\n</ARCXML>';
	var theReply="";
	sendToServer(imsGeocodeURL,theString,26);

}

// write out the geocode XML request
function writeGeocodeXML() {
	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_GEOCODE maxcandidates="' + maxGeocodeCandidates + '" minscore="' + minGeocodeScore + '">\n';
	theString += '<LAYER id="' + GCLayerId[GCActiveLayer] + '" />';
	theString += '<ADDRESS>\n';
	//for (var i=0;i<GCidCount;i++) {
	//	theString += '<GCTAG id="' + GCid[i] + '" value="' + GCvalue[i] + '"/>\n';
	//}
	//parent.addressFrame.document.Search.AddressSearch.value
	theString += '<GCTAG id="Address or Intersection" value="' + parent.addressFrame.document.Search.StreetAdd.value + '"/>\n';
	theString += '<GCTAG id="ZIP" value="' + parent.addressFrame.document.Search.ZIP.value + '"/>\n';
	theString += '<GCTAG id="City" value="' + parent.addressFrame.document.Search.City.value + '"/>\n';
	theString += '<GCTAG id="State" value="' + parent.addressFrame.document.Search.State.value + '"/>\n';
	theString += '</ADDRESS>\n</GET_GEOCODE>\n</REQUEST>\n</ARCXML>\n';
	usingGeocode=1;
//	alert(theString);
	return theString;
	
}

// parse out geocoding layers
function parseGeocodeLayers(theReply) {
	//alert(theReply);
	var pos = 0;
	var startpos = 0;
	var endpos = 0;
	var lpos = 0;
	GCLayers.length=1;
	GCLayerCount=0;
	pos = theReply.indexOf("<LAYERINFO",endpos);
	
		
	while (pos!=-1) {
		lpos = theReply.indexOf('name=',pos);
		if (lpos!=-1) {
			
			startpos = lpos + 6;
			endpos = theReply.indexOf(dQuote,startpos);
			GCLayers[GCLayerCount] = theReply.substring(startpos,endpos);
			startpos = theReply.indexOf('id=',endpos);
			if (startpos!=-1) {
				startpos += 4;
				endpos = theReply.indexOf(dQuote,startpos);
				GCLayerId[GCLayerCount] = theReply.substring(startpos,endpos);
			}
			startpos = theReply.indexOf("<GCSTYLE name=",endpos);
			if (startpos!=-1) {
				startpos += 15;
				endpos = theReply.indexOf(dQuote,startpos);
				GCLayerStyle[GCLayerCount] = theReply.substring(startpos,endpos);
				GCLayerCount=GCLayerCount+1;
			}
			pos = theReply.indexOf("<LAYERINFO",endpos);

		} else {
			// bypass this for now
			//alert(theReply);
			pos =-1;
		}
	}
	
}

// parse out the bacic geocode parameters for the layer
function parseGeocodeParams(theReply,theLayer) {
	//alert(theLayer + "\n" + theReply);
	var pos = 0;
	var startpos = 0;
	var endpos = 0;
	var lpos = 0;
	GCid.length=0;
	GClabel.length=0;
	GCdesc.length=0;
	GCidCount=0;
	var theName = 'name="' + theLayer + '"'
	pos = theReply.indexOf(theName,endpos);
	
	if (pos!=-1) {
		//alert(theName + " at " + pos);
		startpos = pos + 17;
		lpos = theReply.indexOf("</LAYERINFO>",startpos);
		startpos = theReply.indexOf("<GCINPUT id=",startpos);
		while ((startpos<lpos) && (startpos!=-1)) {
			
			//if ((startpos<lpos) && (startpos!=-1)){
				//alert(startpos);
				startpos += 13;
				endpos = theReply.indexOf(dQuote,startpos); 
				GCid[GCidCount] = theReply.substring(startpos,endpos);
				startpos = theReply.indexOf("label=",endpos);
				startpos = startpos + 7;
				endpos = theReply.indexOf(dQuote,startpos); 
				GClabel[GCidCount] = theReply.substring(startpos,endpos);
				startpos = theReply.indexOf("description=",endpos);
				startpos += 13;
				endpos = theReply.indexOf(dQuote,startpos); 
				GCdesc[GCidCount] = theReply.substring(startpos,endpos);
				GCidCount=GCidCount+1;
			//}
			startpos = theReply.indexOf("<GCINPUT id=",endpos);
		}
		return true;
	} else {
		return false;
	}

}

// parse out geocode response and display results in table
function parseGeocodeResults(theReply) {

	//alert(theReply);
	GCscore.length=1;

	var pos = theReply.indexOf("<GCCOUNT count=");
	var lpos = 0;
	var startpos = pos + 16;
	var startpos2=0;
	var endpos = theReply.indexOf(dQuote,startpos);
	var fString = theReply.substring(startpos,endpos);
	GCpointCount=parseInt(fString);
	var gcCount=0;
	//alert(GCpointCount);
	if ((pos>0) && (GCpointCount>0)) {
		pos = theReply.indexOf("<FEATURE");
		if (pos!=-1) {
			while (pos!=-1) {
				lpos = theReply.indexOf("<FIELD",pos);
				if (lpos!=-1) {
					//while (lpos!=-1) {
						startpos2 = theReply.indexOf('name="SCORE"',lpos);
						startpos = theReply.indexOf("FIELDVALUE valuestring=",startpos2);
						startpos = startpos + 24;
						endpos = theReply.indexOf(dQuote,startpos);
						GCscore[gcCount] = theReply.substring(startpos,endpos);
						startpos2 = theReply.indexOf('name="ADDRESSFOUND"',lpos);
						startpos = theReply.indexOf("FIELDVALUE valuestring=",startpos2);
						startpos = startpos + 24;
						endpos = theReply.indexOf(dQuote,startpos);
						GCaddress[gcCount] = theReply.substring(startpos,endpos);
						startpos2 = theReply.indexOf('name="SHAPEFIELD"',lpos);
						startpos = theReply.indexOf("<POINT x=",startpos2);
						startpos += 10;
						endpos = theReply.indexOf(dQuote,startpos);
						GCpointX[gcCount] = theReply.substring(startpos,endpos);
						startpos = theReply.indexOf("y=",endpos);
						startpos = startpos + 3;
						endpos = theReply.indexOf(dQuote,startpos);
						GCpointY[gcCount] = theReply.substring(startpos,endpos);
						gcCount++;
					//	lpos = theReply.indexOf("<FIELD",endpos);
					//}
				}
				pos = theReply.indexOf("<FEATURE",endpos);
			}
		}
		
		
		
		
		if (geocodeAppMode=="locate") {
			
			showGeocode=true;
			geocodeX=GCpointX[0];
			geocodeY=GCpointY[0];
			geocodeLabel=GCaddress[0];				
			hideRetrieveData();
			zoomToSingleSelect = false;
			//} else {
			if (GCpointCount==1) 
			{
	
				//zoom to the street level
				saveLastExtent();
				eLeft = (geocodeX-0.0085);
				eRight = (geocodeX-0.001+0.0095);
				eTop = (geocodeY-0.001+0.0095);
				eBottom = (geocodeY-0.0085);
				
				//sendMapXML();
				
				/////////////////////////////////////////////////////////////////////
				//select the product based of the geocode point
				var activeindex = parent.productFrame.document.productform.products.options[parent.productFrame.document.productform.products.selectedIndex].value;
				//if(parent.productFrame.document.productform.products.selectedIndex!=0)
				//{
					if (activeindex!=99)
					{
						setActiveLayer(activeindex);
						MakeLayerVisible(activeindex);
						searchTolerance=0.001
						var tempWest = geocodeX - searchTolerance;
						var tempNorth = geocodeY + searchTolerance;
						var tempEast = geocodeX + searchTolerance;
						var tempSouth = geocodeY - searchTolerance;
						queryStartRecord=1;
						selectEnvelope='maxy="' + forceComma(tempNorth) + '" maxx="' + forceComma(tempEast) + '" miny="' + forceComma(tempSouth) + '" minx="' + forceComma(tempWest) + '"';
						selectionMode=2;
						showRetrieveData();
						var theString = writeGetFeatures2(tempWest,tempSouth,tempEast,tempNorth);					
						sendToServer(imsQueryURL,theString,selectXMLMode);					
					}
					
					else
					{
						for (var i=0;i<=6;i++) 
						{ //the first 7 layers are msc product layers
							t.LayerVisible[i] = 0;
						}
						t.sendMapXML();
					}

	
	
			}
			else
			{
				var WinAddSearchResults;
				var theFrame = "parent.MapFrame";
				useExternalWindow = true;
					if ((useExternalWindow) || (!useTextFrame)) {
						WinAddSearchResults = window.open("","ExternalGeocodeWindow","width=575,height=120,scrollbars=yes,resizable=yes");
						theFrame = "opener";
						if (parent.MapFrame!=null) theFrame = "opener.parent.MapFrame";
					} else {
						WinAddSearchResults = parent.TextFrame;
						WinAddSearchResults.document.open();
					}							
				WinAddSearchResults.document.open();
				WinAddSearchResults.document.writeln('<html><meta http-equiv="Content-Type" content="text/html; charset=' + charSet + '"><head><title>'+titleList[6] + '</title>'); 
				WinAddSearchResults.document.writeln('<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">');
				WinAddSearchResults.document.writeln('</SCRIPT>');
				WinAddSearchResults.document.writeln('</head>');
				WinAddSearchResults.document.writeln('<body bgcolor="white" text="Black" link="Blue" vlink="Blue" LEFTMARGIN=0 onload="window.focus()">');
				WinAddSearchResults.document.writeln('<font face="arial, helvetica" SIZE="-1"><b>' + GCpointCount + ' address(es) found: </b>');
				WinAddSearchResults.document.writeln('<table border="1" cellspacing="0" cellpadding="2" nowrap bgcolor="' + tableBackColor + '">');
				WinAddSearchResults.document.writeln('<tr><th><font face="arial, helvetica" SIZE="-1">#</font></th><th><font face="arial, helvetica" SIZE="-1">' + msgList[58] + '</font></th><th><font face="arial, helvetica" SIZE="-1">' + msgList[59] + '</th></tr>');
				for (var i=0;i<GCpointCount;i++) {
					var Left = (GCpointX[i]-0.1);
					var Right = (GCpointX[i]-0.1+0.2);
					var Top = (GCpointY[i]-0.1+0.2);
					var Bottom = (GCpointY[i]-0.1);
					WinAddSearchResults.document.writeln('<tr>');
					WinAddSearchResults.document.writeln('<td><font face="arial, helvetica" SIZE="-1"><a href="javascript: ' + theFrame + '.SelectProductByPoint(' + GCpointX[i] + ',' + GCpointY[i] + ',\'' + GCaddress[i] + '\');parent.close();">' + (i+1) + '</a></font></td>');
					//WinAddSearchResults.document.writeln('<td><font face="arial, helvetica" SIZE="-1"><a href="javascript: eLeft = ' + Left + '; ' + 'eRight = ' + Right + '; ' + 'eTop = ' + Top + '; ' + 'eBottom = ' + Bottom + '; ' + theFrame + '.zoomToPoint(' + GCpointX[i] + ',' + GCpointY[i] + ',true,\'' + GCaddress[i] + '\');parent.close();">' + (i+1) + '</a></font></td>');
					//WinAddSearchResults.document.writeln('<td><font face="arial, helvetica" SIZE="-1"><a href="javascript: eLeft=' + Left + '; ' + 'eRight=' + Right + '; ' + 'eTop=' + Top + '; ' + 'eBottom=' + Bottom + '; ' + 'geocodeX=' + GCpointX[i] + '; geocodeY=' + GCpointY[i] + '; geocodeLabel=\'' + GCaddress[i] + '\'; ' + theFrame + '.sendMapXML(); parent.close();">' + (i+1) + '</a></font></td>');
					WinAddSearchResults.document.writeln('<td><font face="arial, helvetica" SIZE="-1">' + GCaddress[i] + '</font></td>');
					WinAddSearchResults.document.writeln('<td><font face="arial, helvetica" SIZE="-1">' + GCscore[i] + '</font></td>');
					WinAddSearchResults.document.writeln('</tr>');
				}
				WinAddSearchResults.document.writeln('</table>');
				WinAddSearchResults.document.close();
				WinAddSearchResults=null;
			//}
			
			useExternalWindow=false;
				//toolMode = 1;
				//if (parent.ToolFrame!=null) parent.ToolFrame.document.location = appDir + "toolbar.htm";
				//if (GCpointCount==1) sendMapXML();
				//if (GCpointCount==1) zoomToPoint(GCpointX[0], GCpointY[0], true, geocodeLabel);
			}
		} else if (geocodeAppMode=="route") {
			
			// requires RouteServer extension
			hideRetrieveData();
			if ( GCpointCount == 1 ) {
				addStop( parseFloat(GCpointX[0]), parseFloat(GCpointY[0]), GCaddress[0], GCaddress[0] );
			} else {
				var url = appDir + "candidates.htm";
				if ((useExternalWindow) || (!useTextFrame)) {
					Win1 = window.open(url,"GeocodeWindow","width=575,height=120,scrollbars=yes,resizable=yes");
				} else {
					parent.TextFrame.document.location = url;
				}
			}
			
		} else if (geocodeAppMode=="address") {
		
			// requires RouteServer extension
			if (zoomToGCPoint) {
				zoomToGCPoint=false;
				hideRetrieveData();
				zoomToRouteEnvelope(parseFloat(GCpointX[0]), parseFloat(GCpointY[0]),parseFloat(GCpointX[0]), parseFloat(GCpointY[0]));
			} else {
				hideRetrieveData();
				//alert(GCaddress[0]);
				writeRGCresultPage(GCaddress[0]);
			}
		}
	} else {
		//no match

		alert(msgList[60]);
		hideRetrieveData();
		//var url = appDir + "addmatch.htm";
		//if ((useExternalWindow) || (!useTextFrame)) {
		//	Win1 = window.open(url,"GeocodeWindow","width=575,height=120,scrollbars=yes,resizable=yes");
		//} else {
		//	parent.TextFrame.document.location = url;
		//}
		
	}
	
}

function MakeLayerVisible(l)
{
	for (var i=0;i<=6;i++) 
	{ //the first 7 layers are msc product layers
		t.LayerVisible[i] = 0;
	}
	t.LayerVisible[l]=1;   
	
}

function SelectProductByPoint(x,y,label)
{	
	showGeocode=true;
	geocodeX=x;
	geocodeY=y;
	geocodeLabel=label;
	//zoom to the street level
	saveLastExtent();
	eLeft = (geocodeX-0.0085);
	eRight = (geocodeX-0.001+0.0095);
	eTop = (geocodeY-0.001+0.0095);
	eBottom = (geocodeY-0.0085);

	//sendMapXML();

	/////////////////////////////////////////////////////////////////////
	//select the product based of the geocode point
	var activeindex = parent.productFrame.document.productform.products.options[parent.productFrame.document.productform.products.selectedIndex].value;
	//if(parent.productFrame.document.productform.products.selectedIndex!=0)
	//{
		if (activeindex!=99)
		{
			setActiveLayer(activeindex);
			MakeLayerVisible(activeindex);
			searchTolerance=0.001
			var tempWest = geocodeX - searchTolerance;
			var tempNorth = geocodeY + searchTolerance;
			var tempEast = geocodeX + searchTolerance;
			var tempSouth = geocodeY - searchTolerance;
			queryStartRecord=1;
			selectEnvelope='maxy="' + forceComma(tempNorth) + '" maxx="' + forceComma(tempEast) + '" miny="' + forceComma(tempSouth) + '" minx="' + forceComma(tempWest) + '"';
			selectionMode=2;
			showRetrieveData();
			var theString = writeGetFeatures2(tempWest,tempSouth,tempEast,tempNorth);
			sendToServer(imsQueryURL,theString,selectXMLMode);					
		}
		else
		{
			for (var i=0;i<=6;i++) 
			{ //the first 7 layers are msc product layers
				t.LayerVisible[i] = 0;
			}
			t.sendMapXML();
		}		
			
				
}