aimsMapPresent=true;

var useLimitExtent=false;
var getStartingExtent=true;
var getLimitExtent=true;
var enforceFullExtent=false;

var eLeft = -180.0;
var eRight = 180.0;
var eTop = 90.0;
var eBottom = -90.0;
var fullLeft = eLeft;
var fullRight = eRight;
var fullTop = eTop;
var fullBottom = eBottom;
var iWidth = 630;
var iHeight = 512;
var i2Width = 150;
var i2Height = 120;

var imageLimitLeft = limitLeft;
var imageLimitTop = limitTop;
var imageLimitRight = limitRight;
var imageLimitBottom = limitBottom;

var appDir = "";

var xDistance = Math.abs(eRight-eLeft);
var yDistance = Math.abs(eTop-eBottom);
var panX = xDistance * panFactor;
var panY = yDistance * panFactor;
var pixelX = xDistance/iWidth;
var pixelY = yDistance/iHeight;
var mapX = eLeft;
var mapY = eTop;
var lastLeft = eLeft;
var lastRight = eRight;
var lastTop = eTop;
var lastBottom = eBottom;
var fullOVLeft = eLeft;
var fullOVRight = eRight;
var fullOVTop = eTop;
var fullOVBottom = eBottom;
var theCursor = "crosshair";
var canLoad=false;

var fullWidth = Math.abs(fullRight - fullLeft);
var fullHeight = Math.abs(fullTop - fullBottom);
var fullOVWidth = Math.abs(fullOVRight - fullOVLeft);
var fullOVHeight = Math.abs(fullOVTop - fullOVBottom);
var mapScaleFactor = xDistance / iWidth;

var geocodeX = 0;
var geocodeY = 0;
var showGeocode = false;
var geocodeLabel = "";

var queryZoom=false;

var sQuote = "'";
var dQuote = '"';

var toolMode = 0;
var legendVisible=false;


MapUnits = MapUnits.toUpperCase();
ScaleBarUnits = ScaleBarUnits.toUpperCase();
 
// zoom out to full extent
function fullExtent() {
	if (aimsDHTMLPresent) moveLayer("theMap",hspc,vspc);
	window.scrollTo(0,0);
	eLeft = fullLeft;
	eRight = fullRight;
	eTop = fullTop;
	eBottom = fullBottom;
	sendMapXML();
}

function startExtent() {
	if (aimsDHTMLPresent) moveLayer("theMap",hspc,vspc);
	window.scrollTo(0,0);
	eLeft = startLeft;
	eRight = startRight;
	eTop = startTop;
	eBottom = startBottom;
	//var theString = writeXML();
	sendMapXML();
}

function zoomToEnvelope(minXin,minYin,maxXin,maxYin) {
	eLeft=minXin;
	eBottom=minYin;
	eRight=maxXin;
	eTop=maxYin;
	sendMapXML()	
}


