﻿function fillChildDropDown(catID, nextDD) {
    if (catID === "-999") {
        return;
    }

    if (nextDD.length > 0) {
        jQuery.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/Store/Controls/ScriptService.asmx/GetChildCategories",
            dataType: "json",
            data: "{'catID': '" + catID + "'}",
            success: function (categories) {
                nextDD.removeAttr("disabled");

                var options = [];

                options.push('<option value="-999">');
                options.push(nextDD.children().first().html());
                options.push("</option>");

                for (var i = 0; i < categories.d.length; i++) {
                    options.push('<option value="', categories.d[i].CatID, '">',
                        categories.d[i].CatName, '</option>');
                }

                nextDD.html(options.join(''));

                clearNextDDs(nextDD);
            }
        });
    }
}

function clearNextDDs(dd) {
    var nextDD = dd.next(".CategoryFilterCategoryDropDown");

    if (nextDD.length == 0) {
        return;
    }
    var defaultOption = '<option value="-999">' + nextDD.children().first().html() + '</option>';

    nextDD.html(defaultOption);
    nextDD.attr("disabled", "disabled");

    clearNextDDs(nextDD);
}

function filterCategory(catID, attributeDDs) {
    if (catID === "-999") {
        return false;
    }

    var catURL = "/store/Category.aspx?catID=" + catID;

    var valid = true;

    if (attributeDDs.length > 0) {
        var attIDs = [];
        attributeDDs.each(function () {
            var attID = $(this).val();

            if (attID === "-999") {
                valid = false;
            }

            attIDs.push(attID);
        });

        catURL += "&Attribs=" + attIDs.join("+");
    }

    if (valid) {
        location.href = catURL;
    }

    return false;
}
