// 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;


// First call to popup window
$(document).ready(function() {
    window.setTimeout("pop_init()", SESSION_TIME);
});

// Main popup window handler
function pop_init() {
    // show modal div
    $("html").css("overflow", "hidden");
    $("body").append("<div id='popup_overlay'></div><div id='popup_window'></div>");
    //$("#popup_overlay").click(popup_remove);  // removed to make sure user clicks button to continue session.
    $("#popup_overlay").addClass("popup_overlayBG");
    $("#popup_overlay").fadeIn("slow");

    // build warning box
    $("#popup_window").append("<h1>Warning</h1>");
    $("#popup_window").append("<p id='popup_message'>Your session is about to expire.  Please click the button below to continue working without losing your session.</p>");
    $("#popup_window").append("<div class='buttons'><center><button id='continue' class='positive' type='submit'><img src='images/tick.png' alt=''/> Continue Working</button></center></div>");

    // attach action to button
    $("#continue").click(session_refresh);

    // display warning window
    popup_position(400, 300);
    $("#popup_window").css({ display: "block" }); //for safari using css instead of show
    $("#continue").focus();
    $("#continue").blur();
    
    // set pop-up timeout
    SESSION_ALIVE = 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") {
                $(".buttons").hide();
                $("#popup_message").html("<center><br />" + msg + "<br /></center>");
                window.setTimeout("popup_remove()", 3000);
            }
            else
                window.location.href = "login.asp";
        }
    });
}

// remove all added objects and restart timer
function popup_remove() {
    $("#popup_window").fadeOut("fast", function() { $('#popup_window,#popup_overlay').trigger("unload").unbind().remove(); });
    //if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
        $("body", "html").css({ height: "auto", width: "auto" });
        $("html").css("overflow", "");
    //}

    window.setTimeout("pop_init()", SESSION_TIME);
}

// determine size of popup window and adjust position
function popup_position(P_WIDTH, P_HEIGHT) {
    $("#popup_window").css({ marginLeft: '-' + parseInt((P_WIDTH / 2), 10) + 'px', width: P_WIDTH + 'px' });
}

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
    });
}