﻿function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}


function areCookiesEnabled() {
    //alert('Testing');
    //If we already checked the availability of cookies - return what we found
    if (document.getElementById('hidAreCookiesEnabled')) {
        //alert('true');
        if (document.getElementById('hidAreCookiesEnabled').value != 'notset') {
            //alert('true1- ' + document.getElementById('hidAreCookiesEnabled').value);
            return document.getElementById('hidAreCookiesEnabled').value;
        }
        else {
            //Else - we will check and set it
            //alert('true2 - ' + navigator.cookieEnabled);
            //var cookieEnabled = (navigator.cookieEnabled) ? true : false;

            //if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled) {
                document.cookie = "testcookie";
                cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false;
            //}
            //alert('true2 - ' + cookieEnabled)

            document.getElementById('hidAreCookiesEnabled').value = cookieEnabled.toString();

            if (cookieEnabled.toString() == 'false') {
                //alert('Please enable your cookies in order to experience all the capabilities of our website.');
                window.location = 'Error/CookieNeeded.aspx';
            }
            

            return (cookieEnabled.toString());
        }
    }
    else {
        //alert('false');
        return 'false';
    }
}


function removeVariableFromQueryString(variable) {
    var result = "";
    var query = window.location.search.substring(1);
    //alert('Query - ' + query);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] != variable) {
            if (result == '')
                result = result + vars[i];
            else
                result = result + '&' + vars[i];
        }
    }
    //alert('result - ' + result);
    return result;
}

function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return pair[1];
        }
    }
    return '000';
    //alert('Query Variable ' + variable + ' not found');
}

function getHash() {
    var theHash = '';

    //alert('areCookiesEnabled() - ' + areCookiesEnabled());
    if (areCookiesEnabled() == 'true') {

        theHash = readCookie('wechash');
        if (theHash != null)
            theHash = theHash.replace(/___/g, ";"); //.replace(/{amp}/g, "&");
        else
            theHash = '';
    }

    if (!theHash)
        theHash = "";

    //var theHash = window.location.hash;
    if (theHash.length > 1) {
        //alert('1');
        //if (theHash[0] == '#') {
        //alert(theHash[0]);
            theHash = ltrim(theHash, '#');
        //}
        //theHash = theHash.substr(1);
    }
    //alert(theHash);
    
    return theHash;
    //return window.location.hash.replace(";", "%");
}

function setHashFromQS() {
    //alert('1');
    if (areCookiesEnabled() == 'true') {
        //alert('2');
        //alert('wechash - ' + window.location.hash.replace(/;/g, "___").replace(/&/g, "{amp}"));
        createCookie('wechash', window.location.hash.replace(/;/g, "___").replace(/&/g, "{amp}"), 30);
    }
    //alert('3');
}

function setHash(theHash) {
    if (areCookiesEnabled() == 'true') {
        if (theHash.length > 1) {
            if (theHash[0] != '#') {
                theHash = '#' + theHash;
            }
        }
        //alert(theHash);

        //alert(theHash);
        if (theHash != null)
            theHash = theHash.replace(/;/g, "___").replace(/&/g, "{amp}");
        else
            theHash = '';
        
        createCookie('wechash', theHash, 30);
    }
//    else {
//        theHash = theHash.replace(/&/g, "{amp}");
//        window.location.hash = theHash;
//    }
}

function getHashVariable(variable, theHash) {
    if (theHash.length > 1) {
        //theHash = theHash.substr(1);
        var vars = theHash.split("/");
        //alert('vars.length - ' + vars.length);
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");

            //alert('pair[0] - ' + pair[0]);
            //alert('pair[1] - ' + pair[1]);
            
            if (pair[0] == variable) {
                return pair[1];
            }
        }
    }
    return '000';
    //alert('Query Variable ' + variable + ' not found');
}

function removeVariableFromHash(variable) {
    var result = "";
    var theHash = getHash();

    if (theHash.length > 1) {
        //theHash = theHash.substr(1);
        //alert(theHash);    
        var vars = theHash.split("/");
        for (var i = 0; i < vars.length; i++) {
            //alert(vars.length);    
            var pair = vars[i].split("=");
            if (pair[0] != variable) {
                if (result == '')
                    result = result + vars[i];
                else
                    result = result + '/' + vars[i];
            }
        }
    }
    //alert('result - ' + result);
    return result;
}

function setHashVariable(variable, value) {
    //alert('b');
    var theHash = removeVariableFromHash(variable);
    if (theHash == '')
        theHash = variable.toString() + '=' + value.toString();
    else
        theHash = theHash + '/' + variable.toString() + '=' + value.toString();

    updateQueryString(theHash);
}


function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

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;
}

function eraseCookie(name) {
    createCookie(name, "", -1);

}
