
var SiteSelector = new SiteSelector()
function SiteSelector() {
    var CONST_COOKIE_PARAM_SELECTED_SITE = "CookieSelectedSite";
    var CONST_COOKIE_SHOW_SPLASH = "CookieShowSplash";
    var CONST_COOKIE_NOT_SHOW_SPLASH = "SessionCookieNotShowSplash";
    var CONST_QUERYSTRING_RESET_SITE = "ResetSiteSelection";

    this.CheckForPreviousSelection = function(currentSite) {
        // First check if request for CookieSelectedSite reset was made.
        var reset = this.GetQueryStringValue(CONST_QUERYSTRING_RESET_SITE);
        if ((reset !== null) && (reset == "true")) {
            // Reset CookieSelectedSite.
            this.SetCookie(CONST_COOKIE_PARAM_SELECTED_SITE, currentSite)
            // Redirect visitor to remove reset parameter from url querystring.
            window.location = currentSite;
            return;
        }

        var previousSelectedSite = this.GetCookie(CONST_COOKIE_PARAM_SELECTED_SITE);
        if ((previousSelectedSite !== null) && (previousSelectedSite.length > 0) && (previousSelectedSite != currentSite)) {
            // Value set for "CookieSelectedSite" differs from current site.
            // Redirect visitor to previous selected site.
            window.location = previousSelectedSite + '?' + CONST_QUERYSTRING_RESET_SITE + '=true';
        }
        return;
    }

    this.RememberSelectionAndRedirect = function(selectedURL) {
        this.SetCookie(CONST_COOKIE_PARAM_SELECTED_SITE, selectedURL);
        window.location = selectedURL + '?' + CONST_QUERYSTRING_RESET_SITE + '=true';
    }

    this.SetCookie = function(keyName, value) {
        var date = new Date();
        date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();

        document.cookie = keyName + "=" + escape(value) + expires + "; path=/";
    }

    this.GetCookie = function(keyName) {
        var allCookieStringKeyValPairs = document.cookie.split(';');
        return this.GetValue(keyName, allCookieStringKeyValPairs);
    }

    this.SetSessionCookie = function(name, value) 
    {
        document.cookie = name + "=" + value;
    } 

    this.GetSessionCookie = function(name) 
    {
        var search = name + "="
        var returnvalue = "";
        if (document.cookie.length > 0) {
            offset = document.cookie.indexOf(search)
            // if cookie exists
            if (offset != -1) {
                offset += search.length
                // set index of beginning of value
                end = document.cookie.indexOf(";", offset);
                // set index of end of cookie value
                if (end == -1) end = document.cookie.length;
                returnvalue = unescape(document.cookie.substring(offset, end))
            }
        }
        return returnvalue;
    }


    this.GetQueryStringValue = function(keyName) {
        var allQueryStringKeyValPairs = {};
        if (window.location.search.length > 1) {
            allQueryStringKeyValPairs = window.location.search.split("&");
        }
        return this.GetValue(keyName, allQueryStringKeyValPairs);
    }

    this.GetValue = function(keyName, keyValuePairs) {
        for (i = 0; i < keyValuePairs.length; i++) {
            var keyVal = keyValuePairs[i].split("=");
            var key = keyVal[0].replace(/^\s+|\s+$/g, '');
            key = key.replace('?', '');

            if (key == keyName) {
                if (keyVal.length > 1) {
                    return unescape(keyVal[1].replace(/^\s+|\s+$/g, ''));

                }
                return null;
            }
        }
        return null;
    }

    this.SplashRedirect = function(selectedURL, checkboxId) {
        var targetCheckBox = document.getElementById(checkboxId);
        var showSplashAgain = !targetCheckBox.checked;
        this.SetCookie(CONST_COOKIE_SHOW_SPLASH, showSplashAgain);
        this.SetSessionCookie(CONST_COOKIE_NOT_SHOW_SPLASH, "true");
        window.location = selectedURL + '?' + CONST_QUERYSTRING_RESET_SITE + '=true';
    }

    this.ShowSiteSelectorSplash = function() {
        var showSplashAgain = this.GetCookie(CONST_COOKIE_SHOW_SPLASH);
        var resetSiteSelection = this.GetQueryStringValue(CONST_QUERYSTRING_RESET_SITE);
        var notVisitAgain = this.GetSessionCookie(CONST_COOKIE_NOT_SHOW_SPLASH);

        //Situation A: already the popup was shown, do not show it in the current session again.
        if ((notVisitAgain !== null) && (notVisitAgain == "true"))
        {
            return false;
        }
        
        //Situation B: A selection was not made or not applicible anymore.
        if ((showSplashAgain == null) && (resetSiteSelection == null)) {
            return true;
        }

        //situation C: A selection was made but 'remember selection' was set to false
        if ((showSplashAgain !== null) && (showSplashAgain == "false") || (resetSiteSelection !== null && resetSiteSelection == "true")) {
            return false;
        }

        //situation D: A selection was made in the past because 'remember selection' was set to true
        if ((showSplashAgain !== null) && (showSplashAgain == "true")) {
            return true;
        }
    }
}

