﻿
// Javascript file for Web Mapping Application

var reloadTimer;
var webMapAppLeftPanelWidth = 262;
var webMapAppToggleWidth = 10;
var webMapAppTopBannerHeight = 80;
var newLoad = false;
var webMapAppCloseCallback = "";
var webMapAppCopyrightCallback = "";
var webMapAppMoveFunction = null;
var webMapAppMapDisplay = null;
var webMapAppPanelDisplay = null;
var webMapAppPanelDisplayCell = null;
var webMapAppPanelDisplayTableCell = null;
var webMapAppPanelScrollDiv = null;
var webMapAppToggleDisplay = null;
var webMapAppSpacerDiv = null;
var webMapAppPanelBottomSlider = null;
var webMapAppScaleBar = null;
var webMapAppNavBar = null;
var webMapAppCopyrightText = null;
var webMapAppWindowWidth = 500;
var webMapAppLeftOffsetX = 0;
var webMapAppRightOffsetX = 0
var webMapAppDefaultMinDockWidth = 125;
var webMapAppMinDockWidth = webMapAppDefaultMinDockWidth;
var webMapAppMapLeft = 262;
var webMapAppHasScroll = false;
var webMapAppLastHasScroll = false;
var m_measureToolbarId = "";
var webMapAppCurrentParcelSearch = "";
var theme = "";
var resultsFPExpanded = "false";
var tocExpanded = "false";

var s_window = "";

function load() 
{
   if (GBrowserIsCompatible(lat, lon)) {
     var map = new GMap2(document.getElementById("map"));
     map.setCenter(new GLatLng(lat, lon), 13);
   }
}

function summaryPaint(fullwindow,contents)
{
  if (!s_window.closed){
//    id_window=window.open ("","mywindow1","status=1,scrollbars=1,resizeable=1,location=1,menubar=1"); 
    id_window.document.getElementById('SummaryGuts').innerHTML = contents; 
  }
  else {
    s_window=window.open ('','mywindow1',"mywindow","menubar=1,resizable=1,width=350,height=250"); 

    s_window.document.write(fullwindow);
  }
}

function summaryPaint2(windowContents)
{
  if (s_window == "" || s_window.closed == true) {
    s_window=window.open ('','mywindow','width=400,menubar=1,resizable=1,scrollbars=1'); 
    s_window.document.write(windowContents);
    s_window.document.close();
}
  else {
    s_window.close();
    s_window=window.open ('','mywindow1','width=400,menubar=1,resizable=1,scrollbars=1'); 
    s_window.document.write(windowContents);
    s_window.document.close();
  }
}
function OpenBrowserHelp() {
    window.open('http://maps1.larimer.org/apps/lcparlocator/help/default.htm', 'HelpWindow');
}
function OpenBrowserMS(URL)
{
    window.open (URL,'msMapsWindow'); 
}

function OpenBrowserGoogle(URL)
{
    window.open (URL,'googleMapsWindow','width=520,height=430'); 
}


// function to set initial sizes of page elements
function setPageElementSizes() {
        // set body style 
        if (document.documentElement) {
            document.documentElement.style.overflow = "hidden";
            document.documentElement.style.height = "100%"; 
        } else {
            document.body.style.overflow = "hidden";
            document.body.style.height = "100%";
        }  
        // get necessary elements
        webMapAppMapDisplay = document.getElementById("Map_Panel");
        webMapAppPanelDisplay = document.getElementById("LeftPanelCellDiv");
        webMapAppPanelDisplayCell = document.getElementById("LeftPanelCell");
        webMapAppPanelScrollDiv = document.getElementById("LeftPanelScrollDiv");
        webMapAppToggleDisplay = document.getElementById("ToggleCell");
        webMapAppPanelSlider = document.getElementById("PanelSlider");
        webMapAppPanelDisplayTableCell = document.getElementById("LeftPanelTableCell");
        webMapAppPanelBottomSlider = document.getElementById("PanelSliderBottom");
        webMapAppScaleBar = document.getElementById("ScaleBar1");
        webMapAppNavBar = document.getElementById("Navigation1");
        webMapAppCopyrightText = document.getElementById("CopyrightTextHolder");
        var headerDisplay = document.getElementById("PageHeader");
        var linkDisplay = document.getElementById("LinkBar");
        // set scroll on Dock
        webMapAppPanelScrollDiv.style.overflowY = "auto";
        if (isIE) {
            webMapAppPanelDisplay.style.overflow = "hidden";
        } 
        // get the set widths and heights
        webMapAppLeftPanelWidth = webMapAppPanelDisplay.clientWidth;
        webMapAppToggleWidth = parseInt(webMapAppToggleDisplay.style.width);
        webMapAppTopBannerHeight = headerDisplay.clientHeight + linkDisplay.clientHeight;  // Here was the + 40
        // get browser window dimensions
        var sWidth = getWinWidth();
        var sHeight = getWinHeight();
        // set map display dimensions
        var mWidth = sWidth - webMapAppLeftPanelWidth - webMapAppToggleWidth;
        var mHeight = sHeight - webMapAppTopBannerHeight;
        webMapAppMapLeft = webMapAppLeftPanelWidth + webMapAppToggleWidth;
        webMapAppMapDisplay.style.width =  mWidth + "px";
        webMapAppMapDisplay.style.height = mHeight  + "px";
        if (webMapAppScaleBar!=null) {
            var sbWidth = webMapAppScaleBar.clientWidth;
            var sbHeight = webMapAppScaleBar.clientHeight;
            webMapAppScaleBar.style.left = (sWidth - sbWidth - 10) + "px";
            webMapAppScaleBar.style.top = (sHeight - sbHeight - 10) + "px";
            //webMapAppScaleBar.style.width = "auto";
            //webMapAppScaleBar.style.height = "auto";
        } 
        if (webMapAppNavBar!=null) {
            var sbWidth = webMapAppNavBar.clientWidth;
            var sbHeight = webMapAppNavBar.clientHeight;
            webMapAppNavBar.style.left =  (sWidth-100) + "px"; //(sWidth - sbWidth - 10) + "px";
            webMapAppNavBar.style.top = 25 + "px"; //(sHeight - sbHeight - 10) + "px";
            webMapAppNavBar.style.width = "auto";
            webMapAppNavBar.style.height = "auto";
        } 
        if (webMapAppCopyrightText!=null) {
            webMapAppCopyrightText.onmousedown = webMapAppGetCopyrightText;
            var crtHeight = webMapAppCopyrightText.clientHeight;
            webMapAppCopyrightText.style.left = (webMapAppMapLeft + 10) + "px";
            webMapAppCopyrightText.style.top = (sHeight - crtHeight - 10) + "px";
        }
        // set heights of left panel and toggle bar
        webMapAppToggleDisplay.style.height = mHeight  + "px";
        webMapAppPanelScrollDiv.style.height = mHeight  + "px";
        esriMaxFloatingPanelDragRight = sWidth - 15;
        esriMaxFloatingPanelDragBottom = sHeight - 15;

}

