// aimsMap.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*		dependent on aimsXML.js, ArcIMSparam.js, aimsCommon.js
*/

var aimsMapPresent=true;

// global variables
	// show xml responses
var debugOn = 0;
  // can debug be toggled?
var setDebug = true;

var useLimitExtent=false;
var getStartingExtent=true;
var getLimitExtent=true;
var enforceFullExtent=false;

var scale=0;
var plot=false;
var plotlr, plottb;
var plotwidth, plotheight;
var finishplot=false;
var templeft=0;
var tempright=0;
var temptop=0;
var tempbottom=0;

// map extents. . . dynamically updated
var left = -180.0;
var right = 180.0;
var top = 90.0;
var bottom = -90.0;
var fullLeft = left;
var fullRight = right;
var fullTop = top;
var fullBottom = bottom;
// map size . . . dynamically updated
var mWidth = 0;
var mHeight = 0;
var sWidth = 0;
var sHeight =0;
var ovRatio =0;
var locHeight = 0;
var locWidth =0;
var loadBannerLeft = 0;
var loadBannerTop = 0;
var iWidth = 630;
var iHeight = 512;
// location map size . . . dynamically updated
var i2Width = 150;
var i2Height = 120;


var lr=0,tb=0;
var doprint = false;
//variables for Printing 
var maptitle = "Columbia County IMS";
var printdate = "";
var thescale = "";
var userscale = 0;
var pagesize = "";
var pagelayout="";

var simpscale = 0;
var snapscale = 0;
var fixscale = 0;
var imageLimitLeft = limitLeft;
var imageLimitTop = limitTop;
var imageLimitRight = limitRight;
var imageLimitBottom = limitBottom;
var setscale=0;
var appDir = "";

var xDistance = Math.abs(right-left);
var yDistance = Math.abs(top-bottom);
var panX = xDistance * panFactor;
var panY = yDistance * panFactor;
var pixelX = xDistance/iWidth;
var pixelY = yDistance/iHeight;
var mapX = left;
var mapY = top;
var lastLeft = left;
var lastRight = right;
var lastTop = top;
var lastBottom = bottom;
var fullOVLeft = left;
var fullOVRight = right;
var fullOVTop = top;
var fullOVBottom = bottom;
var theCursor = "crosshair";
var canLoad=true;
if (imsURL!="") 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 sQuote = "'";
var dQuote = '"';

var toolMode = 1;
var legendVisible=false;
// set legend visible at service load... showTOC=false & hasTOC=true
if ((hasTOC) && (!showTOC)) legendVisible=true;

MapUnits = MapUnits.toUpperCase();
ScaleBarUnits = ScaleBarUnits.toUpperCase();

// save the extent 
function saveLastExtent() {
	lastLeft = left;
	lastRight = right;
	lastTop = top;
	lastBottom = bottom;
}

// zoom out to full extent
function fullExtent() {
	if (aimsDHTMLPresent) moveLayer("theMap",hspc,vspc);
	window.scrollTo(0,0);
	saveLastExtent();
	left = fullLeft;
	right = fullRight;
	top = fullTop;
	bottom = fullBottom;
	//var theString = writeXML();
	sendMapXML();
}

function startExtent() {
	if (aimsDHTMLPresent) moveLayer("theMap",hspc,vspc);
	window.scrollTo(0,0);
	saveLastExtent();
	left = startLeft;
	right = startRight;
	top = startTop;
	bottom = startBottom;
	//var theString = writeXML();
	sendMapXML();
}

// zoom back to last extent
function zoomBack() {
	if (aimsDHTMLPresent) moveLayer("theMap",hspc,vspc);
	var left1 = left;
	var right1 = right;
	var top1 = top;
	var bottom1 = bottom;
	left = lastLeft;
	right = lastRight;
	top = lastTop;
	bottom = lastBottom;
	lastLeft = left1;
	lastRight = right1;
	lastTop = top1;
	lastBottom = bottom1;
	//var theString = writeXML();
	sendMapXML();	
}

