﻿var sessionTimer;

function startSessionAlertTimer(millis)
{
    sessionTimer = setTimeout('sessionExpiryAlert();', millis);
}

function sessionExpiryAlert()
{
    alert("Your session will expire in one (1) minute, at which point you will be logged out automatically.");

    clearTimeout(sessionTimer);
}

function addLoadEvent(func)
{
    var oldonload = window.onload;
    if (typeof window.onload != 'function')
    {
        window.onload = func;
    } else
    {
        window.onload = function ()
        {
            if (oldonload)
            {
                oldonload();
            }
            func();
        }
    }
}

function addResizeEvent(func)
{
    var oldonresize = window.onresize;
    if (typeof window.onresize != 'function')
    {
        window.onresize = func;
    } else
    {
        window.onresize = function ()
        {
            if (oldonresize)
            {
                oldonresize();
            }
            func();
        }
    }
}

function getWinHeight()
{
    var myHeight = 600;

    if (typeof (window.innerWidth) == 'number')
    {
        //Non-IE
        myHeight = window.innerHeight;

        //alert("Non-IE");
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
    {
        //IE 6+ in 'standards compliant mode'
        myHeight = document.documentElement.clientHeight - 10;

        //alert("IE6+");
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight))
    {
        //IE 4 compatible
        myHeight = document.body.clientHeight;

        //alert("IE Generic");
    }

    return myHeight;
}

function UserName(o)
{
    var td = o.parentNode;
    var tr = td.parentNode;
    var child = tr.childNodes[0];
    return child.innerText;
}

function Enable(o)
{
    var elem = document.getElementById('ctl00$btnPurchase');
    elem.disabled = false;
    var hid = document.getElementById('ctl00$hdfOptionName');
    hid.value = o.id;
}

function Disable(o)
{
    o.disabled = true;
    var elem = document.getElementById('ctl00_cphMain_btnCancel');
    elem.disabled = true;
    elem = document.getElementById('ctl00_cphMain_lblConfirmation');
    elem.innerText = "Please wait while your request is being processed....";
}

function finalizeDate(sender)
{
    if (sender == null || sender.value.length == 0 || sender.value == "__/__/____" || sender.value == "FROM" || sender.value == "THRU")
        return;

    var msg = "";
    var dateComponents = new Array();
    var now = new Date();

    dateComponents = sender.value.split("/");

    var newDate = "";
    var originalDate = dateComponents[0].toString() + "/" + dateComponents[1].toString() + "/" + dateComponents[2].toString();

    // Check month... 
    if (parseInt(dateComponents[0].toString(), 10) == 0)
    {
        msg += "\n\nThe month portion of the date '" + originalDate + "' is '0' and will automatically be converted to '1'.";

        dateComponents[0] = "01";
    }
    else if (parseInt(dateComponents[0].toString(), 10) > 12)
    {
        msg += "\n\nThe month portion of the date '" + originalDate + "' is '" + parseInt(dateComponents[0].toString(), 10) +
            "' and will automatically be converted to '12'.";

        dateComponents[0] = "12";
    }

    // Check day...
    if (dateComponents[0] != new Date(sender.value).getMonth() + 1)
    {
        if (parseInt(dateComponents[1].toString(), 10) == 0)
        {
            // User entered "0" || "00" for the day... change to first day of month.
            dateComponents[1] = "01";

            msg += "\n\nThe day portion of the date '" + originalDate + "' is '0' and will automatically be converted to '1'.";
        }
        else
        {
            // Day supplied exceeds number of days in month supplied (i.e., 11/31/2008).  
            // Back day up to last day of supplied month (i.e., 11/30/2008).
            dateComponents[1] = getLastDayOfMonth(dateComponents[0], dateComponents[2]);

            msg += "\n\nThe day portion of the date '" + originalDate +
                "' exceeds the number of days in the month and will be rolled back to the last day of the month.";
        }
    }

    // Check year...
    var year = parseInt(dateComponents[2].toString(), 10);

    // If user enters 2-digit year, automatically convert it to 4-digit year
    // If the 2-digit year is > current 2-digit year, we roll back to 19xx
    // If the 2-digit year is between 0 and current 2-digit year, we use 20xx.
    if (dateComponents[2].toString().substring(0, 2) == "00")
    {
        var entered2DigitYear = 0;
        var current2DigitYear = now.getFullYear().toString().substring(2);

        if (dateComponents[2].toString().substring(2, 3) == "0")
        {
            entered2DigitYear = parseInt(dateComponents[2].toString().substring(3), 10);
        }
        else
        {
            entered2DigitYear = parseInt(dateComponents[2].toString().substring(2), 10);
        }

        if (entered2DigitYear > current2DigitYear)
        {
            if (entered2DigitYear.toString().length == 1)
            {
                year = (parseInt("190" + entered2DigitYear.toString(), 10));
            }
            else
            {
                year = (parseInt("19" + entered2DigitYear.toString(), 10));
            }
        }
        else
        {
            if (entered2DigitYear.toString().length == 1)
            {
                year = (parseInt("200" + entered2DigitYear.toString(), 10));
            }
            else
            {
                year = (parseInt("20" + entered2DigitYear.toString(), 10));
            }
        }

        msg += "\n\nThe year portion of the date '" + originalDate + "' is '" + dateComponents[2].toString() +
            "' and will automatically be converted to '" + year + "'.";

        dateComponents[2] = year;
    }

    // If year is outside acceptable bounds, return
    if (year < 1700 || year > now.getFullYear())
    {
        alert("\n\nThe date '" + originalDate + "' has an invalid year of '" + dateComponents[2].toString() + "'.\n\n" +
            "Valid years are from 1700 through " + now.getFullYear().toString() + ".");

        sender.focus();

        return;
    }

    newDate = dateComponents[0].toString() + "/" + dateComponents[1].toString() + "/" + dateComponents[2].toString();

    if (msg.length > 0)
    {
        msg += "\n\n\nThe new date is '" + newDate + "'.";
        //alert(msg);
    }

    sender.value = newDate;
}

