﻿        var reloadTimer;

        //Type.registerNamespace('ESRI.ADF.WebMappingApplication');

        function onLoad() {
            //called before the ESRI map control is loaded, to ensure the size of the map control
            // is correct. This requires  the Map_Content_Panel to be the correct size.
            //resizeLowerDiv();
            var mapdiv = document.getElementById("Map_Content_Panel");
            var winHeight = getWinHeight();
            var winWidth = getWinWidth();
            var searchdiv = document.getElementById("Search_Panel");
            var bardiv = document.getElementById("HeaderDiv");
            var barHeight = parseInt(bardiv.style.height);
            //var splitterObj = document.getElementById("vwdSplitter");
            //var splitterWidth = splitterObj.clientWidth;

            var mapHeight = winHeight - barHeight - 1;
            if (mapHeight < 0) mapHeight = 0;
           
            var mapWidth = winWidth
                    - parseInt(mapdiv.style.left)
                    - 16
                    - 1
                    - searchdiv.clientWidth
                    - parseInt(searchdiv.style.right)
                    - 0
                    ; //borderwidth of search panel
            if (mapWidth < 0) mapWidth = 0;
            mapdiv.style.height = mapHeight + "px";
            mapdiv.style.width = mapWidth + "px";

        }
        function ontimeout() {
            alert("Application has timed out. Please refresh the map before continuing");
            //window.location.reload();
        }
        function initializeJS() {
            //disable client timeout mechanism on ADF javascript library
            ESRI.ADF.System.maximumLapseTime = false; //override ADF timeout check

            ESRI.ADF.System.showLapseAlert = ontimeout;

            map = $find("Map1");
            var toolbar = $find("Toolbar1");
            if (map != null && toolbar != null) {
                toolbar.add_onToolSelected(OnToolSelectHandler);
            }
            
            //resizeLowerDiv();
            if (map != null)
            {
                map.checkMapsize(false);
            }
            window.onresize = AdjustMapSizeHandler;

            //window.setTimeout("initCollapseControl()", 100)
            initCollapseControl();

            resizeLowerDiv();
        }