function updateScaleBarLocation()
{
     var sWidth = getWinWidth();
     var sHeight = getWinHeight();
     webMapAppScaleBar = document.getElementById("ScaleBar1");
     if (webMapAppScaleBar!=null) {
        var sbWidth = webMapAppScaleBar.clientWidth;
        var sbHeight = webMapAppScaleBar.clientHeight;
        webMapAppScaleBar.style.left = (sWidth - sbWidth - 10) + "px";
        webMapAppScaleBar.style.top = (sHeight - sbHeight - 10) + "px";
        //webMapAppScaleBar.style.width = "auto";
        //webMapAppScaleBar.style.height = "auto";
     }
    //alert("change scalebar dude"); 
}

// function to toggle Dock visibility
function togglePanelDock() {
    if (webMapAppPanelDisplay.style.display=="none") {
        expandPanelDock();
    } else {
        collapsePanelDock();
    }     
}

function expandPanelDock() {
    var image = document.images["CollapseImage"];
    webMapAppPanelDisplay.style.display = "block";
    image.src = "images/collapse_left.gif";
    image.alt = "Collapse";
    webMapAppPanelSlider.style.cursor = "e-resize";
    webMapAppPanelBottomSlider.style.cursor = "e-resize"; 
    webMapAppMapLeft = webMapAppLeftPanelWidth + webMapAppToggleWidth;
    webMapAppMapDisplay.style.left =  webMapAppMapLeft + "px";
    AdjustMapSize(); 
}

function collapsePanelDock() {
    var image = document.images["CollapseImage"];
    dockWidthString = webMapAppPanelDisplayCell.clientWidth + "px";
    webMapAppPanelDisplay.style.display = "none";
    //webMapAppPanelDisplayCell.style.width = "1px";
    image.src = "images/expand_right.gif";
    image.alt = "Expand";
    webMapAppPanelSlider.style.cursor = "default"; 
    webMapAppPanelBottomSlider.style.cursor = "default"; 
    webMapAppMapLeft = webMapAppToggleWidth; 
    webMapAppMapDisplay.style.left =  webMapAppMapLeft + "px";  
    AdjustMapSize();  
   
}  

// function for adjusting element sizes when brower is resized
function AdjustMapSize() {
   // set element widths 
    webMapAppPanelDisplay.style.width =  webMapAppLeftPanelWidth + "px";
    webMapAppToggleDisplay.style.width = webMapAppToggleWidth + "px";
   // get browser window dimensions 
    var sWidth = getWinWidth();
    var sHeight = getWinHeight();
    // calc dimensions needed for map
    var mWidth = sWidth - webMapAppPanelDisplayCell.clientWidth;
    var mHeight = sHeight - webMapAppTopBannerHeight + 20;
    if (mWidth<5) mWidth = 5;
    if (mHeight<5) mHeight = 5;  
    webMapAppMapDisplay.style.width =  mWidth + "px";
    webMapAppMapDisplay.style.left =  webMapAppPanelDisplayCell.clientWidth + "px"; 
   // set heights on elements 
    webMapAppMapDisplay.style.height = mHeight  + "px";
    webMapAppToggleDisplay.style.height = mHeight  + "px";
    webMapAppPanelScrollDiv.style.height = mHeight + "px";
    
    if (webMapAppScaleBar!=null) {
        var sbWidth = webMapAppScaleBar.clientWidth;
        var sbHeight = webMapAppScaleBar.clientHeight;
        webMapAppScaleBar.style.left = (sWidth - sbWidth) + "px";
        webMapAppScaleBar.style.top = (sHeight - sbHeight - 10) + "px";
    } 
    if (webMapAppNavBar!=null) {
        //var sbWidth = webMapAppScaleBar.clientWidth;
        //var sbHeight = webMapAppScaleBar.clientHeight;
        webMapAppNavBar.style.left =  (sWidth-100) + "px"; // (sWidth - sbWidth - 10) + "px";
        webMapAppNavBar.style.top =  25 + "px"; //(sHeight - sbHeight - 10) + "px";
    }
   // resize the map 
    window.setTimeout("resizeTheMap(" + mWidth + ", " + mHeight + ", false);", 500);
   // update map properties 
     
    var box = calcElementPosition("Map_Panel"); 
    map.containerLeft = box.left;
	map.containerTop = box.top;
    if (webMapAppCopyrightText!=null) {
        var crtHeight = webMapAppCopyrightText.clientHeight;
        webMapAppCopyrightText.style.left = (box.left + 10) + "px";
        webMapAppCopyrightText.style.top = (sHeight - crtHeight - 10) + "px";
    }
    return false;
}

