var preloaded_images = new Array();
function preloadImages() {
    if (document.images) {
        var args = preloadImages.arguments;
        var img_count;
        for (var i = 0; i < args.length; i++) {
            img_count = parseInt(preloaded_images.length);
            preloaded_images[img_count] = new Image();
            args[i] = (args[i].substring(0, 1) == "/") ? location.protocol + "\/\/" + location.host + args[i] : args[i];
            preloaded_images[img_count].src = args[i];
        }
    }
}

function clearSearchBox(textBox) {
    var value = textBox.value;
    if (value == "search our site") {
        textBox.value = "";
    }
    else {
        textBox.select();
    }
}

function searchSite() {
    with (document.forms['searchTop']) {
        submit();
    }
}

function getCombinedFunctionCode(p, s) {
    var func1 = p + "";
    var func2 = s + "";
    func1 = func1.substring(0, func1.lastIndexOf("\}"));
    func2 = func2.substring(func2.indexOf("\{") + 1);
    func1 = func1 + func2;
    return func1.substring(func1.indexOf("\{") + 1, func1.lastIndexOf("\}"));
}

function elementExist(id) {
    if (document.getElementById) {
        return (document.getElementById(id));
    } else if (document.all) {
        return (document.all.item(id));
    }
    return false;
}

function getXmlEntities(s) {
    var str = "";
    for (var i = 0; i < s.length; i++) {
        chrCode = s.charCodeAt(i);
        if ((chrCode >= 160 && chrCode <= 255) || chrCode == 34 || chrCode == 38 || chrCode == 39 || chrCode == 60 || chrCode == 62) {
            str += "&#" + chrCode + ";";
        } else { str += s.charAt(i); }
    }
    return str;
}

function changeFontSize(sObj, sheet, days) {
    if (eObj = elementExist("styleSelectSmall")) { eObj.className = ""; }
    if (eObj = elementExist("styleSelectMedium")) { eObj.className = ""; }
    if (eObj = elementExist("styleSelectLarge")) { eObj.className = ""; }
    sObj.className = "selected";
    chooseStyle(sheet, days);
}

function markSelectedFontSize() {
    if (eObj = elementExist("styleSelectSmall")) { eObj.className = ((!selectedtitle || selectedtitle == "none") ? "selected" : ""); }
    if (eObj = elementExist("styleSelectMedium")) { eObj.className = ((selectedtitle == "medium") ? "selected" : ""); }
    if (eObj = elementExist("styleSelectLarge")) { eObj.className = ((selectedtitle == "large") ? "selected" : ""); }
    chooseStyle(selectedtitle, 60);
}

function resetQuickTabs() {
    if (eObj = elementExist("tabRecipes")) { eObj.className = ""; }
    if (eObj = elementExist("quickRecipes")) { eObj.style.display = "none"; }
    if (eObj = elementExist("tabClassesAndEvents")) { eObj.className = ""; }
    if (eObj = elementExist("quickClassesAndEvents")) { eObj.style.display = "none"; }
    if (eObj = elementExist("tabRiskQuiz")) { eObj.className = ""; }
    if (eObj = elementExist("quickRiskQuiz")) { eObj.style.display = "none"; }
    if (eObj = elementExist("tabEmergency")) { eObj.className = ""; }
    if (eObj = elementExist("quickEmergency")) { eObj.style.display = "none"; }
}

function selectQuickTab(tab) {
    resetQuickTabs();
    switch (tab.innerHTML) {
        case "Risk Quiz":
            if (eObj = elementExist("quickRiskQuiz")) { eObj.style.display = "block"; }
            break;
        case "Classes/Events":
            if (eObj = elementExist("quickClassesAndEvents")) { eObj.style.display = "block"; }
            break;
        case "Recipes":
            if (eObj = elementExist("quickRecipes")) { eObj.style.display = "block"; }
            break;
        case "Emergency":
            if (eObj = elementExist("quickEmergency")) { eObj.style.display = "block"; }
            break;
        default:
            break;
    }
    tab.className = "selected";
}

function searchClassEvent() {
    addDropdownParam("events", "events");
    addDropdownParam("eventtype", "eventtype");
    window.location = "/kidneyInformation/events/eventsList.html" + getQueryString();
}

function searchRecipe() {
    addDropdownParam("recipetype", "type");
    window.location = "/healthyLiving/livingWell/nutrition/recipes/recipesList.html" + getQueryString();
}

location.querystring = (function () {
    var result = {};
    var querystring = decodeURIComponent(location.search);
    if (!querystring) {
        return result;
    }
    var pairs = querystring.substring(1).split("&");
    var splitPair;
    for (var i = 0; i < pairs.length; i++) {
        splitPair = pairs[i].split("=");
        if (splitPair[0].indexOf("[]") < -1) {
            var key = splitPair[0].substr(0, splitPair[0].length - 2);
            if (!result[key]) {
                result[key] = new Array();
            }
            result[key].push(splitPair[1]);
        } else {
            result[splitPair[0]] = splitPair[1];
        }
    }
    return result;
})();
function getQueryString() {
    var querystring = "?";
    for (var key in location.querystring) {
        if (key && location.querystring[key] != null && location.querystring[key] != "") {
            if (querystring.length > 1) {
                querystring += "&";
            }
            querystring += key + "=" + encodeURIComponent(location.querystring[key]);
        }
    }
    return querystring.length > 1 ? querystring : "";
}
function getUrl() {
    var querystring = "?";
    for (var key in location.querystring) {
        if (key && location.querystring[key] != null && location.querystring[key] != "") {
            if (querystring.length > 1) {
                querystring += "&";
            }
            querystring += key + "=" + encodeURIComponent(location.querystring[key]);
        }
    }
    return location.href.split("?")[0] + (querystring.length > 1 ? querystring : "");
}
function addParameter(paraName, value) {
    location.querystring[paraName] = value;
}
function removeParameter(paraName) {
    delete location.querystring[paraName];
}
function reloadPage() {
    window.location = getUrl();
}
function addDropdownParam(name, paramName) {
    var q;
    if ($("#" + name).find(":selected").val()) {
        addParameter(paramName, $("#" + name).find(":selected").val());
    }
    else {
        removeParameter(paramName);
    }
}
window.onload = markSelectedFontSize;
window.onunload = function () { }