//        function onBodyLoad() {
//            window.onresize = AdjustMapSizeHandler;
//            resizeLowerDiv();
//        }
//        


        function AdjustMapSize() {
            resizeLowerDiv();
            // resize the map 
            //window.setTimeout("resizeTheMap();", 100);
            //map.resizeToParent();
            return false;
        }
        // function for resizing map in Web Map App
        function resizeTheMap() {
            map.checkMapsize(true    );
            map.refresh();
            return false; 
        }
        function resizeLowerDiv() {
            //Content Div height = window.height - BarDiv.height
            var gutterWidths = 2 * 12;
            var winHeight = getWinHeight();
            var winWidth = getWinWidth();
            var bardiv = document.getElementById("HeaderDiv");
            var barWidth = (winWidth - gutterWidths);
            if (barWidth < 0) barWidth = 0;
            bardiv.style.width = barWidth + "px";
            var barHeight = parseInt(bardiv.style.height);
            var mapHeight = winHeight - barHeight - 1;
            if (mapHeight < 0) mapHeight = 0;
            var searchdiv = $get("Search_Panel");
            var searchWidth = searchdiv.clientWidth;
            //var searchHeight = searchdiv.scrollHeight;
            var searchHeight = searchdiv.scrollHeight;

            var splitterObj = $get("vwdSplitter");
            var splitterWidth = (splitterObj == null ? 0 : splitterObj.clientWidth);
            /**
            var mapWidth = (document.documentElement.clientWidth - searchdiv.clientWidth - splitterObj.clientWidth - mapdiv.offsetLeft - parseInt(searchdiv.style.right) - (parseInt(mapdiv.style.borderWidth) * 2));
            */
            var collapseControlPanel = $get("CollapseControlPanel");
            collapseControlPanel.style.width = searchWidth + "px";

            var mapdiv = document.getElementById("Map_Content_Panel");
            var mapWidth = winWidth
                    - parseInt(mapdiv.style.left)
                    - splitterWidth
                    - 1
                    - searchdiv.clientWidth
                    - parseInt(searchdiv.style.right)
                    - 0
                    ; //borderwidth of search panel
            if (mapWidth < 0) mapWidth = 0;
            searchdiv.style.height = mapHeight + "px";
            mapdiv.style.height = mapHeight + "px";
            mapdiv.style.width = mapWidth + "px";
            if(splitterObj != null)
            {
                splitterObj.style.left = (parseInt(mapdiv.style.left) + mapWidth + 1) + "px";
                splitterObj.style.height = mapHeight + "px";
            }
            bardiv = document.getElementById("leftmargin");
            bardiv.style.height = winHeight;
            bardiv = document.getElementById("rightmargin");
            bardiv.style.height = winHeight;

            if (map == null) map = $find("Map1");
            if (map != null) {
                map.checkMapsize(false);
            }
            window.setTimeout("resizeViewPortPanels('viewportpanel'," + (searchWidth - 21 - 15) + "," + (searchdiv.scrollHeight - 74) + ")", 100);
            
        }

    // handler for window resize
    var currheight;
    var currwidth;
    function AdjustMapSizeHandler(e) {
        if(currheight != document.documentElement.clientHeight 
         || currwidth != document.documentElement.clientWidth)
	    {
            window.clearTimeout(reloadTimer);
            reloadTimer = window.setTimeout("AdjustMapSize();",100);
    	}
	    currheight = document.documentElement.clientHeight;
	    currwidth = document.documentElement.clientWidth;
    }

    function initCollapseControl() {
        var ccb = $find("CollapseControlBehavior1");
        if (ccb != null) {
            ccb.add_expandComplete(expandHandler);
            ccb.add_collapseComplete(expandHandler);
        }
    }

    function expandHandler(sender, args) {
        window.clearTimeout(reloadTimer);
        //move button
        var expandButton = $get("CollapseControlImage1");
        var buttonStyle = expandButton.style;
        //FIXME: IE specific
//        if (buttonStyle.styleFloat == "left") {
//            buttonStyle.styleFloat = "right";
//        }
//        else {
//            buttonStyle.styleFloat = "left";
//        }
        reloadTimer = window.setTimeout("AdjustMapSize();", 25);
    }

    function splitterOnResize(splitter) {
        var winWidth = getWinWidth();
        // do any other work that needs to happen when the 
        // splitter resizes. this is a good place to handle 
        // any complex resizing rules, etc.
        var splitterObj;
        if (splitter == null) {
            splitterObj = $get("vwdSplitter");
        }
        else {
            splitterObj = splitter.splitterBar;
        }

        // make sure the width is in number format
        var mapdiv = $get("Map_Content_Panel");
        var searchdiv = $get("Search_Panel");
        var gutterWidths = 12 * 2;
       
        var newSearchWidth = (winWidth
                         - parseInt(mapdiv.style.left)
                         - mapdiv.clientWidth
                         - 1
                         - splitterObj.clientWidth
                         - parseInt(searchdiv.style.right)
                         );
        var newHeight = searchdiv.scrollHeight;
        searchdiv.style.width = newSearchWidth + "px";
        var collapseControlPanel = $get("CollapseControlPanel");
        collapseControlPanel.style.width = newSearchWidth + "px";
        window.setTimeout("resizeViewPortPanels('viewportpanel'," + (newSearchWidth - 21 - 15) + "," + (newHeight - 74) + ")", 25);
        
    }

    function resizeViewPortPanels(className, newWidth, newHeight) {
        //validate size values are non-negative - fix if they are
        if (newWidth < 0) newWidth = 0;
        if (newHeight < 0) newHeight = 0;
        
        // use your favorite method of getting all the elements you need
        var objs = getElementsByClassName(className);
        var i;
        for (i in objs) {
            var obj = objs[i];
            obj.style.width = newWidth + "px";
            //obj.style.height = newHeight + "px";
            obj.style.height = "auto";
        }
    }


    function resizeSearchPanel(panelId) {
        var rightPanel = $get("Search_Panel");
        var insidePanel = $get(panelId);
        var newWidth = rightPanel.scrollWidth;
        var newHeight = rightPanel.scrollHeight;
        window.setTimeout("resizeViewPortPanels('viewportpanel'," + (newWidth - 21 - 15) + "," + (newHeight - 74) + ")", 25);
    
    }
    
    /*
    Developed by Robert Nyman, http://www.robertnyman.com
    Code/licensing: http://code.google.com/p/getelementsbyclassname/
    */
    var getElementsByClassName = function(className, tag, elm) {
        if (document.getElementsByClassName) {
            getElementsByClassName = function(className, tag, elm) {
                elm = elm || document;
                var elements = elm.getElementsByClassName(className),
				nodeName = (tag) ? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
                for (var i = 0, il = elements.length; i < il; i += 1) {
                    current = elements[i];
                    if (!nodeName || nodeName.test(current.nodeName)) {
                        returnElements.push(current);
                    }
                }
                return returnElements;
            };
        }
        else if (document.evaluate) {
            getElementsByClassName = function(className, tag, elm) {
                tag = tag || "*";
                elm = elm || document;
                var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace) ? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
                for (var j = 0, jl = classes.length; j < jl; j += 1) {
                    classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
                }
                try {
                    elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
                }
                catch (e) {
                    elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
                }
                while ((node = elements.iterateNext())) {
                    returnElements.push(node);
                }
                return returnElements;
            };
        }
        else {
            getElementsByClassName = function(className, tag, elm) {
                tag = tag || "*";
                elm = elm || document;
                var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all) ? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
                for (var k = 0, kl = classes.length; k < kl; k += 1) {
                    classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
                }
                for (var l = 0, ll = elements.length; l < ll; l += 1) {
                    current = elements[l];
                    match = false;
                    for (var m = 0, ml = classesToCheck.length; m < ml; m += 1) {
                        match = classesToCheck[m].test(current.className);
                        if (!match) {
                            break;
                        }
                    }
                    if (match) {
                        returnElements.push(current);
                    }
                }
                return returnElements;
            };
        }
        return getElementsByClassName(className, tag, elm);
    };        
    


    function toggleMagnifier() {
        var mag = $get("Magnifier1");
        if (mag != null) {
            toggleFloatingPanelVisibility('Magnifier1');
        } else
            alert("Magnifier is not available");
    }

    function toggleMeasure() {
        var mag = $get("Measure_Panel");
        if (mag != null) {
            var wasHidden = (mag.style.visibility != "visible");
            toggleFloatingPanelVisibility('Measure_Panel');
            if (wasHidden) {
                lastToolUsed = "Measure";
                startMeasure(false);
            }

        } else
            alert("Measure is not available");
    }

    function toggleCoordinates() {
        var mag = $get("Coordinates_Panel");
        if (mag != null) {
            var wasHidden = (mag.style.visibility != "visible");
            toggleFloatingPanelVisibility('Coordinates_Panel');
            if (wasHidden) {
                lastToolUsed = "Coordinates";
                startCoordinates();
            }

        } else
            alert("Coordinates Tool is not available");
    }

    function toggleSelectionPanel(layerId, selectedTool) {
        var mag = $get("Selection_Panel");
        if (mag != null) {
            var wasHidden = (mag.style.visibility != "visible");
            toggleFloatingPanelVisibility('Selection_Panel');
            if (wasHidden) {
                m_currentSelectionToolbarTool = selectedTool;
                m_selectionLayer = layerId;
                startSelection(layerId);
            }

        } else
            alert("Selection Tool is not available");
    }

    function toggleScale() {
        var mag = $get("Scale_Panel");
        if (mag != null) {
            var wasHidden = (mag.style.visibility != "visible");
            toggleFloatingPanelVisibility('Scale_Panel');
            if (wasHidden) {
                startScale();
                
            }

        } else
            alert("Scale Tool is not available");
    }


    function launchCommand(toolbarCommand) {
        var toolbarName = 'Toolbar1';
        var toolbar = Toolbars[toolbarName];
        var toolbarComp = $find(toolbarName);

        var arg = 'eventArg=' + toolbarCommand + '&ControlID=' + toolbarName + '&ControlType=Toolbar&PageID=' + toolbar.pageID;
        toolbarComp.doCallback(arg, toolbarComp);

    }
    function launchTool(toolbarTool) {
        var toolbarName = 'Toolbar1';
        var toolbar = Toolbars[toolbarName];
        //var clientActionFunction = new Function('DragRectangle');
        //clientActionFunction.call(null);
        esriClientActionFunction = 'DragRectangle';
        window.setTimeout("eval(esriClientActionFunction);", 0);
        var currentToolField = $get(toolbar.currentToolField);
		currentToolField.value = toolbarTool;
        toolbar.selectTool();
        toolbar.refreshGroup();
    
    }

    function launchCallback(toolbarCommand) {
        var toolbarName = 'Map1';
        var toolbarComp = $find(toolbarName);
        var context = 'Map1'; 
        var message = 'eventArg=' + toolbarCommand + '&ControlID=' + toolbarName + '&ControlType=Map&PageID=';
        toolbarComp.doCallback(message, toolbarComp);
        //WebForm_DoCallback('__Page', message, processCallbackResult, context, postBackError, true);
        //eval(_callbackFunctionString);

    }

    function stepBackward() {
        var map = $find('Map1');
        if (map) { moreHistory = map.stepExtentHistory(-1); }    
        
    }
    function stepForward() {
        var map = $find('Map1');
        if (map) { moreHistory = map.stepExtentHistory(+1); }

    }


    function toggleUserComments() {
        var mag = $find('programmaticModalPopupBehavior');
        if (mag != null) {
            mag.show();
        } else
            alert("User Comments is not available");
    }
    function hideUserComments() {
        var mag = $find('programmaticModalPopupBehavior');
        if (mag != null) {
            mag.hide();
        } else
            alert("User Comments is not available");
    }
    function validateUserComments()
    {
        var mag = $find('programmaticModalPopupBehavior');
        var msg;
        
        msg = $get("theName");
        if(msg.value == "")
        {
            alert("Please enter your name before submitting this form.");
            //mag.show();       
            return false;     
        }        
        msg = $get("theReturnAddress");
        if(msg.value == "")
        {
            alert("Please enter your E-mail address before submitting this form.");
            //mag.show();       
            return false;     
        }        
        msg = $get("theDept");
        if(msg.value == "")
        {
            alert("Please enter your phone number before submitting this form.");
            //mag.show();       
            return false;     
        }
        msg = $get("theBodyText");
        if(msg.value == "")
        {
            alert("Please enter a comment before submitting this form.");
            //mag.show();       
            return false;     
        }
        
        
            mag.hide();
            return true;
        
    }
    
    function clearSelection() {
        var map = $find('Map1');
        var layercollection = map.get_layers();
        var layerCount = layercollection.get_layerCount();
        for(i=0;i<layerCount;i++) {
            var curLayer = layercollection.get_layer(i);
        }
        //remove highlight layer
        //layercollection.remove(highlightLayer);
    }