// function for resizing map in Web Map App
function resizeTheMap(width, height, resizeExtent) {
    if (resizeExtent == null) resizeExtent = true;
    map = $find("Map1");
    map.set_progressBarAlignment(ESRI.ADF.System.ContentAlignment.BottomCenter);
    map.resize(width, height, resizeExtent);
    //map.checkMapsize();
    var div = document.getElementById("LeftPanelCellDiv"); 
    // update overview, if doc panel is expanded 
    if (ov!=null && div.style.display!="none") { 
        var argument = "ControlType=OverviewMap&EventArg=OverviewZoom";
        var context = ov.controlName; 
        eval(ov.callBackFunctionString);
    }
    return false; 
}

// handler for window resize
function AdjustMapSizeHandler(e) {
    window.clearTimeout(reloadTimer);
	reloadTimer = window.setTimeout("AdjustMapSize();",1000);
}

// function run at startup
function startUp() {
        // set up identify mode for javascript
//        map.ctrlMode = "MapIdentify";
//        map.ctrlAction = "Point";
//        map.ctrlCursor = "pointer";
//        map.ctrlFunction = "MapIdClick(e)";
        //var m = Maps["Map1"];
        if (newLoad) {
            // execute only on intial load.... not callbacks
            //map.divObject.style.cursor = "wait";
            //map.style.cursor = "wait";
            // move magnifier and measure toolbar to top left corner of map display 
            var box = calcElementPosition("Map_Panel"); 
            //var mag = document.getElementById("Magnifier1");
//            if (mag!=null && typeof(esriMagnifiers)!="undefined" && esriMagnifiers!=null) { 
//                    floatingPanel = esriMagnifiers["Magnifier1"].floatingPanel; 
//                    if (floatingPanel!=null) moveTo(box.left, box.top);
//            }
            var tb = document.getElementById(m_measureToolbarId);
			if (tb!=null) {
			    tb.style.left = box.left + "px";
			    tb.style.top = box.top + "px";
			}
			var fp = document.getElementById("CopyrightText_Panel");
			if (fp!=null) {
			    floatingPanel = fp;
			    moveTo(box.left, box.top);
			}        
        } 
        // set window resize event handler
       window.onresize = AdjustMapSizeHandler;
       for (var fp in FloatingPanels) {
            FloatingPanels[fp].onDockFunction = scrollDockToPanel;
        }
//        if (webMapAppScaleBar != null) {
//            var sbWidth = webMapAppScaleBar.clientWidth;
//            var sbHeight = webMapAppScaleBar.clientHeight;
//            webMapAppScaleBar.style.left = (sWidth - sbWidth - 10) + "px";
//            webMapAppScaleBar.style.top = (sHeight - sbHeight - 10) + "px";
//        } 
        //AdjustMapSize();
        //alert("Start Up");                       
}  

// function to request closing of session items.... only called if at least one resource is local non-pooled
function CloseOut() {
	var argument = "ControlID=Map1&ControlType=Map&EventArg=CloseOutApplication";
	var context = map.controlName;
	eval(webMapAppCloseCallback);
}

// response function to close out browser ... request sent to server by CloseOut()
function CloseOutResponse(response, context) {
    window.close(); 
    // if user selects Cancel in close dialog, send to close page 
    document.location = response; 
}


function startWebMapAppDockDrag(e) {
    webMapAppMoveFunction = document.onmousemove;
    document.onmouseup = stopWebMapAppDocDrag;  
    if (webMapAppPanelDisplay.style.display!="none") {
        webMapAppWindowWidth = getWinWidth();
        getXY(e); 
        webMapAppLeftOffsetX = mouseX - webMapAppPanelDisplay.clientWidth;
        var box = calcElementPosition("Map_Panel");
        webMapAppRightOffsetX = box.left - mouseX; 
        document.onmousemove = moveWebMapAppDockDrag;
        var ovPanel = document.getElementById("OverviewMap_Panel_BodyRow");
        var ovDisplay =  document.getElementById("OVDiv_OverviewMap_Panel_OverviewMap1");
        // because the panel cell will be as wide as the overview map image, keep the element width larger than the image. 
        if (FloatingPanels["OverviewMap_Panel"]!=null &&FloatingPanels["OverviewMap_Panel"].docked)
            webMapAppMinDockWidth = parseInt(ovDisplay.style.width) + 20;
        else    
            webMapAppMinDockWidth = webMapAppDefaultMinDockWidth; 
    }
    return false;  
}

function moveWebMapAppDockDrag(e) {
    getXY(e);
    webMapAppLeftPanelWidth =  mouseX - webMapAppLeftOffsetX;
    if (webMapAppLeftPanelWidth < webMapAppMinDockWidth) webMapAppLeftPanelWidth = webMapAppMinDockWidth; 
    var mapLeftString =  (webMapAppLeftPanelWidth + webMapAppToggleDisplay.clientWidth) + "px";
    //if (webMapAppPanelDisplayTableCell.clientWidth - webMapAppToggleDisplay.clientWidth > webMapAppLeftPanelWidth) webMapAppLeftPanelWidth = webMapAppPanelDisplayTableCell.clientWidth - webMapAppToggleDisplay.clientWidth;
    var widthString =  webMapAppLeftPanelWidth + "px";
    webMapAppPanelDisplay.style.width = widthString;
    //var width =  webMapAppWindowWidth - webMapAppMapDisplay.clientWidth;
    var width =  webMapAppWindowWidth - webMapAppPanelDisplayCell.clientWidth; 
    webMapAppMapDisplay.style.width = width + "px";
    webMapAppMapDisplay.style.left = mapLeftString;
    return false;
}

