
function updateStates(country) {

	$('#shipping-zip').removeAttr('disabled');

	if (country == 'US') {
		$('#shipping-zip-label').text('Zip Code:');
	} else if (country == 'CA') {
		$('#shipping-zip-label').text('Postal Code:');
	} else {
		$('#shipping-zip').attr('disabled','disabled');
		$('#internationalCustomers').click();
	}

	$('#shipping-zip').val('');

	return;

	var i, allStates, allProvinces, states, options, $shippingState;

	allStates = { AL:'Alabama',AK:'Alaska',AZ:'Arizona',AR:'Arkansas',
		CA:'California',CO:'Colorado',CT:'Connecticut',DE:'Delaware',
		DC:'District of Columbia',FL:'Florida',GA:'Georgia',HI:'Hawaii',ID:'Idaho',
		IL:'Illinois','IN':'Indiana',IA:'Iowa',KS:'Kansas',KY:'Kentucky',
		LA:'Louisiana',ME:'Maine',MD:'Maryland',MA:'Massachusetts',MI:'Michigan',
		MN:'Minnesota',MS:'Mississippi',MO:'Missouri',MT:'Montana',NE:'Nebraska',
		NV:'Nevada',NH:'New Hampshire',NJ:'New Jersey',NM:'New Mexico',NY:'New York',
		NC:'North Carolina',ND:'North Dakota',OH:'Ohio',OK:'Oklahoma',OR:'Oregon',
		PA:'Pennsylvania',RI:'Rhode Island',SC:'South Carolina',SD:'South Dakota',
		TN:'Tennessee',TX:'Texas',UT:'Utah',VT:'Vermont',VA:'Virginia',
		WA:'Washington',WV:'West Virginia',WI:'Wisconsin',WY:'Wyoming'
	};

  allProvinces = { ON:'Ontario',QC:'Quebec',NS:'Nova Scotia',
		NB:'New Brunswick',MB:'Manitoba',BC:'British Columbia',
		PE:'Prince Edward Island',SK:'Saskatchewan',AB:'Alberta',
		NL:'Newfoundland &amp; Labrador',NT:'Northwest Territories',YT:'Yukon',
		NU:'Nunavut'
	};

	$shippingState = $('select#shipping-state');

	switch(country) {
	case 'US': states = allStates;    break;
	case 'CA': states = allProvinces; break;
	case 'IN': states = false;         break;
	}

	options = '<option value="" selected="selected">State/Province...</option>';

	if (states) {
		// CA/US
		$shippingState.removeAttr('disabled');

		for (i in states) {
			if (i.length === 2)
				options += '<option value="' + i + '">' + states[i] + '</option>';
		}
	} else {
		// International
		$shippingState.attr('disabled','disabled');
		$('#internationalCustomers').click();
	}

	$shippingState.html(options);

	updateUniform('select#shipping-state');
}

/**
 * Update cart updates the items, but cannot add or remove rows from the cart
 */
