		var cart_type = "";
		var jRpc;
		var update_taxes = false;
		var have_state = false;
		var state_name = "";
		
		function addToCart(product_id, quantity)
		{
			if (product_id == undefined || quantity == undefined || quantity == 0)
				return;
				
			// make an RPC to the server adding the product to the cart
			loadingCart("Updating Cart...");					
			jRpc.send(handleCart, {method: 'addToCart', params: {product_id: product_id, quantity: quantity, cart_type: cart_type}});
		}
		
		function updateTaxes(obj)
		{
			if (update_taxes)
				return;
				
			state_name = obj.options[obj.selectedIndex].value;
			if (state_name == "")
			{
				alert("Please select a state or choose 'Outside USA'");
				update_taxes = false;
				return;
			}
			update_taxes = true;
			if (obj.id == 'tax_state')
			{
				var st = document.getElementById('state');
				if (st)
				{
					st.selectedIndex = obj.selectedIndex;
				}
			}
			else
			{
				var st = document.getElementById('tax_state');
				if (st)
				{
					st.selectedIndex = obj.selectedIndex;
				}
			}
			have_state = true;
			jRpc.send(handleUpdateTaxes, {method: 'updateTaxes', params: {state: state_name}});
		}
		
		function handleUpdateTaxes(o)
		{
			if (o.success)
			{
				getCart();
			}
			else
			{
				alert("Error: Unable to set the state for taxes and shipping.");
			}
		}
		
		function setBackupOption()
		{
			var option = $("#backup_option option:selected").val();
			loadingCart("Updating Cart...");	
			if (option != 'NONE')
			{
				$("#customer_info").css("display", "");
				setValidators();
			}
			else
			{
				$("#customer_info").css("display", "none");
				if (frmvalidator != null)
					clearValidators();
			}
			jRpc.send(handleCart, {method: 'setBackupOption', params: {cart_type: cart_type, option: option}});
		}		
		
		function applyCouponCode()
		{
			var code = $("#coupon_code").val();
			if (code != '')
			{
				jRpc.send(handleCart, {method: 'applyCouponCode', params: {cart_type: cart_type, coupon_code: code}});				
			}
		}
		
		function removeFromCart(product_id)
		{
			var ret = confirm("Are you sure you want to remove this from your cart?");
			if (!ret)
				return;
				
			// make an RPC to the server removing the product to the cart
			loadingCart("Updating Cart...");					
			jRpc.send(handleCart, {method: 'removeFromCart', params: {product_id: product_id, cart_type: cart_type}});
		}		
		
		function updateQuantity(product_id)
		{
			var obj = document.getElementById('qty_'+product_id);
			if (obj)
			{
				var qty = obj.value;
				loadingCart("Updating Cart...");					
				jRpc.send(handleCart, {method: 'updateQuantity', params: {product_id: product_id, quantity: qty, cart_type: cart_type}});
			}
		}			
		
	     function isNumberKey(evt)
	      {
	         var charCode = (evt.which) ? evt.which : event.keyCode
	         if (charCode > 31 && (charCode < 48 || charCode > 57))
	            return false;
	
	         return true;
	      }

		
		function emptyCart()
		{
			var ret = confirm("Are you sure you want to remove everything from your cart?");
			if (!ret)
				return;
				
			// make an RPC to empty the cart
			loadingCart("Emptying Cart...");
			jRpc.send(handleCart, {method: 'emptyCart', params: {}});
		}
		
		function getCart()
		{		
			loadingCart("Loading Cart...");
			jRpc.send(handleCart, {method: 'getCart', params: {cart_type: cart_type}});
		}
	
		function loadingCart(msg)
		{
			if ($('#loading_cart').length > 0)	
			{
				$("#loading_cart").html('<img src="/cs-templates/images/loading.gif" hspace="3"/>'+msg);
				$("#loading_cart").css("display", "");							
			}
		}
		
		function handleCart(o)
		{
			if (o.success)
			{
				switch (o.rows.state)
				{
					case 'remove':
					case 'add':
					case 'view':
					default:
					{

						if ($('#shopping_cart').length > 0)
							$('#shopping_cart').html(o.rows.cart);	

						if ($('#cart_total').length > 0)
						{
							var html = o.rows.total;
							if (parseFloat(o.rows.total) > 0)
							{
								html = '<a class="green" href="/?p=/shop/checkout" style="text-decoration: none;">'+html+'</a> ';
								html += '<a class="green" href="/?p=/shop/checkout" style="text-decoration: none;"><img hspace="3" align="absmiddle" src="/cs-templates/images/cart_go.gif" border="0" style="text-decoration: none;"/></a>';
							}
							$('#cart_total').html(html);	
						}
							
						if ($('#loading_cart').length > 0)	
							$("#loading_cart").css("display", "none");		
							
						if (update_taxes)
						{
							update_taxes = false;
							if (state_name != 'OU')
								alert("Sales tax and shipping information have been updated.");
							else
							 	alert("Please note: Tax and shipping information for orders outside of the US are handled by our staff. Please submit your order and we will contact you with payment information as soon as possible.");
						}
						break;
					}
				}
			}
		}
		
		
		$(document).ready(function(){
			jRpc = $.jRpc({
				rpc_url: "/rpc.php"
			});	
			getCart();	
		});	