// CivXplorer Code to support viewer layout and functionality
// Suzanne Fliege
// Red Horse Geographics, LLC

// user screen and window resolutions
var reloadTimer = 0;
var reloadInterval = 0;
// page form
var theCXForm = null;
// menu constants
var menuItemList = new Array();
var menuItemListCount = 0;
var menuSpace = "&nbsp;&nbsp;";
// layout
var borderWidth = 5;
var separatorWidth = 10;
var infopanelHeight = 60;
// sidebar
var sidebarResizeTimer = null;
var sidebarWidth = 250; // mobile should be 200
var sidebarHeight = 500;
var openSidebarHeight = 338;
var sidebarMode = 5;
var oldSidebarMode = 0;
var sidebarFloatCount = 5;
var sidebarFloatHeight = 20;
var moveSidebarCount = 0;
var moveSteps = 20;
// tools
var toolbarLeft = 0;
// map dimensions
var iTop = 0;
var iLeft = 0;
var iRight = 0;
var iBottom = 0;
var iWidth = 0;
var iHeight = 0;
// ov map dimensions
var i2Width = 0;
var i2Height = 0;
// command line startup
var startQueryString = "";

// *****************
//    page load
// *****************
// functions to run on CX start
function CXload() {
	// set page form variable
	theCXForm = document.getElementById('cxForm');
    // layout adjusted to browser size
    ResizePageControls(false);
	// load sidebars and start page
	loadCustomPages();
    // set background properties
    document.getElementById('cxBackground').onmouseover = "JavaScript:hideHelp();";
	// set default lat/long values
	theCXForm.LongDecString.value = defaultLong;
	theCXForm.LatDecString.value = defaultLat;
    // activate starting sidebar
    showSidebar(startingSidebar);
	// reset map dropdown
	theCXForm.cxPrintMapType.selectedIndex = 0;
	// start loading javascript	
	var cxScripts = document.createElement('script');
	cxScripts.setAttribute("type","text/javascript");
	cxScripts.setAttribute("src","javascript/cxCommon.js");
	document.getElementsByTagName('head')[0].appendChild(cxScripts);
}

// update content in a layer
function updateContent(name, content) {
	document.getElementById(name).innerHTML = content;
}

// interval to check if previous map redraw is complete before resizing
function checkOkToResize() {
	if (!waitForResponse) {
		clearInterval(reloadInterval);
		sendMapRequest();
	}
	return false;
}

