﻿var overlayID = "bkg";
var dialogID = "dlg";
var closeID = "dlgX";
var bkPanel = 0;
const timers = {};
$(document).ready(function () { Init(); });

function Init()
{
	try { Typekit.load(); } catch (e) { }
	$("#navBar").css("opacity", 0);
	var navs = new Array("Quote", "About Us", "Support", "Contact Us", "Courier Services");
	var urls = new Array("/", "about", "https://support.niparcels.com/help", "contact", "courier-services");
	var siz = new Array(0, 0, 0, 0, 1);

	$(navs).each(function (i, e) {
		$("#navBar").append("<div style=\"position:absolute;left:0px;\" class=\"navButton navUnselected navSize" + siz[i] + "\" onclick=\"location.href='" + urls[i] + "'\">" + this + "</div>");
	});

	var xpos = 0;$(".navButton").each(function (i) { $(this).css("left", xpos); xpos += $(this).width() + 9; });
	$(".navButton:nth-child(1)").addClass("navSelected");
	$(".navButton").click(function () { if ($(this).hasClass("navUnselected")) { $(".navButton").removeClass("navSelected").addClass("navUnselected"); $(this).removeClass("navUnselected").addClass("navSelected"); } });
	$("#navBar").css("opacity", 1);
	$("#ftFB").click(function () { window.open("https://www.facebook.com/niparcels", "_blank") });
	$("#ftTW").click(function () { window.open("https://www.twitter.com/niparcels", "_blank") });
	$(document).on("click", ".btnLogOut", function () { A("m=o"); });
	$(document).on("click", "#topupBox", function () { location.href = "top-up.aspx"; });
	$(document).on("click", "#btnMyOrders", function () { location.href = "orders"; });
	$(document).on("click", "#btnMyAccount", function () { location.href = "account"; });
	$(document).on("click", "#btnTrack", function () { A("m=trk&id=" + escape($("#tbTrack").val()) + "&pc=" + escape($("#tbPostCode").val())); });
	$(document).on("keydown", "#tbTrack", function (e) { var k = e.charCode ? e.charCode : e.keyCode; if (k == 13) $("#btnTrack").trigger("click"); });
	$(document).on("click", "#tbTrack_wmk", function () { $(this).hide(); $("#tbTrack").focus(); });
	$(document).on("focus", "#tbTrack", function () { $("#tbTrack_wmk").hide(); });
	$(document).on("blur", "#tbTrack", function () { if ($(this).val() == "") { $("#tbTrack_wmk").show(); } });
	$(document).on("click", ".mfp-dlg .kill", function () { mobCloseAlert(); });

	_mslgn("_tbli");
	_mslgn("_tbpw");
	$(document).on("click", "#msBtnLogin", function () { var orderuid = (typeof (hash) == "string") ? hash : ""; mobServe("general", "action=dsklogin&em=" + escape($("#_tbli").val()) + "&pw=" + escape($("#_tbpw").val()) + "&topbox=true&booking=" + ($("#mobBookProgress").length > 0) + "&hash=" + orderuid); });
	$(document).on("keydown", "#_tbpw", function (e) { var k = e.charCode ? e.charCode : e.keyCode; if (k == 13) $("#msBtnLogin").trigger("click"); });
	$(document).on("click", "#msForgottenPassword", function () { var em = $("#_tbli").val(); if (em.length == 0) { alert("Enter your email address get a password reset link"); } else { mobServe("general", "action=forgotten-password&email=" + escape(em)); } });

	InitHelpers();
}

function DYM(e) { $(".dym").remove(); $("#rg_em").after("<div class='dym'>" + e + "</div>"); }
function UDYM(o) { $("#rg_em").val($(o).attr("data-email")); $(".dym").remove(); }

function InitHelpers()
{
	$(document).on("click", ".helper", function ()
	{
		var _helper = this;
		var hID = "#helper";
		$(this).attr({ "act": "0", "z-index": 9999 });
		var isact = $(this).attr("act");
		if (isact == "1") return false;
		$(this).attr("act", "1");

		$(hID).remove();
		var dPos = $(this).offset();
		var _m = 12;
		var _w = 160;
		var _x = dPos.left;
		var _y = dPos.top;
		$("body").append("<div id='helper' style='position:absolute;top:" + _y + "px;left:" + _x + "px;width:" + _w + "px;'><div style='padding:8px;overflow:hidden;'>" + $(this).attr("info") + "</div></div>");

		// max height
		var _h = $(hID).height();

		// inner text/html
		var _t = $(hID + " > div").html();

		// clear inner text
		$(hID).html("<div style='padding:2px;overflow:hidden;'></div>").css({ "width": 4, "opacity": 0.95 });

		// set y position of bubble box
		$(hID).css({ "top": _y + ($(this).height() / 2) - 4 });

		var _dY = (_x - _w - _m);
		var _isL = true;

		// too far left off screen
		if (_dY < 8)
		{
			_x += $(this).width();
			$(hID).css("left", _x);
			_dY = (_x + ($(this).width() / 2));
			_isL = false;
		}
		$(hID).off("mouseout");
		$(window).off("click.helper");

		$(hID).stop().animate({ "width": _w, "left": _dY, "top": (_y - (_h / 2) + 10), "height": _h }, 400, "easeOutExpo",
			function ()
			{
				$(this).html("<div style='padding:8px;overflow:hidden;'>" + _t + "</div>");
				$(this).find("div").css("opacity", 0);
				$(this).find("div").stop().animate({ "opacity": 1 }, 150);

				$(_helper).mouseout(function ()
				{
					$(_helper).attr("act", "0");
					$(hID).stop().animate({ "opacity": 0 }, 500, function ()
					{
						RemoveHelper();
					});
				});

				$(hID).on("click", function () {
					RemoveHelper();
				});

				$(window).on("click.helper", function () {
					RemoveHelper();
				});
			}
		);
	});
};

