/*
	Current File: inc_master.js
	Description: Used for Server Communication and Event Trapping.
	Created By: Eric Schultz
	Date: 1/15/2006

	Relies On: inc_browser.js, inc_dhtml.js, inc_settings.js
*/

/* CLASS: clsExtents */
	function clsExtents() {
		this.X1 = 0.0;
		this.Y1 = 0.0;
		this.X2 = 0.0;
		this.Y2 = 0.0;
		this.init = false;

		this.reset = function() {
			this.X1 = 0.0;
			this.Y1 = 0.0;
			this.X2 = 0.0;
			this.Y2 = 0.0;
			this.init = false;
		};

		this.getMinX = function() { return (this.X1 > this.X2?this.X2:this.X1); };
		this.getMinY = function() { return (this.Y1 > this.Y2?this.Y2:this.Y1); };
		this.getMaxX = function() { return (this.X1 < this.X2?this.X2:this.X1); };
		this.getMaxY = function() { return (this.Y1 < this.Y2?this.Y2:this.Y1); };
		this.getWidth = function() { return (this.getMaxX() - this.getMinX()); };
		this.getHeight = function() { return (this.getMaxY() - this.getMinY()); };
		this.getWidthMid = function() { return ((this.X1 + this.X2) / 2); };
		this.getHeightMid = function() { return ((this.Y1 + this.Y2) / 2); };

		this.isValid = function() {
			return ((!isNaN(this.X1))&&(!isNaN(this.Y1))&&(!isNaN(this.X2))&&(!isNaN(this.Y2)));
		};

		this.clone = function() {
			var temp = new clsExtents();
			temp.X1 = this.X1;
			temp.Y1 = this.Y1;
			temp.X2 = this.X2;
			temp.Y2 = this.Y2;
			temp.init = this.init;
			return temp;
		};
	}


/* Public Variables (Not Customizable) */
	var MapServiceStarted = false;

	var MapServiceExtents = new clsExtents();
	var MapServiceCurrent = new clsExtents();
	var MapSelection = new clsExtents();
	var MapWindow = new clsExtents();
	var MapPrint = new clsExtents();

	var MapVersionID = "";
	var MapVersionDisplay = "";
	var MapVersionMapGrid = "";

	var MapLayerID = [];
	var MapLayerType = [];
	var MapLayerDisplay = [];
	var MapLayerVisible = [];
	var MapPageName = [];
	var MapHistory = [];
	var MapHistoryPointer = 0;

	var MapGotoDisplay = [];
	var MapGotoLink = [];

	var MapHotSpots = [];

	var strCurrentMapSignature = "";
	var strLastMapSignature = "";
	var strLastOVSignature = "";

	var strSearchCounty = "";
	var strSearchMappage = "";
	var strSearchParcel = "";
	var strSearchSubindex = "";
	var strSearchTownship = "";
	var strSearchArea = "";

	var strSearchType = "";
	var strToolbarType = "";

	var mapX, mapY, mouseX, mouseY, mouseDown = false;
	var lastimageWidth, lastimageHeight;
	var realOVWidth , realOVHeight, realMapToOVWidth, realMapToOVHeight;
	var selectedTool;
	var zoomwindowmode;

	var dragapproved = false;
	var initialwidth, initialheight, tempx, tempy, offsetx, offsety;
	var movingObject = "";