// resize page controls when map/sidebar/browser area resizes
function ResizePageControls(doRedraw) {
	// window size
    GetPageDimensions();
	// background
	document.getElementById('cxBackground').style.height = winHeight + "px";
	document.getElementById('cxBackground').style.width = winWidth + "px";
	// sidebar elements
	document.getElementById('cxSidebarTop').style.top = separatorWidth + "px";
	document.getElementById('cxSidebarTop').style.left = separatorWidth + "px";
	document.getElementById('cxSidebarBottom').style.top = (separatorWidth+32+sidebarHeight) + "px";
	document.getElementById('cxSidebarBottom').style.left = separatorWidth + "px";
	document.getElementById('cxSidebarLeft').style.height = (sidebarHeight+27) + "px";
	document.getElementById('cxSidebarLeft').style.left = separatorWidth + "px";
	document.getElementById('cxSidebarRight').style.height = (sidebarHeight+27) + "px";
	document.getElementById('cxSidebarRight').style.left = (separatorWidth+borderWidth+sidebarWidth) + "px";
	document.getElementById('cxSidebarBackground').style.top = (separatorWidth+32) + "px";
	document.getElementById('cxSidebarBackground').style.left = (separatorWidth+borderWidth) + "px";
	document.getElementById('cxSidebarBackground').style.height = sidebarHeight + "px";
    // move sidebar headers below the open one
    for (var i=sidebarMode;i<sidebarFloatCount;i++){
        divName = "cxSidebar" + (i+1) + "Header";
        document.getElementById(divName).style.top = (separatorWidth+32+sidebarFloatHeight*i+openSidebarHeight) + "px";
    }
    // adjust height of open sidebar
    var divName = "cxSidebar" + sidebarMode;
    document.getElementById(divName).style.height = openSidebarHeight + "px";
	// map & footer border elements
	document.getElementById('cxMapBorderUL').style.top = separatorWidth + "px";
	document.getElementById('cxMapBorderUL').style.left = (sidebarWidth+separatorWidth*2+borderWidth*2) + "px";
	document.getElementById('cxMapBorderTop').style.top = separatorWidth + "px";
	document.getElementById('cxMapBorderTop').style.left = (sidebarWidth+separatorWidth*2+borderWidth*3) + "px";
	document.getElementById('cxMapBorderTop').style.width = mapWidth + "px";
	document.getElementById('cxMapBorderUR').style.top = separatorWidth + "px";
	document.getElementById('cxMapBorderUR').style.left = (sidebarWidth+separatorWidth*2+borderWidth*3+mapWidth) + "px";
	document.getElementById('cxMapBorderLeft').style.top = (separatorWidth+borderWidth) + "px";
	document.getElementById('cxMapBorderLeft').style.left = (sidebarWidth+separatorWidth*2+borderWidth*2) + "px";
	document.getElementById('cxMapBorderLeft').style.height = (mapHeight+infopanelHeight+borderWidth) + "px";
	document.getElementById('cxMapBorderRight').style.top = (separatorWidth+borderWidth) + "px";
	document.getElementById('cxMapBorderRight').style.left = (sidebarWidth+separatorWidth*2+borderWidth*3+mapWidth) + "px";
	document.getElementById('cxMapBorderRight').style.height = (mapHeight+infopanelHeight+borderWidth) + "px";
	document.getElementById('cxMapBorderMiddle').style.top = (separatorWidth+borderWidth+infopanelHeight) + "px";
	document.getElementById('cxMapBorderMiddle').style.left = (sidebarWidth+separatorWidth*2+borderWidth*3) + "px";
	document.getElementById('cxMapBorderMiddle').style.width = mapWidth + "px";
	document.getElementById('cxMapBorderLL').style.top = (separatorWidth+borderWidth*2+infopanelHeight+mapHeight) + "px";
	document.getElementById('cxMapBorderLL').style.left = (sidebarWidth+separatorWidth*2+borderWidth*2) + "px";
	document.getElementById('cxMapBorderBottom').style.top = (separatorWidth+borderWidth*2+infopanelHeight+mapHeight) + "px";
	document.getElementById('cxMapBorderBottom').style.left = (sidebarWidth+separatorWidth*2+borderWidth*3) + "px";
	document.getElementById('cxMapBorderBottom').style.width = mapWidth + "px";
	document.getElementById('cxMapBorderLR').style.top = (separatorWidth+borderWidth*2+infopanelHeight+mapHeight) + "px";
	document.getElementById('cxMapBorderLR').style.left = (sidebarWidth+separatorWidth*2+borderWidth*3+mapWidth) + "px";
	// info panel
	document.getElementById('cxInfoPanel').style.top = (separatorWidth+borderWidth) + "px";
	document.getElementById('cxInfoPanel').style.left = (sidebarWidth+separatorWidth*2+borderWidth*3) + "px";
	document.getElementById('cxInfoPanel').style.width = mapWidth + "px";
	document.getElementById('cxInfoPanel').style.height = infopanelHeight + "px";
	// map
	document.getElementById('theMap').style.top = (separatorWidth+borderWidth*2+infopanelHeight) + "px";
	document.getElementById('theMap').style.left = (sidebarWidth+separatorWidth*2+borderWidth*3) + "px";
	document.getElementById('theMap').style.width = mapWidth + "px";
	document.getElementById('theMap').style.height = mapHeight + "px";
	// zoom
	document.getElementById('theZoomBox').style.top = (separatorWidth+borderWidth*2+infopanelHeight) + "px";
	document.getElementById('theZoomBox').style.left = (sidebarWidth+separatorWidth*2+borderWidth*3) + "px";
	document.getElementById('theZoomBox').style.width = mapWidth + "px";
	document.getElementById('theZoomBox').style.height = mapHeight + "px";
	// polyline
	document.getElementById('thePolyline').style.top = (separatorWidth+borderWidth*2+infopanelHeight) + "px";
	document.getElementById('thePolyline').style.left = (sidebarWidth+separatorWidth*2+borderWidth*3) + "px";
	document.getElementById('thePolyline').style.width = mapWidth + "px";
	document.getElementById('thePolyline').style.height = mapHeight + "px";
	// rubberband
	document.getElementById('theRubberband').style.top = (separatorWidth+borderWidth*2+infopanelHeight) + "px";
	document.getElementById('theRubberband').style.left = (sidebarWidth+separatorWidth*2+borderWidth*3) + "px";
	document.getElementById('theRubberband').style.width = mapWidth + "px";
	document.getElementById('theRubberband').style.height = mapHeight + "px";
	// mouse capture
	document.getElementById('mouseCapture').style.top = (separatorWidth+borderWidth*2+infopanelHeight) + "px";
	document.getElementById('mouseCapture').style.left = (sidebarWidth+separatorWidth*2+borderWidth*3) + "px";
	document.getElementById('mouseCapture').style.width = mapWidth + "px";	
	document.getElementById('mouseCapture').style.height = mapHeight + "px";	
	document.getElementById('mouseCaptureImage').style.width = mapWidth + "px";	
	document.getElementById('mouseCaptureImage').style.height = mapHeight + "px";	
	// map tools
	toolbarLeft = sidebarWidth+separatorWidth*2+borderWidth*3;
	document.getElementById('cxMapToolsVertical').style.left = toolbarLeft + "px";
	document.getElementById('cxMapToolsVertical').style.top = (separatorWidth+borderWidth*2+infopanelHeight) + "px";
	document.getElementById('cxMapToolsVertical').style.visibility = "visible";
	document.getElementById('cxMapToolsDial').style.left = toolbarLeft + "px";
	document.getElementById('cxMapToolsDial').style.top = (separatorWidth+borderWidth*2+infopanelHeight) + "px";
	document.getElementById('cxMapToolsDial').style.visibility = "visible";
	document.getElementById('cxMapToolsHorizontal').style.left = toolbarLeft + "px";
	document.getElementById('cxMapToolsHorizontal').style.top = (separatorWidth+borderWidth*2+infopanelHeight) + "px";
	document.getElementById('cxMapToolsHorizontal').style.visibility = "visible";
	// locator
	updateOVMap();
	// help
	document.getElementById('cxHelp').style.top = (separatorWidth+borderWidth*2+infopanelHeight) + "px";
	document.getElementById('cxHelp').style.left = (sidebarWidth+separatorWidth*2+borderWidth*3) + "px";
	document.getElementById('cxHelp').style.height = mapHeight + "px";
	document.getElementById('cxHelp').style.width = mapWidth + "px";
	// reporting
	document.getElementById('cxPopupMask').style.top = (separatorWidth+borderWidth) + "px";
	document.getElementById('cxPopupMask').style.left = (sidebarWidth+separatorWidth*2+borderWidth*3) + "px";
	document.getElementById('cxPopupMask').style.height = (mapHeight+infopanelHeight+borderWidth) + "px";
	document.getElementById('cxPopupMask').style.width = mapWidth + "px";
	document.getElementById('cxPopup').style.top = (separatorWidth+borderWidth+25) + "px";
	document.getElementById('cxPopup').style.left = (sidebarWidth+borderWidth*3+separatorWidth*2+5) + "px";
	document.getElementById('cxPopup').style.height = (mapHeight+infopanelHeight+borderWidth-30) + "px";
	document.getElementById('cxPopup').style.width = (mapWidth-10) + "px";
    // entry page (disclaimer)
	document.getElementById('cxEntryPage').style.height = winHeight + "px";
	document.getElementById('cxEntryPage').style.width = winWidth + "px";
	// spinner
	document.getElementById('cxProtectorSpinner').style.top = separatorWidth + "px";
	document.getElementById('cxProtectorSpinner').style.left = (sidebarWidth+separatorWidth*2+borderWidth*2) + "px";
	document.getElementById('cxProtectorSpinner').style.height = (borderWidth*3+mapHeight+infopanelHeight) + "px";
	document.getElementById('cxProtectorSpinner').style.width = (mapWidth+borderWidth*2) + "px";
	// timeout before map redraw
	if (doRedraw) {
		window.clearTimeout(reloadTimer);
		reloadTimer = window.setTimeout("if (waitForResponse) { reloadInterval=setInterval('checkOkToResize();',500) } else { sendMapRequest(); }",2000);
	}
}