function stopWebMapAppDocDrag(e) {
    document.onmousemove = webMapAppMoveFunction;
    document.onmouseup = null;   
    webMapAppCheckPanelWidths();
    AdjustMapSize();  
    return false;
}

function OpenWindow(url) {
    window.open(url);
}

function webMapAppCheckPanelScroll() {
    webMapAppPanelScrollDiv = document.getElementById("LeftPanelScrollDiv");
    if (webMapAppPanelScrollDiv.scrollHeight>webMapAppPanelScrollDiv.clientHeight) {
        webMapAppHasScroll = true;
    }  else {
        webMapAppHasScroll = false;
    }  
    //webMapAppCheckPanelWidths(); 
    if (webMapAppHasScroll!=webMapAppLastHasScroll)
        AdjustMapSize();
    webMapAppLastHasScroll = webMapAppHasScroll;    
    return false; 
}

function webMapAppCheckPanelWidths() {
    var maxWidth = 0;
    var node; 
    for (var i=0; i< webMapAppPanelDisplay.childNodes.length; i++) {
        if (webMapAppPanelDisplay.childNodes[i].tagName=="TABLE") {
            node = webMapAppPanelDisplay.childNodes[i];
            if (node.clientWidth>maxWidth) maxWidth = node.clientWidth; 
        }
    }  
     webMapAppPanelDisplay.style.width = maxWidth + "px";
    return false;
}

function webMapAppGetCopyrightText() {
	var argument = "ControlID=Map1&ControlType=Map&EventArg=GetCopyrightText";
	var context = map.controlName;
	eval(webMapAppCopyrightCallback);
	showFloatingPanel('CopyrightText_Panel');
}

function scrollDockToPanel(panelElement) {
    if (panelElement==null) return;
    var yPos = panelElement.offsetTop;
    webMapAppPanelScrollDiv.scrollTop = yPos;  
}


//FireCallBack - Fires callback against map control.
//
//Arguments -
//  Control Name: The control to update
//  Server Function: The name of the 'function' in RaiseCallbackEvent to run
//  Server Mode: The 'mode' of the function to run (ie subset choice, use same as Server Function if no mode)
//  ServerArgs: Any argument data needed on server side.  Data must be in format:
//            var=val&var2=val2&var3=val3 and so on.
// Author: DJones - 2/3/07        
function FireCallBack(controlName, ServerFunction, ServerMode, ServerArgs){

    if(ServerMode == "") {
        ServerMode = "novalue";
    }
    if(ServerArgs != "")
    {
        ServerArgs = "&" + ServerArgs;
    }
    var Arguments = "ControlID=" + controlName + "&ControlType=Map&EventArg=" + ServerFunction + "&Map1_mode=" + ServerMode + ServerArgs;

    var Context = controlName;

    eval(WebForm_DoCallback('__Page',Arguments,processCallbackResult,Context,null,true));
}

//---Floating Panel Functions---//

//Shows the floating panel
function ShowFP(PanelID)
{
    var maxzindex = document.getElementById("maxzindex").value;
    maxzindex++;
    document.getElementById("maxzindex").value = maxzindex;
    document.getElementById(PanelID).style.zIndex = maxzindex;
    
//var toolbarName = "SelectToolbar";
// get the form that contains the toolbar
//var f = document.forms["form1"];
// set the appropriate map tool - theis will be from the name attribute in your source page
//f.elements[Toolbars[toolbarName].currentToolField].value = "MapSelectPoint";

// the javascript function that must be run for the particular toolbar button
//MapDragImage ( 'Map1' , 'MapSelectPoint', true,''); 
// refresh the toolbar
//Toolbars[toolbarName].refreshGroup();
//Toolbars[toolbarName].selectTool();

    
//      
//    if (PanelID == "ParcelDetailSearchFP")
//    {   
//       if (webMapAppCurrentParcelSearch == "")
//       {
//          webMapAppCurrentParcelSearch = "ParcelSearchOwnerAddressTab";
//       }
       
//       tabOnClick(webMapAppCurrentParcelSearch);
//    }
    
    showFloatingPanel(PanelID,false);
    if (PanelID == "SelectToolFP")
    {
        
        //document.all("MapSelectPoint").focus();
        //document.getElementById('SelectToolFP_SelectToolbarMapSelectPoint').focus();
       //document.getElementById('SelectToolFP_SelectToolbarMapSelectPoint').click();
        
        // id of the toolbar
        var toolbarName = "SelectToolFP_SelectToolbar";
        // get the form that contains the toolbar
        var f = document.forms["form1"];
      
        // set the appropriate map tool - theis will be from the name attribute in your source page
        f.elements[Toolbars[toolbarName].currentToolField].value = "MapSelectPoint";
        // the javascript function that must be run for the particular toolbar button
        //MapDragImage ( 'Map1' , 'MapSelectPoint', true,''); 
        MapPoint ('Map1','MapSelectPoint', true,'');
        //ToolbarMouseDown( 'SelectToolFP_SelectToolbar', 'MapSelectPoint', 'Tool', event);
        // refresh the toolbar
        Toolbars[toolbarName].refreshGroup();
        Toolbars[toolbarName].selectTool();
        map = Maps["Map1"];
        map.set_cursor("pointer");
        
        
        //FireCallBack("Map1","SelectToolFocus","","");
    }
}