/* Main Functions: */
	function resetAll() {
		MapServiceStarted = false;

		MapServiceExtents.reset();
		MapServiceCurrent.reset();
		MapSelection.reset();
		MapWindow.reset();
		MapPrint.reset();

		MapVersionID = "";
		MapVersionDisplay = "";
		MapVersionMapGrid = "";

		MapLayerID = [];
		MapLayerType = [];
		MapLayerDisplay = [];
		MapLayerVisible = [];
		MapPageName = [];

		MapHistory = [];

		MapGotoDisplay = [];
		MapGotoLink = [];

		MapHotSpots = [];

		strCurrentMapSignature = "";
		strLastMapSignature = "";
		strLastOVSignature = "";

		MapHistoryPointer = 0;
	}

	function setTool(mode){
		if (!MapServiceStarted) return; //Service not started or errored out, so exit

		hideToolWindow();
		mouseDown = false;

		//Set the new tool
			switch(mode) {
				case "zoomin":
					var tempExtents = new clsExtents();
					tempExtents.X1 = MapServiceCurrent.getWidthMid() - ((MapServiceCurrent.getWidth()/2) / ZoomMultiplier);
					tempExtents.Y1 = MapServiceCurrent.getHeightMid() + ((MapServiceCurrent.getHeight()/2) / ZoomMultiplier);
					tempExtents.X2 = MapServiceCurrent.getWidthMid() + ((MapServiceCurrent.getWidth()/2) / ZoomMultiplier);
					tempExtents.Y2 = MapServiceCurrent.getHeightMid() - ((MapServiceCurrent.getHeight()/2) / ZoomMultiplier);
					tempExtents.init = true;
					MapServiceCurrent = tempExtents.clone();
					truncateHistory();
					break;
				case "zoomout":
					var tempExtents = new clsExtents();
					tempExtents.X1 = MapServiceCurrent.getWidthMid() - ((MapServiceCurrent.getWidth()/2) * ZoomMultiplier);
					tempExtents.Y1 = MapServiceCurrent.getHeightMid() + ((MapServiceCurrent.getHeight()/2) * ZoomMultiplier);
					tempExtents.X2 = MapServiceCurrent.getWidthMid() + ((MapServiceCurrent.getWidth()/2) * ZoomMultiplier);
					tempExtents.Y2 = MapServiceCurrent.getHeightMid() - ((MapServiceCurrent.getHeight()/2) * ZoomMultiplier);
					tempExtents.init = true;
					MapServiceCurrent = tempExtents.clone();
					truncateHistory();
					break;

				case "killhotspots":
					var confirm_to = confirm("Do you really want to go remove all the hotspots on this page?");
					if (confirm_to == true)	killHotspots("&mappage=" + MapPageName[0]);
					return;

				case "hotspotlink":
					var myHotSpotPreview = document.getElementById("divHotSpotPreview");
					var myBodyFrame = document.getElementById("divBodyFrame");
					if (myHotSpotPreview != null) {
						myBodyFrame.removeChild(myHotSpotPreview);
					}

					var eDiv=document.createElement("DIV");
					eDiv.id = "divHotSpotPreview";
					eDiv.style.position="absolute";
					eDiv.style.zIndex = 66;
					eDiv.style.left = "0px";
					eDiv.style.top = "0px";
					eDiv.style.width = "16px";
					eDiv.style.height = "16px";

					var eImg=document.createElement("IMG");
					eImg.src = "images/icon_star.gif";

					eDiv.appendChild(eImg);
					myBodyFrame.appendChild(eDiv);

					setActiveTool(mode);
					return;
				case "zoominwindow":
				case "zoomoutwindow":
				case "pan":
				case "panalign":
					var myHotSpotPreview = document.getElementById("divHotSpotPreview");
					var myBodyFrame = document.getElementById("divBodyFrame");
					if (myHotSpotPreview != null) {
						myBodyFrame.removeChild(myHotSpotPreview);
					}

					setActiveTool(mode);
					return;
				case "zoomextent":
					MapServiceCurrent = MapServiceExtents.clone();
					truncateHistory();
					break;
				case "pansouth":
					MapServiceHeight = MapServiceCurrent.getHeight() * PanMultiplier;
					MapServiceCurrent.Y1 -= MapServiceHeight;
					MapServiceCurrent.Y2 -= MapServiceHeight;
					truncateHistory();
					break;
				case "paneast":
					MapServiceWidth = MapServiceCurrent.getWidth() * PanMultiplier;
					MapServiceCurrent.X1 += MapServiceWidth;
					MapServiceCurrent.X2 += MapServiceWidth;
					truncateHistory();
					break;
				case "pannorth":
					MapServiceHeight = MapServiceCurrent.getHeight() * PanMultiplier;
					MapServiceCurrent.Y1 += MapServiceHeight;
					MapServiceCurrent.Y2 += MapServiceHeight;
					truncateHistory();
					break;
				case "panwest":
					MapServiceWidth = MapServiceCurrent.getWidth() * PanMultiplier;
					MapServiceCurrent.X1 -= MapServiceWidth;
					MapServiceCurrent.X2 -= MapServiceWidth;
					truncateHistory();
					break;
				case "viewprevious":
					if (MapHistoryPointer <= 0) {
						validateHistory();
						return;
					}

					MapHistoryPointer--;
					parseHistory();
					break;
				case "viewnext":
					if (MapHistoryPointer >= (MapHistory.length-1)) {
						validateHistory();
						return;
					}

					MapHistoryPointer++;
					parseHistory();
					break;
				case "nothing":
					setActiveTool("");
					return;
				case "print":
					//MapPrint = MapServiceCurrent.clone();
					//regeneratePrintImage();
					pollPrintFrame();
					//window.print();
					return;
				case "search":
					pollSearchFrame();
					return;
				case "copy":
					var div = getDIV('imgMapImage' + (intNextMapDiv == 1?2:1), false);
					//div.contentEditable = 'true';
					if (document.body.createControlRange) {
						var controlRange = document.body.createControlRange();
						controlRange.addElement(div);
						controlRange.execCommand('Copy');
					} else {
						alert("Couldn't copy image to clipboard, Please right-click on the image and choose 'Copy Image'.")
					}
					//div.contentEditable = 'false';

					return;
				case "resetframes":
					arrWindowLocations = clone(arrWindowLocationsDefaults);

					for(var x = 0; x < arrWindowLocationsDefaults.length; x++) {
						if (!arrWindowLocationsDefaults[x][5]) {
							moveLayer(arrWindowLocationsDefaults[x][0], arrWindowLocationsDefaults[x][1], arrWindowLocationsDefaults[x][2]);

							if (arrWindowLocationsDefaults[x][3]) {
								showLayer(arrWindowLocationsDefaults[x][0]);
							} else {
								hideLayer(arrWindowLocationsDefaults[x][0]);
							}
						}
					}

					if (isFrameVisibleSettings('divMapOVFrame')) {
						if (strLastOVSignature != strLastMapSignature) {
							//Reload Overview Map
							regenerateOVImage();
						}
					}

					return;
			}

		//Refresh
			regenerateMainImage();

		//Refresh History Buttons
			validateHistory();
	}

	function setToolBar(tool){
		//Reset
		hideLayer('divNavigationFrame');
		hideLayer('divLayerFrame');
		hideLayer('divGotoFrame');
		hideLayer('divMapGridFrame');
		updateContent('divToolbarCaption', '');
		getDIV('divToolNavTab', true).backgroundColor='#FFFFFF';
		getDIV('divToolLayersTab', true).backgroundColor='#FFFFFF';
		getDIV('divToolGotoTab', true).backgroundColor='#FFFFFF';
		getDIV('divToolMapGridTab', true).backgroundColor='#FFFFFF';

		//Set
		if (tool != '') {
			strToolbarType = tool;

			switch(tool){
				case 'divNavigationFrame':
					updateContent('divToolbarCaption', 'Main Toolbar - Nav');
					getDIV('divToolNavTab', true).backgroundColor='#DAE3FF';
					break;
				case 'divLayerFrame':
					updateContent('divToolbarCaption', 'Main Toolbar - Layers');
					getDIV('divToolLayersTab', true).backgroundColor='#DAE3FF';
					break;
				case 'divGotoFrame':
					updateContent('divToolbarCaption', 'Main Toolbar - Goto');
					getDIV('divToolGotoTab', true).backgroundColor='#DAE3FF';
					break;
				case 'divMapGridFrame':
					updateContent('divToolbarCaption', 'Main Toolbar - Grid');
					getDIV('divToolMapGridTab', true).backgroundColor='#DAE3FF';
					break;
				default:
					updateContent('divToolbarCaption', 'Main Toolbar');
					break;
			}

			showLayer(tool);
		}
	}

	function setSearchToolBar(tool){
		//Reset
		hideLayer('divSearchGeneralFrame');
		hideLayer('divSearchSubdivisionFrame');
		updateContent('divSearchCaption', '');
		getDIV('divSearchGeneralTab', true).backgroundColor='#FFFFFF';
		getDIV('divSearchSubdivisionTab', true).backgroundColor='#FFFFFF';

		//Set
		if (tool != '') {
			strSearchType = tool;

			switch(tool){
				case 'divSearchGeneralFrame':
					updateContent('divSearchCaption', 'Search - General');
					getDIV('divSearchGeneralTab', true).backgroundColor='#DAE3FF';
					break;
				case 'divSearchSubdivisionFrame':
					updateContent('divSearchCaption', 'Search - Subdivision');
					getDIV('divSearchSubdivisionTab', true).backgroundColor='#DAE3FF';
					break;
			}

			showLayer(tool);
		}
	}

	function truncateHistory() {
		if (MapHistory.length > 0) {
			MapHistory = MapHistory.slice(0, MapHistoryPointer+1);
			MapHistoryPointer = MapHistory.length-1;
		} else {
			MapHistoryPointer = 0;
		}
	}

	function validateHistory() {}

	function parseHistory() {
		var arrBuildMapHistoryString = MapHistory[MapHistoryPointer].split("|");

		MapLayerID = [];
		MapLayerType = [];
		MapLayerDisplay = [];
		MapLayerVisible = [];
		MapPageName = [];

		MapVersionID = arrBuildMapHistoryString[0];
		MapVersionDisplay = arrBuildMapHistoryString[1];
		MapVersionMapGrid = arrBuildMapHistoryString[2];

		for(var x = 0; x < arrBuildMapHistoryString[3]; x++) {

			MapLayerID.push(arrBuildMapHistoryString[(x * 5) + 4]);
			MapLayerType.push(arrBuildMapHistoryString[(x * 5) + 5]);
			MapLayerDisplay.push(arrBuildMapHistoryString[(x * 5) + 6]);
			MapLayerVisible.push(eval(arrBuildMapHistoryString[(x * 5) + 7]));
			MapPageName.push(arrBuildMapHistoryString[(x * 5) + 8]);

		}

		MapServiceCurrent.X1 = parseFloat(arrBuildMapHistoryString[arrBuildMapHistoryString.length-4]);
		MapServiceCurrent.Y1 = parseFloat(arrBuildMapHistoryString[arrBuildMapHistoryString.length-3]);
		MapServiceCurrent.X2 = parseFloat(arrBuildMapHistoryString[arrBuildMapHistoryString.length-2]);
		MapServiceCurrent.Y2 = parseFloat(arrBuildMapHistoryString[arrBuildMapHistoryString.length-1]);
		MapServiceCurrent.init = true;

		refreshProjectName();
		refreshMapLayers();
		refreshAlignmentLayer();
		refreshMapGrid();
		pollHotSpots();
	}

	//Clear and hide the zoom window box
	function hideToolWindow(){
		if (jg_doc != null) jg_doc.clear();
		zoomwindowmode = 0;
	}

	//Show the Loading Frame in the center of the browser
	function showLoading() {
		setMouseCursor('progress');
		moveLayer('divFrameLoading', (getDIVWidth("divBodyFrame")/2) - (getDIVWidth('divFrameLoading')/2), (getDIVHeight("divBodyFrame")/2) - (getDIVHeight('divFrameLoading')/2));
		showLayer('divFrameLoading');
	}

	//Hide the Loading Frame -- it'll only hide if the intObjectLoading is 0.
	function hideLoading() {
		if (intObjectsLoading == 0) {
			setMouseCursor('default');
			hideLayer('divFrameLoading');
		}
	}

	function initService() {
		if(isNaN(getDIVWidth('divMapFrame' + intNextMapDiv))||isNaN(getDIVHeight('divMapFrame' + intNextMapDiv))) {
			alert("There was an error trying to initiate this service and it stopped you from viewing maps\n\nYou may be using an older or unsupported browser that is not yet supported.\nWe recommend using the following browsers:\n   Mozilla Firefox 2.0 or newer\n   Microsoft Internet Explorer 6.0 or newer\n   Netscape 7.0 or newer");
			hideLayer('divFrameLoading');
			return;
		}

		if ((lastimageWidth != "") && ((lastimageWidth != getDIVWidth('divMapFrame' + intNextMapDiv))||(lastimageHeight != getDIVHeight('divMapFrame' + intNextMapDiv)))) {
			if (strCurrentMapSignature == buildMapRequestString()) {
				showLoading();

				lastimageWidth = getDIVWidth('divMapFrame' + intNextMapDiv);
				lastimageHeight = getDIVHeight('divMapFrame' + intNextMapDiv);
				regenerateMainImage();

				return;
			}
		}

		if (strCurrentMapSignature != buildMapRequestString()) {
			showLoading();

			//TODO - ajax code, loading, intcount++
			executeAJAXVersionLookup();

			MapServiceStarted = true;
			lastimageWidth = getDIVWidth('divMapFrame' + intNextMapDiv);
			lastimageHeight = getDIVHeight('divMapFrame' + intNextMapDiv);
		}
	}

	function regenerateMainImage() {
		if (!MapServiceStarted) return; //Service not started or errored out, so exit

		if (!MapServiceCurrent.init) {
			MapServiceCurrent = MapServiceExtents.clone();
		}

		intObjectsLoading = 0;

		hideToolWindow();
		ensureInBounds(MapServiceCurrent);
		fixAspect(MapServiceCurrent, getDIVWidth('divMapFrame' + intNextMapDiv), getDIVHeight('divMapFrame' + intNextMapDiv));

		strCurrentMapSignature = buildMapRequestString();

		//document.location = "#" + strCurrentMapSignature;

		if (MapHistoryPointer >= (MapHistory.length-1)) {
			if (buildMapHistoryString() != MapHistory[MapHistoryPointer]) {
				MapHistory.push(buildMapHistoryString());
				MapHistoryPointer = MapHistory.length-1;
			}
		}

		if (MapLayersVisible() != 0) {
			regenerateOVImage();

			getDIV('imgMapImage' + intNextMapDiv, false).src = 'ajax/ajaxGetMetadata.php?' + strCurrentMapSignature + '&width=' + getDIVWidth('divMapFrame' + intNextMapDiv) + '&height=' + getDIVHeight('divMapFrame' + intNextMapDiv) + '&bbox=' + MapServiceCurrent.getMinX() + ',' + MapServiceCurrent.getMinY() + ',' + MapServiceCurrent.getMaxX() + ',' + MapServiceCurrent.getMaxY() + '&format=gif&ts=' + (new Date().getTime());
			intObjectsLoading++;
			showLoading();
		} else {
			hideLoading();
		}

		strLastMapSignature = strCurrentMapSignature;
	}

	function regeneratePrintImage(PrintWidth, PrintHeight) {
		if (!MapServiceStarted) return; //Service not started or errored out, so exit

		intObjectsLoading = 0;

		MapPrint = MapServiceCurrent.clone();
		var PrintWidth = 615;
		var PrintHeight = 830;

		switch(document.getElementById('cmbPrintPageSize').value) {
			case "8.5x11":
				if (document.getElementById('cmbPrintOrientation').value == "Portrait") {
					PrintWidth = 615;
					PrintHeight = 830;
				} else {
					PrintWidth = 830;
					PrintHeight = 615;
				}
				break;

			case "8.5x11o":
				if (document.getElementById('cmbPrintOrientation').value == "Portrait") {
					PrintWidth = 760;
					PrintHeight = 885;
				} else {
					PrintWidth = 885;
					PrintHeight = 760;
				}
				break;
		}

		PrintWidth *= 2;
		PrintHeight *= 2;

		hideToolWindow();
		ensureInBounds(MapPrint);
		fixAspect(MapPrint, PrintWidth, PrintHeight);

		var xWindow = window.open("divPrintFrame.php?" + strCurrentMapSignature +
									"&width=" + PrintWidth +
									"&height=" + PrintHeight +
									"&bbox=" + MapPrint.getMinX() + "," + MapPrint.getMinY() + "," + MapPrint.getMaxX() + "," + MapPrint.getMaxY() +
									"&format=gif" +
									"&county=" + escape(MapVersionDisplay) +
									"&page=" + escape(MapPageName[0]) +
									"&title=" + escape(document.getElementById('txtPrintTitle').value) +
									"&ts=" + (new Date().getTime()), "SidwellMapsPrint", "resizable=1,menubar=1,toolbar=1,width=" + ((PrintWidth/2)+50) + ",height=450");
		xWindow.focus();

		hideLayer('divPrintFrame');
	}

	function regenerateOVImage() {
		if (isFrameVisibleSettings('divMapOVFrame')) {
			//Refresh OV Window Size
				resizeLayer('divMapOVFrame', 160, 160 * (MapServiceExtents.getHeight() / MapServiceExtents.getWidth()));

			//Reload Overview Map
			if (strLastOVSignature != strCurrentMapSignature) {
				getDIV('imgMapOVImage', false).src = 'ajax/ajaxGetMetadata.php?' + strCurrentMapSignature + '&width=' + (parseInt(getDIV('divMapOVFrame', true).width)-2) + '&height=' + (parseInt(getDIV('divMapOVFrame', true).height)-2) + '&bbox=' + MapServiceExtents.getMinX() + ',' + MapServiceExtents.getMinY() + ',' + MapServiceExtents.getMaxX() + ',' + MapServiceExtents.getMaxY() + '&format=jpg&ts=' + (new Date().getTime());
				intObjectsLoading++;
				showLoading();
				strLastOVSignature = strCurrentMapSignature;
			}
		}

		//Update Current Extents Bounds
		realOVWidth = getDIVWidth('divMapOVFrame') * (MapServiceCurrent.getWidth() / MapServiceExtents.getWidth());
		realOVHeight = getDIVHeight('divMapOVFrame') * (MapServiceCurrent.getHeight() / MapServiceExtents.getHeight());
		realMapToOVWidth = (MapServiceExtents.getWidth() / getDIVWidth('divMapOVFrame'));
		realMapToOVHeight = (MapServiceExtents.getHeight() / getDIVHeight('divMapOVFrame'));

		var extentsXRange = MapServiceExtents.X2 - MapServiceExtents.X1;
		var extentsYRange = MapServiceExtents.Y2 - MapServiceExtents.Y1;

		var realOVLeft = 0;
		var realOVTop = 0;

		if (MapServiceCurrent.X1 != MapServiceExtents.X1) {
			realOVLeft = (1-((MapServiceExtents.X2 - MapServiceCurrent.X1)/extentsXRange)) * getDIVWidth('divMapOVFrame');
		}
		if (MapServiceCurrent.Y1 != MapServiceExtents.Y1) {
			realOVTop = (1-((MapServiceExtents.Y2 - MapServiceCurrent.Y1)/extentsYRange)) * getDIVHeight('divMapOVFrame');
		}


		//FIX: IE and Borders!?!
		realOVWidthfix = 0;
		realOVHeightfix = 0;
		realOVTopfix = 0;
		if (isIE) {
				realOVWidthfix = 1;
				realOVHeightfix = 1;
		//		realOVTopfix = 1;
		}

		resizeLayer('MapOVViewExtents', realOVWidth + realOVWidthfix, realOVHeight + realOVHeightfix);
		moveLayer('MapOVViewExtents', realOVLeft, realOVTop + realOVTopfix);
	}

	function refreshMapLayers() {
		var strTemp = '';

		if (MapLayerDisplay.length > 0) {
			for(x = 0; x < MapLayerDisplay.length; x++) {
				strTemp += '<input type="checkbox" id="layerid' + MapLayerID[x] + '" onclick="validateMapLayer(' + x + ');" ' + (MapLayerVisible[x]?'CHECKED':'') + '><label for="layerid' + MapLayerID[x] + '">' + MapLayerDisplay[x] + '</label><br>';
			}
		} else {
			strTemp = 'No Layers';
		}

		updateContent('divLayers', strTemp);
	}

	function refreshMapGoto() {
		var strTemp = '<div style="overflow: auto; width: 148px; height: 120px; font-size: 10px;">';

		if (MapGotoDisplay.length > 0) {
			for(x = 0; x < MapGotoDisplay.length; x++) {
				if (MapGotoDisplay[x].indexOf("L") > 0) {
					strTemp += '&nbsp;&nbsp;';
				}

				if (MapGotoLink[x] == MapPageName[0]) {
					strTemp += '&nbsp;' + MapGotoDisplay[x] + '&nbsp;<br>';
				} else {
					strTemp += '&nbsp;<a href="javascript:setMapPage(\'' + MapGotoLink[x] + '\');">' + MapGotoDisplay[x] + '</a>&nbsp;<br>';
				}
			}
		} else {
			strTemp += 'No Nearby Pages';
		}

		strTemp += '</div>';

		updateContent('divGoto', strTemp);
	}

	function refreshMapGrid() {
		var strTemp = '<div style="overflow: none; width: 148px; height: 162px; font-size: 10px;">';

		if (MapVersionMapGrid.length > 0) {
			strTemp += '<img src="images/' + MapVersionMapGrid + '" border="0"/>';
		} else {
			strTemp += 'Map Grid Not Available';
		}

		strTemp += '</div>';

		updateContent('divMapGrid', strTemp);
	}

	function refreshMapHotSpots() {

		var myBodyFrame = document.getElementById("divBodyFrame");

		//Kill all HotStopDIV's Here
		var myHotspot = null;
		while((myHotspot = document.getElementById("divHotSpot")) != null)
		{
			myBodyFrame.removeChild(myHotspot);
		}

		if ((MapHotSpots == null) || (MapHotSpots == "")) return;

		for(var x = 0; x < MapHotSpots.length; x++) {
			var MapHotSpotsParts = MapHotSpots[x].split('|');

			var ScreenX = ((getDIVWidth('divMapFrame' + intNextMapDiv) * ((MapHotSpotsParts[1] - MapServiceCurrent.getMinX()) / MapServiceCurrent.getWidth())));
			var ScreenY = ((getDIVHeight('divMapFrame' + intNextMapDiv) * ((MapServiceCurrent.getMaxY()-MapHotSpotsParts[2]) / MapServiceCurrent.getHeight())));

			ScreenX += getDIVWidth("divMapFramePadding");

			//1) create a div on the fly
			//2) reproject div in to world coords

			var eDiv=document.createElement("DIV");
			eDiv.id = "divHotSpot";
			eDiv.style.position="absolute";
			eDiv.style.zIndex = 66;
			eDiv.style.left = (ScreenX - 9) + "px";
			eDiv.style.top = (ScreenY - 9) + "px";
			eDiv.style.width = "16px";
			eDiv.style.height = "16px";
			eDiv.style.cursor = "pointer";
			eDiv.gotopage = MapHotSpotsParts[4];
			eDiv.gotocaption = MapHotSpotsParts[0];

			eDiv.onmousemove = function(event) { if(zoomwindowmode < 1) { fireOVWindowMove(event); bMouseOverExtentsWindow = true; statusBar(this.gotocaption); } };
			eDiv.onmouseover = function(event) { if(zoomwindowmode < 1) hideToolWindow(event); };
			eDiv.onmousedown = function(event) { eval(this.gotopage); return false; };


			var eImg=document.createElement("IMG");
			eImg.src = "images/icon_star.gif";
			eImg.title = MapHotSpotsParts[0];

			eDiv.appendChild(eImg);
			myBodyFrame.appendChild(eDiv);
		}
	}


	function refreshProjectName() {
		updateContent('divProjectInfo', '<table cellspacing=2 cellpadding=0 border=0><tr align="right"><td><font size="4px" color="#FFFFFF">' + MapVersionDisplay + ' - ' + MapPageName[0] + '</td></tr></table>');
	}

	function refreshAlignmentLayer() {
		hideLayer('divAlignmentFrame');

		for(x = 0; x < MapLayerDisplay.length; x++) {
			switch(MapLayerType[x]) {
				case "PRIAERIAL":
				case "AERIAL":
				case "PRISOIL":
				case "SOIL":
					if (MapLayerVisible[x]) {
						showLayer('divAlignmentFrame');
						getAlignmentSettings();
						return;
					}
					break;
			}
		}
	}

	//Set Mouse Cords
	function setMouseCords(e) {
		mouseX = getMouseX(e) - getDIVLeft("divBodyFrame");
		mouseY = getMouseY(e) - getDIVTop("divBodyFrame");

		if (!MapServiceStarted) return;

		mapX = (MapServiceCurrent.getMinX() + (MapServiceCurrent.getWidth() * ((mouseX - getDIVLeft("divMapFrame" + intNextMapDiv)) / getDIVWidth('divMapFrame' + intNextMapDiv))));
		mapY = (MapServiceCurrent.getMaxY() - (MapServiceCurrent.getHeight() * ((mouseY - getDIVTop("divMapFrame" + intNextMapDiv)) / getDIVHeight('divMapFrame' + intNextMapDiv))));
	}

	//Set Mouse Cursor
	function setMouseCursor(cursor){
		getDIV("divBodyFrame",true).cursor = cursor;
	}