// The month parameter is NOT zero-based (1 = January)
function getLastDayOfMonth(month, year)
{
    switch (parseInt(month, 10))
    {
        case 1:
            return 31;
        case 2:
            if (isLeapYear(year))
                return 29;
            else
                return 28;
        case 3:
            return 31;
        case 4:
            return 30;
        case 5:
            return 31;
        case 6:
            return 30;
        case 7:
            return 31;
        case 8:
            return 31;
        case 9:
            return 30;
        case 10:
            return 31;
        case 11:
            return 30;
        case 12:
            return 31;
    }
}

function isLeapYear(year)
{
    year = parseInt(year, 10);

    if (year % 4 == 0)
    {
        if (year % 100 != 0)
        {
            return true;
        }
        else
        {
            if (year % 400 == 0)
                return true;
            else
                return false;
        }
    }

    return false;
}

function getViewportDimensions()
{
    var intH = 0, intW = 0;

    if (self.innerHeight)
    {
        intH = window.innerHeight;
        intW = window.innerWidth;
    }
    else
    {
        intH = document.body.clientHeight;
        intW = document.body.clientWidth;
    }

    return {
        height: parseInt(intH, 10),
        width: parseInt(intW, 10)
    };
}

function centerElement(elem)
{
    var viewport = getViewportDimensions();

    var left = parseInt(((viewport.width - 100) / 2), 10);
    var top = parseInt(((viewport.height - 100) / 2), 10);

    elem.style.position = 'absolute';
    elem.style.left = left + 'px';
    elem.style.top = top + 'px';

    //viewport = left = top = elem = null;
}

function alertBrowserData()
{
    var txt = "Browser CodeName: " + navigator.appCodeName + "\n";
    txt += "Browser Name: " + navigator.appName + "\n";
    txt += "Browser Version: " + navigator.appVersion + "\n";
    txt += "Cookies Enabled: " + navigator.cookieEnabled + "\n";
    txt += "Platform: " + navigator.platform + "\n";
    txt += "User-agent header: " + navigator.userAgent;

    alert(txt);
}

function getBrowserAdjustedViewportHeight()
{
    var height = getViewportDimensions().height;
    var browserAlert = "";

    if (navigator.userAgent.indexOf("Chrome") > -1)
    {
        height -= 20;
        browserAlert = "Chrome";
    }
    else if (navigator.userAgent.indexOf("Safari") > -1)
    {
        height -= 20;
        browserAlert = "Safari";
    }
    else if (navigator.userAgent.indexOf("Opera") > -1)
    {
        height -= 10;
        browserAlert = "Opera";
    }
    else if (navigator.userAgent.indexOf("MSIE") > -1)
    {
        if (navigator.appVersion.indexOf("MSIE 10.") > -1)
        {
            browserAlert = "Internet Explorer 10.x";
        }
        else if (navigator.appVersion.indexOf("MSIE 9.") > -1)
        {
            browserAlert = "Internet Explorer 9.x";
        }
        else if (navigator.appVersion.indexOf("MSIE 8.") > -1)
        {
            browserAlert = "Internet Explorer 8.x";

            height += 15;
        }
        else if (navigator.appVersion.indexOf("MSIE 7.") > -1)
        {
            browserAlert = "Internet Explorer 7.x";
        }
        else
        {
            browserAlert = "Internet Explorer <= 6.x";
        }
    }
    else if (navigator.userAgent.indexOf("Firefox") > -1)
    {
        height -= 20;
        browserAlert = "Firefox";
    }
    else
    {
        height -= 20;
        browserAlert = "Unknown";
    }

    // Uncomment to ensure browser detection is working properly
    //alert(browserAlert + ": " + navigator.userAgent);

    return height;
}

function printThroughApplet(url, username, printJob)
{
    window.document.eSPA.printImage(url, username, printJob);
}

/**
    Use eSearch.Framework.UIUtils.SetFocus(Page, Control) instead
    of interacting with these two functions directly.
*/

// Sets focus to the specified element once the DOM is ready.
function setFocusAfterDOMReady(clientId)
{
    $(document).ready(function ()
    {
        try
        {
            //Use timeout so autocompleteextender has time to init
            setTimeout('$("#' + clientId + '").focus();', 100);
            //$("#" + clientId).focus();
        }
        catch (ex)
        {
        }
    });
}

// Set focus to a control after an async postback.
// More reliable than server-side ScriptManager.SetFocus
function setFocusAfterAsyncPostback(clientId)
{
    function EndRequestHandler(sender, args)
    {
        if (args.get_error() == undefined)
        {
            try
            {
                //Use timeout so autocompleteextender has time to init
                setTimeout('$("#' + clientId + '").focus();', 100);
                //$("#" + clientId).focus();
            }
            catch (ex)
            {
            }
        }
    }

    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
}

function updateCartUI()
{
    __doPostBack('ctl00_lbRefreshCart', '');
}