//Hides the floating panel
function HideFP(PanelID)
{
    hideFloatingPanel(PanelID,false);
}
//Updates the floating panel's z-order to make it appear on top of other panels
function UpdatePanelZ(PanelID)
{
//    var maxzindex = document.getElementById("maxzindex").value;
//    maxzindex++;
//    document.getElementById("maxzindex").value = maxzindex;
//    document.getElementById(PanelID).style.zIndex = maxzindex;
}
function UpdatePanelZ2(PanelID) {
        var maxzindex = document.getElementById("maxzindex").value;
        maxzindex++;
        document.getElementById("maxzindex").value = maxzindex;
        document.getElementById(PanelID).style.zIndex = maxzindex;
}
//Fires callback to clear user added graphics
function ClearGraphicSelection()
{
    FireCallBack("Map1", "ClearSelection", "Selection", "");
    HideAllIdIcon();
    //HideIdIcon(1);
}

function PrintPDF()
{
    ShowFP("PDFNotify_panel");
    cursor_wait();
    FireCallBack("Map1","Export2PDF","","");
    cursor_clear();
}


//---previous extent functions---//
// Fires callback to force map to zoom to previous extent
function PreviousExtent()
{
    FireCallBack("Map1","PreviousExtent","Extent","");
}


//---html attribute table functions---//
// Fires callback to display the selected features in an html attribute table
function ShowHTMLTable(LayerName,ShowZoom,ShowHighlight,AutoZoomExtent,DivID)
{
    FireCallBack("Map1","ShowAttributes","Selection","layer=" + LayerName + "&showzoom=" + ShowZoom + "&showhighlight=" + ShowHighlight + "&autozoom=" + AutoZoomExtent + "&divid=" + DivID ); 
}
// Fires callback to highlight a selected feature.  In addition, selected features row is highlighted in attribute table
function HighLightGraphicFeature(LayerName,ObjectID,RowID)
{
    //highlight feature on map
    FireCallBack("Map1","HighLightFeature","Selection","layer=" + LayerName + "&objectid=" + ObjectID);
    //highlight row
    Row = document.getElementById(RowID);
    if (Row.className == "midGreen")
    {
         var table = document.getElementById("selectedattributetable");
         for (j=1;j<table.rows.length;j++) 
         { 
            if(table.rows[j].id == Row.id)
            {
                if (j % 2 == 0)
                {
                    TDClass = "lightBrown";
                    Row.className = TDClass; 
                }
                else
                {
                    TDClass = "lightGreen";
                    Row.className = TDClass; 
                }
            }
         }
    }else{
        Row.className = "midGreen";
    }
    
}
// Zoooms all and none at same time, yess sir yee
function ZoomAllNone(id)
{
    FireCallBack("Map1","SelectionAllNone","Selection","id=" + id);
}

// Zooms to graphic feature on specified layer
function ZoomGraphicFeature(LayerName,ObjectID)
{ 
    FireCallBack("Map1","ZoomFeature","Selection","layer=" + LayerName + "&objectid=" + ObjectID);
}

// Enables the sorting of columns on attribute table
function EnableSort()
{
    sortables_init();
}

//Alters the buttonID's css class
function MouseActionButton(buttonID,cssClass)
{
    button = document.getElementById(buttonID);
    button.className = cssClass;
}

//Fires callback to zoom to all selected features
function ZoomAll(LayerName)
{
    FireCallBack("Map1","SelectionZoomAll","Selection","layer=" + LayerName);
}

// Fires callback to select all selected features (highlight all)
function SelectAll(LayerName)
{
    FireCallBack("Map1","SelectionHighlightAll","Selection","layer=" + LayerName);
     var table = document.getElementById("selectedattributetable");
     for (j=1;j<table.rows.length;j++) 
     { 
        //set class
        TDClass = "midGreen";
        table.rows[j].className = TDClass; 
        
        //set checkbox
        document.getElementById("HighLightGraphicCheckBox_" + (j-1)).checked = true;
     }
}

// Fires callback to unhighlight all selected features
function UnSelectAll(LayerName)
{
    FireCallBack("Map1","SelectionUnHighlightAll","Selection","layer=" + LayerName);
    var table = document.getElementById("selectedattributetable");
     for (j=1;j<table.rows.length;j++) 
     { 
       
        if (j % 2 == 0)
        {
            TDClass = "lightBrown";
            table.rows[j].className = TDClass; 
        }
        else
        {
            TDClass = "lightGreen";
            table.rows[j].className = TDClass; 
        }
        
        //set checkbox
        document.getElementById("HighLightGraphicCheckBox_" + (j-1)).checked = false;
     }
}

// Fires callback to flip highlighted selection of selected features
function FlipSelection(LayerName)
{
    var table = document.getElementById("selectedattributetable");
     for (j=1;j<table.rows.length;j++) 
     { 
        if(table.rows[j].className == "midGreen")
        {
            if (j % 2 == 0)
            {
                TDClass = "lightBrown";
                table.rows[j].className = TDClass; 
            }
            else
            {
                TDClass = "lightGreen";
                table.rows[j].className = TDClass; 
            }  
            document.getElementById("HighLightGraphicCheckBox_" + (j-1)).checked = false;
        }
        else
        {
             TDClass = "midGreen";
             table.rows[j].className = TDClass; 
             
             document.getElementById("HighLightGraphicCheckBox_" + (j-1)).checked = true;
        }
     }
    
    FireCallBack("Map1","SelectionHighlightFlip","Selection","layer=" + LayerName);
}