function zoomToPoint(xIn, yIn, drawIt,theLabel) {
	//alert(xIn + "," + yIn);
	var mWMargin = 0;
	var mHMargin = 0;
	//mWMargin = Math.abs(fullRight-fullLeft) * selectPointMargin;
	//mHMargin = Math.abs(fullTop-fullBottom) * selectPointMargin;
	mWMargin = Math.abs(limitRight-limitLeft) * selectPointMargin;
	mHMargin = Math.abs(limitTop-limitBottom) * selectPointMargin;
	//alert(mWMargin + "," + mHMargin);
	saveLastExtent();
	left = xIn - mWMargin;
	right = xIn + mWMargin;
	top = yIn + mHMargin;
	bottom = yIn - mHMargin;
	if (drawIt) {
		showGeocode=true;
		geocodeX=xIn;
		geocodeY=yIn;
		geocodeLabel=theLabel;
	}
	sendMapXML();
}

function zoomToEnvelope(minXin,minYin,maxXin,maxYin) {
	//alert(minXin + ", " + minYin + ", " + maxXin + ", " + maxYin)
	saveLastExtent();
	left=minXin;
	bottom=minYin;
	right=maxXin;
	top=maxYin;
	//alert("Left:" + minXin + "\nTop:" + maxYin + "\nRight:" + maxXin + "\nBottom:" + minYin);
	sendMapXML()
	
}

// zoom to center of fullextent at set scale
function zoomScale(inScale) {
	// inScale is scale factor where 1.0 = 100% of fullWidth and fullHeight
	//inScale=(inScale/21000);
	var halfWidth = fullWidth / 2;
	var halfHeight = fullHeight / 2
	var midX = right - (xDistance / 2);
	var midY = top - (yDistance / 2);
	var newWidth = halfWidth * inScale;
	var newHeight = halfHeight * inScale;
	saveLastExtent();
	left = midX - newWidth;
	right = midX + newWidth;
	top = midY + newHeight;
	bottom = midY - newHeight;
	sendMapXML();
	
}

// get URLs and extents from URL
function getCommandLineParams(cmdString) {
	var cmdString2 = cmdString.toUpperCase();
	var pos = cmdString2.indexOf("MAPSERVICE=");
	var startpos = 0;
	var endpos = 0;
	if (pos!=-1) {
		startpos = pos + 11;
		endpos = cmdString.indexOf("&",startpos);
		if (endpos==-1) endpos = cmdString.length;
		imsURL = serverURL + cmdString.substring(startpos,endpos);
	}
	var pos = cmdString2.indexOf("OVMAPSERVICE=");
	if (pos!=-1) {
		startpos = pos + 13;
		endpos = cmdString.indexOf("&",startpos);
		if (endpos==-1) endpos = cmdString.length;
		imsOVURL = serverURL + cmdString.substring(startpos,endpos);
	}
	var 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);
	}
	var 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);
	}
	var 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);
	}
	var 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);
	}
	var 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);
	}
	var 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);
	}
	var 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);
	}
	var 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);
	}
	var 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;
	}
	//alert("imsURL=" + imsURL + "\nimsOVURL=" + imsOVURL);
	if (imsURL!="") {
		imsQueryURL= imsURL + "&CustomService=Query";
		imsGeocodeURL = imsURL + "&CustomService=Geocode";
		canLoad=false;
	} 
}

// get directory path of URL
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;
}

// put up the "RetriveData" image
function showRetrieveData() {
	if (hasLayer("LoadData")) {
		showLayer("LoadData");
	}
}

// hide the "RetriveData" image
function hideRetrieveData() {
	if (hasLayer("LoadData")) {
		hideLayer("LoadData");
	}
}

// put up the "RetriveMap" image
function showRetrieveMap() {
	if (hasLayer("LoadMap")) {
		showLayer("LoadMap");
	}
}

// hide the "RetriveMap" image
function hideRetrieveMap() {
	if (hasLayer("LoadMap")) {
		hideLayer("LoadMap");
	}
}

/*  *****************************************************
*	Various Distance Conversion Functions
*	*****************************************************
*/