function updateCart(cart) {
	// Update items
	$(cart.items).each(function (i, e) {
		// Format the price as a currency and add setup fee
		var price = formatCurrency(e.price);
		if (e.setup_fee) {
			price += ' + ' + formatCurrency(e.setup_fee) + ' setup fee';
		}

		// Update the individual columns for this row
		$('#item-' + e.idx)
			.find('.total').text(formatCurrency(e.cost)).end()
			.find('.price').text(price).end()
			// Update qty text box on parent items
			.find('td.qty')
				.find('input[name=quantity]').val(e.quantity).end()
				// Update qty field on child items
				.find('div.qty').text(e.quantity).end();
	});

	$('#subtotal').text(formatCurrency(cart.subtotal_pre_coupon));
	$('#grand-total').text(formatCurrency(cart.grand_total));
	$('#cart-total-header').text(formatCurrency(cart.grand_total));

	$('#shipping-flatrate-total').hide();

	$('#shipping-cost').text(formatCurrency(cart.shipping_estimate));

	if (cart.shipping_discount) {
		// We have a discount, but it doesn't completely cover the cost
		$('#shipping-discount-total').show();
		$('#shipping-discount').text('-' + formatCurrency(cart.shipping_discount));
	} else {
		// No shipping discount
		$('#shipping-discount-total').hide();
	}

	if (cart.coupon_code) {
		$('#coupon-area form').hide();
		$('#coupon-response').hide();
		$('#coupon-applied')
			.find('h5').html(cart.coupon_name.toUpperCase()).end()
			.show();
	} else {
		$('#coupon-applied').hide();
		$('#coupon-area form').show();
		$('#coupon-response').show().text('');
	}

	$('#coupon-field').val(cart.coupon_code);

	$('#discount').text('-' + formatCurrency(cart.discount_amount));
	$('#discount-total').toggle(cart.discount_amount > 0);

		$('#discount-total span.cost-category').text('Discount' +
			(cart.discount_percent ? ' (' + cart.discount_percent + '%)' : '')
		);

	$('#add-coupon-code').toggle(cart.discount_amount == 0);

	if (cart.tax_cost) {
		$('#tax-cost').text(formatCurrency(cart.tax_cost));
		$('#tax-total').show();
	} else {
		$('#tax-total').hide();
	}

	// Revalidate the other fields now that they've changed
	//$('#cart input.qty-input').validate();
}

$(function () {
	$('input[type=submit].update').hide();

	$('#shipping-country').change(function () {
		//updateTotals();
		updateStates($(this).val());
		//$('#shipping-info').submit();
	});

	$('#shipping-zip').change(function () {
		if ($(this).validate()) {
			$('#shipping-info').submit();
		}
	}).validate({
		length : [5,7],
		filter : /^[0-9a-zA-Z]/,
		callback : function (value) {
			var country = $('#shipping-country').val();

			var match = false;

			if (country == 'US') {
				match = value.match(/^\d{5}$/);
			} else if (country == 'CA') {
				match = value.match(/^([a-z]\d[a-z]) ?(\d[a-z]\d)$/i);
			}

			if (match)
				return true;

			return "Invalid zip/postal code"
		},
		transform : function(value) {
			return value.toUpperCase().replace(/^([A-Z]\d[A-Z]) ?(\d[A-Z]\d)$/, '$1 $2');
		}
	}).uniform();

	$('#shipping-state').change(function () {
		if ($(this).val() !== '') {
			$('#shipping-info').submit();
		}
	}).validate({});

	/* NOTE Reenable this for expedited shipping
	$('#shipping-method').change(function () {
		$('#shipping-info').submit();
	});
	*/

	$('#cart input.qty-input').each(function (i, f) {
		var minQty = parseInt($(f).parent().find('input[name=min_quantity]').val(), 10);
		$(f).validate({
			filter     : /^[0-9]$/,
			max_length : 4,
			min        : minQty,
			max        : 9999,
			message    : 'This item as a minimum quantity of ' + minQty
		});
	});

	$('td.qty form.qty').ajaxForm({
		success : function (data) {
			updateCart(data.cart);
		},
		event : [ 'submit', 'change' ]
	});

	$('#shipping-info,').ajaxForm(function (data) {
		updateCart(data.cart);
	});

	$('#coupon-code').ajaxForm({
		response : '#coupon-response',
		success  : function (data) { updateCart(data.cart); }
	});

	$('#remove-coupon').click(function (event) {
		event.preventDefault();
		$.post('/coupons/clear.json', {}, function (data) { updateCart(data.cart); }, 'json');
		return false;
	});

	$('#btn-checkout').click(function (event) {
		if (!$('#shipping-info').isValid()) {
			event.preventDefault();
			return false;
			//window.location.assign('/checkout');
		}
	});

	$('.btn-remove').click(function (event) {
		return confirm('Are you sure you want to remove this item from your cart?');
	});

	$('#btn-update').click(function (event) {
		event.preventDefault();
	});

	$('#cart tr.item:even').addClass('even');
	$('#cart tr.item:odd').addClass('odd');

	if (typeof(cart_data) == 'function') {
		updateCart(cart_data());
	}
});


