//Style Sheet Switcher version 1.0 Nov 9th, 2005
//Author: Dynamic Drive: http://www.dynamicdrive.com
//Usage terms: http://www.dynamicdrive.com/notice.htm

function PageQuery(q) {
    if(q.length > 1)
        this.q = q.substring(1, q.length);
    else
        this.q = null;

    this.keyValuePairs = new Array();

    if(q) {
        for(var i=0; i < this.q.split("&").length; i++) {
            this.keyValuePairs[i] = this.q.split("&")[i];
        }
    }

    this.getKeyValuePairs = function() {
        return this.keyValuePairs;
    }

    this.getValue = function(s) {
        for(var j=0; j < this.keyValuePairs.length; j++) {
            if(this.keyValuePairs[j].split("=")[0] == s)
                return this.keyValuePairs[j].split("=")[1];
        }
        return false;
    }

    this.getParameters = function() {
        var a = new Array(this.getLength());
        for(var j=0; j < this.keyValuePairs.length; j++) {
            a[j] = this.keyValuePairs[j].split("=")[0];
        }
        return a;
    }
    this.getLength = function() { return this.keyValuePairs.length; }
}

function queryString(key){
    var page = new PageQuery(window.location.search);
    return unescape(page.getValue(key));
}

function displayItem(key){
    if(queryString(key)=='false'){
        document.write("you didn't enter a ?name=value querystring item.");
    }else{
        document.write(queryString(key));
    }
}

function getCookie(Name) { 
    var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
    if (document.cookie.match(re)) //if cookie found
        return document.cookie.match(re)[0].split("=")[1]; //return its value
    return null;
}

function setCookie(name, value, days) {
    var expireDate = new Date();
    //set "expstring" to either future or past date, to set or delete cookie, respectively
    var expstring = (typeof days!="undefined")? expireDate.setDate(expireDate.getDate()+parseInt(days)) : expireDate.setDate(expireDate.getDate()-5);
    document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/";
}

function deleteCookie(name){
    setCookie(name, "moot");
}

function setStylesheet(title) {
    var i, cacheobj;
    for(i=0; (cacheobj = document.getElementsByTagName("link")[i]); i++) {
        if(cacheobj.getAttribute("rel").indexOf("style") != -1 && cacheobj.getAttribute("title")) {
            cacheobj.disabled = true;
            if(cacheobj.getAttribute("title") == title){
                cacheobj.disabled = false; //enable chosen style sheet
            }
        }
    }
}

function chooseStyle(styletitle, days){
    if (document.getElementById){

        //document.getElementById("nav1").href = "http://www.airtoons.com/?at_c="+styletitle;
        document.getElementById("nav2").href = "http://www.merchline.com/airtoons/?at_c="+styletitle;
        /*document.getElementById("nav3").href = "http://www.airtoons.com/portraits.php?at_c="+styletitle;
        document.getElementById("nav4").href = "http://www.airtoons.com/potd.php?at_c="+styletitle;
        document.getElementById("nav5").href = "http://www.airtoons.com/discuss/?at_c="+styletitle;
        document.getElementById("nav6").href = "http://www.airtoons.com/mailbag.php?at_c="+styletitle;
        document.getElementById("nav7").href = "http://www.airtoons.com/contact.php?at_c="+styletitle;*/

        if(document.getElementById("helper")){
            document.getElementById("helper").innerHTML = "";
        }
        setStylesheet(styletitle);
        setCookie("airtoons_color_theme", styletitle, days);
        if(document.buyform){
            var ccolors = styletitle.split("_");
            var fgc = ccolors[0];
            var bgc = ccolors[1];
            document.buyform.page_style.value = bgc;
        }
    }
}

function indicateSelected(element){ //Optional function that shows which style sheet is currently selected within group of radio buttons or select menu
    for (var i = 0; i < element.options.length; i++){
        if (element[i].value == selectedtitle){ //if match found between form element value and cookie value
            element[i].selected = true;
            break;
        }
    }
}

/*var selectedtitle = getCookie("airtoons_color_theme");

// if we aren't forcing a new color scheme from the query string, get the stored stuffs
if (queryString('at_c') == 'false' && document.getElementById && selectedtitle != null) { //load user chosen style sheet if there is one stored
    setStylesheet(selectedtitle);
}*/
