"use strict";
var primeiro = 1, j, ElementoTopico, txt, tipo, FiltroConteudo, ultimo, SalvaLista, OElemento;
var x, xmlDoc = null;
var i = 1;
var hi = '<h4>', hf = '</h4>';
var entra;

function PegarTag(j, entra) {
	var txt = '';
	try {
		txt = x[j].getElementsByTagName(entra)[0].childNodes[0].nodeValue;
	}
	catch (err) {
		txt = '';
	}
	return (txt);
}

function CriarLink(funcao, valor, id) {
	if (id !== '') {
		id = ' id = "' + id + '"';
	}
	return (' <a' + id + ' href = "' + funcao + '">' + valor + '</a>');
}

function ExtrairTopico(i) {
	var titulo, titulo1, exemplo, ConteudoBnf;
	txt = '<fieldset>';
	titulo1 = PegarTag(i, "titulo");
	titulo = CriarLink('javascript:ExibirElementos([' + i + '])', titulo1, '');
	if (ElementoTopico[1][tipo]) {
		txt += '<legend><small>' + i + '</small>.' + titulo + '</legend>';
	}
	if (ElementoTopico[2][tipo]) {
		ConteudoBnf = PegarTag(i, "bnf");
		if (ConteudoBnf !== '') {
			txt += titulo + ': ' + ConteudoBnf + '<br />';
		}
	} // bnf
	if (ElementoTopico[4][tipo]) {
		txt += PegarTag(i, "desc");
	}
	if (ElementoTopico[3][tipo]) { // exemplo
		exemplo = PegarTag(i, "exemplo");
		if (exemplo !== '') {
			txt += '<br /><table class="exemplo"><tr><td>' + exemplo + '</td></tr></table>';
		}
	}
	if (ElementoTopico[5][tipo]) { // obs
		txt += PegarTag(i, "obs");
	}
	return (txt + '</fieldset>');
}

function exibir(onde, oque) {
	try {
		if (onde !== '') {
			document.getElementById(onde).innerHTML =  oque;
		}
	}
	catch (err107) {
		alert('function exibir(onde, oque)=' + onde + ', ' + oque);
	}
}

function ExibirElementos(lista) {
	var id;
	tipo = 2;
	SalvaLista = lista;
	OElemento = lista[0];
	txt = "";
	for (j = 0; j < lista.length; j++) {
		txt += ExtrairTopico(lista[j]);
	}
	exibir("corpo", txt);
	txt = '';
	if (lista[0] !== 1) {
		if (lista[0] === ultimo) {
			id = 'seguinte';
		}
		else {
			id = '';
		}
		txt += ' ' + CriarLink('javascript:ExibirElementos([primeiro])', '&laquo;', id); // <<
		txt += ' ' + CriarLink('javascript:anterior()', '&lt;', '');
	}
	txt += ' ' + OElemento;
	if (lista[0] !== ultimo) {
		txt += ' ' + CriarLink('javascript:seguinte()', '&gt;', 'seguinte');
		txt += ' ' + CriarLink('javascript:ExibirElementos([ultimo])', '&raquo;', '');
	}
	txt += ' ' + CriarLink('javascript:show2(primeiro, ultimo)', 'todos', '');
	txt += ' ' + CriarLink('javascript:Mudar(ElementoFiltro, 0);', '<img src="filtro.gif" alt="Habilitar/Desabilitar Índice e Filtro" title="Habilitar/Desabilitar Índice e Filtro" />', '');
	exibir("navegacao", txt);
	document.getElementById('seguinte').focus();
}

function show2(inicio, fim) {
	tipo = 3;
	txt = '';
	for (var j = inicio;j <= fim;j++) {
		txt = txt + ExtrairTopico(j);
	}
	exibir("navegacao", inicio + '.' + CriarLink('javascript:ExibirElementos([' + inicio + '])', PegarTag(inicio, "titulo", '')) + ': ' + fim + '.' + CriarLink('javascript:ExibirElementos([' + fim + '])', PegarTag(fim, "titulo"), '') + CriarLink('javascript:Mudar(ElementoFiltro, 0);', '<img src="filtro.gif" alt="Habilitar/Desabilitar Índice e Filtro" title="Habilitar/Desabilitar Índice e Filtro" />', ''));
	exibir("corpo", txt);
}

function seguinte() {
	if (OElemento < ultimo) {
		ExibirElementos([++OElemento]);
	}
    document.getElementById('seguinte').focus();
}

