var CSS_YES = 'y';

function querySt(key) {
    query = window.location.search.substring(1);
    set = query.split("&");
    for (i = 0; i < set.length; i++) {
        pair = set[i].split("=");
        if (pair[0] == key) {
            return pair[1];
        }
    }
}

function setRefOnAnchors(allow) {
    if (allow) {
        x = document.getElementById('content');
        a = x.getElementsByTagName('a');
        for (i = 0; i < a.length; i++) {
            a[i].href = a[i].href.search(/\?/) >= 0 ? a[i].href + "&css=" + CSS_YES : a[i].href + "?css=" + CSS_YES;
        }

        // Also put the ref variable on form actions
        x = document.getElementById('IDX-searchForm');
        if (x) {
            y = document.createElement('input');
            y.type = 'hidden'; y.name = 'css'; y.value = CSS_YES;
            x.appendChild(y);
        }
        // Add a class to the body
        b = document.getElementsByTagName("body")[0];
        b.className = b.className + " idx-custom";
    }
}

function css_link_switch() {
    allow = 0;
    var css = querySt('css');
    if (css == CSS_YES) {
        x = document.getElementsByTagName('link');
        for (i = 0; i < x.length; i++) {
            if (x[i].href.indexOf("/css/screen.css") >= 0) {
                y = document.getElementsByTagName("link")[i]
                yp = y.parentNode;
                yp.removeChild(y);
            }
        }
        l = document.createElement("link");
        l.setAttribute("rel", "stylesheet");
        l.setAttribute("type", "text/css");
        l.setAttribute("media", "screen");
        l.setAttribute("href", rpsurl + "/themes/globalcss/idx/idxbroker/theme.ashx"); //platinum.css");
        yp.appendChild(l);

        try {
            l2 = document.createElement("link");
            l2.setAttribute("rel", "stylesheet");
            l2.setAttribute("type", "text/css");
            l2.setAttribute("media", "screen");
            l2.setAttribute("href", rpsurl + "/agent_files/IDX/custom.css");
            yp.appendChild(l2);
        }
        catch (e) { }

        allow = 1; // Allowed to display our custom sidebar branding, and setRefOnAnchors()
    }
}
// Switch the CSS file link immediately so there isn't a FOUC.
css_link_switch();

// Once the DOM is loaded, edit the anchors and add the sidebar.
// Doesn't switch CSS, but happens when we do switch styles.
function css_switch() {
    setRefOnAnchors(allow);
    sidebar(allow);
}
