$(document).ready(function() {
    //initiate variables and fields
    var total;
    $(".ticketPrice").text("0");
    $(".shipping").text("0");
    $(".totalCost").text("0");
    $(".requests").val("");
    $(".shippingDiv").hide();
    $(".shippingSBDiv").hide();
	$(".deliveryRadio :radio").attr("checked", false);


    //color change animation		
    var originalBG = "transparent";
    var fadeColor = "#fbe3ac";

    $(".requests").change(function() {
        var whichIndex = $(".requests").index(this);

        if (isNaN(Number($(this).val()))) {
            $(this).css("border", "1px solid #FF0000");
        } else {
            $(this).css("border", "1px solid #969696");
            $(".ticketPrice").eq(whichIndex).text(($(this).val() * $(".price").eq(whichIndex).html().substring(1)).toFixed(2)); //populates visible fields
            $(".ticketPrice").eq(whichIndex).parent().animate({ backgroundColor: fadeColor }, 750).animate({ backgroundColor: originalBG }, 750);  //animates to show update
            $(".ticketPriceHidden").eq(whichIndex).val($(".ticketPrice").eq(whichIndex).text());  //populates hidden fields
        }
        calculateForm();
    });

    $(".shippingDDL").change(function() {
        $(".shipping").text($(".shippingDDL").val()); //populates visible fields
        $(".shipping").parent().animate({ backgroundColor: fadeColor }, 750).animate({ backgroundColor: originalBG }, 750);  //animates to show update
        $(".shippingPriceHidden").val($(".shipping").text()); //populates hidden fields
        if ($("#delivery2_rad")[0].selectedIndex == 4) {
            $(".shippingDiv").slideDown();
            $(".shippingDiv").parent().parent().animate({ backgroundColor: fadeColor }, 750).animate({ backgroundColor: originalBG }, 750);  //animates to show update;
        } else {
            $(".shippingDiv").slideUp();
            $(".shippingAccountNum").val("");
        }
        calculateForm();
    });

    $(".deliveryRadio :radio").click(function() {
        var whichIndex = $(".deliveryRadio :radio").index(this);
        //alert($(".deliveryRadio :radio").eq(whichIndex).val());
        if ($(".deliveryRadio :radio").eq(whichIndex).val() == "Mail") {
            $(".shippingSBDiv").slideDown();
            $(".shippingSBDiv").parent().animate({ backgroundColor: fadeColor }, 750).animate({ backgroundColor: originalBG }, 750);
        } else {
            $(".shippingDiv").slideUp();
            $(".shippingDDL").attr("selectedIndex", 0);
            $(".shippingSBDiv").slideUp();
            $(".shippingAccountNum").val("");
            $(".shipping").text("0");
            $(".shippingPriceHidden").val("0");
        }
    });

    function calculateForm() {
        total = 0;
        $(".ticketPrice").each(function(i) {
            total += Number($(this).text());
        });

        total += Number($(".shipping").text());
        $(".totalCost").text(total.toFixed(2));
        $(".totalCostBox").val($(".totalCost").text());
    }

    $(".requests").keypress(function(e) {
        //if the letter is not digit then display error and don't type anything
        if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
            //display error message
            //$("#errmsg").html("Digits Only").show().fadeOut("slow");
            return false;
        }
    });
});