function anterior() {
	if (OElemento > primeiro) {
		ExibirElementos([--OElemento]);
	}
}

function Refresh(conjunto, elemento) {
	conjunto[elemento][tipo] = (conjunto[elemento][tipo] + 1) % 2;
	if (tipo === 2) {
		ExibirElementos(SalvaLista);
	}
	else {
		if (tipo === 3) {
			show2(primeiro, ultimo);
		}
		else {
			alert('tipo = ' + tipo + ' Válido: 2=lista de elementos ou 3=todos');
		}
	}
}

function ExibirElemento(FiltroConteudo, tipo) {
	if (tipo === 2) {
		ExibirElementos(FiltroConteudo);
	}
	else {
		if (tipo === 3) {
			show2(primeiro, ultimo);
		}
		else {
			alert('tipo = ' + tipo + ' Válido: 2=lista de elementos ou 3=todos');
		}
	}
}

function completar(entra) {
	var fim, posicao, topico, txt = '';
	while (entra !== '') {
		posicao = entra.search('~');
		if (posicao === -1) {
			return (txt += entra); 
		}
		txt += entra.substring(0, posicao);
		fim = entra.substring(posicao + 1);
		posicao = fim.search('~');
		topico = PegarTag(0, fim.substring(0, posicao));
		if (topico === '') {
			return (''); 
		}
		txt += topico;
		entra = fim.substring(posicao + 1);
	}
	return (txt);
}

function CompletarLista(ListaCompletar) {
	txt = '';
	var tamanho = ListaCompletar.length;
	for (j = 0; j < tamanho; j = j + 1) {
		txt += completar(ListaCompletar [j]);
	}
	return (txt);
}

function Mudar(conjunto, elemento) {
	var txt = '';
	conjunto[elemento][tipo] = (conjunto[elemento][tipo] + 1) % 2;
	if (conjunto[elemento][tipo]) {
		txt = CompletarLista(conjunto[elemento][4]);
	}
	eval(conjunto[elemento][5]);
	exibir(conjunto[elemento][6], txt);
}

function ExibirConjunto(tab) {
	var j, txt = '';
	for (j = 1; j < tab.length; j++) {
		if (tab[j][1] === 0) {// elemento
			if (tab [j][5]===''){
				txt += CriarLink("javascript: Mudar(" + tab [0][1] + ", " + j + ");", tab[j][0], '');
			}
			else {
				txt += CriarLink(tab [j][5], tab[j][0], '');
			}
		}
		else { // conjunto
			if (tab[j][tipo]) { // elementos
				txt += '<fieldset><legend style="font-weight: bold;">';
			}
			txt += tab[j][0] + CriarLink("javascript:Atualizar ('" + tab [0][1] + "[" + j + "][" + tipo + "]')", ' {' + (tab[j][1].length - 1) + '} ', ''); // nome conjunto
			if (tab[j][tipo]) {
				txt +=	'</legend>' +
						' ' + ExibirConjunto(tab[j][1]) +
						'</fieldset>';
			}
		}
	}
	return (txt);
}

function Atualizar(e) {
	eval(e + '= (' + e + ' + 1)%2;');
	exibir("indice", ExibirConjunto(ElementoFiltro));
} // sai

function ExibirPagina(tab) {
	var j;
	for (j = 1; j < tab.length;j++) {
		entra = [];
		if (tab[j][tipo]) {
			entra = tab[j][4];
			eval(tab [j][5]);
		}
		exibir(tab [j][6], CompletarLista(entra));
	}
}

function iniciar(xml, FiltroConteudo, tipo) {
	xmlDoc = document.implementation.createDocument("", "", null);
	xmlDoc.async = false;
	xmlDoc.load(xml);
	x = xmlDoc.getElementsByTagName("topico");
	ultimo = x.length - 1;
	document.title = completar('~titulo~');
	ElementoFiltro [1][0] = PegarTag(0, "nome");
	while (Indice.length > 1) {
		Indice.pop();
	}
	for (j = 1; j <= ultimo; j++) {
		Indice.push([j + '.' + PegarTag(j, 'titulo'), 0, 1, 1, [''], 'javascript:ExibirElementos([' + j + ']);', '']);
	}
	ExibirElemento(FiltroConteudo, tipo);
// ExibirElementos([2,4,6]);
	txt = '';
	if (ElementoFiltro[0][tipo]) {
		txt = ExibirConjunto(ElementoFiltro);
	}
	exibir("indice", txt);
	ExibirPagina(FormatoPagina);
	ExibirPagina(ElementoPagina);
}
