$('#checkout-confirm').on('submit', function(e) {
  e.preventDefault();

  if (!$('#wirecard-bank_transfer').prop('checked')) {
    return;
  }

  $('span.form-error').remove();

  var $form = $('#form-wirecard'),
    $formerror = $form.find('span.form-error'),
    $this = $(this).find('.ftr-steps__button'),
    data = $form.serializeObject();

  if ($formerror.length > 0) {
    $(window).scrollTop(
      $($formerror[0])
        .parent('label')
        .offset().top
    );
    return false;
  }

  data['cidade'] = $form.find('#codigo_municipio').val();

  $.ajax({
    type: 'POST',
    url: 'index.php?route=payment/wirecard/pay_bank_transfer',
    data: data,
    dataType: 'json',
    beforeSend: function() {
      $this.find('.ftr-steps__button').prop('disabled', true);
      $('.ftr-loading-bar').removeClass('ftr-none');
    },
    complete: function() {
      $('.ftr-loading-bar').addClass('ftr-none');
      $this.find('.ftr-steps__button').prop('disabled', false);
    },
    success: function(json) {
      if (json['error']) {
        $('.ftr-field').removeClass('error');
        $('.ftr-field__error').remove();

        $.each(json['error'], function(index, value) {
          $('input[name=' + index + ']')
            .closest('.ftr-field')
            .addClass('error');
          $('input[name=' + index + ']  + .ftr-field__text').after(
            '<span class="ftr-field__error">' + value + '</span>'
          );
          notificationMsg(value, 'error');
          if (index == 't_e_limiter_ip' || index == 't_e_limiter'){
            setTimeout(function (e){
              location.href = ('index.php?route=account/logout');
            },5000)
          }
        });

        return;
      }

      if (json['redirect']) {
        location.href = json['redirect'];
      }
    },
    error: function(request, status, error) {
      console.log(request.responseText);
    },
  });
});
