var checksTemas = null;

var appVarsFN = {
	inputBuscador : "#input_buscador",
	capaGesBuscador : "#gesbuscador",
	init : function(){
		$(appVarsFN.inputBuscador).click(function(){
			if (this.value == 'texto a buscar')
				this.value = '';
		});
		$(appVarsFN.capaGesBuscador).html('<div class="gesbuscador">mostrar filtrar resultados</div>');
		$(".buscador_interior > .int").hide();
		
		$(appVarsFN.capaGesBuscador).toggle(function(){
			$(this).html('<div class="gesbuscador">ocultar filtrar resultados</div>');
			$(".buscador_interior > .int").slideDown("slow");
		},function(){
			$(this).html('<div class="gesbuscador">mostrar filtrar resultados</div>');
			$(".buscador_interior > .int").slideUp("slow");
		});
	}
}
	

$(document).ready(appVarsFN.init);

function reset_contacto() {
	document.getElementById('nombre').value = '';
	document.getElementById('email').value = '';
	document.getElementById('asunto').value = '';
	document.getElementById('mensaje').innerHTML = '';
	document.getElementById('formcontacto').submit();
}

function addCheckTema(id, idpadre)
{
	if (checksTemas == null) checksTemas = new Array();
	checksTemas[checksTemas.length] = 'tema_' + id + '_' + idpadre;
}

function changeCheckTema(id, idpadre)
{
	var check = document.getElementById('tema_' + id + '_' + idpadre);
	if (check != undefined)
	{
		activarHijos(id, check.checked);
		activarPadre(idpadre, check.checked);
	}
}

function activarHijos(idpadre, value)
{
	for (var i=0; i<checksTemas.length; i++)
	{
		var partes = checksTemas[i].split('_');
		if ((partes.length >= 3) && (partes[2] == idpadre))
		{
			document.getElementById(checksTemas[i]).checked = value;
			activarHijos(partes[1], value);
		}
	}
}

function activarPadre(id, value)
{
	for (var i=0; i<checksTemas.length; i++)
	{
		var partes = checksTemas[i].split('_');
		if ((partes.length >= 3) && (partes[1] == id))
		{
			if ((value && todosHijosActivos(id)) || (!value))
			{
				document.getElementById(checksTemas[i]).checked = value;
				activarPadre(partes[2], value);
			}
		}
	}
}

function todosHijosActivos(id)
{
	var todos = true;
	
	for (var i=0; i<checksTemas.length && todos; i++)
	{
		var partes = checksTemas[i].split('_');
		if ((partes.length >= 3) && (partes[2] == id))
		{
			if (!document.getElementById(checksTemas[i]).checked)
				todos = false;
			else
				todos = todosHijosActivos(partes[1]);
		}
	}
	
	return todos;
}

function actualizaCheckTodos()
{
	var listbox = document.getElementById("tipo");
	var checkbox = document.getElementById("todos");
	if ((listbox != undefined) && (checkbox != undefined))
	{
		var	valor = listbox.options[listbox.selectedIndex].value;
		if (valor == '3')
			checkbox.disabled = true;
		else
			checkbox.disabled = false;
	}
}