/* Mouse Event Trapping*/
	function getImageXY(e) {

		setMouseCords(e);

		if (!MapServiceStarted) return; //Service not started or errored out, so exit

		switch(selectedTool) {
			case "zoominwindow":
			case "zoomoutwindow":
				setMouseCursor("crosshair");
				if(zoomwindowmode==0) {
					hideToolWindow();
				} else if(zoomwindowmode==1) {
					MapWindow.X2 = mouseX;
					MapWindow.Y2 = mouseY;
					var zoomwinLeft = MapWindow.getMinX();
					var zoomwinTop = MapWindow.getMinY();
					var zoomwinWidth = MapWindow.getWidth();
					var zoomwinHeight = MapWindow.getHeight();

					if (zoomPreserveAspect) {
						zoomwinHeight = zoomwinWidth * (getDIVHeight('divMapFrame' + intNextMapDiv) / getDIVWidth('divMapFrame' + intNextMapDiv));
					}

					jg_doc.clear();
					jg_doc.setColor("#0000FF");
					jg_doc.setStroke(2);
					jg_doc.drawRect((zoomwinLeft - getDIVWidth("divMapFramePadding"))-1, zoomwinTop-1, zoomwinWidth-1, zoomwinHeight-1);
					jg_doc.paint();
				}
				break;

			case "pan":
			case "panalign":
				setMouseCursor("move");
				break;

			case "hotspotlink":
				var myHotSpotPreview = document.getElementById("divHotSpotPreview");

				if (myHotSpotPreview != null) {
					myHotSpotPreview.style.left = (mouseX - 9) + "px";
					myHotSpotPreview.style.top = (mouseY - 9) + "px";
				}

				break;

			default:
				setMouseCursor("default");
				break;
		}

		statusBar(trimPrecision(mapX, 0) + ', ' + trimPrecision(mapY, 0));
	}

	function getMouseUp(e){
		setMouseCords(e);

		mouseDown = false;

		if (!MapServiceStarted) return; //Service not started or errored out, so exit

		if(detectRightClick(e)) {
			switch(selectedTool){
				case "zoominwindow":
				case "zoomoutwindow":
					hideToolWindow();
					break;
			}
			return false; //Right Click was pressed, so exit
		}

		switch(selectedTool) {
			case "zoominwindow":
				MapSelection.X2 = mapX;
				MapSelection.Y2 = mapY;

				if(((MapWindow.getWidth() < 5)&&(MapWindow.getHeight() < 5)) || (zoomwindowmode == 0)){

					var tempExtents = new clsExtents();
					tempExtents.X1 = MapSelection.getWidthMid() - ((MapServiceCurrent.getWidth()/2) / ZoomMultiplier);
					tempExtents.Y1 = MapSelection.getHeightMid() + ((MapServiceCurrent.getHeight()/2) / ZoomMultiplier);
					tempExtents.X2 = MapSelection.getWidthMid() + ((MapServiceCurrent.getWidth()/2) / ZoomMultiplier);
					tempExtents.Y2 = MapSelection.getHeightMid() - ((MapServiceCurrent.getHeight()/2) / ZoomMultiplier);
					tempExtents.init = true;
					MapServiceCurrent = tempExtents.clone();

				} else if(zoomwindowmode==1) { //Set second point
					if (zoomPreserveAspect) {
						MapSelection.Y2 = (MapSelection.Y1 - (MapSelection.getWidth() * (getDIVHeight('divMapFrame' + intNextMapDiv) / getDIVWidth('divMapFrame' + intNextMapDiv))));
					}

					MapServiceCurrent = MapSelection.clone();
				} else {
					break;
				}

				//Execute zoom in window
				truncateHistory();
				regenerateMainImage();
				validateHistory();
				break;

			case "zoomoutwindow":
				MapSelection.X2 = mapX;
				MapSelection.Y2 = mapY;

				var tempExtents = new clsExtents();

				if(((MapWindow.getWidth() < 5)&&(MapWindow.getHeight() < 5)) || (zoomwindowmode == 0)){

					tempExtents.X1 = MapSelection.getWidthMid() - ((MapServiceCurrent.getWidth()/2) * ZoomMultiplier);
					tempExtents.Y1 = MapSelection.getHeightMid() + ((MapServiceCurrent.getHeight()/2) * ZoomMultiplier);
					tempExtents.X2 = MapSelection.getWidthMid() + ((MapServiceCurrent.getWidth()/2) * ZoomMultiplier);
					tempExtents.Y2 = MapSelection.getHeightMid() - ((MapServiceCurrent.getHeight()/2) * ZoomMultiplier);

				} else if(zoomwindowmode == 1) {
					if (zoomPreserveAspect) {
						MapSelection.Y2 = (MapSelection.Y1 - (MapSelection.getWidth() * (getDIVHeight('divMapFrame' + intNextMapDiv) / getDIVWidth('divMapFrame' + intNextMapDiv))));
					}

					tempExtents.X1 = MapSelection.getWidthMid() - ((MapServiceCurrent.getWidth()/2) + (MapServiceCurrent.getWidth() - MapSelection.getWidth()));
					tempExtents.Y1 = MapSelection.getHeightMid() + ((MapServiceCurrent.getHeight()/2) + (MapServiceCurrent.getHeight() - MapSelection.getHeight()));
					tempExtents.X2 = MapSelection.getWidthMid() + ((MapServiceCurrent.getWidth()/2) + (MapServiceCurrent.getWidth() - MapSelection.getWidth()));
					tempExtents.Y2 = MapSelection.getHeightMid() - ((MapServiceCurrent.getHeight()/2) + (MapServiceCurrent.getHeight() - MapSelection.getHeight()));

				} else {
					break;
				}

				//Execute zoom out window
				tempExtents.init = true;
				MapServiceCurrent = tempExtents.clone();

				truncateHistory();
				regenerateMainImage();
				validateHistory();
				break;

			case "hotspotlink":
				uploadHotspot("&mappage=" + MapPageName[0] +
							  "&x=" + mapX +
							  "&y=" + mapY +
							  "&caption=Jump to " + getDIV("txtHSLinkPage", false).value +
							  "&url=setMapPage('" + getDIV("txtHSLinkPage", false).value + "')");
				break;
		}
	}

	function getMouseDown(e){
		setMouseCords(e);

		mouseDown = true;

		if (!MapServiceStarted) return; //Service not started or errored out, so exit

		if(detectRightClick(e)) {
			switch(selectedTool){
				case "zoominwindow":
				case "zoomoutwindow":
					hideToolWindow();
					break;
			}
			return true; //Right Click was pressed, so exit
		}

		if(zoomwindowmode==0){
			switch(selectedTool){
				case "zoominwindow":
				case "zoomoutwindow":
					MapWindow.X1 = mouseX;
					MapWindow.Y1 = mouseY;
					MapWindow.X2 = mouseX;
					MapWindow.Y2 = mouseY;
					MapSelection.X1 = mapX;
					MapSelection.Y1 = mapY;
					MapSelection.X2 = mapX;
					MapSelection.Y2 = mapY;
					MapSelection.init = true;
					break;
			}
		}
	}

	function getDrag(e){
		if (!MapServiceStarted) return; //Service not started or errored out, so exit

		if(detectRightClick(e)) {
			switch(selectedTool){
				case "zoominwindow":
				case "zoomoutwindow":
					hideToolWindow();
					break;
			}
			return false; //Right Click was pressed, so exit
		}

		if(zoomwindowmode==0){
			switch(selectedTool){
				case "zoominwindow":
				case "zoomoutwindow":
					zoomwindowmode = 1;
					break;
			}
		}
		return false;
	}

	function executePan() {
		if (!MapServiceStarted) return; //Service not started or errored out, so exit

		var newposx = (findPosX(getDIV('divMapFrame' + (intNextMapDiv == 1?2:1)))) - getDIVWidth("divMapFramePadding");
		var newposy = (findPosY(getDIV('divMapFrame' + (intNextMapDiv == 1?2:1)))) - getDIVTop("divBodyFrame");
		var newscale = ((MapServiceCurrent.X1 - MapServiceCurrent.X2) / getDIVWidth('divMapFrame' + (intNextMapDiv == 1?2:1)));

		//var newposy = (findPosY(getDIV('divMapFrame' + (intNextMapDiv == 1?2:1))) + 1) - getDIVTop("divBodyFrame");

		if(selectedTool=="pan") {
			stopdrag();

			MapServiceCurrent.X1 += (newposx * newscale);
			MapServiceCurrent.X2 += (newposx * newscale);
			MapServiceCurrent.Y1 += (-newposy * newscale);
			MapServiceCurrent.Y2 += (-newposy * newscale);
			truncateHistory();

			setTool('refresh');
		}

		if(selectedTool=="panalign") {
			stopdrag();

			dblAlignmentX += (-newposx * newscale);
			dblAlignmentY += (-newposy * newscale);

			applyAlignmentSettings();
		}
	}

	function ensureInBounds(extent) {
		extent.X1 = parseFloat(extent.X1);
		extent.Y1 = parseFloat(extent.Y1);
		extent.X2 = parseFloat(extent.X2);
		extent.Y2 = parseFloat(extent.Y2);

		var newWidth = extent.getWidth();
		var newHeight = extent.getHeight();

		if (newWidth > MapServiceExtents.getWidth()) newWidth = MapServiceExtents.getWidth();
		if (newHeight > MapServiceExtents.getHeight()) newHeight = MapServiceExtents.getHeight();

		if (extent.Y2 > extent.Y1) {
			temp = extent.Y1;
			extent.Y1 = extent.Y2;
			extent.Y2 = temp;
		}

		if (extent.X1 > extent.X2) {
			temp = extent.X1;
			extent.X1 = extent.X2;
			extent.X2 = temp;
		}

		if (extent.X2 > MapServiceExtents.X2) {
			extent.X2 = MapServiceExtents.X2;
			extent.X1 = extent.X2 - newWidth;
		}

		if (extent.Y1 > MapServiceExtents.Y1) {
			extent.Y1 = MapServiceExtents.Y1;
			extent.Y2 = extent.Y1 - newHeight;
		}

		if (extent.getWidth() < 50) {
			extent.X1 = extent.getWidthMid() - 25;
			extent.X2 = extent.X1 + 50;
		}

		if (extent.getHeight() < 50) {
			extent.Y1 = extent.getHeightMid() - 25;
			extent.Y2 = extent.Y1 + 50;
		}
	}

	function fixAspect(extent, imagewidth, imageheight) {
		var realWidth = 0;
		var realHeight = 0;
	    var xw = extent.getWidth();
	    var yw = extent.getHeight();
	    var f1 = imagewidth / xw; //very small
	    var f2 = imageheight / yw; //very small

	    if(Math.abs(f1) == Math.abs(f2)) return;

		if(f1 < f2){
			//uses f1 (f1 is smaller)
			realHeight = (yw / (f1 / f2)) - yw;
		} else {
			//uses f2 (f2 is smaller)
			realWidth = (xw / (f2 / f1)) - xw;
		}

		extent.X1 -= (realWidth / 2);
		extent.Y1 += (realHeight / 2);
		extent.X2 += (realWidth / 2);
		extent.Y2 -= (realHeight / 2);
	}

	function buildMapRequestString() {
		strBuildMapRequestString = "versionid=" + MapVersionID + "&";
		var y = 1;

		for(var x = 0; x < MapLayerID.length; x++) {
			if (MapLayerVisible[x]) {
				strBuildMapRequestString += "typeid" + y + "=" + MapLayerID[x] + "&";
				strBuildMapRequestString += "mapname" + y + "=" + MapPageName[0] + "&";
				y++;
			}
		}

		if (strBuildMapRequestString.length > 0) strBuildMapRequestString = strBuildMapRequestString.substring(0, strBuildMapRequestString.length - 1);

		return strBuildMapRequestString;
	}

	function MapLayersVisible() {
		var y = 0;

		for(var x = 0; x < MapLayerID.length; x++) {
			if (MapLayerVisible[x]) {
				y++;
			}
		}

		return y;
	}

	function validateMapLayer(layerid) {
		if (MapLayerID.length == 1) {
			refreshMapLayers();
			return;
		}

		MapLayerVisible[layerid] = !MapLayerVisible[layerid];

		if (MapLayersVisible() == 0) {
			MapLayerVisible[layerid] = true;
			refreshMapLayers();
			return;
		}

		//Make sure if composites is checked that nothing else is:
		if ((MapLayerType[layerid] == "PRICOMPOSITE") && (MapLayerVisible[layerid])) {

			//Composite active, so disable all other layers
			for(var y = 0; y < MapLayerID.length; y++) {
				if ((MapLayerVisible[y]) && (y != layerid)) {
					MapLayerVisible[y] = false;
				}
			}

		} else {

			//This is not a composite layer, so disable all other composite layers
			for(var y = 0; y < MapLayerID.length; y++) {
				if (MapLayerType[y] == "PRICOMPOSITE") {
					MapLayerVisible[y] = false;
				}
			}

		}


		refreshMapLayers();

		if (isFrameVisibleSettings('divAlignmentFrame')) {
			refreshAlignmentLayer();
		}

		setTool('refresh');
	}

	function buildMapHistoryString() {
		var strBuildMapHistoryString = MapVersionID + "|" + MapVersionDisplay + "|" + MapVersionMapGrid + "|" + MapLayerID.length;

		for(var x = 0; x < MapLayerID.length; x++) {
			strBuildMapHistoryString += "|" + MapLayerID[x];
			strBuildMapHistoryString += "|" + MapLayerType[x];
			strBuildMapHistoryString += "|" + MapLayerDisplay[x];
			strBuildMapHistoryString += "|" + MapLayerVisible[x];
			strBuildMapHistoryString += "|" + MapPageName[0];
		}

		strBuildMapHistoryString += '|' + MapServiceCurrent.getMinX() + '|' + MapServiceCurrent.getMinY() + '|' + MapServiceCurrent.getMaxX() + '|' + MapServiceCurrent.getMaxY();

		return strBuildMapHistoryString;
	}

