String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

/**
 * Enables alternative style which title is the given styleTitle.
 *
 * All other alternative styles will get disabled.
 *
 * @param styleTitle    title of the alternative style which should be activated
 */
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;
            }
        }
    }
}

/**
 * Returns the currently enabled alternative stylesheet title, or null if non is enabled
 *
 * @returns             title of enabled alternative stylesheet or null if non is enabled
 */
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;
}

/**
 * Remembers current active style by setting an browser cookie
 *
 * This should be triggered at the unload-event.
 *
 * <code>
 * &lt;body ... unload="rememberStyle('test')" ... &gt;
 * </code>
 *
 * Defaults duration to 365 days. No path is currently set.
 *
 * @param cookieName    name of cookie, which should be set (to allow multiple definitions on one domain)
 */
function rememberStyle(cookieName) {
    var styleTitle = getActiveStyle();
    if (styleTitle != null)
        setCookie(cookieName, styleTitle, 365);
    else
        setCookie(cookieName, "(none)", 365);
}

/**
 * Reads cookie and sets style accordingly
 *
 * This should be triggered at the load-event.
 *
 * <code>
 * &lt;body ... load="useStyleAgain('test')" ... &gt;
 * </code>
 *
 * Defaults duration to 365 days. No path is currently set.
 *
 * @param cookieName    name of cookie, which should be set (to allow multiple definitions on one domain)
 */
function useStyleAgain(cookieName) {
    styleName = getCookie(cookieName);
    if(styleName == "(none)")
        changeStyle("");
    else
        changeStyle(styleName);
}

/**
 * Sets an cookie
 *
 * @param name          name of cookie
 * @param value         value of cookie
 * @param duration      duration of cookie, given in days
 */
function setCookie(name, value, duration) {
    now = new Date();
    expire = new Date(now.getTime() + duration * 86400000);

    document.cookie = name + "=" + value + ";expires=" + expire.toGMTString() + ";";
}

/**
 * Gets an cookie value or returns null if not found or cookies are disabled.
 *
 * @param name          name of cookie
 * @returns             value of cookie if set, else null
 */
function getCookie(name) {
    if (document.cookie) {
        var tmp = document.cookie;
        var cookies = tmp.split(";");
        var i=0;
        for(; i<cookies.length; i++) {
            assignIndex = cookies[i].indexOf("=");
            cookieName = cookies[i].substring(0, assignIndex);
            cookieValue = cookies[i].substring(assignIndex+1);
            if(cookieName.trim() == name)
                return cookieValue;
        }
    }

    return null;
}

