function initialize() {
  geocoder = new google.maps.Geocoder();
}

function getCountryStates(countryId, stateId, dv) {
  //alert(fcom.makeUrl('DriverRegistration', 'getStates', [countryId, stateId]));
  fcom.ajax(
    fcom.makeUrl("DriverRegistration", "getStates", [countryId, stateId]),
    "",
    function (res) {
      //alert(res);
      $(dv).empty();
      $(dv).append(res);
    }
  );
}
function getModelsMake(makeId, modelId, dv) {
  fcom.ajax(
    fcom.makeUrl("DriverRegistration", "getCarModels", [makeId, modelId]),
    "",
    function (res) {
      //alert(res); alert(dv);
      $(dv).empty();
      $(dv).append(res);
    }
  );
}
function resendVerificationLink(user) {
  if (user == "") {
    return false;
  }
  $(document).trigger("closeMsg.systemMessage");
  $.mbsmessage(langLbl.processing, false, "alert--process alert");
  fcom.updateWithAjax(
    fcom.makeUrl("DriverRegistration", "resendVerification", [user]),
    "",
    function (ans) {
      $.mbsmessage(ans.msg, false, "alert alert--success");
    }
  );
}

function getCardType(number) {
  // visa
  var re = new RegExp("^4");
  if (number.match(re) != null) return "Visa";

  // Mastercard
  re = new RegExp("^5[1-5]");
  if (number.match(re) != null) return "Mastercard";

  // AMEX
  re = new RegExp("^3[47]");
  if (number.match(re) != null) return "AMEX";

  // Discover
  re = new RegExp(
    "^(6011|622(12[6-9]|1[3-9][0-9]|[2-8][0-9]{2}|9[0-1][0-9]|92[0-5]|64[4-9])|65)"
  );
  if (number.match(re) != null) return "Discover";

  // Diners
  re = new RegExp("^36");
  if (number.match(re) != null) return "Diners";

  // Diners - Carte Blanche
  re = new RegExp("^30[0-5]");
  if (number.match(re) != null) return "Diners - Carte Blanche";

  // JCB
  re = new RegExp("^35(2[89]|[3-8][0-9])");
  if (number.match(re) != null) return "JCB";

  // Visa Electron
  re = new RegExp("^(4026|417500|4508|4844|491(3|7))");
  if (number.match(re) != null) return "Visa Electron";

  return "";
}

(function ($) {
  $.extend(fcom, {
    getLoader: function () {
      return '<div class="loader-yk"><div class="loader-yk-inner"></div></div>';
    },

    scrollToTop: function (obj) {
      if (typeof obj == undefined || obj == null) {
        $("html, body").animate(
          { scrollTop: $("html, body").offset().top - 100 },
          "slow"
        );
      } else {
        $("html, body").animate(
          { scrollTop: $(obj).offset().top - 100 },
          "slow"
        );
      }
    },
    resetEditorInstance: function () {
      if (extendEditorJs == true) {
        var editors = oUtil.arrEditor;
        for (x in editors) {
          eval("delete window." + editors[x]);
        }
        oUtil.arrEditor = [];
      }
    },
    setEditorLayout: function (lang_id) {
      if (extendEditorJs == true) {
        var editors = oUtil.arrEditor;
        layout = langLbl["language" + lang_id];
        for (x in editors) {
          $("#idContent" + editors[x])
            .contents()
            .find("body")
            .css("direction", layout);
        }
      }
    },
  });

  $(document).bind("loading.facebox", function () {
    $("#facebox .content").addClass("fbminwidth");
  });

  $(document).bind("afterClose.facebox", fcom.resetEditorInstance);
  $(document).bind("beforeReveal.facebox", function () {
    $("#facebox .content").addClass("fbminwidth");
  });

  $(document).bind("reveal.facebox", function () {
    $("#facebox .content").addClass("fbminwidth");
  });

  $.systemMessage = function (data, cls) {
    initialize();
    $.systemMessage.loading();
    $.systemMessage.fillSysMessage(data, cls);
  };

  $.extend($.systemMessage, {
    settings: {
      closeimage: siteConstants.webroot + "images/facebox/close.gif",
    },
    loading: function () {
      $(".system_message").show();
    },
    fillSysMessage: function (data, cls) {
      if (cls) $(".system_message").addClass(cls);
      $(".system_message .content").html(data);
      $(".system_message").fadeIn();

      if (CONF_AUTO_CLOSE_SYSTEM_MESSAGES == 1) {
        var time = CONF_TIME_AUTO_CLOSE_SYSTEM_MESSAGES * 1000;
        setTimeout(function () {
          $.systemMessage.close();
        }, time);
      }

      /* $('.system_message').css({top:10}); */
    },
    close: function () {
      $(document).trigger("closeMsg.systemMessage");
    },
  });

  $(document).bind("closeMsg.systemMessage", function () {
    $(".system_message").fadeOut();
  });

  function initialize() {
    $(".system_message .closeMsg").click($.systemMessage.close);
  }
  /* [ */
  $.fn.serialize_without_blank = function () {
    var $form = this,
      result,
      $disabled = $([]);

    $form.find(":input").each(function () {
      var $this = $(this);
      if ($.trim($this.val()) === "" && !$this.is(":disabled")) {
        $disabled.add($this);
        $this.attr("disabled", true);
      }
    });

    result = $form.serialize();
    $disabled.removeAttr("disabled");
    return result;
  };
  /* ] */
})(jQuery);

function isUserLogged() {
  var isUserLogged = 0;
  $.ajax({
    url: fcom.makeUrl("DriverRegistration", "checkAjaxUserLoggedIn"),
    async: false,
    dataType: "json",
  }).done(function (ans) {
    isUserLogged = parseInt(ans.isUserLogged);
  });
  return isUserLogged;
}

$(window).scroll(function () {
  $(this).scrollTop() > 50
    ? $("header").addClass("js--scrolled")
    : $("header").removeClass("js--scrolled");
});
