﻿// Javascript that contains the common functions used in the FAQ.
// The common functions were written AS MODULAR AS POSSIBLE, using simple returns
// such as true/false. This was done to allow greater flexibility on specific themes.

// Function to get the feedback data as a JavaScript object.
//
// The reason for not having a procedure to submit is that the submit feedback requires
// an AJAX call to the controller. As the files are separated, the nature of AJAX (async)
// creates a problem in which the AJAX call will be executed in different times.
// It also allows a greater flexibility on what to display when submit is successful.
function getFeedbackData() {
    var feedbackData = {};
    feedbackData.Rating = $("input[name='Feedback.FeedbackRating']:checked").val();
    feedbackData.Info = $("#Feedback_FeedbackInfo").val();
    feedbackData.Contact = $("input[name='Feedback.NeedContact']:checked").val();
    feedbackData.Name = $("input#Feedback_Name").val();
    feedbackData.Phone = $("input#Feedback_Phone").val();
    feedbackData.Email = $("input#Feedback_Email").val();
    feedbackData.Postal = $("input#Feedback_Postal").val();
    feedbackData.QuestionId = $("input#Question_QuestionID").val();

    return feedbackData;
}

// Function used to clear all the elements/inputs inside an element. This is most useful
// when used for clearing forms.
//
// Parameters:
// [element] A jQuery object of an element.
function resetForm(element) {
    $(element).find(':input').each(function () {
        switch (this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                $(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });
}

// Function that displays the categories hierarchy. This function is meant to be
// used recursively by assigning each <select onchange> event of the children to
// call this function again.
//
// Parameters:
// [category] The current category object.
// [container] The container in which the list is appended to.
// [level] The current level (zero-based) of the category hierarchy.
// [subcatShown] Indicates whether to show the subcategory offset (tree) or not.
//
// Current Bugs:
// For deep hierarchy as follows:
// A - A.1 - A.1.1
//           A.1.2
//     A.2 - A.2.1
//           A.2.2
// This function still shows all the grandchildren. For example, selecting A will
// bring A.1 and A.2, but selecting A.1 or A.2 will show all the grandchildren
// (albeit in a separate list).
//
// Another bug that might or might not relate to the previous bug, try to select
// A, and then A.2, and then All Categories in A. Upon selecting A.2 again, there
// will be duplicate grandchildren in a separate list).
function displayCategories(category, container, level, subcatShown) {
    // Do not display ROOT category
    if (!category.IsRoot) {
        // Create a new list
        var select = $("<select>").attr("id", "category-" + level);

        // If the current level/category container does NOT have a list
        if ($("select", container).length <= 0) {
            // Append a list to the container
            select = select.appendTo(container);
        }
        else {
            // Use the current container's list
            select = $("select", container);
        }

        // Create All Categories option and always autoselects it
        var allCat = $("<option>").attr("value", -1).text("All Categories");
        allCat = allCat.attr("selected", "selected");

        // If All Categories does NOT exist
        if ($("option[value='-1']", select).length <= 0) {
            // Append All Categories
            allCat.appendTo(select);
        }

        // Create current category option
        var catName = $("<option>").attr("value", category.Id).text(category.Name);

        if (category.Id == $("input#CurrentCategoryId").val() && subcatShown) {
            catName = catName.attr("selected", "selected");
        }

        // If current category does NOT exist
        if ($("option[value='" + category.Id + "']", select).length <= 0) {
            // Append current category to list
            catName.appendTo(select);
        }

        // Creating an offset div for indenting
        var offset = $("<div>").addClass("offset");
        if (category.HasChild)
            offset = offset.appendTo(container);

        // Registering onchange event
        select.change(function () {
            // If All Categories selected
            if ($(this).val() == -1) {
                // Remove the offset/subcategory
                $("div", $(this).parent()).remove();
                //alert($("select#category-" + (level - 1)).val());

                var categoryLevel = level - 1;
                if (categoryLevel < 0) {
                    categoryLevel = 0;
                }

                $("input#CurrentCategoryId").val($("select#category-" + categoryLevel).val());
            }
            else {
                // Recreate offset/subcategory only if there is child (otherwise there will be duplicate offsets)
                if (category.HasChild)
                    offset = offset.appendTo(container);

                // Load the popular questions
                $("div#popularCatFAQs").load("FAQ/GetPopularQuestionsByCategory/" + $(this).val());
                $("input#CurrentCategoryId").val($(this).val());
            }

            // If category has subcategories
            if (category.HasChild) {
                // Iterate through the subcategories
                $.each(category.Children, function (i, subcat) {
                    displayCategories(subcat, offset, level + 1, subcatShown);
                });
            }
        });

        if (category.HasChild && subcatShown) {
            $("option[value='" + category.Id + "']", select).attr("selected", "selected");

            // Iterate through the subcategories
            $.each(category.Children, function (i, subcat) {
                displayCategories(subcat, offset, level + 1, subcatShown);
            });
        }
    }
    else {
        // If root then process the children straight away instead of displaying it
        if (category.HasChild) {
            $.each(category.Children, function (i, subcat) {
                displayCategories(subcat, container, level + 1, subcatShown);
            });
        }
    }
}