// get scale bar distance
function getScaleBarDistance() {
parent.ScaleFrame.document.open();
parent.ScaleFrame.document.writeln('<SCRIPT LANGUAGE="Javascript">');
parent.ScaleFrame.document.writeln('function zoomtoscale() {');
parent.ScaleFrame.document.writeln('var zscale=0;');
parent.ScaleFrame.document.writeln('zscale=document.zoomscale.text1.value;');
parent.ScaleFrame.document.writeln('if(parent.MapFrame.fixscale != zscale){');
parent.ScaleFrame.document.writeln('parent.MapFrame.fixscale=zscale;');
parent.ScaleFrame.document.writeln('parent.MapFrame.zoomscale(zscale);');
parent.ScaleFrame.document.writeln('}');
parent.ScaleFrame.document.writeln('}');
parent.ScaleFrame.document.writeln('function zoomtosection() {');
parent.ScaleFrame.document.writeln('var Value1 = document.zoomscale.text2.value;');
parent.ScaleFrame.document.writeln('parent.MapFrame.getSecQuery(Value1);');
parent.ScaleFrame.document.writeln('}');
parent.ScaleFrame.document.writeln('function zoomtosubdivision() {');
parent.ScaleFrame.document.writeln('var Value1 = document.zoomscale.SubValue.value;');
parent.ScaleFrame.document.writeln('parent.MapFrame.getSubQuery(Value1);');
parent.ScaleFrame.document.writeln('}');
parent.ScaleFrame.document.writeln('function newImage(arg) {');
parent.ScaleFrame.document.writeln('	if (document.images) {');
parent.ScaleFrame.document.writeln('		rslt = new Image();');
parent.ScaleFrame.document.writeln('		rslt.src = arg;');
parent.ScaleFrame.document.writeln('		return rslt;');
parent.ScaleFrame.document.writeln('	}');
parent.ScaleFrame.document.writeln('}');
parent.ScaleFrame.document.writeln('function changeImages() {');
parent.ScaleFrame.document.writeln('	if (document.images && (preloadFlag == true)) {');
parent.ScaleFrame.document.writeln('		for (var i=0; i<changeImages.arguments.length; i+=2) {');
parent.ScaleFrame.document.writeln('			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];');
parent.ScaleFrame.document.writeln('		}');
parent.ScaleFrame.document.writeln('	}');
parent.ScaleFrame.document.writeln('}');
parent.ScaleFrame.document.writeln('var preloadFlag = false;');
parent.ScaleFrame.document.writeln('function preloadImages() {');
parent.ScaleFrame.document.writeln('	if (document.images) {');
parent.ScaleFrame.document.writeln('		ZoomtoScale_01_over = newImage("/GISWeb/WaukCo/images/images/ZoomtoScale_01-over.png");');
parent.ScaleFrame.document.writeln('		ZoomtoScale_01_down = newImage("/GISWeb/WaukCo/images/images/ZoomtoScale_01-down.png");');
parent.ScaleFrame.document.writeln('		preloadFlag = true;');
parent.ScaleFrame.document.writeln('	}');
parent.ScaleFrame.document.writeln('}');
parent.ScaleFrame.document.writeln('</SCRIPT></head>');
		var reswidth = screen.width;
		var u = Math.pow(10,0);
		var zscale=0;
		var scaleright = parseInt(right * u + 0.5) / u;
		var scaleleft =  parseInt(left * u + 0.5) / u;
		var offLR=(scaleright-scaleleft);
		if (reswidth=640){
		lr=7;
		tb=5.5;
		}
		if (reswidth=800){
		lr=8.5;
		tb=6.5;
		}
		if (reswidth=1024){
		lr=9;
		tb=7;
		}
		if (reswidth=1280){
		lr=10;
		tb=7.5;
		}
		zscale = offLR/lr;
		//alert(setscale);
		if (setscale == 0){
		zscale = parseInt(zscale * u + 0.5) / u;
		//alert(zscale);
		}else{
		zscale = setscale;
		setscale=0;
		}
		snapscale = zscale;
		//alert("FIXSCALE: " + fixscale);
		//alert("zscale: " + zscale);
		if((fixscale != zscale) && (fixscale != 0)){
			zscale = fixscale;
			fixscale = 0;
		}
		parent.ScaleFrame.document.writeln('<BODY background="images/ColumbiaBack.png" text="#6699CC" LEFTMARGIN=0 TOPMARGIN=0 link=Blue vlink=Blue alink=Blue ONLOAD="preloadImages();">');
		parent.ScaleFrame.document.writeln('<form id=zoomscale name=zoomscale><center><strong>');
		parent.ScaleFrame.document.writeln('<table width="95%" bgcolor="#FFFFFF" border=0 cellpadding=0 cellspacing=0 align="center" valign="center">');
		parent.ScaleFrame.document.writeln('<td><font face=Tahoma size=2 color="#000080"><b><i>Zoom To: &nbsp;</i></b> 1in= <INPUT type="text" id=text1 name=text1 size=2 value=' + zscale + '> ft');
		parent.ScaleFrame.document.writeln('</font></td><td valign="center"><A HREF="#"');
		parent.ScaleFrame.document.writeln('ONMOUSEOVER="changeImages(\'ZoomScale-a\', \'/Website/ColumbiaCo/images/Buttons/Go-b.jpg\'); return true;"');
		parent.ScaleFrame.document.writeln('ONMOUSEOUT="changeImages(\'ZoomScale-a\', \'/Website/ColumbiaCo/images/Buttons/Go-a.jpg\'); return true;"');
		parent.ScaleFrame.document.writeln('ONMOUSEDOWN="changeImages(\'ZoomScale-a\', \'/Website/ColumbiaCo/images/Buttons/Go-a.jpg\'); zoomtoscale(); return true;"');
		parent.ScaleFrame.document.writeln('ONMOUSEUP="changeImages(\'ZoomScale-a\', \'/Website/ColumbiaCo/images/Buttons/Go-b.jpg\'); return true;">');
		parent.ScaleFrame.document.writeln('<IMG NAME="ZoomScale-a" SRC="/Website/ColumbiaCo/images/Buttons/Go-a.jpg" BORDER=0></A></td>');
		parent.ScaleFrame.document.writeln('</strong></center></table></form></BODY></HTML>');
		parent.ScaleFrame.document.close();
	// get distance from left and right values in map units
	// convert to ScaleBar units then clip to ScaleBar size
	var mUnits = MapUnits;
	var mDistance = right - left;
	var sbDistance = 0
	if (MapUnits=="DEGREES") {
		// if DEGREES then convert to feet
			// first get mid y point
		var midY = bottom + (top-bottom)/2;
		var Lon1 = left * Math.PI / 180;
		var Lon2 = right * Math.PI / 180;
		var Lat1 = midY * Math.PI / 180;
		var Lat2 = midY * Math.PI / 180;
		var LonDist = Math.abs(Lon1-Lon2);
		var LatDist = Math.abs(Lat1-Lat2);
		var A = Math.pow(Math.sin(LatDist / 2),2) + Math.cos(Lat1) * Math.cos(Lat2) * Math.pow(Math.sin(LonDist /2),2);
		//var A = Math.cos(Lat1) * Math.cos(Lat2) * Math.pow(Math.sin(LonDist /2),2);
		var C = 2 * Math.asin(Math.min(1, Math.sqrt(A)));
		var D = (3963 - 13 * Math.sin((Lat1 + Lat2) / 2)) * C
		mDistance = D * 5280;
		mUnits = "FEET";
	}
	if (mUnits != ScaleBarUnits) {
		theDist = mDistance
		mDistance = convertUnits(theDist,mUnits,ScaleBarUnits);
	}
	
	sbDistance = mDistance/5;
	// /*
	var num1 = 0;
	var num2 = sbDistance;
	if (sbDistance>10000000) {
		num1 = parseInt(sbDistance/5000000);
		num2 = num1 * 5000000;
	} else if (sbDistance>1000000) {
		num1 = parseInt(sbDistance/500000);
		num2 = num1 * 500000;
	} else if (sbDistance>100000) {
		num1 = parseInt(sbDistance/50000);
		num2 = num1 * 50000;
	} else if (sbDistance>10000) {
		num1 = parseInt(sbDistance/5000);
		num2 = num1 * 5000;
	} else if (sbDistance>1000) {
		num1 = parseInt(sbDistance/500);
		num2 = num1 * 500;
	} else if (sbDistance>100) {
		num1 = parseInt(sbDistance/50);
		num2 = num1 * 50;
	} else if (sbDistance>10) {
		num1 = parseInt(sbDistance/5);
		num2 = num1 * 5;
	} else if (sbDistance>1) {
		num1 = parseInt(sbDistance/0.25);
		num2 = num1 * 0.25;
	} else if (sbDistance>0.1) {
		num1 = parseInt(sbDistance/0.025);
		num2 = num1 * 0.025;
	}
	sbDistance = num2;
	// */
	if (sbDistance > 2) {
		ScaleBarPrecision = "0";
	} else if (sbDistance > 1) {
		ScaleBarPrecision = "1";
	} else if (sbDistance > 0.1) {
		ScaleBarPrecision = "2";
	} else if (sbDistance > 0.01) {
		ScaleBarPrecision = "3";
	} else {
		ScaleBarPrecision = "4";
	}
	return sbDistance;
}

