<div class="modal-header">
  <button type="button" class="pull-right btn btn-danger btn-sm" data-dismiss="modal" aria-hidden="true"><i class="fa fa-close"></i></button>
  <h4 class="modal-title">{{ firstname }} {{ lastname }} - {{ tab_address }}</h4>
</div>
<div class="modal-body">
  <form action="{{ action }}" method="post" enctype="multipart/form-data" id="form-customer-address" class="form-horizontal">
	<input type="hidden" name="customer_group_id" value="{{ customer_group_id }}" />
	<select name="customer_group_id" id="input-customer-group" class="hidden">
      {% for customer_group in customer_groups %}
        {% if customer_group.customer_group_id == customer_group_id %}
          <option value="{{ customer_group.customer_group_id }}" selected="selected">{{ customer_group.name }}</option>
          {% else %}
          <option value="{{ customer_group.customer_group_id }}">{{ customer_group.name }}</option>
        {% endif %}
      {% endfor %}
    </select>
	<div id="tab-general">
        <div class="row">
          <div class="col-sm-2">
            <ul class="nav nav-pills nav-stacked" id="address">
              {% set address_row = 1 %}
              {% for address in addresses %}
                <li><a href="#tab-address{{ address_row }}" data-toggle="tab"><i class="fa fa-minus-circle" onclick="$('#address a:first').tab('show'); $('#address a[href=\'#tab-address{{ address_row }}\']').parent().remove(); $('#tab-address{{ address_row }}').remove();"></i> {{ tab_address }} {{ address_row }}</a></li>
                {% set address_row = address_row + 1 %}
              {% endfor %}
              <li id="address-add"><a onclick="addAddress();"><i class="fa fa-plus-circle"></i> {{ button_address_add }}</a></li>
            </ul>
          </div>
          <div class="col-sm-10">
            <div class="tab-content">
              {% set address_row = 1 %}
                {% for address in addresses %}
                  <div class="tab-pane " id="tab-address{{ address_row }}">
                    <input type="hidden" name="address[{{ address_row }}][address_id]" value="{{ address.address_id }}" />
                    <div class="form-group required">
                      <label class="col-sm-3 control-label" for="input-firstname{{ address_row }}">{{ entry_firstname }}</label>
                      <div class="col-sm-9">
                        <input type="text" name="address[{{ address_row }}][firstname]" value="{{ address.firstname }}" placeholder="{{ entry_firstname }}" id="input-firstname{{ address_row }}" class="form-control" />
                        {% if error_address[address_row].firstname %}
                          <div class="text-danger">{{ error_address[address_row].firstname }}</div>
                        {% endif %}
					  </div>
                    </div>
                    <div class="form-group required">
                      <label class="col-sm-3 control-label" for="input-lastname{{ address_row }}">{{ entry_lastname }}</label>
                      <div class="col-sm-9">
                        <input type="text" name="address[{{ address_row }}][lastname]" value="{{ address.lastname }}" placeholder="{{ entry_lastname }}" id="input-lastname{{ address_row }}" class="form-control" />
                        {% if error_address[address_row].lastname %}
                          <div class="text-danger">{{ error_address[address_row].lastname }}</div>
                        {% endif %}
					  </div>
                    </div>
                    <div class="form-group">
                      <label class="col-sm-3 control-label" for="input-company{{ address_row }}">{{ entry_company }}</label>
                      <div class="col-sm-9">
                        <input type="text" name="address[{{ address_row }}][company]" value="{{ address.company }}" placeholder="{{ entry_company }}" id="input-company{{ address_row }}" class="form-control" />
                      </div>
                    </div>
                    <div class="form-group required">
                      <label class="col-sm-3 control-label" for="input-address-1{{ address_row }}">{{ entry_address_1 }}</label>
                      <div class="col-sm-9">
                        <input type="text" name="address[{{ address_row }}][address_1]" value="{{ address.address_1 }}" placeholder="{{ entry_address_1 }}" id="input-address-1{{ address_row }}" class="form-control" />
                        {% if error_address[address_row].address_1 %}
                          <div class="text-danger">{{ error_address[address_row].address_1 }}</div>
                        {% endif %}
					  </div>
                    </div>
                    <div class="form-group">
                      <label class="col-sm-3 control-label" for="input-address-2{{ address_row }}">{{ entry_address_2 }}</label>
                      <div class="col-sm-9">
                        <input type="text" name="address[{{ address_row }}][address_2]" value="{{ address.address_2 }}" placeholder="{{ entry_address_2 }}" id="input-address-2{{ address_row }}" class="form-control" />
                      </div>
                    </div>
                    <div class="form-group required">
                      <label class="col-sm-3 control-label" for="input-city{{ address_row }}">{{ entry_city }}</label>
                      <div class="col-sm-9">
                        <input type="text" name="address[{{ address_row }}][city]" value="{{ address.city }}" placeholder="{{ entry_city }}" id="input-city{{ address_row }}" class="form-control" />
                        {% if error_address[address_row].city %}
                          <div class="text-danger">{{ error_address[address_row].city }}</div>
                        {% endif %}
					  </div>
                    </div>
                    <div class="form-group required">
                      <label class="col-sm-3 control-label" for="input-postcode{{ address_row }}">{{ entry_postcode }}</label>
                      <div class="col-sm-9">
                        <input type="text" name="address[{{ address_row }}][postcode]" value="{{ address.postcode }}" placeholder="{{ entry_postcode }}" id="input-postcode{{ address_row }}" class="form-control" />
                        {% if error_address[address_row].postcode %}
                          <div class="text-danger">{{ error_address[address_row].postcode }}</div>
                        {% endif %}
					  </div>
                    </div>
                    <div class="form-group required">
                      <label class="col-sm-3 control-label" for="input-country{{ address_row }}">{{ entry_country }}</label>
                      <div class="col-sm-9">
                        <select name="address[{{ address_row }}][country_id]" id="input-country{{ address_row }}" onchange="country(this, '{{ address_row }}', '{{ address.zone_id }}');" class="form-control">
                          <option value="">{{ text_select }}</option>
                          {% for country in countries %}
                            {% if country.country_id == address.country_id %}
                              <option value="{{ country.country_id }}" selected="selected">{{ country.name }}</option>
                              {% else %}
                              <option value="{{ country.country_id }}">{{ country.name }}</option>
                            {% endif %}
                          {% endfor %}
                        </select>
                        {% if error_address[address_row].country %}
                          <div class="text-danger">{{ error_address[address_row].country }}</div>
                        {% endif %}
					  </div>
                    </div>
                    <div class="form-group required">
                      <label class="col-sm-3 control-label" for="input-zone{{ address_row }}">{{ entry_zone }}</label>
                      <div class="col-sm-9">
                        <select name="address[{{ address_row }}][zone_id]" id="input-zone{{ address_row }}" class="form-control">
                        </select>
                        {% if error_address[address_row].zone %}
                          <div class="text-danger">{{ error_address[address_row].zone }}</div>
                        {% endif %}
					  </div>
                    </div>
                    {% for custom_field in custom_fields %}
                      {% if custom_field.location == 'address' %}
                        {% if custom_field.type == 'select' %}
						  <div class="form-group custom-field custom-field{{ custom_field.custom_field_id }}" data-sort="{{ custom_field.sort_order + 1 }}">
							<label class="col-sm-3 control-label" for="input-address{{ address_row }}-custom-field{{ custom_field.custom_field_id }}">{{ custom_field.name }}</label>
							<div class="col-sm-9">
							  <select name="address[{{ address_row }}][custom_field][{{ custom_field.custom_field_id }}]" id="input-address{{ address_row }}-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 address.custom_field[custom_field.custom_field_id] and custom_field_value.custom_field_value_id == address.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>
							  {% if error_address[address_row]['custom_field'][custom_field.custom_field_id] %}
							  <div class="text-danger">{{ error_address[address_row].custom_field[custom_field.custom_field_id] }}</div>
							  {% endif %}</div>
						  </div>
                        {% endif %}
                        {% if custom_field.type == 'radio' %}
						  <div class="form-group custom-field custom-field{{ custom_field.custom_field_id }}" data-sort="{{ custom_field.sort_order + 1 }}">
							<label class="col-sm-3 control-label">{{ custom_field.name }}</label>
							<div class="col-sm-9">
							  <div> {% for custom_field_value in custom_field.custom_field_value %}
								<div class="radio"> {% if address.custom_field[custom_field.custom_field_id] and custom_field_value.custom_field_value_id == address.custom_field[custom_field.custom_field_id] %}
								  <label>
									<input type="radio" name="address[{{ address_row }}][custom_field][{{ 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="address[{{ address_row }}][custom_field][{{ custom_field.custom_field_id }}]" value="{{ custom_field_value.custom_field_value_id }}" />
									{{ custom_field_value.name }}</label>
								  {% endif %}</div>
								{% endfor %} </div>
							  {% if error_address[address_row].custom_field[custom_field.custom_field_id] %}
							  <div class="text-danger">{{ error_address[address_row].custom_field[custom_field.custom_field_id] }}</div>
							  {% endif %}</div>
						  </div>
                        {% endif %}
                        {% if custom_field.type == 'checkbox' %}
						  <div class="form-group custom-field custom-field{{ custom_field.custom_field_id }}" data-sort="{{ custom_field.sort_order + 1 }}">
							<label class="col-sm-3 control-label">{{ custom_field.name }}</label>
							<div class="col-sm-9">
							  <div> {% for custom_field_value in custom_field.custom_field_value %}
								<div class="checkbox"> {% if address.custom_field[custom_field.custom_field_id] and custom_field_value.custom_field_value_id in address.custom_field[custom_field.custom_field_id] %}
								  <label>
									<input type="checkbox" name="address[{{ address_row }}][custom_field][{{ 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="address[{{ address_row }}][custom_field][{{ custom_field.custom_field_id }}][]" value="{{ custom_field_value.custom_field_value_id }}" />
									{{ custom_field_value.name }}</label>
								  {% endif %}</div>
								{% endfor %} </div>
							  {% if error_address[address_row]['custom_field'][custom_field.custom_field_id] %}
							  <div class="text-danger">{{ error_address[address_row].custom_field[custom_field.custom_field_id] }}</div>
							  {% endif %}</div>
						  </div>
                        {% endif %}
                        {% if custom_field.type == 'text' %}
						  <div class="form-group custom-field custom-field{{ custom_field.custom_field_id }}" data-sort="{{ custom_field.sort_order + 1 }}">
							<label class="col-sm-3 control-label" for="input-address{{ address_row }}-custom-field{{ custom_field.custom_field_id }}">{{ custom_field.name }}</label>
							<div class="col-sm-9">
							  <input type="text" name="address[{{ address_row }}][custom_field][{{ custom_field.custom_field_id }}]" value="{{ address.custom_field[custom_field.custom_field_id] ? address.custom_field[custom_field.custom_field_id] : custom_field.value }}" placeholder="{{ custom_field.name }}" id="input-address{{ address_row }}-custom-field{{ custom_field.custom_field_id }}" class="form-control" />
							  {% if error_address[address_row]['custom_field'][custom_field.custom_field_id] %}
							  <div class="text-danger">{{ error_address[address_row].custom_field[custom_field.custom_field_id] }}</div>
							  {% endif %}</div>
						  </div>
                        {% endif %}
                        {% if custom_field.type == 'textarea' %}
						  <div class="form-group custom-field custom-field{{ custom_field.custom_field_id }}" data-sort="{{ custom_field.sort_order + 1 }}">
							<label class="col-sm-3 control-label" for="input-address{{ address_row }}-custom-field{{ custom_field.custom_field_id }}">{{ custom_field.name }}</label>
							<div class="col-sm-9">
							  <textarea name="address[{{ address_row }}][custom_field][{{ custom_field.custom_field_id }}]" rows="5" placeholder="{{ custom_field.name }}" id="input-address{{ address_row }}-custom-field{{ custom_field.custom_field_id }}" class="form-control">{{ address.custom_field[custom_field.custom_field_id] ? address.custom_field[custom_field.custom_field_id] : custom_field.value }}</textarea>
							  {% if error_address[address_row]['custom_field'][custom_field.custom_field_id] %}
							  <div class="text-danger">{{ error_address[address_row].custom_field[custom_field.custom_field_id] }}</div>
							  {% endif %}</div>
						  </div>
                        {% endif %}
                        {% if custom_field.type == 'file' %}
						  <div class="form-group custom-field custom-field{{ custom_field.custom_field_id }}" data-sort="{{ custom_field.sort_order + 1 }}">
							<label class="col-sm-3 control-label">{{ custom_field.name }}</label>
							<div class="col-sm-9">
							  <button type="button" id="button-address{{ address_row }}-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="address[{{ address_row }}][custom_field][{{ custom_field.custom_field_id }}]" value="{{ address.custom_field[custom_field.custom_field_id] ? address.custom_field[custom_field.custom_field_id] }}" />
							  {% if error_address[address_row]['custom_field'][custom_field.custom_field_id] %}
							  <div class="text-danger">{{ error_address[address_row].custom_field[custom_field.custom_field_id] }}</div>
							  {% endif %}</div>
						  </div>
                        {% endif %}
                        {% if custom_field.type == 'date' %}
						  <div class="form-group custom-field custom-field{{ custom_field.custom_field_id }}" data-sort="{{ custom_field.sort_order + 1 }}">
							<label class="col-sm-3 control-label" for="input-address{{ address_row }}-custom-field{{ custom_field.custom_field_id }}">{{ custom_field.name }}</label>
							<div class="col-sm-9">
							  <div class="input-group date">
								<input type="text" name="address[{{ address_row }}][custom_field][{{ custom_field.custom_field_id }}]" value="{{ address.custom_field[custom_field.custom_field_id] ? address.custom_field[custom_field.custom_field_id] : custom_field.value }}" placeholder="{{ custom_field.name }}" data-date-format="YYYY-MM-DD" id="input-address{{ address_row }}-custom-field{{ custom_field.custom_field_id }}" class="form-control" />
								<span class="input-group-btn">
								<button type="button" class="btn btn-default"><i class="fa fa-calendar"></i></button>
								</span></div>
							  {% if error_address[address_row]['custom_field'][custom_field.custom_field_id] %}
							  <div class="text-danger">{{ error_address[address_row].custom_field[custom_field.custom_field_id] }}</div>
							  {% endif %}</div>
						  </div>
                        {% endif %}
                        {% if custom_field.type == 'time' %}
						  <div class="form-group custom-field custom-field{{ custom_field.custom_field_id }}" data-sort="{{ custom_field.sort_order + 1 }}">
							<label class="col-sm-3 control-label" for="input-address{{ address_row }}-custom-field{{ custom_field.custom_field_id }}">{{ custom_field.name }}</label>
							<div class="col-sm-9">
							  <div class="input-group time">
								<input type="text" name="address[{{ address_row }}][custom_field][{{ custom_field.custom_field_id }}]" value="{{ address.custom_field[custom_field.custom_field_id] ? address.custom_field[custom_field.custom_field_id] : custom_field.value }}" placeholder="{{ custom_field.name }}" data-date-format="HH:mm" id="input-address{{ address_row }}-custom-field{{ custom_field.custom_field_id }}" class="form-control" />
								<span class="input-group-btn">
								<button type="button" class="btn btn-default"><i class="fa fa-calendar"></i></button>
								</span></div>
							  {% if error_address[address_row]['custom_field'][custom_field.custom_field_id] %}
							  <div class="text-danger">{{ error_address[address_row].custom_field[custom_field.custom_field_id] }}</div>
							  {% endif %}</div>
						  </div>
                        {% endif %}
                        {% if custom_field.type == 'datetime' %}
						  <div class="form-group custom-field custom-field{{ custom_field.custom_field_id }}" data-sort="{{ custom_field.sort_order + 1 }}">
							<label class="col-sm-3 control-label" for="input-address{{ address_row }}-custom-field{{ custom_field.custom_field_id }}">{{ custom_field.name }}</label>
							<div class="col-sm-9">
							  <div class="input-group datetime">
								<input type="text" name="address[{{ address_row }}][custom_field][{{ custom_field.custom_field_id }}]" value="{{ address.custom_field[custom_field.custom_field_id] ? address.custom_field[custom_field.custom_field_id] : custom_field.value }}" placeholder="{{ custom_field.name }}" data-date-format="YYYY-MM-DD HH:mm" id="input-address{{ address_row }}-custom-field{{ custom_field.custom_field_id }}" class="form-control" />
								<span class="input-group-btn">
								<button type="button" class="btn btn-default"><i class="fa fa-calendar"></i></button>
								</span> </div>
							  {% if error_address[address_row]['custom_field'][custom_field.custom_field_id] %}
							  <div class="text-danger">{{ error_address[address_row].custom_field[custom_field.custom_field_id] }}</div>
							  {% endif %}</div>
						  </div>
                        {% endif %}
                      {% endif %}
                    {% endfor %}
                    <div class="form-group">
                      <label class="col-sm-3 control-label">{{ entry_default }}</label>
                      <div class="col-sm-9">
                        <div class="radio">
						  <label>
						    {% if address.address_id == address_id or not addresses %}
                              <input type="radio" name="address[{{ address_row }}][default]" value="{{ address_row }}" checked="checked" />
                              {% else %}
                              <input type="radio" name="address[{{ address_row }}][default]" value="{{ address_row }}" />
                            {% endif %}
						  </label>
						</div>
                      </div>
                    </div>
                  </div>
                {% set address_row = address_row + 1 %}
              {% endfor %}
            </div>
          </div>
        </div>
	</div>
  </form>
