/* --------------------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------------------- */
/* ------------------------------------------ COMPONENTE --------------------------------------- */
/* --------------------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------------------- */
function JSObj_cms_content_list()
{
	this.posId = 1;
	this.pageId = '';
	this.compId = 1;
	this.lngId = 'es';
	this.currentPage = '';
	this.bOK = false;

	// Recarga la posición
	this.reload = function () {
		var objReloader = new WbePositionReloader();
		objReloader.posId = this.posId;
		objReloader.pageId = this.pageId;
		objReloader.reload();
	};
	
	this.search = function () {
	
		/*var objReloader = new WbePositionReloader();
		objReloader.posId = this.posId;
		var asParams = new Array();
		asParams[0] = 'search_text_' + this.posId + '=' + 
				eval('document.InmediaFrm.search_text_' + this.posId).value;
		asParams[1] = 'ordering_type_' + this.posId + '=' + 
				eval('document.InmediaFrm.ordering_type_' + this.posId).value;
		objReloader.reload(asParams);*/
		
		var sUrl = this.currentPage + '?'
		sUrl += 'search_text_' + this.posId + '=' + 
				eval('document.InmediaFrm.search_text_' + this.posId).value;
		sUrl += '&';
		/*sUrl += 'ordering_type_' + this.posId + '=' + 
				eval('document.InmediaFrm.ordering_type_' + this.posId).value;
		sUrl += '&';
		sUrl += 'search_published_' + this.posId + '=' + 
				eval('document.InmediaFrm.search_published_' + this.posId).value;*/

		document.location.href = sUrl;
	
	};

	this.goToPage = function (index) {
		var sUrl = this.currentPage + '?';
		/*sUrl += 'search_text_' + this.posId + '=' + 
				eval('document.InmediaFrm.search_text_' + this.posId).value;
		sUrl += '&';
		sUrl += 'ordering_type_' + this.posId + '=' + 
				eval('document.InmediaFrm.ordering_type_' + this.posId).value;
		sUrl += '&';
		sUrl += 'search_published_' + this.posId + '=' + 
				eval('document.InmediaFrm.search_published_' + this.posId).value;
		sUrl += '&';*/
		sUrl += 'page_' + this.posId + '=' + index;

		document.location.href = sUrl;
	};

	// Lanza un evento de cambio de contenido
	this.changeStatus = function (sEvent, sCVId) {
		var xmlObj;
		var oAjax = new WBE_AjaxClass();
		oAjax.clear();
		oAjax.addPostParameter("ver_id", sCVId);
		xmlObj = oAjax.throwEventXML(sEvent);
		this.processXml(oAjax, xmlObj, sCVId);
	}
	
	// Publica la versión
	this.publishVersion = function (sCVId) 
	{
		this.showSavingMsg();
		window.scroll(0, 0);
		this.changeStatus("cms_publish_version", sCVId);
		this.hideSavingMsg();
	};

	// Despublica la versión
	this.unpublishVersion = function (sCVId) 
	{
		this.showSavingMsg();
		window.scroll(0, 0);
		this.changeStatus("cms_unpublish_version", sCVId);
		this.hideSavingMsg();
	};
	
	// Publica la versión y recarga la página
	this.publishVersionAndReload = function (sCVId) 
	{
		this.showSavingMsg();
		window.scroll(0, 0);
		this.changeStatus("cms_publish_version", sCVId);
		this.hideSavingMsg();
		this.reload();
	};

	// Despublica la versión y recarga la página
	this.unpublishVersionAndReload = function (sCVId) 
	{
		this.showSavingMsg();
		window.scroll(0, 0);
		this.changeStatus("cms_unpublish_version", sCVId);
		this.hideSavingMsg();
		this.reload();
	};

	// como el 'unpublish' pero además desactiva el usuario
	this.deactivateUser = function (sVerId) {
		if (!confirm("Se va a desactivar la cuenta del usuario.\n ¿Está seguro que quiere continuar?")) return;
		var oAjax = new WBE_AjaxClass();
		oAjax.clear();
		oAjax.addPostParameter("ver_id", sVerId);
		xmlObj = oAjax.throwEventXML("cms_unpublish_version");
		this.processXml(oAjax, xmlObj, sVerId);
		if (this.bOK) {
			oAjax.clear();
			oAjax.addPostParameter("ver_id", sVerId);
			oAjax.throwEvent2("user_block_adm_cv");
		}
		this.reload();
	};
	
	// como el 'publish' pero además activa el usuario relacionado con el contenido
	this.activateUser = function (sVerId) {
		var oAjax = new WBE_AjaxClass();
		oAjax.clear();
		oAjax.addPostParameter("ver_id", sVerId);
		xmlObj = oAjax.throwEventXML("cms_publish_version");
		this.processXml(oAjax, xmlObj, sVerId);
		if (this.bOK) {
			oAjax.clear();
			oAjax.addPostParameter("ver_id", sVerId);
			oAjax.throwEvent2("user_activate_adm_cv");
		}
		this.reload();
	};
	
	// Cancela la versión
	this.cancelVersion = function (sCVId) 
	{
		if (!confirm("¿Estas seguro que deseas cancelar esta versión?.")) return;
		
		this.showSavingMsg();
		window.scroll(0, 0);
		this.changeStatus("cms_cancel_version", sCVId);
		this.hideSavingMsg();
	};

	// Borra la versión
	this.deleteVersion = function (sCVId) 
	{
		if (!confirm("SE ELIMINARÁ ESTE CONTENIDO EN TODOS SUS IDIOMAS.\n ¿Está seguro que quiere continuar?")) return;
		
		this.showSavingMsg();
		window.scroll(0, 0);
		this.changeStatus("cms_delete_version", sCVId);
		this.hideSavingMsg();
	};

	// Despublica la versión en un idiooma
	this.unpublishVersionLng = function (sCVId) 
	{
		this.showSavingMsg();
		window.scroll(0, 0);
		this.changeStatus("cms_unpublish_version_lng", sCVId);
		this.hideSavingMsg();
	};

	// Publica la versión en un idiomae
	this.publishVersionLng = function (sCVId) 
	{
		this.showSavingMsg();
		window.scroll(0, 0);
		this.changeStatus("cms_publish_version_lng", sCVId);
		this.hideSavingMsg();
	};

	// Despublica la versión en un idioma y recarga
	this.unpublishVersionLngAndReload = function (sCVId) 
	{
		this.showSavingMsg();
		window.scroll(0, 0);
		this.changeStatus("cms_unpublish_version_lng", sCVId);
		this.hideSavingMsg();
		document.location.reload();
	};

	// Publica la versión en un idioma y recarga
	this.publishVersionLngAndReload = function (sCVId) 
	{
		this.showSavingMsg();
		window.scroll(0, 0);
		this.changeStatus("cms_publish_version_lng", sCVId);
		this.hideSavingMsg();
		document.location.reload();
	};

	
	// Procesa la salida XML
	this.processXml = function (oAjax, xmlObj, sCVId) {
		var verId, sStatus, sStatusLng;
		
		this.bOK = false;
		if (xmlObj) {
			verId = oAjax.getXMLNodeValue(xmlObj, "ver_id");
			sStatus = oAjax.getXMLNodeValue(xmlObj, "stat");
			sStatusLng = oAjax.getXMLNodeValue(xmlObj, "stat_lng");
			if (verId == sCVId) {
				this.bOK = true;
				if (sStatus == 'deleted' || sStatus == 'cancelled') {
					document.location.reload();
				} else if (sStatusLng == 'published') {
					this.showUnPublish(verId);
				} else {
					this.showPublish(verId);
				}
			}
		}
		if (this.bOK) this.showOkMsg();
		else this.showNOkMsg();
	};

	// Muestra el estado no publicado	
	this.showPublish = function (verId) {
		var l1, l2, l3, l4;
		l1 = document.getElementById('publish_' + verId);
		l2 = document.getElementById('unpublish_' + verId);
		l3 = document.getElementById('status_nok_' + verId);
		l4 = document.getElementById('status_ok_' + verId);
		if (l1) l1.style.display = '';
		if (l2) l2.style.display = 'none';
		if (l3) l3.style.display = '';
		if (l4) l4.style.display = 'none';
	};
	
	// Muestra en el estado no publicado.
	this.showUnPublish = function (verId) {
		var l1, l2, l3, l4;
		l1 = document.getElementById('publish_' + verId);
		l2 = document.getElementById('unpublish_' + verId);
		l3 = document.getElementById('status_nok_' + verId);
		l4 = document.getElementById('status_ok_' + verId);
		if (l1) l1.style.display = 'none';
		if (l2) l2.style.display = '';
		if (l3) l3.style.display = 'none';
		if (l4) l4.style.display = '';
	};
	
	this.showOkMsg = function () {
		if (document.getElementById('message_ok')) document.getElementById('message_ok').style.display = '';
	};
	
	this.hideOkMsg = function () {
		if (document.getElementById('message_ok')) document.getElementById('message_ok').style.display = 'none';
	};
	
	this.showNOkMsg = function () {
		if (document.getElementById('message_nok')) document.getElementById('message_nok').style.display = '';
	};
	
	this.hideNOkMsg = function () {
		if (document.getElementById('message_nok')) document.getElementById('message_nok').style.display = 'none';
	};

	this.showSavingMsg = function () {
		if (document.getElementById('message_saving')) document.getElementById('message_saving').style.display = '';
	};
	
	this.hideSavingMsg = function () {
		if (document.getElementById('message_saving')) document.getElementById('message_saving').style.display = 'none';
	};
	
	
	// PARA LAS ACCIONES MASIVAS
	
	this.toggleCheck = function (elem_name) {
		var elem = eval('document.InmediaFrm.' + elem_name);
		if (elem.length) {
			var bDo = !elem[0].checked;
			for (var i = 0; i < elem.length; i++) {
				elem[i].checked = bDo;
			}
		} else {
			elem.checked = !elem.checked;
		}	
	};
	
	this.getChecked = function (elem_name) {
		var elem = eval('document.InmediaFrm.' + elem_name);
		var sValue = '';
		if (elem.length) {
			for (var i = 0; i < elem.length; i++) {
				if (elem[i].checked) {
					if (sValue != '') sValue += ',';
					sValue += elem[i].value;
				}
			}
		} else {
			if (elem.checked) sValue = elem.value;
		}	
		return sValue;
	};

	// Lanza un evento de cambio de contenido
	this.changeStatusMulti = function (sEvent, sCVId) {
		var xmlObj;
		var oAjax = new WBE_AjaxClass();
		oAjax.clear();
		oAjax.addPostParameter("ver_ids", sCVId);
		xmlObj = oAjax.throwEventXML(sEvent);
		this.processMultiXml(oAjax, xmlObj, sCVId);
	}

	// Despublica la versión en un idioma y recarga
	this.unpublishVersionLngAndReloadMulti = function (sCVId) 
	{
		this.showSavingMsg();
		window.scroll(0, 0);
		this.changeStatusMulti("cms_unpublish_version_lngs", sCVId);
		this.hideSavingMsg();
		document.location.reload();
	};

	// Publica la versión en un idioma y recarga
	this.publishVersionLngAndReloadMulti = function (sCVId) 
	{
		this.showSavingMsg();
		window.scroll(0, 0);
		this.changeStatusMulti("cms_publish_version_lngs", sCVId);
		this.hideSavingMsg();
		document.location.reload();
	};
	
	// Procesa la salida XML
	this.processMultiXml = function (oAjax, xmlObj, sCVId) {
		var verIds;
		
		this.bOK = false;
		if (xmlObj) {
			this.bOK = true;
		}
		if (this.bOK) this.showOkMsg();
		else this.showNOkMsg();
	};

	// Borra la versión
	this.deleteVersionsAndReload = function (sCVId) 
	{
		if (!confirm("SE ELIMINARÁN ESTOS CONTENIDOS EN TODOS SUS IDIOMAS.\n ¿Está seguro que quiere continuar?")) return;
		
		this.showSavingMsg();
		window.scroll(0, 0);
		this.changeStatusMulti("cms_delete_versions", sCVId);
		this.hideSavingMsg();
		document.location.reload();
	};
	
	// Exporta la versión a PDF
	this.exportPdfVersion = function(sVerId) {
		var oAjax = new WBE_AjaxClass();
		var oW, sName;
		oAjax.clear();
		oAjax.addPostParameter("ver_id", sVerId);
		xmlObj = oAjax.throwEventXML("cms_pdf_export");
		if (xmlObj) {
			sName = oAjax.getXMLNodeValue(xmlObj, "n");
			if (sName!='') oW = window.open("/config/temp/" + sName, "", "");
		} else {
			alert('Funcionalidad no configurada en este proyecto.');
		}
	}
	
};

