// aimsService.js
// JavaScript file for changing map service from a pulldown list

// load in MapService
function loadMapService(mapURL,ovURL,theServiceIndex,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;
//		resetParams();
		showGeocode=false;
		clickCount=0;
        CopyrightText = theAcetateTitle[theServiceIndex];
		imsURL = mapURL;
		imsQueryURL= imsURL + "&CustomService=Query";
		imsGeocodeURL = imsURL + "&CustomService=Geocode";
		imsExtractURL = imsURL + "ext&CustomService=Extract";
		if (ovURL!="") {
			imsOVURL = ovURL;
			hasOVMap=true;
		} else {
			ovIsVisible=true;
			toggleOVMap();
			imsOVURL = "";
			hasOVMap=false;
		}
		if (parent.TextFrame!=null) parent.TextFrame.document.location= appDir + "text.htm";
			pastStart=true;
			XMLMode=1003;
		var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_SERVICE_INFO renderer="false" extensions="false" fields="false" />\n';
		theString = theString + '</REQUEST>\n</ARCXML>';
		var theReply="";
			sendToServer(imsURL,theString,1003);
//			startUp();
//			sendMapXML();
//			startUp();
	} 
}

// process the new map service and set up layers
function processNewService(theReply) {
//	alert(theReply);
	checkForForbiddenTags(theReply);
	// check for separators in serviceinfo
	var endpos = 0;
	var startpos = 0;
	var pos = theReply.indexOf("<SEPARATORS");
	if (pos!=-1) {
		
		startpos = theReply.indexOf("ts=",pos);
		if (startpos!=-1) {
			startpos += 4;
			endpos = theReply.indexOf(dQuote,startpos);
			pairsDelimiter = theReply.substring(startpos,endpos);
		}
		startpos = theReply.indexOf("cs=",pos);
		if (startpos!=-1) {
			startpos += 4;
			endpos = theReply.indexOf(dQuote,startpos);
			coordsDelimiter = theReply.substring(startpos,endpos);
		}
		//alert("pairsDelimiter="+pairsDelimiter+"\ncoordsDelimiter="+coordsDelimiter);
		checkCoords();
	}
	
//	if (getStartingExtent) {
//		getXYs(theReply);
//		startLeft=eLeft;
//		startRight=eRight;
//		startTop=eTop;
//		startBottom=eBottom;
//	} else {
//		eLeft=startLeft;
//		eRight=startRight;
//		eTop=startTop;
//		eBottom=startBottom;
//		xDistance = Math.abs(eRight-eLeft);
//		var sFactor = xDistance / iWidth
//		mapScaleFactor = sFactor;
//		
//	}
	if (aimsLayersPresent) {
		getLayers(theReply);
		if (setLayerVisible.length>0) setupLayerVisible();
	}
	if (aimsQueryPresent) {
		if (useStoredQuery) checkStoredQueries(theReply);
	} else {
		useStoredQuery=false;
	}
	 xDistance = Math.abs(eRight-eLeft);
	 yDistance = Math.abs(eTop-eBottom);
	 xHalf = xDistance/2;
	 yHalf = yDistance/2;
	 panX = xDistance * panFactor;
	 panY = yDistance * panFactor;
	 if (chkUnits) {
		 if (MapUnits=="DEGREES") {
		 	if ((eRight > 250) || (eTop > 150)) MapUnits="FEET";
			// alert(MapUnits);
		 } 
		 chkUnits=false;
	 }
	 mouseX = 0;
	 mouseY = 0;
	 pixelX = xDistance/iWidth;
	 pixelY = yDistance/iHeight;
	 mapX = eLeft;
	 mayY = eTop;
	 lastLeft = eLeft;
	 lastRight = eRight;
	 lastTop = eTop;
	 lastBottom = eBottom;
	 if (hasOVMap == false) {
		 fullLeft = limitLeft;
		 fullRight = limitRight;
		 fullTop = limitTop;
		 fullBottom = limitBottom;
		 fullWidth = Math.abs(fullRight - fullLeft);
		 fullHeight = Math.abs(fullTop - fullBottom);
	 
	 }
	if (aimsLayersPresent) {
	 	if ((hasTOC) && (showTOC)) {
		 	parent.TOCFrame.document.location=appDir+"toc.htm";
		}
	}
	if (aimsGeocodePresent) {
		if (GCLayerCount==0) {
			if ((useGeocode) || (useReverseGeocode)) {
				useGeocode=false;
				useReverseGeocode=false;
			}
		}
	} else {
			useGeocode=false;
			useReverseGeocode=false;	
	}
	if (parent.ToolFrame!=null) {
		//alert("Refreshing toolbar");
		parent.ToolFrame.document.location= appDir + "toolbar.htm";
	} else if (hasToolBarOnLayer) {
		// requires custom function getLayerListContent. . . example in layerlist.js in Hyperlink sample
		var content = getLayerListContent();
		if (isNav) {
			replaceLayerContent("theToolBar",content);
		} else {
			content = swapStuff(content,"\\'",sQuote);
			document.all.theToolBar.innerHTML = content;
		}
		
	}
	hideRetrieveData();
	if ((ovIsVisible) && (aimsDHTMLPresent)) {
		ovIsVisible = false;
		toggleOVMap();
	}
	if (enforceFullExtent) {
		writeBlankMapXML();
	} else {
		if ((aimsQueryPresent) && (highlightedOne!="") && (queryZoom)) {
			setStartQuery();
		} else {
			sendMapXML();
		}
	}
}

