{% extends "shop/checkout.html" %}
{% load i18n %}

{% block extra_head %}
{{ block.super }}
<script type="text/javascript">
$(function() {

	var sameShipping = $('#id_same_billing_shipping');
	
	// prepopulate shipping fields with billing values if "same as" checkbox 
	// checked by iterating the billing fields, mapping each billing field name  
	// to the shipping field name and setting its value
	$('#checkout-form').submit(function() {
		if (sameShipping.attr('checked')) {
			$('input[name^=billing_]').each(function() {
				var shippingName = this.name.replace('billing_', 'shipping_');
				$('input[name=' + shippingName + ']').attr('value', this.value);
			});
		}
	});

	// show/hide shipping fields on change of "same as" checkbox and call on load
	sameShipping.change(function() {
		$('#shipping_fields')[sameShipping.attr('checked') ? 'hide' : 'show']();
	}).change(); 

});
</script>
{% endblock %}

{% block fields %}
{% if cart.has_items %}
{% if not request.user.is_authenticated and settings.SHOP_CHECKOUT_ACCOUNT_ENABLED %}
<p>If you have an existing accout or would like to create one, please
    <a href="{{ settings.SHOP_LOGIN_URL }}">log in or sign up</a>.</p>
{% endif %}
<fieldset>
	<legend>{% trans "Billing Details" %}</legend>
	<ul>{{ form.billing_detail_fields.as_ul }}</ul>
</fieldset>
<fieldset>
	<legend>{% trans "Delivery Details" %}</legend>
	<ul class="checkbox">{{ form.same_billing_shipping_field.as_ul }}</ul>
	<ul id="shipping_fields">{{ form.shipping_detail_fields.as_ul }}</ul>
	<ul>{{ form.additional_instructions_field.as_ul }}</ul>
</fieldset>
{% if not settings.SHOP_CHECKOUT_STEPS_SPLIT %}
{% include "shop/payment_fields.html" %}
{% endif %}
<ul class="checkbox">{{ form.remember_field.as_ul }}</ul>
<ul>{{ form.other_fields.as_ul }}</ul>
{% endif %}
{% endblock %}