// Fires callback to save csv of selected features
function SaveSelection(LayerName)
{
    //export the selection set to CSV format
     FireCallBack("Map1","ExportSelectionTable","Selection","export=true");
}

//calls reporting funcationality
function ReportSelection(LayerName)
{
   ExportMapAttributes();
}


function ExpandTOC() 
{
//    if (tocExpanded == "false") {
//        tocExpanded = "true";
//        expandFloatingPanel("Toc_Panel");
//    }
//    else {
//        tocExpanded = "false";
//        collapseFloatingPanel("Toc_Panel");
//    }
//    expandFloatingPanel("Toc_Panel");    
}
//shows attr tablehttp://www.larimer.org/assessor/query/Detail.cfm?PropertyTypeVar=Residential&BuildingIDVar=001&NumVar=R0399477&direct=1
function ShowAttributeTable() // seitz
{
      if (resultsFPExpanded == "false")
      {
          resultsFPExpanded = "true";
        expandFloatingPanel("QueryResultsFP");
      }
      else
      {
          resultsFPExpanded = "false";
        collapseFloatingPanel("QueryResultsFP");
      }
//    FireCallBack("Map1","ShowSelectionTable","Selection","show=true");
}


//shows attribute table working
function ShowAttributeTableWorking()
{
    FireCallBack("Map1","ShowSelectionTable","Selection","show=true");
}

//shows parcel details floating panel and data
function GetParcelDetails(SPNval)
{
    var strASRURL = "http://www.larimer.org/assessor/query/results.cfm?parcelnumber=" + SPNval;
    window.open(strASRURL);
    
}

// update scalebar
function ChangeScaleBar(Unit)
{
    FireCallBack("Map1","ScaleBar","ScaleBar","unit=" + unit);
}


function StreetViewWindow()
{
    var XCoordObj = document.getElementById("CoordZoomX");
    var YCoordObj = document.getElementById("CoordZoomY");
    var CordSys = document.getElementById("CoordZoomSystem");
}



//---Map Zoom to Coordinate Functions---//




//checks to see if its a good string
function IsGoodString(string,IsInt)
{
    if(IsInt == true){
        var CoordValidChars = "0123456789-";
    }else{
        var CoordValidChars = "0123456789.-";
    }
    for (var idx = 0;idx<string.length; idx++) 
        { 
            char = string.charAt(idx); 

            // if the char isn't in the string of good 
            // characters then exit returning false 
            if (CoordValidChars.indexOf(char) == -1) 
            { 
              return false; 
            } 
        }
        return true;
}

//update the coordinate unit
function CoordChangeUnit()
{
    var CordSys = document.getElementById("CoordZoomSystem");
    var SelCordSys = CordSys.options[CordSys.selectedIndex].value;
    var CoordXLabel = document.getElementById("CoordZoomXLabel");
    var CoordYLabel = document.getElementById("CoordZoomYLabel");
    var CoordTitle = document.getElementById("CoordZoomTitle");
    var XCoordObj = document.getElementById("CoordZoomX");
    var XCoordObj2 = document.getElementById("CoordZoomX2");
    var XCoordObj3 = document.getElementById("CoordZoomX3");
    var YCoordObj = document.getElementById("CoordZoomY");
    var YCoordObj2 = document.getElementById("CoordZoomY2");
    var YCoordObj3 = document.getElementById("CoordZoomY3");
    if (SelCordSys == "DD")
    {
        XCoordObj2.style.visibility = "hidden";
        XCoordObj3.style.visibility = "hidden";
        YCoordObj2.style.visibility = "hidden";
        YCoordObj3.style.visibility = "hidden";
        XCoordObj2.style.display = "none";
        XCoordObj3.style.display = "none";
        YCoordObj2.style.display = "none";
        YCoordObj3.style.display = "none";
        XCoordObj.style.width = "100px";
        YCoordObj.style.width = "100px";
        CoordXLabel.innerHTML = "Long:";
        CoordYLabel.innerHTML = "Lat:";
        CoordTitle.innerHTML = "Enter Coordinate (Decimal Degrees)";
    }
    else if(SelCordSys == "LL")
    {
        XCoordObj2.style.visibility = "visible";
        XCoordObj3.style.visibility = "visible";
        YCoordObj2.style.visibility = "visible";
        YCoordObj3.style.visibility = "visible";
        XCoordObj2.style.display = "inline";
        XCoordObj3.style.display = "inline";
        YCoordObj2.style.display = "inline";
        YCoordObj3.style.display = "inline";
        XCoordObj.style.width = "30px";
        YCoordObj.style.width = "30px";
        CoordXLabel.innerHTML = "Long:";
        CoordYLabel.innerHTML = "Lat:";
        CoordTitle.innerHTML = "Enter Coordinate (D M S)";
    }
    else if(SelCordSys == "SP")
    {
        XCoordObj2.style.visibility = "hidden";
        XCoordObj3.style.visibility = "hidden";
        YCoordObj2.style.visibility = "hidden";
        YCoordObj3.style.visibility = "hidden";
        XCoordObj2.style.display = "none";
        XCoordObj3.style.display = "none";
        YCoordObj2.style.display = "none";
        YCoordObj3.style.display = "none";
        XCoordObj.style.width = "100px";
        YCoordObj.style.width = "100px";
        CoordXLabel.innerHTML = "Easting:";
        CoordYLabel.innerHTML = "Northing:";
        CoordTitle.innerHTML = "Enter Coordinate (State Plane)";
    }
}
//---End Coordinate Functions---//