/* Tooltip Functions */
	var pTooltipTimer = null;
	var intTooltipX = 0;
	var intTooltipY = 0;
	var strTooltipID = "";

	function initTooltips() {
		for(var x = 0; x < arrTooltipInformation.length; x++) {
			if (!(document.getElementById(arrTooltipInformation[x][0]) == null)) {
				document.getElementById(arrTooltipInformation[x][0]).onmouseout = hideTooltip;
				document.getElementById(arrTooltipInformation[x][0]).onmousemove = movingTooltip;
			}
		}
	}

	function movingTooltip(e) {
		if (pTooltipTimer != null) clearTimeout(pTooltipTimer);

		intTooltipX = getMouseX(e);
		intTooltipY = getMouseY(e);
		strTooltipID = getFiringElement(e).id;

		for(var x = 0; x < arrTooltipInformation.length; x++) {
			if (arrTooltipInformation[x][0] == strTooltipID) {
				if (eval(arrTooltipInformation[x][2])) {
					statusBar(arrTooltipInformation[x][1].replace(/<br \/>/ig, " -- "));
					break;
				}
			}
		}

		if (isLayerVisiable('divFrameTooltip')) {
			showTooltip();
		} else {
			pTooltipTimer = setTimeout("showTooltip()", 600);
		}
	}

	function showTooltip() {
		for(var x = 0; x < arrTooltipInformation.length; x++) {
			if (arrTooltipInformation[x][0] == strTooltipID) {
				if (eval(arrTooltipInformation[x][2])) {
					updateContent('divFrameTooltip', '<table cellpadding="0" cellspacing="2" border="0"><tr><td><img src="images/icon_quickinfo.gif"></td><td nowrap class="ContextItem">' + arrTooltipInformation[x][1] + '</td></tr></table>');
					moveLayer('divFrameTooltip', intTooltipX, intTooltipY + 20);
					showLayer('divFrameTooltip');
					pTooltipTimer = null;
					return;
				}
			}
		}

		hideLayer('divFrameTooltip');
		pTooltipTimer = null;
	}

	function hideTooltip() {
		hideLayer('divFrameTooltip');
		if (pTooltipTimer != null) clearTimeout(pTooltipTimer);
		pTooltipTimer = null;
	}