</div>
<div class="modal-footer">
  <div class="text-center"><a data-form="form-customer-address" class="button-save btn btn-primary"><i class="fa fa-save"></i> {{ button_save }}</a></div>
</div>
<script type="text/javascript"><!--
$(".button-save").on('click', function() {		
	$.ajax({
		url: $('#' + $(this).data('form')).attr('action'),
		type: 'post',
		dataType: 'json',
		data: $('#' + $(this).data('form') + ' input[type=\'text\'], #' + $(this).data('form') + ' input[type=\'hidden\'], #' + $(this).data('form') + ' input[type=\'radio\']:checked, #' + $(this).data('form') + ' input[type=\'checkbox\']:checked, #' + $(this).data('form') + ' select, #' + $(this).data('form') + ' textarea'),
		success: function(json) {
			if (json['error']) {
				$('#form-customer-address').before('<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> ' + json['error'] + '</div>');
				
				$('.alert-danger').delay(1500).fadeOut(500);	
			}
								
			if (json['success']) {				
				$('#form-customer-address').before('<div class="alert alert-success"><i class="fa fa-exclamation-circle"></i> ' + json['success'] + '</div>');
			
				setTimeout(function(){
					$('#modal-customer-edit').modal('hide');
				}, 1000)
			}
		}
	});
}); 
//--></script>
<script type="text/javascript"><!--
  $('select[name=\'customer_group_id\']').on('change', function() {
    $.ajax({
      url: 'index.php?route=customer/customer/customfield&user_token={{ user_token }}&customer_group_id=' + this.value,
      dataType: 'json',
      success: function(json) {
        $('.custom-field').hide();
        $('.custom-field').removeClass('required');

        for (i = 0; i < json.length; i++) {
          custom_field = json[i];

          $('.custom-field' + custom_field['custom_field_id']).show();

          if (custom_field['required']) {
            $('.custom-field' + custom_field['custom_field_id']).addClass('required');
          }
        }
      },
      error: function(xhr, ajaxOptions, thrownError) {
        alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
      }
    });
  });

  $('select[name=\'customer_group_id\']').trigger('change');
  //--></script> 