// close popup window
function closeEntryPageDisplay() {
	updateContent("cxEntryPage","");
    document.getElementById('cxEntryPage').style.display = "none";
    return false; 
}

// **************************************
// returns browser size information
// **************************************
function GetPageDimensions() {
	// window size
	if (!window.innerWidth) { //IE
		if (!(document.documentElement.clientWidth == 0)) { //strict mode
			winWidth = document.documentElement.clientWidth;
			winHeight = document.documentElement.clientHeight;
		} else { //quirks mode
			winWidth = document.body.clientWidth;
			winHeight = document.body.clientHeight;
		}
	} else { //w3c
		winWidth = window.innerWidth;
		winHeight = window.innerHeight;
	}
	// component sizes
	sidebarHeight = winHeight - separatorWidth*2 - 32 - borderWidth;
	openSidebarHeight = sidebarHeight - (sidebarFloatCount*sidebarFloatHeight);
	mapHeight = winHeight - infopanelHeight - separatorWidth*2 - borderWidth*3;
	mapWidth = winWidth - (sidebarWidth+borderWidth*2) - separatorWidth*3 - borderWidth*2;
	// map interaction variables
	iWidth = mapWidth;
	iHeight = mapHeight;
	iTop = (separatorWidth+borderWidth*2+infopanelHeight);
	iLeft = (sidebarWidth+separatorWidth*2+borderWidth*3);
	iRight = iLeft + iWidth;
	iBottom = iTop + iHeight;
	return false;
}