function zoomscale(value){
var printactive = true;
		var reswidth = screen.width;
		scale = 0;
		var lr=0,tb=0;
		var mapscale=0;
		var userscale=0;
		var off=0;
		var jump=false;
		userscale=value;
		if(userscale == 0){
		alert("A Scale of Zero Can Not be Set!");
		}else{
		var offLR=(right-left)/2;
		var offTB=(top-bottom)/2;
		if (reswidth==640){
		lr=7;
		tb=4.25;
		}
		if (reswidth==800){
		lr=8.25;
		tb=5.50;
		}
		if (reswidth==1024){
		lr=9;
		tb=6.25;
		}
		if(reswidth==1280){
		lr=9.75;
		tb=7;
		}
		scale = offLR/lr;
		while(userscale != scale){
		if (userscale > scale){
		//zoomout to printscale
		var changeLR = (userscale * lr)/2;
		changeLR = (changeLR - offLR);
		left = (left - changeLR);
		right = (right + changeLR);
		var changeTB = (userscale * tb)/2;
		changeTB = (changeTB - offTB);
		top = (top + changeTB);
		bottom = (bottom - changeTB);
		}else{
		//alert("in");
		//zoomin to printscale
		var changeLR = (userscale * lr)/2;
		changeLR = (offLR - changeLR);
		left = left + changeLR;
		right = right - changeLR;
		var changeTB = (userscale * tb)/2;
		changeTB = (offTB - changeTB);
		top = top - changeTB;
		bottom = bottom + changeTB;
		}
		offLR = right-left;
		offTB = top-bottom;
		scale = offLR/lr;
		scaleTB = offTB/tb;
		}
	sendMapXML();
		}
}

