/* This script must be loaded at the bottom of the page */

function TrapEnterKey(e)
{
    /* 
        Use this function onkeypress to stop form submission
        when the enter key is pressed in a text box.
     */
    if (!e) e = window.event;
    return (isCR(e) == false);
}

function isCR (myEvent) {
    if (myEvent.keyCode == 13) {
        return true;
    } else {
        return false;
    }
}

/* Cookie helpers */
function createCookie(name,value,days,domain) 
{
    if (days) 
    {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    if (domain) {
        domain = "; domain=" + domain;
    }
    else domain = "";
    document.cookie = name+"="+value+expires+domain+"; path=/";
}

function readCookie(name) 
{
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
	    var c = ca[i];
	    while (c.charAt(0)==' ') c = c.substring(1,c.length);
	    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

/* Left Panel Hide/Show */
var LeftPanel = new function LeftPanelType() {
    
    var div;
    var container;
    var navcontent;
    var currentwidth;
    var showhidespeed = 10;
    
    this.HideShowNav = function(obj) {
        div.style.overflow = "hidden";
        var show = (navcontent.style.visibility == "hidden");
        if (show) 
        {
            currentwidth = 20;
            navcontent.style.visibility = "visible";
            obj.src = obj.src.replace("expand.gif","contract.gif");
            obj.title = "Collapse Navigation Panel";
            window.setTimeout("LeftPanel.ShowNav()",showhidespeed);
            // Set cookie to show nav
            createCookie("navHidden","0",30);
        }
        else 
        {
            currentwidth = 198;
            obj.src = obj.src.replace("contract.gif","expand.gif");
            obj.title = "Expand Navigation Panel";
            window.setTimeout("LeftPanel.HideNav()",showhidespeed);
            // Set cookie to hide nav
            createCookie("navHidden","1",30);
        }
        div.style.overflow = "";
        return false;
    };
    
    this.HideNav = function() {
        currentwidth-=25;
        if (currentwidth<20) currentwidth=20;
        div.style.width = currentwidth+"px";
        div.parentNode.style.width = currentwidth+"px";
        container.style.backgroundPosition = ((198-currentwidth)*-1)+"px 0px";
        if (currentwidth>20) window.setTimeout("LeftPanel.HideNav()",showhidespeed);
        else navcontent.style.visibility = "hidden";    
    };
    
    this.ShowNav = function() {
        currentwidth+=25;
        if (currentwidth>198) currentwidth=198;
        div.style.width = currentwidth+"px";
        div.parentNode.style.width = currentwidth+"px";
        container.style.backgroundPosition = ((198-currentwidth)*-1)+"px 0px";
        if (currentwidth<198) window.setTimeout("LeftPanel.ShowNav()",showhidespeed);
    };
    
    this.StartNav = function() {
        var obj = document.getElementById("imgExpando");
        div = obj.parentNode.parentNode;
        container = document.getElementById("main_content_1");
        navcontent = document.getElementById("navcontent");
        if (readCookie("navHidden")=="1")
        {
            container.style.backgroundPosition = "-178px 0px";
            div.style.width = "20px";
            div.parentNode.style.width = "20px";
            navcontent.style.visibility = "hidden";
            obj.src = obj.src.replace("contract.gif","expand.gif");
            obj.title = "Expand Navigation Panel";
        }    
    };
};
$addHandler(window,"load",LeftPanel.StartNav);

function getViewPort() {
    var x = 0;
    var y = 0;
    if (document.documentElement && typeof(document.documentElement.clientHeight) != 'undefined') {
        x = document.documentElement.clientWidth;
        y = document.documentElement.clientHeight;
    }
    else if (self.innerHeight) {
        x = self.innerWidth;
        y = self.innerHeight;
    }
    else if (typeof(document.body.clientHeight) != 'undefined') {
        x = document.body.clientWidth;
        y = document.body.clientHeight;
    }
    return {width:x, height:y};
}

function getScrollOffset() {
    var x = 0;
    var y = 0;
    if (document.documentElement && typeof(document.documentElement.scrollTop) != 'undefined') {
        x = document.documentElement.scrollLeft;
        y = document.documentElement.scrollTop;
    }
    else if (self.pageYOffset) {
        x = self.pageXOffset;
        y = self.pageYOffset;
    }
    else if (typeof(document.body.scrollTop) != 'undefined') {
        x = document.body.scrollLeft;
        y = document.body.scrollTop;
    }
    return {scrollLeft: x, scrollTop: y};
}

// Global UpdateProgress stuff
var GlobalProgress = new function GlobalProgressType() {
    
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    var gp = null;
    var gpSize = null;
    function BeginRequest(sender, args) {
        if (!gp) {
            gp = $get('GlobalProgressChild').parentNode;
            gp.style.position = "absolute";
        }
        if (!gpSize) {
            // Shenanigans to get size of box
            Sys.UI.DomElement.setLocation(gp, -1000, 0);
            gp.style.display = "block";
            gpSize = {height: gp.offsetHeight, width: gp.offsetWidth};
            gp.style.display = "none";
        }
        var viewport = getViewPort();
        var scroll = getScrollOffset();
        // Center
        //var x = Math.round(viewport.width / 2 + scroll.scrollLeft - gp.offsetWidth / 2);
        //var y = Math.round(viewport.height / 2 + scroll.scrollTop - gp.offsetHeight / 2);
        // Left, Center
        //var x = scroll.scrollLeft + 2;
        //var y = Math.round(viewport.height / 2 + scroll.scrollTop - gp.offsetHeight / 2);
        // Left, Bottom
        var x = scroll.scrollLeft + 2;
        var y = viewport.height + scroll.scrollTop - gpSize.height - 2;
        Sys.UI.DomElement.setLocation(gp, x, y);
    }
    
    function InitializeRequest(sender, args) {
       if (prm.get_isInAsyncPostBack()) { args.set_cancel(true); }
    }
    
    this.AbortPostBack = function() {
        if (prm.get_isInAsyncPostBack()) { prm.abortPostBack(); }
    }
    
    prm.add_initializeRequest(InitializeRequest);
    prm.add_beginRequest(BeginRequest);

};

function WarnMaintenance() {
    var cookie = readCookie("MAINTENANCE");
    if (cookie) return;
    if ((typeof SERVER_MAINTENANCE == 'undefined') || window.location.hostname.match("dfg\.ca\.gov$") != "dfg.ca.gov") return;
    // 15 minutes before outage, give notice
    if (!(SERVER_MAINTENANCE.MSToStart==0 && SERVER_MAINTENANCE.MSToEnd==0) && SERVER_MAINTENANCE.MSToStart <= 900000) {
        var secs = Math.floor((Math.abs(SERVER_MAINTENANCE.MSToStart) / 1000));
        var mins = Math.floor((secs) / 60);
        secs %= 60;
        var msg = "WARNING: Routine website maintenance ";
        if (SERVER_MAINTENANCE.MSToStart > 0) {
            msg += "is scheduled to begin in approximately " + mins + " minute(s) and "
                + secs + " second(s).";
        }
        else {
            msg += "was scheduled to begin approximately " + mins + " minute(s) and "
                + secs + " second(s) ago.  The website may become temporarily unavailable at any time.";
        }
        msg += "\n\n** Your work may be interrupted and changes may be lost! **";
        alert(msg);
        createCookie("MAINTENANCE", "NRM Website Maintenance Warning",null,".dfg.ca.gov");
    }
    else if (SERVER_MAINTENANCE.MSToStart>0) {
        // Check again in 5 minutes
        SERVER_MAINTENANCE.MSToStart -= 300000;
        SERVER_MAINTENANCE.MSToEnd -= 30000;
        setTimeout("WarnMaintenance()", 300000);
    }
}
setTimeout("WarnMaintenance()", 3000); // 3 second delay

// Session timeout warning functions
function LaunchTimeout(url, finaltimeout) {
    var startTime = new Date(); 
    if (finaltimeout!=0) alert('For security reasons your session will expire in 5 minutues due to inactivity.  Please press OK to continue with your session.');
    var endTime = new Date();
    if ((endTime-startTime)<300000) 
    {
        try { AuthKeepAlive(); } 
        catch (err) { window.location = url; } 
    } else window.location = url; 
}

function AuthKeepAliveCallback(arg, context) {
    if (typeof (arg) != 'undefined' && arg != null && arg.length>0) {
        var args = arg.split(',');
        var launchtimeout = parseInt(args[0]);
        var finaltimeout = parseInt(args[1]);
        keepAliveTimeout = setTimeout("LaunchTimeout('" + context + "',"+finaltimeout+")", launchtimeout);
    }
    else window.location = context;
}

function AuthKeepAliveError(arg,context){ window.location = context; }
