String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

function changeStyle(styleTitle) {
    var styles = document.getElementsByTagName("link");
    for(var i = 0; i < styles.length; i++) {
        a = styles[i];
        if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
            a.disabled = true;
            if (a.getAttribute("title") == styleTitle) {
                a.disabled = false;
            }
        }
    }
}

function getActiveStyle() {
    var styles = document.getElementsByTagName("link");
    for(var i = 0; i < styles.length; i++) {
        a = styles[i];
        if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
            if (a.disabled == false) return a.getAttribute("title");
        }
    }

    return null;
}

function setCookie(name, value, duration) {
    now = new Date();
    expire = new Date(now.getTime() + duration * 86400000);

    document.cookie = name + "=" + value + ";Expires=" + expire.toGMTString() + ";";
}

function getCookie(name) {
    if (document.cookie) {
        var tmp = document.cookie;
        var cookies = tmp.split(";");
        var i=0;
        for(; i<cookies.length; i++) {
            cookieName = cookies[i].substring(0, cookies[i].indexOf("="));
            cookieValue = cookies[i].substring(cookies[i].indexOf("=")+1);
            if(cookieName.trim() == name)
                return cookieValue;
        }
    }

    return null;
}

function rememberStyle(cookieName) {
    var styleTitle = getActiveStyle();
    if (styleTitle != null)
        setCookie(cookieName, styleTitle, 365);
    else
        setCookie(cookieName, "", 365);
}

function useStyleAgain(cookieName) {
    styleName = getCookie(cookieName);
    changeStyle(styleName);
}