// **************************************
//   page protection controls
// **************************************
// turn protectors on
function ProtectorOn() {
    document.getElementById('cxInfoPanel').style.visibility = 'hidden';
    document.getElementById('cxProtector').style.visibility = 'visible';
    document.getElementById('cxProtectorSpinner').style.visibility = 'visible';
    return false;
}
// turn protectors off
function ProtectorOff() {
    document.getElementById('cxProtector').style.visibility = 'hidden';
    document.getElementById('cxProtectorSpinner').style.visibility = 'hidden';
    document.getElementById('cxInfoPanel').style.visibility = 'visible';
    return false;
}

// **************************************
//   popup page controls
// **************************************
// open popup window with specified URL
function openPopupDisplay(theURL) {
    // hide protector overlay
    ProtectorOff();
    clickFunction("nothing");
    if (theURL.toUpperCase().indexOf("ERROR") == -1) {
        var theString = '<iframe id="InlinePrintFrame" name="InlinePrintFrame" width="100%" height="100%" scrolling="auto" frameborder="no" src="'+theURL+'" border="0"></iframe>';
        updateContent('cxPopup',theString);
        document.getElementById('cxPopup').style.overflow = "hidden";
        document.getElementById('cxPopupMask').style.display = "inline";  
        document.getElementById('cxPopup').style.display = "inline";
    } else {
        alert(theURL);
    }
}

// close popup window
function closePopupDisplay() {
    document.getElementById('cxPopupMask').style.display = "none";  
    document.getElementById('cxPopup').style.display = "none";
    return false; 
}

// **************************************
//   help
// **************************************
// turn help popup on/off
function toggleHelp() {
	hideHelp();
	if (showUserHelp) {
		showUserHelp = false;
		theCXForm.cxShowHelpNotes.checked = false;
	} else {
		showUserHelp = true;
		theCXForm.cxShowHelpNotes.checked = true;
	}
}

// **************************************
//   OV map
// **************************************
// toggle ov map visibility
function toggleOVMap() {
    moveOVMapCount = 0;
	hideHelp();
    if (showOverviewMap) {
		//theCXForm.cxShowOVmap.checked = false;
	    showOverviewMap = false;
    } else {
		//theCXForm.cxShowOVmap.checked = true;
	    showOverviewMap = true;
    }
	updateOVMap();
}

