$(document).ready(function () {

  $(document).on('init_common', function () {
    // copy file content to textarea with drag and drop
    $("textarea.drag-and-drop:not(.drag-and-drop-init)").each(function (i, el) {
      $(this).addClass("drag-and-drop-init");
      $(this).on("drop", function (e) {
        e.preventDefault();
        var file = e.originalEvent.dataTransfer.files[0];
        var reader = new FileReader();
        var result;
        reader.readAsText(file);
        reader.onload = function () {
          result = reader.result;
          e.target.value = result;
        }
      });
    })
  });

  $(document).trigger('init_common');

  let elementsArray = document.querySelectorAll(".flag_phone_prefix");

  elementsArray.forEach(function (elem) {
    // type -> id of element in which we want phone prefixes with flags
    var id = elem.id;
    var type = (id.split('_'))[0];

    if (type) {
      window[type + 'input'] = document.querySelector("#" + type + "_tel_line");

      if (window[type + 'input'].value != null) {
        window[type + '_prefix'] = document.querySelector("#" + type + "_tel_prefix");
        window[type + 'input'].value = window[type + '_prefix'].value.replace('.', '') + window[type + 'input'].value;
      }

      // initialise plugin
      window[type + 'iti'] = window.intlTelInput(window[type + 'input'], {
        initialCountry: "auto",
        nationalMode: true,
        geoIpLookup: callback => {
          fetch("https://ipapi.co/json")
            .then(res => res.json())
            .then(data => callback(data.country_code))
            .catch(() => callback("us"));
        },
        showSelectedDialCode: true,
        hiddenInput: () => ({ phone: type + "_full_phone", country: type + "_full_phone_country" }),
        utilsScript: window.location.origin + "/vendor/intl-tel-input/build/js/utils.js"
      });

      const reset = () => {
        window[type + 'input'].classList.remove("error");
      };

      window[type + 'input'].addEventListener('change', reset);
      window[type + 'input'].addEventListener('keyup', reset);

      $("#" + type + "_tel_line").on('keyup blur', function (event) {

        var inputType = document.querySelector("#" + type + "_tel_line");
        var inputParent = inputType.parentElement;

        var prefix = inputParent.querySelector(".iti__selected-dial-code");
        if (prefix.innerHTML == "") {
          return;
        }

        var phoneNumber = window[type + 'input'].value;
        var isPrefix = phoneNumber.includes(prefix.innerHTML);
        var numberWithoutPrefix = phoneNumber.split(prefix.innerHTML);

        if (numberWithoutPrefix[1] == " " || event.type == "blur") {
          if (isPrefix) {
            window[type + 'input'].value = numberWithoutPrefix[1];

          }
        }
      });
    }
  });
});

 function scrollToElement(element)
 {
	if (typeof element === 'string') {
		element = document.querySelector(element);
	}
	if (!element) {
		return;
	}

	element.scrollIntoView({behavior: "smooth"});
}

/** /customer/login/customer */
$(document).ready(function () {

  $("#password1, #new_password1").on(
    "keyup",
    function (e) {
      var password = $(this).val();

      // minimální počet znaků
      var length = $(this).attr("min_length");
      if(password.length >= length) {
        passwordCheckList("#password_length", "add");
      } else {
        passwordCheckList("#password_length", "remove");
      }

      // aspoň 1 velké písmeno
      if(password.match(/[A-Z]/)) {
        passwordCheckList("#password_big_letter", "add");
      } else {
        passwordCheckList("#password_big_letter", "remove");
      }

      // aspoň 1 malé písmeno
      if(password.match(/[a-z]/)) {
        passwordCheckList("#password_small_letter", "add");
      } else {
        passwordCheckList("#password_small_letter", "remove");
      }

      // aspoň 1 číslice
      if(password.match(/[0-9]/)) {
        passwordCheckList("#password_digit", "add");
      } else {
        passwordCheckList("#password_digit", "remove");
      }

    }
  );

  function passwordCheckList(item, changeType) {
    var itemIcon = $(item).find("i");

    if (changeType == "add") {
      $(item).removeClass("text-secondary");
      $(item).addClass("text-success");
      itemIcon.removeClass("fas fa-times");
      itemIcon.addClass("fa fa-check");
    } else {
      if($(item).hasClass("text-success")) {
        $(item).removeClass("text-success");
        itemIcon.removeClass("fa fa-check");
        $(item).addClass("text-secondary");
        itemIcon.addClass("fas fa-times");
      }
    }
  }
});