//---Adjust North Arrow Position--//

function cursor_wait() {
    document.body.style.cursor = 'wait';
}

function cursor_clear() {
  document.body.style.cursor = 'default';
}


//---Parcel Search Functionality---//

// open a search form 
function OpenSearchForms()
{
    //open parcel search form
    ShowFP('ParcelSearchFP');
}
function MoveToFullVersion() 
{
    FireCallBack("Map1", "OpenFullMapViewer", "Selection", "reload=true");
}
function SearchForParcel()
{
   window.location.href = "http://www.larimer.org/assessor/query/search.cfm";

}


//---End Parcel Search Functionality---//


//-- Callback for removing, or hiding id icons --//
//function HideIdIcon(nodeID)
//{
//    FireCallBack("Map1","HideIdResult","HideIdResult","nodeID=" + nodeID);
//}
function HideAllIdIcon() {
    var idCount = document.getElementById("identifycounter").value;
    if (idCount > 0) {
        for( i = 1; i<=idCount; i++){
            HideIdIcon(i);
        }
        document.getElementById("identifycounter").value = 0;
    }    
}

function HideIdIcon(nodeID) {
    //FireCallBack("Map1", "HideIdResult", "HideIdResult", "nodeID=" + nodeID);
    var tag = "IdentifyLocation_" + nodeID;
    document.getElementById(tag).style.visibility = 'hidden';
}


//---Map & Attribute Print/Export Functionality---//
function ExportMap()
{
    var ExportTitle = " "; //escape(document.getElementById("ExportTitle").value);
    document.getElementById('exportdiv').innerHTML = "<img alt='working' src='images/callbackActivityIndicator.gif' />&nbsp;Working, please wait...";
    FireCallBack("Map1","Export","Export","format=PDF&title=" + ExportTitle );
//    if(ExportTitle == "Hal")
//    {
//        ZoomAllNone(1);
//    }
//    if(ExportTitle == "SeriousCat")
//    {
//        ZoomAllNone(2);
//    }
}


function ExportMapAttributes()
{
    var ExportTitle = document.getElementById("ExportTitle").value;
    document.getElementById('exportdiv').innerHTML = "<img alt='working' src='images/callbackActivityIndicator.gif' />&nbsp;Working, please wait...";
    FireCallBack("Map1","ExportAttributes","Export","format=PDF&title=" + ExportTitle);
}
//---End Map Print Functionality---//


//--Map Buffer Functionality--//
function MapBuffer()
{
    var BufDistance = document.getElementById("BufferDistance").value;
    var BufSelLayer = document.getElementById("BufferFP_BufferSelectLayer"); //because its a server html control, its name is appended with the parent server
    //control, which is BufferFP
    var BufLayerValue = BufSelLayer.options[BufSelLayer.selectedIndex].value;
    if(IsGoodString(BufDistance,true))
    {
        if(BufDistance == "" || BufDistance == null)
        {
            BufDistance = 100;
        }
        document.getElementById('BufferWork').innerHTML = "<img alt='working' src='images/callbackActivityIndicator.gif' />&nbsp;Working, please wait...";
        FireCallBack("Map1","Buffer","Buffer","distance=" + BufDistance + "&layer=" + BufLayerValue);
        ZoomAll(BufLayerValue);

        HideFP('BufferFP');
         
    }
    else
    {
        document.getElementById('BufferWork').innerHTML = "Please specify a numeric buffer distance!";
    }
}
//--End Buffer Functionality--//

//Reloads existing attribute table
function ReloadTable()
{
    FireCallBack("Map1","ReloadHTMLTable","Selection","reload=true");
}

function OpenOtherMapViewer() {
    FireCallBack("Map1", "OpenOtherMapViewer", "Selection", "reload=true");
}

function ClearMessage(ID)
{
    //alert(ID);
    //document.getElementById(ID).innerHTML = "clear me yo yo yo";
}
//-- async working indicators --//
// Displays working indicators in map when user pans, zooms etc
function showBusyIndicator(sender) {

            showLayer("BusyIndicator");

            if (sender!=null) {

                window.status = "Pending Images: " + sender.pendingTiles.length; 

            }  

        }

        

        function showPendingTiles(sender) {

            if (sender!=null) {

                window.status = "Pending Images: " + sender.pendingTiles.length; 

            }  

        }

        

        function hideBusyIndicator(sender) {

            hideLayer("BusyIndicator");

            if (sender!=null) {

                window.status = ""; 

            }  

        }    

// Set up MS Map tool
function Google(divid) {
    map = Maps[divid];
    //MapPoint(map.controlName, "Google", false);
    MapPoint(divid, "Google", false);
    //map.divObject.onmousedown = GoogleFunction;
    RemoveMouseDownHandlers(map)
    map.add_mouseDown(GoogleFunction);
    map.set_cursor("pointer");
}

function GoogleFunction(e) {

    var toolMode = map.__activeToolMode;
    if (toolMode != null && toolMode == "Google") {
        map.cursor = map.divObject.style.cursor;
        GetXY(e);
        //var box = calcElementPosition(map.containerDivId);
        //var box = calcElementPosition(map.get_uniqueID());
        zleft = mouseX;
        ztop = mouseY;
        ValueList = "zleft=" + zleft + "&ztop=" + ztop;
        FireCallBack("Map1", "Google", "Google", ValueList);
    }
   
}
// Set up MS Map tool
function MSMaps(divid) {
    map = Maps[divid];
    //map.set_cursor("pointer");
    //map.divObject.style.cursor = "pointer";
    //map._cursor = "pointer";
   
    
    //MapPoint(map.controlName, "MSMap", false);
    MapPoint(divid, "MSMap", false);
    //map._onMouseDown = MSMapsFunction;
    //var m1 = document.getElementById("Map1");
    RemoveMouseDownHandlers(map);
    map.add_mouseDown(MSMapsFunction);
    map.set_cursor("pointer");
}