function plotpage(size,layout,scale,title){
var u = Math.pow(10,0);

userscale= scale;
pagesize = size;
pagelayout=layout;
maptitle = title;

	if(pagesize == 0){
		alert("Please select a Page Size!");
	}else{


var plotscale = 0;
if (pagesize=="A"){
	
	if(layout=="P"){
		legHeight = 150;
		legWidth = 350;
		plotlr = 7.5;
		plottb = 7;
		plotwidth=720;
		plotheight=672;
	}
	if(layout=="L"){
		legHeight =432;
		legWidth = 192;
		plotlr = 8;
		plottb = 7.5;
		plotwidth = 768;
		plotheight = 720;
	}
}
if (pagesize=="B"){

	if(layout=="P"){
		legHeight = 144;
		legWidth = 384;
		plotlr = 7;
		plottb = 8.5;
		plotwidth=672;
		plotheight=816;
	}
	if(layout=="L"){
		legHeight = 384;
		legWidth = 240;
		plotlr = 9.375;
		plottb = 7;
		plotwidth = 900;
		plotheight =672;
	}
}
if (pagesize=="C"){
	if(layout=="P"){
		legHeight = 240;
		legWidth = 576;
		plotlr = 12;
		plottb = 12.5;
		plotwidth=1152;
		plotheight=1200;
	}
	if(layout=="L"){
		legHeight = 768;
		legWidth = 240;
		plotlr =13.5;
		plottb = 12;
		plotwidth =1296;
		plotheight = 1152;
	}
}
if (pagesize=="D"){

	if(layout=="P"){
		legHeight = 384;
		legWidth = 864;
		plotlr = 20;
		plottb = 28;
		plotwidth=1920;
		plotheight=2688;
	}
	if(layout=="L"){
		legHeight = 960;
		legWidth = 336;
		plotlr = 21;
		plottb = 15;
		plotwidth = 2016;
		plotheight = 1440;
	}
}
if (pagesize=="E"){
	if(layout=="P"){
		legHeight = 384;
		legWidth = 768;
		plotlr = 32;
		plottb = 37;
		plotwidth=2304;
		plotheight=2520;
	}
	if(layout=="L"){
		legHeight = 1152;
		legWidth = 384;
		plotlr = 37;
		plottb =32;
		plotwidth=2664;
		plotheight=2304;
	}
}
		templeft=left;
		tempright=right;
		temptop=top;
		tempbottom=bottom;
		left = (left - (plotlr * plotscale));
		right = (right + (plotlr * plotscale));
		top =  (top + (plottb * plotscale));
		bottom = (bottom - (plottb * plotscale));
		offLR=(right-left)/2;
		offTB=(top-bottom)/2;
		plotscale = offLR/plotlr;
		while(userscale != plotscale){
		if (userscale > plotscale){
		//zoomout to printscale
		var changeLR = (userscale * plotlr)/2;
		changeLR = (changeLR - offLR);
		left = (left - changeLR);
		right = (right + changeLR);
		var changeTB = (userscale * plottb)/2;
		changeTB = (changeTB - offTB);
		top = (top + changeTB);
		bottom = (bottom - changeTB);
		}else{
		//zoomin to printscale
		var changeLR = (userscale * plotlr)/2;
		changeLR = (offLR - changeLR);
		left = left + changeLR;
		right = right - changeLR;
		var changeTB = (userscale * plottb)/2;
		changeTB = (offTB - changeTB);
		top = top - changeTB;
		bottom = bottom + changeTB;
		}
		offLR=right-left;
		offTB=top-bottom;
		plotscale = offLR/plotlr;
		plotscale = parseInt(plotscale * u + 0.5) / u;
		scaleTB = offTB/plottb;
		}
		plot=true;
		//sendMapXML();
		plotit()
	}
}
	
