// 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();

selectFields= "#ALL#";
swapSelectFields=true;
useFieldAlias=true;
canLoad=true;

var holdPan=usePan;
var holdPanNorth=usePanNorth;
var holdPanWest=usePanWest;
var holdPanEast=usePanEast;
var holdPanSouth=usePanSouth;
var holdZoomIn=useZoomIn;
var holdZoomOut=useZoomOut;
var holdFullExtent=useFullExtent;
var holdZoomActive=useZoomActive;
var holdZoomLast=useZoomLast;
var holdIdentify=useIdentify;
var holdMeasure=useMeasure;
var holdSetUnits=useSetUnits;
var holdSelect=useSelect;
var holdQuery=useQuery;
var holdFind=useFind;
var holdGeocode=useGeocode;
var holdStoredQuery=useStoredQuery;
var holdClearSelect=useClearSelect;
var holdPrint=usePrint;
var holdBuffer=useBuffer;
var holdExtract=useExtract;
var holdHyperLink=useHyperLink;
var holdHyperLinkAny=useHyperLinkAny;
var holdIdentifyAll=useIdentifyAll;
var holdBufferShape=useBufferShape; 
var holdDebug=setDebug;

// process the catalog and list the ImageServices
function processCatalog(theReply) {
		//alert ("function startMap\nResponse:\n" + theReply);
		var theReplyUC = theReply.toUpperCase();
		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 = theReplyUC.indexOf("<SERVICE NAME=",endpos);
				if (lpos != -1) {
					startpos = lpos + 15
					endpos = theReply.indexOf(dQuote, startpos);
					tempString = theReply.substring(startpos,endpos);
					startpos = theReplyUC.indexOf("TYPE=",endpos);
					startpos = startpos + 6;
					endpos = startpos + 5;
					if (theReply.substring(startpos,endpos)=="Image") {
						startpos = theReplyUC.indexOf("STATUS=",endpos);
						startpos = startpos + 8;
						endpos = startpos + 7;
						if (theReplyUC.substring(startpos,endpos)=="ENABLED") {
							ServiceName[i]=tempString;
							i=i+1;
							serviceCount=serviceCount+1;
						}
					}
					
				}
			}
			ServiceName.sort();
			//loadMapForm();
		} else {
			alert("Error compiling MapService List");
		}
		hideRetrieveData();
		if (imsURL!="") startUp();	
}

// 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><meta http-equiv="Content-Type" content="text/html; charset=' + charSet + '"><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('		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('		theFrame.loadMapService(mURL,mOVURL,true);');
		//Win1.document.writeln('		theFrame.loadMapService(mURL,mOVURL,false);');
		Win1.document.writeln('		if (opener) window.close();');
		Win1.document.writeln('	}');
		Win1.document.writeln('</script>');
		Win1.document.writeln('</head>');
		Win1.document.writeln('<body bgcolor="black" topmargin=0 leftmargin=0 rightmargin=0 text="Black" onload="window.focus()">');
		Win1.document.writeln('<div align="center"><form onsubmit="javascipt:loadIt();return false;">');
		Win1.document.writeln('<table width=100% cellpadding="2" cellspacing="2">');
		Win1.document.writeln('	<tr><td bgcolor="' + textFrameBackColor + '" align="center" colspan=3><font face="Arial" size="-1"><b>Load MapService</b></font></td></tr>');
		Win1.document.writeln('<tr><td bgcolor="' + textFrameBackColor + '" align="center"><font face="Arial" size="-2">Main Map:');
		Win1.document.writeln('	<select name="theValue">');
		for (var i=0;i<serviceCount;i++) {
			Win1.document.writeln('<option value="' + ServiceName[i] + '">' + ServiceName[i] + '</option>');
		}
		Win1.document.writeln('	</select>');
		Win1.document.writeln('</td>');
		Win1.document.writeln('	<td bgcolor="' + textFrameBackColor + '" align="center"><font face="Arial" size="-2">Overview Map:<select name="theOVValue">');
		for (var i=0;i<serviceCount;i++) {
			Win1.document.writeln('<option value="' + ServiceName[i] + '">' + ServiceName[i] + '</option>');
		}
		Win1.document.writeln('	</select>');

		Win1.document.writeln('</td>');
		Win1.document.write('<td bgcolor="' + textFrameBackColor + '" align="center"><font face="Arial" size="-2">&nbsp;<input type="Checkbox" name="putOV" value="yes"');
		if (hasOVMap) Win1.document.write(' checked');
		Win1.document.writeln('>Use Overview Map</td>');
		Win1.document.write('</tr><tr><td align="center" bgcolor="' + textFrameBackColor + '" colspan=3>');
		Win1.document.writeln('<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('</tr></table></form></div></body></html>');
		Win1.document.close();
		Win1=null;

}

