// aimsCommon.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*		dependent on aimsXML.js, ArcIMSparam.js, aimsMap.js
*/

var aimsCommonPresent=true;


jsPlace = "parent.MapFrame";

//var theReply = "It didn't work!";
var queryTool = 0;
var legendImage="";
var modeBlurb = "Zoom In Mode";


var chkUnits=false;
var legendTemp = false;
var drawSelectBoundary = false;
var ovIsVisible=false;

var showBuffer = false;

var theImagePath = "";
var theImageURLPath = "";
var theServiceName = "";
var mapPath = "";
var mapURL = "";
var mapService = "";
var ovPath = "";
var ovURL = "";
var ovService = "";

var theVersion = navigator.appVersion;
var theVersNum = parseFloat(theVersion);
var theBrowser = navigator.appName;
var noOverlay = true;  //must be true for now
//alert(browser + " - " + versNum + " - " + version);

//if ((theVersion.indexOf("MSIE 5")!=-1) || (theBrowser=="Netscape") || (theVersNum>=5))  noOverlay = false;



/*
***************************************************************************************

Common functions

***************************************************************************************
*/



// when there is a mapservice to load, it proceeds from here
function startUp() {

	if (imsURL != "") {
		//alert(imsURL);
		//if (parent.MapFrame!=null) resizeMap();
		iWidth = document.theImage.width;
		iHeight = document.theImage.height;
		if (imsURL!=imsOVURL) toggleOVVisible = false;
		getStartExtent();
		
	}
}

// get the starting extent
function getStartExtent() {
	if (parent.PostFrame.document.forms[0]!=null) {
		var theString = '<ARCXML VERSION="1.0.1">\n<REQUEST>\n<GET_SERVICE_INFO renderer="false" extensions="false" fields="false" />\n';
		theString = theString + '</REQUEST>\n</ARCXML>';
		var theReply="";
		if (getLimitExtent) {
			if (hasOVMap) {
				sendToServer(imsOVURL,theString,3);
			} else {
				sendToServer(imsURL,theString,3);
			}
		} else {
			
			XMLMode=3;
			//alert("No request sent for Limit Extent");
			processXML("No request for Limit Extent")
		}
	} else {
		parent.location.reload();
		//alert("Unable to start. Required HTML Form missing (jsForm.htm).");
	}
}

// process the start extent and set up layers
function processStartExtent(theReply) {

	if (getStartingExtent) {
		getXYs(theReply);
		startLeft=left;
		startRight=right;
		startTop=top;
		startBottom=bottom;
	} else {
		left=startLeft;
		right=startRight;
		top=startTop;
		bottom=startBottom;
		xDistance = Math.abs(right-left);
		var sFactor = xDistance / iWidth
		//if (aimsLayersPresent) {
		//	if (sFactor!=mapScaleFactor) {
				mapScaleFactor = sFactor;
		//		if ((hasTOC) && (!legendVisible)) parent.TOCFrame.document.location = appDir + "toc.htm";
		//	} 
		//}
		
	}
	if (aimsLayersPresent) getLayers(theReply);
	if (aimsQueryPresent) {
		if (useStoredQueries) checkStoredQueries(theReply);
	} else {
		useStoreQueries=false;
	}
	 xDistance = Math.abs(right-left);
	 yDistance = Math.abs(top-bottom);
	 xHalf = xDistance/2;
	 yHalf = yDistance/2;
	 panX = xDistance * panFactor;
	 panY = yDistance * panFactor;
	 if (chkUnits) {
		 if (MapUnits=="DEGREES") {
		 	if ((right > 250) || (top > 150)) MapUnits="FEET";
			// alert(MapUnits);
		 } 
		 chkUnits=false;
	 }
	 mouseX = 0;
	 mouseY = 0;
	 pixelX = xDistance/iWidth;
	 pixelY = yDistance/iHeight;
	 mapX = left;
	 mayY = top;
	 lastLeft = left;
	 lastRight = right;
	 lastTop = top;
	 lastBottom = bottom;
	 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) {
		parent.ToolFrame.document.location= appDir + "toolbar.htm";
	} else if (hasToolBarOnLayer) {
		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 {
		sendMapXML();
	}
}


