{% if addresses %}
<div class="radio">
  <label><input type="radio" name="shipping_address" value="existing" id="shipping-address-existing" checked="checked" />
  {{ text_address_existing }}</label>
</div>
<div id="shipping-existing">
  <select name="address_id" class="form-control">
    {% for address in addresses %} 
    {% if address.address_id == address_id %} 
    <option value="{{ address.address_id }}" selected="selected">{{ address.firstname }} {{ address.lastname }} , {{ address.address_1 }} , {{ address.city }} , {{ address.postcode }} , {{ address.zone }} , {{ address.country }}</option>
    {% else %}   
    <option value="{{ address.address_id }}">{{ address.firstname }} {{ address.lastname }} , {{ address.address_1 }} , {{ address.city }} , {{ address.postcode }} , {{ address.zone }} , {{ address.country }}</option>
    {% endif %} 
    {% endfor %}
  </select>
</div>
<div class="radio">
  <label><input type="radio" name="shipping_address" value="new" id="shipping-address-new" />
  {{ text_address_new }}</label>
</div>
{% else %}   
<input type="radio" name="shipping_address" value="new" id="shipping-address-new" class="hide" checked="checked" />
{% endif %}
<div id="shipping-new" style="display: {{ addresses ? 'none' : 'block' }};">
{% for field in fields %} 
  {% if field == 'country' %} 
    {% if attribute(_context, 'field_' ~ field).display %}
	<div class="col-sm-6{{ attribute(_context, 'field_' ~ field).required ? ' required' }}">
	  <label class="control-label">{{ entry_country }}</label>
	  <select name="country_id" class="form-control" id="input-shipping-country">
	  {% for country in countries %} 
		{% if country.country_id == country_id %} 
		<option value="{{ country.country_id }}" selected="selected">{{ country.name }}</option>
		{% else %}   
		<option value="{{ country.country_id }}">{{ country.name }}</option>
		{% endif %}
	  {% endfor %}
	  </select>
	</div>
	{% else %}   
	<select name="country_id" class="hide">
	{% for country in countries %} 
	  {% if country.country_id == country_id %} 
	  <option value="{{ country.country_id }}" selected="selected">{{ country.name }}</option>
	  {% else %}   
	  <option value="{{ country.country_id }}">{{ country.name }}</option>
	  {% endif %}
	{% endfor %}
	</select>
	{% endif %}
  {% elseif field == 'zone' %}
    {% if attribute(_context, 'field_' ~ field).display %}
	<div class="col-sm-6{{ attribute(_context, 'field_' ~ field).required ? ' required' }}">
	  <label class="control-label">{{ entry_zone }}</label>
	  <select name="zone_id" class="form-control" id="input-shipping-zone"></select>
	</div>
	{% else %}   
	  <select name="zone_id" class="hide"></select>
	{% endif %}
  {% else %}   
    {% if attribute(_context, 'field_' ~ field).display %}
	<div{{ field == 'postcode' ? ' id="shipping-postcode-required"' }} class="col-sm-6{{ attribute(_context, 'field_' ~ field).required ? ' required' }}">
	  <label class="control-label" for="input-shipping-{{ field|replace({'_' : '-'}) }}">{{ attribute(_context, 'entry_' ~ field) }}</label>
	  <input type="text" name="{{ field }}" placeholder="{{ attribute(_context, 'field_' ~ field).placeholder }}" value="{{ attribute(_context, 'field_' ~ field).default }}" class="form-control"  id="input-shipping-{{ field|replace({'_' : '-'}) }}" />
	</div>
	{% else %}   
	<input type="text" name="{{ field }}" value="{{ attribute(_context, 'field_' ~ field).default }}" class="hide" />
	{% endif %} 
  {% endif %}
{% endfor %}
<!-- CUSTOM FIELDS -->
<div id="custom-field-shipping">
  {% for custom_field in custom_fields %} 
  {% if custom_field.location == 'address' %} 
	<div class="col-sm-6 custom-field" data-sort="{{ custom_field.sort_order }}" id="shipping-custom-field{{ custom_field.custom_field_id }}">
	  <label class="control-label" for="input-shipping-custom-field{{ custom_field.custom_field_id }}">{{ custom_field.name }}</label>
	  {% if custom_field.type == 'select' %} 
		<select name="custom_field[{{ custom_field.location }}][{{ custom_field.custom_field_id }}]" id="input-shipping-custom-field{{ custom_field.custom_field_id }}" class="form-control">
		  <option value="">{{ text_select }}</option>
		  {% for custom_field_value in custom_field.custom_field_value %} 
		  {% if guest_custom_field.custom_field.custom_field_id and custom_field_value.custom_field_value_id == guest_custom_field.custom_field.custom_field_id %} 
		  <option value="{{ custom_field_value.custom_field_value_id }}" selected="selected">{{ custom_field_value.name }}</option>
		  {% else %}   
		  <option value="{{ custom_field_value.custom_field_value_id }}">{{ custom_field_value.name }}</option>
		  {% endif %}
		  {% endfor %}
		</select>
	  {% endif %}
	  {% if custom_field.type == 'radio' %} 
		{% for custom_field_value in custom_field %} 
		  <div class="radio">
			{% if guest_custom_field.custom_field.custom_field_id and custom_field_value.custom_field_value_id == guest_custom_field.custom_field.custom_field_id %} 
			<label>
			  <input type="radio" name="custom_field[{{ custom_field.location }}][{{ custom_field.custom_field_id }}]" value="{{ custom_field_value.custom_field_value_id }}" checked="checked" />
			  {{ custom_field_value.name }}</label>
			{% else %}   
			<label>
			  <input type="radio" name="custom_field[{{ custom_field.location }}][{{ custom_field.custom_field_id }}]" value="{{ custom_field_value.custom_field_value_id }}" />
			  {{ custom_field_value.name }}</label>
			{% endif %}
		  </div>
		{% endfor %}
	  {% endif %}
	  {% if custom_field.type == 'checkbox' %} 
		{% for custom_field_value in custom_field %} 
		  <div class="checkbox">
			{% if guest_custom_field.custom_field.custom_field_id and custom_field_value.custom_field_value_id in guest_custom_field.custom_field.custom_field_id %}
			<label>
			  <input type="checkbox" name="custom_field[{{ custom_field.location}}][{{ custom_field.custom_field_id }}][]" value="{{ custom_field_value.custom_field_value_id }}" checked="checked" />
			  {{ custom_field_value.name }}</label>
			{% else %}   
			<label>
			  <input type="checkbox" name="custom_field[{{ custom_field.location }}][{{ custom_field.custom_field_id }}][]" value="{{ custom_field_value.custom_field_value_id }}" />
			  {{ custom_field_value.name }}</label>
			{% endif %} 
		  </div>
		{% endfor %}
	  {% endif %}
	  {% if custom_field.type == 'text' %} 
		<input type="text" name="custom_field[{{ custom_field.location }}][{{ custom_field.custom_field_id }}]" value="{{ guest_custom_field[custom_field.custom_field_id] ? guest_custom_field[custom_field.custom_field_id] : custom_field.value }}" placeholder="{{ custom_field.name }}" id="input-shipping-custom-field{{ custom_field.custom_field_id }}" class="form-control" />
	  {% endif %} 
	  {% if custom_field.type == 'textarea' %} 
		<textarea name="custom_field[{{ custom_field.location }}][{{ custom_field.custom_field_id }}]" rows="5" placeholder="{{ custom_field.name }}" id="input-shipping-custom-field{{ custom_field.custom_field_id }}" class="form-control">{{ guest_custom_field[custom_field.custom_field_id] ? guest_custom_field[custom_field.custom_field_id] : custom_field.value }}</textarea>
	  {% endif %} 
	  {% if custom_field.type == 'file' %} 
		<br />
		<button type="button" id="button-shipping-custom-field{{ custom_field.custom_field_id }}" data-loading-text="{{ text_loading }}" class="btn btn-default"><i class="fa fa-upload"></i>{{ button_upload }}</button>
		<input type="hidden" name="custom_field[{{ custom_field.location }}][{{ custom_field.custom_field_id }}]" value="{{ guest_custom_field[custom_field.custom_field_id] ? guest_custom_field[custom_field.custom_field_id] }}" />
	  {% endif %} 
	  {% if custom_field.type == 'date' %} 
		<input type="text" name="custom_field[{{ custom_field.location }}][{{ custom_field.custom_field_id }}]" value="{{ guest_custom_field[custom_field.custom_field_id] ? guest_custom_field[custom_field.custom_field_id] : custom_field.value }}" placeholder="{{ custom_field.name }}" id="input-shipping-custom-field{{ custom_field.custom_field_id }}" class="form-control date" />
	  {% endif %} 
	  {% if custom_field.type == 'time' %} 
		<input type="text" name="custom_field[{{ custom_field.location }}][{{ custom_field.custom_field_id }}]" value="{{ guest_custom_field[custom_field.custom_field_id] ? guest_custom_field[custom_field.custom_field_id] : custom_field.value }}" placeholder="{{ custom_field.name }}" id="input-shipping-custom-field{{ custom_field.custom_field_id }}" class="form-control time" />
	  {% endif %} 
	  {% if custom_field.type == 'datetime' %} 
		<input type="text" name="custom_field[{{ custom_field.location }}][{{ custom_field.custom_field_id }}]" value="{{ guest_custom_field[custom_field.custom_field_id] ? guest_custom_field[custom_field.custom_field_id] : custom_field.value }}" placeholder="{{ custom_field.name }}" id="input-shipping-custom-field{{ custom_field.custom_field_id }}" class="form-control datetime" />
	  {% endif %} 
    </div>
  {% endif %}
  {% endfor %}