// resize ov map based on extent height/width ratio
function resizeOVMap() {
	// overview image size
	ovHeight = parseInt(limitHeight/limitWidth*ovWidth);
	i2Width = ovWidth;
	i2Height = ovHeight;
	updateOVMap();
	return false;	
}

// ov map resizing/visibility
function updateOVMap() {
	if (showOverviewMap) {
	    document.getElementById('cxOVMapBorder').style.height = (ovHeight+borderWidth) + "px";
	    document.getElementById('cxOVMapBorder').style.width = (ovWidth+borderWidth) + "px";
	    document.getElementById('cxOVMapBorder').style.top = (separatorWidth+borderWidth*2+infopanelHeight) + "px";	
	    document.getElementById('cxOVMapBorder').style.left = (winWidth-separatorWidth-ovWidth-borderWidth*2) + "px";
		document.getElementById('cxOVMapBorder').style.visibility = "visible";
	    document.getElementById('theOVMap').style.height = ovHeight + "px";
	    document.getElementById('theOVMap').style.width = ovWidth + "px";
	    document.getElementById('theOVMap').style.top = (separatorWidth+borderWidth*2+infopanelHeight) + "px";	
	    document.getElementById('theOVMap').style.left = (winWidth-separatorWidth-ovWidth-borderWidth) + "px";
		document.getElementById('theOVMap').style.visibility = "visible";
	    document.getElementById('theOVMapZoomBox').style.height = ovHeight + "px";
	    document.getElementById('theOVMapZoomBox').style.width = ovWidth + "px";
	    document.getElementById('theOVMapZoomBox').style.top = (separatorWidth+borderWidth*2+infopanelHeight) + "px";	
	    document.getElementById('theOVMapZoomBox').style.left = (winWidth-separatorWidth-ovWidth-borderWidth) + "px";
	    document.getElementById('theOVMapZoomBox').style.visibility = "visible";
	    document.getElementById('theOVMapArrow').style.top = (separatorWidth+borderWidth*3+infopanelHeight+ovHeight-16) + "px";	
	    document.getElementById('theOVMapArrow').style.left = (winWidth-separatorWidth-borderWidth*2-ovWidth) + "px";
	    document.getElementById('theOVMapArrow').className = "ovMapClose";
	} else {
		document.getElementById('cxOVMapBorder').style.visibility = "hidden";
		document.getElementById('theOVMap').style.visibility = "hidden";
	    document.getElementById('theOVMapZoomBox').style.visibility = "hidden";
        document.getElementById('theOVMapArrow').style.top = (separatorWidth+borderWidth*2+infopanelHeight) + "px";	
        document.getElementById('theOVMapArrow').style.left = (winWidth-separatorWidth-borderWidth-16) + "px";
	    document.getElementById('theOVMapArrow').className = "ovMapOpen";
    }
	return false;
}

// **************************************
//   sidebar controls
// **************************************
function resetSidebarTitles() {
    var titleString = "";
    for (var i=0;i<=(sidebarFloatCount+1);i++) {
        titleString = "cxSidebar" + i + "Title";
	    if (document.getElementById(titleString) != null) document.getElementById(titleString).className = "sidebarTextNormal";
	}
	return false;
}

function showSidebar(theSidebar) {
    if (sidebarMode != theSidebar) {
        resetSidebarTitles();
	    // sidebars
	    oldSidebarMode = parseInt(sidebarMode);
	    sidebarMode = parseInt(theSidebar);
        // change sidebar visibility for move
        document.getElementById(("cxSidebar" + oldSidebarMode)).style.visibility = "hidden";
	    // move intervals
	    moveSidebarCount = 0;
        clearInterval(sidebarResizeTimer);
        sidebarResizeTimer = setInterval("moveSidebars()",10);
    }
	return false;
}