// request a list of available Image MapServices
function startMap() {
	showRetrieveData();
	if (aimsGenericPresent) {
		var theText = "<GETCLIENTSERVICES/>";
		sendToServer(catURL,theText,5);
		
	} else {
		startUp();
	}
}

/*  ************************
*	Extent functions
*	************************
*/

// get the Map Image width
function getMapWidth () {
	var mapFrameWidth = thePageWin.innerWidth;
	if (mapFrameWidth == null) {
		mapFrameWidth = thePageDoc.body.clientWidth;
	}
	return mapFrameWidth;
}

 //get the Map Image height
function getMapHeight () {
	var mapFrameHeight = thePageWin.innerHeight;
	if (mapFrameHeight == null) {
		mapFrameHeight = thePageDoc.body.clientHeight;
	}
	return mapFrameHeight;
}

function checkCurrentExtent() {
	var msg = "Current Extent:\nLeft: " + left + "\nBottom: " + bottom + "\nRight: " + right + "\nTop: " + top;
	var ratio1 = xDistance/fullWidth;
	msg += "\n\nRatio to Full Extent: " + ratio1;
	alert(msg);
}


/*  ************************
*	Mode display functions
*	************************
*/
// write out ModeFrame page
function writeModeFrame(currentMode) {
	parent.ModeFrame.document.location= appDir + "ModeFrame.htm";
}

// write out Mode on dynamic layer
function writeModeLayers(currentMode) {
	var content = '<font face="' + modeLayerFont + '"color="' +  modeLayerShadowColor + '"size=' + modeLayerSize + '><b>' + currentMode + '</b></font>';
	replaceLayerContent("theMode1",content);
	content = '<font face="' + modeLayerFont + '"color="' +  modeLayerColor + '"size=' + modeLayerSize + '><b>' + currentMode + '</b></font>';
	replaceLayerContent("theMode2",content);

}

/*  *****************************************************
*	Various String manipulation Functions
*	*****************************************************
*/

// swap out double quotes for single
function swapQuotes2(inText) {
	var doubleQuote = dQuote;
	var singleQuote = "'";
	var preTemp = "";
	var posTemp = "";
	var nextPos = 0;
	var ePos = inText.length;
	var pos=9;
	while (pos != -1) {
		pos = inText.indexOf(dQuote);
		if (pos!=-1) {
			nextPos=pos+1;
			preTemp = inText.substring(0,pos);
			posTemp = inText.substring(nextPos,ePos);
			inText = preTemp + sQuote + posTemp;
		}
	}
	return inText;
}

function swapQuotes(inText) {
	inText = inText.replace(/"/g, "'");
	return inText;
}

// convert hexidecimal rgb number to delimited decimal rgb
function convertHexToDec(hexColor) {
	var pos = hexColor.indexOf(",");
	var decString = hexColor;
	if (pos==-1) {
		pos = hexColor.indexOf("#");
		if (pos!=-1) {
			hexColor = hexColor.substring((pos + 1),(pos + 7));
		}
		//alert(hexColor);
		var redHex = hexColor.substring(0,2);
		var greenHex = hexColor.substring(2,4);
		var blueHex = hexColor.substring(4,6);
		decString = parseInt(redHex,16) + "," + parseInt(greenHex,16) + "," + parseInt(blueHex,16);
		
	} 
	//alert(decString);
	return decString;
	
}


// swap out one interior string with another
function swapStuff(oldString,oldStuff,newStuff) {
	var pos = 0;
	var rpos = 0;
	var epos = 0;
	var leftString = "";
	var rightString = "";
	pos = oldString.indexOf(oldStuff);			
	while (pos!=-1) {
		epos = oldString.length;
		rpos = pos + oldStuff.length;
		leftString = oldString.substring(0,pos);
		rightString = oldString.substring(rpos,epos);
		oldString = leftString + newStuff + rightString;
		pos = oldString.indexOf(oldStuff);
	}
	leftString=null;
	rightString=null;
	return oldString;
}

/*  *****************************************************
*	Various utility Functions
*	*****************************************************
*/

// disables error checking
function clearError() {
	return true;
}

// reset error checking to default
function resetError() {
	return false;
}


function reloadApp() {
	if (isNav) {
		document.location = "default.htm";
	}
}	
