
var loginwait = 0;

function modlogin(sfx, avatar, displayname, textdir, has2fa, otplogin) {
	if (loginwait == 1) { return false; } //prevent double click
	if (otplogin == 1) {
		var btnelem = 'otpbtn'+sfx;
	} else {
		var btnelem = 'sublogin'+sfx;
	}
	var rnd = Math.floor((Math.random()*100)+1);
	var edata = { 'rnd': rnd };
	edata.uname = document.getElementById('uname'+sfx).value.trim();
	edata.pword = document.getElementById('pword'+sfx).value.trim();
	edata.modtoken = document.getElementById('modtoken'+sfx).value;
	var iuser_link = document.getElementById('loginform'+sfx).action;
	var login_action = iuser_link+'ilogin';
	if (edata.uname == '') {
		document.getElementById('uname'+sfx).focus();
		return false;
	}
	if (edata.pword == '') {
		document.getElementById('pword'+sfx).focus();
		return false;
	}
	if (has2fa == 1) {
		if (otplogin == 1) {
			edata.otp = document.getElementById('vcode'+sfx).value.trim();
			if (edata.otp == '') {
				document.getElementById('vcode'+sfx).focus();
				return false;
			}
			if (isNaN(edata.otp)) {
				document.getElementById('vcode'+sfx).focus();
				return false;
			}
		} else {
			login_action = iuser_link+'makeotp';
		}
	}

	var successfunc = function(xreply) {
		loginwait = 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 != '') {
				alert(jsonObj.errormsg);
			} else {
				alert('Action failed!');
			}
		} else {
			if (has2fa == 1) {
				if (otplogin == 1) {
					modloginShowUser(sfx, displayname, avatar, iuser_link, jsonObj);
				} else {
					var tiptxt = document.getElementById('modlogvcode_tip').getAttribute('data-genlng');
					if (jsonObj.method == 'sms') {
						document.getElementById('modlogvcode_tip').innerHTML = tiptxt.replace('@@@', 'SMS');
					} else {
						document.getElementById('modlogvcode_tip').innerHTML = tiptxt.replace('@@@', 'e-mail');
					}
					document.getElementById('modloginformwrap').className = 'elx5_invisible';
					document.getElementById('mod2faloginformwrap').className = 'elx5_zero';
				}
			} else {
				modloginShowUser(sfx, displayname, avatar, iuser_link, jsonObj);
			}
		}
	};
	var errorfunc = function (XMLHttpRequest, textStatus, errorThrown) {
		loginwait = 0;
		document.getElementById(btnelem).innerHTML = document.getElementById(btnelem).getAttribute('data-loginlng');
		alert('Login failed with error message '+errorThrown);
	};
	loginwait = 1;
	document.getElementById(btnelem).innerHTML = document.getElementById(btnelem).getAttribute('data-waitlng');
	elxAjax('POST', login_action, edata, null, null, successfunc, errorfunc);
}