function getCommandLineParams(cmdString) {
	setLayerVisible.length=0;
	var cmdString2 = cmdString.toUpperCase();
	var startpos = 0;
	var endpos = 0;
	var pos = cmdString2.indexOf("HOST=");
	if (pos!=-1) {
		startpos = pos + 5;
		endpos = cmdString.indexOf("&",startpos);
		if (endpos==-1) endpos = cmdString.length;
		hostName = cmdString.substring(startpos,endpos);
		serverURL  = "http://" + hostName + "/servlet/com.esri.esrimap.Esrimap?ServiceName=";
	}
	pos = cmdString2.indexOf("SERVICE="); // formally was MAPSERVICE=
	if (pos!=-1) {
		startpos = pos + 8;
		endpos = cmdString.indexOf("&",startpos);
		if (endpos==-1) endpos = cmdString.length;
		imsURL = serverURL + cmdString.substring(startpos,endpos);
	}
	pos = cmdString2.indexOf("OVMAP="); // formally was OVMAPSERVICE=
	if (pos!=-1) {
		startpos = pos + 6;
		endpos = cmdString.indexOf("&",startpos);
		if (endpos==-1) endpos = cmdString.length;
		imsOVURL = serverURL + cmdString.substring(startpos,endpos);
	}
	pos = cmdString2.indexOf("BOX=");
	if (pos!=-1) {
		startpos = pos + 4;
		endpos = cmdString.indexOf("&",startpos);
		if (endpos==-1) endpos = cmdString.length;
		var boxString = cmdString.substring(startpos,endpos);
		var xyBox = boxString.split(":");
		if (xyBox.length==4) {
			startLeft = parseFloat(xyBox[0]);
			startBottom = parseFloat(xyBox[1]);
			startRight = parseFloat(xyBox[2]);
			startTop = parseFloat(xyBox[3]);
			eLeft=startLeft;
			eBottom=startBottom;
			eRight=startRight;
			eTop = startTop;
		}
		//xyBox=null;
	}
	pos = cmdString2.indexOf("MAXRECT=");
	if (pos!=-1) {
		startpos = pos + 8;
		endpos = cmdString.indexOf("&",startpos);
		if (endpos==-1) endpos = cmdString.length;
		var boxString = cmdString.substring(startpos,endpos);
		var xyBox = boxString.split(":");
		if (xyBox.length==4) {
			limitLeft = xyBox[0];
			limitBottom = xyBox[1];
			limitRight = xyBox[2];
			limitTop = xyBox[3];
		}
	}
	if (aimsLayersPresent) {
		// you need to have aimsLayers.js loaded
		pos = cmdString2.indexOf("LAYERS=");
		if (pos!=-1) {
			startpos = pos + 7;
			endpos = cmdString.indexOf("&",startpos);
			if (endpos==-1) endpos = cmdString.length;
			var layers = cmdString.substring(startpos,endpos);
			var numLayers=layers.length;
			icount=0;
			while (icount<=numLayers-1) {
			  onoff=layers.substring(icount,icount+1);
			  if (onoff=='0') {
			  	setLayerVisible[icount]=false;
			  }
			  else if (onoff=='1') {
			    setLayerVisible[icount]=true;
			  }
			  icount=icount+1;
			}
			layers="";
		}
		
		pos = cmdString2.indexOf("ACTIVELAYER=");
		if (pos!=-1) {
			startpos = pos + 12;
			endpos = cmdString.indexOf("&",startpos);
			if (endpos==-1) endpos = cmdString.length;
			var actlyr = cmdString.substring(startpos,endpos);
			ActiveLayerIndex = parseInt(actlyr);
			selectType = LayerType[ActiveLayerIndex];	
			actlyr = "";
		}	
	}
	
	pos = cmdString2.indexOf("STARTLEFT=");
	if (pos!=-1) {
		startpos = pos + 10;
		endpos = cmdString.indexOf("&",startpos);
		if (endpos==-1) endpos = cmdString.length;
		startLeft = cmdString.substring(startpos,endpos);
	}
	pos = cmdString2.indexOf("STARTTOP=");
	if (pos!=-1) {
		startpos = pos + 9;
		endpos = cmdString.indexOf("&",startpos);
		if (endpos==-1) endpos = cmdString.length;
		startTop = cmdString.substring(startpos,endpos);
	}
	pos = cmdString2.indexOf("STARTRIGHT=");
	if (pos!=-1) {
		startpos = pos + 11;
		endpos = cmdString.indexOf("&",startpos);
		if (endpos==-1) endpos = cmdString.length;
		startRight = cmdString.substring(startpos,endpos);
	}
	pos = cmdString2.indexOf("STARTBOTTOM=");
	if (pos!=-1) {
		startpos = pos + 12;
		endpos = cmdString.indexOf("&",startpos);
		if (endpos==-1) endpos = cmdString.length;
		startBottom = cmdString.substring(startpos,endpos);
	}
	pos = cmdString2.indexOf("LIMITLEFT=");
	if (pos!=-1) {
		startpos = pos + 10;
		endpos = cmdString.indexOf("&",startpos);
		if (endpos==-1) endpos = cmdString.length;
		limitLeft = cmdString.substring(startpos,endpos);
	}
	pos = cmdString2.indexOf("LIMITTOP=");
	if (pos!=-1) {
		startpos = pos + 9;
		endpos = cmdString.indexOf("&",startpos);
		if (endpos==-1) endpos = cmdString.length;
		limitTop = cmdString.substring(startpos,endpos);
	}
	pos = cmdString2.indexOf("LIMITRIGHT=");
	if (pos!=-1) {
		startpos = pos + 11;
		endpos = cmdString.indexOf("&",startpos);
		if (endpos==-1) endpos = cmdString.length;
		limitRight = cmdString.substring(startpos,endpos);
	}
	pos = cmdString2.indexOf("LIMITBOTTOM=");
	if (pos!=-1) {
		startpos = pos + 12;
		endpos = cmdString.indexOf("&",startpos);
		if (endpos==-1) endpos = cmdString.length;
		limitBottom = cmdString.substring(startpos,endpos);
	}
	pos = cmdString2.indexOf("EXTENT=AUTO");
	if (pos!=-1) {
		startLeft=0;
		startRight=0;
		startTop=0;
		startBottom=0;
		limitLeft=0;
		limitRight=0;
		limitTop=0;
		limitBottom=0;
		getStartingExtent=true;
		getLimitExtent=true;
	}
	
	if (imsURL!="") {
		imsQueryURL= imsURL + "&CustomService=Query";
		imsGeocodeURL = imsURL + "&CustomService=Geocode";
		//canLoad=false;
	} 
	if ((startLeft!=0) && (startRight!=0)) getStartingExtent=false;
	if ((limitLeft!=0) && (limitRight!=0)) {
		getLimitExtent=false;
		enforceFullExtent=true;
	}
}


