﻿/* Hook up Application event handlers. */
var app = Sys.Application;
app.add_init(ApplicationInitTM);

var tmSestm = 0;
var warnedSestm = false;

function ApplicationInitTM(sender)
{
    var prm = Sys.WebForms.PageRequestManager.getInstance();
  
    if (!prm.get_isInAsyncPostBack())
    {
        prm.add_beginRequest(BeginRequestTM);
        prm.add_endRequest(EndRequestTM);
        if ((window.location.toString()).indexOf("../../SignIn/Home.aspx", 0) >= 0)
            warnedSestm = true;
            
        startSesTm();
    }
}

function BeginRequestTM(sender, args)
{
    stopSesTm();
}

function EndRequestTM(sender, args)
{
    startSesTm();
}

/* timer funcs */

function startSesTm()
{
    if (tmSestm == 0)
    {
        //tmSestm = setTimeout(endAppSession, 1140000); // 19 mins 0 secs
        tmSestm = setTimeout(endAppSession, 1740000); // 29 mins 0 secs 
    }
}

function stopSesTm()
{
    if (tmSestm != 0)
    {
        clearTimeout(tmSestm);
        tmSestm = 0;
    }
}

function endAppSession()
{
    if (!warnedSestm)
    {
        showPopupStm('Your session will expire in 60 seconds due to inactivity.');
        tmSestm = setTimeout(endAppSession, 60000);
        warnedSestm = true;
    }
    else
    {
        window.location = '../../SignIn/Home.aspx?ErrorID=1';
    }
}

/* done with timer */

/* error message windows */

var errtmh_st = 0;
var errwin_st = null;

function showPopupStm(msg)
{
    if (errwin_st == null || errwin_st == "undefined")
    {
        errwin_st = dhtmlwindow.open("sestmalert", "inline", "<div style=\"float: left;\"><img src=\"../../Images/Shared/yellowexclamationmark.gif\" width=\"32\" height=\"32\"" +
                            " align=\"middle\" /></div><div style=\"float: left; margin-left: 10px; width: 290px;\">" + msg + "</div>" +
                            "<div style=\"float: left; text-align: center; width: 100%; position: relative; top: 7px\">" +
                            "<input type=\"button\" value=\"Ok\" onclick=\"hidePopupStm()\"></div>", 
                            "Session Warning", "width=350px,height=70px,resize=0,scrolling=0,center=1");
        errwin_st.onclose = function(){ errwin_st = null; if (errtmh_st != 0 && errtmh_st != "undefined") { clearTimeout(errtmh_st); errtmh_st = 0; } return true; }
    }
    else
    {
        errwin_st.show();
    }

    if (errtmh_st == null || errtmh_st == 0 || errtmh_st.toString() == "undefined")
        errtmh_st = setTimeout(hidePopupStm, 4000);
}

function hidePopupStm()
{
    errtmh_st = 0;
    if (errwin_st != null && errwin_st != "undefined")
        errwin_st.hide();
}

/* done with error messages */

/* end of script */
if (typeof(Sys) !== "undefined") {
    Sys.Application.notifyScriptLoaded();
}

  
