     var regDigitsOnly =  /^\s*\d+\s*$/;

			$(function()
			{		
					$(".cartItem").each(function() { addRow($(this)) });

					$(".addToCart").click(function() 
					{		addToCart(itemObj);
					});
			});

		function addToCart(itemObj)
		{	//Is that a new item
			var existingMatch = $(".cartName:contains(" + itemObj.shortname + ")");
			if (existingMatch.length>0)
			{		existingMatch = existingMatch.parent();
					previousQty = parseInt(existingMatch.find(".cartQty").val());
					existingMatch.find(".cartQty").val(++previousQty).data("oldval",previousQty);
					updateTotal();
					$.get("cart.php", {"action":"addItem","id":itemObj.id,"qty":previousQty});			
					return;
			}
			newItem = $(".cartItemTemplate").clone().removeClass("cartItemTemplate").addClass("cartItem");
			newItem.find(".cartQty").val(1);
			newItem.find(".cartName").text(itemObj.shortname);
			newItem.find(".cartPrice").text(parseMoney(itemObj.price)).attr("data-price",parseFloat(itemObj.price));
			newItem.attr("data-id",itemObj.id);
			$("#total").before(newItem);
			newItem.fadeIn("fast");
			addRow(newItem);
			updateTotal();
			$.get("cart.php", {"action":"addItem","id":itemObj.id,"qty":newItem.find(".cartQty").val()});			
		}
		
		function updateTotal()
		{	var total = 0;
			$(".cartItem").each(function()
			{	total += parseInt($(this).find(".cartQty").val()) * parseFloat($(this).find(".cartPrice").attr("data-price"));
			});
			$("#total").text("total: " + parseMoney(total));
		}

		function parseMoney(dblVal)
		{	return dblVal+".00$";
		}
		
		function addRow(row)
		{	//Row represent a cartItem element
				var field = row.find(".cartQty");
				field.change(function() {
					if (field.val() == "0" || field.val() == "")
						row.fadeOut("fast", function() { $(this).remove(); cartLogic(); }).find(".cartQty").val(0);				
					if (!regDigitsOnly.test(field.val()))
						field.val(field.data("oldval"));
					field.data("oldval",field.val());
					$.get("cart.php", {"action":"addItem","id":row.attr("data-id"),"qty":row.find(".cartQty").val()});			
					updateTotal();
				});
				field.data("oldval",field.val());
				cartLogic();
		}

		function cartLogic()
		{	var cart = $("#cart");
			if (cart.find(".cartItem").length > 0)
				cart.show("slow");
			else
				cart.hide("slow");
			
		}
