﻿
// Set bNoTimeout = true on any page to disable the timout popups

var inactivityTimeout = 10*60; //The inactivity popup will appear after 10 minutes
var popupTimeout = 60; //The timeout popup will automatically proceed after 60 seconds

var TimerIDArray = new Array()
var TimerIDCount = 0;

function addTimeout(tid) {
    TimerIDArray[TimerIDCount++] = tid;
}
function clearAllTimeouts() {
    for (i = 0 ; i < TimerIDCount ; i++)
    {
        window.clearTimeout(TimerIDArray[i]);
    }
    TimerIDArray.length = 0;
    TimerIDCount = 0;
}
function clearEvents() {
    if (document.layers)
        document.releaseEvents(Event.MOUSEDDOWN | Event.KEYUP);
    document.onmousedown = document.onkeyup = document.onmousemove = null;
}

function inactivityPopup() {
    clearEvents();
    clearAllTimeouts();
    var timeLeft = popupTimeout;
    var $dialog = $('<div title="Need more time?"></div>')
        .html('<p>Do you need more time?</p><p>You will be logged out and your<br />cart will be emptied in <span class="count">' + timeLeft + ' seconds</span>.</p>')
        .dialog({
            dialogClass: 'yesno',
            bgiframe: true,
            autoOpen: false,
            height: 180,
            width: 340,
            modal: true,
            buttons: {
                'Start Over': function() { location.href='Logout.aspx'; },
                'Continue': function() { clearAllTimeouts(); setInactivityTimer(inactivityTimeout*1000,"inactivityPopup();",false); $dialog.dialog('close'); }
            },
            resizable: false,
            draggable: false
        })
        .dialog('open');
    addTimeout(setTimeout('reduceNumber(' + (timeLeft) + ')',1000));
}
function reduceNumber(timeLeft) {
    timeLeft--;
    clearAllTimeouts();
    if (timeLeft<0) {
        location.href="Logout.aspx?src=timeout";
    } else {
        $(".count").text(timeLeft + " second" + (timeLeft==1?"":"s"));
        addTimeout(setTimeout('reduceNumber(' + timeLeft + ')',1000));
    }
}


var tid, time, action;
function setInactivityTimer (time, action, repeat) {
    window.time = time;
    window.action = action;
    window.repeat = repeat;
    clearAllTimeouts();
    if (document.layers)
        document.captureEvents(Event.MOUSEDOWN | Event.KEYUP);
    document.onmousemove = document.onkeyup =
        function (evt) {
            setInactivityTimer(window.time, window.action, window.repeat);
            return true;
        };
    if (repeat)
        action += '; setInactivityTimer(' + time + ', "' + action + '", ' + repeat + ');';
    else 
        action += '; clearEvents();';
    addTimeout(setTimeout(action, time));
}

$(document).ready(function() {
    //Check to see if bNoTimeout is false (or not set)... use try/catch to catch a not set exception
    try {
        if (!bNoTimeout) {
            setInactivityTimer(inactivityTimeout*1000,"inactivityPopup();",false);
        }
    } catch (e) {
        setInactivityTimer(inactivityTimeout*1000,"inactivityPopup();",false);
    }
});

function doPopup(title, message) {
    var $dialog = $('<div title="' + title + '"></div>')
        .html(message)
        .dialog({
            bgiframe: true,
            autoOpen: false,
            height: 180,
            width: 340,
            modal: true,
            buttons: {
                'Ok':  function() { $dialog.dialog('close'); } 
            },
            resizable: false,
            draggable: false
        })
        .dialog('open');
}

function doLoading(container, width, height) {
    $(container).hide();
    $('#loading').show()
}
