﻿/*****************************************************************
* Name:
*	multisession.js
*
* Purpose:
*	A function that attempts to capture multiple browsers being opened.
*	
* Parameters:
*
* Created: 
*	SR 02/28/2008
*
* History:
*****************************************************************/
function enforceSingleSession()
{
    var pass = true;
    
    if (window.navigator.cookieEnabled)
    {
        if (window.navigator.appName == "Microsoft Internet Explorer")
        {
            if (window.self.opener && !window.document.referrer)
            {
                pass = false;
            }
        }
        else if (window.navigator.appName == "Netscape")
        {
            if (window.self.opener)
            {
                pass = false;
            }
        }
    }
    
    return pass;
}

function getElementValue(element)
{
    var value = "";
    
    if (document.getElementById)
    {
        value = document.getElementById(element).innerHTML;
    }
    else if (document.all)
    {
        value = document.all[element].innerHTML;
    }
    else if (document.layers)
    {
        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 = "";
    var cookiepath = "";

    if (window.navigator.cookieEnabled && !self.opener)
//    if (enforceSingleSession())
    {
        // The multibrowser problem doesn't occur where cookies are not available
        var cs_cnt = -1;
        var index = document.cookie.indexOf("cs_cnt");
        var countbegin = -1;
        var countend = -1;
        if (index == -1)
        {
            cs_cnt = 1;
        }
        else
        {
            countbegin = (document.cookie.indexOf("=", index) + 1);
            countend = document.cookie.indexOf(";", index);
            if (countend == -1)
            {
                countend = document.cookie.length;
                cs_cnt = eval(document.cookie.substring(countbegin, countend));
            }
            else
            {
                if (countend < countbegin)
                {
                    cs_cnt = 0;
                }
                else
                {
                    cs_cnt = eval(document.cookie.substring(countbegin, countend));
                }
            }
            if (cs_cnt < 1)
            {
                cs_cnt = 1;
            }
            else
            {
                cs_cnt = cs_cnt + 1;
            }
        }
        
        cookiepath = getElementValue("CookiePath");
        document.cookie = "cs_cnt="+cs_cnt + "; path=" + cookiepath;
        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 && !self.opener)
//    if (enforceSingleSession())
    {
        // The multibrowser problem doesn't occur where cookies are not available
        var cs_cnt = -1;
        var index = document.cookie.indexOf("cs_cnt");
        var countbegin = -1;
        var countend = -1;
        if (index == -1)
        {
            // This could happen if the cookies was enabled after accessing a career section
            // We suppose there was only one browser opened
            cs_cnt = 0;
        }
        else
        {
            countbegin = (document.cookie.indexOf("=", index) + 1);
            countend = document.cookie.indexOf(";", index);
            if (countend == -1)
            {
                countend = document.cookie.length;
                cs_cnt = eval(document.cookie.substring(countbegin, countend));
            }
            else
            {
                if (countend < countbegin)
                {
                    cs_cnt = 0;
                }
                else
                {
                    cs_cnt = eval(document.cookie.substring(countbegin, countend));
                }
            }
            if (cs_cnt > 0)
            {
                cs_cnt = cs_cnt - 1;
            }
            else
            {
                cs_cnt = 0;
            }
        }
        cookiepath = getElementValue("CookiePath");
        document.cookie = "cs_cnt="+cs_cnt + "; path=" + cookiepath;

    }
}

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);
