/** 
Template Five by Is Open Source
Author: Ioannis Sannos
https://www.isopensource.com
*/

var tpl5loginwait = 0;

function tpl5SwitchLang() {
	if (!document.getElementById('tpl5langfm')) { return; }
	var fmObj = document.getElementById('tpl5langfm');
	var selObj = document.getElementById('tpl5_select_lang');
	var tpl5URL = selObj.options[selObj.selectedIndex].getAttribute('data-act');
	window.location.href = tpl5URL;
}

function tpl5ScrollTop() {
	document.body.scrollTop = 0;
	document.documentElement.scrollTop = 0;
}

function tpl5OnLoad() {
	document.getElementById('tpl5_to_top').addEventListener('click', function(event){ event.preventDefault(); tpl5ScrollTop(); }, false);
	document.getElementById('tpl5_mobmenu').addEventListener('click', function(event){ event.preventDefault(); tpl5OpenMenu(); }, false);
	document.getElementById('tpl5_mobmenuclose').addEventListener('click', function(event){ event.preventDefault(); tpl5CloseMenu(); }, false);
	if (document.getElementById('tpl5_loginlink')) {
		document.getElementById('tpl5_loginlink').addEventListener('click', function(event){ event.preventDefault(); elx5ModalOpen('tplog'); }, false);
	}
	if (document.getElementById('tpl5_logoutlink')) {
		let tpl5lo = document.getElementById('tpl5_logoutlink').getAttribute('data-lo');
		let tpl5rl = document.getElementById('tpl5_logoutlink').getAttribute('data-rl');
		document.getElementById('tpl5_logoutlink').addEventListener('click', function(event){ event.preventDefault(); tpl5Logout(tpl5lo, tpl5rl); }, false);
	}
	if (document.getElementById('tpl5loginform')) {
		let twofa = document.getElementById('tpl5loginform').getAttribute('data-twofa');
		document.getElementById('tpl5loginform').addEventListener('submit', function(event){ event.preventDefault(); tpl5Login(twofa, 0); return false; }, false);
	}
	if (document.getElementById('tpl52faloginform')) {
		document.getElementById('tpl52faloginform').addEventListener('submit', function(event){ event.preventDefault(); tpl5Login(1, 1); return false; }, false);
	}
	if (document.getElementById('tpl5_select_lang')) {
		document.getElementById('tpl5_select_lang').addEventListener('change', function(){ tpl5SwitchLang(); }, false);
	}
	document.getElementById('tpl5_to_top').style.display = 'none';
	var tpl5StickyOffset = document.getElementById('tpl5_header_menu_line').offsetTop;
	var tpl5Height = document.getElementById('tpl5_header_all').offsetHeight;
	tpl5Height = parseInt(tpl5Height, 10);
	if (tpl5Height > 100) {
		var tpl5FixedLimit = tpl5Height;
	} else {
		var tpl5Width = document.getElementById('tpl5_header_menu_line').offsetWidth;
		if (tpl5Width < 651) {
			var tpl5FixedLimit = 160;
		} else {
			var tpl5FixedLimit = 300;
		}
	}

	window.onscroll = function() {
		var sticky = document.getElementById('tpl5_header_menu_line');
		if (window.pageYOffset !== undefined) {
			var scroll = window.pageYOffset;
		} else if ((document.compatMode || '') === 'CSS1Compat') {
			var scroll = document.documentElement.scrollTop;
		} else {
			var scroll = document.body.scrollTop;
		}
		if (scroll >= tpl5FixedLimit) {
			sticky.classList.remove('tpl5_fixedmenu');
			sticky.classList.add('tpl5_fixedmenudark');
		} else if (scroll >= tpl5StickyOffset) {
			sticky.classList.remove('tpl5_fixedmenudark');
			sticky.classList.add('tpl5_fixedmenu');
		} else {
			sticky.classList.remove('tpl5_fixedmenu');
			sticky.classList.remove('tpl5_fixedmenudark');
		}
		if (scroll > 500) {
			document.getElementById('tpl5_to_top').style.display = 'block';
		} else {
			document.getElementById('tpl5_to_top').style.display = 'none';
		}
	};
}

function tpl5OpenMenu() {
	document.getElementById('tpl5_menu').style.width = '100%';
	document.getElementById('tpl5_maincontainer').style.display = 'none';
	document.getElementById('tpl5_footer').style.display = 'none';
}

