// 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 imsGeocodeURL = 'http://www.pagnet.org/servlet/com.esri.esrimap.Esrimap?ServiceName=GeoCode&CustomService=geocode';
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


if (imsGeocodeURL=="") {
	imsGeocodeURL= imsURL + "&CustomService=Geocode";
}

/*
***************************************************************************************

Geocoding functions

***************************************************************************************
*/

// set up geocode
function setupGeocode() {
		getGeocodeParams();
}
function setupGeocode2() {
		getGeocodeParams2();
}
// 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,25);

}

// 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);
//alert("getGeoCodeParams\n **** imsGeocodeURL--" + imsGeocodeURL + "\n **** theString--"+theString)

}
function getGeocodeParams2() {
	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_SERVICE_INFO />\n';
	theString += '</REQUEST>\n</ARCXML>';
	var theReply="";

	sendToServer(imsGeocodeURL,theString,266);
//alert("getGeoCodeParams2\n **** imsGeocodeURL--" + imsGeocodeURL + "\n **** theString--"+theString)

}
// 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 += '<LAYER id="' + GCLayerId[1] + '" />';
	theString += '<ADDRESS>\n';
	for (var i=0;i<GCidCount;i++) {
		theString += '<GCTAG id="' + GCid[i] + '" value="' + GCvalue[i] + '"/>\n';
	}
	theString += '</ADDRESS>\n</GET_GEOCODE>\n</REQUEST>\n</ARCXML>\n';
	return theString;

}

// parse out geocoding layers
function parseGeocodeLayers(theReply) {
//alert("function parseGeocodeLayers(theReply) {")
	//alert(theReply);
	var pos = 0;
	var startpos = 0;
	var endpos = 0;
	var lpos = 0;
	var rCount = 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);
				if (GCLayerStyle[GCLayerCount]==SdcGeocodeStyle) rCount++;
				//alert(GCLayerStyle[GCLayerCount]);

			}
			GCLayerCount=GCLayerCount+1;
			pos = theReply.indexOf("<LAYERINFO",endpos);

		} else {
			// bypass this for now
			//alert(theReply);
			pos =-1;
		}
	}
	if (rCount==0) {
		//no route extension parameters
		useRoute=false;
		useReverseGeocode=false;
	}
	if (GCLayerCount==0) {
		useGeocode=false;
		useRoute=false;
		useReverseGeocode=false;
	}
	if (parent.ToolFrame!=null) {
		//alert("Refreshing toolbar");
		parent.ToolFrame.document.location= appDir + "toolbar.htm";
	}
	//alert("useRoute=" + useRoute + "\nuseReverseGeocode=" + useReverseGeocode);

}

// parse out the bacic geocode parameters for the layer
function parseGeocodeParams(theReply,theLayer) {
//alert("function parseGeocodeParams(theReply,theLayer) {");
	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 + '"' //theLayer = StrNetall
	pos = theReply.indexOf(theName,endpos);
//alert("theNAME - "+theLayer)
	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("description=",endpos);
				startpos += 13;
				endpos = theReply.indexOf(dQuote,startpos);
				GCdesc[GCidCount] = theReply.substring(startpos,endpos);
				startpos = theReply.indexOf("label=",endpos);
				startpos = startpos + 7;
				endpos = theReply.indexOf(dQuote,startpos);
				GClabel[GCidCount] = theReply.substring(startpos,endpos);
				GCidCount=GCidCount+1;
			//}
			startpos = theReply.indexOf("<GCINPUT id=",endpos);
		}
		return true;
	} else {
		return false;
	}

}

///**************************************\/ \/ \/ \/ ***************************

/*function MYparseGeocodeResults(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;
	if ((pos>0) && (GCpointCount>0)) {
		pos = theReply.indexOf("<FEATURE");
		if (pos!=-1) { //----- THis section just assigns the geocode results to arrays to hold the information -----
			while (pos!=-1) {
				lpos = theReply.indexOf("<FIELD",pos);
				if (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++;
				}
				pos = theReply.indexOf("<FEATURE",endpos);
			}
		} // -- NOW TAKE THE TOP MATCH LOCATION AND ASSIGN IT TO THE GEOCODE VARIABLES WE"LL USE LATER -----
			showGeocode=true;  //IF YOU WANT THE GEOCODE POINT TO BE SHOWN
			geocodeX=GCpointX[0];
			geocodeY=GCpointY[0];
			geocodeLabel=GCaddress[0];
			//hideRetrieveData();
			//if (GCpointCount==1) sendMapXML();
			//----------- Now start the ID ------------
			GeoIdentify();

	} else {  //no match
		alert(msgList[60]); //unable to locate
		var url = appDir + "TUSDsearch.htm";
		if ((useExternalWindow) || (!useTextFrame)) {
			Win1 = window.open(url,"GeocodeWindow","width=575,height=120,scrollbars=yes,resizable=yes");
		} else {
			parent.TextFrame.document.location = url;
		}
	}
}*/

