/**
* @package		Elxis
* @copyright	Copyright (c) 2006-2025 elxis.org (https://www.elxis.org). All rights reserved.
* @license		Elxis Public License ( https://www.elxis.org/elxis-public-license.html )
* @author		Elxis Team ( https://www.elxis.org )
* @description 	Elxis CMS is free software. Read the license for copyright notices and details
*/

/* POST COMMENT */
function elx5PostComment() {
	var successfunc = function(xreply) {
		elx5StopPageLoader('pgloadcomments');
		try {
			var jsonObj = JSON.parse(xreply);
		} catch(e) {
			document.getElementById('postcommentreply').innerHTML = e.message;
			document.getElementById('postcommentreply').className = 'elx5_warning';
			return false;
		}

		if (parseInt(jsonObj.success, 10) == 1) {
			document.getElementById('pcommessage').value = '';
			if (parseInt(jsonObj.waitapproval, 10) == 1) {
				if (jsonObj.message != '') {
					document.getElementById('postcommentreply').innerHTML = jsonObj.message;
				} else {
					document.getElementById('postcommentreply').innerHTML = 'Thanks for your comment. It will be published after approval.';
				}
				document.getElementById('postcommentreply').className = 'elx5_success';
			} else {
				document.getElementById('postcommentreply').className = 'elx5_invisible';

				let liObj = document.createElement('li');
				liObj.id = 'elx_comment_'+jsonObj.comid;

				let avObj = document.createElement('div');
				avObj.className = 'elx5_comment_avatar';
				avObj.innerHTML = '<img src="'+jsonObj.avatar+'" alt="'+jsonObj.author+'" title="'+jsonObj.author+'" />';
				liObj.appendChild(avObj);

				let topObj = document.createElement('div');
				topObj.className = 'elx5_comment_top';
				let auObj = document.createElement('div');
				auObj.className = 'elx5_comment_author';
				let ntext = document.createTextNode(jsonObj.author);
				auObj.appendChild(ntext);
				topObj.appendChild(auObj);
				let tiObj = document.createElement('time');
				tiObj.className = 'elx5_comment_date';
				ntext = document.createTextNode(jsonObj.created);
				tiObj.appendChild(ntext);
				topObj.appendChild(tiObj);

				let mObj = document.createElement('div');
				mObj.className = 'elx5_comment_message';
				mObj.id = 'elx_comment_message_'+jsonObj.comid;
				mObj.innerHTML = jsonObj.commessage;

				let mainObj = document.createElement('div');
				mainObj.className = 'elx5_comment_main';
				mainObj.appendChild(topObj);
				mainObj.appendChild(mObj);

				var compubid = 0;
				var comdelid = 0;
				if ((jsonObj.canmail == 1) || (jsonObj.candel == 1) || ((jsonObj.canpub == 1) && (jsonObj.published == 0))) {
					let actObj = document.createElement('div');
					actObj.className = 'elx5_comment_actions';
					let acthtml = '';
					if (jsonObj.canmail == 1) {
						acthtml += '<a href="mailto:'+jsonObj.email+'" title="'+jsonObj.email+'"><i class="fas fa-envelope"></i><span class="elx5_mobhide"> e-mail</span></a>';
					}
					if ((jsonObj.canpub == 1) && (jsonObj.published == 0)) {
						acthtml += '<a href="#" data-artpubcomlink="'+jsonObj.comid+'" id="elx_comment_publish_'+jsonObj.comid+'" title="'+jsonObj.lngpublish+'">';
						acthtml += '<i class="fas fa-check-square"></i><span class="elx5_mobhide"> '+jsonObj.lngpublish+'</span></a>';
						compubid = jsonObj.comid;
					}
					if (jsonObj.candel == 1)  {
						acthtml += '<a href="#" data-artdelcomlink="'+jsonObj.comid+'" id="elx_comment_del_'+jsonObj.comid+'" title="'+jsonObj.lngdelete+'">';
						acthtml += '<i class="fas fa-trash"></i><span class="elx5_mobhide"> '+jsonObj.lngdelete+'</span></a>';
						comdelid = jsonObj.comid;
					}
					actObj.innerHTML = acthtml;
					mainObj.appendChild(actObj);
				}
				liObj.appendChild(mainObj);
				document.getElementById('elx_comments_list').appendChild(liObj);

				if (compubid > 0) {
					document.getElementById('elx_comment_publish_'+compubid).addEventListener('click', function(e){ e.preventDefault(); elx5PublishComment(compubid); }, false);
				}
				if (comdelid > 0) {
					document.getElementById('elx_comment_del_'+comdelid).addEventListener('click', function(e){ e.preventDefault(); elx5DeleteComment(comdelid); }, false);
				}
				if (document.getElementById('pcomcomseccode')) {
					document.getElementById('pcomcomseccode').value = '';
					if (document.getElementById('pcomcomseccodebox')) {//no robot
						document.getElementById('pcomcomseccodebox').className = 'elxnorobotbox';
						document.getElementById('pcomcomseccodebox').innerHTML = '&#160;';
					}
				}
				if (document.getElementById('elx_comment_0')) {
					var li0Obj = document.getElementById('elx_comment_0');
					document.getElementById('elx_comments_list').removeChild(li0Obj);
				}
			}
		} else {//error
			if (jsonObj.message != '') {
				document.getElementById('postcommentreply').innerHTML = jsonObj.message;
			} else {
				document.getElementById('postcommentreply').innerHTML = 'Action failed!';
			}
			document.getElementById('postcommentreply').className = 'elx5_warning';
		}
	}
	var errorfunc = function (XMLHttpRequest, textStatus, errorThrown) {
		elx5StopPageLoader('pgloadcomments');
		document.getElementById('postcommentreply').innerHTML = 'Error! '+errorThrown; 
		document.getElementById('postcommentreply').className = 'elx5_warning';
	}

	var edata = {};
	edata.id = parseInt(document.getElementById('pcomid').value, 10);
	if (isNaN(edata.id) || (edata.id < 1)) { return false; }
	if (document.getElementById('pcomauthor')) {
		edata.author = document.getElementById('pcomauthor').value;
		edata.author = edata.author.replace(/['"]/g,'');
	}
	if (document.getElementById('pcomemail')) {
		edata.email = document.getElementById('pcomemail').value;
		edata.email = edata.email.replace(/['"]/g,'');
	}
	edata.message = document.getElementById('pcommessage').value;
	edata.message = edata.message.replace(/['"]/g,'');
	if (edata.message == '') {
		document.getElementById('pcommessage').focus();
		return false;
	}
	edata.act = 'postcomment';
	edata.comseccode = document.getElementById('pcomcomseccode').value;
	edata.token = document.getElementById('pcomtoken').value;
	edata.rnd = Math.floor((Math.random()*100)+1);

	document.getElementById('postcommentreply').className = 'elx5_invisible';

	var eurl = document.getElementById('fmpostcomment').action;
	elx5StartPageLoader('pgloadcomments');
	elxAjax('POST', eurl, edata, null, null, successfunc, errorfunc);
}


/* DELETE COMMENT */
function elx5DeleteComment(id) {
	if (isNaN(id)) { return false; }
	id = parseInt(id, 10);
	if (id < 1) { return false; }

	var ulObj = document.getElementById('elx_comments_list');
	var prompttxt = ulObj.getAttribute('data-lngsure');

	var successfunc = function(xreply) {
		elx5StopPageLoader('pgloadcomments');
		try {
			var jsonObj = JSON.parse(xreply);
		} catch(e) {
			alert(e.message);
			return false;
		}
		if (parseInt(jsonObj.success, 10) < 1) {
			if (jsonObj.message != '') {
				alert(jsonObj.message);
			} else {
				alert('Action failed!');
			}
		} else {
			if (document.getElementById('elx_comment_'+id)) {
				let liObj = document.getElementById('elx_comment_'+id);
				ulObj.removeChild(liObj);
			}
			var lis = ulObj.querySelectorAll('li');
			let addli0 = false;
			if (!lis) {
				addli0 = true;
			} else if (lis.length == 0) {
				addli0 = true;
			}
			if (addli0) {
				let li0Obj = document.createElement('li');
				li0Obj.id = 'elx_comment_0';
				li0Obj.className = 'elx5_nocomments';
				let ntext = document.createTextNode(ulObj.getAttribute('data-lngnocomments'));
				li0Obj.appendChild(ntext);
				ulObj.appendChild(li0Obj);
			}
		}
	};
	var errorfunc = function (XMLHttpRequest, textStatus, errorThrown) {
		elx5StopPageLoader('pgloadcomments');
		alert('Action failed! '+errorThrown);
	};

	if (confirm(prompttxt)) {
		let rnd = Math.floor((Math.random()*100)+1);
		var edata = {'id': id, 'act': 'delcomment', 'rnd': rnd };
		var eurl = ulObj.getAttribute('data-tools');
		elx5StartPageLoader('pgloadcomments');
		elxAjax('POST', eurl, edata, null, null, successfunc, errorfunc);
	}
}


/* PUBLISH COMMENT (AJAX) */
function elx5PublishComment(id) {
	if (isNaN(id)) { return false; }
	id = parseInt(id, 10);
	if (id < 1) { return false; }

	var ulObj = document.getElementById('elx_comments_list');

	var successfunc = function(xreply) {
		elx5StopPageLoader('pgloadcomments');
		try {
			var jsonObj = JSON.parse(xreply);
		} catch(e) {
			alert(e.message);
			return false;
		}
		if (parseInt(jsonObj.success, 10) < 1) {
			if (jsonObj.message != '') {
				alert(jsonObj.message);
			} else {
				alert('Action failed!');
			}
		} else {
			document.getElementById('elx_comment_message_'+id).className  = 'elx5_comment_message';
			document.getElementById('elx_comment_publish_'+id).innerHTML = '';
			document.getElementById('elx_comment_publish_'+id).className = 'elx5_invisible';
			document.getElementById('elx_comment_publish_'+id).style.margin = '0px';
			document.getElementById('elx_comment_publish_'+id).style.padding = '0px';
		}
	}
	var errorfunc = function (XMLHttpRequest, textStatus, errorThrown) {
		elx5StopPageLoader('pgloadcomments');
		alert('Action failed! '+errorThrown);
	}

	let rnd = Math.floor((Math.random()*100)+1);
	var edata = {'id': id, 'act': 'pubcomment', 'rnd': rnd };
	var eurl = ulObj.getAttribute('data-tools');
	elx5StartPageLoader('pgloadcomments');
	elxAjax('POST', eurl, edata, null, null, successfunc, errorfunc);
}


/* 
HIGHLIGHT WORDS 
Original JavaScript code by Chirp Internet: www.chirp.com.au
*/
function textHighlight(id) {
	var targetNode = document.getElementById(id) || document.body;
	var skipTags = new RegExp("^(?:EM|SCRIPT|FORM|SPAN)$");
	var colors = ["#ff6", "#a0ffff", "#9f9", "#f99", "#f6f"];
	var wordColor = [];
	var colorIdx = 0;
	var matchRegex = "";
	this.setRegex = function(input) {
		input = input.replace(/^\\u([^\w]+|[^\w])+$/g, "").replace(/\\u([^\w'-])+/g, "|");
		matchRegex = new RegExp("(" + input + ")","i");
	}

	this.getRegex = function() {
		return matchRegex.toString().replace(/^\/\\b\(|\)\\b\/i$/g, "").replace(/\|/g, " ");
	}

	this.hiliteWords = function(node) {
		if ((typeof node == 'undefined') || !node) { return; }
		if (!matchRegex) return;
		if (skipTags.test(node.nodeName)) return;
		if (node.hasChildNodes()) {
			for(var i=0; i < node.childNodes.length; i++) { this.hiliteWords(node.childNodes[i]); }
		}
		if(node.nodeType == 3) {
			if((nv = node.nodeValue) && (regs = matchRegex.exec(nv))) {
				if(!wordColor[regs[0].toLowerCase()]) { wordColor[regs[0].toLowerCase()] = colors[colorIdx++ % colors.length]; }
				var match = document.createElement('EM');
        		match.appendChild(document.createTextNode(regs[0]));
        		match.style.backgroundColor = wordColor[regs[0].toLowerCase()];
        		match.style.fontStyle = "inherit";
        		match.style.color = "#000";
        		var after = node.splitText(regs.index);
        		after.nodeValue = after.nodeValue.substring(regs[0].length);
        		node.parentNode.insertBefore(match, after);
			}
		}
	};

	this.remove = function() {
		var arr = document.getElementsByTagName('EM');
		while(arr.length && (el = arr[0])) { el.parentNode.replaceChild(el.firstChild, el); }
	};

	this.apply = function(input) {
		if ((typeof input == 'undefined') || !input) { return; }
    	this.remove();
    	this.setRegex(input);
    	this.hiliteWords(targetNode);
	};
}


function elxArtOnLoad() {
	if (document.getElementById('elxartprintlink')) {
		document.getElementById('elxartprintlink').addEventListener('click', function(event){ event.preventDefault(); window.print(); }, false);
	}
	if (document.getElementById('elxartprintpoplink')) {
		let artlink = document.getElementById('elxartprintpoplink').getAttribute('data-artlink');
		document.getElementById('elxartprintpoplink').addEventListener('click', function(event){ event.preventDefault(); elxPopup(artlink, 800, 600); }, false);
	}
	if (document.getElementById('elxartfriendlink')) {
		let frlink = document.getElementById('elxartfriendlink').getAttribute('data-frlink');
		document.getElementById('elxartfriendlink').addEventListener('click', function(event){ event.preventDefault(); elxPopup(frlink, 600, 450); }, false);
	}
	if (document.getElementById('elxarttwilink')) {
		let twlink = document.getElementById('elxarttwilink').getAttribute('data-twlink');
		document.getElementById('elxarttwilink').addEventListener('click', function(event){ event.preventDefault(); elxPopup(twlink, 700, 450); }, false);
	}
	if (document.getElementById('elxartfblink')) {
		let fblink = document.getElementById('elxartfblink').getAttribute('data-fblink');
		document.getElementById('elxartfblink').addEventListener('click', function(event){ event.preventDefault(); elxPopup(fblink, 700, 450); }, false);
	}
	var artpcls = document.querySelectorAll('[data-artpubcomlink]');
	if (artpcls) {
		for (let q = 0; q < artpcls.length; q++) {
			let artcid = artpcls[q].getAttribute('data-artpubcomlink');
			artpcls[q].addEventListener('click', function(event){ event.preventDefault(); elx5PublishComment(artcid); }, false);
		}
	}
	var artdcls = document.querySelectorAll('[data-artdelcomlink]');
	if (artdcls) {
		for (let w = 0; w < artdcls.length; w++) {
			let artdcid = artdcls[w].getAttribute('data-artdelcomlink');
			artdcls[w].addEventListener('click', function(event){ event.preventDefault(); elx5DeleteComment(artdcid); }, false);
		}
	}
	if (document.getElementById('fmpostcomment')) {
		document.getElementById('fmpostcomment').addEventListener('submit', function(event){ event.preventDefault(); elx5PostComment(); return false; }, false);
	}
}

function elxCtgOnLoad() {
	if (document.getElementById('elxctgprlink')) {
		document.getElementById('elxctgprlink').addEventListener('click', function(e){ e.preventDefault(); window.print(); }, false);
	}
	if (document.getElementById('elxctgpoplink')) {
		let poplink = document.getElementById('elxctgpoplink').getAttribute('data-poplink');
		document.getElementById('elxctgpoplink').addEventListener('click', function(e){ e.preventDefault(); elxPopup(poplink, 800, 600); }, false);
	}
	if (document.getElementById('elxctgprlink2')) {
		document.getElementById('elxctgprlink2').addEventListener('click', function(e){ e.preventDefault(); window.print(); }, false);
	}
	if (document.getElementById('elxctgpoplink2')) {
		let poplink = document.getElementById('elxctgpoplink2').getAttribute('data-poplink');
		document.getElementById('elxctgpoplink2').addEventListener('click', function(e){ e.preventDefault(); elxPopup(poplink, 800, 600); }, false);
	}
}

function elxConGenOnLoad() {
	if (document.getElementById('elxcongenblink')) {
		document.getElementById('elxcongenblink').addEventListener('click', function(event){ event.preventDefault(); window.history.go(-1); }, false);
	}
	if (document.getElementById('elxcongenclink')) {
		document.getElementById('elxcongenclink').addEventListener('click', function(event){ event.preventDefault(); window.close(); }, false);
	}
}