<script type="text/javascript"><!--
  var address_row = {{ address_row }};

  function addAddress() {
    html  = '<div class="tab-pane" id="tab-address' + address_row + '">';
    html += '  <input type="hidden" name="address[' + address_row + '][address_id]" value="" />';

    html += '  <div class="form-group required">';
    html += '    <label class="col-sm-3 control-label" for="input-firstname' + address_row + '">{{ entry_firstname }}</label>';
    html += '    <div class="col-sm-9"><input type="text" name="address[' + address_row + '][firstname]" value="" placeholder="{{ entry_firstname }}" id="input-firstname' + address_row + '" class="form-control" /></div>';
    html += '  </div>';

    html += '  <div class="form-group required">';
    html += '    <label class="col-sm-3 control-label" for="input-lastname' + address_row + '">{{ entry_lastname }}</label>';
    html += '    <div class="col-sm-9"><input type="text" name="address[' + address_row + '][lastname]" value="" placeholder="{{ entry_lastname }}" id="input-lastname' + address_row + '" class="form-control" /></div>';
    html += '  </div>';

    html += '  <div class="form-group">';
    html += '    <label class="col-sm-3 control-label" for="input-company' + address_row + '">{{ entry_company }}</label>';
    html += '    <div class="col-sm-9"><input type="text" name="address[' + address_row + '][company]" value="" placeholder="{{ entry_company }}" id="input-company' + address_row + '" class="form-control" /></div>';
    html += '  </div>';

    html += '  <div class="form-group required">';
    html += '    <label class="col-sm-3 control-label" for="input-address-1' + address_row + '">{{ entry_address_1 }}</label>';
    html += '    <div class="col-sm-9"><input type="text" name="address[' + address_row + '][address_1]" value="" placeholder="{{ entry_address_1 }}" id="input-address-1' + address_row + '" class="form-control" /></div>';
    html += '  </div>';

    html += '  <div class="form-group">';
    html += '    <label class="col-sm-3 control-label" for="input-address-2' + address_row + '">{{ entry_address_2 }}</label>';
    html += '    <div class="col-sm-9"><input type="text" name="address[' + address_row + '][address_2]" value="" placeholder="{{ entry_address_2 }}" id="input-address-2' + address_row + '" class="form-control" /></div>';
    html += '  </div>';

    html += '  <div class="form-group required">';
    html += '    <label class="col-sm-3 control-label" for="input-city' + address_row + '">{{ entry_city }}</label>';
    html += '    <div class="col-sm-9"><input type="text" name="address[' + address_row + '][city]" value="" placeholder="{{ entry_city }}" id="input-city' + address_row + '" class="form-control" /></div>';
    html += '  </div>';

    html += '  <div class="form-group required">';
    html += '    <label class="col-sm-3 control-label" for="input-postcode' + address_row + '">{{ entry_postcode }}</label>';
    html += '    <div class="col-sm-9"><input type="text" name="address[' + address_row + '][postcode]" value="" placeholder="{{ entry_postcode }}" id="input-postcode' + address_row + '" class="form-control" /></div>';
    html += '  </div>';

    html += '  <div class="form-group required">';
    html += '    <label class="col-sm-3 control-label" for="input-country' + address_row + '">{{ entry_country }}</label>';
    html += '    <div class="col-sm-9"><select name="address[' + address_row + '][country_id]" id="input-country' + address_row + '" onchange="country(this, \'' + address_row + '\', \'0\');" class="form-control">';
      html += '         <option value="">{{ text_select }}</option>';
      {% for country in countries %}
      html += '         <option value="{{ country.country_id }}">{{ country.name|escape('js') }}</option>';
      {% endfor %}
      html += '      </select></div>';
    html += '  </div>';

    html += '  <div class="form-group required">';
    html += '    <label class="col-sm-3 control-label" for="input-zone' + address_row + '">{{ entry_zone }}</label>';
    html += '    <div class="col-sm-9"><select name="address[' + address_row + '][zone_id]" id="input-zone' + address_row + '" class="form-control"><option value="">{{ text_none }}</option></select></div>';
    html += '  </div>';

    // Custom Fields
    {% for custom_field in custom_fields %}
    {% if custom_field.location == 'address' %}
    {% if custom_field.type == 'select' %}

    html += '  <div class="form-group custom-field custom-field{{ custom_field.custom_field_id }}" data-sort="{{ custom_field.sort_order + 1 }}">';
    html += '  		<label class="col-sm-3 control-label" for="input-address' + address_row + '-custom-field{{ custom_field.custom_field_id }}">{{ custom_field.name|e('js') }}</label>';
    html += '  		<div class="col-sm-9">';
    html += '  		  <select name="address[' + address_row + '][custom_field][{{ custom_field.custom_field_id }}]" id="input-address' + address_row + '-custom-field{{ custom_field.custom_field_id }}" class="form-control">';
    html += '  			<option value="">{{ text_select }}</option>';

    {% for custom_field_value in custom_field.custom_field_value %}
    html += '  			<option value="{{ custom_field_value.custom_field_value_id }}">{{ custom_field.name|e('js') }}</option>';
    {% endfor %}

    html += '  		  </select>';
    html += '  		</div>';
    html += '  	  </div>';
    {% endif %}

    {% if custom_field.type == 'radio' %}
    html += '  	  <div class="form-group custom-field custom-field{{ custom_field.custom_field_id }}">';
    html += '  		<label class="col-sm-3 control-label">{{ custom_field.name|e('js') }}</label>';
    html += '  		<div class="col-sm-9">';
    html += '  		  <div>';

    {% for custom_field_value in custom_field.custom_field_value %}
    html += '  			<div class="radio"><label><input type="radio" name="address[' + address_row + '][custom_field][{{ custom_field.custom_field_id }}]" value="{{ custom_field_value.custom_field_value_id }}" /> {{ custom_field_value.name|e('js') }}</label></div>';
    {% endfor %}

    html += '		  </div>';
    html += '		</div>';
    html += '	  </div>';
    {% endif %}

    {% if custom_field.type == 'checkbox' %}
    html += '	  <div class="form-group custom-field custom-field{{ custom_field.custom_field_id }}" data-sort="{{ custom_field.sort_order + 1 }}">';
    html += '		<label class="col-sm-3 control-label">{{ custom_field.name|e('js') }}</label>';
    html += '		<div class="col-sm-9">';
    html += '		  <div>';

    {% for custom_field_value in custom_field.custom_field_value %}
    html += '			<div class="checkbox"><label><input type="checkbox" name="address[{{ address_row }}][custom_field][{{ custom_field.custom_field_id }}][]" value="{{ custom_field_value.custom_field_value_id }}" /> {{ custom_field_value.name|e('js') }}</label></div>';
    {% endfor %}

    html += '		  </div>';
    html += '		</div>';
    html += '	  </div>';
    {% endif %}

    {% if custom_field.type == 'text' %}
    html += '	  <div class="form-group custom-field custom-field{{ custom_field.custom_field_id }}" data-sort="{{ custom_field.sort_order + 1 }}">';
    html += '		<label class="col-sm-3 control-label" for="input-address' + address_row + '-custom-field{{ custom_field.custom_field_id }}">{{ custom_field_value.name|e('js') }}</label>';
    html += '		<div class="col-sm-9">';
    html += '		  <input type="text" name="address[' + address_row + '][custom_field][{{ custom_field.custom_field_id }}]" value="{{ custom_field.value|e('js') }}" placeholder="{{ custom_field_value.name|e('js') }}" id="input-address' + address_row + '-custom-field{{ custom_field.custom_field_id }}" class="form-control" />';
    html += '		</div>';
    html += '	  </div>';
    {% endif %}

    {% if custom_field.type == 'textarea' %}
    html += '	  <div class="form-group custom-field custom-field{{ custom_field.custom_field_id }}" data-sort="{{ custom_field.sort_order + 1 }}">';
    html += '		<label class="col-sm-3 control-label" for="input-address' + address_row + '-custom-field{{ custom_field.custom_field_id }}">{{ custom_field_value.name|e('js') }}</label>';
    html += '		<div class="col-sm-9">';
    html += '		  <textarea name="address[' + address_row + '][custom_field][{{ custom_field.custom_field_id }}]" rows="5" placeholder="{{ custom_field_value.name|e('js') }}" id="input-address' + address_row + '-custom-field{{ custom_field.custom_field_id }}" class="form-control">{{ custom_field.value|e('js') }}</textarea>';
    html += '		</div>';
    html += '	  </div>';
    {% endif %}

    {% if custom_field.type == 'file' %}
    html += '	  <div class="form-group custom-field custom-field{{ custom_field.custom_field_id }}" data-sort="{{ custom_field.sort_order + 1 }}">';
    html += '		<label class="col-sm-3 control-label">{{ custom_field.name|e('js') }}</label>';
    html += '		<div class="col-sm-9">';
    html += '		  <button type="button" id="button-address' + address_row + '-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>';
    html += '		  <input type="hidden" name="address[' + address_row + '][{{ custom_field.custom_field_id }}]" value="" id="input-address' + address_row + '-custom-field{{ custom_field.custom_field_id }}" />';
    html += '		</div>';
    html += '	  </div>';
    {% endif %}

    {% if custom_field.type == 'date' %}
    html += '	  <div class="form-group custom-field custom-field{{ custom_field.custom_field_id }}" data-sort="{{ custom_field.sort_order + 1 }}">';
    html += '		<label class="col-sm-3 control-label" for="input-address' + address_row + '-custom-field{{ custom_field.custom_field_id }}">{{ custom_field_value.name|e('js') }}</label>';
    html += '		<div class="col-sm-9">';
    html += '		  <div class="input-group date"><input type="text" name="address[' + address_row + '][custom_field][{{ custom_field.custom_field_id }}]" value="{{ custom_field.value|e('js') }}" placeholder="{{ custom_field.name|e('js') }} data-date-format="YYYY-MM-DD" id="input-address' + address_row + '-custom-field{{ custom_field.custom_field_id }}" class="form-control" /><span class="input-group-btn"><button type="button" class="btn btn-default"><i class="fa fa-calendar"></i></button></span></div>';
    html += '		</div>';
    html += '	  </div>';
    {% endif %}

    {% if custom_field.type == 'time' %}
    html += '	  <div class="form-group custom-field custom-field{{ custom_field.custom_field_id }}" data-sort="{{ custom_field.sort_order + 1 }}">';
    html += '		<label class="col-sm-3 control-label" for="input-address' + address_row + '-custom-field{{ custom_field.custom_field_id }}">{{ custom_field_value.name|e('js') }}</label>';
    html += '		<div class="col-sm-9">';
    html += '		  <div class="input-group time"><input type="text" name="address[' + address_row + '][custom_field][{{ custom_field.custom_field_id }}]" value="{{ custom_field.value|e('js') }}" placeholder="{{ custom_field.name|e('js') }}" data-date-format="HH:mm" id="input-address' + address_row + '-custom-field{{ custom_field.custom_field_id }}" class="form-control" /><span class="input-group-btn"><button type="button" class="btn btn-default"><i class="fa fa-calendar"></i></button></span></div>';
    html += '		</div>';
    html += '	  </div>';
    {% endif %}

    {% if custom_field.type == 'datetime' %}
    html += '	  <div class="form-group custom-field custom-field{{ custom_field.custom_field_id }}" data-sort="{{ custom_field.sort_order + 1 }}">';
    html += '		<label class="col-sm-3 control-label" for="input-address' + address_row + '-custom-field{{ custom_field.custom_field_id }}">{{ custom_field_value.name|e('js') }}</label>';
    html += '		<div class="col-sm-9">';
    html += '		  <div class="input-group datetime"><input type="text" name="address[' + address_row + '][custom_field][{{ custom_field.custom_field_id }}]" value="{{ custom_field.value|e('js') }}" placeholder="{{ custom_field.name|e('js') }}" data-date-format="YYYY-MM-DD HH:mm" id="input-address' + address_row + '-custom-field{{ custom_field.custom_field_id }}" class="form-control" /><span class="input-group-btn"><button type="button" class="btn btn-default"><i class="fa fa-calendar"></i></button></span></div>';
    html += '		</div>';
    html += '	  </div>';
    {% endif %}

    {% endif %}
    {% endfor %}

    html += '  <div class="form-group">';
    html += '    <label class="col-sm-3 control-label">{{ entry_default }}</label>';
    html += '    <div class="col-sm-9"><div class="radio"><label><input type="radio" name="address[' + address_row + '][default]" value="1" /></label></div></div>';
    html += '  </div>';

    html += '</div>';

    $('#tab-general .tab-content').append(html);

    $('select[name=\'customer_group_id\']').trigger('change');

    $('select[name=\'address[' + address_row + '][country_id]\']').trigger('change');

    $('#address-add').before('<li><a href="#tab-address' + address_row + '" data-toggle="tab"><i class="fa fa-minus-circle" onclick="$(\'#address a:first\').tab(\'show\'); $(\'a[href=\\\'#tab-address' + address_row + '\\\']\').parent().remove(); $(\'#tab-address' + address_row + '\').remove();"></i> {{ tab_address }} ' + address_row + '</a></li>');

    $('#address a[href=\'#tab-address' + address_row + '\']').tab('show');

    $('.date').datetimepicker({
		language: '{{ datepicker }}',
		pickTime: false
    });

	$('.datetime').datetimepicker({
		language: '{{ datepicker }}',
		pickDate: true,
		pickTime: true
    });

    $('.time').datetimepicker({
		language: '{{ datepicker }}',
		pickDate: false
    });

    $('#tab-address' + address_row + ' .form-group[data-sort]').detach().each(function() {
		if ($(this).attr('data-sort') >= 0 && $(this).attr('data-sort') <= $('#tab-address' + address_row + ' .form-group').length) {
			$('#tab-address' + address_row + ' .form-group').eq($(this).attr('data-sort')).before(this);
		}
		
		if ($(this).attr('data-sort') > $('#tab-address' + address_row + ' .form-group').length) {
			$('#tab-address' + address_row + ' .form-group:last').after(this);
		}
		
		if ($(this).attr('data-sort') < -$('#tab-address' + address_row + ' .form-group').length) {
			$('#tab-address' + address_row + ' .form-group:first').before(this);
		}
    });

    address_row++;
  }
  //--></script> 
  <script type="text/javascript"><!--
  function country(element, index, zone_id) {
    $.ajax({
      url: 'index.php?route=localisation/country/country&user_token={{ user_token }}&country_id=' + element.value,
      dataType: 'json',
      beforeSend: function() {
        $('select[name=\'address[' + index + '][country_id]\']').prop('disabled', true);
      },
      complete: function() {
        $('select[name=\'address[' + index + '][country_id]\']').prop('disabled', false);
      },
      success: function(json) {
        if (json['postcode_required'] == '1') {
          $('input[name=\'address[' + index + '][postcode]\']').parent().parent().addClass('required');
        } else {
          $('input[name=\'address[' + index + '][postcode]\']').parent().parent().removeClass('required');
        }

        html = '<option value="">{{ text_select }}</option>';

        if (json['zone'] && 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">{{ text_none }}</option>';
        }

        $('select[name=\'address[' + index + '][zone_id]\']').html(html);
      },
      error: function(xhr, ajaxOptions, thrownError) {
        alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
      }
    });
  }

  $('select[name$=\'[country_id]\']').trigger('change');
  //--></script> 
  <script type="text/javascript"><!--