function tpl5CloseMenu() {
	document.getElementById('tpl5_maincontainer').style.display = 'block';
	document.getElementById('tpl5_footer').style.display = 'block';
	document.getElementById('tpl5_menu').style.width = '0px';
}

function tpl5Login(has2fa, otplogin) {
	if (tpl5loginwait == 1) { return false; } //prevent double click
	if (otplogin == 1) {
		var btnelem = 'tpoptotpbtn';
	} else {
		var btnelem = 'tplogsublogin';
	}

	var rnd = Math.floor((Math.random()*100)+1);
	var edata = { 'rnd': rnd };
	edata.uname = document.getElementById('tploguname').value.trim();
	edata.pword = document.getElementById('tplogpword').value.trim();
	edata.modtoken = document.getElementById('tplogmodtoken').value;
	var login_action = document.getElementById('tpl5loginform').action+'ilogin';
	if (edata.uname == '') {
		document.getElementById('tploguname').focus();
		return false;
	}
	if (edata.pword == '') {
		document.getElementById('tplogpword').focus();
		return false;
	}
	if (has2fa == 1) {
		if (otplogin == 1) {
			edata.otp = document.getElementById('tpoptvcode').value.trim();
			if (edata.otp == '') {
				document.getElementById('tpoptvcode').focus();
				return false;
			}
			if (isNaN(edata.otp)) {
				document.getElementById('tpoptvcode').focus();
				return false;
			}
		} else {
			login_action = document.getElementById('tpl5loginform').action+'makeotp';
		}
	}

	elx5ModalMessageHide('tplog');

	var successfunc = function(xreply) {
		tpl5loginwait = 0;
		try {
			var jsonObj = JSON.parse(xreply);
		} catch(e) {
			document.getElementById(btnelem).innerHTML = document.getElementById(btnelem).getAttribute('data-loginlng');
			return false;
		}
		if (parseInt(jsonObj.success, 10) < 1) {
			document.getElementById(btnelem).innerHTML = document.getElementById(btnelem).getAttribute('data-loginlng');
			if (jsonObj.errormsg != '') {
				elx5ModalMessageShow('tplog', jsonObj.errormsg, 'elx5_error');
			} else {
				elx5ModalMessageShow('tplog', 'Action failed!', 'elx5_error');
			}
			return false;
		} else {
			if (has2fa == 1) {
				if (otplogin == 1) {
					location.reload();
				} else {
					var tiptxt = document.getElementById('tpl5vcode_tip').getAttribute('data-genlng');
					if (jsonObj.method == 'sms') {
						document.getElementById('tpl5vcode_tip').innerHTML = tiptxt.replace('@@@', 'SMS');
					} else {
						document.getElementById('tpl5vcode_tip').innerHTML = tiptxt.replace('@@@', 'e-mail');
					}
					document.getElementById('tpl5loginformwrap').className = 'elx5_invisible';
					document.getElementById('tpl52faloginformwrap').className = 'elx5_zero';
				}
			} else {
				location.reload();
			}
		}
	};
	var errorfunc = function (XMLHttpRequest, textStatus, errorThrown) {
		tpl5loginwait = 0;
		document.getElementById(btnelem).innerHTML = document.getElementById(btnelem).getAttribute('data-loginlng');
		elx5ModalMessageShow('tplog', 'Login failed with error message '+errorThrown, 'elx5_error');
	};
	tpl5loginwait = 1;
	document.getElementById(btnelem).innerHTML = document.getElementById(btnelem).getAttribute('data-waitlng');
	elxAjax('POST', login_action, edata, null, null, successfunc, errorfunc);
}

function tpl5Logout(logoutlink, redirect_link) {
	if (tpl5loginwait == 1) { return false; } //prevent double click
	var rnd = Math.floor((Math.random()*100)+1);
	var edata = { 'rnd': rnd };
	var successfunc = function(xreply) {
		tpl5loginwait = 0;
		try {
			var jsonObj = JSON.parse(xreply);
		} catch(e) {
			return false;
		}
		if (parseInt(jsonObj.success, 10) < 1) {
			if (jsonObj.errormsg != '') {
				alert(jsonObj.errormsg);
			} else {
				alert('Action failed!');
			}
		} else {
			window.location.href = redirect_link;
		}
	};
	var errorfunc = function (XMLHttpRequest, textStatus, errorThrown) {
		tpl5loginwait = 0;
		alert('Logout failed with error message '+errorThrown);
	};
	tpl5loginwait = 1;
	elxAjax('POST', logoutlink, edata, null, null, successfunc, errorfunc);
}