/* Window Location and Visibility */
	function getFrameIndex(strFrameName) {
		for(var x = 0; x < arrWindowLocations.length; x++) {
			if (arrWindowLocations[x][0] == strFrameName) {
				return x;
			}
		}

		return -1;
	}

	function loadFramePositionSettings(strFrameName) {
		var newX = 0;
		var newY = 0;
		var intFrameIndex = -1;

		if ((intFrameIndex = getFrameIndex(strFrameName)) != -1) {
			if ((arrWindowLocations[intFrameIndex][5]) && (!arrWindowLocations[intFrameIndex][6])) {
				arrWindowLocations[intFrameIndex][1] = (getDIVWidth('divBodyFrame')/2) - (getDIVWidth(strFrameName)/2);
				arrWindowLocations[intFrameIndex][2] = (getDIVHeight('divBodyFrame')/2) - (getDIVHeight(strFrameName)/2);
			}

			newX = arrWindowLocations[intFrameIndex][1];
			newY = arrWindowLocations[intFrameIndex][2];
		}

		moveLayer(strFrameName, newX, newY);
	}

	function setFramePositionSettings(strFrameName, intX, intY) {
		var intFrameIndex = -1;

		if ((intFrameIndex = getFrameIndex(strFrameName)) != -1) {
			arrWindowLocations[intFrameIndex][6] = true;
			arrWindowLocations[intFrameIndex][1] = intX;
			arrWindowLocations[intFrameIndex][2] = intY;
		}
	}

	function getFramePositionSettings(strFrameName) {
		var intFrameIndex = -1;

		if ((intFrameIndex = getFrameIndex(strFrameName)) != -1) {
			return arrWindowLocations[intFrameIndex][1] + ',' + arrWindowLocations[intFrameIndex][2];
		}
	}

	function isFrameVisibleSettings(strFrameName) {
		var intFrameIndex = -1;

		if ((intFrameIndex = getFrameIndex(strFrameName)) != -1) {
			return arrWindowLocations[intFrameIndex][3];
		}

		return false;
	}

	/* isFrameAnchored - return true if frame cannot be moved or false if it can */
	function isFrameAnchored(strFrameName) {
		var intFrameIndex = -1;

		if ((intFrameIndex = getFrameIndex(strFrameName)) != -1) {
			return arrWindowLocations[intFrameIndex][4];
		}

		return true;
	}

	function setFrameVisibleSettings(strFrameName, boolValue, boolActivate) {
		var intFrameIndex = -1;

		if ((intFrameIndex = getFrameIndex(strFrameName)) != -1) {
			arrWindowLocations[intFrameIndex][3] = boolValue;

			if (boolActivate) {
				if (boolValue) {
					loadFramePositionSettings(strFrameName);
					showLayer(strFrameName);
				} else {
					hideLayer(strFrameName);
				}
			}
		}
	}


