// Global variable used to set repeated session timeout calls.
//    Set SESSION_TIME to (# seconds) * 1000 for the total milliseconds
//    to wait before sending the popup windows to the user.
//    You should give the user two minutes before the session times out to respond,
//    e.g. for a 15 minute timeout, set it to 13 * 60 * 1000 = 780000 (~ 13 minutes).
var SESSION_TIME = 780000;

// Global variable used to test if the session has expired.  Set to true until popup
//    is shown to the user.  Reset when they click Continue.
var SESSION_ALIVE = true;
var RESET_TIMER = false;


// First call to popup window
$(document).ready(function () {
    RESET_TIMER = true;
    window.setTimeout("pop_init()", SESSION_TIME);
});

// Main popup window handler
function pop_init() {
    modal_dialog("Warning", "<span id=\"popup_message\" style=\"font: normal 10pt Arial; \">Your session is about to expire.  Please click the button below to continue working without losing your session.</span>", 400, 100, "Continue Working", session_refresh, false, true, "#timeout-dialog", 1000, popup_remove);
    
    // set pop-up timeout
    SESSION_ALIVE = false;
    RESET_TIMER = false;
    window.setTimeout("popup_expired()", 115000);
}

// session ajax call from button click
function session_refresh() {
    SESSION_ALIVE = true;
    $.ajax({
        cache: false,
        dataType: "text",
        url: "SessionKeepAlive.asp",
        success: function (msg) {
            if (msg != "Failure") {
                $("#timeout-dialog .ui-dialog-buttonpane").hide();
                $("#popup_message").html("<center><br />" + msg + "<br /></center>");
                window.setTimeout("popup_remove()", 3000);
            }
            else
                window.location.href = "login.asp";
        }
    });

    return false;
}

// remove all added objects and restart timer
function popup_remove() {
    if (!RESET_TIMER) {
        SESSION_ALIVE = true;
        RESET_TIMER = true;
        $("#timeout-dialog").dialog("close");
        window.setTimeout("pop_init()", SESSION_TIME);
    }
}

function popup_expired() {
    if (!SESSION_ALIVE)
        window.location.href = "login.asp";
}
function replaceBadCharacters() {
    // Get all textboxes and text area controls
    $('input[type=text], textarea').each(function() {
        var txtobj = $('#' + this.id);
        txtobj.attr("value", txtobj.attr("value").replace(new RegExp(String.fromCharCode(8216), "g"), String.fromCharCode(39))); // Microsoft Word left-side apostrophe
        txtobj.attr("value", txtobj.attr("value").replace(new RegExp(String.fromCharCode(8217), "g"), String.fromCharCode(39))); // Microsoft Word right-side apostrophe
        txtobj.attr("value", txtobj.attr("value").replace(new RegExp(String.fromCharCode(8220), "g"), "'")); // Microsoft Word left-side quotes
        txtobj.attr("value", txtobj.attr("value").replace(new RegExp(String.fromCharCode(8221), "g"), "'")); // Microsoft Word right-side quotes
    });
}
