// 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 dogeocodestart="yes";
var dogeocodeend="yes";
var onegeocode="";
var doEnd="no";
var firstgeocodename="";
var secondgeocodename="";
var labelgeocodes="no";
var startbuffdist=25;
var endbuffdist=25;
var buffinterval=1;


if (imsGeocodeURL=="") {
	imsGeocodeURL= imsURL + "&CustomService=Geocode";
}

/*
***************************************************************************************

Geocoding functions 

***************************************************************************************
*/

function startTransitRequest(){
	
theEndX=0;
theEndY=0;

	if(parent.TextFrame.theSearchForm.elements[8].checked){
		newbuffdist="660";
	}
	else if(parent.TextFrame.theSearchForm.elements[9].checked){
		newbuffdist="1320";
	}
	else if(parent.TextFrame.theSearchForm.elements[10].checked){
		newbuffdist="2640";
	}

	if((parent.TextFrame.theSearchForm.StartAddress.value==""||parent.TextFrame.theSearchForm.StartZipCode.value=="") && parent.TextFrame.theSearchForm.startplace.options[parent.TextFrame.theSearchForm.startplace.selectedIndex].value==""  ){
		alert("Please enter a starting address and/or a zip code or a starting location.");
		return false;
	} else if(parent.TextFrame.theSearchForm.StartAddress.value==""||parent.TextFrame.theSearchForm.StartZipCode.value=="") {
		dogeocodestart="no";
	} else {
		dogeocodestart="yes";	
	}
	
	if((parent.TextFrame.theSearchForm.EndAddress.value==""||parent.TextFrame.theSearchForm.EndZipCode.value=="") && parent.TextFrame.theSearchForm.endplace.options[parent.TextFrame.theSearchForm.endplace.selectedIndex].value=="" ){
		dogeocodeend="yes";


		onegeocode="yes";
		doEnd="no";
		NearestStart = new Array();
		NearestEnd = new Array();
		BusRoutesStart = new Array();
		BusRoutesEnd = new Array();
		BusRoutesCommon = new Array();
		theEndX=0;
		theEndY=0;
		
		TransferArray = new Array();
		sendGeocodeRequest();
		

	} else if(parent.TextFrame.theSearchForm.EndAddress.value==""||parent.TextFrame.theSearchForm.EndZipCode.value=="") {
	


	dogeocodeend="no";
		
		onegeocode="no";
		doEnd="yes";
		NearestStart = new Array();
		NearestEnd = new Array();
		BusRoutesStart = new Array();
		BusRoutesEnd = new Array();
		BusRoutesCommon = new Array();
		
		TransferArray = new Array();
		sendGeocodeRequest();

	} else {
		dogeocodeend="yes";

		onegeocode="no";
		doEnd="yes";
		NearestStart = new Array();
		NearestEnd = new Array();
		BusRoutesStart = new Array();
		BusRoutesEnd = new Array();
		BusRoutesCommon = new Array();
		
		TransferArray = new Array();
		sendGeocodeRequest();
	}
	
	
}



// 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);

}
///////////////////////////////////////////////////////////////

