﻿var DisallowAnotherLoginTillSlidingMinsVal = 0.5; // should be less than SystemSettings.DofeUserDisallowAnotherLoginTillSlidingValue
var DofeServiceApiPath = "http://10.10.30.250:9010/DOFEService.svc/";
var CurrentUserType = "";
var snId = "";
var disablLogoutMessage = false;

var DashBoardView = {
    init: function () {

    },
    config: {
        isPostBack: false,
        async: false,
        cache: false,
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        data: '{}',
        dataType: 'json',
        crossDomain: true,
        baseURL: "",
        method: "",
        url: "",
        ajaxCallMode: "",
        global: false // makes sure ajaxStart doesn't get called
    },

    Logout: function () {
        //alert(DofeServiceApiPath);
        ///
        DashBoardView.config.method = "Logout";
        DashBoardView.config.url = '/Modules/Dofe/UserProfileManagement/UserProfileManagement.asmx/' + DashBoardView.config.method;
        DashBoardView.config.data = JSON2.stringify({ portalID: SageFramePortalID });
        DashBoardView.config.ajaxCallMode = DashBoardView.Redirect;
        DashBoardView.ajaxCall(DashBoardView.config);
    },
    Redirect: function () {
        //window.location.href = SageFrameHostURL + "/sf/sflogin.aspx";

    },
    ajaxSuccess: function (data) {
    },
    ajaxFailure: function () {
    },

    ajaxCall: function (config) {
        $.ajax({
            type: DashBoardView.config.type,
            contentType: DashBoardView.config.contentType,
            async: DashBoardView.config.async,
            cache: DashBoardView.config.cache,
            url: DashBoardView.config.url,
            data: DashBoardView.config.data,
            dataType: DashBoardView.config.dataType,
            success: DashBoardView.config.ajaxCallMode,
            error: DashBoardView.ajaxFailure,
            global: DashBoardView.config.global
        });
    }
};

function KeepAlive() {
    theHub.server.isSessionValid(snId).done(function (result) {
        if (result === false) {
            window.location.href = "/sf/sfLogin.aspx?ReturnUrl=" + window.location.href;
        }
        isAlive = result;
    });
}

function DisplaySessionTimeoutMessage() {
    $("body").append("<p>Your session has expired due to inactivity.</p>");
    $("#divPopup").remove();
    $("body").addClass("session-active");
    var html = '';
    html += '<div id="divPopup" style="display: block;" class="popupbox masterForm sf-modal session-popup">';
    html += '<div class="sf-modal-dialog">';
    html += '<div class="sf-modal-content">';
    html += '<div class="sf-modal-header">';
    html += '<h4 class="sf-modal-title">Session Expiration Warning</h4>';
    html += '</div>';
    html += '<div class="sf-modal-body">';
    html += '<p>Your session has expired.<br />Please click the button below to goto the login page or reload page to continue if you\'ve logged in from another tab.</p>';
    html += '<div>';
    html += '<button class="sfBtn btn-primary" onclick="window.location.href=\'/Common-Login.aspx?ReturnUrl=' + window.location.href + '\'">Refresh Page</button>';
    html += '</div>';
    html += '</div>';
    html += '</div>';
    html += '</div>';
    html += '</div>';
    html += '<div id="fade" style="display: block;">';
    html += '</div>';

    $("body").append(html);
    $('title').html('Session Expiration Warning');
}

function DisplayLoggedOutMessage() {
    if (!disablLogoutMessage) {
        $("body").append("<p>You have logged out from the current session.</p>");
        $("#divPopup").remove();
        $("body").addClass("session-active");
        var html = '';
        html += '<div id="divPopup" style="display: block;" class="popupbox masterForm sf-modal session-popup">';
        html += '<div class="sf-modal-dialog">';
        html += '<div class="sf-modal-content">';
        html += '<div class="sf-modal-header">';
        html += '<h4 class="sf-modal-title">Logged Out Warning</h4>';
        html += '</div>';
        html += '<div class="sf-modal-body">';
        html += '<p>You have been logged out from the current session.<br />Please click the button below to goto the login page or reload page to continue if you\'ve logged in from another tab.</p>';
        html += '<div>';
        html += '<button class="sfBtn btn-primary" onclick="window.location.href=\'/Common-Login.aspx?ReturnUrl=' + window.location.href + '\'">Refresh Page</button>';
        html += '</div>';
        html += '</div>';
        html += '</div>';
        html += '</div>';
        html += '</div>';

        $("body").append(html);
        $('title').html('Logged Out Warning');
    }
}

function DisplayDuplicateLogin() {
    $("body").append("<p>You have logged in somewhere else.</p>");
    $("#divPopup").remove();
    $("body").addClass("session-active");
    var html = '';
    html += '<div id="divPopup" style="display: block;" class="popupbox masterForm sf-modal session-popup">';
    html += '<div class="sf-modal-dialog">';
    html += '<div class="sf-modal-content">';
    html += '<div class="sf-modal-header">';
    html += '<h4 class="sf-modal-title">Duplicate Login Warning</h4>';
    html += '</div>';
    html += '<div class="sf-modal-body">';
    html += '<p>You have been logged out from the current session.<br />Please click the button below to goto the login page or reload page to continue if you\'ve logged in from another tab.</p>';
    html += '<div>';
    html += '<button class="sfBtn btn-primary" onclick="window.location.href=\'/Common-Login.aspx?ReturnUrl=' + window.location.href + '\'">Refresh Page</button>';
    html += '</div>';
    html += '</div>';
    html += '</div>';
    html += '</div>';
    html += '</div>';

    $("body").append(html);
    $('title').html('Duplicate Login Warning');
}

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

