$(document).ready(function() {

$('#loading span').ajaxStart(function() {
	$(this).fadeIn();
}).ajaxStop(function() {
	$(this).fadeOut();
});

/* Order Form
==================================== */
	$(".order_item input.add").click(function() {
		var quantity_input = $(this).siblings('input.quantity');
		var quantity_select = $(this).siblings('select.quantity');
		
		if(quantity_input.val() == undefined) {
			var quantity = quantity_select.val();
		} else {
			var quantity = quantity_input.val();
		}
	
		

		
		if(quantity != "" && quantity != "0") {
			var itemid = $(this).siblings('input.itemid').val();
			var feedback = $(this).siblings('span.feedback');
			var addbutton = $(this);
			var label = $(this).siblings('label');
			
			$.ajax({
				url: "add_to_cart.php",
				type: "post",
				data: "add_item=" + itemid + "&quantity=" + quantity,
				success: function(html) {
					$('#debug').html(html);
					feedback.html("Added to cart");
					quantity_input.hide();
					quantity_select.hide();
					addbutton.hide();
					label.hide();
					feedback.fadeIn();
	
				},
				error: function(html) {
					$('#debug').html(html);
				}
			});
		} // check if zero
		
	});
	
	
/* Shipping and Billing
==================================== */

	if($("#shippingsame").is(":checked")) {
		$(".shipping").hide();
	}
	
	$("#shippingsame").change(function() {
		if(this.checked) {
			$(".shipping").slideUp();
		} else {
			$(".shipping").slideDown();
		}
	});
	
/* Empty Cart button
==================================== */

	$("#emptycart a").click(function() {
		$.ajax({
			url: "empty_cart.php",
			type: "post",
			data: "empty=true",
			success: function(html) {
				$(".debug").html(html);
				update_cart();
			},
			error: function(html) {
				$(".debug").html(html);
			}
		});
	});
	
	
/* On-click info form validation
==================================== */
	$("#submit_info").click(function() {
		info_validate();
	});

	
	
/* Do setup
==================================== */
	
	update_cart();
	
});

/* Draw and/or Update Cart Table
==================================== */
	function update_cart() {
		
		$.ajax({
			url: "cart_table.php",
			type: "post",
			data: "",
			success: function(htmlcontent) {
				// alert(html);
				$("#cart_table").html(htmlcontent);
				// alert($("#cart").html());
				$(".total").effect("highlight", {}, 1000);
				ajaxify_cart();
				remove_from_cart();
			},
			error: function(html) {
				$(".debug").html(html);
			}
		});
		
		//$(".debug").html("hi");
	}


/* Update cart quantity
==================================== */

function ajaxify_cart() {
	$(".quantity").change(function() {
	
		var item_id = $(this).attr("name");
		var new_quantity = $(this).val();
		
		$.ajax({
			url: "update_cart.php",
			type: "post",
			data: "item_id=" + item_id + "&new_quantity=" + new_quantity,
			success: function(html) {
				$(".debug").html(html);
				update_cart();
			},
			error: function(html) {
				$(".debug").html(html);
			}
		});
	});
}	

function remove_from_cart() {
	$(".removefromcart").click(function() {
	
		var item_id = $(this).attr("title");
		var new_quantity = 0;
		
		$.ajax({
			url: "update_cart.php",
			type: "post",
			data: "item_id=" + item_id + "&new_quantity=" + new_quantity,
			success: function(html) {
				$(".debug").html(html);
				update_cart();
			},
			error: function(html) {
				$(".debug").html(html);
			}
		});
	});
}




/* Info form validation
==================================== */
	function info_validate() {

		missing = false;
		reset_field_colors();

		if($("#contactname").val() == "") {
			mark_red($("#contactname"));
			missing = true;
		}
		
		if($("#contactschool").val() == "") {
			mark_red($("#contactschool"));
			missing = true;
		}
		
		if($("#contactaddress").val() == "") {
			mark_red($("#contactaddress"));
			missing = true;
		}
		
		if($("#contactcity").val() == "") {
			mark_red($("#contactcity"));
			missing = true;
		}
		
		if($("#contactstate").val() == "") {
			mark_red($("#contactstate"));
			missing = true;
		}
		
		if($("#contactzip").val() == "") {
			mark_red($("#contactzip"));
			missing = true;
		}
		
		if($("#contactemail").val() == "") {
			mark_red($("#contactemail"));
			missing = true;
		}
		
		if($("#contactphone").val() == "") {
			mark_red($("#contactphone"));
			missing = true;
		}
		
		// if shipping isn't the same
		var shippingsame = $("#shippingsame").attr('checked');
		if(shippingsame == false) {
			
			if($("#shipname").val() == "") {
				mark_red($("#shipname"));
				missing = true;
			}
			
			if($("#shipschool").val() == "") {
				mark_red($("#shipschool"));
				missing = true;
			}
			
			if($("#shipaddress").val() == "") {
				mark_red($("#shipaddress"));
				missing = true;
			}
			
			if($("#shipcity").val() == "") {
				mark_red($("#shipcity"));
				missing = true;
			}
			
			if($("#shipstate").val() == "") {
				mark_red($("#shipstate"));
				missing = true;
			}
			
			if($("#shipzip").val() == "") {
				mark_red($("#shipzip"));
				missing = true;
			}
			
			if($("#shipemail").val() == "") {
				mark_red($("#shipemail"));
				missing = true;
			}
			
			if($("#shipphone").val() == "") {
				mark_red($("#shipphone"));
				missing = true;
			}			
			
		}
		
		if($("#purchaseorder").val() == "") {
			mark_red($("#purchaseorder"));
			missing = true;
		}	

		if($("#checknumber").val() == "") {
			mark_red($("#checknumber"));
			missing = true;
		}			
		
		if(missing) {
			$("#missinginfo").fadeIn();
		} else {
			$("#missinginfo").fadeOut();
			document.infoform.submit();
		}
		
	}
	
	function mark_red(item) {
		$(item).css("border", "solid 2px #a00");
		$(item).css("background", "#fed0c7");
	}
	
	function reset_field_colors() {
		$("#infoform input.text").css("border", "solid 1px #aad");
		$("#infoform input.text").css("background", "#def");
	}
