function addItem(id) {
	var pid = "item_"+id;
	var sku="";
	var qty ="";
	var details = $("#"+pid+"_details").val().split("|");
	sku = details[0];
	qty = $("#"+pid+"_qty").val();
	qty = Math.floor(Math.abs(qty));

	if(sku && qty) {
		$("#"+pid+" .msg").html("").show();
		function form_processed(results){
			var result = results.split("|");
			if(result[0] == "noqty") {
				$("#"+pid+" .msg").html("<span class=\"error\">Only "+result[1]+" in stock</span>").pause(3000).fadeOut("slow");
			} else {
				$("#"+pid+" .msg").html("<span class=\"success\">Your items were added to the cart</span>").pause(2000).fadeOut("slow");
				$("#quick-cart em").html("Total items: "+result[0]);
				//$("#"+pid+" input.button").attr({ disabled: "disabled" }).css({ color: "#808080", cursor: "default" }).blur();
			}
		}
		$.ajax({type:'GET', url:'/ajax/cart.php', data:"action=add&itemsku="+sku+"&itemqty="+qty, success:form_processed});
		$("#"+pid+" .msg").html("adding item...");
	} else {
		$("#"+pid+" .msg").html("<span class=\"error\">Please choose a color/size and quantity</span>");
	}
	return false;
}

function updateItem(sku) {
	var pid = "item_"+sku;
	var qty="";
	var item_cost="";
	qty = $("#"+pid+"_qty").val();
	qty = Math.floor(Math.abs(qty));
	item_cost = $("#"+pid+" .quantity-price h4").html();
	item_cost = item_cost.substr(1);
	var subtotal = (qty * item_cost).toFixed(2);

	if(sku && qty) {
		$("#"+pid+" .msg em").html("").show();
		function form_processed(results){
			var result = results.split("|");
			if(result[0] == "noqty") {
				$("#"+pid+" .msg em").html("<span class=\"error\">Only "+result[1]+" in stock</span>").pause(3000).fadeOut("slow");
			} else {
				$("#"+pid+" .msg em").html("<span class=\"success\">Your item was updated</span>").pause(2000).fadeOut("slow");
				if(result[1] > 0) {
					$("#disc").html("Total Discount(s) =");
					$("#total h4").html("$"+number_format(result[1], 2, '.', ',')).show();
				} else {
					$("#disc").html("");
					$("#total h4").html("").hide();
				}
				$("#total h5").html("$"+number_format(result[0], 2, '.', ','));
				$("#"+pid+" .quantity-price h5").html("$"+number_format(subtotal, 2, '.', ','));
			}
		}
		$.ajax({type:'GET', url:'/ajax/cart.php', data:"action=update&itemsku="+sku+"&itemqty="+qty, success:form_processed});
		$("#"+pid+" .msg em").html("updating item...");
	} else {
		$("#"+pid+" .msg em").html("<span class=\"error\">There was an error, please try again</span>");
	}
	return false;
}

function removeItem(sku) {
	var pid = "item_"+sku;

	if(sku) {
		$("#"+pid+" .msg em").html("").show();
		function form_processed(results){
			var result = results.split("|");
			if ($(".cart-row:visible").size() > 2 ) {
				$("#"+pid).addClass("hidden").fadeOut("slow");
			} else {
				removeAllItems();
			}
			if(result[1] > 0) {
				$("#disc").html("Total Discount(s) =");
				$("#total h4").html("$"+number_format(result[1], 2, '.', ',')).show();
			} else {
				$("#disc").html("");
				$("#total h4").html("").hide();
			}
			$("#total h5").html("$"+number_format(result[0], 2, '.', ','));
		}
		$.ajax({type:'GET', url:'/ajax/cart.php', data:"action=remove&itemsku="+sku, success:form_processed});
	} else {
		$("#"+pid+" .msg em").html("<span class=\"error\">There was an error, please try again</span>");
	}
	return false;
}

function removeAllItems() {
	function form_processed(){
		$(".cart-row:visible").fadeOut("slow");
		$("#cart h2").html("Your Cart is Empty, <a href=\"https://shop.safetyfirstracing.com/\">Continue Shopping &raquo;</a>").pause(1000).fadeIn("fast");
	}
	$.ajax({type:'GET', url:'/ajax/cart.php', data:"action=removeall", success:form_processed});
	return false;
}