</div>
</div><!-- CLOSE WRAPPING BOX -->
<script type="text/javascript"><!--
// Shipping address form functions
$(document).ready(function() {
	$('#shipping-address input[name=\'shipping_address\']').on('change', function() {
		if (this.value == 'new') {
			$('#shipping-existing').slideUp();
			$('#shipping-new').slideDown();
			
			setTimeout(function() {
				$('#shipping-address select[name=\'country_id\']').trigger('change');
			}, 200);
		} else {
			$('#shipping-existing').slideDown();
			$('#shipping-new').slideUp();

			reloadShippingMethodById($('#shipping-address select[name=\'address_id\']').val());
		}
	});
	
	$('#shipping-address input[name=\'shipping_address\']:checked').trigger('change');

	// Sort the custom fields
	$('#custom-field-shipping .custom-field[data-sort]').detach().each(function() {
		if ($(this).attr('data-sort') >= 0 && $(this).attr('data-sort') <= $('#shipping-new .col-sm-6').length) {
			$('#shipping-new .col-sm-6').eq($(this).attr('data-sort')).before(this);
		} 
		
		if ($(this).attr('data-sort') > $('#shipping-new .col-sm-6').length) {
			$('#shipping-new .col-sm-6:last').after(this);
		}
			
		if ($(this).attr('data-sort') < -$('#shipping-new .col-sm-6').length) {
			$('#shipping-new .col-sm-6:first').before(this);
		}
	});

	$('#shipping-address button[id^=\'button-shipping-custom-field\']').on('click', function() {
		var node = this;

		$('#form-upload').remove();

		$('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="file" /></form>');

		$('#form-upload input[name=\'file\']').trigger('click');

		timer = setInterval(function() {
			if ($('#form-upload input[name=\'file\']').val() != '') {
				clearInterval(timer);
				
				$.ajax({
					url: 'index.php?route=tool/upload',
					type: 'post',
					dataType: 'json',
					data: new FormData($('#form-upload')[0]),
					cache: false,
					contentType: false,
					processData: false,
					beforeSend: function() {
						$(node).button('loading');
					},
					complete: function() {
						$(node).button('reset');
					},
					success: function(json) {
						$('.text-danger').remove();
						
						if (json['error']) {
							$(node).parent().find('input[name^=\'custom_field\']').after('<div class="text-danger">' + json['error'] + '</div>');
						}
		
						if (json['success']) {
							alert(json['success']);
		
							$(node).parent().find('input[name^=\'custom_field\']').attr('value', json['file']);
						}
					},
					error: function(xhr, ajaxOptions, thrownError) {
						alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
					}
				});
			}
		}, 500);
	});

	$('#shipping-address select[name=\'zone_id\']').on('change', function() {
		reloadShippingMethod('shipping');
	});

	$('#shipping-address select[name=\'country_id\']').on('change', function() {
		$.ajax({
			url: 'index.php?route=extension/quickcheckout/checkout/country&country_id=' + this.value,
			dataType: 'json',
			cache: false,
			beforeSend: function() {
				$('#shipping-address select[name=\'country_id\']').after('<i class="fa fa-spinner fa-spin"></i>');
			},
			complete: function() {
				$('.fa-spinner').remove();
			},
			success: function(json) {
				if (json['postcode_required'] == '1') {
					$('#shipping-postcode-required').addClass('required');
				} else {
					$('#shipping-postcode-required').removeClass('required');
				}

				html = '';

				if (json['zone'] != '') {
					for (i = 0; i < json['zone'].length; i++) {
						html += '<option value="' + json['zone'][i]['zone_id'] + '"';

						if (json['zone'][i]['zone_id'] == '{{ zone_id }} ') {
							html += ' selected="selected"';
						}

						html += '>' + json['zone'][i]['name'] + '</option>';
					}
				} else {
					html += '<option value="0" selected="selected">{{ text_none }}</option>';
				}

				$('#shipping-address select[name=\'zone_id\']').html(html).trigger('change');
			},
			{% if debug %} 
			error: function(xhr, ajaxOptions, thrownError) {
				alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
			}
			{% endif %} 
		});
	});

	$('#shipping-address select[name=\'address_id\']').on('change', function() {
		if ($('#shipping-address input[name=\'shipping_address\']:checked').val() == 'new') {
			reloadShippingMethod('shipping');
		} else {
			reloadShippingMethodById($('#shipping-address select[name=\'address_id\']').val());
		}
	});
});
//--></script>