if (typeof frontend == 'undefined') {
	var frontend = {};
}

frontend.checkout = {};
frontend.checkout.cart = {
	init: function(updateCartUrl) {
		$('#CheckoutCart select.product_quantity').bind('change', function() {
			$('#dialog-loading').dialog('open');

			$('#CheckoutCart').attr('action', updateCartUrl);
			$('#CheckoutCart').submit();
			return true;
		});

		$('#CheckoutCart a.delete').bind('click', function() {
			$(this).parents('tr').find('td select.product_quantity').val(0);
			$('#dialog-loading').dialog('open');

			$('#CheckoutCart').attr('action', updateCartUrl);
			$('#CheckoutCart').submit();
			return true;
		});
	}
};

frontend.checkout.shipping = {
	init: function(updateShippingUrl, rates, total) {
		$('#CheckoutShipping').validate({
			ShippingCountry: ['required']
		});

		$('#ShippingCountry').bind('change', function() {
			if (this.value != '') {
				$('#dialog-loading').dialog('open');

				$('#CheckoutShipping').attr('action', updateShippingUrl);
				$('#CheckoutShipping').submit();
			}

			return true;
		});

		var updateAmount = function() {
			var method = $('input.shipping_method:checked').val();
			var amount = parseFloat(rates[method]);

			$('em.shipping_rate').html('€' + amount.toFixed(2));

			if (method == 'mail') {
				if ($('#ShippingLetter').is(':checked')) {
					$('em.shipping_letter').html('€' + parseFloat(rates['letter']).toFixed(2));
					amount += parseFloat(rates['letter']);
				} else {
					$('em.shipping_letter').html('€0.00');
				}

				$('#ShippingLetter').parents('tr').show();
			} else {
				$('#ShippingLetter').parents('tr').hide();
			}
			var totalAmount = total + amount;

			$('em.shipping_amount').html('€' + amount.toFixed(2));
			$('em.total_amount').html('€' + totalAmount.toFixed(2));
		};
		$('input.shipping_method').bind('click', updateAmount);
		$('#ShippingLetter').bind('click', updateAmount);
	}
};

frontend.checkout.contact = {
	init: function(customerLoginUrl) {
		// Login
		$('#CheckoutContact a.contact_login').bind('click', function() {
			$('#dialog-loading').dialog('open');

			$('#CheckoutContact').attr('action', customerLoginUrl);
			$('#CheckoutContact').unbind('submit').submit();
			return true;
		});

		// Guest ?
		$('#CustomerGuest0').bind('click', function() {
			$('#CustomerPasswordBox').show();
		});
		$('#CustomerGuest1').bind('click', function() {
			$('#CustomerPasswordBox').hide();
		});

		// Validation
		$('#CheckoutContact').validate({
			// Customer
			CustomerName: ['required'],
			CustomerEmail: ['required', 'email'],
			CustomerEmailConfirmation: ['required', 'email'],
			CustomerPhone: ['required'],
			CustomerBirthday: ['required', 'date'],
			CustomerPassword: ['required', ['minLength', 6]],
			CustomerPasswordConfirmation: ['required', ['minLength', 6]],

			// Quote
			QuoteShippingAddress: ['required'],
			QuoteShippingZipCode: ['required'],
			QuoteShippingCity: ['required'],
			QuoteShippingCountry: ['required'],
			QuoteBillingAddress: ['required'],
			QuoteBillingZipCode: ['required'],
			QuoteBillingCity: ['required'],
			QuoteBillingCountry: ['required']
		});

		// Same Address ?
		$('#QuoteShippingAddress, #QuoteShippingAddressExtra, #QuoteShippingZipCode, #QuoteShippingCity').bind('change', function(checked) {
			if ($('#QuoteUseSameAddress').attr('checked')) {
				var _id = this.id.replace('QuoteShipping', 'QuoteBilling');
				$('#' + _id).val(this.value);
			}
		});

		$('#QuoteUseSameAddress').bind('click', function() {
			$('#QuoteBillingAddress, #QuoteBillingAddressExtra, #QuoteBillingZipCode, #QuoteBillingCity, #QuoteBillingCountry')
				.attr('readonly', this.checked);

			if (this.checked) {
				$('#QuoteShippingAddress, #QuoteShippingAddressExtra, #QuoteShippingZipCode, #QuoteShippingCity, #QuoteShippingCountry')
				.each(function() {
					var _id = this.id.replace('QuoteShipping', 'QuoteBilling');
					$('#' + _id).val(this.value);
				});
			}
		});
	}
};

frontend.checkout.payment = {
	init: function() {
		$('#CheckoutPayment').submit(function() {
			if (!$('#QuoteAccept').attr('checked')) {
				$('#dialog-alert')
					.html(i18nCatalog['checkout-payment-accept'])
					.dialog('open');

				return false;
			}

			return true;
		});
        }
};


