﻿
window.onload = PageInit;
var timer;
var oTdCurrentTab = null;
var IsStop = false;
var wHeight;

function PageInit() {

    //Set the client Timezone Offset and insert it in to the cookie
    var timeOffset = new Date().getTimezoneOffset();
    document.cookie = "TimezoneOffset" + "=" + timeOffset + "; path=/";

    //SetHeight();
    setTimeout(handleUserSessions, 1000 * 18);

//    var oDDL = document.getElementById("ctl00_ddlLanguage");
//    oDDL.onchange = DDLLanguage_selectedIndexChanged;

//    var url = window.location.href;
//    if (url.indexOf('?') != -1) {
//        var selectedLanguage = url.split('?language=')[1];
//        for (var i = 0; i < oDDL.options.length; i++) {
//            if (oDDL.options[i].value == selectedLanguage) {
//                oDDL.selectedIndex = i;
//            }
//        }
//    }

    if (typeof window.InitPage == 'function') {
        // function exists, so we can now call it
        InitPage();
   }
}

function StopAnimation() {
    nlsUtil.StartAnimation(false);
}

//Set workspace height
function SetHeight() {

    //Geting browser workspace height
    wHeight = nlsUtil.GetWorkspaceHeight();

    if (wHeight < 600)
        wHeight = 600;
    var savedSpace = 111; //space needed by headers, footers etc'
    var contentPlace = document.getElementById("workPlace");
    contentPlace.style.height = wHeight;
}

//function DDLLanguage_selectedIndexChanged() {
//    var language = this.options(this.selectedIndex).value;
//    var url = window.location.href;
//    if (url.indexOf('?') != -1) {
//        url = url.split('?')[0];
//    }
//    window.location.href = url + "?language=" + language;
//}


// ---------------------------------------------------------------------------------------------------------------
// User session Ajax methods
//----------------------------------------------------------------------------------------------------------------

var AjaxErrorCount = 0;

function handleUserSessions() {
    if (typeof oRequest == "undefined")
        oRequest = nlsAjax.createXMLHttp();
    else if (oRequest.readyState != 0)
        oRequest.abort();
    nlsAjax.sendGetRequest(oRequest, true, "../../AjaxHandlers/MainPageHandler.ashx?m=CheckUserSession", SecurityAjaxResponse);
}

function SecurityAjaxResponse() {

    if (oRequest.readyState == 4) {
        if (oRequest.status == 200) {
            AjaxErrorCount = 0;
            handleSessionCheckResponse(oRequest.responseText);
        }
        else {
            AjaxErrorCount++;
        }
    }
    else {
        AjaxErrorCount++;
    }

    if (AjaxErrorCount > 4) {
        try {
            alert(NoAjaxConnection);
        }
        catch (exception) { }
        finally {
            transferToLoginPage();
        }
    }
}

function handleSessionCheckResponse(response) {

    switch (response) {

        case "AnotherSession":
            {
                AjaxErrorCount = 0;
                try {                   
                    alert(SessionDisconnctdAnotherUser);
                }
                catch (exception) { }
                finally {
                    transferToLoginPage();
                }
                break;
            }

        case "ok":
            {
                // OK - Check again later
                setTimeout(handleUserSessions, 1000 * 40);
                break;
            }

        case "error":
            {
                setTimeout(handleUserSessions, 1000 * 20);
                AjaxErrorCount++;
                break;
            }
    }
}

function transferToLoginPage() {
    if (typeof oRequest != "undefined" && oRequest != null) {
        oRequest.abort();
        oRequest = null;
    }

    // Remove the "onbeforeunload" event from the body.
    var oBody = document.getElementsByTagName("body")[0];
    oBody.onbeforeunload = null;
    window.location = "Login.aspx";
}

