// global variables
var glob_auth_way;
var glob_user_input;
var glob_input_type;
var reset_pass_caller;
var mobile_reset_hash;
var user_first_name;
var user_last_name;
var resettab_code;

jQuery(document).ready(function ($) {

  $('.kepro-corner-close').click(function(){
    $('.kepro-corner-box').remove();
  });

  // auto submit verify code fields
  $(
    "#verifycode, #verifycodereset, #verifycodejl, #verifycodejr, #verifycodereset, #verifycode-loginotptab, #inlineverify"
  ).on("keyup", function (e) {
    var inputText = $(this).val();
    if (
      jQuery.isNumeric(inputText) &&
      inputText.length == mreeir_js_options.verifycodelength
    ) {
      var e = jQuery.Event("keypress");
      e.which = 13;
      e.keyCode = 13;
      $(this).trigger(e);
    }
  });

  // tabbed login register start
  $(".ktablinks").on("click", function (e) {
    e.preventDefault();
    if ($(this).hasClass("koutactive")) return;
    $(".ktablinks").removeClass("koutactive");
    $(this).addClass("koutactive");
    $(".ktabcontent").toggleClass("koutactive");
  });

  $(".ktabinnerlinks").on("click", function (e) {
    e.preventDefault();
    $(this).parents(".ktabinner").toggleClass("kinneractive");
    $(this)
      .parents(".ktabinner")
      .siblings(".ktabinner")
      .not("#kerasnologintabreset")
      .addClass("kinneractive");
  });

  $(".ktabinnerreset").on("click", function (e) {
    e.preventDefault();
    $(this).parents(".ktabinner").toggleClass("kinneractive");
    $(this)
      .parents(".ktabinner")
      .siblings(".ktabinner#kerasnologintabreset")
      .addClass("kinneractive");
  });

  $("#ktabinnerotp").on("click", function (e) {
    e.preventDefault();
    $(this).parents(".ktabinner").toggleClass("kinneractive");
    $(this)
      .parents(".ktabinner")
      .siblings(".ktabinner#kerasnologintabotp")
      .addClass("kinneractive");
  });

  $("#ktabinnerpass").on("click", function (e) {
    e.preventDefault();
    $(this).parents(".ktabinner").toggleClass("kinneractive");
    $(this)
      .parents(".ktabinner")
      .siblings(".ktabinner#kerasnologintabup")
      .addClass("kinneractive");
  });

  // tabbed login register end

  // downloads start

  // download form submit - step 1
  $(".dluser_mobile").keypress(function (e) {
    var key = e.which;
    if (key == 13) {
      // the enter key code
      $(this)
        .parent()
        .siblings("li.smtbtn")
        .find(".dluser_mobile_submit")
        .click();
      return false;
    }
  });

  $(".dluser_mobile_submit").on("click", function (e) {
    e.preventDefault();

    $this_submit = $(this);
    var $mobile = $(this)
      .parent(".smtbtn")
      .siblings("li")
      .find(".dluser_mobile")
      .val();
    var $userisnotauser = $(this)
      .parent(".smtbtn")
      .siblings(".dluser_is_not_user")
      .val();

    var $name = "";
    var $family = "";

    if (
      $this_submit.parent(".smtbtn").siblings("li").find(".dluser_first_name")
        .length > 0
    ) {
      $name_obj = $this_submit
        .parent(".smtbtn")
        .siblings("li")
        .find(".dluser_first_name");
      $name = $name_obj.val();
      if ($name_obj.hasClass("itisrequired")) {
        if (mreeir_is_empty($name)) {
          mreeir_notification(mreeir_messeges.downloadusername);
          return;
        }
      }
    }

    if (
      $this_submit.parent(".smtbtn").siblings("li").find(".dluser_last_name")
        .length > 0
    ) {
      $family_obj = $this_submit
        .parent(".smtbtn")
        .siblings("li")
        .find(".dluser_last_name");
      $family = $family_obj.val();
      if ($family_obj.hasClass("itisrequired")) {
        if (mreeir_is_empty($family)) {
          mreeir_notification(mreeir_messeges.downloaduserfamily);
          return;
        }
      }
    }

    if (mreeir_is_empty($mobile)) {
      mreeir_notification(mreeir_messeges.wrongMobile);
      return;
    }

    if (mreeir_is_empty_fake($userisnotauser)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    $mobile = mreeir_correct_mobile_number($mobile);

    if (!mreeir_is_mobile($mobile)) {
      mreeir_notification(mreeir_messeges.wrongMobile);
      return;
    }

    glob_user_input = $mobile;
    user_first_name = $name;
    user_last_name = $family;

    jQuery.ajax({
      url: mreeir_js_options.ajaxurl,
      type: "POST",
      data: {
        action: "mreeir_send_download_sms",
        mobile: $mobile,
        userisnotauser: $userisnotauser,
        security: mreeir_js_options.noncesenddownloadsms,
      },
      beforeSend: function () {
        $this_submit
          .parent()
          .parent()
          .siblings(".mreeir-loading")
          .removeClass("mreeir-hide");
      },
      success: function (response) {
        if ($.trim(response) == "success") {
          $this_submit.parent(".smtbtn").siblings(".kdl-step-1").hide();
          $this_submit.parent(".smtbtn").hide();
          $this_submit.hide();
          $this_submit.parent(".smtbtn").siblings(".kdl-step-2").show();
          $this_submit
            .parent(".smtbtn")
            .siblings(".kdl-step-2")
            .find(".kdl-step-2")
            .show();
          if (
            $this_submit
              .parent(".smtbtn")
              .siblings(".dlregister-wrapper")
              .find(".kdl-step-2").length > 0
          ) {
            $this_submit
              .parent(".smtbtn")
              .siblings(".dlregister-wrapper")
              .find(".kdl-step-2")
              .show();
          }
          $this_submit
            .parent(".smtbtn")
            .siblings("li")
            .find(".kdl-step-2.dlverify_code")
            .focus();
        }
        if ($.trim(response) == "wrong-info") {
          mreeir_notification(mreeir_messeges.wrongMobile);
        }
        if ($.trim(response) == "send-failed") {
          mreeir_notification(mreeir_messeges.sendFailed);
        }
        if ($.trim(response) == "sms-send-time-limit") {
          mreeir_notification(mreeir_messeges.SendTimeLimit);
        }
      },
      complete: function () {
        $this_submit
          .parent()
          .parent()
          .siblings(".mreeir-loading")
          .addClass("mreeir-hide");
      },
    });
  });

  // download form submit - step 2
  $(".dlverify_code").keypress(function (e) {
    var key = e.which;
    if (key == 13) {
      // the enter key code
      $(this)
        .parent()
        .siblings("li.smtbtn")
        .find(".dluser_code_submit")
        .click();
      return false;
    }
  });

  jQuery(".dluser_code_submit").on("click", function (e) {
    e.preventDefault();
    $this_submit = $(this);
    var $wrapper_id = $(this).closest(".download-main-wrapper").attr("id");
    $wrapper_id = $("#" + $wrapper_id);

    var verifycode = $this_submit
      .parent(".smtbtn")
      .siblings("li")
      .find(".dlverify_code")
      .val();
    verifycode = mreeir_correct_mobile_number(verifycode);
    var $userisnotauser = $this_submit
      .parent()
      .siblings(".dluser_is_not_user")
      .val();
    var dlid = $this_submit.parent().siblings(".dldlid").val();
    var registerme = "0";

    if (
      $this_submit.parent().siblings(".dlregister-wrapper").find(".dlregister")
        .length
    ) {
      var chk = $this_submit
        .parent()
        .siblings(".dlregister-wrapper")
        .find(".dlregister");
      if (chk.is(":checked")) {
        registerme = "1";
      }
    }

    if (mreeir_is_empty_fake($userisnotauser)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    if (!jQuery.isNumeric(dlid)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    if (mreeir_is_empty(glob_user_input)) return;

    if (user_first_name === "undefined" || user_first_name === null) return;
    if (user_last_name === "undefined" || user_last_name === null) return;

    if (
      jQuery.isNumeric(verifycode) &&
      verifycode.length == mreeir_js_options.verifycodelength
    ) {
      $this_submit = $(this);
      jQuery.ajax({
        url: mreeir_js_options.ajaxurl,
        type: "POST",
        data: {
          action: "mreeir_verify_code_download",
          mobile: glob_user_input,
          name: user_first_name,
          family: user_last_name,
          userisnotauser: $userisnotauser,
          code: verifycode,
          downloadid: dlid,
          register: registerme,
          security: mreeir_js_options.noncedownloadverifycode,
        },
        beforeSend: function () {
          $this_submit
            .parent()
            .parent()
            .siblings(".mreeir-loading")
            .removeClass("mreeir-hide");
        },
        success: function (response) {
          switch (response) {
            case "success-register":
              mreeir_notification(mreeir_messeges.downloadRegister);
              location.reload();
              break;
            case "success-login":
              mreeir_notification(mreeir_messeges.downloadLogin);
              location.reload();
              break;
            case "success-download":
              mreeir_get_dl_links_ajax(dlid, $wrapper_id);
              break;
            case "wrong-name":
              mreeir_notification(mreeir_messeges.downloadusername);
              break;
            case "wrong-family":
              mreeir_notification(mreeir_messeges.downloaduserfamily);
              break;
            case "wrong-code":
              mreeir_notification(mreeir_messeges.wrongCode);
              break;
            case "wrong-mobile":
              mreeir_notification(mreeir_messeges.wrongMobile);
              break;
            case "failed-register":
              mreeir_notification(mreeir_messeges.errorInRegister);
              break;
            case "failed-login":
              mreeir_notification(mreeir_messeges.errorInLogin);
              break;
          }
        },
        complete: function () {
          $this_submit
            .parent()
            .parent()
            .siblings(".mreeir-loading")
            .addClass("mreeir-hide");
        },
      });
    } else {
      mreeir_notification(mreeir_messeges.wrongCode);
    }
  });

  // download form submit - step 3 - get download links HTML
  function mreeir_get_dl_links_ajax($download_id, $wrapper_id) {
    jQuery.ajax({
      url: mreeir_js_options.ajaxurl,
      type: "POST",
      dataType: "html",
      data: {
        action: "mreeir_get_download_links",
        downloadid: $download_id,
        security: mreeir_js_options.noncegetdownloadlinks,
      },
      beforeSend: function () {
        $wrapper_id.find(".mreeir-loading").removeClass("mreeir-hide");
      },
      success: function (response) {
        $wrapper_id.find(".download-inputs").fadeOut().remove();
        $wrapper_id.append(response);
        $wrapper_id.find(".mreeir-dllinks-wrapper").hide().fadeIn(1000);
      },
      complete: function () {
        $wrapper_id.find(".mreeir-loading").addClass("mreeir-hide");
      },
    });
  }

  // downloads end

  // change redirect to checkout by url parameter - start
  var source = mreeir_get_url_parameter("src");

  if (source == "checkout" && !mreeir_is_empty(mreeir_js_options.checkouturl)) {
    mreeir_js_options.registerredirect = mreeir_js_options.checkouturl;
    mreeir_js_options.loginredirect = mreeir_js_options.checkouturl;
  }

  // change redirect to limited url - start

  function kerasno_is_valid_url(string) {
    let url;
    try {
      url = new URL(string);
    } catch (_) {
      return false;
    }
    return url.protocol === "http:" || url.protocol === "https:";
  }

  var limited_url = mreeir_get_url_parameter("limited");
  limited_url = decodeURIComponent(limited_url);

  if (kerasno_is_valid_url(limited_url)) {
    mreeir_js_options.registerredirect = limited_url;
    mreeir_js_options.loginredirect = limited_url;
  }

  // change redirect to limited url - end

  //modal start
  // Open Full Modal
  var call_popup = mreeir_js_options.popup_selector;
  if (call_popup.trim()) {
    $(call_popup).on("click", function (e) {
      e.preventDefault();
      $("#mreeir-modal-full-outer").show();
      $(".mreeir-form-wrapper.getmobileemail #mobileemail").focus();
    });
  }

  $(".mreeir-modal-full").on("click", function (e) {
    e.preventDefault();
    $("#mreeir-modal-full-outer").show();
    $(".mreeir-form-wrapper.getmobileemail #mobileemail").focus();
  });

  //Close Full Modal By Click Overlay
  $("#mreeir-modal-full-outer")
    .on("click", function () {
      $("#mreeir-modal-full-outer").hide();
    })
    .children()
    .click(function (e) {
      return false;
    });

  //Close Full Modal By ESC Key Press
  $(document).on("keydown", function (event) {
    if (event.key == "Escape") {
      if ($("#mreeir-modal-full-outer").length > 0) {
        $("#mreeir-modal-full-outer").hide();
      }
    }
  });
  //modal end

  $(".mreeir-form-wrapper.getmobileemail #mobileemail").focus();

  // resend counter contoroller - start
  //TODO: mreeir_resend_code_master
  // countdown interval holders to prevent overlapping timers
  var mreeir_cd_master = null;
  var mreeir_cd_second = null;
  var mreeir_cd_tab_reset = null;
  var mreeir_cd_tab_login = null;
  var mreeir_cd_tab_register = null;
  function mreeir_resend_code_master() {
    var $full_time = mreeir_js_options.resendcodetime;
    if (mreeir_cd_master) {
      clearInterval(mreeir_cd_master);
      mreeir_cd_master = null;
    }
    $(".mreeir-form-wrapper.getcode .mreeir-resendtime").text(
      mreeirsec2human($full_time)
    );
    $full_time--;
    mreeir_cd_master = setInterval(function () {
      $(".mreeir-form-wrapper.getcode .mreeir-resendtime").text(
        mreeirsec2human($full_time)
      );
      $full_time--;
      if ($full_time < 0) {
        clearInterval(mreeir_cd_master);
        mreeir_cd_master = null;
        $(".mreeir-form-wrapper.getcode .mreeir-resendtime").text("");
        mreeir_resend_code_master_timer_off();
      }
    }, 1000);
  }

  function mreeirsec2human(seconds) {
    sec = seconds % 60;
    min = parseInt(seconds / 60);
    if (sec.toString().length == 1) {
      // padding
      sec = "0" + sec;
    }
    if (min.toString().length == 1) {
      // padding
      min = "0" + min;
    }
    return min + ":" + sec;
  }

  function mreeir_resend_code_master_timer_off() {
    $('.mreeir-form-wrapper.getcode .hfield_wrapper.hfield_wrapper_captcha,.mreeir-form-wrapper.getcodereset .hfield_wrapper.hfield_wrapper_captcha').show();
    if ($(".woosub_captcha_refresh").length > 0) {
      $(".woosub_captcha_refresh").trigger("click");
    }
    $(".mreeir-form-wrapper.getcode .mreeir-resendcode-wrapper").hide();
    $(".mreeir-form-wrapper.getcode .mreeir-resendcode-wrapper-" + glob_auth_way).show();
    $(".mreeir-form-wrapper.getcode .kepro_auth_ways").show();

    $(".mreeir-form-wrapper.getcode .mreeir-resendtime-wrapper").hide();
    $(".mreeir-form-wrapper.getcode #" + glob_auth_way + "_wrapper").hide();
    $("#getcode" + glob_auth_way).prop("checked", true);

    // $('.mreeir-form-wrapper.getcode .mreeir-resendcode-wrapper').show();

    //tabbed form - login tab
    $(
      ".mreeir-form-wrapper.logintab-otp-getcode .mreeir-resendtime-wrapper"
    ).hide();
    $(
      ".mreeir-form-wrapper.logintab-otp-getcode .mreeir-resendcode-wrapper"
    ).show();

    //tabbed form - register tab
    $(
      ".mreeir-form-wrapper.getcode-tab-register .mreeir-resendtime-wrapper"
    ).hide();
    $(
      ".mreeir-form-wrapper.getcode-tab-register .mreeir-resendcode-wrapper"
    ).show();

    //tabbed form - reset tab
    $(
      ".mreeir-form-wrapper.resettab-otp-getcode .mreeir-resendtime-wrapper"
    ).hide();
    $(
      ".mreeir-form-wrapper.resettab-otp-getcode .mreeir-resendcode-wrapper"
    ).show();
  }
  //TODO: mreeir_resend_code_master_sender_off
  function mreeir_resend_code_master_sender_off() {
    $('.mreeir-form-wrapper.getcode .hfield_wrapper.hfield_wrapper_captcha,.mreeir-form-wrapper.getcodereset .hfield_wrapper.hfield_wrapper_captcha').hide();
    $(".mreeir-form-wrapper.getcode .mreeir-resendtime-wrapper").show();
    $(".mreeir-form-wrapper.getcode .mreeir-resendcode-wrapper").hide();
    $(".mreeir-form-wrapper.getcode .kepro_auth_ways").hide();

    //tabbed form - login tab
    $(
      ".mreeir-form-wrapper.logintab-otp-getcode .mreeir-resendtime-wrapper"
    ).show();
    $(
      ".mreeir-form-wrapper.logintab-otp-getcode .mreeir-resendcode-wrapper"
    ).hide();

    //tabbed form - register tab
    $(
      ".mreeir-form-wrapper.getcode-tab-register .mreeir-resendtime-wrapper"
    ).show();
    $(
      ".mreeir-form-wrapper.getcode-tab-register .mreeir-resendcode-wrapper"
    ).hide();

    //tabbed form - reset tab
    $(
      ".mreeir-form-wrapper.resettab-otp-getcode .mreeir-resendtime-wrapper"
    ).show();
    $(
      ".mreeir-form-wrapper.resettab-otp-getcode .mreeir-resendcode-wrapper"
    ).hide();
  }

  function mreeir_resend_code_second() {
    var $full_time = mreeir_js_options.resendcodetime;
    $(".mreeir-form-wrapper.getcodereset .mreeir-resendtime").text(
      mreeirsec2human($full_time)
    );
    $full_time--;
    var $master_cd = setInterval(function () {
      $(".mreeir-form-wrapper.getcodereset .mreeir-resendtime").text(
        mreeirsec2human($full_time)
      );
      $full_time--;
      if ($full_time < 0) {
        clearInterval($master_cd);
        $(".mreeir-form-wrapper.getcodereset .mreeir-resendtime").text("");
        resendCodeSecondTimerOff();
      }
    }, 1000);
  }

  function resendCodeSecondTimerOff() {
    $('.mreeir-form-wrapper.getcode .hfield_wrapper.hfield_wrapper_captcha,.mreeir-form-wrapper.getcodereset .hfield_wrapper.hfield_wrapper_captcha').show();
    if ($(".woosub_captcha_refresh").length > 0) {
      $(".woosub_captcha_refresh").trigger("click");
    }
    $(".mreeir-form-wrapper.getcodereset .mreeir-resendtime-wrapper").hide();
    $(".mreeir-form-wrapper.getcodereset .mreeir-resendcode-wrapper").show();
  }
  function mreeir_resend_code_second_sender_off() {
    $('.mreeir-form-wrapper.getcode .hfield_wrapper.hfield_wrapper_captcha,.mreeir-form-wrapper.getcodereset .hfield_wrapper.hfield_wrapper_captcha').hide();
    $(".mreeir-form-wrapper.getcodereset .mreeir-resendtime-wrapper").show();
    $(".mreeir-form-wrapper.getcodereset .mreeir-resendcode-wrapper").hide();
  }
  // resend counter contoroller - end

  // form just login - strat
  //  Verify Code - Just login Form
  $("#mobileemailjl").keypress(function (e) {
    var key = e.which;
    if (key == 13) {
      // the enter key code
      $("#submit-jl1").click();
      return false;
    }
  });

  $("#submit-jl1").on("click", function (e) {
    e.preventDefault();
    var $inputType = "";
    var $mobileemail = $("#mobileemailjl").val();
    var $userisnotauser = $(
      ".mreeir-form-wrapper.getmobileemail .userisnotauser"
    ).val();

    if (mreeir_is_empty($mobileemail)) {
      mreeir_notification(mreeir_messeges.MobileEmailNotValid);
      return;
    }

    if (mreeir_is_empty_fake($userisnotauser)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    $mobileemail = mreeir_correct_mobile_number($mobileemail);

    if (!mreeir_is_mobile($mobileemail) && !mreeir_is_email($mobileemail)) {
      mreeir_notification(mreeir_messeges.MobileEmailNotValid);
      return;
    } else {
      if (mreeir_is_email($mobileemail)) {
        $inputType = "email";
      }
      if (mreeir_is_mobile($mobileemail)) {
        $inputType = "mobile";
      }
    }

    glob_user_input = $mobileemail;
    glob_input_type = $inputType;

    if ($inputType == "mobile") {
      jQuery.ajax({
        url: mreeir_js_options.ajaxurl,
        type: "POST",
        data: {
          action: "mreeir_send_sms_jl",
          mobileemail: $mobileemail,
          userisnotauser: $userisnotauser,
          type: $inputType,
          security: mreeir_js_options.noncesendsms,
        },
        beforeSend: function () {
          $(".mreeir-element-wrapper.mreeir-loading").removeClass(
            "mreeir-hide"
          );
        },
        success: function (response) {
          if ($.trim(response) == "success") {
            jQuery(".mreeir-form-wrapper.getmobileemail").hide();
            mreeir_form_controller($(".mreeir-form-wrapper.getcode"));
            jQuery(".mreeir-form-wrapper.getcode #verifycode").focus();
            mreeir_resend_code_master();
          }
          if ($.trim(response) == "wrong-info") {
            mreeir_notification(mreeir_messeges.MobileEmailNotValid);
          }
          if ($.trim(response) == "send-failed") {
            mreeir_notification(mreeir_messeges.sendFailed);
          }
          if ($.trim(response) == "sms-send-time-limit") {
            mreeir_notification(mreeir_messeges.SendTimeLimit);
          }
          if ($.trim(response) == "wrong-mobile") {
            mreeir_notification(mreeir_messeges.wrongMobile);
          }
          if ($.trim(response) == "blocked-mobile") {
            mreeir_notification(mreeir_js_options.mobileisblocked);
          }
        },
        complete: function () {
          $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
        },
      });
    }

    if ($inputType == "email") {
      mreeir_form_controller($(".mreeir-form-wrapper.emailgetpassword"));
      $(".mreeir-form-wrapper.emailgetpassword #emailverifypassword").focus();
    }
  });

  $("#verifycodejl").keypress(function (e) {
    var key = e.which;
    if (key == 13) {
      // the enter key code
      $("#submit-code-just-login").click();
      return false;
    }
  });
  jQuery("#submit-code-just-login").on("click", function (e) {
    e.preventDefault();
    var verifycode = $("#verifycodejl").val();
    verifycode = mreeir_correct_mobile_number(verifycode);
    var $userisnotauser = $(
      ".mreeir-form-wrapper.getcode .userisnotauser"
    ).val();

    if (mreeir_is_empty_fake($userisnotauser)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    if (mreeir_is_empty(glob_user_input)) return;
    if (
      jQuery.isNumeric(verifycode) &&
      verifycode.length == mreeir_js_options.verifycodelength
    ) {
      jQuery.ajax({
        url: mreeir_js_options.ajaxurl,
        type: "POST",
        data: {
          action: "mreeir_verify_code_just_login",
          mobile: glob_user_input,
          userisnotauser: $userisnotauser,
          code: verifycode,
          nopass: "yes",
          security: mreeir_js_options.nonceverifycode,
        },
        beforeSend: function () {
          $(".mreeir-element-wrapper.mreeir-loading").removeClass(
            "mreeir-hide"
          );
        },
        success: function (response) {
          if ($.trim(response) == "success-login") {
            window.location = mreeir_js_options.loginredirect;
          }
          if ($.trim(response) == "wrong-code") {
            mreeir_notification(mreeir_messeges.wrongCode);
          }
          if ($.trim(response) == "wrong-mobile") {
            mreeir_notification(mreeir_messeges.wrongMobile);
          }
          if ($.trim(response) == "failed-login") {
            mreeir_notification(mreeir_messeges.errorInLogin);
          }
          if ($.trim(response) == "blocked-mobile") {
            mreeir_notification(mreeir_js_options.mobileisblocked);
          }
        },
        complete: function () {
          $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
        },
      });
    } else {
    }
  });
  // form just login - end

  // form just register - start
  //  Verify Code - Just Register Form
  $("#verifycodejr").keypress(function (e) {
    var key = e.which;
    if (key == 13) {
      // the enter key code
      $("#submit-code-just-register").click();
      return false;
    }
  });

  jQuery("#submit-code-just-register").on("click", function (e) {
    e.preventDefault();
    var verifycode = $("#verifycodejr").val();
    verifycode = mreeir_correct_mobile_number(verifycode);
    var $userisnotauser = $(
      ".mreeir-form-wrapper.getcode .userisnotauser"
    ).val();

    if (mreeir_is_empty_fake($userisnotauser)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    if (mreeir_is_empty(glob_user_input)) return;
    if (
      jQuery.isNumeric(verifycode) &&
      verifycode.length == mreeir_js_options.verifycodelength
    ) {
      jQuery.ajax({
        url: mreeir_js_options.ajaxurl,
        type: "POST",
        data: {
          action: "mreeir_verify_code",
          mobile: glob_user_input,
          userisnotauser: $userisnotauser,
          code: verifycode,
          security: mreeir_js_options.nonceverifycode,
        },
        beforeSend: function () {
          $(".mreeir-element-wrapper.mreeir-loading").removeClass(
            "mreeir-hide"
          );
        },
        success: function (response) {
          if ($.trim(response) == "success-register") {
            window.location = mreeir_js_options.registerredirect;
          }
          if ($.trim(response) == "success-login") {
            window.location = mreeir_js_options.loginredirect;
          }
          if ($.trim(response) == "wrong-code") {
            mreeir_notification(mreeir_messeges.wrongCode);
          }
          if ($.trim(response) == "wrong-mobile") {
            mreeir_notification(mreeir_messeges.wrongMobile);
          }
          if ($.trim(response) == "failed-register") {
            mreeir_notification(mreeir_messeges.errorInRegister);
          }
          if ($.trim(response) == "failed-login") {
            mreeir_notification(mreeir_messeges.errorInLogin);
          }
          if ($.trim(response) == "blocked-mobile") {
            mreeir_notification(mreeir_js_options.mobileisblocked);
          }
        },
        complete: function () {
          $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
        },
      });
    } else {
    }
  });
  // form just register - end

  // tabbed form - login with username password - start
  $("#mobileemailusernamepassword").keypress(function (e) {
    var key = e.which;
    if (key == 13) {
      // the enter key code
      $("#tabbed-login-submit").click();
      return false;
    }
  });

  $("#tabbed-login-submit").on("click", function (e) {
    e.preventDefault();
    var $inputType = "";
    var $mobileemailusername = $("#mobileemailusername").val();
    var $mobileemailusernamepassword = $("#mobileemailusernamepassword").val();
    var $userisnotauser = $(
      ".mreeir-form-wrapper.getmobileemailusername .userisnotauser"
    ).val();

    if (mreeir_is_empty($mobileemailusername)) {
      mreeir_notification(mreeir_messeges.MobileEmailNotValid);
      return;
    }
    if (mreeir_is_empty($mobileemailusernamepassword)) {
      mreeir_notification(mreeir_messeges.PasswordIsRequired);
      return;
    }

    if (mreeir_is_empty_fake($userisnotauser)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    $mobileemailusername = mreeir_correct_mobile_number($mobileemailusername);

    if (mreeir_is_email($mobileemailusername)) {
      $inputType = "email";
    } else if (mreeir_is_mobile($mobileemailusername)) {
      $inputType = "mobile";
    } else {
      $inputType = "username";
    }

    jQuery.ajax({
      url: mreeir_js_options.ajaxurl,
      type: "POST",
      data: {
        action: "mreeir_login_mobile_email_username",
        mobileemail: $mobileemailusername,
        mobileemailusernamepassword: $mobileemailusernamepassword,
        userisnotauser: $userisnotauser,
        type: $inputType,
        security: mreeir_js_options.loginwithall,
      },
      beforeSend: function () {
        $(".mreeir-element-wrapper.mreeir-loading").removeClass("mreeir-hide");
      },
      success: function (response) {
        if ($.trim(response) == "success-login") {
          mreeir_notification_success(mreeir_messeges.successLogin);
          window.location = mreeir_js_options.loginredirect;
        }
        if ($.trim(response) == "success-register") {
          mreeir_notification_success(mreeir_messeges.successRegister);
          window.location = mreeir_js_options.registerredirect;
        }
        if ($.trim(response) == "wrong-info") {
          mreeir_notification(mreeir_messeges.wrongInfo);
        }
        if ($.trim(response) == "wrong-mobile") {
          mreeir_notification(mreeir_messeges.wrongMobile);
        }
      },
      complete: function () {
        $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
      },
    });
  });
  // tabbed form - login with username password - end

  // tabbed form - reset otp - start
  $("#reset-getmobileemail").keypress(function (e) {
    var key = e.which;
    if (key == 13) {
      // the enter key code
      $("#reset-tab-getmobileemail").click();
      return false;
    }
  });

  $("#reset-tab-getmobileemail").on("click", function (e) {
    e.preventDefault();
    var $inputType = "";
    var $mobileemail = $("#reset-getmobileemail").val();
    var $userisnotauser = $(
      ".mreeir-form-wrapper.resettab-getmobileemail .userisnotauser"
    ).val();

    if (mreeir_is_empty($mobileemail)) {
      mreeir_notification(mreeir_messeges.MobileEmailNotValid);
      return;
    }

    if (mreeir_is_empty_fake($userisnotauser)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    $mobileemail = mreeir_correct_mobile_number($mobileemail);
    glob_user_input = $mobileemail;

    if (!mreeir_is_mobile($mobileemail) && !mreeir_is_email($mobileemail)) {
      mreeir_notification(mreeir_messeges.MobileEmailNotValid);
      return;
    } else {
      if (mreeir_is_email($mobileemail)) {
        $inputType = "email";
      }
      if (mreeir_is_mobile($mobileemail)) {
        $inputType = "mobile";
      }
    }

    glob_input_type = $inputType;

    jQuery.ajax({
      url: mreeir_js_options.ajaxurl,
      type: "POST",
      data: {
        action: "mreeir_send_mobilesms_emailcode",
        mobileemail: $mobileemail,
        userisnotauser: $userisnotauser,
        type: $inputType,
        security: mreeir_js_options.sendmobilesmsemailcode,
      },
      beforeSend: function () {
        $(".mreeir-element-wrapper.mreeir-loading").removeClass("mreeir-hide");
      },
      success: function (response) {
        if ($.trim(response) == "success") {
          mreeir_resend_code_master_sender_off();
          jQuery(".mreeir-form-wrapper.resettab-getmobileemail").hide();
          mreeir_form_controller(
            $(".mreeir-form-wrapper.resettab-otp-getcode")
          );
          jQuery(
            ".mreeir-form-wrapper.resettab-otp-getcode #reset-getcode"
          ).focus();
          mreeir_resend_code_master_tab_reset();
        }
        if ($.trim(response) == "wrong-info") {
          mreeir_notification(mreeir_messeges.MobileEmailNotValid);
        }
        if ($.trim(response) == "mobile-not-exists") {
          mreeir_notification(mreeir_messeges.mobileNotExists);
        }
        if ($.trim(response) == "email-not-exists") {
          mreeir_notification(mreeir_messeges.emailNotExists);
        }
        if ($.trim(response) == "send-failed") {
          mreeir_notification(mreeir_messeges.sendFailed);
        }
        if ($.trim(response) == "send-email-failed") {
          mreeir_notification(mreeir_messeges.sendEmailFailed);
        }
        if ($.trim(response) == "sms-send-time-limit") {
          mreeir_notification(mreeir_messeges.SendTimeLimit);
        }
        if ($.trim(response) == "wrong-mobile") {
          mreeir_notification(mreeir_messeges.wrongMobile);
        }
      },
      complete: function () {
        $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
      },
    });
  });

  function mreeir_resend_code_master_tab_reset() {
    var $full_time = mreeir_js_options.resendcodetime;

    // for tabbed form - login tab
    if (mreeir_cd_tab_reset) {
      clearInterval(mreeir_cd_tab_reset);
      mreeir_cd_tab_reset = null;
    }
    $(".mreeir-form-wrapper.resettab-otp-getcode .mreeir-resendtime").text(
      mreeirsec2human($full_time)
    );
    $full_time--;
    mreeir_cd_tab_reset = setInterval(function () {
      $(".mreeir-form-wrapper.resettab-otp-getcode .mreeir-resendtime").text(
        mreeirsec2human($full_time)
      );
      $full_time--;
      if ($full_time < 0) {
        clearInterval(mreeir_cd_tab_reset);
        mreeir_cd_tab_reset = null;
        $(".mreeir-form-wrapper.resettab-otp-getcode .mreeir-resendtime").text(
          ""
        );
        mreeir_resend_code_master_timer_off();
      }
    }, 1000);
  }

  $(
    ".resettab-otp-getcode.mreeir-form-wrapper.resettab-otp-getcode a.resend-code"
  ).on("click", function (e) {
    e.preventDefault();
    $("#reset-tab-getmobileemail").trigger("click");
  });

  $("#reset-getcode").keypress(function (e) {
    var key = e.which;
    if (key == 13) {
      // the enter key code
      $("#reset-tab-sendotp").click();
      return false;
    }
  });

  jQuery("#reset-tab-sendotp").on("click", function (e) {
    e.preventDefault();

    var kerasno_is_popup = false;
    var verify_code_target = $(e.target);

    if (verify_code_target.parents("#mreeir-modal-full-outer").length) {
      kerasno_is_popup = true;
    }

    var verifycode = $("#reset-getcode").val();
    verifycode = mreeir_correct_mobile_number(verifycode);
    var $userisnotauser = $(
      ".mreeir-form-wrapper.resettab-otp-getcode .userisnotauser"
    ).val();

    if (mreeir_is_empty(glob_input_type)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    if (mreeir_is_empty_fake($userisnotauser)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    if (mreeir_is_empty(glob_user_input)) return;
    resettab_code = verifycode;
    if (
      jQuery.isNumeric(verifycode) &&
      verifycode.length == mreeir_js_options.verifycodelength
    ) {
      jQuery.ajax({
        url: mreeir_js_options.ajaxurl,
        type: "POST",
        data: {
          action: "mreeir_verify_code_reset_tab",
          mobile: glob_user_input,
          userisnotauser: $userisnotauser,
          type: glob_input_type,
          code: verifycode,
          nopass: "yes",
          security: mreeir_js_options.nonceverifycodetabotp,
        },
        beforeSend: function () {
          $(".mreeir-element-wrapper.mreeir-loading").removeClass(
            "mreeir-hide"
          );
        },
        success: function (response) {
          if ($.trim(response) == "correct-code") {
            jQuery(".mreeir-form-wrapper.resettab-otp-getcode").hide();
            mreeir_form_controller(
              $(".mreeir-form-wrapper.resettab-setpassword")
            );
            jQuery(
              ".mreeir-form-wrapper.resettab-setpassword #reset-userpassword"
            ).focus();
          }
          if ($.trim(response) == "wrong-code") {
            mreeir_notification(mreeir_messeges.wrongCode);
          }
          if ($.trim(response) == "wrong-mobile") {
            mreeir_notification(mreeir_messeges.wrongMobile);
          }
          if ($.trim(response) == "wrong-email") {
            mreeir_notification(mreeir_messeges.wrongEmail);
          }
          if ($.trim(response) == "failed-login") {
            mreeir_notification(mreeir_messeges.errorInLogin);
          }
        },
        complete: function () {
          $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
        },
      });
    } else {
    }
  });

  //  Set New Password By Email / Mobile Code
  $("#reset-tab-set-password").on("click", function (e) {
    e.preventDefault();
    var $password = $("#reset-userpassword").val();
    $password = mreeir_correct_mobile_number($password);

    var $password_confirm = $("#reset-userpasswordconfirm").val();
    $password_confirm = mreeir_correct_mobile_number($password_confirm);

    var $userisnotauser = $(
      ".mreeir-form-wrapper.resettab-setpassword .userisnotauser"
    ).val();

    if (mreeir_is_empty_fake($userisnotauser)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    if (mreeir_is_empty($password) || mreeir_is_empty($password_confirm)) {
      mreeir_notification(mreeir_messeges.emptyPassword);
      return;
    }

    if ($password != $password_confirm) {
      mreeir_notification(mreeir_messeges.mismatchPassword);
      return;
    }

    if (mreeir_is_empty(glob_user_input)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    if (mreeir_is_empty(glob_input_type)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    if (mreeir_is_empty(resettab_code)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    if (mreeir_js_options.strongpassword == "true") {
      if (!mreeir_is_password_ok($password)) {
        mreeir_notification(mreeir_messeges.passwordNotStrong);
        return;
      }
    }

    jQuery.ajax({
      url: mreeir_js_options.ajaxurl,
      type: "POST",
      data: {
        action: "mreeir_set_new_password_mobile_email",
        type: glob_input_type,
        emailmobile: glob_user_input,
        code: resettab_code,
        userisnotauser: $userisnotauser,
        password: $password,
        passwordconfirm: $password_confirm,
        security: mreeir_js_options.noncesetnewpasswordtab,
      },
      beforeSend: function () {
        $(".mreeir-element-wrapper.mreeir-loading").removeClass("mreeir-hide");
      },
      success: function (response) {
        if ($.trim(response) == "success") {
          mreeir_form_controller(
            $(".mreeir-form-wrapper.successresetpassinfo")
          );
        }
        if ($.trim(response) == "wrong-info") {
          mreeir_notification(mreeir_messeges.wrongInfo);
        }
        if ($.trim(response) == "passwords-mismatch") {
          mreeir_notification(mreeir_messeges.mismatchPassword);
        }
        if ($.trim(response) == "password-is-not-strong") {
          mreeir_notification(mreeir_messeges.passwordNotStrong);
        }
      },
      complete: function () {
        $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
      },
    });
  });
  // tabbed form - reset otp - end

  // tabbed form - login with mobile / email otp - start
  $("#mobileemailjltab").keypress(function (e) {
    var key = e.which;
    if (key == 13) {
      // the enter key code
      $("#justlogin-tab").click();
      return false;
    }
  });

  $("#justlogin-tab").on("click", function (e) {
    e.preventDefault();
    var $inputType = "";
    var $mobileemail = $("#mobileemailjltab").val();
    var $userisnotauser = $(
      ".mreeir-form-wrapper.getmobileemail .userisnotauser"
    ).val();

    if (mreeir_is_empty($mobileemail)) {
      mreeir_notification(mreeir_messeges.MobileEmailNotValid);
      return;
    }

    if (mreeir_is_empty_fake($userisnotauser)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    $mobileemail = mreeir_correct_mobile_number($mobileemail);
    glob_user_input = $mobileemail;

    if (!mreeir_is_mobile($mobileemail) && !mreeir_is_email($mobileemail)) {
      mreeir_notification(mreeir_messeges.MobileEmailNotValid);
      return;
    } else {
      if (mreeir_is_email($mobileemail)) {
        $inputType = "email";
      }
      if (mreeir_is_mobile($mobileemail)) {
        $inputType = "mobile";
      }
    }

    glob_input_type = $inputType;

    jQuery.ajax({
      url: mreeir_js_options.ajaxurl,
      type: "POST",
      data: {
        action: "mreeir_send_mobilesms_emailcode",
        mobileemail: $mobileemail,
        userisnotauser: $userisnotauser,
        type: $inputType,
        security: mreeir_js_options.sendmobilesmsemailcode,
      },
      beforeSend: function () {
        $(".mreeir-element-wrapper.mreeir-loading").removeClass("mreeir-hide");
      },
      success: function (response) {
        if ($.trim(response) == "success") {
          mreeir_resend_code_master_sender_off();
          jQuery(".mreeir-form-wrapper.getmobileemail").hide();
          mreeir_form_controller(
            $(".mreeir-form-wrapper.logintab-otp-getcode")
          );
          jQuery(
            ".mreeir-form-wrapper.logintab-otp-getcode #verifycode-loginotptab"
          ).focus();
          mreeir_resend_code_master_tab_login();
        }
        if ($.trim(response) == "wrong-info") {
          mreeir_notification(mreeir_messeges.MobileEmailNotValid);
        }
        if ($.trim(response) == "mobile-not-exists") {
          mreeir_notification(mreeir_messeges.mobileNotExists);
        }
        if ($.trim(response) == "email-not-exists") {
          mreeir_notification(mreeir_messeges.emailNotExists);
        }
        if ($.trim(response) == "send-failed") {
          mreeir_notification(mreeir_messeges.sendFailed);
        }
        if ($.trim(response) == "send-email-failed") {
          mreeir_notification(mreeir_messeges.sendEmailFailed);
        }
        if ($.trim(response) == "sms-send-time-limit") {
          mreeir_notification(mreeir_messeges.SendTimeLimit);
        }
        if ($.trim(response) == "wrong-mobile") {
          mreeir_notification(mreeir_messeges.wrongMobile);
        }
        if ($.trim(response) == "blocked-mobile") {
          mreeir_notification(mreeir_js_options.mobileisblocked);
        }
      },
      complete: function () {
        $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
      },
    });
  });

  function mreeir_resend_code_master_tab_login() {
    var $full_time = mreeir_js_options.resendcodetime;

    // for tabbed form - login tab
    if (mreeir_cd_tab_login) {
      clearInterval(mreeir_cd_tab_login);
      mreeir_cd_tab_login = null;
    }
    $(".mreeir-form-wrapper.logintab-otp-getcode .mreeir-resendtime").text(
      mreeirsec2human($full_time)
    );
    $full_time--;
    mreeir_cd_tab_login = setInterval(function () {
      $(".mreeir-form-wrapper.logintab-otp-getcode .mreeir-resendtime").text(
        mreeirsec2human($full_time)
      );
      $full_time--;
      if ($full_time < 0) {
        clearInterval(mreeir_cd_tab_login);
        mreeir_cd_tab_login = null;
        $(".mreeir-form-wrapper.logintab-otp-getcode .mreeir-resendtime").text(
          ""
        );
        mreeir_resend_code_master_timer_off();
      }
    }, 1000);
  }

  $(
    ".logintab-otp-getcode.mreeir-form-wrapper.logintab-otp-getcode a.resend-code"
  ).on("click", function (e) {
    e.preventDefault();
    $("#justlogin-tab").trigger("click");
  });

  $("#verifycode-loginotptab").keypress(function (e) {
    var key = e.which;
    if (key == 13) {
      // the enter key code
      $("#submit-logintabotp").click();
      return false;
    }
  });
  jQuery("#submit-logintabotp").on("click", function (e) {
    e.preventDefault();

    var kerasno_is_popup = false;
    var verify_code_target = $(e.target);

    if (verify_code_target.parents("#mreeir-modal-full-outer").length) {
      kerasno_is_popup = true;
    }

    var verifycode = $("#verifycode-loginotptab").val();
    verifycode = mreeir_correct_mobile_number(verifycode);
    var $userisnotauser = $(
      ".mreeir-form-wrapper.logintab-otp-getcode .userisnotauser"
    ).val();

    if (mreeir_is_empty(glob_input_type)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    if (mreeir_is_empty_fake($userisnotauser)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    if (mreeir_is_empty(glob_user_input)) return;
    if (
      jQuery.isNumeric(verifycode) &&
      verifycode.length == mreeir_js_options.verifycodelength
    ) {
      jQuery.ajax({
        url: mreeir_js_options.ajaxurl,
        type: "POST",
        data: {
          action: "mreeir_verify_code_just_login_tab",
          mobile: glob_user_input,
          userisnotauser: $userisnotauser,
          type: glob_input_type,
          code: verifycode,
          nopass: "yes",
          security: mreeir_js_options.nonceverifycodetabotp,
        },
        beforeSend: function () {
          $(".mreeir-element-wrapper.mreeir-loading").removeClass(
            "mreeir-hide"
          );
        },
        success: function (response) {
          if ($.trim(response) == "success-login") {
            mreeir_notification_success(mreeir_messeges.successLogin);
            if (
              kerasno_is_popup &&
              mreeir_js_options.popupnoredirect == "true"
            ) {
              location.reload();
            } else {
              window.location = mreeir_js_options.loginredirect;
            }
          }
          if ($.trim(response) == "wrong-code") {
            mreeir_notification(mreeir_messeges.wrongCode);
          }
          if ($.trim(response) == "wrong-mobile") {
            mreeir_notification(mreeir_messeges.wrongMobile);
          }
          if ($.trim(response) == "wrong-email") {
            mreeir_notification(mreeir_messeges.wrongEmail);
          }
          if ($.trim(response) == "failed-login") {
            mreeir_notification(mreeir_messeges.errorInLogin);
          }
          if ($.trim(response) == "blocked-mobile") {
            mreeir_notification(mreeir_js_options.mobileisblocked);
          }
        },
        complete: function () {
          $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
        },
      });
    } else {
    }
  });
  // tabbed form - login with mobile / email otp - end

  // tabbed form - register with password - inline verifcation - start
  // inline verify mobile/email - send code
  $("#regmobileemail").keypress(function (e) {
    var key = e.which;
    if (key == 13) {
      // the enter key code
      $("#inline-send-code").click();
      return false;
    }
  });

  $("#inline-send-code").on("click", function (e) {
    e.preventDefault();

    var $inputType = "";
    var $mobileemail = $("#regmobileemail").val();
    var $userisnotauser = $(
      ".mreeir-form-wrapper.getmobileemail .userisnotauser"
    ).val();

    if (mreeir_is_empty($mobileemail)) {
      mreeir_notification(mreeir_messeges.MobileEmailNotValid);
      return;
    }

    if (mreeir_is_empty_fake($userisnotauser)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    $mobileemail = mreeir_correct_mobile_number($mobileemail);

    if (
      mreeir_js_options.complexdisableemail == "true" &&
      mreeir_is_email($mobileemail)
    ) {
      mreeir_notification(mreeir_messeges.MobileEmailNotValid);
      return;
    }

    if (!mreeir_is_mobile($mobileemail) && !mreeir_is_email($mobileemail)) {
      mreeir_notification(mreeir_messeges.MobileEmailNotValid);
      return;
    } else {
      if (mreeir_is_email($mobileemail)) {
        $inputType = "email";
      }
      if (mreeir_is_mobile($mobileemail)) {
        $inputType = "mobile";
      }
    }
    glob_user_input = $mobileemail;
    glob_input_type = $inputType;
    jQuery.ajax({
      url: mreeir_js_options.ajaxurl,
      type: "POST",
      data: {
        action: "mreeir_send_mobilesms_emailcode_just_register",
        mobileemail: $mobileemail,
        userisnotauser: $userisnotauser,
        type: $inputType,
        isreg: "1",
        security: mreeir_js_options.sendmobilesmsemailcode,
      },
      beforeSend: function () {
        $(".mreeir-element-wrapper.mreeir-loading").removeClass("mreeir-hide");
      },
      success: function (response) {
        if ($.trim(response) == "success") {
          $("div#inline-verify-step1.inline-verify-inner").hide();
          $("div#inline-verify-step2.inline-verify-inner").css(
            "display",
            "grid"
          );
          $(
            "div#inline-verify-step2.inline-verify-inner input#inlineverify"
          ).focus();
        }
        if ($.trim(response) == "repeated-mobile") {
          mreeir_notification(mreeir_messeges.repeatedMobile);
        }
        if ($.trim(response) == "repeated-email") {
          mreeir_notification(mreeir_messeges.repeatedEmail);
        }
        if ($.trim(response) == "wrong-info") {
          mreeir_notification(mreeir_messeges.MobileEmailNotValid);
        }
        if ($.trim(response) == "send-failed") {
          mreeir_notification(mreeir_messeges.sendFailed);
        }
        if ($.trim(response) == "send-email-failed") {
          mreeir_notification(mreeir_messeges.sendEmailFailed);
        }
        if ($.trim(response) == "sms-send-time-limit") {
          mreeir_notification(mreeir_messeges.SendTimeLimit);
        }
        if ($.trim(response) == "blocked-mobile") {
          mreeir_notification(mreeir_js_options.mobileisblocked);
        }
      },
      complete: function () {
        $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
      },
    });
  });

  // inline verify mobile/email - verify code
  $("#inlineverify").keypress(function (e) {
    var key = e.which;
    if (key == 13) {
      // the enter key code
      $("#inline-verify-code").click();
      return false;
    }
  });

  jQuery("#inline-verify-code").on("click", function (e) {
    e.preventDefault();
    var verifycode = $("#inlineverify").val();
    verifycode = mreeir_correct_mobile_number(verifycode);
    var $userisnotauser = $(
      ".mreeir-form-wrapper.registerwithuserpass .userisnotauser"
    ).val();

    if (mreeir_is_empty_fake($userisnotauser)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    if (mreeir_is_empty(glob_user_input)) return;
    if (
      jQuery.isNumeric(verifycode) &&
      verifycode.length == mreeir_js_options.verifycodelength
    ) {
      jQuery.ajax({
        url: mreeir_js_options.ajaxurl,
        type: "POST",
        data: {
          action: "mreeir_register_otp_inline",
          mobileemail: glob_user_input,
          userisnotauser: $userisnotauser,
          code: verifycode,
          type: glob_input_type,
          security: mreeir_js_options.otpregistertab,
        },
        beforeSend: function () {
          $(".mreeir-element-wrapper.mreeir-loading").removeClass(
            "mreeir-hide"
          );
        },
        success: function (response) {
          if ($.trim(response.data.message) == "success") {
            $("#inlineverifiedmobileemail").val(glob_user_input);
            $("#inlineverifiedchecksum").val(response.data.checksum);
            $("div#inline-verify-step2.inline-verify-inner").hide();
            $("div#inline-verify-step3.inline-verify-inner").show();
            $("div#inline-verify-step3.inline-verify-inner")
              .next("input")
              .focus();
          }
          if ($.trim(response.data.message) == "repeated-mobile") {
            mreeir_notification(mreeir_messeges.repeatedMobile);
          }
          if ($.trim(response.data.message) == "repeated-email") {
            mreeir_notification(mreeir_messeges.repeatedEmail);
          }
          if ($.trim(response.data.message) == "repeated-username") {
            mreeir_notification(mreeir_messeges.repeatedUsername);
          }
          if ($.trim(response.data.message) == "wrong-code") {
            mreeir_notification(mreeir_messeges.wrongCode);
          }
          if ($.trim(response.data.message) == "wrong-mobile") {
            mreeir_notification(mreeir_messeges.wrongMobile);
          }
          if ($.trim(response.data.message) == "wrong-info") {
            mreeir_notification(mreeir_messeges.wrongInfo);
          }
          if ($.trim(response.data.message) == "blocked-mobile") {
            mreeir_notification(mreeir_js_options.mobileisblocked);
          }
        },
        complete: function () {
          $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
        },
      });
    } else {
    }
  });
  // tabbed form - register with password - inline verifcation - end

  // tabbed form - register with password - last part - start
  $("#regpasswordconfirm").keypress(function (e) {
    var key = e.which;
    if (key == 13) {
      // the enter key code
      $("#tabbed-register-submit").click();
      return false;
    }
  });

  $("#tabbed-register-submit").on("click", function (e) {
    e.preventDefault();
    var $name = "";
    var $family = "";
    var $mobileemail = $("#regmobileemail").val();
    var $userisnotauser = $(
      ".mreeir-form-wrapper.registerwithuserpass .userisnotauser"
    ).val();
    var $pass = "";
    var $passconfirm = "";

    var $verified_emailmobile = $("#inlineverifiedmobileemail").val();
    var $verified_checksum = $("#inlineverifiedchecksum").val();
    var $verifycode = $("#inlineverify").val();

    $verified_emailmobile = mreeir_correct_mobile_number($verified_emailmobile);
    if (
      !mreeir_is_mobile($verified_emailmobile) &&
      !mreeir_is_email($verified_emailmobile)
    ) {
      mreeir_notification(mreeir_messeges.firstverify);
      return;
    }
    if (mreeir_is_empty($verified_checksum)) {
      mreeir_notification(mreeir_messeges.firstverify);
      return;
    }

    if (mreeir_is_empty($verifycode)) {
      mreeir_notification(mreeir_messeges.firstverify);
      return;
    }

    if (mreeir_js_options.complexdisablrnamefamily == "false") {
      // name and family not disable
      // name field
      if ($("#regfname").length > 0) {
        $name = $("#regfname").val();

        if (mreeir_is_empty($name)) {
          mreeir_notification(mreeir_messeges.nameRequired);
          return;
        }
      }

      // family field
      if ($("#reglname").length > 0) {
        $family = $("#reglname").val();

        if (mreeir_is_empty($family)) {
          mreeir_notification(mreeir_messeges.familyRequired);
          return;
        }
      }
    }

    $mobileemail = mreeir_correct_mobile_number($mobileemail);

    if (!mreeir_is_mobile($mobileemail) && !mreeir_is_email($mobileemail)) {
      mreeir_notification(mreeir_messeges.MobileEmailNotValid);
      return;
    } else {
      if (mreeir_is_email($mobileemail)) {
        $inputType = "email";
      }
      if (mreeir_is_mobile($mobileemail)) {
        $inputType = "mobile";
      }
    }

    // password field
    if ($("#regpassword").length > 0) {
      $pass = $("#regpassword").val();

      if (mreeir_is_empty($pass)) {
        mreeir_notification(mreeir_messeges.passRequired);
        return;
      }
    }

    // confirm password field
    if ($("#regpasswordconfirm").length > 0) {
      $passconfirm = $("#regpasswordconfirm").val();

      if (mreeir_is_empty($passconfirm)) {
        mreeir_notification(mreeir_messeges.passConfirmRequired);
        return;
      }

      if ($passconfirm != $pass) {
        mreeir_notification(mreeir_messeges.passNotEqual);
        return;
      }
    }

    jQuery.ajax({
      url: mreeir_js_options.ajaxurl,
      type: "POST",
      dataType: "json",
      data: {
        action: "mreeir_register_with_password",
        name: $name,
        family: $family,
        mobileemail: $mobileemail,
        pass: $pass,
        passconfirm: $passconfirm,
        userisnotauser: $userisnotauser,
        verified_emailmobile: $verified_emailmobile,
        verified_checksum: $verified_checksum,
        verifycode: $verifycode,
        type: $inputType,
        security: mreeir_js_options.registerwithpassword,
      },
      beforeSend: function () {
        $(".mreeir-element-wrapper.mreeir-loading").removeClass("mreeir-hide");
      },
      success: function (response) {
        if (response.success) {
          mreeir_notification_success(response.data.message);
          if (mreeir_is_empty(mreeir_js_options.registerredirectcomplete)) {
            window.location = mreeir_js_options.registerredirect;
          } else {
            window.location =
              mreeir_js_options.registerredirectcomplete +
              "?redirect=" +
              mreeir_js_options.registerredirect;
          }
        } else {
          mreeir_notification(response.data.message);
        }
      },
      complete: function () {
        $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
      },
    });
  });
  // tabbed form - register with password - last part - end

  // tabbed form - register with otp - start
  // send otp code
  $("#regotpmobileemail").keypress(function (e) {
    var key = e.which;
    if (key == 13) {
      // the enter key code
      $("#reg-otp-submit").click();
      return false;
    }
  });

  $("#reg-otp-submit").on("click", function (e) {
    e.preventDefault();
    var $inputType = "";
    var $mobileemail = $("#regotpmobileemail").val();
    var $userisnotauser = $(
      ".mreeir-form-wrapper.getmobileemail .userisnotauser"
    ).val();

    if (mreeir_is_empty($mobileemail)) {
      mreeir_notification(mreeir_messeges.MobileEmailNotValid);
      return;
    }

    if (mreeir_is_empty_fake($userisnotauser)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    $mobileemail = mreeir_correct_mobile_number($mobileemail);

    if (!mreeir_is_mobile($mobileemail) && !mreeir_is_email($mobileemail)) {
      mreeir_notification(mreeir_messeges.MobileEmailNotValid);
      return;
    } else {
      if (mreeir_is_email($mobileemail)) {
        $inputType = "email";
      }
      if (mreeir_is_mobile($mobileemail)) {
        $inputType = "mobile";
      }
    }
    glob_user_input = $mobileemail;
    glob_input_type = $inputType;
    jQuery.ajax({
      url: mreeir_js_options.ajaxurl,
      type: "POST",
      data: {
        action: "mreeir_send_mobilesms_emailcode_just_register",
        mobileemail: $mobileemail,
        userisnotauser: $userisnotauser,
        type: $inputType,
        isreg: "1",
        security: mreeir_js_options.sendmobilesmsemailcode,
      },
      beforeSend: function () {
        $(".mreeir-element-wrapper.mreeir-loading").removeClass("mreeir-hide");
      },
      success: function (response) {
        if ($.trim(response) == "success") {
          mreeir_resend_code_master_sender_off();
          jQuery(".mreeir-form-wrapper.registermobileemail").hide();
          mreeir_form_controller(
            $(".mreeir-form-wrapper.getcode-tab-register")
          );
          jQuery(
            ".mreeir-form-wrapper.getcode-tab-register #verifycoderegtab"
          ).focus();
          mreeir_resend_code_master_tab_register();
        }
        if ($.trim(response) == "repeated-username") {
          mreeir_notification(mreeir_messeges.repeatedUsername);
        }
        if ($.trim(response) == "repeated-mobile") {
          mreeir_notification(mreeir_messeges.repeatedMobile);
        }
        if ($.trim(response) == "repeated-email") {
          mreeir_notification(mreeir_messeges.repeatedEmail);
        }
        if ($.trim(response) == "wrong-info") {
          mreeir_notification(mreeir_messeges.MobileEmailNotValid);
        }
        if ($.trim(response) == "send-email-failed") {
          mreeir_notification(mreeir_messeges.sendEmailFailed);
        }
        if ($.trim(response) == "send-failed") {
          mreeir_notification(mreeir_messeges.sendFailed);
        }
        if ($.trim(response) == "sms-send-time-limit") {
          mreeir_notification(mreeir_messeges.SendTimeLimit);
        }
        if ($.trim(response) == "blocked-mobile") {
          mreeir_notification(mreeir_js_options.mobileisblocked);
        }
      },
      complete: function () {
        $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
      },
    });
  });

  $(".mreeir-form-wrapper.getcode-tab-register a.resend-code").on(
    "click",
    function (e) {
      e.preventDefault();
      $("#reg-otp-submit").trigger("click");
    }
  );

  // verify otp code
  $("#verifycoderegtab").keypress(function (e) {
    var key = e.which;
    if (key == 13) {
      // the enter key code
      $("#submit-code-just-register-tab").click();
      return false;
    }
  });

  jQuery("#submit-code-just-register-tab").on("click", function (e) {
    e.preventDefault();
    var verifycode = $("#verifycoderegtab").val();
    verifycode = mreeir_correct_mobile_number(verifycode);
    var $userisnotauser = $(
      ".mreeir-form-wrapper.getcode-tab-register .userisnotauser"
    ).val();

    if (mreeir_is_empty_fake($userisnotauser)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    if (mreeir_is_empty(glob_user_input)) return;
    if (
      jQuery.isNumeric(verifycode) &&
      verifycode.length == mreeir_js_options.verifycodelength
    ) {
      jQuery.ajax({
        url: mreeir_js_options.ajaxurl,
        type: "POST",
        dataType: "json",
        data: {
          action: "mreeir_register_with_otp_tab",
          mobileemail: glob_user_input,
          userisnotauser: $userisnotauser,
          code: verifycode,
          type: glob_input_type,
          security: mreeir_js_options.otpregistertab,
        },
        beforeSend: function () {
          $(".mreeir-element-wrapper.mreeir-loading").removeClass(
            "mreeir-hide"
          );
        },
        success: function (response) {
          if (response.success) {
            mreeir_notification_success(response.data.message);
            if (mreeir_is_empty(mreeir_js_options.registerredirectcomplete)) {
              window.location = mreeir_js_options.registerredirect;
            } else {
              window.location =
                mreeir_js_options.registerredirectcomplete +
                "?redirect=" +
                mreeir_js_options.registerredirect;
            }
          } else {
            mreeir_notification(response.data.message);
          }
        },
        complete: function () {
          $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
        },
      });
    } else {
    }
  });

  function mreeir_resend_code_master_tab_register() {
    var $full_time = mreeir_js_options.resendcodetime;

    // for tabbed form - register tab
    if (mreeir_cd_tab_register) {
      clearInterval(mreeir_cd_tab_register);
      mreeir_cd_tab_register = null;
    }
    $(".mreeir-form-wrapper.getcode-tab-register .mreeir-resendtime").text(
      mreeirsec2human($full_time)
    );
    $full_time--;
    mreeir_cd_tab_register = setInterval(function () {
      $(".mreeir-form-wrapper.getcode-tab-register .mreeir-resendtime").text(
        mreeirsec2human($full_time)
      );
      $full_time--;
      if ($full_time < 0) {
        clearInterval(mreeir_cd_tab_register);
        mreeir_cd_tab_register = null;
        $(".mreeir-form-wrapper.getcode-tab-register .mreeir-resendtime").text(
          ""
        );
        mreeir_resend_code_master_timer_off();
      }
    }, 1000);
  }
  // tabbed form - register with otp - end

  $("#kepro_magic_link_btn").on("click", function (e) {
    e.preventDefault();

    let email = "";
    let _captcha = "";
    let _captcha_hash = "";
    let data = {};

    // شناسایی فیلد ایمیل
    if ($("#mobileemail").length > 0) {
      email = $("#mobileemail").val();

      // بررسی کپچا در این حالت خاص
      let captcha_selector = $(
        ".mreeir-form-wrapper.getmobileemail .hfield_wrapper.hfield_wrapper_captcha .hcaptcha_minifier .hcaptcha"
      );
      if (captcha_selector.length > 0) {
        let captcha_hash_selector = $(
          ".mreeir-form-wrapper.getmobileemail .hfield_wrapper.hfield_wrapper_captcha_image"
        ).find(".woosubcaptchavalue");
        _captcha = captcha_selector.val();
        _captcha_hash = captcha_hash_selector.val();

        if (!mreeir_is_empty(_captcha) && !mreeir_is_empty(_captcha_hash)) {
          if (!kerasno_check_is_number(_captcha)) {
            mreeir_notification("کد امنیتی باید عددی باشد");
            return;
          }

          if (_captcha.length !== 4) {
            mreeir_notification("کد امنیتی باید 4 رقمی باشد");
            return;
          }

          data.captcha = _captcha;
          data.captchahash = _captcha_hash;
        } else {
          mreeir_notification("کد امنیتی وارد شده معتبر نیست");
          return;
        }
      }
    } else if ($("#mobileemailjl").length > 0) {
      email = $("#mobileemailjl").val();
    } else if ($("#mobileemailusername").length > 0) {
      email = $("#mobileemailusername").val();
    } else {
      mreeir_notification(mreeir_messeges.wrongEmail);
      return;
    }

    if (!mreeir_is_email(email)) {
      mreeir_notification(mreeir_messeges.wrongEmail);
      return;
    }

    data = {
      ...data,
      action: "kepro_login_with_magic_link",
      email: email,
      security: mreeir_js_options.maginlink,
    };

    $.ajax({
      url: mreeir_js_options.ajaxurl,
      type: "POST",
      data: data,
      beforeSend: function () {
        $(".mreeir-element-wrapper.mreeir-loading").removeClass("mreeir-hide");
      },
      success: function (response) {
        if (response.success) {
          mreeir_notification(
            "در صورت صحیح بودن آدرس ایمیل، لینک ورود به سایت برای آن ارسال خواهد شد"
          );
        } else {
          mreeir_notification(response.data.message || "خطا در ارسال لینک");
        }
      },
      complete: function () {
        if ($(".woosub_captcha_refresh").length > 0) {
          $(".woosub_captcha_refresh").trigger("click");
        }
        $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
      },
    });
  });

  //  login/register/reset password form -start
  //  Get Mobile Or Email From User

  $(".kepro_auth_ways_inner label").click(function (e) {
    var current_input = $(this).find("input");
    current_input.prop("checked", true);
    var radioID = current_input.attr("id");
    var buttonID = "";
    // Set the button ID and name based on the selected radio button
    if (radioID === "getmobilesms") {
      buttonID = "submit-step1";
    } else if (radioID === "getmobileussd") {
      buttonID = "submit-ussd-step1";
    } else if (radioID === "getmobilebale") {
      buttonID = "submit-bale-step1";
    } else if (radioID === "getmobilevoice") {
      buttonID = "submit-voice-step1";
    }

    $(this).closest(".kepro_auth_ways").siblings("button").attr("id", buttonID);
    $(this)
      .closest(".kepro_auth_ways")
      .siblings("button")
      .attr("name", buttonID);
  });

  $("#mobileemail").keypress(function (e) {
    var key = e.which;
    if (key == 13) {
      // the enter key code
      if ($("#submit-step1").length > 0) {
        $("#submit-step1").click();
      }
      if ($("#submit-bale-step1").length > 0) {
        $("#submit-bale-step1").click();
      }
      if ($("#submit-ussd-step1").length > 0) {
        $("#submit-ussd-step1").click();
      }
      if ($("#submit-voice-step1").length > 0) {
        $("#submit-voice-step1").click();
      }
      return false;
    }
  });

  $(".getmobileemail .mreeir-form-submit button").click(function (e) {
    e.preventDefault();
    var currentBTN = $(this);

    if (currentBTN.attr("id") != "submit-step1") {
      return;
    }

    var $inputType = "";
    var $mobileemail = $("#mobileemail").val();
    var $userisnotauser = $(
      ".mreeir-form-wrapper.getmobileemail .userisnotauser"
    ).val();
    
    // captcha
    var _captcha = "";
    var _captcha_hash = "";
    var captcha_selector = $(
      ".mreeir-form-wrapper.getmobileemail .hfield_wrapper.hfield_wrapper_captcha .hcaptcha_minifier .hcaptcha"
    );
    if (captcha_selector.length > 0) {
      var captcha_hash_selector = $(
        ".mreeir-form-wrapper.getmobileemail .hfield_wrapper.hfield_wrapper_captcha_image"
      ).find(".woosubcaptchavalue");
      _captcha = captcha_selector.val();
      _captcha_hash = captcha_hash_selector.val();

      if (mreeir_is_empty(_captcha) || mreeir_is_empty(_captcha_hash)) {
        mreeir_notification("کد امنیتی وارد شده معتبر نیست");
        return;
      }

      if (!kerasno_check_is_number(_captcha)) {
        mreeir_notification("کد امنیتی باید عددی باشد");
        return;
      }

      if (_captcha.length !== 4) {
        mreeir_notification("کد امنیتی باید 4 رقمی باشد");
        return;
      }
    }
    if (mreeir_is_empty($mobileemail)) {
      mreeir_notification(mreeir_messeges.MobileEmailNotValid);
      return;
    }

    if (mreeir_is_empty_fake($userisnotauser)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    $mobileemail = mreeir_correct_mobile_number($mobileemail);

    if (!mreeir_is_mobile($mobileemail) && !mreeir_is_email($mobileemail)) {
      mreeir_notification(mreeir_messeges.MobileEmailNotValid);
      return;
    } else {
      if (mreeir_is_email($mobileemail)) {
        $inputType = "email";
      }
      if (mreeir_is_mobile($mobileemail)) {
        $inputType = "mobile";
      }
    }

    glob_user_input = $mobileemail;
    glob_input_type = $inputType;
    glob_auth_way = "sms";

    if ($inputType == "mobile") {
      jQuery.ajax({
        url: mreeir_js_options.ajaxurl,
        type: "POST",
        data: {
          action: "mreeir_send_sms",
          mobileemail: $mobileemail,
          userisnotauser: $userisnotauser,
          type: $inputType,
          captcha: _captcha,
          captchahash: _captcha_hash,
          security: mreeir_js_options.noncesendsms,
        },
        beforeSend: function () {
          $(".mreeir-element-wrapper.mreeir-loading").removeClass(
            "mreeir-hide"
          );
        },
        success: function (response) {
          if ($.trim(response) == "success") {
            jQuery(".mreeir-form-wrapper.getmobileemail").hide();
            mreeir_form_controller($(".mreeir-form-wrapper.getcode"));
            jQuery(".mreeir-form-wrapper.getcode #verifycode").focus();
            mreeir_resend_code_master();
            kepro_getcode_message_controller(glob_auth_way);
          }
          if ($.trim(response) == "wrong-captcha") {
            if ($(".woosub_captcha_refresh").length > 0) {
              $(".woosub_captcha_refresh").trigger("click");
            }
            mreeir_notification(mreeir_messeges.CaptchaNotValid);
          }
          if ($.trim(response) == "wrong-info") {
            mreeir_notification(mreeir_messeges.MobileEmailNotValid);
            if (mreeir_js_options.showauthwaysonfail == "yes") {
              currentBTN
                .siblings(".kepro_auth_ways")
                .removeClass("kepro_hide_auth_ways");
            }
          }
          if ($.trim(response) == "send-failed") {
            mreeir_notification(mreeir_messeges.sendFailed);
            if (mreeir_js_options.showauthwaysonfail == "yes") {
              currentBTN
                .siblings(".kepro_auth_ways")
                .removeClass("kepro_hide_auth_ways");
            }
          }
          if ($.trim(response) == "sms-send-time-limit") {
            mreeir_notification(mreeir_messeges.SendTimeLimit);
            if (mreeir_js_options.showauthwaysonfail == "yes") {
              currentBTN
                .siblings(".kepro_auth_ways")
                .removeClass("kepro_hide_auth_ways");
            }
          }
          if ($.trim(response) == "blocked-mobile") {
            mreeir_notification(mreeir_js_options.mobileisblocked);
          }
        },
        complete: function () {
          $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
        },
      });
    }

    if ($inputType == "email") {
      if ($(".hfield_wrapper.hfield_wrapper_captcha").length > 0) {
        jQuery.ajax({
          url: mreeir_js_options.ajaxurl,
          type: "POST",
          data: {
            action: "just_verify_captcha",
            userisnotauser: $userisnotauser,
            captcha: _captcha,
            captchahash: _captcha_hash,
            security: mreeir_js_options.nonceverifycaptcha,
          },
          beforeSend: function () {
            $(".mreeir-element-wrapper.mreeir-loading").removeClass(
              "mreeir-hide"
            );
          },
          success: function (response) {
            if ($.trim(response) == "captcha-is-correct") {
              mreeir_form_controller(
                $(".mreeir-form-wrapper.emailgetpassword")
              );
              $(
                ".mreeir-form-wrapper.emailgetpassword #emailverifypassword"
              ).focus();
            }
            if ($.trim(response) == "wrong-captcha") {
              mreeir_notification(mreeir_messeges.wrongCaptcha);
              if ($(".woosub_captcha_refresh").length > 0) {
                $(".woosub_captcha_refresh").trigger("click");
              }
            }
          },
          complete: function () {
            $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
          },
        });
      } else {
        mreeir_form_controller($(".mreeir-form-wrapper.emailgetpassword"));
        $(".mreeir-form-wrapper.emailgetpassword #emailverifypassword").focus();
      }
    }
  });

  //bale
  $(".getmobileemail .mreeir-form-submit button").click(function (e) {
    e.preventDefault();
    var currentBTN = $(this);

    if (currentBTN.attr("id") != "submit-bale-step1") {
      return;
    }

    var $mobile = $("#mobileemail").val();
    var $userisnotauser = $(
      ".mreeir-form-wrapper.getmobileemail .userisnotauser"
    ).val();

    // captcha
    var _captcha = "";
    var _captcha_hash = "";
    var captcha_selector = $(
      ".mreeir-form-wrapper.getmobileemail .hfield_wrapper.hfield_wrapper_captcha .hcaptcha_minifier .hcaptcha"
    );
    if (captcha_selector.length > 0) {
      var captcha_hash_selector = $(
        ".mreeir-form-wrapper.getmobileemail .hfield_wrapper.hfield_wrapper_captcha_image"
      ).find(".woosubcaptchavalue");
      _captcha = captcha_selector.val();
      _captcha_hash = captcha_hash_selector.val();

      if (mreeir_is_empty(_captcha) || mreeir_is_empty(_captcha_hash)) {
        mreeir_notification("کد امنیتی وارد شده معتبر نیست");
        return;
      }

      if (!kerasno_check_is_number(_captcha)) {
        mreeir_notification("کد امنیتی باید عددی باشد");
        return;
      }

      if (_captcha.length !== 4) {
        mreeir_notification("کد امنیتی باید 4 رقمی باشد");
        return;
      }
    }

    if (mreeir_is_empty($mobile)) {
      mreeir_notification(mreeir_messeges.bale_mobile_not_valid);
      return;
    }

    if (mreeir_is_empty_fake($userisnotauser)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    $mobile = mreeir_correct_mobile_number($mobile);

    if (!mreeir_is_mobile_bale($mobile)) {
      mreeir_notification(mreeir_messeges.bale_mobile_not_valid);
      return;
    }

    glob_user_input = mreeir_normalize_mobile_for_bale($mobile);
    glob_input_type = "mobile";
    glob_auth_way = "bale";

    jQuery.ajax({
      url: mreeir_js_options.ajaxurl,
      type: "POST",
      data: {
        action: "kepro_bale_set_otp",
        mobile: $mobile,
        captcha: _captcha,
        captchahash: _captcha_hash,
        userisnotauser: $userisnotauser,
        security: mreeir_js_options.noncesendsms,
      },
      beforeSend: function () {
        $(".mreeir-element-wrapper.mreeir-loading").removeClass("mreeir-hide");
      },
      success: function (response) {
        if (response.success) {
          var $bale_number = response.data.bale;
          jQuery(".mreeir-form-wrapper.getmobileemail").hide();
          mreeir_form_controller($(".mreeir-form-wrapper.getcode"));
          jQuery(".mreeir-form-wrapper.getcode #verifycode").focus();
          mreeir_resend_code_master();
          kepro_getcode_message_controller(glob_auth_way);
          $(".mreeir-form-wrapper.getcode #bale_wrapper #balenumber").text(
            $bale_number
          );
        } else {
          mreeir_notification(response.data.message);
          if (mreeir_js_options.showauthwaysonfail == "yes") {
            currentBTN
              .siblings(".kepro_auth_ways")
              .removeClass("kepro_hide_auth_ways");
          }
        }
      },

      complete: function () {
        $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
      },
    });
  });
  var mreeir_normalize_mobile_for_bale = function (mobileNumber) {
    // فقط ارقام را نگه‌دار
    mobileNumber = mobileNumber.replace(/\D/g, "");

    // اگر 11 رقمی و با 09 شروع شده باشد
    if (/^09\d{9}$/.test(mobileNumber)) {
      return mobileNumber;
    }

    // اگر 12 رقمی باشد و با صفر شروع نشده باشد
    if (/^[1-9]\d{11}$/.test(mobileNumber)) {
      return "0" + mobileNumber.substring(2); // حذف دو رقم اول و اضافه کردن صفر
    }

    // در غیر این صورت مقدار نامعتبر
    return false;
  };

  //ussd
  $(".getmobileemail .mreeir-form-submit button").click(function (e) {
    e.preventDefault();
    var currentBTN = $(this);

    if (currentBTN.attr("id") != "submit-ussd-step1") {
      return;
    }

    var $mobile = $("#mobileemail").val();
    var $userisnotauser = $(
      ".mreeir-form-wrapper.getmobileemail .userisnotauser"
    ).val();

    // captcha
    var _captcha = "";
    var _captcha_hash = "";
    var captcha_selector = $(
      ".mreeir-form-wrapper.getmobileemail .hfield_wrapper.hfield_wrapper_captcha .hcaptcha_minifier .hcaptcha"
    );
    if (captcha_selector.length > 0) {
      var captcha_hash_selector = $(
        ".mreeir-form-wrapper.getmobileemail .hfield_wrapper.hfield_wrapper_captcha_image"
      ).find(".woosubcaptchavalue");
      _captcha = captcha_selector.val();
      _captcha_hash = captcha_hash_selector.val();

      if (mreeir_is_empty(_captcha) || mreeir_is_empty(_captcha_hash)) {
        mreeir_notification("کد امنیتی وارد شده معتبر نیست");
        return;
      }

      if (!kerasno_check_is_number(_captcha)) {
        mreeir_notification("کد امنیتی باید عددی باشد");
        return;
      }

      if (_captcha.length !== 4) {
        mreeir_notification("کد امنیتی باید 4 رقمی باشد");
        return;
      }
    }

    if (mreeir_is_empty($mobile)) {
      mreeir_notification(mreeir_messeges.ussd_mobile_not_valid);
      return;
    }

    if (mreeir_is_empty_fake($userisnotauser)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    $mobile = mreeir_correct_mobile_number($mobile);

    if (!mreeir_is_mobile($mobile)) {
      mreeir_notification(mreeir_messeges.ussd_mobile_not_valid);
      return;
    }

    glob_user_input = $mobile;
    glob_input_type = "mobile";
    glob_auth_way = "ussd";

    jQuery.ajax({
      url: mreeir_js_options.ajaxurl,
      type: "POST",
      data: {
        action: "kepro_ussd_set_otp",
        mobile: $mobile,
        captcha: _captcha,
        captchahash: _captcha_hash,
        userisnotauser: $userisnotauser,
        security: mreeir_js_options.noncesendsms,
      },
      beforeSend: function () {
        $(".mreeir-element-wrapper.mreeir-loading").removeClass("mreeir-hide");
      },
      success: function (response) {
        if (response.success) {
          var $ussd_number = response.data.ussd;
          jQuery(".mreeir-form-wrapper.getmobileemail").hide();
          mreeir_form_controller($(".mreeir-form-wrapper.getcode"));
          jQuery(".mreeir-form-wrapper.getcode #verifycode").focus();
          mreeir_resend_code_master();
          kepro_getcode_message_controller(glob_auth_way);
          $(".mreeir-form-wrapper.getcode #ussd_wrapper #ussdnumber").text(
            $ussd_number
          );
        } else {
          mreeir_notification(response.data.message);
          if (mreeir_js_options.showauthwaysonfail == "yes") {
            currentBTN
              .siblings(".kepro_auth_ways")
              .removeClass("kepro_hide_auth_ways");
          }
        }
      },

      complete: function () {
        $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
      },
    });
  });

  //voice
  $(".getmobileemail .mreeir-form-submit button").click(function (e) {
    e.preventDefault();
    var currentBTN = $(this);

    if (currentBTN.attr("id") != "submit-voice-step1") {
      return;
    }

    var $mobile = $("#mobileemail").val();
    var $userisnotauser = $(
      ".mreeir-form-wrapper.getmobileemail .userisnotauser"
    ).val();

    if (mreeir_is_empty($mobile)) {
      mreeir_notification(mreeir_messeges.voice_mobile_not_valid);
      return;
    }

    if (mreeir_is_empty_fake($userisnotauser)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    $mobile = mreeir_correct_mobile_number($mobile);

    if (!mreeir_is_mobile($mobile)) {
      mreeir_notification(mreeir_messeges.voice_mobile_not_valid);
      return;
    }

    // captcha
    var _captcha = "";
    var _captcha_hash = "";
    var captcha_selector = $(
      ".mreeir-form-wrapper.getmobileemail .hfield_wrapper.hfield_wrapper_captcha .hcaptcha_minifier .hcaptcha"
    );
    if (captcha_selector.length > 0) {
      var captcha_hash_selector = $(
        ".mreeir-form-wrapper.getmobileemail .hfield_wrapper.hfield_wrapper_captcha_image"
      ).find(".woosubcaptchavalue");
      _captcha = captcha_selector.val();
      _captcha_hash = captcha_hash_selector.val();

      if (mreeir_is_empty(_captcha) || mreeir_is_empty(_captcha_hash)) {
        mreeir_notification("کد امنیتی وارد شده معتبر نیست");
        return;
      }

      if (!kerasno_check_is_number(_captcha)) {
        mreeir_notification("کد امنیتی باید عددی باشد");
        return;
      }

      if (_captcha.length !== 4) {
        mreeir_notification("کد امنیتی باید 4 رقمی باشد");
        return;
      }
    }

    glob_user_input = $mobile;
    glob_input_type = "mobile";
    glob_auth_way = "voice";

    jQuery.ajax({
      url: mreeir_js_options.ajaxurl,
      type: "POST",
      data: {
        action: "kepro_voice_set_otp",
        mobile: $mobile,
        captcha: _captcha,
        captchahash: _captcha_hash,
        userisnotauser: $userisnotauser,
        security: mreeir_js_options.noncesendsms,
      },
      beforeSend: function () {
        $(".mreeir-element-wrapper.mreeir-loading").removeClass("mreeir-hide");
      },
      success: function (response) {
        if (response.success) {
          jQuery(".mreeir-form-wrapper.getmobileemail").hide();
          mreeir_form_controller($(".mreeir-form-wrapper.getcode"));
          jQuery(".mreeir-form-wrapper.getcode #verifycode").focus();
          mreeir_resend_code_master();
          kepro_getcode_message_controller(glob_auth_way);
        } else {
          mreeir_notification(response.data.message);
          if (mreeir_js_options.showauthwaysonfail == "yes") {
            currentBTN
              .siblings(".kepro_auth_ways")
              .removeClass("kepro_hide_auth_ways");
          }
        }
      },
      complete: function () {
        $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
      },
    });
  });

  $(".mreeir-form-wrapper.getcode #verifycode").on("keyup", function () {
    var $length = $(this).val().length;
    if ($length == mreeir_js_options.verifycodelength) {
      $(this)
        .closest(".mreeir-element-wrapper")
        .siblings(".mreeir-element-wrapper")
        .find("#submit-step2")
        .removeAttr("disabled");
    } else {
      $(this)
        .closest(".mreeir-element-wrapper")
        .siblings(".mreeir-element-wrapper")
        .find("#submit-step2")
        .attr("disabled", "disabled");
    }
  });

  $('input[type="radio"]#getcodesms').click(function (e) {
    kepro_getcode_manage_resend_code_link("sms");
  });
  $('input[type="radio"]#getcodebale').click(function (e) {
    kepro_getcode_manage_resend_code_link("bale");
  });
  $('input[type="radio"]#getcodeussd').click(function (e) {
    kepro_getcode_manage_resend_code_link("ussd");
  });
  $('input[type="radio"]#getcodevoice').click(function (e) {
    kepro_getcode_manage_resend_code_link("voice");
  });

  function kepro_getcode_manage_resend_code_link($item) {
    $(".mreeir-form-wrapper.getcode .mreeir-resendcode-wrapper").hide();
    $(
      ".mreeir-form-wrapper.getcode .mreeir-resendcode-wrapper-" + $item
    ).show();
  }

  $(".mreeir-form-wrapper.getcode a.resend-code").on("click", function (e) {
    e.preventDefault();
    var $inputType = "";
    var $mobileemail = glob_user_input;
    var $userisnotauser = $(
      ".mreeir-form-wrapper.getcode .userisnotauser"
    ).val();

    if (mreeir_is_empty($mobileemail)) {
      mreeir_notification(mreeir_messeges.MobileEmailNotValid);
      return;
    }

    if (mreeir_is_empty_fake($userisnotauser)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    // captcha
    var _captcha = "";
    var _captcha_hash = "";
    var captcha_selector = $(
      ".mreeir-form-wrapper.getcode .hfield_wrapper.hfield_wrapper_captcha .hcaptcha_minifier .hcaptcha"
    );
    if (captcha_selector.length > 0) {
      var captcha_hash_selector = $(
        ".mreeir-form-wrapper.getcode .hfield_wrapper.hfield_wrapper_captcha_image"
      ).find(".woosubcaptchavalue");
      _captcha = captcha_selector.val();
      _captcha_hash = captcha_hash_selector.val();

      if (mreeir_is_empty(_captcha) || mreeir_is_empty(_captcha_hash)) {
        mreeir_notification("کد امنیتی وارد شده معتبر نیست");
        return;
      }

      if (!kerasno_check_is_number(_captcha)) {
        mreeir_notification("کد امنیتی باید عددی باشد");
        return;
      }

      if (_captcha.length !== 4) {
        mreeir_notification("کد امنیتی باید 4 رقمی باشد");
        return;
      }
    }

    $inputType = "mobile";
    glob_auth_way = "sms";
    jQuery.ajax({
      url: mreeir_js_options.ajaxurl,
      type: "POST",
      data: {
        action: "mreeir_send_sms",
        mobileemail: $mobileemail,
        captcha: _captcha,
        captchahash: _captcha_hash,
        userisnotauser: $userisnotauser,
        type: $inputType,
        security: mreeir_js_options.noncesendsms,
      },
      beforeSend: function () {
        $(".mreeir-element-wrapper.mreeir-loading").removeClass("mreeir-hide");
      },
      success: function (response) {
        if ($.trim(response) == "success") {
          $(".mreeir-form-wrapper.getcode #verifycode").focus();
          mreeir_resend_code_master_sender_off();
          mreeir_resend_code_master();
          kepro_getcode_message_controller(glob_auth_way);
        }
        if ($.trim(response) == "wrong-info") {
          mreeir_notification(mreeir_messeges.MobileEmailNotValid);
        }
        if ($.trim(response) == "send-failed") {
          mreeir_notification(mreeir_messeges.sendFailed);
        }
        if ($.trim(response) == "sms-send-time-limit") {
          mreeir_notification(mreeir_messeges.SendTimeLimit);
        }
      },
      complete: function () {
        $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
      },
    });
  });

  $(".mreeir-form-wrapper.getcode a.resend-code-bale").on(
    "click",
    function (e) {
      e.preventDefault();
      var $mobile = glob_user_input;
      var $userisnotauser = $(
        ".mreeir-form-wrapper.getcode .userisnotauser"
      ).val();

      if (mreeir_is_empty($mobile)) {
        mreeir_notification(mreeir_messeges.MobileEmailNotValid);
        return;
      }

      if (mreeir_is_empty_fake($userisnotauser)) {
        mreeir_notification(mreeir_messeges.wrongInfo);
        return;
      }
      
      // captcha
      var _captcha = "";
      var _captcha_hash = "";
      var captcha_selector = $(
        ".mreeir-form-wrapper.getcode .hfield_wrapper.hfield_wrapper_captcha .hcaptcha_minifier .hcaptcha"
      );
      if (captcha_selector.length > 0) {
        var captcha_hash_selector = $(
          ".mreeir-form-wrapper.getcode .hfield_wrapper.hfield_wrapper_captcha_image"
        ).find(".woosubcaptchavalue");
        _captcha = captcha_selector.val();
        _captcha_hash = captcha_hash_selector.val();

        if (mreeir_is_empty(_captcha) || mreeir_is_empty(_captcha_hash)) {
          mreeir_notification("کد امنیتی وارد شده معتبر نیست");
          return;
        }

        if (!kerasno_check_is_number(_captcha)) {
          mreeir_notification("کد امنیتی باید عددی باشد");
          return;
        }

        if (_captcha.length !== 4) {
          mreeir_notification("کد امنیتی باید 4 رقمی باشد");
          return;
        }
      }

      glob_auth_way = "bale";
      jQuery.ajax({
        url: mreeir_js_options.ajaxurl,
        type: "POST",
        data: {
          action: "kepro_bale_set_otp",
          mobile: $mobile,
          captcha: _captcha,
          captchahash: _captcha_hash,
          userisnotauser: $userisnotauser,
          security: mreeir_js_options.noncesendsms,
        },
        beforeSend: function () {
          $(".mreeir-element-wrapper.mreeir-loading").removeClass(
            "mreeir-hide"
          );
        },
        success: function (response) {
          if (response.success) {
            $(".mreeir-form-wrapper.getcode #verifycode").focus();
            mreeir_resend_code_master_sender_off();
            mreeir_resend_code_master();
            var $bale_number = response.data.ussd;
            kepro_getcode_message_controller(glob_auth_way);
            $(".mreeir-form-wrapper.getcode #bale_wrapper #balenumber").text(
              $bale_number
            );
          } else {
            mreeir_notification(response.data.message);
            if (mreeir_js_options.showauthwaysonfail == "yes") {
              currentBTN
                .siblings(".kepro_auth_ways")
                .removeClass("kepro_hide_auth_ways");
            }
          }
        },
        complete: function () {
          $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
        },
      });
    }
  );

  $(".mreeir-form-wrapper.getcode a.resend-code-ussd").on(
    "click",
    function (e) {
      e.preventDefault();
      var $mobile = glob_user_input;
      var $userisnotauser = $(
        ".mreeir-form-wrapper.getcode .userisnotauser"
      ).val();

      if (mreeir_is_empty($mobile)) {
        mreeir_notification(mreeir_messeges.MobileEmailNotValid);
        return;
      }

      if (mreeir_is_empty_fake($userisnotauser)) {
        mreeir_notification(mreeir_messeges.wrongInfo);
        return;
      }

      // captcha
      var _captcha = "";
      var _captcha_hash = "";
      var captcha_selector = $(
        ".mreeir-form-wrapper.getcode .hfield_wrapper.hfield_wrapper_captcha .hcaptcha_minifier .hcaptcha"
      );
      if (captcha_selector.length > 0) {
        var captcha_hash_selector = $(
          ".mreeir-form-wrapper.getcode .hfield_wrapper.hfield_wrapper_captcha_image"
        ).find(".woosubcaptchavalue");
        _captcha = captcha_selector.val();
        _captcha_hash = captcha_hash_selector.val();

        if (mreeir_is_empty(_captcha) || mreeir_is_empty(_captcha_hash)) {
          mreeir_notification("کد امنیتی وارد شده معتبر نیست");
          return;
        }

        if (!kerasno_check_is_number(_captcha)) {
          mreeir_notification("کد امنیتی باید عددی باشد");
          return;
        }

        if (_captcha.length !== 4) {
          mreeir_notification("کد امنیتی باید 4 رقمی باشد");
          return;
        }
      }

      glob_auth_way = "ussd";
      jQuery.ajax({
        url: mreeir_js_options.ajaxurl,
        type: "POST",
        data: {
          action: "kepro_ussd_set_otp",
          mobile: $mobile,
          captcha: _captcha,
          captchahash: _captcha_hash,
          userisnotauser: $userisnotauser,
          security: mreeir_js_options.noncesendsms,
        },
        beforeSend: function () {
          $(".mreeir-element-wrapper.mreeir-loading").removeClass(
            "mreeir-hide"
          );
        },
        success: function (response) {
          if (response.success) {
            $(".mreeir-form-wrapper.getcode #verifycode").focus();
            mreeir_resend_code_master_sender_off();
            mreeir_resend_code_master();
            var $ussd_number = response.data.ussd;
            kepro_getcode_message_controller(glob_auth_way);
            $(".mreeir-form-wrapper.getcode #ussd_wrapper #ussdnumber").text(
              $ussd_number
            );
          } else {
            mreeir_notification(response.data.message);
            if (mreeir_js_options.showauthwaysonfail == "yes") {
              currentBTN
                .siblings(".kepro_auth_ways")
                .removeClass("kepro_hide_auth_ways");
            }
          }
        },
        complete: function () {
          $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
        },
      });
    }
  );

  $(".mreeir-form-wrapper.getcode a.resend-code-voice").on(
    "click",
    function (e) {
      e.preventDefault();
      var $mobile = glob_user_input;
      var $userisnotauser = $(
        ".mreeir-form-wrapper.getcode .userisnotauser"
      ).val();

      if (mreeir_is_empty($mobile)) {
        mreeir_notification(mreeir_messeges.MobileEmailNotValid);
        return;
      }

      if (mreeir_is_empty_fake($userisnotauser)) {
        mreeir_notification(mreeir_messeges.wrongInfo);
        return;
      }

      // captcha
      var _captcha = "";
      var _captcha_hash = "";
      var captcha_selector = $(
        ".mreeir-form-wrapper.getcode .hfield_wrapper.hfield_wrapper_captcha .hcaptcha_minifier .hcaptcha"
      );
      if (captcha_selector.length > 0) {
        var captcha_hash_selector = $(
          ".mreeir-form-wrapper.getcode .hfield_wrapper.hfield_wrapper_captcha_image"
        ).find(".woosubcaptchavalue");
        _captcha = captcha_selector.val();
        _captcha_hash = captcha_hash_selector.val();

        if (mreeir_is_empty(_captcha) || mreeir_is_empty(_captcha_hash)) {
          mreeir_notification("کد امنیتی وارد شده معتبر نیست");
          return;
        }

        if (!kerasno_check_is_number(_captcha)) {
          mreeir_notification("کد امنیتی باید عددی باشد");
          return;
        }

        if (_captcha.length !== 4) {
          mreeir_notification("کد امنیتی باید 4 رقمی باشد");
          return;
        }
      }

      glob_auth_way = "voice";
      jQuery.ajax({
        url: mreeir_js_options.ajaxurl,
        type: "POST",
        data: {
          action: "kepro_voice_set_otp",
          mobile: $mobile,
          captcha: _captcha,
          captchahash: _captcha_hash,
          userisnotauser: $userisnotauser,
          security: mreeir_js_options.noncesendsms,
        },
        beforeSend: function () {
          $(".mreeir-element-wrapper.mreeir-loading").removeClass(
            "mreeir-hide"
          );
        },
        success: function (response) {
          if (response.success) {
            $(".mreeir-form-wrapper.getcode #verifycode").focus();
            mreeir_resend_code_master_sender_off();
            mreeir_resend_code_master();
            kepro_getcode_message_controller(glob_auth_way);
          } else {
            mreeir_notification(response.data.message);
          }
        },
        complete: function () {
          $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
        },
      });
    }
  );

  //  Verify Code
  $("#verifycode").keypress(function (e) {
    var key = e.which;
    if (key == 13) {
      // the enter key code
      $("#submit-step2").click();
      return false;
    }
  });

  jQuery("#submit-step2").on("click", function (e) {
    e.preventDefault();

    var kerasno_is_popup = false;
    var verify_code_target = $(e.target);

    if (verify_code_target.parents("#mreeir-modal-full-outer").length) {
      kerasno_is_popup = true;
    }

    var verifycode = $("#verifycode").val();
    verifycode = mreeir_correct_mobile_number(verifycode);

    var $userisnotauser = $(
      ".mreeir-form-wrapper.getcode .userisnotauser"
    ).val();

    if (mreeir_is_empty_fake($userisnotauser)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    if (mreeir_is_empty(glob_user_input)) return;
    if (
      jQuery.isNumeric(verifycode) &&
      verifycode.length == mreeir_js_options.verifycodelength
    ) {
      jQuery.ajax({
        url: mreeir_js_options.ajaxurl,
        type: "POST",
        data: {
          action: "mreeir_verify_code",
          mobile: glob_user_input,
          userisnotauser: $userisnotauser,
          code: verifycode,
          security: mreeir_js_options.nonceverifycode,
        },
        beforeSend: function () {
          $(".mreeir-element-wrapper.mreeir-loading").removeClass(
            "mreeir-hide"
          );
        },
        success: function (response) {
          if ($.trim(response) == "registeration-disabled") {
            mreeir_notification(mreeir_js_options.registrationdisabled);
          }
          if ($.trim(response) == "success-register") {
            mreeir_notification_success(mreeir_messeges.successRegister);
            if (
              kerasno_is_popup &&
              mreeir_js_options.popupnoredirect == "true"
            ) {
              location.reload();
            } else {
              if (mreeir_is_empty(mreeir_js_options.registerredirectcomplete)) {
                window.location = mreeir_js_options.registerredirect;
              } else {
                window.location =
                  mreeir_js_options.registerredirectcomplete +
                  "?redirect=" +
                  mreeir_js_options.registerredirect;
              }
            }
          }
          if ($.trim(response) == "success-login") {
            mreeir_notification_success(mreeir_messeges.successLogin);
            if (
              kerasno_is_popup &&
              mreeir_js_options.popupnoredirect == "true"
            ) {
              location.reload();
            } else {
              window.location = mreeir_js_options.loginredirect;
            }
          }
          if ($.trim(response) == "wrong-code") {
            mreeir_notification(mreeir_messeges.wrongCode);
          }
          if ($.trim(response) == "wrong-mobile") {
            mreeir_notification(mreeir_messeges.wrongMobile);
          }
          if ($.trim(response) == "failed-register") {
            mreeir_notification(mreeir_messeges.errorInRegister);
          }
          if ($.trim(response) == "failed-login") {
            mreeir_notification(mreeir_messeges.errorInLogin);
          }
          if ($.trim(response) == "blocked-mobile") {
            mreeir_notification(mreeir_js_options.mobileisblocked);
          }
        },
        complete: function () {
          $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
        },
      });
    } else {
    }
  });

  //  Verify Password
  $("#verifypassword").keypress(function (e) {
    var key = e.which;
    if (key == 13) {
      // the enter key code
      $("#submit-step3").click();
      return false;
    }
  });

  jQuery("#submit-step3").on("click", function (e) {
    e.preventDefault();
    var verifypassword = $("#verifypassword").val();
    verifypassword = mreeir_correct_mobile_number(verifypassword);
    var $userisnotauser = $(
      ".mreeir-form-wrapper.getpassword .userisnotauser"
    ).val();

    if (mreeir_is_empty_fake($userisnotauser)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    if (mreeir_is_empty(glob_user_input)) return;
    if (!mreeir_is_empty(verifypassword)) {
      jQuery.ajax({
        url: mreeir_js_options.ajaxurl,
        type: "POST",
        data: {
          action: "mreeir_verify_password",
          mobile: glob_user_input,
          userisnotauser: $userisnotauser,
          password: verifypassword,
          security: mreeir_js_options.nonceverifypassword,
        },
        beforeSend: function () {
          $(".mreeir-element-wrapper.mreeir-loading").removeClass(
            "mreeir-hide"
          );
        },
        success: function (response) {
          if ($.trim(response) == "success-login") {
            window.location = mreeir_js_options.loginredirect;
          }
          if ($.trim(response) == "wrong-password") {
            mreeir_notification(mreeir_messeges.wrongPassword);
          }
          if ($.trim(response) == "wrong-mobile") {
            mreeir_notification(mreeir_messeges.wrongMobile);
          }
          if ($.trim(response) == "user-not-exists") {
            mreeir_notification(mreeir_messeges.wronguser);
          }
          if ($.trim(response) == "wrong-info") {
            mreeir_notification(mreeir_messeges.wrongInfo);
          }
        },
        complete: function () {
          $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
        },
      });
    } else {
    }
  });

  //  Get Password For Email
  $("#emailverifypassword").keypress(function (e) {
    var key = e.which;
    if (key == 13) {
      // the enter key code
      $("#submit-step4").click();
      return false;
    }
  });

  jQuery("#submit-step4").on("click", function (e) {
    e.preventDefault();
    var emailverifypassword = $("#emailverifypassword").val();
    emailverifypassword = mreeir_correct_mobile_number(emailverifypassword);
    var $userisnotauser = $(
      ".mreeir-form-wrapper.emailgetpassword .userisnotauser"
    ).val();

    if (mreeir_is_empty(glob_user_input)) return;

    if (mreeir_is_empty_fake($userisnotauser)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    if (!mreeir_is_empty(emailverifypassword)) {
      jQuery.ajax({
        url: mreeir_js_options.ajaxurl,
        type: "POST",
        data: {
          action: "mreeir_login_with_email_password",
          email: glob_user_input,
          userisnotauser: $userisnotauser,
          password: emailverifypassword,
          security: mreeir_js_options.nonceemailpassword,
        },
        beforeSend: function () {
          $(".mreeir-element-wrapper.mreeir-loading").removeClass(
            "mreeir-hide"
          );
        },
        success: function (response) {
          if ($.trim(response) == "success-login") {
            window.location = mreeir_js_options.loginredirect;
          }
          if ($.trim(response) == "wrong-info") {
            mreeir_notification(mreeir_messeges.wrongInfo);
          }
          if ($.trim(response) == "wrong-email") {
            mreeir_notification(mreeir_messeges.wrongEmail);
          }
        },
        complete: function () {
          $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
        },
      });
    } else {
    }
  });

  //  Get Reset Code Or recovery Email
  $("#getresetcode").keypress(function (e) {
    var key = e.which;
    if (key == 13) {
      // the enter key code
      $("#submit-step5").click();
      return false;
    }
  });

  $("#submit-step5").on("click", function (e) {
    e.preventDefault();
    var $inputType = "";
    var $mobileemail = $("#getresetcode").val();
    var $userisnotauser = $(
      ".mreeir-form-wrapper.resetpassword .userisnotauser"
    ).val();

    if (mreeir_is_empty($mobileemail)) {
      mreeir_notification(mreeir_messeges.MobileEmailNotValid);
      return;
    }

    if (mreeir_is_empty_fake($userisnotauser)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    $mobileemail = mreeir_correct_mobile_number($mobileemail);

    if (!mreeir_is_mobile($mobileemail) && !mreeir_is_email($mobileemail)) {
      mreeir_notification(mreeir_messeges.MobileEmailNotValid);
      return;
    } else {
      if (mreeir_is_email($mobileemail)) {
        $inputType = "email";
      }
      if (mreeir_is_mobile($mobileemail)) {
        $inputType = "mobile";
      }
    }

    var _captcha = '';
    var _captcha_hash = '';
    // captcha
    var captcha_selector = $('.mreeir-form-wrapper.resetpassword .hfield_wrapper.hfield_wrapper_captcha .hcaptcha_minifier .hcaptcha');
    if ( captcha_selector.length > 0 ) {
      var captcha_hash_selector = $('.mreeir-form-wrapper.resetpassword .hfield_wrapper.hfield_wrapper_captcha_image').find('.woosubcaptchavalue');
      _captcha = captcha_selector.val();            
      _captcha_hash = captcha_hash_selector.val();
            
      if ( mreeir_is_empty(_captcha) ||  mreeir_is_empty(_captcha_hash) ) {
        mreeir_notification('کد امنیتی وارد شده معتبر نیست');
        return;
      }

      
      
      if ( ! kerasno_check_is_number(_captcha)) {
        mreeir_notification('کد امنیتی باید عددی باشد');
        return;
      }

      if ( _captcha.length !== 4) {
        mreeir_notification('کد امنیتی باید 4 رقمی باشد');
        return;
      }
    }

    glob_user_input = $mobileemail;
    glob_input_type = $inputType;
    if ($inputType == "mobile") {
      jQuery.ajax({
        url: mreeir_js_options.ajaxurl,
        type: "POST",
        data: {
          action: "mreeir_send_sms",
          mobileemail: $mobileemail,
          captcha: _captcha,
          captchahash: _captcha_hash,
          userisnotauser: $userisnotauser,
          type: $inputType,
          security: mreeir_js_options.noncesendsms,
        },
        beforeSend: function () {
          $(".mreeir-element-wrapper.mreeir-loading").removeClass(
            "mreeir-hide"
          );
        },
        success: function (response) {
          if ($.trim(response) == "success") {
            mreeir_form_controller($(".mreeir-form-wrapper.getcodereset"));
            $(".mreeir-form-wrapper.getcodereset #verifycode").focus();
            mreeir_resend_code_second();
          }
          if ($.trim(response) == "wrong-captcha") {
            if ($(".woosub_captcha_refresh").length > 0) {
              $(".woosub_captcha_refresh").trigger("click");
            }
            mreeir_notification(mreeir_messeges.CaptchaNotValid);
          }
          if ($.trim(response) == "wrong-info") {
            mreeir_notification(mreeir_messeges.MobileEmailNotValid);
          }
          if ($.trim(response) == "send-failed") {
            mreeir_notification(mreeir_messeges.sendFailed);
          }
          if ($.trim(response) == "sms-send-time-limit") {
            mreeir_notification(mreeir_messeges.SendTimeLimit);
          }
          if ($.trim(response) == "blocked-mobile") {
            mreeir_notification(mreeir_js_options.mobileisblocked);
          }
        },
        complete: function () {
          $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
        },
      });
    }

    if ($inputType == "email") {
      jQuery.ajax({
        url: mreeir_js_options.ajaxurl,
        type: "POST",
        data: {
          action: "mreeir_send_reset_link_email",
          mobileemail: $mobileemail,
          userisnotauser: $userisnotauser,
          type: $inputType,
          security: mreeir_js_options.noncesendresetlink,
        },
        beforeSend: function () {
          $(".mreeir-element-wrapper.mreeir-loading").removeClass(
            "mreeir-hide"
          );
        },
        success: function (response) {
          if ($.trim(response) == "success") {
            mreeir_form_controller($(".mreeir-form-wrapper.emailresetinfo"));
          }
          if ($.trim(response) == "wrong-email") {
            mreeir_notification(mreeir_messeges.wrongEmail);
          }
          if ($.trim(response) == "wrong-saving") {
            mreeir_notification(mreeir_messeges.wrongSaving);
          }
          if ($.trim(response) == "send-email-failed") {
            mreeir_notification(mreeir_messeges.sendEmailFailed);
          }
        },
        complete: function () {
          $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
        },
      });
    }
  });

  $(".mreeir-form-wrapper.getcodereset a.resend-code").on(
    "click",
    function (e) {
      e.preventDefault();
      var $inputType = "";
      var $mobileemail = glob_user_input;
      var $userisnotauser = $(
        ".mreeir-form-wrapper.getcodereset .userisnotauser"
      ).val();

      if (mreeir_is_empty($mobileemail)) {
        mreeir_notification(mreeir_messeges.MobileEmailNotValid);
        return;
      }

      if (mreeir_is_empty_fake($userisnotauser)) {
        mreeir_notification(mreeir_messeges.wrongInfo);
        return;
      }
      
      // captcha
      var _captcha = "";
      var _captcha_hash = "";
      var captcha_selector = $(
        ".mreeir-form-wrapper.getcodereset .hfield_wrapper.hfield_wrapper_captcha .hcaptcha_minifier .hcaptcha"
      );
      if (captcha_selector.length > 0) {
        var captcha_hash_selector = $(
          ".mreeir-form-wrapper.getcodereset .hfield_wrapper.hfield_wrapper_captcha_image"
        ).find(".woosubcaptchavalue");
        _captcha = captcha_selector.val();
        _captcha_hash = captcha_hash_selector.val();

        if (mreeir_is_empty(_captcha) || mreeir_is_empty(_captcha_hash)) {
          mreeir_notification("کد امنیتی وارد شده معتبر نیست");
          return;
        }

        if (!kerasno_check_is_number(_captcha)) {
          mreeir_notification("کد امنیتی باید عددی باشد");
          return;
        }

        if (_captcha.length !== 4) {
          mreeir_notification("کد امنیتی باید 4 رقمی باشد");
          return;
        }
      }

      $inputType = "mobile";

      jQuery.ajax({
        url: mreeir_js_options.ajaxurl,
        type: "POST",
        data: {
          action: "mreeir_send_sms",
          mobileemail: $mobileemail,
          captcha: _captcha,
          captchahash: _captcha_hash,
          userisnotauser: $userisnotauser,
          type: $inputType,
          security: mreeir_js_options.noncesendsms,
        },
        beforeSend: function () {
          $(".mreeir-element-wrapper.mreeir-loading").removeClass(
            "mreeir-hide"
          );
        },
        success: function (response) {
          if ($.trim(response) == "success") {
            $(".mreeir-form-wrapper.getcodereset #verifycodereset").focus();
            mreeir_resend_code_second_sender_off();
            mreeir_resend_code_second();
          }
          if ($.trim(response) == "wrong-captcha") {
            if ($(".woosub_captcha_refresh").length > 0) {
              $(".woosub_captcha_refresh").trigger("click");
            }
            mreeir_notification(mreeir_messeges.CaptchaNotValid);
          }
          if ($.trim(response) == "wrong-info") {
            mreeir_notification(mreeir_messeges.MobileEmailNotValid);
          }
          if ($.trim(response) == "send-failed") {
            mreeir_notification(mreeir_messeges.sendFailed);
          }
          if ($.trim(response) == "sms-send-time-limit") {
            mreeir_notification(mreeir_messeges.SendTimeLimit);
          }
        },
        complete: function () {
          $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
        },
      });
    }
  );

  //  Verify Code Reset
  $("#verifycodereset").keypress(function (e) {
    var key = e.which;
    if (key == 13) {
      // the enter key code
      $("#submit-step6").click();
      return false;
    }
  });

  jQuery("#submit-step6").on("click", function (e) {
    e.preventDefault();
    var verifycodereset = $("#verifycodereset").val();
    verifycodereset = mreeir_correct_mobile_number(verifycodereset);
    var $userisnotauser = $(
      ".mreeir-form-wrapper.getcodereset .userisnotauser"
    ).val();

    if (mreeir_is_empty(glob_user_input)) return;

    if (mreeir_is_empty_fake($userisnotauser)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    if (
      jQuery.isNumeric(verifycodereset) &&
      verifycodereset.length == mreeir_js_options.verifycodelength
    ) {
      jQuery.ajax({
        url: mreeir_js_options.ajaxurl,
        type: "POST",
        data: {
          action: "mreeir_verify_code_reset",
          mobile: glob_user_input,
          userisnotauser: $userisnotauser,
          code: verifycodereset,
          security: mreeir_js_options.nonceverifycodereset,
        },
        beforeSend: function () {
          $(".mreeir-element-wrapper.mreeir-loading").removeClass(
            "mreeir-hide"
          );
        },
        success: function (response) {
          if (response.length == 32) {
            mobile_reset_hash = response;
            mreeir_form_controller($(".mreeir-form-wrapper.setpassword"));
            $(".mreeir-form-wrapper.setpassword #userpassword").focus();
          }
          if ($.trim(response) == "faild") {
            mreeir_notification(mreeir_messeges.pleaseRegisterFirst);
          }
          if ($.trim(response) == "wrong-code") {
            mreeir_notification(mreeir_messeges.wrongCode);
          }
          if ($.trim(response) == "wrong-mobile") {
            mreeir_notification(mreeir_messeges.wrongMobile);
          }
          if ($.trim(response) == "blocked-mobile") {
            mreeir_notification(mreeir_js_options.mobileisblocked);
          }
        },
        complete: function () {
          $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
        },
      });
    } else {
    }
  });

  //  Set New Password Fom
  $("#userpassword").keypress(function (e) {
    var key = e.which;
    if (key == 13 && $(this).val()) {
      // the enter key code
      $("#userpasswordconfirm").focus();
      return false;
    }
  });

  $("#userpasswordconfirm").keypress(function (e) {
    var key = e.which;
    if (key == 13 && $(this).val() && $("#userpassword").val()) {
      // the enter key code
      $("#submit-step8").click();
      return false;
    }
  });

  //  Redirect To Home Page
  $("#submit-step7").on("click", function (e) {
    e.preventDefault();
    window.location = mreeir_js_options.homepageurl;
    return false;
  });

  //  Set New Password By Email Link
  $("#submit-step8").on("click", function (e) {
    e.preventDefault();
    var $password = $("#userpassword").val();
    $password = mreeir_correct_mobile_number($password);

    var $password_confirm = $("#userpasswordconfirm").val();
    $password_confirm = mreeir_correct_mobile_number($password_confirm);

    var $userisnotauser = $(
      ".mreeir-form-wrapper.setpassword .userisnotauser"
    ).val();

    if (mreeir_is_empty_fake($userisnotauser)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    if (mreeir_is_empty($password) || mreeir_is_empty($password_confirm)) {
      mreeir_notification(mreeir_messeges.emptyPassword);
      return;
    }

    if ($password != $password_confirm) {
      mreeir_notification(mreeir_messeges.mismatchPassword);
      return;
    }

    if (mreeir_is_empty(mreeir_js_options.emailforreset)) {
      mreeir_notification(mreeir_messeges.mismatchPassword);
      return;
    }

    if (mreeir_js_options.strongpassword == "true") {
      if (!mreeir_is_password_ok($password)) {
        mreeir_notification(mreeir_messeges.passwordNotStrong);
        return;
      }
    }

    jQuery.ajax({
      url: mreeir_js_options.ajaxurl,
      type: "POST",
      data: {
        action: "mreeir_set_new_password_email",
        user: mreeir_js_options.userforreset,
        email: mreeir_js_options.emailforreset,
        userisnotauser: $userisnotauser,
        password: $password,
        passwordconfirm: $password_confirm,
        security: mreeir_js_options.noncesetnewpassword,
      },
      beforeSend: function () {
        $(".mreeir-element-wrapper.mreeir-loading").removeClass("mreeir-hide");
      },
      success: function (response) {
        if ($.trim(response) == "success") {
          mreeir_form_controller(
            $(".mreeir-form-wrapper.successresetpassinfo")
          );
        }
        if ($.trim(response) == "wrong-info") {
          mreeir_notification(mreeir_messeges.wrongInfo);
        }
        if ($.trim(response) == "passwords-mismatch") {
          mreeir_notification(mreeir_messeges.mismatchPassword);
        }
        if ($.trim(response) == "password-is-not-strong") {
          mreeir_notification(mreeir_messeges.passwordNotStrong);
        }
      },
      complete: function () {
        $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
      },
    });
  });

  //  Redirect To Login Page After Success Set Password
  $("#submit-step9").on("click", function (e) {
    e.preventDefault();
    window.location = mreeir_js_options.loginpageurl;
    return false;
  });

  //  Set New Password By Mobile
  $("#submit-step10").on("click", function (e) {
    e.preventDefault();
    var $password = $("#userpassword").val();
    $password = mreeir_correct_mobile_number($password);

    var $password_confirm = $("#userpasswordconfirm").val();
    $password_confirm = mreeir_correct_mobile_number($password_confirm);

    var $userisnotauser = $(
      ".mreeir-form-wrapper.setpassword .userisnotauser"
    ).val();

    if (mobile_reset_hash.length != 32) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    if (mreeir_is_empty_fake($userisnotauser)) {
      mreeir_notification(mreeir_messeges.wrongInfo);
      return;
    }

    if (mreeir_is_empty($password) || mreeir_is_empty($password_confirm)) {
      mreeir_notification(mreeir_messeges.emptyPassword);
      return;
    }

    if ($password != $password_confirm) {
      mreeir_notification(mreeir_messeges.mismatchPassword);
      return;
    }

    if (mreeir_js_options.strongpassword == "true") {
      if (!mreeir_is_password_ok($password)) {
        mreeir_notification(mreeir_messeges.passwordNotStrong);
        return;
      }
    }

    jQuery.ajax({
      url: mreeir_js_options.ajaxurl,
      type: "POST",
      data: {
        action: "mreeir_set_new_password_mobile",
        hashedmobile: mobile_reset_hash,
        userisnotauser: $userisnotauser,
        password: $password,
        passwordconfirm: $password_confirm,
        security: mreeir_js_options.noncesetnewpasswordmobile,
      },
      beforeSend: function () {
        $(".mreeir-element-wrapper.mreeir-loading").removeClass("mreeir-hide");
      },
      success: function (response) {
        if ($.trim(response) == "success") {
          mreeir_form_controller(
            $(".mreeir-form-wrapper.successresetpassinfo")
          );
        }
        if ($.trim(response) == "wrong-info") {
          mreeir_notification(mreeir_messeges.wrongInfo);
        }
        if ($.trim(response) == "password-is-not-strong") {
          mreeir_notification(mreeir_messeges.passwordNotStrong);
        }
      },
      complete: function () {
        $(".mreeir-element-wrapper.mreeir-loading").addClass("mreeir-hide");
      },
    });
  });
  //login/register/reset password form - end

  // functions and variables - start

  function kepro_getcode_message_controller($itemtoshow) {
    $(
      ".mreeir-form-wrapper.getcode .mreeir-logreg-form.mreeir-master-width .mreeir-element-wrapper.mreeir-form-message div"
    ).hide();
    $(
      ".mreeir-form-wrapper.getcode .mreeir-logreg-form.mreeir-master-width .mreeir-element-wrapper.mreeir-form-message #" +
        $itemtoshow +
        "_wrapper"
    ).show();
  }

  var mreeir_notification_success = function (message) {
    $.toast({
      text: message,
      showHideTransition: mreeir_js_options.toast_transition,
      textAlign: "center",
      stack: false,
      position: mreeir_js_options.toast_position,
      bgColor: "green",
      textColor: "#fff",
      loaderBg: "#003d00",
      class: "mreeir-error-toast",
      hideAfter: 2000,
      allowToastClose: mreeir_js_options.toast_close,
    });
  };

  var mreeir_notification = function (message) {
    $.toast({
      text: message,
      showHideTransition: mreeir_js_options.toast_transition,
      textAlign: "center",
      stack: false,
      position: mreeir_js_options.toast_position,
      bgColor: mreeir_js_options.toast_bgcolor,
      textColor: mreeir_js_options.toast_color, //'#721c24',
      loaderBg: mreeir_js_options.toast_progress_color,
      class: "mreeir-error-toast",
      hideAfter: mreeir_js_options.toast_time,
      allowToastClose: mreeir_js_options.toast_close,
    });
  };
  var mreeir_is_email = function (email) {
    var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    return regex.test(email);
  };

  var mreeir_is_mobile = function (mobileNumber) {
    var regex = /^09\d{9}$/;
    return regex.test(mobileNumber);
  };

  // check empty function
  var mreeir_is_empty_fake = function (variable) {
    return variable !== undefined && variable !== null && variable !== "";
  };

  //tabbed form back buttons
  $tab_login_opt_back = $(
    ".mreeir-form-wrapper.logintab-otp-getcode .mreeir-logreg-form .mreeir-element-wrapper.mreeir-back"
  );
  $tab_login_opt_back.on("click", function (e) {
    e.preventDefault();
    mreeir_form_controller($(".mreeir-form-wrapper.getmobileemail"));
    mreeir_resend_code_master_sender_off();
  });

  $tab_register_opt_back = $(
    ".mreeir-form-wrapper.getcode-tab-register .mreeir-logreg-form .mreeir-element-wrapper.mreeir-back"
  );
  $tab_register_opt_back.on("click", function (e) {
    e.preventDefault();
    mreeir_form_controller($(".mreeir-form-wrapper.registermobileemail"));
    mreeir_resend_code_master_sender_off();
  });

  $back_step2to1 = $(
    ".mreeir-form-wrapper.getcode .mreeir-logreg-form .mreeir-element-wrapper.mreeir-back"
  );
  $back_step3to2 = $(
    ".mreeir-form-wrapper.getpassword .mreeir-logreg-form .mreeir-element-wrapper.mreeir-back"
  );
  $back_step4to3or6 = $(
    ".mreeir-form-wrapper.resetpassword .mreeir-logreg-form .mreeir-element-wrapper.mreeir-back"
  );
  $back_step5to4 = $(
    ".mreeir-form-wrapper.getcodereset .mreeir-logreg-form .mreeir-element-wrapper.mreeir-back"
  );
  $back_step6to1 = $(
    ".mreeir-form-wrapper.emailgetpassword .mreeir-logreg-form .mreeir-element-wrapper.mreeir-back"
  );

  $back_step2to1.on("click", function (e) {
    e.preventDefault();
    mreeir_form_controller($(".mreeir-form-wrapper.getmobileemail"));
    mreeir_resend_code_master_sender_off();
  });

  $back_step3to2.on("click", function (e) {
    e.preventDefault();
    mreeir_form_controller($(".mreeir-form-wrapper.getcode"));
  });

  $back_step4to3or6.on("click", function (e) {
    e.preventDefault();
    if (reset_pass_caller == "email") {
      mreeir_form_controller($(".mreeir-form-wrapper.emailgetpassword"));
    } else {
      mreeir_form_controller($(".mreeir-form-wrapper.getpassword"));
    }
  });

  $back_step5to4.on("click", function (e) {
    e.preventDefault();
    mreeir_form_controller($(".mreeir-form-wrapper.resetpassword"));
    mreeir_resend_code_second_sender_off();
  });

  $back_step6to1.on("click", function (e) {
    e.preventDefault();
    mreeir_form_controller($(".mreeir-form-wrapper.getmobileemail"));
  });

  $("#enter-with-password").on("click", function (e) {
    e.preventDefault();
    mreeir_form_controller($(".mreeir-form-wrapper.getpassword"));
  });

  $("#enter-with-code").on("click", function (e) {
    e.preventDefault();
    mreeir_form_controller($(".mreeir-form-wrapper.getcode"));
  });

  $(".mreeir-form-wrapper.emailgetpassword .goto-reset-password").on(
    "click",
    function (e) {
      e.preventDefault();
      reset_pass_caller = "email";
      mreeir_form_controller($(".mreeir-form-wrapper.resetpassword"));
      if (glob_user_input) {
        $(".mreeir-form-wrapper.resetpassword #getresetcode").val(
          glob_user_input
        );
      }
      $(".mreeir-form-wrapper.resetpassword #getresetcode").focus();
    }
  );

  $(".mreeir-form-wrapper.getpassword .goto-reset-password").on(
    "click",
    function (e) {
      e.preventDefault();
      reset_pass_caller = "mobile";
      mreeir_form_controller($(".mreeir-form-wrapper.resetpassword"));
      if (glob_user_input) {
        $(".mreeir-form-wrapper.resetpassword #getresetcode").val(
          glob_user_input
        );
      }
    }
  );

  function mreeir_form_controller(destination_form_obj) {
    destination_form_obj
      .siblings(".mreeir-form-wrapper")
      .removeClass("mreeir-force-show");
    destination_form_obj
      .siblings(".mreeir-form-wrapper")
      .addClass("mreeir-force-hide");

    destination_form_obj.removeClass("mreeir-force-hide");
    destination_form_obj.addClass("mreeir-force-show");
  }

  function mreeir_correct_mobile_number($mobile) {
    var inputs = "" + $mobile;
    new_val = inputs
      .replace(/۰/g, "0")
      .replace(/۱/g, "1")
      .replace(/۲/g, "2")
      .replace(/۳/g, "3")
      .replace(/۴/g, "4")
      .replace(/۵/g, "5")
      .replace(/۶/g, "6")
      .replace(/۷/g, "7")
      .replace(/۸/g, "8")
      .replace(/۹/g, "9")
      .replace(/٤/g, "4")
      .replace(/٥/g, "5")
      .replace(/٦/g, "6");
    new_val = new_val
      .replace(/٠/g, "0")
      .replace(/١/g, "1")
      .replace(/٢/g, "2")
      .replace(/٣/g, "3")
      .replace(/٤/g, "4")
      .replace(/۵/g, "5")
      .replace(/٦/g, "6")
      .replace(/٧/g, "7")
      .replace(/٨/g, "8")
      .replace(/٩/g, "9")
      .replace(/٤/g, "4")
      .replace(/٥/g, "5")
      .replace(/٦/g, "6");
    return new_val;
  }

  // check empty function
  function mreeir_is_empty(data) {
    if (typeof data === "object") {
      if (JSON.stringify(data) === "{}" || JSON.stringify(data) === "[]") {
        return true;
      } else if (!data) {
        return true;
      }
      return false;
    } else if (typeof data === "string") {
      if (!data.trim()) {
        return true;
      }
      return false;
    } else if (typeof data === "undefined") {
      return true;
    } else {
      return false;
    }
  }

  function mreeir_get_url_parameter(sParam) {
    var sPageURL = window.location.search.substring(1),
      sURLVariables = sPageURL.split("&"),
      sParameterName,
      i;

    for (i = 0; i < sURLVariables.length; i++) {
      sParameterName = sURLVariables[i].split("=");

      if (sParameterName[0] === sParam) {
        return sParameterName[1] === undefined
          ? true
          : decodeURIComponent(sParameterName[1]);
      }
    }
    return false;
  }
  function kerasno_check_is_number(input) {
    if (!/^\d+$/.test(input)) {
      return false;
    }
    return true;
  }
  function mreeir_is_password_ok($password) {
    $is_ok = false;

    $is_number_found = $password.match(/[0-9]/);
    $is_lowercase_found = $password.match(/[a-z]/);
    $is_uppercase_found = $password.match(/[A-Z]/);
    $is_symbol_found = $password.match(/[^a-zA-Z0-9]/);

    if ($password.length < 8) {
      $is_ok = false;
    } else if (!$is_number_found) {
      $is_ok = false;
    } else if (!$is_lowercase_found) {
      $is_ok = false;
    } else if (!$is_uppercase_found) {
      $is_ok = false;
    } else if (!$is_symbol_found) {
      $is_ok = false;
    } else {
      // Password is OK.
      $is_ok = true;
    }

    return $is_ok;
  }
  var mreeir_messeges = {
    bale_mobile_not_valid:
      "برای احراز هویت از طریق پیام رسان بله یک شماره موبایل معتبر وارد کنید",
    ussd_mobile_not_valid:
      "برای احراز هویت از طریق کد دستوری یک شماره موبایل معتبر وارد کنید",
    voice_mobile_not_valid:
      "برای احراز هویت از طریق تماس یک شماره موبایل معتبر وارد کنید",
    PasswordIsRequired: "کلمه عبور الزامی است",
    MobileEmailNotValid: "اطلاعات وارد شده معتبر نیست",
    CaptchaNotValid: "کد امنیتی وارد شده معتبر نیست",
    wrongCaptcha: "کد امنیتی صحیح نیست",
    sendFailed: "خطا در ارسال پیامک",
    sendEmailFailed: "خطا در ارسال ایمیل",
    wrongCode: "کد تایید اشتباه است",
    wrongPassword: "رمز وارد شده اشتباه است",
    wronguser: "شماره موبایل ثبت نشده است. با رمز یکبار مصرف وارد شوید",
    wrongMobile: "شماره موبایل معتبر نیست",
    wrongEmail: "آدرس ایمیل معتبر نیست",
    wrongInfo: "اطلاعات وارد شده معتبر نیست",
    wrongSaving: "خطا در فرآیند بازیابی رمز عبور",
    SendTimeLimit: "لطفاً برای ارسال مجدد کد تایید لحظاتی صبر کنید",
    errorInLogin: "خطایی در فرآیند ورود رخ داده است",
    errorInRegister: "خطایی در فرآیند ثبت نام رخ داده است",

    emptyPassword: "لطفاً همه اطلاعات را وارد کنید",
    mismatchPassword: "رمز عبور و تکرار رمز عبور یکسان نیستند",
    passwordNotStrong: "رمز عبور وارد شده ضعیف است",
    pleaseRegisterFirst: "لطفاً ابتدا در سایت ثبت نام کنید",

    downloadRegister: "عملیات ثبت نام  برای دانلود فایل با موفقیت انجام شد",
    downloadLogin: "عملیات ثبت نام و ورود برای دانلود فایل با موفقیت انجام شد",
    downloadDownload: "عملیات دانلود با موفقیت انجام شد",
    downloadusername: "فیلد نام الزامی می باشد",
    downloaduserfamily: "فیلد نام خانوادگی الزامی می باشد",

    successLogin: "شما با موفقیت وارد سایت شدید",
    successRegister: "ثبت نام شما با موفقیت انجام شد",

    // regiter with password
    nameRequired: "لطفاً نام خود را وارد کنید",
    familyRequired: "لطفاً نام خانوادگی خود را وارد کنید",
    passRequired: "فیلد رمز عبور الزامی می باشد",
    passConfirmRequired: "فیلد تکرار رمز عبور الزامی می باشد",
    passNotEqual: "رمز عبور و تکرار رمز عبور یکسان نیستند",
    repeatedMobile: "شماره موبایل تکراری می باشد",
    repeatedEmail: "ایمیل تکراری می باشد",
    repeatedUsername: "نام کاربری تکراری می باشد",
    mobileNotExists: "این شماره موبایل در سایت وجود ندارد",
    emailNotExists: "این ایمیل در سایت وجود ندارد",
    sendEmailFailed: "خطایی در ارسال ایمیل رخ داد",
    firstverify: 'لطفا با استفاده از دکمه "ارسال کد" احراز هویت را انجام دهید',
  };

  var mreeir_is_mobile_bale = function (mobileNumber) {
    // حذف فاصله‌ها یا کاراکترهای غیرعددی (اختیاری)
    mobileNumber = mobileNumber.replace(/\D/g, "");

    var regex11 = /^09\d{9}$/; // 11 رقمی با 09
    var regex12 = /^[1-9]\d{11}$/; // 12 رقمی که با صفر شروع نمی‌شود

    return regex11.test(mobileNumber) || regex12.test(mobileNumber);
  };

  // captcha handler
  $('.woosub_captcha_refresh').on('click', function(){
      woosub_captcha_generator($(this));
  });

  if ($('.woosub_captcha_refresh').length > 0 ) {
      $('.woosub_captcha_refresh').each(function(){	
          woosub_captcha_generator($(this));
      });
  }

  function woosub_captcha_generator(element) {
      $.ajax({
          url:  mreeir_js_options.ajaxurl,
          type: 'post',
          dataType: 'json',
          data: {
              action: 'woosub_generate_captcha',
          },
          success: function(response) {
              var base64Image = response.image;
              var captchaHash = response.hash;

              element.siblings('.woosubcaptchaimage').attr('src', 'data:image/png;base64,' + base64Image);
              element.siblings('.woosubcaptchavalue').val(captchaHash);
          },
          error: function(jqXHR, textStatus, errorThrown) {
              // Handle the error if the AJAX request fails
              console.error('AJAX Error: ' + textStatus, errorThrown);
          }
      });
  }

  // functions and variables - end
});
