/* Component Content javascript */
/* Elxis CMS - https://www.elxis.org */
/* Ioannis Sannos (datahell) */
 
 
function con5MoveCategory(moveup) {
	if (typeof moveup === 'undefined') { moveup = 1; }
	moveup = parseInt(moveup, 10);
	var catid = elx5SelectedTableItem('categoriestbl', false);
	if (catid === false) { return; }
	catid = parseInt(catid, 10);
	if (catid < 1) { return; }

	var successfunc = function(xreply) {
		elx5StopPageLoader();
		try {
			var jsonObj = JSON.parse(xreply);
		} catch(e) {
			alert('Action failed!');
			return;
		}
		if (parseInt(jsonObj.success, 10) < 1) {
			if (jsonObj.message != '') {
				alert(jsonObj.message);
			} else {
				alert('Action failed!');
			}
		} else {
			location.reload(true);
		}
	}
	var errorfunc = function (XMLHttpRequest, textStatus, errorThrown) {
		elx5StopPageLoader();
		alert('Error! '+errorThrown);
	}

	var rnd = Math.floor((Math.random()*100)+1);
	var edata = { 'catid':catid, 'moveup':moveup, 'rnd':rnd };
	var eurl = document.getElementById('categoriestbl').getAttribute('data-inpage')+'move';

	elx5StartPageLoader();
	elxAjax('POST', eurl, edata, null, null, successfunc, errorfunc);
}

function con5CategoryTrans(tbl) {
	var elid = elx5SelectedTableItem(tbl, false);
	if (elid === false) { return; }
	elid = parseInt(elid, 10);
	if (elid < 1) { return; }
	var eurl = document.getElementById('con5categorytranslations').innerHTML+'&id='+elid;
	eurl = eurl.replace(/&amp;/g, '&');
	elxPopup(eurl, 650, 400, 'ctgtranspreview', 'yes');
}

function con5ArticleTrans(tbl) {
	var elid = elx5SelectedTableItem(tbl, false);
	if (elid === false) { return; }
	elid = parseInt(elid, 10);
	if (elid < 1) { return; }
	var eurl = document.getElementById('con5articletranslations').innerHTML+'&id='+elid;
	eurl = eurl.replace(/&amp;/g, '&');
	elxPopup(eurl, 650, 400, 'arttranspreview', 'yes');
}

function con5Filter(catid, rest_options) {
	var eurl = document.getElementById('articlestbl').getAttribute('data-listpage')+'?catid='+catid;
	if (rest_options != '') { eurl += '&'+rest_options; }
	window.location.href = eurl;
}


function con5UnFilter(rest_options) {
	var eurl = document.getElementById('articlestbl').getAttribute('data-listpage');
	if (rest_options != '') { eurl += '?'+rest_options; }
	window.location.href = eurl;
}


function con5CopyMoveArticles(itemaction) {
	if (typeof itemaction === 'undefined') { return; }
	if (itemaction != 'copy') { itemaction = 'move'; }
	var ids = elx5SelectedTableItem('articlestbl', true);
	if (ids === false) { return; }
	if (ids.length == 0) { return; }
	document.getElementById('cpmvartids').value = ids.join(',');
	document.getElementById('cpmvtask').value = itemaction;
	document.getElementById('cpmvartcategory').selectedIndex = 0;
	if (itemaction == 'copy') {
		document.getElementById('elx5_modaltitleeartcp').innerHTML = document.getElementById('fmcpmvarticle').getAttribute('data-lngcopy');
		document.getElementById('eartcpmvsave').innerHTML = document.getElementById('fmcpmvarticle').getAttribute('data-lngcopy');
	} else {
		document.getElementById('elx5_modaltitleeartcp').innerHTML = document.getElementById('fmcpmvarticle').getAttribute('data-lngmove');
		document.getElementById('eartcpmvsave').innerHTML = document.getElementById('fmcpmvarticle').getAttribute('data-lngmove');
	}
	document.getElementById('elx5_modalmessageeartcp').className = 'elx5_invisible';
	elx5ModalOpen('eartcp');
}