function checkCookie() {
    var user = getCookie("username");
    if (user != "") {
        alert("Welcome again " + user);
    } else {
        user = prompt("Please enter your name:", "");
        if (user != "" && user != null) {
            setCookie("username", user, 365);
        }
    }
}

(function ($) {
    DashBoardView.init();
})(jQuery);

//if (SageFrameUserName != 'anonymoususer' && SageFrameUserName != 'superuser') {
//    var idleTimer = null;
//    var idleState = false;
//    var idleMin = 15; //in minutes
//    var idleWait = 1000 * 60 * idleMin;
//    var d = new Date();
//    //setCookie("idleTimeout", d.getTime() + idleWait, d.getTime() + idleWait * 3600);
//    setCookie("idleTimebit", 0, d.getTime() + idleWait * 3600);

//    (function ($) {
//        $(document).ready(function () {
//            //clearTimeout(idleTimer);
//            $('*').bind('mousemove keydown scroll', function () {
//                setCookie("idleTimebit", 1, d.getTime() + idleWait * 3600);
//                if (idleState == true) {
//                    $("body").append("<p>Welcome Back.</p>");
//                }

//                //idleTimer = setTimeout(function () {

//                //    var timeOut = getCookie("idleTimeout");
//                //    var currentTime = new Date();
//                //    var timeDiff = timeOut - currentTime.getTime();

//                //    if (timeDiff > 0) {

//                //    }
//                //    else {
//                //        alert(timeOut + "\n" + currentTime.getTime() + "\n" + timeDiff);
//                //        $("body").append("<p>You've been idle for " + idleWait / 1000 + " seconds.</p>");
//                //        $("#divPopup").remove();
//                //        $("body").addClass("session-active");
//                //        $("body").append('<div id="divPopup" style="display: block;" class="popupbox masterForm sf-modal session-popup"><div class="sf-modal-dialog"><div class="sf-modal-content"><div class="sf-modal-header"><h4 class="sf-modal-title">Session Expiration Warning</h4></div> <!--//.sf-modal-header--><div class="sf-modal-body"><p>Session has been expired. Please refresh your page and login again.</p><div><button id="login" class="sfBtn btn btn-primary">Refresh and login</button></div></div></div> <!--//.sf-modal-body--></div> <!--//.sf-modal-content--></div> <!--//.sf-modal-dialog--></div> <!--//.sf-modal--><div id="fade" style="display: block;"></div>');
//                //        //$('#divPopup').draggable();
//                //        DashBoardView.Logout();
//                //        $('body').on('click', '#login', function () {
//                //            $("#divPopup").remove();
//                //            window.location.href = SageFrameHostURL + "/sf/sflogin.aspx";
//                //        });
//                //        $('title').html('Session Expiration Warning');

//                //        idleState = true;
//                //    }
//                //}, idleWait);

//                idleState = false;


//            });

//            idleTimer = setInterval(function () {
//                var idleBit = getCookie("idleTimebit");
//                //alert(idleBit);
//                if (idleBit == 1) {
//                    setCookie("idleTimebit", 0, d.getTime() + idleWait * 3600);
//                }
//                else {
//                    //if (idleState)
//                    //    clearTimeout(idleTimer);
//                    $("body").append("<p>You've been idle for " + idleWait / 1000 + " seconds.</p>");
//                    $("#divPopup").remove();
//                    $("body").addClass("session-active");
//                    $("body").append('<div id="divPopup" style="display: block;" class="popupbox masterForm sf-modal session-popup"><div class="sf-modal-dialog"><div class="sf-modal-content"><div class="sf-modal-header"><h4 class="sf-modal-title">Session Expiration Warning</h4></div> <!--//.sf-modal-header--><div class="sf-modal-body"><p>Session has been expired. Please refresh your page and login again.</p><div><button id="login" class="sfBtn btn btn-primary">Refresh and login</button></div></div></div> <!--//.sf-modal-body--></div> <!--//.sf-modal-content--></div> <!--//.sf-modal-dialog--></div> <!--//.sf-modal--><div id="fade" style="display: block;"></div>');
//                    //$('#divPopup').draggable();
//                    DashBoardView.Logout();
//                    $('body').on('click', '#login', function () {
//                        $("#divPopup").remove();
//                        window.location.href = SageFrameHostURL + "/sf/sflogin.aspx";
//                    });
//                    $('title').html('Session Expiration Warning');
//                    clearTimeout(idleTimer);
//                    idleState = true;
//                }
//            }, idleWait);

//            $("body").trigger("mousemove");

//        });
//    })(jQuery)
//}