/* Frame Validators */
	function setFrameFocus(strFrameName) {
		if(getDIV(strFrameName, true).zIndex >= 200) return;
		if(getDIV(strFrameName, true).zIndex < 100) return;

		for(var x = 0; x < arrWindowLocations.length; x++) {
			if(getDIV(arrWindowLocations[x][0], true).zIndex == 200) {
				getDIV(arrWindowLocations[x][0], true).zIndex = getDIV(arrWindowLocations[x][0], false).oldzIndex;
				break;
			}
		}

		getDIV(strFrameName, false).oldzIndex = getDIV(strFrameName, true).zIndex;
		getDIV(strFrameName, true).zIndex = 200;
	}


	function keepAllFramesInBounds() {
		for(var x = 0; x < arrWindowLocations.length; x++) {
			keepFrameInBounds(arrWindowLocations[x][0]);
		}
	}

	function keepFrameInBounds(strFrameName){
		var dirty = false;

		if (isFrameAnchored(strFrameName)) return;

		if (!(document.getElementById(strFrameName) == null)) {
			if (((getDIVWidth(strFrameName)+2) + (findPosX(document.getElementById(strFrameName)))) > getWindowWidth()) {
				moveLayer(strFrameName, getWindowWidth() - (getDIVWidth(strFrameName)+2), null);
				dirty = true;
			} else if ((findPosX(document.getElementById(strFrameName))) < 0) {
				moveLayer(strFrameName, 0, null);
				dirty = true;
			}

			if (((getDIVHeight(strFrameName)+2) + (findPosY(document.getElementById(strFrameName)))) > getWindowHeight()) {
				moveLayer(strFrameName, null, getWindowHeight() - (getDIVHeight(strFrameName)+2));
				dirty = true;
			} else if ((findPosY(document.getElementById(strFrameName))) < 0) {
				moveLayer(strFrameName, null, 0);
				dirty = true;
			}
		}
	}