function plotit(){
var mapwidth = getMapWidth();
var mapheight = getMapHeight();
var plotString="";
plotString='<?xml version="1.0" encoding="UTF-8" ?>';
plotString=plotString+'<ARCXML version="1.1">';
plotString=plotString+'<REQUEST>';
plotString=plotString+'<GET_IMAGE>';
plotString=plotString+'<PROPERTIES>';
plotString=plotString+'<ENVELOPE minx="' + left +'" miny="' + bottom + '" maxx="' + right + '" maxy="' + top +'" />';
//plotString=plotString+'<LEGEND title="Columbia County Legend" font="' + legFont + '" height="400" autoextend="true" />';
//plotString=plotString+'<LEGEND />';
plotString=plotString+'<IMAGESIZE width="' + mapwidth + '" height="' + mapheight + '" printwidth="' + plotwidth + '" printheight="' + plotheight + '" scalesymbols="true" />';
plotString=plotString+'<LAYERLIST>';
for (var i=0;i<layerCount;i++) {
	if (LayerVisible[i]==1) {
		plotString=plotString+'<LAYERDEF id="' + LayerID[i] + '" visible="true" />';
	}else {
		plotString=plotString+'<LAYERDEF id="' + LayerID[i] + '" visible="false" />';
	}			
}
plotString=plotString+'</LAYERLIST>';
plotString=plotString+'</PROPERTIES>';
plotString=plotString+ plotbuff;
plotString=plotString+ plotsel;

// any custom stuff to fit here
	if (aimsCustomPresentText) plotString+= addCustomTextToMap();

	if (aimsCustomPresentSymbol) plotString+= addCustomSymbolToMap();
	// add custom leader to fit here
	if (aimsCustomPresentLeader && NumLeaderLines>=1 ) {
		plotString+= '<LAYER type="ACETATE" name="allTheClicks">\n';
		plotString+= addCustomLeaderToMap();
		plotString+= '</LAYER>\n';
		}

plotString=plotString+'</GET_IMAGE>';
plotString=plotString+'</REQUEST>';
plotString=plotString+'</ARCXML>';
sendToServer(imsURL,plotString,666);
}