function moveSidebars() {
    moveSidebarCount += 1;
    if (moveSidebarCount < moveSteps) {
        // new move interval
	    var moveInt = parseInt(openSidebarHeight/moveSteps);
        var moveSidebarHeight = moveInt*moveSidebarCount;
        var divName = "";
	    // move the sidebars
	    if (sidebarMode > oldSidebarMode) { // moving the sidebars up
	        // move headers
    	    for (var i=oldSidebarMode;i<sidebarMode;i++){
                divName = "cxSidebar" + (i+1) + "Header";
                document.getElementById(divName).style.top = (separatorWidth+32+sidebarFloatHeight*i+openSidebarHeight-moveSidebarHeight) + "px";
    	    }
    	    // move & resize sidebars
            divName = "cxSidebar" + oldSidebarMode;
            document.getElementById(divName).style.height = (openSidebarHeight-moveSidebarHeight) + "px";
            divName = "cxSidebar" + sidebarMode;
            document.getElementById(divName).style.top = (separatorWidth+32+sidebarFloatHeight*sidebarMode+openSidebarHeight-moveSidebarHeight) + "px";
            document.getElementById(divName).style.height = moveSidebarHeight + "px";
	    } else { // moving the sidebars down
	        // move headers
    	    for (var i=oldSidebarMode;i>sidebarMode;i--){
    	        if (i != 0) {
                    divName = "cxSidebar" + i + "Header";
                    document.getElementById(divName).style.top = (separatorWidth+32+sidebarFloatHeight*(i-1)+moveSidebarHeight) + "px";
                }
    	    }
    	    // move & resize sidebars
            divName = "cxSidebar" + oldSidebarMode;
            document.getElementById(divName).style.top = (separatorWidth+32+sidebarFloatHeight*oldSidebarMode+moveSidebarHeight) + "px";
            document.getElementById(divName).style.height = (openSidebarHeight-moveSidebarHeight) + "px";
            divName = "cxSidebar" + sidebarMode;
            document.getElementById(divName).style.top = (separatorWidth+32+sidebarFloatHeight*sidebarMode) + "px";
            document.getElementById(divName).style.height = moveSidebarHeight + "px";
	    }
    } else {
	    // move the sidebars
	    if (sidebarMode > oldSidebarMode) { // moving the sidebars up
	        // move headers
    	    for (var i=oldSidebarMode;i<sidebarMode;i++){
                divName = "cxSidebar" + (i+1) + "Header";
                document.getElementById(divName).style.top = (separatorWidth+32+sidebarFloatHeight*i) + "px";
    	    }
    	    // move & resize sidebars
            divName = "cxSidebar" + oldSidebarMode;
            document.getElementById(divName).style.height = "0px";
            //document.getElementById(divName).style.visibility = "hidden";
            divName = "cxSidebar" + sidebarMode;
            document.getElementById(divName).style.top = (separatorWidth+32+sidebarFloatHeight*sidebarMode) + "px";
            document.getElementById(divName).style.height = openSidebarHeight + "px";
            document.getElementById(divName).style.visibility = "visible";
	    } else { // moving the sidebars down
	        // move headers
    	    for (var i=oldSidebarMode;i>sidebarMode;i--){
    	        if (i != 0) {
                    divName = "cxSidebar" + i + "Header";
                    document.getElementById(divName).style.top = (separatorWidth+32+sidebarFloatHeight*(i-1)+openSidebarHeight) + "px";
                }
    	    }
    	    // move & resize sidebars
            divName = "cxSidebar" + oldSidebarMode;
            document.getElementById(divName).style.top = (separatorWidth+32+sidebarFloatHeight*oldSidebarMode+openSidebarHeight) + "px";
            document.getElementById(divName).style.height = "0px";
            //document.getElementById(divName).style.visibility = "hidden";
            divName = "cxSidebar" + sidebarMode;
            document.getElementById(divName).style.top = (separatorWidth+32+sidebarFloatHeight*sidebarMode) + "px";
            document.getElementById(divName).style.height = openSidebarHeight + "px";
            document.getElementById(divName).style.visibility = "visible";
	    }
        document.getElementById(("cxSidebar" + sidebarMode + "Title")).className = "sidebarTextSelected";
        clearInterval(sidebarResizeTimer);
    }
}

