if (typeof P4 == 'undefined')
	var P4 = {};
// define P4 so we can later include P4.Util

var ShoppingCart =
{
	init: function()
	{
		$$('input.remove-button').each(function(checkbox)
		                               {
			                               Event.observe(checkbox, "click", this.recalculate.bind(this), true);
			                               Event.observe(checkbox, "change", this.recalculate.bind(this), true);
		                               }.bind(this));
		$$('input.quantityInput').each(function(checkbox)
		                               {
			                               Event.observe(checkbox, "keyup", this.recalculate.bind(this), true);
			                               Event.observe(checkbox, "change", this.recalculate.bind(this), true);
		                               }.bind(this));
		this.orderTotalElement = $$('.order-total-row .total-column')[0];
		this.shippingElement = $$('.shopping-cart tr.shipping-fee-row .total-column')[0];
		this.dutyElement = $$('.shopping-cart tr.duty-fee-row .total-column')[0];
		this.taxElement = $$('.shopping-cart tr.tax-fee-row .total-column')[0];
		var orderTotal = this.orderTotalElement.innerHTML;
		this.fractionSeparator = P4.Util.getFractionSeparator(orderTotal);
		this.currencySymbol = P4.Util.getCurrencySymbol(orderTotal);
		var getFee = function(type)
		{
			var elements = $$('.shopping-cart tr.' + type + '-row .total-column');
			if (elements && elements.length > 0)
				return P4.Util.parseCurrency(elements[0].innerHTML);
			else
				return 0;
		};
		this.handlingFee = getFee('handling-fee');
		this.couponDiscount = getFee('coupon');
		this.promoDiscount = getFee('promo');
		this.recalculate();
		var intlCountry = $('internationalShipping_country')
		if (intlCountry)
		{
			Element.observe(intlCountry, 'change', function()
			{
				if (intlCountry.value)
				{
					new Ajax.Request("/getInternationalShippingCost",
					                 {
						                 parameters: $H({ country: intlCountry.value }).toQueryString(),
						                 onFailure: P4.onFailure,
						                 onException: P4.onException,
						                 onSuccess: function(xmlResponse, xjson)
						                 {
							                 var json = eval(xmlResponse.responseText);
							                 if (json.error)
							                 {
								                 window.alert(json.error);
							                 }
							                 else
							                 {
								                 var costs = [];
								                 costs.push('<div class="internationalShippingMessage">Good news! We can ship to you. The following costs will apply:</div>');
								                 costs.push('<table class="internationalShippingResults">');
								                 if (json.duty)
									                 costs.push('<tr><th>Duty</th><td>' + json.duty + '</td></tr>');
								                 if (json.tax)
									                 costs.push('<tr><th>Tax</th><td>' + json.tax + '</td></tr>');
								                 if (json.shipping)
									                 costs.push('<tr><th>Shipping</th><td>' + json.shipping + '</td></tr>');
								                 if (json.insurance)
									                 costs.push('<tr><th>Insurance</th><td>' + json.insurance + '</td></tr>');
								                 costs.push('</table>');
								                 var dest = $('internationalShippingCosts');
								                 dest.update(costs.join(''));
								                 dest.show();
							                 }
						                 }
					                 });
				}
			});
		}
	},

	recalculate: function()
	{
		var shipping = this.baseShipping;
		var duty = 0;
		var tax = 0;
		var orderTotal = this.handlingFee + this.couponDiscount + this.promoDiscount;
		$$('.shopping-cart tbody tr.item-row').each(function(row, index)
		                                            {
			                                            var quantityElement = row.select('.quantityInput')[0];
			                                            var removeElement = row.getElementsByClassName('.remove-button')[0];
			                                            var quantity = quantityElement.value;
			                                            if (!isNaN(quantity))
			                                            {
				                                            var priceElement = row.select('.price-column')[0];
				                                            var price = P4.Util.parseCurrency(priceElement.innerHTML);
				                                            if (quantity < 0)
					                                            quantity = 0;
				                                            var total = Math.floor(quantity) * price;
				                                            if (removeElement && removeElement.checked)
					                                            total = 0;
				                                            row.select('.total-column')[0].innerHTML =
						                                            P4.Util.formatNumber(total, 2, this.fractionSeparator, this.currencySymbol);
				                                            orderTotal += total;
				                                            shipping += Math.floor(quantity) * this.shipping[index];
				                                            duty += Math.floor(quantity) * this.duty[index];
				                                            tax += Math.floor(quantity) * this.tax[index];
			                                            }
		                                            }.bind(this));
		orderTotal += shipping + duty + tax;
		this.orderTotalElement.innerHTML = P4.Util.formatNumber(orderTotal, 2, this.fractionSeparator, this.currencySymbol);
		this.shippingElement.innerHTML = P4.Util.formatNumber(shipping, 2, this.fractionSeparator, this.currencySymbol);
		if (this.dutyElement)
			this.dutyElement.innerHTML = P4.Util.formatNumber(duty, 2, this.fractionSeparator, this.currencySymbol);
		if (this.taxElement)
			this.taxElement.innerHTML = P4.Util.formatNumber(tax, 2, this.fractionSeparator, this.currencySymbol);
		this.innerHTML = P4.Util.formatNumber(orderTotal, 2, this.fractionSeparator, this.currencySymbol);
	}
};