/* Drag and Drop Functions */
	function drag_drop(e){
		if (dragapproved){
			newoffsetx = getMouseX(e);
			newoffsety = getMouseY(e);

			moveLayer(movingObject, tempx+newoffsetx-offsetx, tempy+newoffsety-offsety);

			keepAllFramesInBounds();
		}
	}

	function initializedrag(div, e){
		if(detectRightClick(e)) return false; //Right Click was pressed, so exit

		if ((getFrameIndex(div)) != -1) {
			if (isFrameAnchored(div)) return;
		}

		movingObject = div;
		offsetx = getMouseX(e);
		offsety = getMouseY(e);

		tempx = parseInt(document.getElementById(movingObject).style.left);
		tempy = parseInt(document.getElementById(movingObject).style.top);

		if (getFrameIndex(movingObject) != -1) {
			if (document.getElementById(movingObject).oldfilter != "") {
				setOpacity(document.getElementById(movingObject), 100);
				document.getElementById(movingObject).style.filter = document.getElementById(movingObject).oldfilter + " " + document.getElementById(movingObject).style.filter;
			}
			document.getElementById(movingObject).oldfilter = document.getElementById(movingObject).style.filter;
			setOpacity(document.getElementById(movingObject), 70);
		}

		dragapproved = true;
	}

	function stopdrag(){
		dragapproved = false;
		if (movingObject != "") {
			setFramePositionSettings(movingObject, parseInt(document.getElementById(movingObject).style.left), parseInt(document.getElementById(movingObject).style.top));

			if (getFrameIndex(movingObject) != -1) {
				setOpacity(document.getElementById(movingObject), 100);
				document.getElementById(movingObject).style.filter = document.getElementById(movingObject).oldfilter + " " + document.getElementById(movingObject).style.filter;
				document.getElementById(movingObject).oldfilter = "";
			}

			movingObject = "";
		}
	}


	//Bind Event for movable content windows
	document.onmousemove = drag_drop;