$(function() {
    $("#Login").validate();
    $("#Register").validate({
	rules: {
	    Email2: {
		required: true,
		equalTo: "#Email1"
	    },
	    Password2: {
		required: true,
		minlength: 6,
		equalTo: "#Password1"
	    }
	}
    });
    $("#Account").validate();


    // BACK AND NEXT BUTTONS
    $("#Wizard .back").click(function() {
	var Step = $(this).attr("rel");

	$(".wizard .breadcrumb li").removeClass("darkpurple");
	$(".wizard .breadcrumb li").addClass("purple");
	$(".wizard .breadcrumb li." + Step).addClass("darkpurple");
	
	if (Step == "step1") {
	    $.post("/partials/ajax/event_reservations_reset.php");
	}
	
	if (Step == "step2") {
	    $(this).parent().parent().hide();
	
	} else {
	    $(this).parent().hide();
	}
	
	$("#Wizard fieldset." + Step).show();
	return false;
    });

    $("#Wizard .next").click(function() {
	var EventID = $("#EventID").val();
	var UnitID = $("#Time").val();
	var Tickets = $("#Tickets").val();
	var Step = $(this).attr("rel");

	$(".wizard .breadcrumb li").removeClass("darkpurple");
	$(".wizard .breadcrumb li").addClass("purple");
	$(".wizard .breadcrumb li." + Step).addClass("darkpurple");
	
	if (Step == "step2") {
	    $.post("/partials/ajax/event_reservations.php", { UnitID: UnitID, Quantity: Tickets });
	}

	if (Step == "step4") {
	    var FormData = $("#Wizard").serialize();
	    
	    $.post("/partials/ajax/event_basket_summary.php", { Form: FormData }, function(Summary) {
		$("#Wizard #Summary").html(Summary);
	    });
	    
	    $(this).parent().parent().hide();
	    
	} else {
	    $(this).parent().hide();
	}

	$("#Wizard fieldset." + Step).show();
	return false;
    });

    // ON DATE CHANGE, LOOKUP TIMES AND TICKETS
    $("#Wizard #Date").change(function() {
	var DateID = $(this).val();

	$.post("/partials/ajax/event_times.php", { DateID: DateID }, function(Times){
	    $("#Wizard #Time").html(Times);
	});

	$.post("/partials/ajax/event_tickets.php", { DateID: DateID, UnitID: 0 }, function(Tickets) {
	    $("#Wizard #Tickets").html(Tickets);

	    var Tickets = $("#Tickets").val();
	    $.post("/partials/ajax/event_pricing.php", { Total: Tickets, Children: 0 }, function(Difference) {
		$("#Wizard #Adults").html(Difference);
	    });
	});
    });


    // ON TIME CHANGE, LOOKUP UP TICKETS
    $("#Wizard #Time").change(function() {
	var UnitID = $(this).val();

	$.post("/partials/ajax/event_tickets.php", { UnitID: UnitID }, function(Tickets){
	    $("#Wizard #Tickets").html(Tickets);
	});
    });


    // ON TIME CHANGE, LOOKUP UP TICKETS
    $("#Wizard #Tickets").change(function() {
	var Tickets = $(this).val();
	var DiscoveryDay = $("#DiscoveryDay").val();
	var PriceAdult = $("#PriceAdult").val();

	if (PriceAdult == "0.00") {
	    $.post("/partials/ajax/event_pricing.php", { Total: Tickets, Children: 0, isChildrenOnly: true }, function(Difference) {
		$("#Wizard #Children").html(Difference);
	    });
	    
	    if (DiscoveryDay == "Y") {
		$(".step2 .next").hide();
		$.post("/partials/ajax/child_ages.php", { Children: Tickets }, function(Dropdowns) {
		    $("#Wizard #ChildAges").html(Dropdowns);
		});
	    }
	    
	} else {
	    $.post("/partials/ajax/event_pricing.php", { Total: Tickets, Children: 0 }, function(Difference) {
		$("#Wizard #Adults").html(Difference);
	    });

	    $.post("/partials/ajax/event_pricing.php", { Total: Tickets, Children: Tickets }, function(Difference) {
		$("#Wizard #Children").html(Difference);
	    });
	}
    });


    // ON ADULT CHANGE, LOOKUP UP CHILDREN
    $("#Wizard #Adults").change(function() {
	var Tickets = $("#Tickets").val();
	var DiscoveryDay = $("#DiscoveryDay").val();
	var Adults = $(this).val();

	$.post("/partials/ajax/event_pricing.php", { Total: Tickets, Children: Adults }, function(Difference) {
	    $("#Wizard #Children").html(Difference);
	});

	if (Tickets != Adults && DiscoveryDay == "Y") {
	    $(".step2 .next").hide();
	    $("#ChildAgesArea").show();
	    
	    // GET DROPDOWNS FOR CHILD TICKETS
	    var Children = Tickets - Adults;
	    $.post("/partials/ajax/child_ages.php", { Children: Children }, function(Dropdowns) {
		$("#Wizard #ChildAges").html(Dropdowns);
	    });
	    
	} else {
	    $(".step2 .next").show();
	    $("#ChildAgesArea").hide();
	}
    });


    // ON CHILDREN CHANGE, LOOKUP UP ADULTS
    $("#Wizard #Children").change(function() {
	var Tickets = $("#Tickets").val();
	var DiscoveryDay = $("#DiscoveryDay").val();
	var Children = $(this).val();

	$.post("/partials/ajax/event_pricing.php", { Total: Tickets, Children: Children, isAdult: true }, function(Difference){
	    $("#Wizard #Adults").html(Difference);
	});
	
	if (Children != 0 && DiscoveryDay == "Y") {
	    $(".step2 .next").hide();
	    $("#ChildAgesArea").show();
	    
	    // GET DROPDOWNS FOR CHILD TICKETS
	    $.post("/partials/ajax/child_ages.php", { Children: Children }, function(Dropdowns) {
		$("#Wizard #ChildAges").html(Dropdowns);
	    });
	    
	} else {
	    $(".step2 .next").show();
	    $("#ChildAgesArea").hide();
	}
    });
    
    
    $(".child-age").live("change", function() {
	if ($(this).val() != "-") {
	    var Count = 0;
	    
	    $(".child-age").each(function() {
		if ($(this).val() != "-") {
		    Count++;
		}
	    });
	    
	    if ($(".child-age").length == Count) {
		$(".step2 .next").show();
		
	    } else {
		$(".step2 .next").hide();
	    }
	    
	} else {
	    $(".step2 .next").hide();
	}
    });
    
    
    $(".basket .item .delete").click(function() {
	var ItemID = $(this).attr("rel");
	
	$.post("/partials/ajax/basket_item_delete.php", { ItemID: ItemID }, function(Data) {
	    if (Data == "Empty Basket") {
		window.location.href = "/basket/";
	    
	    } else {
		$("#Item" + ItemID).hide();
	    }
	});
	
	return false;
    });
    
    
});
