if(!window.BAGGERS) var BAGGERS = {};

BAGGERS.Cart = {
	UpdateQty: function(item,size,sku,offset,anchor) {
		anchor.style.visibility = 'hidden';
		var el  = $('qty_'+item+'sku_'+sku);
		var qty = (el.innerHTML * 1) + offset;
		el.update(qty);
		var url = "/ajax/updateItemQty.php";
		if(offset < 0) {
			var params = 'removefromcart=' + item + '&size=' + size + '&part_sku=' + sku;
		} else {
			var params = 'addtocart=' + item + '&size=' + size + '&part_sku=' + sku;
		}

		new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: params,
				onSuccess: function(res,json) {
					anchor.style.visibility = 'visible';
					if(json.subtotal == "0.00") {
						$('totals').hide();
						$('checkout').hide();
						new Insertion.Bottom('sideContent','<p id="emptyMessage">You cart is currently empty</p>')
					} else {
						$('totals').show();
						$('checkout').show();
						if($('emptyMessage')) $('emptyMessage').hide();
					}
					if(qty < 1) {
						$('product_'+item).remove();
					} else if(qty > 1) {
						$('delete_'+item).hide();
						$('remove_'+item).show();
					} else {
						$('delete_'+item).show();
						$('remove_'+item).hide();
					}
					
					$('subtotal').update(formatCurrency(json.subtotal));
					
				}.bind(this)
			}
		);
	},
	SetShipping: function() {

		var url = "/ajax/setshipping.php";

		var form = $('checkoutForm');

		var shippingmethods = form.getInputs('radio', 'shippingMethod');

		var shippingmethod = '';
		shippingmethods.each(function(sm){
			if (sm.checked) {
				shippingmethod=sm.value;
			}
		}.bind(this));

		if (shippingmethod=='') {
			alert('Please select a shipping method.');
			return false;
		}

		
		if (!$F('ship_zip').match(/^\d{5}|^\w{3} \w{3}/)) {
			if(!$F('zip').match(/^\d{5}|^\w{3} \w{3}/)) {
				alert('Please enter a valid zip code.');
				if($('shippingMethod_05').checked) $('shippingMethod_05').checked = false;
				if($('shippingMethod_04').checked) $('shippingMethod_04').checked = false;
				if($('shippingMethod_03').checked) $('shippingMethod_03').checked = false;
				if($('shippingMethod_02').checked) $('shippingMethod_02').checked = false;
				if($('shippingMethod_01').checked) $('shippingMethod_01').checked = false;
				return false;
			} else {
				$('ship_zip').value = $('zip').value;
				$('ship_country').value = $('country').value;
			}
			
		}
		
		if (shippingmethod && $F('ship_zip').match(/^\d{5}|^\w{3} \w{3}/)) {
			var params = 'shippingMethod=' + shippingmethod + '&zip=' + $F('ship_zip');
			new Ajax.Request(
				url,
				{
					method: 'post',
					parameters: params,
					onSuccess: function(res,json) {
						$('checkout_subtotal').update(formatCurrency(json.subtotal));
						$('checkout_shipping').update(formatCurrency(json.shipping));
						//$('checkout_handling').update(formatCurrency(json.handling));
						$('checkout_total').update(formatCurrency(json.total));
						
						$('order_subTotal').update(formatCurrency(json.subtotal));
						$('order_shippingTotal').update(formatCurrency(json.shipping));
						$('order_overallTotal').update(formatCurrency(json.total));

						$('paymentInfo').show();
						$('additionalInfo').show();

					}
				}
			);
		}
	}
};

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}