function checkloaded(){
return(maploaded);
}

// calculate distance to current scalebarunits
function calcDistance(mX,mY) {
	if (clickCount>0) {
		var mUnits = MapUnits;
		var mDistance = 0;
		var p = clickCount-1;
		var Lon1 = clickPointX[p] * Math.PI / 180;
		var Lon2 = mX * Math.PI / 180;
		var Lat1 = clickPointY[p] * Math.PI / 180;
		var Lat2 = mY * Math.PI / 180;
		var LonDist = Math.abs(Lon1-Lon2);
		var LatDist = Math.abs(Lat1-Lat2);
		if (MapUnits=="DEGREES") {
			var Lon1 = clickPointX[p] * Math.PI / 180;
			var Lon2 = mX * Math.PI / 180;
			var Lat1 = clickPointY[p] * Math.PI / 180;
			var Lat2 = mY * Math.PI / 180;
			var LonDist = Math.abs(Lon1-Lon2);
			var LatDist = Math.abs(Lat1-Lat2);
			var A = Math.pow(Math.sin(LatDist / 2),2) + Math.cos(Lat1) * Math.cos(Lat2) * Math.pow(Math.sin(LonDist /2),2);
			//var A = Math.cos(Lat1) * Math.cos(Lat2) * Math.pow(Math.sin(LonDist /2),2);
			var C = 2 * Math.asin(Math.min(1, Math.sqrt(A)));
			var D = (3963 - 13 * Math.sin((Lat1 + Lat2) / 2)) * C
			mDistance = D * 5280;
			mUnits = "FEET";
		} else {
			var xD = Math.abs(mX - clickPointX[p]);
			var yD = Math.abs(mY - clickPointY[p]);
			mDistance = Math.sqrt(Math.pow(xD,2) + Math.pow(yD,2));
		}
		
		var theDist = convertUnits(mDistance,mUnits,ScaleBarUnits);
		var u = Math.pow(10,numDecimals);
		currentMeasure = parseInt(theDist*u+0.5)/u;
		
		updateMeasureBox();
		
	}
}

// convert the amounts to new units
function convertUnits(theDist1,mUnits,sUnits) {	
	var theDist = parseFloat(theDist1);
	var mDistance = theDist;
	//alert(theDist);
	if (mUnits == "FEET") {
		if (sUnits=="MILES") {
			mDistance = theDist / 5280;
		} else if (sUnits == "METERS") {
			mDistance = theDist * 0.304800609601;
		} else if (sUnits == "KILOMETERS") {
			mDistance = theDist * 0.000304800609601;
		}
	} else {
		if (sUnits=="MILES") {
			mDistance = theDist * 0.0006213699;
		} else if (sUnits == "FEET") {
			mDistance = theDist * 3.280833;
		} else if (sUnits == "KILOMETERS") {
			mDistance = theDist / 1000;
		}
	}
	var u = Math.pow(10,numDecimals);
	//alert(u);
	if (!isNav) mDistance = parseInt(mDistance * u + 0.5) / u
	//alert(mDistance);
	return mDistance;
}