function sendGeocodeRequest(){


thestartaddress=parent.TextFrame.theSearchForm.StartNumb.value +" "+ parent.TextFrame.theSearchForm.StartAddress.value;
theendaddress=parent.TextFrame.theSearchForm.EndNumb.value +" "+ parent.TextFrame.theSearchForm.EndAddress.value;


	var theHost="broomegis.co.broome.ny.us";
	//var theService="Transitpub";
	var theService="Elections";
	var serverURL="http://" + theHost + "/servlet/com.esri.esrimap.Esrimap?ServiceName=";
	var cVersion="&ClientVersion=4.0";
	var actionURL=serverURL + theService + cVersion;

	var theServer="&CustomService=Geocode";
	var theForm="&Form=True";
	var theEncode="&Encode=False";
	var actionURL = actionURL + theServer + theForm + theEncode;
//alert(actionURL)

	
	var theXML = '<ARCXML version="1.1">';
		theXML = theXML + '<REQUEST>';
		theXML = theXML + '<GET_GEOCODE maxcandidates="10" minscore="50">';
		//theXML = theXML + '<LAYER id="0" />';
		theXML = theXML + '<LAYER id="5" />';
		theXML = theXML + '<ADDRESS>';
		if(onegeocode == "no"){
		//theXML = theXML + '<GCTAG id="Street" value="' + parent.TextFrame.theSearchForm.EndAddress.value + '" />';
	theXML = theXML + '<GCTAG id="Street" value="' + theendaddress + '" />';

 		theXML = theXML + '<GCTAG id="Zone" value="' + parent.TextFrame.theSearchForm.EndZipCode.value + '" />';
		} else {
		//theXML = theXML + '<GCTAG id="Street" value="' + parent.TextFrame.theSearchForm.StartAddress.value + '" />';
	theXML = theXML + '<GCTAG id="Street" value="' + thestartaddress + '" />';

 		theXML = theXML + '<GCTAG id="Zone" value="' + parent.TextFrame.theSearchForm.StartZipCode.value + '" />';
		}
		theXML = theXML + '<GCTAG id="CrossStreet" value="" />';
		theXML = theXML + '</ADDRESS>';
		theXML = theXML + '</GET_GEOCODE>';
		theXML = theXML + '</REQUEST>';
		theXML = theXML + '</ARCXML>';



	var thePostForm=parent.PostFrame.document.forms[0];
if(onegeocode=="no" && dogeocodeend=="no"){

	var string = parent.TextFrame.theSearchForm.endplace.options[parent.TextFrame.theSearchForm.endplace.selectedIndex].value;
	var separator = ':';
	var stringArray = string.split(separator);

	theEndX = stringArray[0];
	theEndY = stringArray[1];
	onegeocode="yes";
	
	secondgeocodename=parent.TextFrame.theSearchForm.endplace.options[parent.TextFrame.theSearchForm.endplace.selectedIndex].text;
	sendGeocodeRequest();

} else if(onegeocode=="yes" && dogeocodestart=="no"){

	var string = parent.TextFrame.theSearchForm.startplace.options[parent.TextFrame.theSearchForm.startplace.selectedIndex].value;
	var separator = ':';
	var stringArray = string.split(separator);

	theStartX = stringArray[0];
	theStartY = stringArray[1];
	firstgeocodename=parent.TextFrame.theSearchForm.startplace.options[parent.TextFrame.theSearchForm.startplace.selectedIndex].text;

	labelgeocodes="yes";
//CHANGE ABOVE
	
	parent.MapFrame.selectBusStops();

	
} else {
	thePostForm.action = actionURL;
	thePostForm.ArcXMLRequest.value = theXML;
	thePostForm.JavaScriptFunction.value="parent.MapFrame.processMyXML";
	thePostForm.submit();
}

}