function getPath(theFullPath) {
	var theSlash = theFullPath.lastIndexOf("/");
	var theDir = theFullPath.substring(0,theSlash);
	if (theDir==null) theDir="";
	theDir = theDir + "/";
	return theDir;

}

// check for existance of layer
function hasLayer(name) {
	var result = false;
	if (isNav4) {
		if (document.layers[name]!=null) result=true;
	}  else if (isIE) {
		if (eval('document.all.' + name)!=null) result=true;
	} else if (isNav) {
		var theElements = document.getElementsByTagName("DIV");
		var theObj;
		var j = -1;
		for (i=0;i<theElements.length;i++) {
			if (theElements[i].id==name) result=true;
		}
    }
	return result;
}

function showRetrieveData() {
	if (hasLayer("LoadData")) {
		showLayer("LoadData");
	}
}

function hideRetrieveData() {
	if (hasLayer("LoadData")) {
		hideLayer("LoadData");
	}
}

function showRetrieveMap() {
	if (hasLayer("LoadMap")) {
		showLayer("LoadMap");
	}
}

function hideRetrieveMap() {
	if (hasLayer("LoadMap")) {
		hideLayer("LoadMap");
	}
}

function beforeMapRefresh() {}

function afterMapRefresh() {}	

function zoomButton(zoomType) {
	if (zoomType == 1) {
		// zoom in
		eLeft = eLeft + (xHalf/2);
		eRight = eRight - (xHalf/2);
		eTop = eTop - (yHalf/2);
		eBottom = eBottom + (yHalf/2);
	}
	else {
		// zoom out
		eLeft = eLeft - xHalf;
		eRight = eRight + xHalf;
		eTop = eTop + yHalf;
		eBottom = eBottom - yHalf;
	}
	checkFullExtent();
	//var theString = writeXML();
	sendMapXML();
}

// pan using arrow buttons
function panButton(panType) {
	xDistance = Math.abs(eRight-eLeft);
	yDistance = Math.abs(eTop-eBottom);
	panX = xDistance * panFactor;
	panY = yDistance * panFactor;
	switch(panType) {
	//if (panType == 1) {
	case 1:
		//west
		eLeft = eLeft - panX;
		eRight = eLeft + xDistance;
		break
	case 2:
		// north
		eTop = eTop + panY;
		eBottom = eTop - yDistance;
		break
	case 3:
		// east
		eRight = eRight + panX;
		eLeft = eRight - xDistance;
		break
	case 4:
		// south
		eBottom = eBottom - panY;
		eTop = eBottom + yDistance;
		break
	case 5:
		// southwest
		eTop = eTop - panY;
		eLeft = eLeft - panX;
		eBottom = eTop - yDistance;
		eRight = eLeft + xDistance;
		break
	case 6:
		// northwest
		eTop = eTop + panY;
		eLeft = eLeft - panX;
		eBottom = eTop - yDistance;
		eRight = eLeft + xDistance;
		break
	case 7:
		// northeast
		eTop = eTop + panY;
		eLeft = eLeft + panX;
		eBottom = eTop - yDistance;
		eRight = eLeft + xDistance;
		break
	case 8:
		// southeast
		eTop = eTop - panY;
		eLeft = eLeft + panX;
		eBottom = eTop - yDistance;
		eRight = eLeft + xDistance;
	}
	checkFullExtent();
	sendMapXML();
	
}

function checkFullExtent() {
	if (xDistance>fullWidth) xDistance = fullWidth;
	if (yDistance>fullHeight) yDistance = fullHeight;
	if (enforceFullExtent) {
		if (eLeft < limitLeft) {
			eLeft = limitLeft;
			eRight = eLeft + xDistance;
		}
		if (eTop > imageLimitTop) {
			eTop = imageLimitTop;	
			eBottom = eTop - yDistance;
		}	
		if (eRight > imageLimitRight) {
			eRight = imageLimitRight;
			eLeft = eRight - xDistance;
		}
		if (eBottom < imageLimitBottom) {
			eBottom = imageLimitBottom;	
			eTop = eBottom + yDistance;
		}	
	}

}