function modloginShowUser(sfx, displayname, avatar, iuser_link, jsonObj) {
	if (displayname == 2) {
		var username_txt = jsonObj.uname;
	} else if (jsonObj.firstname != '') {
		var username_txt = jsonObj.firstname+' '+jsonObj.lastname;
	} else if (jsonObj.email != '') {
		var username_txt = jsonObj.email;
	} else {
		var username_txt = jsonObj.uname;
	}

	var logout_txt = document.getElementById('mlogin_logout'+sfx).innerHTML;
	var profile_link = document.getElementById('mlogin_profile'+sfx).innerHTML;
	var logout_redir = '';
	if (document.getElementById('mlogout_redir'+sfx)) {
		logout_redir = document.getElementById('mlogout_redir'+sfx).innerHTML;
	}

	var wrapObj = document.getElementById('modlogin_wrapper'+sfx);
	wrapObj.innerHTML = '';
	if (avatar == 1) {
		var avObj = document.createElement('DIV');
		avObj.className = 'elx5_modlogin_avatarbox';
		avObj.innerHTML = '<a href="'+profile_link+'" title="'+jsonObj.uname+'"><img src="'+jsonObj.avatar+'" alt="avatar"></a>';
		wrapObj.appendChild(avObj);
		var infoObj = document.createElement('DIV');
		infoObj.className = 'elx5_modlogin_mainbox';
	} else {
		var infoObj = document.createElement('DIV');
		infoObj.className = 'elx5_zero';
	}

	var profObj = document.createElement('A');
	var profText = document.createTextNode(username_txt);
	profObj.appendChild(profText);
	profObj.title = jsonObj.uname;
	profObj.className = 'elx5_modlogin_profile';
	profObj.href = profile_link;
	infoObj.appendChild(profObj);
	var logoutlink = iuser_link+'ilogout';
	var outObj = document.createElement('A');
	var outText = document.createTextNode(logout_txt);
	outObj.appendChild(outText);
	outObj.title = logout_txt;
	outObj.className = 'elx5_modlogin_logout';
	outObj.href = '#';
	outObj.addEventListener('click', function(e){ e.preventDefault(); modlogout(logoutlink, logout_redir); }, false);
	infoObj.appendChild(outObj);
	wrapObj.appendChild(infoObj);
}


function modlogout(logoutlink, redirect_link) {
	if (loginwait == 1) { return false; } //prevent double click
	var rnd = Math.floor((Math.random()*100)+1);
	var edata = { 'rnd': rnd };
	var successfunc = function(xreply) {
		loginwait = 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 {
			if (redirect_link == '') {
				location.reload();
			} else {
				window.location.href = redirect_link;
			}
		}
	};
	var errorfunc = function (XMLHttpRequest, textStatus, errorThrown) {
		loginwait = 0;
		alert('Logout failed with error message '+errorThrown);
	};
	loginwait = 1;
	elxAjax('POST', logoutlink, edata, null, null, successfunc, errorfunc);
}

function modLogOnLoad() {
	let elements = document.getElementsByName('loginform');
	if (elements.length > 0) {
		for (let q=0; q < elements.length; q++) {
			if (!elements[q].hasAttribute('data-rand')) { continue; }
			let rand = elements[q].getAttribute('data-rand');
			let avat = elements[q].getAttribute('data-avat');
			let disp = elements[q].getAttribute('data-disp');
			let dir = elements[q].getAttribute('data-dir');
			let twofa = elements[q].getAttribute('data-twofa');
			elements[q].addEventListener('submit', function(event){ event.preventDefault(); modlogin(rand, avat, disp, dir, twofa, 0); return false; }, false);
		}
	}
	if (document.getElementById('elx5_modlogin_logout')) {
		let loli = document.getElementById('elx5_modlogin_logout').getAttribute('data-loli');
		let lord = document.getElementById('elx5_modlogin_logout').getAttribute('data-lord');
		document.getElementById('elx5_modlogin_logout').addEventListener('click', function(event){ event.preventDefault(); modlogout(loli, lord); }, false);
	}
	for (var ki=0; ki < 10; ki++) {
		if (!document.getElementById('elxmolog_prov'+ki)) { break; }
		let plink = document.getElementById('elxmolog_prov'+ki).getAttribute('data-plink');
		let auth = document.getElementById('elxmolog_prov'+ki).getAttribute('data-auth');
		document.getElementById('elxmolog_prov'+ki).addEventListener('click', function(event){ event.preventDefault(); elxPopup(plink, 700, 550, 'Login with '+auth); }, false);
	}
	let lforms = document.querySelectorAll('form[data-modloginrand]');
	if (lforms.length > 0) {
		for (let q=0; q < lforms.length; q++) {
			lforms[q].addEventListener('submit', function(e){
				let lrand = this.getAttribute('data-modloginrand');
				let avat = this.getAttribute('data-avat');
				let dname = this.getAttribute('data-dname');
				let ldir = this.getAttribute('data-dir');
				let ltwofa = this.getAttribute('data-twofa');
				e.preventDefault();
				modlogin(lrand, avat, dname, ldir, ltwofa, 1);
				return false;
			}, false);
		}
	}
}