// reset sidebars
function resetSidebars() {
	resetSidebarTitles();
	// sidebars
	oldSidebarMode = parseInt(sidebarMode);
	sidebarMode = 0;
	// change sidebar visibility for move
	document.getElementById(("cxSidebar" + oldSidebarMode)).style.visibility = "hidden";
	// move headers
	for (var i=1;i<sidebarFloatCount;i++){
		divName = "cxSidebar" + (i+1) + "Header";
		document.getElementById(divName).style.top = (separatorWidth+32+sidebarFloatHeight*i) + "px";
	}
	// move & resize sidebars
	divName = "cxSidebar" + oldSidebarMode;
	document.getElementById(divName).style.height = "0px";
	//document.getElementById(divName).style.visibility = "hidden";
	divName = "cxSidebar" + sidebarMode;
	document.getElementById(divName).style.top = (separatorWidth+32+sidebarFloatHeight*sidebarMode) + "px";
	document.getElementById(divName).style.height = openSidebarHeight + "px";
	document.getElementById(divName).style.visibility = "visible";
	document.getElementById(("cxSidebar" + sidebarMode + "Title")).className = "sidebarTextSelected";
	return false;
}

// *************************************
//    toolbar support
// *************************************
// highlight maptool
function highlightMapTool(maptoolName) {
	// highlight toolbars
	highlightMapToolbars();
	// highlight tool
	resetMapTools();
	document.getElementById(maptoolName).className = "toolbarButtonHighlight";
	if (showUserHelp) showHelp(maptoolName);
	return false;
}

// reset maptool highlights
function resetMapTools() {
    if (toolModeText == "zoomin") { document.getElementById("cxOnMap-ZoomIn").className='toolbarButtonSelected'; } else { document.getElementById("cxOnMap-ZoomIn").className='toolbarButtonNormal'; }
    if (toolModeText == "zoomout") { document.getElementById("cxOnMap-ZoomOut").className='toolbarButtonSelected'; } else { document.getElementById("cxOnMap-ZoomOut").className='toolbarButtonNormal'; }
    document.getElementById("cxOnMap-ZoomAll").className='toolbarButtonNormal';
    if (toolModeText == "pan") { document.getElementById("cxOnMap-Recenter").className='toolbarButtonSelected'; } else { document.getElementById("cxOnMap-Recenter").className='toolbarButtonNormal'; }
    document.getElementById("cxOnMap-ZoomPrevious").className='toolbarButtonNormal';
    if (toolModeText == "identify") { document.getElementById("cxOnMap-Identify").className='toolbarButtonSelected'; } else { document.getElementById("cxOnMap-Identify").className='toolbarButtonNormal'; }
    if (toolModeText == "select") { document.getElementById("cxOnMap-Select").className='toolbarButtonSelected'; } else { document.getElementById("cxOnMap-Select").className='toolbarButtonNormal'; }
    if (toolModeText == "measure") { document.getElementById("cxOnMap-Measure").className='toolbarButtonSelected'; } else { document.getElementById("cxOnMap-Measure").className='toolbarButtonNormal'; }
    if (toolModeText == "mapgraphic") { document.getElementById("cxOnMap-Graphics").className='toolbarButtonSelected'; } else { document.getElementById("cxOnMap-Graphics").className='toolbarButtonNormal'; }
    if (toolModeText == "mapsettings") { document.getElementById("cxOnMap-Settings").className='toolbarButtonSelected'; } else { document.getElementById("cxOnMap-Settings").className='toolbarButtonNormal'; }
	return false;
}

// highlight maptoolbar set
function highlightMapToolbars() {
	document.getElementById('cxMapToolsVertical').className = "toolbarHighlight";
	document.getElementById('cxMapToolsHorizontal').className = "toolbarHighlight";
	return false;
}

// reset entire maptoolbar set
function resetMapToolbars() {
	resetMapTools();
	document.getElementById('cxMapToolsVertical').className = "toolbarNormal";
	document.getElementById('cxMapToolsHorizontal').className = "toolbarNormal";
	return false;
}

