// aimsGeneric.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*		dependent on aimsXML.js, ArcIMSparam.js, aimsCommon.js, aimsMap.js,
*		aimsLayers.js, aimsDHTML.js
*/

aimsGenericPresent=true;

var serviceCount = 0;
var ServiceName = new Array();

var selectFields= "#ID# #SHAPE#";
swapSelectFields=true;
useFieldAlias=true;

// 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();
			startUp();
		} 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('	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('		var mOVURL = "";');
		Win1.document.writeln('		var reedsburgOV = "reedsburgOV";');
		//Win1.document.writeln('		if (theForm.putOV.checked) {');
		//Win1.document.writeln('			theIndex=theForm.theOVValue.selectedIndex;');
		Win1.document.writeln('			theIndex=0;');
		Win1.document.writeln('			mOVURL = "' + serverURL + '" + reedsburgOV;');
		//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('		theFrame.loadMapService(mURL,mOVURL,true);');
		Win1.document.writeln('		window.close();');
		Win1.document.writeln('	}');
		Win1.document.writeln('</script>');
		Win1.document.writeln('</head>');
		Win1.document.writeln('<body BACKGROUND="white" topmargin=0 leftmargin=0 rightmargin=0 text="Black" onload="window.focus()">');
		Win1.document.writeln('<form onsubmit="javascipt:loadIt();return false;">');
		Win1.document.writeln('<center><font face="Tahoma" size="-1"><b>Load Pre-defined Map Service</b></font><p>');
		Win1.document.writeln('<font face="Arial" size="-2">Map Serivce:');
		Win1.document.writeln('<select name="theValue">');
		for (var i=2;i<serviceCount;i++) {
		Win1.document.writeln('<option value="' + ServiceName[i] + '">' + ServiceName[i] + '</option>');
		}
		Win1.document.writeln('</select>');
		//Win1.document.writeln('<font face="Arial" size="-2">Overview Map:<select name="theOVValue">');
		//for (var i=0;i<1;i++) {
		//Win1.document.writeln('<option value="Demoov">Demoov</option>');
		//}
		//Win1.document.writeln('</select>');
		Win1.document.writeln('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" name="submit" value="Load" onclick="loadIt()">');
		if (useExternalWindow) Win1.document.writeln('&nbsp;&nbsp;&nbsp;<input type="button" name="submit1" value="Cancel" onclick="window.close()">');

		Win1.document.write('</center></body></html>');
		Win1.document.close();
		Win1=null;

}

// load in MapService
function loadMapService(mapURL,ovURL,checkExtents) {
	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;
		
		showGeocode=false;
		clickCount=0;
		imsURL = mapURL;
		imsQueryURL= imsURL + "&CustomService=Query";
		imsGeocodeURL = imsURL + "&CustomService=Geocode";
		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="FEET";
			chkUnits=true;
			pastStart=false;
			startUp();
		} else {
			sendMapXML();
		}
	} 
}