function RemoveHelper() {
	$(window).off("click.helper");
	$("#helper").remove();
}

function _mslgn(id) { $("#" + id).bind("click focus", function () { $(_twm($(this).attr("id")).hide()); }); $("#" + id + "_wm").click(function () { var id = $(this).attr("id").replace("_wm", ""); $("#" + id).focus(); $(this).hide(); }); $("#" + id).blur(function () { if ($(this).val().length < 1) $(_twm($(this).attr("id")).fadeIn()); }); }
function _twm(id) { return ($("#" + id + "_wm")); }


// dialog code
function Popup(html) { mobAlert("", html); }
function ShowAlert(text) { mobAlert("", text); }
function replace(string, replaceDigit, replaceWith) { while (string.indexOf(replaceDigit) > -1) string = string.replace(replaceDigit, replaceWith); return string; }
function IsMobile() { return $(window).width() < 667; }


function ErrorOn(id, txt, xoff)
{
	if (IsMobile())
	{
		var row = $("#" + id).parent().addClass("error");
		$("#" + id + "_error").remove();
		$("#" + id).after("<div id=\"" + id + "_error\" class=\"field_error\">" + txt + "</div>");
		return;
	}
	var cls = (xoff) ? "RTL" : "LTR";
	var html = "<div id=\"" + id + "_e\" class=\"errorNotice error" + cls + "\" style=\"position:absolute;top:0px;left:0px;\">";
	html += "<div style=\"padding:8px;\">";
	html += "<center><b style=\"color:#FFF;\">" + txt + "</b></center></div></div>";
	$("#" + id + "_e").remove();
	var dpos = $("#" + id).offset();
	$("body").append(html);
	var obj = $("#" + id + "_e");
	obj.hide();
	var margin = 12;
	var x = dpos.left;
	if (xoff) { x = dpos.left + $("#" + id).width() + margin; } else { x = dpos.left - obj.width() - margin + 12; }
	obj.css({ "top": dpos.top, "left": x, "opacity": 0.8 });
	obj.fadeIn();
}


function KillNote(el)
{
	if (IsMobile())
	{
		$("#" + el).parent().removeClass("error");
		$("#" + el + "_error").remove();
		return;
	}
	var cy = parseInt($(el + "_e").css("top")); $("#" + el + "_e").animate({ "top": cy - 80, "opacity": 0 }, 800, function () { $(this).remove(); });
}

function A(url) { setTimeout(function () { $.ajax({ url: "code/g.ashx?" + url, async: false, success: function (e) { eval(e); } }); }, 500); }
function DisableButton(obj, b) { var op = (b) ? 0.4 : 1; var cu = (b) ? "default" : "pointer"; $(obj).css({ "opacity": op, "cursor": cu }); }
function ShowOverlay() { $("#" + overlayID).remove(); $("form").append("<div id=\"" + overlayID + "\"></div>"); $("#" + overlayID).css({ opacity: 0, display: "block" }); $("#" + overlayID).animate({ opacity: 0.85 }, 20); }
function HideOverlay() { $(overlayID).remove(); DisBtn(false); }
function HideDialog() { $.magnificPopup.close(); }

//function CenterDlg() { var cHeight = document.documentElement.clientHeight; var popupHeight = $("#" + dialogID).height(); var nY = (cHeight / 2) - (popupHeight / 2); if (nY <= 0) nY = 0; var cWidth = document.documentElement.clientWidth; var popupWidth = $("#" + dialogID).width(); var nX = (cWidth / 2) - (popupWidth / 2); $("#" + dialogID).css({ "top": nY, "left": nX }); }
function __jumpto(sel, exe) { var off = ($(".navbar").length == 0) ? 134 : $(".navbar").height(); $("body").animate({ scrollTop: $(sel).offset().top - off - 8 }, 550, function () { eval(exe); }); }

function IsWholeNumber(input) {
	return /^\d+$/.test(input);
}

function Debounce(fn, timeout, key) {
	return (...args) => {
		let t = 0

		if (typeof key !== "undefined") {
			timers[key] = 0

			t = timers[key]
		}

		clearTimeout(t)
		t = setTimeout(fn.bind(this, ...args), timeout || 500)
	}
}

function AddClassToElement(ele, classId) {
	if (!ele || !classId) {
		return
	}

	if (typeof ele === 'string' || ele instanceof String) {
		$("#" + ele).addClass(classId)
	}
}

function RemoveClassFromElement(ele, classId) {
	if (!ele || !classId) {
		return
	}

	if (typeof ele === 'string' || ele instanceof String) {
		$("#" + ele).removeClass(classId)
	}
}