var tipo;
var elemento=1;

function LoadDoc(fname){
var xmlDoc=null;
try {xmlDoc=document.implementation.createDocument("","",null);} /* navegador que segue padrões*/
catch (err)
	{try {xmlDoc=new ActiveXObject("Msxml2.XMLHTTP");} /* fora do padrão*/
	catch (err)
		{try {xmlDoc=new ActiveXObject("Microsoft.XMLDOM");} /* fora do padrão*/
		catch (err){//alert ("Navegador n&atilde;o est&aacute; aceitando script. Use navegador que aceite padr&otilde;es!");
		}}}
xmlDoc.async=false;
xmlDoc.load (fname);
x=xmlDoc.getElementsByTagName("topico");
ultimo=x.length-1;
return(xmlDoc);}

var xmlDoc='';
var i=1;
const hi='<h4>'; const hf='</h4>';
function NL (ent){
	if (ent =='')return (ent);
	return ('<br />'+ent)}

function ExtrairTopico(i) {var i;
txt='<fieldset>';
var titulo1=PegarTag (i, "titulo");
var titulo=CriarLink ('exibir (comando)', 'show(['+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[1][tipo] || ElementoTopico[2][tipo]) txt+='<br />'
if (ElementoTopico[4][tipo]) {
	descricao=PegarTag (i, "desc");
	if (descricao !='') {txt+=descricao;}}
if (ElementoTopico[3][tipo]) {
	exemplo=PegarTag (i, "exemplo");
	if (exemplo !='') txt+=NL('<table class="exemplo"><tr><td>' + exemplo + '</td></tr></table>');}// exemplo
if (ElementoTopico[5][tipo]) txt+= PegarTag (i, "obs"); // obs
return (txt + '</fieldset>')}

function show(lista){
tipo=2; 
elemento=lista[0];	
//exibir ("atual", lista[0] + '.'+ PegarTag (lista[0], "titulo"));
exibir ("atual", '');
txt ="";
for (var j=0;j<lista.length;j++) txt += ExtrairTopico(lista[j]);
exibir ("show", txt);}

function show2(inicio,fim){
tipo=3; 
txt='';
for (var j=inicio;j<=fim;j++)
  {txt = txt +ExtrairTopico(j);}
  var limite= inicio + '.' + PegarTag (inicio, "titulo") + ' : ' + fim+'.'+
  PegarTag (fim, "titulo");
  exibir("atual", limite);
  exibir("show", txt);}

function seguinte(){if (elemento<ultimo) show ([++elemento])}

function anterior(){if (elemento>primeiro) show ([--elemento])}

function ExibirElemento(){
if (tipo==2) {show ([elemento]);} else
	if (tipo==3) {show2 (primeiro, ultimo);}	else
		{//alert('tipo='+tipo+' Válido: 2 ou 3')
		}}

//function ExibirBusca(){return(buscar);}

var entra;
function PegarTag(j, entra){
var txt='';
try {txt=x[j].getElementsByTagName(entra)[0].childNodes[0].nodeValue;}
catch(err){txt='';}
return (txt);}

function completar(entra){
var txt=''; var fim;
while (entra !=''){
	posicao = entra.search('~');
	if (posicao == -1) return (txt+=entra);
	txt += entra.substring(0, posicao);
	fim = entra.substring(posicao+1);
	var posicao = fim.search('~');
	var tag = fim.substring(0, posicao);
	var topico=PegarTag(0, tag);
	if (topico == '') return('');
	txt += topico;
	entra = fim.substring(posicao+1);}
return (txt)}

function CriarLink(mostrar, funcao, valor){
return('<a href="Javascript: '+mostrar+'" onclick="'+funcao+'; return false;"> '+valor+' </a>')}

function exibir(onde, oque){
//alert('exibir(onde, oque)='+onde+', '+oque);
try {document.getElementById(onde).innerHTML= oque;}
catch(err){}}

function ExibirNavegar(){
txt='<table><tbody><tr><td nowrap="nowrap">';
txt=CriarLink('exibir (primeiro)', 'show([primeiro])','&laquo;')+CriarLink('exibir (anterior)', 'anterior()','<');
exibir("antes", txt);
exibir("atual", 'txt?');
txt=CriarLink('exibir (seguinte)', 'seguinte()','>')+ CriarLink('exibir (ultimo)', 'show([ultimo])','&raquo;')+CriarLink('exibir (conjunto)', 'show2(primeiro, ultimo)','todos');
txt+=CriarLink('Mudar (filtro)', 'Mudar(ElementoFiltro, 0);','filtro');
exibir("depois", txt);}

function CompletarLista(lista){
var txt='';
for (j in lista) txt+= completar(lista [j]);
return (txt);}

function AtualizarElemento(entra, altera, OndeSai){
var txt='';
txt= CompletarLista (entra); // entra
eval (altera);
exibir (OndeSai, txt);} // sai

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 Atualizar(e){
eval(e + '= (' + e + ' + 1)%2;');
exibir("filtro", ExibirConjunto(ElementoFiltro));} // sai

function ExibirPagina(tab){
for (var j=1; j < tab.length;j++){
	if (tab[j][tipo])AtualizarElemento (tab[j][4], tab [j][5], tab [j][6])}}

function ExibirConjunto(tab){
var txt='';
for (var j=1; j < tab.length; j++)
	if (tab[j][1]==0) {// elemento
		txt+= CriarLink ("Mudar ('" + tab [j][0] + "')", "Mudar (" + tab [0][1] + ", " + j + ")" , tab[j][0]);}
	else { // conjuto
		if (tab[j][tipo]) txt+='<fieldset><legend style="font-weight: bold;">'; // elementtos
		txt+= tab[j][0] + CriarLink ("Mudar ('" + tab [j][0] + "')", "Atualizar ('" + tab [0][1] + "[" + j + "][" + tipo + "]')", ' {' + (tab[j][1].length-1)+ '} ') ; // nome conjunto
		if (tab[j][tipo]) {
			if (tab[j][tipo]) txt+='</legend>';
			txt+= ' '+ ExibirConjunto(tab[j][1]);
			txt+= '</fieldset>'}}
return(txt);}

function iniciar (xml, elemento, tipo){
LoadDoc(xml);
document.title = completar('~titulo~');
ElementoFiltro [4][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, [''], 'show(['+j+']);', ''])}
ExibirNavegar();
show(elemento);
//show([2,4,6]);
if(ElementoFiltro[0][tipo])
exibir("filtro", ExibirConjunto(ElementoFiltro));
ExibirPagina(FormatoPagina);
ExibirPagina(ElementoPagina);
}
