﻿/*****************************************************************
* Name:
*	multisession.js
*
* Purpose:
*	A function that attempts to capture multiple browsers being opened.
*
* Parameters:
*
* Created:
*	SR 02/28/2008
*
* History:
*   ~ 1 KB  07/02/2009 #104989 Modify getElementValue to avoid JS error on webservice logs
*   ~ 2 DC  08/10/2010 #137206 Set the cookie path correctly otherwise this doesn't work
*****************************************************************/

function enforceSingleSession() {
    var enforce = true;
    var cookiepath = getElementValue("CookiePath");

    try {
        if (window.opener) {
            var url = window.opener.window.location;
            if (window.opener.window.location.hostname == window.location.hostname
                    && window.opener.window.location.href.indexOf(window.location.hostname + cookiepath) != -1) {
                enforce = false;
            }
        }
    }
    catch (err) {
        //Just return the enforce value at the bottom
    }

    return enforce;
}

function getElementValue(element) {
    var value = "";

    if (document.getElementById) {
        if (document.getElementById(element)) {
            value = document.getElementById(element).innerHTML;
        }
    }
    else if (document.all) {
        if (document.all[element]) {
            value = document.all[element].innerHTML;
        }
    }
    else if (document.layers) {
        if (document.layers[element]) {
            value = document.layers[element].innerHTML;
        }
    }

    return value;
}

//---------------------------------------------------------------------
// This method increments the count of browser using the career section
// on the current session in the document's cookie.
// This code is inserted only when IE browser is used
//---------------------------------------------------------------------
function setMultiBrowserDetection() {
    var url = "";

    if (window.navigator.cookieEnabled && enforceSingleSession())
    {
        // The multibrowser problem doesn't occur where cookies are not available
        var cs_cnt = readCookie("cs_cnt");
        var cs_domain = readCookie("cs_domain");
        var cookiepath = getElementValue("CookiePath");

        if (cs_cnt == null || parseInt(cs_cnt) < 1) {
            cs_cnt = 1;
        }
        else {
            cs_cnt = parseInt(cs_cnt) + 1;
        }

        try {
            document.cookie = "cs_cnt=" + cs_cnt + "; path=" + cookiepath;
        }
        catch (err) {
            //Do nothing
        }

        // #182379 - iPad/iPhone issue when site is mixed SSL with HTTP
        // The unload event does not seem to be firing when going from http to ssl - so the cookie is not being updated
        // and the multisession error is being displayed when it should not be
        if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
            cs_cnt = 1;
        }

        if (cs_cnt > 1) {

            url = getElementValue("MultiSessionURL");

            // More than one browser is using the current session
            window.document.location = url;
        }
    }
}

//---------------------------------------------------------------------
// This method decrements the count of browser using the career section
// on the current session in the document's cookie.
// This code is inserted only when IE browser is used
//---------------------------------------------------------------------
function unsetMultiBrowserDetection() {
    if (window.navigator.cookieEnabled && enforceSingleSession())
    {
        // The multibrowser problem doesn't occur where cookies are not available
        var cs_cnt = readCookie("cs_cnt");
        var cs_domain = readCookie("cs_domain");
        var cookiepath = getElementValue("CookiePath");

        if (parseInt(cs_cnt) > 0) {
            cs_cnt = parseInt(cs_cnt) - 1;
        }
        else {
            cs_cnt = 0;
        }

        try {
            document.cookie = "cs_cnt=" + cs_cnt + "; path=" + cookiepath;
        }
        catch (err) {
            //Do nothing
        }
    }
}

function readCookie(name) {
    var nameEQ = name + '=';
    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, c.length);
        }

        if (c.indexOf(nameEQ) == 0) {
            return c.substring(nameEQ.length, c.length);
        }
    }

    return null;
}
/*
if (window.attachEvent) window.attachEvent("onload", setMultiBrowserDetection);
if (window.attachEvent) window.attachEvent("onunload", unsetMultiBrowserDetection);
if (window.addEventListener) window.addEventListener("load", setMultiBrowserDetection, false);
if (window.addEventListener) window.addEventListener("unload", unsetMultiBrowserDetection, false);
*/

/* Fix for some weird IE 9 issue */
if (window.addEventListener) {
    window.addEventListener("load", setMultiBrowserDetection, false);
    window.addEventListener("unload", unsetMultiBrowserDetection, false);
}
else if (window.attachEvent) {
    window.attachEvent("onload", setMultiBrowserDetection);
    window.attachEvent("onunload", unsetMultiBrowserDetection);
}