function con5CopyMoveArtSave() {
	var edata = {};
	edata.ids = document.getElementById('cpmvartids').value;
	if (edata.ids == '') { return; }
	var sObj = document.getElementById('cpmvartcategory');
	edata.catid = sObj.options[sObj.selectedIndex].value;
	edata.catid = parseInt(edata.catid, 10);
	if (edata.catid < 0) { return; }

	edata.rnd = Math.floor((Math.random()*100)+1);
	var eurl = document.getElementById('fmcpmvarticle').action+''+document.getElementById('cpmvtask').value;

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

	var successfunc = function(xreply) {
		elx5StopPageLoader();
		try {
			var jsonObj = JSON.parse(xreply);
		} catch(e) {
			document.getElementById('elx5_modalmessageeartcp').innerHTML = 'Could not save articles!';
			document.getElementById('elx5_modalmessageeartcp').className = 'elx5_error';
			return false;
		}
		if (parseInt(jsonObj.success, 10) < 1) {
			if (jsonObj.message != '') {
				document.getElementById('elx5_modalmessageeartcp').innerHTML = jsonObj.message;
			} else {
				document.getElementById('elx5_modalmessageeartcp').innerHTML = 'Action failed!';
			}
			document.getElementById('elx5_modalmessageeartcp').className = 'elx5_error';
		} else {
			elx5ModalClose('eartcp');
			location.reload(true);
		}
	}
	var errorfunc = function (XMLHttpRequest, textStatus, errorThrown) {
		elx5StopPageLoader();
		document.getElementById('elx5_modalmessageeartcp').innerHTML = 'Error! '+errorThrown; 
		document.getElementById('elx5_modalmessageeartcp').className = 'elx5_error';
	}

	elx5StartPageLoader();
	elxAjax('POST', eurl, edata, null, null, successfunc, errorfunc);
}


function con5ArticleShare(socialmedia) {
	var elid = elx5SelectedTableItem('articlestbl', false);
	if (elid === false) { return; }
	elid = parseInt(elid, 10);
	if (elid < 1) { return; }
	var sharelink = document.getElementById('articlestbl').getAttribute('data-inpage')+'share.html?type='+socialmedia+'&id='+elid;
	elxPopup(sharelink, 700, 450, 'share', 'yes');
}


function con5CronJobs() {
	var successfunc = function(xreply) {
		elx5StopPageLoader();
		try {
			var jsonObj = JSON.parse(xreply);
		} catch(e) {
			alert(e.message);
			return false;
		}
		if (parseInt(jsonObj.success, 10) == 1) {
			location.reload(true);
		} else {//error
			if (jsonObj.errormsg != '') {
				alert(jsonObj.errormsg);
			} else {
				alert('Action failed!');
			}
		}
	}
	var errorfunc = function (XMLHttpRequest, textStatus, errorThrown) {
		elx5StopPageLoader();
		alert('Error! '+errorThrown); 
	}

	var edata = {};
	edata.rnd = Math.floor((Math.random()*100)+1);
	var eurl = document.getElementById('con5articlecron').innerHTML;
	elx5StartPageLoader();
	elxAjax('POST', eurl, edata, null, null, successfunc, errorfunc);
}


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

	var successfunc = function(xreply) {
		elx5StopPageLoader();
		try {
			var jsonObj = JSON.parse(xreply);
		} catch(e) {
			alert(e.message);
			return false;
		}
		if (parseInt(jsonObj.success, 10) == 1) {
			document.getElementById('con5pubcombox'+cid).className = 'elx5_invisible';
		} else {//error
			if (jsonObj.errormsg != '') {
				alert(jsonObj.errormsg);
			} else {
				alert('Action failed!');
			}
		}
	}
	var errorfunc = function (XMLHttpRequest, textStatus, errorThrown) {
		elx5StopPageLoader();
		alert('Error! '+errorThrown); 
	}

	let rnd = Math.floor((Math.random()*100)+1);
	var edata = {'id': cid, 'rnd': rnd };
	var eurl = document.getElementById('commentstbl').getAttribute('data-inpage')+'publishcomment';
	elx5StartPageLoader();
	elxAjax('POST', eurl, edata, null, null, successfunc, errorfunc);
}


/* DELETE COMMENT (AJAX) */
function con5DeleteComment(cid) {
	if (isNaN(cid)) { return false; }
	cid = parseInt(cid, 10);
	if (cid < 1) { return false; }
	var tblObj = document.getElementById('commentstbl');
	var prompttxt = tblObj.getAttribute('data-deletelng');
	if (confirm(prompttxt)) {
		let rnd = Math.floor((Math.random()*100)+1);
		var edata = {'id': cid, 'rnd': rnd };
		var eurl = tblObj.getAttribute('data-inpage')+'deletecomment';
		var successfunc = function(xreply) {
			elx5StopPageLoader();
			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('datarow'+cid)) {
					var rowObj = document.getElementById('datarow'+cid);
					tblObj.deleteRow(rowObj.rowIndex);
				}
			}
		};
		var errorfunc = function (XMLHttpRequest, textStatus, errorThrown) {
			elx5StopPageLoader();
			alert('Action failed! '+errorThrown);
		};
		elx5StartPageLoader();
		elxAjax('POST', eurl, edata, null, null, successfunc, errorfunc);
	}
}