/* --------------------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------------------- */
/* ------------------------------------------ EDICIÓN DEL COMPONENTE --------------------------------------- */
/* --------------------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------------------- */
function JSObj_cms_content_list_edit()
{
	this.posId = 1;
	this.compId = 1;
	this.parentPosId = 1;
	this.varName = '';
	this.lngId = 'es';

	// Oculta todos los campos de formulario SELECT por el problema que tiene el IE. (como en el back azul)
	this.hideSelectBoxes = function () {
		for(var i = 0; i < document.forms.length; i++) {
			for(var e = 0; e < document.forms[i].length; e++){
				if(document.forms[i].elements[e].tagName == "SELECT")
					document.forms[i].elements[e].style.visibility="hidden";
			} // for e
		} // for i
	};
	
	// Muestra todos los campos de formulario SELECT por el problema que tiene el IE. (como en el back azul)
	this.displaySelectBoxes = function () {
		for(var i = 0; i < document.forms.length; i++) {
			for(var e = 0; e < document.forms[i].length; e++){
				if(document.forms[i].elements[e].tagName == "SELECT")
					document.forms[i].elements[e].style.visibility="visible";
			} // for e
		} // for i
	};
	
	this.save = function () {
		window.parent.objLoading.showWindow();
		this.hideSelectBoxes();
		setTimeout(this.varName + '.save2();', 0);
	}

	this.save2 = function () {
		var objConfig = new WBEConfigMgr();
		objConfig.posId = this.posId;
		objConfig.compId = this.compId;
		//objConfig.cache_delete_prefix = '|travel_offerings/offering_form_edit.' + this.posId + '|';
		
		objConfig.saveConfig();
	
		var objReloader = new WbePositionReloader();
		objReloader.posId = this.parentPosId;
		objReloader.reload();

		window.parent.___wbeADMIN.hidePopUp();
		window.parent.objLoading.hideWindow();
		this.displaySelectBoxes();
	};
};