// *************************************
//    printmap support
// *************************************
// set dialog visibility based on printmap type
function SetPrintMapDialog(theType) {
	document.getElementById('cxPrintMapPDF').style.display = "none";
	document.getElementById('cxPrintMapImage').style.display = "none";
	switch(theType) {
	case "nothing":
		window.status="";
		break
	case "pdf":
		document.getElementById('cxPrintMapPDF').style.display = "inline";
		break
	case "image":
	    document.getElementById('cxPrintMapImage').style.display = "inline";
		break
	default:
		//alert("Function Not Available");
	}
}

// *************************************
//    commandline startup support
// *************************************
// search parameters from URL
function getCXCommandLineParams(cmdString) {
	// Parse out from URL querystring parameters to pass to the viewer.
	// Syntax:
	// ActiveLayerID=layerID			ID of layer to be active as named in AXL
	// Query=queryExpression			query expression to be send on load - expression must be escaped in URL
	// LYRVIS=layerID					ID of layer (or group) to turn on at startup
	// OVRLAY=layerID					ID of overlay layer (or group) to turn on at startup
	var cmdString2 = unescape(cmdString.toUpperCase());
	var endpos = 0;
	var startpos = 0;
	// query layer
	var pos = cmdString2.indexOf("ACTIVELAYERID=");
	if (pos != -1) {
		startpos = pos + 14;
		endpos = cmdString.indexOf("&",startpos);
		if (endpos==-1) endpos = cmdString.length;
		var actlyr = cmdString.substring(startpos,endpos);
		setActiveLayer(actlyr,false,false);
	}	
	// query string
	pos = cmdString2.indexOf("QUERY=");
	startQueryString = "";
	if ((pos != -1) && (ActiveLayerIndex != -1)) {
		startpos = pos + 6;
		endpos = cmdString.indexOf("&",startpos);
		if (endpos==-1) endpos = cmdString.length;
		var escQuery = cmdString.substring(startpos,endpos);
		// expected format of query is query=fieldname%3D'fieldvalue'%20or%20fieldname%3D'fieldvalue'.....  (%3D="=",%22=""",%20=" ", use ' is ok...no code)
		escQuery = replacePlus(escQuery);
		startQueryString = unescape(escQuery);
	}	
	// set layer visibility
	pos = cmdString2.indexOf("LYRVIS=");
	if (pos!=-1) {
		startpos = pos + 7;
		endpos = cmdString.indexOf("&",startpos);
		if (endpos==-1) endpos = cmdString.length;
		var tempString = unescape(cmdString.substring(startpos,endpos));
		var tempArray = tempString.split(":");
		for (var k=0;k<tempArray.length;k++) {
			for (var i=0;i<tocOptionalList.length;i++) {
				if (tempArray[k] == tocOptionalList[i][1]) {
					tocOptionalList[i][3] = "true";
					if (isNaN(tocOptionalList[i][2])) {  // is a group
						TurnGroupOptionalOn(tempArray[k]);
					} else { // is a single layer
						LayerVisible[tocOptionalList[i][2]] = "true";
					}
				}
			}
		}
	}
	// set map type (overlays)
	pos = cmdString2.indexOf("OVRLAY=");
	if (pos!=-1) {
		startpos = pos + 7;
		endpos = cmdString.indexOf("&",startpos);
		if (endpos==-1) endpos = cmdString.length;
		var tempString = unescape(cmdString.substring(startpos,endpos));
		for (var i=0;i<tocOverlayList.length;i++) {
			if (tempString == tocOverlayList[i][1]) {
				if (isNaN(tocOverlayList[i][3])) {  // is a group
					TurnGroupOverlayOn(tempString);
				} else { // is a single overlay layer
					LayerVisible[tocOverlayList[i][3]] = "true";
					tocOverlayCurrent = tempString;
					tocOverlayCurrentType = LayerTypeUser[tocOverlayList[i][3]];
				}
			}
		}
	}
	// refresh layer TOC
	createTOC();
	// write starting query string or just draw map
	if (startQueryString != "") {
		var theString = writeQueryXML(startQueryString);
		zoomToSelected = true;
		webParams = "";
		sendToServer(imsQueryURL,theString,8);
	} else {
		sendMapRequest();
	}
}


