// aimsGeneric.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*		dependent on aimsXML.js, ArcIMSparam.js, aimsCommon.js, aimsMap.js,
*		aimsLayers.js, aimsDHTML.js
*/

//The list of MapServices not to be included in the Participant site. Weeds out Public and Partner Services
var nonParticipant = "Public_Bus_Info Public_Parcel_Find Public_PlannedLanduse Public_Parks Public_Zoning Realtor Data_Requests TIP_Projects"

aimsGenericPresent=true;

var serviceCount = 0;
var ServiceName = new Array();

selectFields=true;
swapSelectFields=true;
useFieldAlias=true;

var theService = "";
var oneTimeURL = "";

// process the catalog and list the ImageServices
function processCatalog(theReply) {
		//alert ("function startMap\nResponse:\n" + theReply);
		var startpos = 0;
		var endpos = 0;
		ServiceName.length;
		serviceCount = 0;
		var pos = -1;
		var spos = 9;
		var lpos = 9;
		var i=0;
		var tempString=""
		var visString = ""
		var testString = "";
		pos = theReply.indexOf("ERROR",endpos);
		if (pos==-1) {
			while (lpos > -1) {
				// find the mapper service group
				lpos = theReply.indexOf("<SERVICE NAME=",endpos);
				if (lpos != -1) {
					startpos = lpos + 15
					endpos = theReply.indexOf(dQuote, startpos);
					tempString = theReply.substring(startpos,endpos);
					startpos = theReply.indexOf("TYPE=",endpos);
					startpos = startpos + 6;
					endpos = startpos + 5;
					if (theReply.substring(startpos,endpos)=="Image") {
						startpos = theReply.indexOf("STATUS=",endpos);
						startpos = startpos + 8;
						endpos = startpos + 7;
						if (theReply.substring(startpos,endpos)=="ENABLED") {
							ServiceName[i]=tempString;
							i=i+1;
							serviceCount=serviceCount+1;
						}
					}
					
				}
			}
			ServiceName.sort();
			if (isPublic) {
				//moved to end of checkParams in ArcimsParams
			} else if (isPartner) {
				var mURL = serverURL + loadThisService;
				var mOVURL = "";
				loadMapService(mURL,mOVURL,true);
			} else	{
				useBuffer = false;
				loadMapForm();
			}
		} else {
			alert("Error compiling MapService List");
		}
		hideRetrieveData();
	
}

