var Wick = Wick || {};

Wick.PaymentDetails = (function () {
  var $btnEnterDetails = $('.btn-enter-details'),
    $form = $btnEnterDetails.parents('form'),
    isGuestPage = $('.page_checkout-payment-details_guest').length;

  function bindEvents () {
    $btnEnterDetails.on('click', function(){
      if ($form.find('#card-name').val().length &&
        $form.find('#first-line-address').val().length &&
        $form.find('#address-line-1').val().length &&
        $form.find('#town-city').val().length &&
        $form.find('#postcode_additional').val().length){
        $('.checkout-payment-details__card-details').removeClass('checkout-payment-details__card-details_hidden');
        $('html, body').animate({
          scrollTop: $('.checkout-payment-details__card-details').offset().top - 15
        }, 700);
        return false;
      }
      Wick.Forms.showValidationErrors($form[0]);
      return false;
    });

    $('.checkout-payment-details__card-details').on('submit', function(){
      var $form = $(this);
      if ($form.find('#card-type').val() &&
        $form.find('#card-name1').val().length &&
        $form.find('#card-number').val().length &&
        $form.find('#month').val() &&
        $form.find('#year').val() &&
        $form.find('#security-code').val()){
        window.location.href = isGuestPage
          ? './page_checkout_confirmation_guest.html'
          : './page_checkout_confirmation.html';
        return false;
      }
      Wick.Forms.showValidationErrors(this);
      return false;
    });
  }

  bindEvents();
})();