function MSMapsFunction(e) {
    var toolMode = map.__activeToolMode;
    if (toolMode != null && toolMode == "MSMap") {
        map.cursor = map.divObject.style.cursor;
	    GetXY(e);
	    //var box = calcElementPosition(map.containerDivId);
	    //var box = calcElementPosition("Map1");
	    zleft = mouseX;
	    ztop = mouseY;
        ValueList = "zleft=" + zleft + "&ztop=" + ztop;
        FireCallBack("Map1", "MSMaps", "MSMaps", ValueList);
    }

}
function RemoveMouseDownHandlersByDivID(divid) {
    map = Maps[divid];
    RemoveMouseDownHandlers(map);
}
function RemoveMouseDownHandlers(map) 
{
    map.remove_mouseDown(GoogleFunction);
    map.remove_mouseDown(MSMapsFunction);
    map.remove_mouseDown(MapIdClick);
    map.remove_mouseDown(MapCoordsClick);
}


//--comments javascrtipt--//
// Fires callback to save user comments to email and send them to predetermined email address
function SubmitComments()
{
    CommentName = escape(document.getElementById("CommentName").value);
    CommentEmail = escape(document.getElementById("CommentEmail").value);
    CommentText = escape(document.getElementById("CommentText").value);
    Comment = document.getElementById("CommentSubject");
    CommentSubject = escape(Comment.options[Comment.selectedIndex].value);
    ValueList = "from=" + CommentName + "&email=" + CommentEmail + "&subject=" + CommentSubject + "&text=" + CommentText;
    
    // Display a message and send the comment via email //
 
    if (CommentSubject == "Question") {
        alert("We have received your Question and will respond to your email address as soon as possible");  
    }
    
    if (CommentSubject == "Comment") {
        alert("Thank you for your comments.  We appreciate your feedback.");
    }
    
    if (CommentSubject == "Website Problem") {
        alert("Thank you for reporting the Website Problem. We will investigate the issue and reply to your email address.");
    }
    
    if (CommentSubject == "Data Problem") {
        alert("Thank you for reporting the Data Problem. We will investigate the issue and reply to your email address.");
    }
    
    if (CommentSubject == "Other") {
        alert("Thank you for your comments.  We appreciate your feedback.");
    }
      
    FireCallBack("Map1","Comments","Comments",ValueList);
    
    // Reset the comment fields and hide the Comment Panel //
    document.getElementById("CommentName").value = "";
    document.getElementById("CommentEmail").value = "";
    document.getElementById("CommentText").value = "";   
    document.getElementById("CommentSubject").selectedIndex = 0;
    
    hideFloatingPanel("CommentsFP", false);
}






// Check for a valid numeric value
function dcIsNumeric(testValue)
{
   var strValidCharacters = "0123456789.";
   var strChar;
   var blnResult = true;
   
   // check that testValue consists of valid characters listed above
   for (i = 0; i < testValue.length && blnResult == true; i++)
   {
      strChar = testValue.charAt(i);
      if (strValidCharacters.indexOf(strChar) == -1)
      {
         blnResult = false;
      }
   }
   return blnResult;
}

function disableRadioInput()
{
    if (document.getElementById('ParcelRdNumber').checked == true)
    {
        
        var ipname = document.getElementById("ParcelSearchSubdivision");
        ipname.disabled = true;
        var ipname = document.getElementById("ParcelSearchRoadName");
        ipname.disabled = true;
        var ipname = document.getElementById("ParcelSearchNumber");
        ipname.disabled = false;
    }
    else if (document.getElementById('ParcelRdRoadName').checked == true)
    {
        var ipname = document.getElementById("ParcelSearchSubdivision");
        ipname.disabled = true;
        var ipname = document.getElementById("ParcelSearchRoadName");
        ipname.disabled = false;
        var ipname = document.getElementById("ParcelSearchNumber");
        ipname.disabled = true;
        
    }
    else if (document.getElementById('ParcelRdSubname').checked == true)
    {
        var ipname = document.getElementById("ParcelSearchSubdivision");
        ipname.disabled = false;
        var ipname = document.getElementById("ParcelSearchRoadName");
        ipname.disabled = true;
        var ipname = document.getElementById("ParcelSearchNumber");
        ipname.disabled = true;
        
    }
}


function EnableWaitCursor()
{
    document.body.style.cursor = "wait";
}
function StartExport(){
    showFloatingPanel('PDFNotify_panel',false);
    EnableWaitCursor();
    FireCallBack("Map1","Print","Print","Print=Print");
}

function GetXY(e) {
    //console.dir(e);
    if (isNav) {
        mouseX = e._mouseDragState.mousedownX;
        mouseY = e._mouseDragState.mousedownY;
    } else {
        mouseX = event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
        mouseY = event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
        // Added to adjust to cursor hot spot in IE
        if (document.all) {
            mouseX -= 3;
            mouseY -= 3;
        };
        // Added to adjust to cursor hot spot in IE
        var box = calcElementPosition("Map1");
        mouseX -= box.left;
        mouseY -= box.top;
    }
    return false;
}
