﻿// Attach the event listeners and logic.
var NonMemberModifier = 0;

$(document).ready(function() {
    $("#rblIsMember input").click(function() {
        if (this.value == "Yes")
            NonMemberModifier = 0;
        else
            NonMemberModifier = .5;

        CalcPrice();
    });

    $("#mealTable input[type=checkbox]").each(function(index, input) {
        var mod = index % 7;
        if (mod == 0 || mod == 3)
            input.Price = 10;
        else if (mod == 1 || mod == 2)
            input.Price = 25;
        else if (mod == 4 || mod == 6)
            input.Price = 40;
        else
            input.Price = 35;
    });

    $("input[type=checkbox][Price]").click(CalcPrice);
    $("table.infoTable input:text").blur(CalcPrice);
    $("#rblIsMember input:first").click();
    $(document.forms[0]).submit(function() {
        // Clear Errors
        $(".fieldError").removeClass("fieldError");
        $(".fieldBlockError").removeClass("fieldBlockError");

        // Validate the form.
        var IsValid = true;
        var fields = $("p.fieldBlock input:text[value=''][class!='ignoreValidate']");
        if (fields.length > 0) {
            fields.parent().prev().addClass("fieldError");
            fields.parents("p.fieldBlock").addClass("fieldBlockError");
            IsValid = false;
        }

        var fields = $("#tblPaymentMethod input:radio[checked]");
        if (fields.length == 0) {
            $("#tblPaymentMethod").addClass("fieldBlockError");
            IsValid = false;
        }

        if (!IsValid) {
            window.scrollTo(0, 0);
            $("#formErrorTitle").show();
        }
        return IsValid;
    });
});

function CalcPrice() {
    // Get all of the price checkboxes
    var total = 0;
    var packages = $("table.infoTable input:text[value!='']").length;
    if (packages > 0)
        total += ((packages - 1) * 120) + 170;
    
    $("input[type=checkbox][Price][checked]").each(function(index, input) {
        total += input.Price;
    });

    total = total + (total * NonMemberModifier);
    $("#eventTotal").text(total);

    // Do the totals for the meal rows.
    $("#mealTable tr").each(function(index, row) {
        if (index > 1) {
            var boxes = $(row).find("input[type=checkbox][checked]");
            if (boxes.length != 0) {
                var total = 0;
                boxes.each(function(bi, b) {
                    total += b.Price;
                });
                $(row).find("td:last").text("$" + (total + (total * NonMemberModifier)));
            } else
                $(row).find("td:last").text(" ");
        }
    });
}