// write out a form for loading MapService
function loadMapForm() {
		var Win1;
		if ((useExternalWindow) || (!useTextFrame)) {
		Win1 = window.open("","LoadWindow","width=550,height=100,scrollbars=yes,resizable=yes");
		} else {
			Win1 = parent.TextFrame;
			Win1.document.open();
		}
		Win1.document.writeln('<html><head><title>Load MapService</title>');
		Win1.document.write('<scri');
		Win1.document.writeln('pt language="javascript">');
		Win1.document.writeln('var t; ');
		Win1.document.writeln('var par; ');
		Win1.document.writeln('if (opener) { ');
		Win1.document.writeln('		if (opener.name=="MapFrame") { ');
		Win1.document.writeln('		t = opener.parent.MapFrame; ');
		Win1.document.writeln('		par = opener.parent; ');
		Win1.document.writeln('	} else { ');
		Win1.document.writeln('		t = opener; ');
		Win1.document.writeln('		par = opener; ');
		Win1.document.writeln('	} ');
		Win1.document.writeln('} else { ');	
		Win1.document.writeln('	if (parent.MapFrame) { ');
		Win1.document.writeln('		t = parent.MapFrame; ');
		Win1.document.writeln('		par = opener; ');
		Win1.document.writeln('	} else { ');
		Win1.document.writeln('		t=document; ');
		Win1.document.writeln('	} ');
		Win1.document.writeln('} ');
		Win1.document.writeln(' function loadHelp() {');
		Win1.document.writeln('		var Win2;');
		Win1.document.writeln('		Win2 = window.open("http://" + hostName + "/website/testparticipant/helpMapService.htm","Help","width=800,height=550,scrollbars=yes,resizable=yes");');
		Win1.document.writeln('		Win2 = null;');
		Win1.document.writeln(' }');
		Win1.document.writeln('	function loadIt() {');
		Win1.document.writeln('		var theForm = document.forms[0];');
		Win1.document.writeln('		var theIndex=theForm.theValue.selectedIndex;');
		Win1.document.writeln('		var mURL = "' + serverURL + '" + theForm.theValue.options[theIndex].value;');
		Win1.document.writeln('		t.loadThisService = theForm.theValue.options[theIndex].value; ');
		Win1.document.writeln('		var mOVURL = "";');
		//Win1.document.writeln('		if (theForm.putOV.checked) {');
		//Win1.document.writeln('			theIndex=theForm.theOVValue.selectedIndex;');
		//Win1.document.writeln('			mOVURL = "' + serverURL + '" + theForm.theOVValue.options[theIndex].value;');
		//Win1.document.writeln('		} ');
		Win1.document.writeln('		var theFrame;');
		//Win1.document.writeln('		if (opener) {');
		//Win1.document.writeln('			theFrame = opener.parent.MapFrame;');
		//Win1.document.writeln('		} else {');
		Win1.document.writeln('			theFrame = parent.MapFrame;');
		//Win1.document.writeln('		}');
		//Win1.document.writeln('		alert(mURL)');
		Win1.document.writeln('		theFrame.loadMapService(mURL,mOVURL,true);');
		//Win1.document.writeln(' 	alert(mURL)');
		//Win1.document.writeln('		alert(mOVURL)');
		//Win1.document.writeln('		theFrame.readAlias(theIndex);');
		//Win1.document.writeln('		window.close();');
		Win1.document.writeln('	}');
		Win1.document.writeln('</script>');
		Win1.document.writeln('</head>');
		Win1.document.writeln('<body bgcolor=#003366 topmargin=0 leftmargin=0 rightmargin=0 text="white" onload="window.focus()">');
		Win1.document.writeln('<div align="center"><form onsubmit="javascript:loadIt();return false;">');
		Win1.document.writeln('<table width=100% cellpadding="1" cellspacing="1">');
		Win1.document.writeln('	<tr><td bgcolor=#003366 align="center" colspan=3><font face="Arial" size="-1" style="bold" color=white><b>Select REGIS Agency Map Service to Display</b><hr width="75%"></font></td></tr>');
		Win1.document.writeln('<tr><td bgcolor=#003366 align="center"><font face="Arial" size="-2" color=white>Main Map:');
		Win1.document.writeln('	<select name="theValue">');
		var pos = -1;
		for (var i=0;i<serviceCount;i++) {
			pos = nonParticipant.indexOf(ServiceName[i]);
			if (pos == -1) {
                        	if (ServiceName[i] != 'overview') {
	   				Win1.document.writeln('<option value="' + ServiceName[i] + '">' + ServiceName[i] + '</option>');
                        	}
			}
		}
		Win1.document.writeln('	</select>');
		Win1.document.writeln('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
		Win1.document.writeln('<input type="button" name="submit" value="Load" onclick="loadIt()" style="font-size: x-small; color: #003366; background: #CCCC99;">');								
		Win1.document.writeln('	</select>');
		Win1.document.writeln('</td>');
		Win1.document.write('</tr><tr><td align="center" bgcolor=#003366 colspan=3><br>');
		Win1.document.writeln('<input type="button" value="\'Which one do I want?\'" onclick="loadHelp()" style="font-size: x-small; color: #003366; background: #CCCC99;">');
		if (useExternalWindow) Win1.document.writeln('&nbsp;&nbsp;&nbsp;<input type="button" name="submit1" value="Cancel" onclick="window.close()">');
		Win1.document.write('</tr></table></form></div></body></html>');
		Win1.document.close();
		Win1=null;

}

// load in MapService
function loadMapService(mapURL,ovURL,checkExtents) {
if ((!isPartner) && (!isPublic)) parent.TopFrame.document.location = "http://" + hostName + "/website/testparticipant/top.htm";
theService = mapURL.substr((mapURL.length - 8), 8)
//readAlias(theService)
//alert(theService)
if (theService == "M_Center") {
	ScaleBarUnits = "FEET";
}
//alert(mapURL.substr((mapURL.length - 8), 8))
	if (mapURL!="") {
		showRetrieveData();
		if (aimsSelectPresent) {
			selectCount=0;
			highlightedOne="";
			selectPoints.length=1;
			selectLeft.length=1;
			selectRight.length=1;
			selectTop.length=1;
			selectBottom.length=1;
		}

		ActiveLayerIndex=0;
		//toggleOVVisible = true;
		showGeocode=false;
		clickCount=0;
		imsURL = mapURL;
		oneTimeURL = mapURL+"&track=this";
		imsQueryURL= imsURL + "&CustomService=Query";
		imsGeocodeURL = imsURL + "&CustomService=Geocode";
                var hostname=document.location.host;
		imsOVURL =  'http://'+hostname+'/servlet/com.esri.esrimap.Esrimap?ServiceName=KCRC_Overview';
		ovBoxColor = "255,0,0";
		toggleOVMap();
		
		//if (ovURL!="") {
			//imsOVURL = ovURL;
			//hasOVMap=true;
		//} else {
			//ovIsVisible=true;
			//toggleOVMap();
			//imsOVURL = "";
			//hasOVMap=false;
		//}
		if (parent.TextFrame!=null) parent.TextFrame.document.location= appDir + "text.htm";
		if (checkExtents) {
			MapUnits="DEGREES";
			chkUnits=true;
			pastStart=false;
			startUp();
		} else {
			sendMapXML();
		}
	} 
}

