	/**
 * Ajax Shopping Cart 
 */
function addItem() {
	var data = $("#cartForm").serialize();
	$.ajax({
   		type: "post",
   		url: "/shopping_cart/carts/add/",
   		data: data,
   		dataType: "json",
		success: function(response, status) {
			itemCallback(response, status);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			cartError(XMLHttpRequest, textStatus, errorThrown);
		}
	});
 
	return false;
} 
function emptyCart() {
	$.ajax({
   		type: "post",
   		url: "/shopping_cart/carts/emptycart/",
   		dataType: "json",
		success: function(response, status) {
			emptySuccess(response, status);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			cartError(XMLHttpRequest, textStatus, errorThrown);
		}
	});
	return false;
}
function removeItem() {
	var data = $("#removeForm").serialize();
	$.ajax({
   		type: "post",
   		url: "/shopping_cart/carts/delete/",
   		data: data,
   		dataType: "json",
		success: function(response, status) {
			removeSuccess(response, status);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			cartError(XMLHttpRequest, textStatus, errorThrown);
		}
	});
	return false;
}
/**
 * Handle the AJAX callbacks
 */
function itemCallback(response, status) {
	// Response was a success
	if (response.success === true) {
		var ajaxcart = new Array;
		if (typeof(response.data) == ("object" || "array")) {
        	$.each(response.data, function(index, data) {
        		var cartitems = '<div class="shoppingcart_item"><div class="shoppingcart_itemname"><p>' + data.name + '</p><form onsubmit="return false;" id="removeForm" method="post"><input type="hidden" name="data[Cart][id]" id="CartId" value="'+ data.id +'"><button type="submit" onclick="removeItem();">remove</button></form></div><div div class="shoppingcart_itemprice"><p>$' + data.price + '</p></div><div div class="shoppingcart_itemqty"><p class="right">' + data.qty + '</p></div></div>';

            	ajaxcart[ajaxcart.length] = cartitems;
        	});
    	} else {
        	ajaxcart[ajaxcart.length] = "<li>" + response.data + "</li>";
		}
		var total = response.total;
		var cartbottom = '<div class="cartbottom"><div class="shoppingcart_total"><p>Subtotal $'+total.toFixed(2)+'</p></div><div class="shoppingcart_emptycart"><p class="right"><button type="button" onclick="emptyCart();">empty cart</button></p></div><div class="shoppingcart_checkout"><p class="center"><img src="/shopping_cart/img/cart_proceedtocheckout.png" onclick="window.location.href=\'/shopping_cart/purchases/review_shoppingcart\';" /></p></div>';
		var itemtop = '<div class="shoppingcart_itemtop"><div class="shoppingcart_itemname"><p><strong>Name</strong></p></div><div class="shoppingcart_itemprice"><p><strong>Price</strong></p></div><div class="shoppingcart_itemqty"><p class="right"><strong>Qty</strong></p></div></div>';
		
		ajaxcart = ajaxcart.join("\n");
		
		$('#title').html(itemtop);
		$("#item").html(ajaxcart).slideDown();
		$("#checkout").html(cartbottom);
		
	// Response contains errors
	} else {
		var errors = new Array;
 
		if (typeof(response.data) == ("object" || "array")) {
			$.each(response.data, function(key, value) {
				var text = (isNaN(key)) ? key +": "+ value : value;
 
				errors[errors.length] = "<li>"+ text +"</li>";
			});
		} else {
			errors[errors.length] = "<li>"+ response.data +"</li>";
		}
 
		errors = errors.join("\n");
		$("#cartError").html(errors).slideDown();
	}
 
	// Remove box after 5 seconds
	setTimeout(function() {
		$(".cartError").slideUp();
	}, 5000);
 
	return false;
}
function removeSuccess(response, status) {
	// Response was a success
	if (response.success === true) {
		var ajaxcart = new Array;
		if (typeof(response.data) == ("object" || "array")) {
        	$.each(response.data, function(index, data) {
        		var cartitems = '<div class="shoppingcart_item"><div class="shoppingcart_itemname"><p>'+data.name+ '</p><form onsubmit="return false;" id="removeForm" method="post"><input type="hidden" name="data[Cart][id]" id="CartId" value="'+data.id+'"><button type="submit" onclick="removeItem();">remove</button></form></div><div class="shoppingcart_itemprice"><p>$'+data.price+'</p></div><div class="shoppingcart_itemqty"><p class="right">'+data.qty+'</p></div></div>';

            	ajaxcart[ajaxcart.length] = cartitems;
        	});
    	} else {
        	ajaxcart[ajaxcart.length] = "<li>" + response.data + "</li>";
		}
		var total = response.total;
		var cartbottom = '<div class="cartbottom"><div class="shoppingcart_total"><p>Subtotal $'+total.toFixed(2)+'</p></div><div class="shoppingcart_emptycart"><p class="right"><button type="button" onclick="emptyCart();">empty cart</button></p></div><div class="shoppingcart_checkout"><p class="center"><img src="/shopping_cart/img/cart_proceedtocheckout.png" onclick="window.location.href=\'/shopping_cart/purchases/review_shoppingcart\';" /></p></div>';
		
		ajaxcart = ajaxcart.join("\n");
		
		$("#item").html(ajaxcart);
		$("#checkout").html(cartbottom);
	
	} else if (response.success === 'empty') {
		
		var emptymsg = '<div class="shoppingcart_empty"><p>There are no items in your shopping cart.</p></div>';
		
		$('.cartbottom').hide();
		$('#item').empty();
		$('#title').html(emptymsg);
	
	
	// Response contains errors
	} else {
		var errors = new Array;
 
		if (typeof(response.data) == ("object" || "array")) {
			$.each(response.data, function(key, value) {
				var text = (isNaN(key)) ? key +": "+ value : value;
 
				errors[errors.length] = "<li>"+ text +"</li>";
			});
		} else {
			errors[errors.length] = "<li>"+ response.data +"</li>";
		}
 
		errors = errors.join("\n");
		$("#cartError").html(errors).slideDown();
	}
 
	// Remove box after 5 seconds
	setTimeout(function() {
		$(".cartError").slideUp();
	}, 5000);
 
	return false;
}
function emptySuccess(response, status) {
	// Response was a success
	if (response.success === true) {
 
		var emptymsg = '<div class="shoppingcart_empty"><p>There are no items in your shopping cart.</p></div>';
		
		$('.cartbottom').hide();
		$('#item').empty();
		$('#title').html(emptymsg);
 
	// Response contains errors
	} else {
		var errors = new Array;
 
		if (typeof(response.data) == ("object" || "array")) {
			$.each(response.data, function(key, value) {
				var text = (isNaN(key)) ? key +": "+ value : value;
 
				errors[errors.length] = "<li>"+ text +"</li>";
			});
		} else {
			errors[errors.length] = "<li>"+ response.data +"</li>";
		}
 
		errors = errors.join("\n");
		$("#cartError").html(errors).slideDown();
	}
 
	// Remove box after 5 seconds
	setTimeout(function() {
		$(".cartError").slideUp();
	}, 5000);
 
	return false;
}
/**
 * Handle an AJAX failure
 */
function cartError(XMLHttpRequest, textStatus, errorThrown) {
	var error = "<li>An unexpected error has occurred.</li>";
	$("#cartError").html(error).slideDown();
}	
