﻿// A utility class for performing hash, querystring, and cookie functions
var Storage = Class.create({
    cookiename: 'CEC_TakingStock',
    language: 'en-US',
    pagesize: 10,
    pagenumber: 0,
    sortcol: 'Name',
    sortdir: 0,
    cookiePairDelimiter: '&',
    cookieAssignment: '=',
    initialize: function() {
    },
    setHashValue: function(key, value) {
        if (!value)
            value = '';

        if (value.endsWith(',')) {
            value = value.substring(0, value.length - 1);
        }
        //For now, only unique values
        var values = value.split();
        values = values.uniq();
        if (key == 'year') {
            values.reverse();    // year is sorted descending in selection box, reverse the order for data layer
        }
        value = values.join(',');

        var hash = window.location.hash.substring(1);

        var keyIndex = hash.indexOf(key);
        if (keyIndex < 0) {
            //no key, so append pair at end
            if (hash.length > 0 && hash.charAt(hash.length - 1) != '|') {
                window.location.hash += '|';
            }
            window.location.hash += key + '=' + value + '|';
        } else {
            //the key is there, so look for it and set/add value
            var hashList = hash.split('|');
            var newHash = "";
            for (var i = 0; i < hashList.length; i++) {
                if (!hashList[i] || hashList[i].length == 0)
                    continue;

                if (hashList[i].startsWith(key)) {
                    newHash += key + '=' + value + '|';
                } else {
                    //add back to the hash and continue
                    newHash += hashList[i] + '|';
                }
            }
            window.location.hash = newHash;
        }
    },

    getHashes: function() {
        var hashes = window.location.hash.substring(1).split('|');
        var hashesAsArray = new Array();
        for (var i = 0; i < hashes.length; i++) {
            var pair = hashes[i];
            if (pair && pair.length > 0) {
                var key = pair.split('=')[0];
                var value = '';
                if (pair.indexOf('=') > 0) {
                    value = pair.split('=')[1];
                }
                hashesAsArray[key] = value;
            }

        }
        return hashesAsArray;
    },
    getHashValue: function(key) {
        var hash = this.getHashes();
        var value = hash[key];
        return value;
    },
    getSearch: function() {
        var hashes = window.location.search.substring(1).split('&');
        var nameValuePairs = new Array();
        for (var i = 0; i < hashes.length; i++) {
            var pair = hashes[i];
            if (pair && pair.length > 0) {
                var tokens = pair.split('=');
                if (!tokens || tokens.length == 0)
                    continue;

                var key = tokens[0];
                var value = '';
                if (tokens.length > 1)
                    value = tokens[1];
                if (key && key.length > 0) {
                    nameValuePairs[key] = value;
                }
            }
        }
        //        hashes.each(function(pair) {});
        return nameValuePairs;
    },
    getSearchValue: function(key) {
        var hash = this.getSearch();
        var value = '';
        if (hash && hash[key])
            value = hash[key];
        return value;
    },
    getCookieValue: function(key) {
        var cookie = this.getCookie(this.cookiename);
        if (cookie && cookie.length > 0) {
            var settings = this.parse(cookie, this.cookiePairDelimiter, this.cookieAssignment);
            if (settings && settings[key])
                return settings[key];
        }
        return "";
    },
    getCookie: function() {
        var cookies = document.cookie;
        var cookie = "";
        if (cookies && cookies.length > 0) {
            var start = cookies.indexOf(this.cookiename);
            if (start >= 0) {
                start += this.cookiename.length + 1;
                var end = cookies.indexOf(";", start);
                if (end > start)
                    cookie = cookies.substring(start, end);
                else
                    cookie = cookies.substring(start, cookies.length);
            }
        }
        return cookie;
    },
    parse: function(parseString, pairDelimiter, assignment) {
        var keyValuePairs = parseString.split(pairDelimiter);
        var hashesAsArray = new Array();
        keyValuePairs.each(function(pair) {
            if (pair && pair.length > 0) {
                var key = pair.split(assignment)[0];
                var value = '';
                if (pair.indexOf(assignment) > 0) {
                    value = pair.split(assignment)[1];
                }
                hashesAsArray[key] = value;
            }
        });
        return hashesAsArray;
    },
    setCookieValue: function(key, value) {
        if (!key || key.length == 0)
            return;

        //decide between temp and permanent cookie
        var theCookieName = this.cookiename;

        var cookie = this.getCookie(theCookieName);
        if (cookie && cookie.length > 0) {
            //assume path and expires is set
            if (cookie.indexOf(key) >= 0) {
                var pattern = new RegExp(key + this.cookieAssignment + "[^" + this.cookiePairDelimiter + "]*");
                cookie = cookie.replace(pattern, key + this.cookieAssignment + value);
            } else {
                cookie += this.cookiePairDelimiter + key + this.cookieAssignment + value;
            }
        } else {
            //assume new cookie
            cookie += key + this.cookieAssignment + value;
        }
        var expires = "";
        var nextYear = new Date();
        nextYear.setFullYear(nextYear.getFullYear() + 1);
        expires = ("; expires=" + nextYear.toGMTString());
        document.cookie = (theCookieName + "=" + cookie
            + "; path=/" + expires);
    },
    getCulture: function() {
        var varlan = this.getCookieValue("lang");
        if (varlan.length > 0)
            return varlan;
        return this.language;
    },
    setLang: function(lang) {
        this.setCookieValue("lang", lang);
        var p = window.location.pathname;
        var search = window.location.search;

        if (search.length > 0) {
            //has parameters already
            if (search.indexOf("varlan") > -1) {
                //has lang already
                search = search.replace(/varlan=\w\w-\w\w/, "varlan=" + lang);
            } else {
                //insert new parameter
                search = "?varlan=" + lang + "&" + search.substring(1, search.length);
            }
        } else {
            //set new parameter
            search = "?varlan=" + lang;
        }

        var hash = window.location.hash;
        window.location.href = p + search + hash;
    },
    setHiddenLike: function(nameLike, value) {
        var inputs = document.forms[0].elements;
        for (var i = 0; i < inputs.length; i++) {
            if (inputs[i].type == 'hidden') {
                if (inputs[i].name.indexOf(nameLike) >= 0) {
                    inputs[i].value = value;
                    break;
                }
            }
        }
    }
});