// Fitxer per l'automatizacio d'inclusio d'etiquetes

// notas de prensa

jQuery(document).ready(function(){

jQuery("input[name=_15_structureId][value=41084]").each(function(){
	jQuery("#_15_structure_el0_content").change(function() {
        //seleccionamos el tipo
	jQuery("#_15_type option[value=press-release]").attr("selected","selected");
	//recogemos el valor de llista_entitats
	var entitat = jQuery("#_15_structure_el0_content").attr("value");
        //borramos las entintats y insertamos las nuevas
		borrarTags();
	if(entitat == 'ems')
		entitat = 'ema';	
	jQuery("input[id$=_tags]").attr("value",entitat + ",sala de premsa");
	jQuery("input[id$=_addTag]").click();
        });
});

function borrarTags()
{
	jQuery(".ui-tag-delete").each(function() {
        eval(jQuery(this).attr("href"));
    });
}

function generarTagEntidad()
{
	if(jQuery("#_15_type option:selected").attr("value") == "licitacion")
	{
		//recogemos el valor de llista_entitats
		var entitat = jQuery("#_15_structure_el4_content").attr("value");
	        //borramos las entintas y insertamos las nuevas
		borrarTags();
		if(entitat == 'MMAMB')
			entitat = 'licitacions mmamb';
        else if(entitat == 'AMB')
			entitat = 'licitacions amb';
		else if(entitat == 'EMA')
			entitat = 'licitacions ema';
		else if(entitat == 'EMT')
			entitat = 'licitacions emt';
		else if(entitat == 'IMT')
			entitat = 'licitacions imt';
		else if(entitat == 'IMPSOL')
			entitat = 'licitacions impsol';
		else entitat = 'licitacions amb'
	
		jQuery("input[id$=_tags]").attr("value",entitat);
		jQuery("input[id$=_addTag]").click();
	}
	else if(jQuery("#_15_type option:selected").attr("value") == "procediment")
	{
		//recogemos el valor de llista_entitats
		var entitat = jQuery("#_15_structure_el2_content").attr("value");
	        //borramos las entintas y insertamos las nuevas
		borrarTags();
		if(entitat == 'MMAMB')
			entitat = 'procediments_negociats mmamb';
        else if(entitat == 'AMB')
			entitat = 'procediments_negociats amb';
		else if(entitat == 'EMA')
			entitat = 'procediments_negociats ema';
		else if(entitat == 'EMT')
			entitat = 'procediments_negociats emt';
		else if(entitat == 'IMT')
			entitat = 'procediments_negociats imt';
		else if(entitat == 'IMPSOL')
			entitat = 'procediments_negociats impsol';
		else entitat = 'procediments_negociats amb'
	
		jQuery("input[id$=_tags]").attr("value",entitat);
		jQuery("input[id$=_addTag]").click();
	}
}

function generarTagTipo()
{
	// Recogemos el campo seleccionado
	if(jQuery("#_15_type option:selected").attr("value") == "licitacion") {
		var tipus = jQuery("#_15_structure_el17_content").attr("value");
		if(tipus == 'OBRES')
			tipus = 'obres';
		else if(tipus == 'PUBLICA')
			tipus = 'publica';
		else if(tipus == 'SUBMINISTRAMENT')
			tipus = 'subministrament';
		else if(tipus == 'SERVEIS')
			tipus = 'serveis';
		else if(tipus == 'GESTIO')
			tipus = 'gestio';
		else if(tipus == 'COLABORACIO')
			tipus = 'colaboracio';
		else tipus = 'obres'
			
	//	jQuery("input[id$=_tags]").attr("value",tipus);
	//	jQuery("input[id$=_addTag]").click();
	}
	
}

function generarTags()
{
	borrarTags();
	generarTagEntidad();
	generarTagTipo();
	
}

//licitacions
jQuery("input[name=_15_structureId][value=526482],input[name=_15_structureId][value=313],input[name=_15_structureId][value=311],input[name=_15_structureId][value=314],input[name=_15_structureId][value=315],input[name=_15_structureId][value=319]").each(function(){
	
	//Correcion de espacios mal escapados (solo estetico)
	jQuery("select[id^=_15_structure_el] option").each(function(){jQuery(this).html(decodeURI(jQuery(this).html()))});
	
	//seleccionamos el tipo
	jQuery("#_15_type option[value=licitacion]").attr("selected","selected");
	
	//copiar el campo motiu en el de descripcion
	jQuery("#_15_structure_el91_content").change(function() {
	var text = decodeURI(jQuery("#_15_structure_el91_content :selected").html());	
	jQuery("#_15_description").html(text);
	});
	
	//cambiar el tag de entitat
	jQuery("#_15_structure_el4_content").change(function() {
		generarTags();
	});

	//cambiar el tag de la categoria de licitacion
	jQuery("#_15_structure_el20_content").change(function() {
		generarTags();
	});
	
	//Generamos los tags (una sola vez)
	generarTags();
});

//procediments negociats
jQuery("input[name=_15_structureId][value=PROCEDIMENTS-NEGOCIATS]").each(function(){
	
	//Correcion de espacios mal escapados (solo estetico)
	jQuery("select[id^=_15_structure_el] option").each(function(){jQuery(this).html(decodeURI(jQuery(this).html()))});
	
	//seleccionamos el tipo
	jQuery("#_15_type option[value=procediment]").attr("selected","selected");
	
	//cambiar el tag de entitat
	jQuery("#_15_structure_el2_content").change(function() {
		generarTags();
	});

	//Generamos los tags (una sola vez)
	generarTags();
});


});