function GeoIdentify() { // used after the geocode located address is found
//alert("function GeoIdentify() {")
	if (checkIfActiveLayerAvailable()) {

	if(parent.TitleFrame2.schLevel[0] == 1){
		 setActiveLayer(16);
	}else if(parent.TitleFrame2.schLevel[0] == 2){
		 setActiveLayer(17);
	}else if(parent.TitleFrame2.schLevel[0] == 3){
		 setActiveLayer(18);
	}else{
		 setActiveLayer(15);
	}

	        //setActiveLayer(17);//Originally 6 = TUSD_Bndy

		highlightedOne="";
		var mapX = geocodeX //mouseX;
		var mapY = geocodeY //mouseY;
		//getMapXY(theX,theY);
		searchTolerance = 1 //(xDistance/iWidth) * 1 //pixelTolerance;
		var tempWest = mapX - searchTolerance;
		var tempNorth = (mapY*1) + searchTolerance;
		var tempEast = (mapX*1) + searchTolerance;
		var tempSouth = mapY - searchTolerance;
		//queryStartRecord=1;
		selectEnvelope='maxy="' + forceComma(tempNorth) + '" maxx="' + forceComma(tempEast) + '" miny="' + forceComma(tempSouth) + '" minx="' + forceComma(tempWest) + '"';
		//selectionMode=2;
	//alert("GeoIdentify - writeGetFeature")
		var theString = writeGetFeatures(tempWest,tempSouth,tempEast,tempNorth);
//alert("ttttt--" + theString)
		showRetrieveData();

		sendToServer(imsQueryURL,theString,identifyXMLMode);
		//Test_Zoom()

	}
}


//*********************************************^^^^^^**********************************

// parse out geocode response and display results in table
function parseGeocodeResults(theReply) {
//alert(theReply);
//alert("function parseGeocodeResults(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;
	var tempX,tempY;
	//alert(GCpointCount);
	if ((pos>0) && (GCpointCount>0)) {

		if (geocodeAppMode=="locate") { //***************************************************************************************

//alert("locate");
		pos = theReply.indexOf("<FEATURE");
			if (pos!=-1) {
				while (pos!=-1) {
					lpos = theReply.indexOf("<FIELD",pos);
					if (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);

						tempX = theReply.substring(startpos,endpos);
						GCpointX[gcCount]= parseFloat(setDecimalString(tempX));
						startpos = theReply.indexOf("y=",endpos);
						startpos = startpos + 3;
						endpos = theReply.indexOf(dQuote,startpos);
						tempY = theReply.substring(startpos,endpos);
						GCpointY[gcCount]= parseFloat(setDecimalString(tempY));
						gcCount++;
					}
					pos = theReply.indexOf("<FEATURE",endpos);
				}
			}
			showGeocode=true;
			geocodeX=GCpointX[0];
			geocodeY=GCpointY[0];
			geocodeLabel=GCaddress[0];

			hideRetrieveData();
			//} else {
			var Win1;
			var gPointX, gPointY
			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="align="center "Arial" SIZE="-1"><b>' + msgList[57] + '</b>');
				Win1.document.writeln('<table border="0" align="center" cellspacing="0" cellpadding="2" style="color: ' + textFrameTextColor + '; background-color: ' + tableBackColor + '; " nowrap>');
				//Win1.document.writeln('<tr><td></td><td>Address</td></tr>');
				for (var i=0;i<GCpointCount;i++) {
					gPointX = convertDecimal(GCpointX[i].toString());
					gPointY = convertDecimal(GCpointY[i].toString());
					Win1.document.writeln('<tr>');
					Win1.document.writeln('<td>');
					if(i==0){
					Win1.document.writeln('Found ' + GCpointCount + ' Location(s)');
					}
					Win1.document.writeln('<td>&nbsp;&nbsp;&nbsp;</td>');
					Win1.document.writeln('</td>');
					//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" COLOR="RED" SIZE="-1"><b>' + GCaddress[i] + '</b></font></td>');
					Win1.document.writeln('<td>&nbsp;&nbsp;</td>');
					Win1.document.writeln('<td><FONT FACE="Arial" SIZE="-1"><a href="javascript:' + theFrame + '.zoomToPoint(' + GCpointX[i] + ',' + GCpointY[i] + ',true,\'' + GCaddress[i] + '\')">Zoom To</a></font></td>');
					Win1.document.writeln('<td>&nbsp;&nbsp;</td>');
					if(i==0){
					Win1.document.writeln('<td><input type="button" onclick="document.location=\'' + appDir + 'addmatch.htm\';" value="' + buttonList[1] + '"></td>');
					}else{
					Win1.document.writeln('<td></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.writeln('</body></html>');
				Win1.document.close();

			Win1=null;
			sendMapXML();
			//if (GCpointCount==1) sendMapXML();

		} else if (geocodeAppMode=="locatesch") {//***************************************************************************************

//alert("lacatesch");

			pos = theReply.indexOf("<FEATURE");
			if (pos!=-1) { //----- THis section just assigns the geocode results to arrays to hold the information -----
				while (pos!=-1) {
					lpos = theReply.indexOf("<FIELD",pos);
					if (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++;
					}
					pos = theReply.indexOf("<FEATURE",endpos);
//alert(theReply)
				}
			} // -- NOW TAKE THE TOP MATCH LOCATION AND ASSIGN IT TO THE GEOCODE VARIABLES WE"LL USE LATER -----
				showGeocode=true;  //IF YOU WANT THE GEOCODE POINT TO BE SHOWN
				geocodeX=GCpointX[0];
				geocodeY=GCpointY[0];
				geocodeLabel=GCaddress[0];
				//hideRetrieveData();
				//if (GCpointCount==1) sendMapXML();
				//----------- Now start the ID ------------
				GeoIdentify();


/*		} else if (geocodeAppMode=="route") {//***************************************************************************************
		//alert("2")
			// 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") {//***************************************************************************************
		//alert("3")
			// 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]);
		//var url = appDir + "addmatch.htm";
		var url = appDir + "TUSDsearch.htm";
		if ((useExternalWindow) || (!useTextFrame)) {
			Win1 = window.open(url,"GeocodeWindow","width=575,height=120,scrollbars=yes,resizable=yes");
		} else {
			parent.TextFrame.document.location = url;
		}
	}

}