function processMyXML(theReply){

//alert(theReply);


	if(onegeocode=="no"){

		if (theReply.search("FEATURE") !== -1){

		theXstart=theReply.indexOf("x=");
		theXend=theReply.indexOf('"',(theXstart + 4));
		theEndX=theReply.substring((theXstart + 3),(theXend - 1));
		theYstart=theReply.indexOf("y=");
		theYend=theReply.indexOf('"',(theYstart + 4));
		theEndY=theReply.substring((theYstart + 3),(theYend - 1));

		theaddressfound=theReply.indexOf("ADDRESSFOUND");
		thenamestart=theReply.indexOf("valuestring=", (theaddressfound));
		thenameend=theReply.indexOf("/>", (thenamestart));
		//secondgeocodename=theReply.substring((thenamestart + 13), (thenameend - 2));
		secondgeocodename=theendaddress+", "+parent.TextFrame.theSearchForm.EndZipCode.value;


		onegeocode="yes";
		
		sendGeocodeRequest();

		} else {

		alert("Cannot Locate Ending Address");

		dogeocodeend="no";
		doEnd = "no";
		onegeocode="no";
		
		NearestStart = new Array();
		NearestEnd = new Array();
		BusRoutesStart = new Array();
		BusRoutesEnd = new Array();
		BusRoutesCommon = new Array();
		
		TransferArray = new Array();
		sendGeocodeRequest();

		
		}
		

	} else if(onegeocode=="yes") {

		if (theReply.search("FEATURE") !== -1){

		theXstart=theReply.indexOf("x=");
		theXend=theReply.indexOf('"',(theXstart + 4));
		theStartX=theReply.substring((theXstart + 3),(theXend - 1));
		theYstart=theReply.indexOf("y=");
		theYend=theReply.indexOf('"',(theYstart + 4));
		theStartY=theReply.substring((theYstart + 3),(theYend - 1));

		theaddressfound=theReply.indexOf("ADDRESSFOUND");
		thenamestart=theReply.indexOf("valuestring=", (theaddressfound));
		thenameend=theReply.indexOf("/>", (thenamestart));
		//firstgeocodename=theReply.substring((thenamestart + 13), (thenameend - 2));
		firstgeocodename=thestartaddress+", "+parent.TextFrame.theSearchForm.StartZipCode.value;
	
		labelgeocodes="yes";
		
		
		parent.MapFrame.selectBusStops();

		} else {

		alert("Cannot Locate Starting Address");
		selectionMode=1;
		labelgeocodes="no"
		parent.MapFrame.sendMapXML();

		}


	}

}

// 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);
			}
		}
		showGeocode=true;
		geocodeX=GCpointX[0];
		geocodeY=GCpointY[0];
		geocodeLabel=GCaddress[0];
			

		//} else {
		var Win1;
		var theFrame = "parent.MapFrame";
			if ((useExternalWindow) || (!useTextFrame)) {
				Win1 = window.open("","GeocodeWindow","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[6] + '</title></head>');
			Win1.document.writeln('<body bgcolor="' + textFrameBackColor + '" text="Black" link="Blue" vlink="Blue" LEFTMARGIN=0 onload="window.focus()">');
			Win1.document.writeln('<FONT FACE="Arial" SIZE="-1"><b>' + msgList[57] + '</b>');
			Win1.document.writeln('<table border="1" cellspacing="0" cellpadding="2" nowrap bgcolor="' + tableBackColor + '">');
			Win1.document.writeln('<tr><th><FONT FACE="Arial" SIZE="-1">#</font></th><th><FONT FACE="Arial" SIZE="-1">' + msgList[58] + '</font></th><th><FONT FACE="Arial" SIZE="-1">' + msgList[59] + '</th></tr>');
			for (var i=0;i<GCpointCount;i++) {
				Win1.document.writeln('<tr>');
				Win1.document.writeln('<td><FONT FACE="Arial" SIZE="-1"><a href="javascript:' + theFrame + '.zoomToPoint(' + GCpointX[i] + ',' + GCpointY[i] + ',true,\'' + GCaddress[i] + '\')">' + (i+1) + '</a></font></td>');
				Win1.document.writeln('<td><FONT FACE="Arial" SIZE="-1">' + GCaddress[i] + '</font></td>');
				Win1.document.writeln('<td><FONT FACE="Arial" SIZE="-1">' + GCscore[i] + '</font></td>');
				Win1.document.writeln('</tr>');
			}
			Win1.document.writeln('</table><form><input type="button" onclick="document.location=\'' + appDir + 'addmatch.htm\';" value="' + buttonList[1] + '"></form>');
			Win1.document.close();
		//}
		Win1=null;
		if (GCpointCount==1) sendMapXML();
		//if (GCpointCount==1) zoomToPoint(GCpointX[0], GCpointY[0], true, geocodeLabel);
	} else {
		//no match
		alert(msgList[60]);
		if (useTextFrame) parent.TextFrame.location=appDir + "addmatch.htm";
	}
	
}