$('#address a:first').tab('show');
//--></script>
  <script type="text/javascript"><!--
  $('#content').delegate('button[id^=\'button-custom-field\'], button[id^=\'button-address\']', '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');

    if (typeof timer != 'undefined') {
        clearInterval(timer);
    }

    timer = setInterval(function() {
      if ($('#form-upload input[name=\'file\']').val() != '') {
        clearInterval(timer);

        $.ajax({
          url: 'index.php?route=tool/upload/upload&user_token={{ user_token }}',
          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) {
            $(node).parent().find('.text-danger').remove();

            if (json['error']) {
              $(node).parent().find('input[type=\'hidden\']').after('<div class="text-danger">' + json['error'] + '</div>');
            }

            if (json['success']) {
              alert(json['success']);
            }

            if (json['code']) {
              $(node).parent().find('input[type=\'hidden\']').val(json['code']);
            }
          },
          error: function(xhr, ajaxOptions, thrownError) {
            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
          }
        });
      }
    }, 500);
  });

  $('.date').datetimepicker({
    language: '{{ datepicker }}',
    pickTime: false
  });

  $('.datetime').datetimepicker({
    language: '{{ datepicker }}',
    pickDate: true,
    pickTime: true
  });

  $('.time').datetimepicker({
    language: '{{ datepicker }}',
    pickDate: false
  });

  // Sort the custom fields
  {% set address_row = 1 %}
  {% for address in addresses %}
  $('#tab-address{{ address_row }} .form-group[data-sort]').detach().each(function() {
    if ($(this).attr('data-sort') >= 0 && $(this).attr('data-sort') <= $('#tab-address{{ address_row }} .form-group').length) {
      $('#tab-address{{ address_row }} .form-group').eq($(this).attr('data-sort')).before(this);
    }

    if ($(this).attr('data-sort') > $('#tab-address{{ address_row }} .form-group').length) {
      $('#tab-address{{ address_row }} .form-group:last').after(this);
    }

    if ($(this).attr('data-sort') < -$('#tab-address{{ address_row }} .form-group').length) {
      $('#tab-address{{ address_row }} .form-group:first').before(this);
    }
  });
  {% set address_row = address_row + 1 %}
  {% endfor %}
//--></script>