function toggleDivVisibility(divId) {
    var div = document.getElementById(divId);
    if (div!=null) {
        if (div.style.visibility != 'visible') {
            div.style.visibility= 'visible';
            div.style.display = 'block';
        }
        else {
            div.style.visibility= 'hidden';
            div.style.display = 'none';
        }
    }
}

var lastToolUsed;
function OnToolSelectHandler(sender, args) {
    if (args.name) {
        var mode = args.name;
        if (mode != "Measure" && lastToolUsed == "Measure") {
            toggleMeasure();
            closeMeasureToolbarTool('');
        }
        if (mode != "Coordinates" && lastToolUsed == "Coordinates") 
            closeCoordinatesToolbarTool();
        if (mode != "MapIdentify" && lastToolUsed == "MapIdentify") {
            if (arcgisIdentifyTool != null) 
                closeIdentifyPanel();
        }
        lastToolUsed = mode;
    }
}

function GetElementRectangle(element) {
    var rect = null;
    if (isIE)
        rect = element.getBoundingClientRect();
    else {
        var bounds = Sys.UI.DomElement.getBounds(element);
        rect = { "left": bounds.x, "top": bounds.y, "right": bounds.x + bounds.width, "bottom": bounds.y + bounds.height };
    }
    return rect;
}


 
    //ASSESSOR PARCEL DETAILS 

        function sendPropTax(theNum) {
	        var propTaxWin = window.open("","Candidates","toolbar=0,location=0,directories=0,status=0,menubar=1,scrollbars=1,resizable=1,copyhistory=0,width=440,height=445");
	        propTaxWin.location.href = "http://www.eproptax.saccounty.net/" //+theNum;
	        propTaxWin.focus();
        }

        function sendEffective() {
	        alert("In most instances, the effective year built is the year construction was completed.  A different year may be used to reflect changes to the property as a result of a major remodel or addition.");				
        }

        function printReport() {
		        window.print();
        }

        function sendLanduse(theNum) {
	        var landuseWin = window.open("","LandUse","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,copyhistory=0,width=435,height=445");
	        landuseWin.location.href = "LandUseCodes.aspx?Code="+theNum;
	        landuseWin.focus();
        }

        function launchHelp() {
	        var win = window.open("","Help","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,copyhistory=0,width=435,height=445");
	        win.location.href = "Help/index.aspx";
	        win.focus();
        }


        function pnInfo() {
	        alert ("Assessor's Parcel numbers follow a specific numeric format as follows: Book# (3 characters) - Page# (4 characters) - Parcel# (3 characters) - Sub-parcel# (4 characters).  Subparcel numbers are typically used to identify individual condominium or planned unit development (PUD) units.  Due to space limitations, the subparcel numbers are not displayed on the map.  In the event the selected parcel number is a condominium or PUD, only the master parcel, rather than the individual unit, will be displayed.  The selected parcel number will appear in green on the map without hyphens.");
        }


        function setValue(obj, attr, container) {
            try {
            if (container == null) {
                container = document;
            }
            var node = container.getElementById(attr);
            if (node == null) {
                alert("document node " + attr + " not found");
                return;
            }
            var val = obj[attr];
            if (val != null) {
                node.innerText = obj[attr];
            }
            else {
                node.innerText = "";
            }
        }
        catch (ex) {
            alert("setValue(" + obj + "," + attr + ") :" + ex.message);
        }
    }

    function formatDate(dt)
    {
        var ret="";
        if(dt.getMonth()==0 && dt.getDate()==1 && dt.getFullYear()==1) {
            ret = "";
        }
        else {
            ret =(dt.getMonth()+1) + "/" + dt.getDate() + "/" + dt.getFullYear();
        }
        return ret;
    }
    

    function ClickHereToPrint(id) {
        try {
            var oContent = document.getElementById(id).innerHTML;
            var consoleRef = window.open('', 'myconsole',
                     'width=350,height=250'
                   + ',menubar=0'
                   + ',toolbar=1'
                   + ',status=0'
                   + ',scrollbars=1'
                   + ',resizable=1');
            var oDoc = consoleRef.document;


            oDoc.write("<html><head></head>");
            oDoc.createStyleSheet("http://" + document.location.host + "/GISViewer/css/style.css");
            oDoc.write("<body onload='this.focus(); this.print();'>");
            oDoc.write(oContent);
            oDoc.write("</body>");
            oDoc.write("</html>");
            oDoc.close();

        }
        catch (e) {
            alert(e.message);
        }
    }
        


if (typeof (Sys) !== 'undefined') { Sys.Application.notifyScriptLoaded(); }