 $(function() {  
     $("#cart tr .remove input").click(function() {  
         var orderCode = $(this).val();  
         $.ajax({  
             type: "GET",  
             url: "?p=update",  
             data: "remove[]=" + orderCode,  
             success: function() {  
                 $("#cart tr .remove input[value=" + orderCode + "]").parent().parent().fadeOut(500, function() {  
                     $(this).remove();  
                     calcPrice();  
                 });  
             },  
             error: function() {  
                 window.location("?p=index&remove[]="+orderCode);  
             }  
         });  
     });  
       
     $("#cart tr .quantity input").change(function() {  
         var orderCode = $(this).attr("name").slice(9, -1);  
         var quantity = $(this).val();  
         $.ajax({  
             type: "GET",  
             url: "?p=update",  
             data: "quantity[" + orderCode + "]=" + quantity,  
             success: function() {  
                 var startColor = $("#cart tr .quantity input[name*=" + orderCode + "]").parent().parent().hasClass("odd") ? "#eee" : "#fff";  
                 $("#cart tr .quantity input[name*=" + orderCode + "]").parent().parent().find("td").animate({ backgroundColor: "#ff8" }, 100).animate({ backgroundColor: startColor }, 800);  
                 calcPrice();  
             },  
             error: function() {  
                 window.location("?p=index&quantity[" + orderCode + "]=" + quantity);  
             }  
         });  
     });  
 });  
   
 function calcPrice() {  
     var totalPrice = 0;  
     $("#cart tr .quantity").parent().each(function() {  
         var quantity = $(".quantity input", this).val();  
         var unitPrice = $(".unit_price", this).text().slice(1);  
         var extendedPrice = quantity*unitPrice;  
         totalPrice += extendedPrice;  
           
         $(".extended_price", this).html("$" + extendedPrice);  
         $("#total_price").html("$"+totalPrice);  
     });  
     if ( totalPrice == 0 ) {  
         $("#cart").parent().replaceWith("<p class='center' style='font-size:14px;font-weight:bold;font-family:arial;padding:5px;'>You have no items in your cart.</p>");  
     }  
 }  

function addToCart(order_code){
	size=""
	if($("#size_"+order_code).length > 0){
		if( $("#size_"+order_code).val() == "#" ){
			alert("Please pick a shirt size");
			return false;
		}	
		size = $("#size_"+order_code).val();
	}
	$.ajax({
		type: "GET",
		url: "/cart/?p=update",
		data: "quantity[" + order_code + "]=1&size[" + order_code + "]="+size,
		success: function(){
			$("#row_"+order_code).animate({ backgroundColor: "#CF8E40" }, 100).animate({ backgroundColor: "#fff" }, 800);
			return false;
		}
	});
}
