/*
*	**** JS DE FRONT *****
*/
function JSObj_content_list_1()
{
	this.posId = 1;
	this.compId = 1;
	this.pageId = null;
	//this.createGoToUrl = null;
	this.lngId = 'es';
	this.search_codes = '';
	this.currentPage = '';
	this.bOK = false;

/* ****  NEWS ********* */	


/**************************************************************************************************/
/*******FUNCIONES DEL FICHERO adm_list.js FUSIÓN PARA LOS NUEVOS COMPONENTES***************/
/**************************************************************************************************/
	//Guarda valoración del contenido
	//Sromero and Amangin 17 de marzo de 2008
	this.saveContentValoration = function(sVerId,inputName,intType) {
		this.inputValueName = document.getElementsByName(inputName);				
		var value=this.inputValue();
							
		var oAjax = new WBE_AjaxClass();
		oAjax.clear();	
		oAjax.addPostParameter('pos_id', this.posId);
		oAjax.addPostParameter('cnt_id', sVerId);
		oAjax.addPostParameter('cntval_type', intType);
		oAjax.addPostParameter('cntval_value', value);			
		this.preValoration();
		var xmlObj = oAjax.throwEventXML("cms_content_valoration_add");
		this.postValoration(xmlObj);
	}
	this.preValoration = function() {};
	this.postValoration = function(xmlObj){
		var oAjax = new WBE_AjaxClass();
		var sStatus = oAjax.getXMLNodeValue(xmlObj, "c");
		switch (sStatus) {
			case "OK":
				break;
			case "ERROR":
				alert('Se ha cometido un error inesperado valorando este contenido, por favor intentelo mas tarde.');
				break;
			default:
				alert(sStatus);
				break;
		}
	};
	this.inputValueName = null;
	this.inputValue =function () {
		var value;
		var type= this.inputValueName[0].type;
		switch (type) {
		case "select":
			value = this.inputValueName[0].options[this.inputValueName[0].selectedIndex].value;
			break;
		case "radio":
			for (var i=0;i<this.inputValueName.length;i++) {
				if(this.inputValueName[i].checked) value=this.inputValueName[i].value;
			}
			break;
		default:
			value=this.inputValueName[0].value;
		}
		return value;
	}
	//FIN VALORACIONES
	
	this.goToPage = function (index) {
		var sUrl = this.currentPage + '?';
		/*sUrl += 'search_text_' + this.posId + '=' + 
				eval('document.WBForm.search_text_' + this.posId).value;
		sUrl += '&';
		sUrl += 'ordering_type_' + this.posId + '=' + 
				eval('document.WBForm.ordering_type_' + this.posId).value;
		sUrl += '&';
		sUrl += 'search_published_' + this.posId + '=' + 
				eval('document.WBForm.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.publishVersion(sCVId);
		this.reload();
	};

	// Despublica la versión y recarga la página
	this.unpublishVersionAndReload = function (sCVId) 
	{
		this.unpublishVersion(sCVId);
		this.reload();
	};

	
	// Pasa el CV a estado ''delete'' y desactiva el usuario
	this.deleteUser = function (sVerId) {
		if (!confirm("Se va a eliminar la cuenta del usuario.\n ¿Está seguro que quiere continuar?")) return;
		var oAjax = new WBE_AjaxClass();
		oAjax.clear();
		oAjax.addPostParameter("ver_id", sVerId);
		var xmlObj = oAjax.throwEventXML("user_delete_adm_cv");
		/*this.processXml(oAjax, xmlObj, sVerId);
		if (this.bOK) {
			// Mostrar un mensaje, talvez??
		}*/
		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 version en un idioma
	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 idioma
	this.publishVersionLng = function (sCVId) 
	{
		this.showSavingMsg();
		window.scroll(0, 0);
		this.changeStatus("cms_publish_version_lng", sCVId);
		this.hideSavingMsg();
	};
	// Despublica la version en un idioma y recarga
	this.unpublishVersionLngAndReload = function (sCVId) 
	{
		this.unpublishVersionLng(sCVId);
		this.reload();
	};

	// Publica la versión en un idioma y recarga
	this.publishVersionLngAndReload = function (sCVId) 
	{
		this.publishVersionLng(sCVId);
		this.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 () {
		document.getElementById('message_ok').style.display = '';
	};
	
	this.hideOkMsg = function () {
		document.getElementById('message_ok').style.display = 'none';
	};
	
	this.showNOkMsg = function () {
		document.getElementById('message_nok').style.display = '';
	};
	
	this.hideNOkMsg = function () {
		document.getElementById('message_nok').style.display = 'none';
	};

	this.showSavingMsg = function () {
		document.getElementById('message_saving').style.display = '';
	};
	
	this.hideSavingMsg = function () {
		document.getElementById('message_saving').style.display = 'none';
	};


/**************************************************************************************************/
/**************************FIN FUNCIONES DEL FICHERO adm_list.js**********************************/
/**************************************************************************************************/
	
	// Recarga la posición
	this.reload = function () {
		document.location.reload();
/*		
		try {
			var objReloader = new WbePositionReloader();
			objReloader.posId = this.posId;
			objReloader.pageId = this.pageId;
			objReloader.reload();
		} catch (e) {
			document.location.reload();
		}
*/
	};
	
	this.search = function () {
		// Recogemos el valor a buscar
		var valueInput = eval('document.forms[0].f0_v1_' + this.posId);
		var value = valueInput.value;
		// Montamos el filtro
		var sf = "<filter>";
		sf += "<sf id='f0' type='text' op='like'>";
		sf += "<fields>" + this.search_codes + "</fields><v1>" + value + "</v1></sf>";
		sf += "</filter>";
		// Realizamos la bsqueda
		var filterInput = eval('document.forms[0].filter_xml_' + this.posId);
		filterInput.value = sf;
		//document.forms[0].filter_xml_.value = sf;
		//alert(document.forms[0].filter_xml_.value);
		document.forms[0].action = document.location.href;
		document.forms[0].submit();
	}
	
	// Despublica una version (LA ELIMINA PARA MANTENER COMPATIBILIDAD)
	this.unpublish = function (sVerId) {
		if (!confirm("SE ELIMINARÁ ESTE CONTENIDO EN TODOS SUS IDIOMAS.\n ¿Está seguro que quiere continuar?")) return;
		var oAjax = new WBE_AjaxClass();
		oAjax.clear();
		oAjax.addPostParameter("ver_id", sVerId);
		oAjax.throwEvent2("cms_delete_version");
		this.reload();
	};
	
	// Despublica una version en un idioma
	this.unpublishLng = function (sVerId) {
		var oAjax = new WBE_AjaxClass();
		var oAOk = document.getElementById("pub_" + this.posId + "_" + sVerId);
		var oANok = document.getElementById("unpub_" + this.posId + "_" + sVerId);
		oAjax.clear();
		oAjax.addPostParameter("ver_id", sVerId);
		oAjax.throwEvent2("cms_unpublish_version_lng");
		if (oAOk && oANok) {
			oAOk.style.display = "inline";		
			oANok.style.display = "none";		
		}
	};

	// Despublica una version en un idioma
	this.publishLng = function (sVerId) {
		var oAjax = new WBE_AjaxClass();
		var oAOk = document.getElementById("pub_" + this.posId + "_" + sVerId);
		var oANok = document.getElementById("unpub_" + this.posId + "_" + sVerId);
		oAjax.clear();
		oAjax.addPostParameter("ver_id", sVerId);
		oAjax.throwEvent2("cms_publish_version_lng");
		if (oAOk && oANok) {
			oAOk.style.display = "none";		
			oANok.style.display = "inline";		
		}
	};

	// Crea un nuevo contenido	
	this.createContent = function (sCTId, sCatId, sParentId) {
		var oAjax = new WBE_AjaxClass();
		oAjax.clear();
		oAjax.addPostParameter('content_ct_id', sCTId);
		if (sCatId != undefined && sCatId != '') {
			oAjax.addPostParameter("category_ids", sCatId);
		}
		if (sParentId != undefined && sParentId != '') {
			oAjax.addPostParameter("cnt_parent_relship", sParentId);
		}
		return oAjax.throwEvent2("cms_insert_content_default", false);
	}
	
	// Crea un nuevo contenido	
	this.create = function (sCTId, sCatId) {
		this.createContent(sCTId, sCatId);
		this.reload();
	};
	
	// Crea un nuevo contenido y recarga la pagina
	this.createAndRepage = function (sCTId, sCatId) {
		this.createContent(sCTId, sCatId);
		document.location.href = document.location.href;
	};
	
	// Crea un nuevo contenido, lo relaciona con el padre y recarga la página
	this.createWithParentAndRepage = function (sCTId, sCatId, sParentId) {
		this.createContent(sCTId, sCatId, sParentId);
		document.location.href = document.location.href;
	};
	
	
	// Crea un nuevo contenido y va a la página de edición
	this.createAndEdit = function (sCTId, sEditUrl, sDestEditVar, sCurrentResourceId, sUrlParams) {
		var oAjax = new WBE_AjaxClass();
		var xmlObj = this.createContent(sCTId);
		if (xmlObj) {
			var verId = oAjax.getXMLNodeValue(xmlObj, "ver_id");
			if (verId != 'undefined' && verId != '' && verId != null){
				//href="http://__link_resource_{$destination_edit_link}__/?{$destination_edit_variable}={Reference}&amp;content_type_id={$content_type_id}&amp;back_link={$current_resource_id}&amp;back_params={$url_params}"
				document.location.href = sEditUrl + '?' + sDestEditVar + '=' + verId + '&content_type_id=' + sCTId + '&back_link=' + sCurrentResourceId + '&back_params=' + sUrlParams;
			} else {
				this.showNOkMsg();
			}
		} else {
			this.showNOkMsg();
		}
	};
	
	
	// Crea un nuevo contenido, lo relaciona con el padre y recarga la página
	this.createWithParentAndEdit = function (sCTId, sCatId, sParentId, sEditUrl, sDestEditVar, sCurrentResourceId, sUrlParams) {
		var oAjax = new WBE_AjaxClass();
		var xmlObj = this.createContent(sCTId, sCatId, sParentId)
		if (xmlObj) {
			var verId = oAjax.getXMLNodeValue(xmlObj, "ver_id");
			if (verId != 'undefined' && verId != '' && verId != null){
				//href="http://__link_resource_{$destination_edit_link}__/?{$destination_edit_variable}={Reference}&amp;content_type_id={$content_type_id}&amp;back_link={$current_resource_id}&amp;back_params={$url_params}"
				document.location.href = sEditUrl + '?' + sDestEditVar + '=' + verId + '&content_type_id=' + sCTId + '&back_link=' + sCurrentResourceId + '&back_params=' + sUrlParams;
			} else {
				this.showNOkMsg();
			}
		} else {
			this.showNOkMsg();
		}
	};
	
	// Exporta el listado a Excel mediante una XSL
	this.Export2Excel = function (sXmlProcess,sOutputName,sDivStatusPrefix) {
		var oAjax = new WBE_AjaxClass();
		oAjax.clear();
		oAjax.addPostParameter('component_position_id', this.posId);
		oAjax.addPostParameter('xml_process_code', sXmlProcess);
		oAjax.addPostParameter('output_file_name', sOutputName);
		
		var xmlObj = oAjax.throwEventXML("cms_list_excel_export", false);
		if (xmlObj) {
			var ok = oAjax.getXMLNodeValue(xmlObj, "ok");
			if (ok != 'undefined' && ok != '' && ok != null){
				document.getElementById(sDivStatusPrefix + '_ok').style.display = '';
			} else {
				document.getElementById(sDivStatusPrefix + '_error').style.display = '';
			}
		} else {
			document.getElementById(sDivStatusPrefix + '_error').style.display = '';
		}
	};
}


/*
*	***** JS DE EDICION *****
*/
function JSObj_structured_content_list_edit()
{
	this.posId = 1;
	this.compId = 1;
	this.parentPosId = 1;
	this.varName = '';
	this.currentSelect = 1;
	this.lngId = 'es';
	this.config_xml_process = 'xml_process';

	this.save = function() {

		window.parent.objLoading.showWindow();
		this.hideSelectBoxes();
		
		var objConfig = new WBEConfigMgr();
		objConfig.posId = this.posId;
		objConfig.compId = this.compId;
		objConfig.cache_delete_prefix = '|wbe.xsl/struct_content_list.' + this.posId + '|';
		
		objConfig.saveConfig();
	
		var objReloader = new WbePositionReloader();
		objReloader.posId = this.parentPosId;
		objReloader.reload();

		window.parent.objLoading.hideWindow();
		window.parent.___wbeADMIN.hidePopUp();
		this.displaySelectBoxes();
	};

	// 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
	};
	
	// Reemplaza la descripción seleccionada para el tipo de contenido
	this.changeCTDesc = function () {
		var dest_div = document.getElementById('ct_desc_' + this.posId);
		var elem_sel = eval('document.forms[0].config_content_type_id_' + this.posId);
		dest_div.innerHTML = eval('aDescs' + this.posId)[elem_sel.selectedIndex];
	};

	// Mustra la ruta de la xsl al seleccionar algún proceso
	this.changeProcess = function () {
		var dest_div = document.getElementById('xsl_path_' + this.posId);
		var elem_sel = eval('document.WBForm.config_' + this.config_xml_process + '_' + this.posId);
		var from_div = document.getElementById('xsl_path_' + this.posId + '_' + elem_sel.selectedIndex);
		dest_div.innerHTML = from_div.innerHTML;
	};

	this.save1 = function () {
		eval('document.forms[0].codes_' + this.posId).value = 'num_pages,num_elems,content_type_id,content_type_request_var,category_id,xml_process,category_variable,category_join_type';
		this.save();
	};

	this.save2 = function () {
		eval('document.forms[0].codes_' + this.posId).value = 'destination_link,destination_variable,relationship_deep,disable_cache,ctype_rel_codes,attribute_codes,search_prefix_var';
		this.save();
	};

	this.save3 = function () {
		eval('document.forms[0].codes_' + this.posId).value = 'inner_filter,inner_filter_init';
		this.save();
	};
	
	this.save4 = function () {
	    eval('document.forms[0].codes_' + this.posId).value = 'allow_new_content,destination_edit_link,destination_edit_variable,show_all_status_content,user_info_flag,ctrl_class,show_prices,show_prices_with_tax,show_valorations,show_discounts';
		this.save();
	};
	
	this.saveAll = function () {
		var oElem = eval('document.forms[0].codes_' + this.posId);
		oElem.value = 'num_pages,num_elems,content_type_id,content_type_request_var,category_id,xml_process,category_variable,category_join_type';
		oElem.value += ',destination_link,destination_variable,relationship_deep,disable_cache,ctype_rel_codes,attribute_codes,search_prefix_var';
		oElem.value += ',inner_filter,inner_filter_init';
		oElem.value += ',allow_new_content,destination_edit_link,destination_edit_variable,show_all_status_content,user_info_flag,ctrl_class,show_prices,show_prices_with_tax,show_valorations,show_discounts';
		var oRssJS = eval('objJS_rss_' + this.posId);
		oRssJS.saveNoAlert();
		this.save();
	};
	
	this.toggleCategoryVariable = function (elem) {
		var elem_value = elem.options[elem.selectedIndex].value;
		if ((elem_value == '-1') || (elem_value == '-2')) {
			document.getElementById('tr_category_var').style.display = '';
		} else {
			document.getElementById('tr_category_var').style.display = 'none';
		}
	};

};