// load in MapService
function loadMapService(mapURL,ovURL,checkExtents) {
	if (mapURL!="") {
		//alert("Running loadMapService()");
		showRetrieveData();
		if (aimsSelectPresent) {
			//alert("Running loadMapService()\naimsSelectPresent = TRUE");
			selectCount=0;
			highlightedOne="";
			selectPoints.length=1;
			selectLeft.length=1;
			selectRight.length=1;
			selectTop.length=1;
			selectBottom.length=1;
		}
		ActiveLayerIndex=0;
		resetParams();
		showGeocode=false;
		clickCount=0;
		//alert("Running loadMapService()\nSetting URLs");
		imsURL = mapURL;
		imsQueryURL= imsURL + "&CustomService=Query";
		imsGeocodeURL = imsURL + "&CustomService=Geocode";
		if (ovURL!="") {
			//alert("Running loadMapService()\nOverview Selected");
			imsOVURL = ovURL;
			hasOVMap=true;
		} else {
			//alert("Running loadMapService()\nNo Overview Selected");
			ovIsVisible=true;
			toggleOVMap();
			imsOVURL = "";
			hasOVMap=false;
		}
		if (parent.TextFrame!=null) parent.TextFrame.document.location= appDir + "text2.htm";
		if (checkExtents) {
			//alert("Running loadMapService()\ncheckExtents = TRUE");
			MapUnits="DEGREES";
			chkUnits=true;
			enforceFullExtent=true;
			pastStart=false;
			startUp();
		} else {
			//alert("Running loadMapService()\ncheckExtents = FALSE");
			sendMapXML();
		}
	} 
}

// get the default parameters and save them
function getDefaultParams() {
	holdPan=usePan;
	holdPanNorth=usePanNorth;
	holdPanWest=usePanWest;
	holdPanEast=usePanEast;
	holdPanSouth=usePanSouth;
	holdZoomIn=useZoomIn;
	holdZoomOut=useZoomOut;
	holdFullExtent=useFullExtent;
	holdZoomActive=useZoomActive;
	holdZoomLast=useZoomLast;
	holdIdentify=useIdentify;
	holdMeasure=useMeasure;
	holdSetUnits=useSetUnits;
	holdSelect=useSelect;
	holdQuery=useQuery;
	holdFind=useFind;
	holdGeocode=useGeocode;
	holdStoredQuery=useStoredQuery;
	holdClearSelect=useClearSelect;
	holdPrint=usePrint;
	holdBuffer=useBuffer;
	holdExtract=useExtract;
	holdHyperLink=useHyperLink;
	holdHyperLinkAny=useHyperLinkAny;
	holdIdentifyAll=useIdentifyAll;
	holdBufferShape=useBufferShape; 
	holdDebug=setDebug;

}

function resetParams() {
	//alert("Running resetParams()");
	usePan=holdPan;
	usePanNorth=holdPanNorth;
	usePanWest=holdPanWest;
	usePanEast=holdPanEast;
	usePanSouth=holdPanSouth;
	useZoomIn=holdZoomIn;
	useZoomOut=holdZoomOut;
	useFullExtent=holdFullExtent;
	useZoomActive=holdZoomActive;
	useZoomLast=holdZoomLast;
	useIdentify=holdIdentify;
	useMeasure=holdMeasure;
	useSetUnits=holdSetUnits;
	useSelect=holdSelect;
	useQuery=holdQuery;
	useFind=holdFind;
	useGeocode=holdGeocode;
	useStoredQuery=holdStoredQuery;
	useClearSelect=holdClearSelect;
	usePrint=holdPrint;
	useBuffer=holdBuffer;
	useExtract=holdExtract;
	useHyperLink=holdHyperLink;
	useHyperLinkAny=holdHyperLinkAny;
	useIdentifyAll=holdIdentifyAll;
	useBufferShape=holdBufferShape; 
	setDebug=holdDebug;

}