var Wick = Wick || {};
Wick.DeliveryDetails = (function () {
  var $formRow = $('.js-row-postcode');

  function bindEvents () {
    $('.btn-checkout-find-address').on('click', function () {
      event.preventDefault();
      var $form = $(this).parents('form'),
        $row = $form.find('.js-row-postcode');

      if ($row.find('#postcode').val().length) {
        $row.removeClass('form-row_validation-error').find('.form-row__error').remove();

        $form.find('[data-finder]').toggle();
        $form.find('[data-auto-find]').toggle();

      } else {
        $row.addClass('form-row_validation-error');
        if (!$row.find('.form-row__error')[0]) {
          $row.append('<div class="form-row__error">Error message here</div>');
        }
      }
    });

    $('.checkout-address-select').on('change', function () {
      var $form = $(this).parents('form');

      $form.find('[data-finded-address]').toggle();
      $form.find('[data-auto-find]').toggle();
    });

    $('.finded-address-edit').on('click', function () {
      var $form = $(this).parents('form');

      $form.find('[data-finder]').toggle();
      $form.find('[data-finded-address]').toggle();
    });
  }

  if (!$formRow.length){
    return;
  }

  bindEvents();
})();