function con5OnLoad() {
	if (document.getElementById('con5copyarticles')) {
		document.getElementById('con5copyarticles').addEventListener('click', function(e){ e.preventDefault(); con5CopyMoveArticles('copy'); }, false);
	}
	if (document.getElementById('con5movearticles')) {
		document.getElementById('con5movearticles').addEventListener('click', function(e){ e.preventDefault(); con5CopyMoveArticles('move'); }, false);
	}
	if (document.getElementById('con5delarticles')) {
		document.getElementById('con5delarticles').addEventListener('click', function(e){ e.preventDefault(); elx5DeleteTableRows('articlestbl', false); }, false);
	}
	if (document.getElementById('con5transarticles')) {
		document.getElementById('con5transarticles').addEventListener('click', function(e){ e.preventDefault(); con5ArticleTrans('articlestbl'); }, false);
	}
	if (document.getElementById('con5twitarticles')) {
		document.getElementById('con5twitarticles').addEventListener('click', function(e){ e.preventDefault(); con5ArticleShare('twitter'); }, false);
	}
	if (document.getElementById('con5fbarticles')) {
		document.getElementById('con5fbarticles').addEventListener('click', function(e){ e.preventDefault(); con5ArticleShare('facebook'); }, false);
	}
	if (document.getElementById('con5crons')) {
		document.getElementById('con5crons').addEventListener('click', function(e){ e.preventDefault(); con5CronJobs(); }, false);
	}
	if (document.getElementById('con5searcharticles')) {
		document.getElementById('con5searcharticles').addEventListener('click', function(e){ e.preventDefault(); elx5Toggle('artsearchoptions'); }, false);
	}
	if (document.getElementById('eartcpmvsave')) {
		document.getElementById('eartcpmvsave').addEventListener('click', function(){ con5CopyMoveArtSave(); }, false);
	}
	if (document.getElementById('con5artcopyseo')) {
		document.getElementById('con5artcopyseo').addEventListener('click', function(e){ e.preventDefault(); elx5CopyToClipboard('earseolinktext'); }, false);
	}
	if (document.getElementById('con5cattrans')) {
		document.getElementById('con5cattrans').addEventListener('click', function(e){ e.preventDefault(); con5CategoryTrans('categoriestbl'); }, false);
	}
	if (document.getElementById('con5catmoveu')) {
		document.getElementById('con5catmoveu').addEventListener('click', function(e){ e.preventDefault(); con5MoveCategory(0); }, false);
	}
	if (document.getElementById('con5catmoved')) {
		document.getElementById('con5catmoved').addEventListener('click', function(e){ e.preventDefault(); con5MoveCategory(1); }, false);
	}
	if (document.getElementById('con5delcats')) {
		document.getElementById('con5delcats').addEventListener('click', function(e){ e.preventDefault(); elx5DeleteTableRows('categoriestbl', false); }, false);
	}
	let statusa = document.querySelectorAll('a.elx5_statusicon');//works on both articles and categories
	if (statusa.length > 0) {
		for (let z=0; z < statusa.length; z++) {
			if (!statusa[z].hasAttribute('data-id')) { continue; }
			statusa[z].addEventListener('click', function(e){
				let aid = this.getAttribute('data-id');
				e.preventDefault();
				elx5ToggleStatus(aid, this);
			}, false);
		}
	}
	let catsf = document.querySelectorAll('a[data-restoptions]');
	if (catsf.length > 0) {
		for (let w=0; w < catsf.length; w++) {
			if (catsf[w].hasAttribute('data-catid')) {
				catsf[w].addEventListener('click', function(e){
					let crestoptions = this.getAttribute('data-restoptions');
					let ccatid = catsf[w].getAttribute('data-catid');
					e.preventDefault();
					con5Filter(ccatid, crestoptions);
				}, false);
			} else {
				catsf[w].addEventListener('click', function(e){
					let crestoptions = this.getAttribute('data-restoptions');
					e.preventDefault();
					con5UnFilter(crestoptions);
				}, false);
			}
		}
	}
	let oids = document.querySelectorAll('input[data-setordid]');
	if (oids.length > 0) {
		for (let w=0; w < oids.length; w++) {
			oids[w].addEventListener('change', function(){
				let setordid = this.getAttribute('data-setordid');
				elx5SetOrdering('setordering'+setordid, setordid, 0);
			}, false);	
		}
	}
	let comlinks = document.querySelectorAll('a[data-pdcomment]');
	if (comlinks.length > 0) {
		for (let w=0; w < comlinks.length; w++) {
			comlinks[w].addEventListener('click', function(e){
				let comid = this.getAttribute('data-pdcomment');
				let act = this.getAttribute('data-act');
				e.preventDefault();
				if (act == 'delete') {
					con5DeleteComment(comid);
				} else {
					con5PublishComment(comid);
				}
			}, false);
		}
	}
}