// set new map extent 
function setExtent(newLeft, newTop, newRight, newBottom) {
//alert(left);
	left = newLeft;
	top = newTop;
	right = newRight;
	bottom = newBottom;
	//alert(left);
}

//  set new full extent 
function setFullExtent(maxLeft, maxTop, maxRight, maxBottom) {
	fullLeft = maxLeft;
	fullTop = maxTop;
	fullRight = maxRight;
	fullBottom = maxBottom;
	fullWidth = Math.abs(fullRight-fullLeft);
	fullHeight = Math.abs(fullTop-fullBottom);
}

function beforeMapRefresh() {
 // add any code for checking parameters before map update
 // called by sendXML() before writing XML request
 //alert("Yup. It works.[1]");
}
 
function afterMapRefresh() {
 // add any code for checking parameters after map update
 // called by processXML() if XMMmode=1 after image and extent is updated
 //alert("Yup. It works.[1]"); 
}	

// zoom using button.
//		zoomType: 1=in; 2=out
function zoomButton(zoomType) {
	saveLastExtent();
	if (zoomType == 1) {
		// zoom in
		left = left + (xHalf/2);
		right = right - (xHalf/2);
		top = top - (yHalf/2);
		bottom = bottom + (yHalf/2);
	}
	else {
		// zoom out
		left = left - xHalf;
		right = right + xHalf;
		top = top + yHalf;
		bottom = bottom - yHalf;
	}
	checkFullExtent();
	//var theString = writeXML();
	sendMapXML();
}

// pan using arrow buttons
function panButton(panType) {
	//alert("Left:" + left + "\nTop:" + top + "\nRight:" + right + "\nBottom:" + bottom + "\nWidth:" + xDistance + "\nHeight:" + yDistance + "\nPanX:" + panX + "\nPanY:" + panY);
	saveLastExtent();
	xDistance = Math.abs(right-left);
	yDistance = Math.abs(top-bottom);
	panX = xDistance * panFactor;
	panY = yDistance * panFactor;
	switch(panType) {
	//if (panType == 1) {
	case 1:
		//west
		left = left - panX;
		right = left + xDistance;
		break
	case 2:
		// north
		top = top + panY;
		bottom = top - yDistance;
		break
	case 3:
		// east
		right = right + panX;
		left = right - xDistance;
		break
	case 4:
		// south
		bottom = bottom - panY;
		top = bottom + yDistance;
		break
	case 5:
		// southwest
		top = top - panY;
		left = left - panX;
		bottom = top - yDistance;
		right = left + xDistance;
		break
	case 6:
		// northwest
		top = top + panY;
		left = left - panX;
		bottom = top - yDistance;
		right = left + xDistance;
		break
	case 7:
		// northeast
		top = top + panY;
		left = left + panX;
		bottom = top - yDistance;
		right = left + xDistance;
		break
	case 8:
		// southeast
		top = top - panY;
		left = left + panX;
		bottom = top - yDistance;
		right = left + xDistance;
	}
	checkFullExtent();
	//alert("Left:" + left + "\nTop:" + top + "\nRight:" + right + "\nBottom:" + bottom + "\nWidth:" + xDistance + "\nHeight:" + yDistance + "\nPanX:" + panX + "\nPanY:" + panY);
	//var theString = writeXML();
	sendMapXML();
	
}

function checkFullExtent() {
	if (xDistance>fullWidth) xDistance = fullWidth;
	if (yDistance>fullHeight) yDistance = fullHeight;
	if (enforceFullExtent) {
		if (left < limitLeft) {
			left = limitLeft;
			right = left + xDistance;
		}
		if (top > imageLimitTop) {
			top = imageLimitTop;	
			bottom = top - yDistance;
		}	
		if (right > imageLimitRight) {
			right = imageLimitRight;
			left = right - xDistance;
		}
		if (bottom < imageLimitBottom) {
			bottom = imageLimitBottom;	
			top = bottom + yDistance;
		}	
	}

}


