//--------------------------------------------------------------------------
// Metodo que faz consultas
//--------------------------------------------------------------------------
function Consultador_consulta(nomesParametros,valoresParametros){
  var i;
  var iframe = document.getElementById(this.iframeId);
  var queryString = nomesParametros[0]+'='+valoresParametros[0];
  for(i=1;i<nomesParametros.length;i++){
    queryString+= '&'+nomesParametros[i]+'='+valoresParametros[i];
  }
  iframe.src = this.urlConsulta+'?'+queryString;
}

//--------------------------------------------------------------------------
// Construtor do consultador
//--------------------------------------------------------------------------
function Consultador(iframeId,urlConsulta){
  this.iframeId = iframeId;
  this.urlConsulta = urlConsulta;
  this.consulta = Consultador_consulta;
}

//--------------------------------------------------------------------------
// Funcao que o iframe chama e que atualiza a tela depois de uma consulta
//--------------------------------------------------------------------------
function refresh(opcode){
  var i;
  if(opcode=='temas' || opcode=='temas_check'){
    var iframe = document.getElementById(consultador.iframeId);
    var select = iframe.contentWindow.document.getElementById('temas');
    var divTemas = document.getElementById('esquerda');
    var indicadoresIframe = iframe.contentWindow.document.getElementById('indicadores');
    var divIndicadores = document.getElementById('indicadores');
    var botaoGerarTabela = document.getElementById('gerarTabela');
    var tema,checkBox;
    divTemas.innerHTML = '';
    divIndicadores.innerHTML = indicadoresIframe.innerHTML;
    for(i=0;i<select.options.length;i++){
      tema = document.createElement('P');
      checkBox = document.createElement('INPUT');
      checkBox.type = 'checkbox';
      checkBox.id = 'cb'+select.options[i].value;
      eval('checkBox.onclick = function(){mostraOuEsconde('+select.options[i].value+');};');
      tema.className = 'formato_temas';
      tema.appendChild(checkBox);
      tema.appendChild(document.createTextNode(select.options[i].text));
      divTemas.appendChild(tema);
    }
    if(opcode=='temas_check'){
      checaIndicadores(); // Funcao escrita pelo php no tpl_tema
    }
    botaoGerarTabela.onclick = submeter;
  }
}

//--------------------------------------------------------------------------
// Chamada quando se clica no botao. Busca no BD os temas e os indicadores
// referentes ao bairro/regiao/cidade definidos pelo usuario
//--------------------------------------------------------------------------
function mostrarTemas(checar){
  if(arguments.length==0) checar = false;
  var opcode = (checar?'temas_check':'temas');
  limpaTemasEIndicadores();
  var regiao = document.getElementById('regioes').value;
  var bairro = document.getElementById('bairros').value;
  var cidade = document.getElementById('cidade').checked;
  var nomesParametros = new Array('opcode','regiao','bairro','cidade');
  var valoresParametros = new Array(opcode,regiao,bairro,cidade);
  consultador.consulta(nomesParametros,valoresParametros);
  document.getElementById('h_regiao').value = regiao;
  document.getElementById('h_bairro').value = bairro;
  document.getElementById('h_cidade').value = cidade;
}

//--------------------------------------------------------------------------
// Mostra ou esconde os indicadores de um tema de acordo com o checked
// da check box referente ao tema
//--------------------------------------------------------------------------
function mostraOuEsconde(codTema){
  var cbTema = document.getElementById('cb'+codTema);
  var divTema = document.getElementById(codTema);
  if(cbTema.checked){
    divTema.style.display = 'block';
  }else{
    divTema.style.display = 'none';
  }
}

//--------------------------------------------------------------------------
// Marca a checkbox do tema identificado por codTema
//--------------------------------------------------------------------------
function checaTema(codTema){
  var cbTema = document.getElementById('cb'+codTema);
  if(!cbTema.checked){
    var divTema = document.getElementById(codTema);
    cbTema.checked = true;
    divTema.style.display = 'block';
  }
}

//--------------------------------------------------------------------------
// Marca a checkbox do indicador identificado por codTema, codInd e ano
// Se o tema codTema nao estiver marcado, marca
//--------------------------------------------------------------------------
function checaIndicador(codTema,codInd,ano){
  var cbInd = document.getElementById('ind'+codTema+','+codInd+','+ano);
  if(!cbInd.checked){
    cbInd.checked = true;
    checaTema(codTema); 
  }
}

//--------------------------------------------------------------------------
// Remove os indicadores dos temas deselecionados
//--------------------------------------------------------------------------
function filtraVisiveis(){
  var divIndicadores = document.getElementById('indicadores');
  var filhos = divIndicadores.childNodes;
  //var totalfilhos = filhos.length;
  for(i=0;i<filhos.length;i++){
    //alert ('Total de filhos:'+filhos.length);
    if(filhos.item(i).nodeName=='DIV'){
      if(filhos.item(i).style.display=='none'){
        filhos.item(i).parentNode.removeChild(filhos.item(i));
//        alert ('SUMIU? ('+i+'º)');
//        alert ('SUMIU');
        i--;
      }
    }
/*    else {
        alert ('socorro e uma '+filhos.item(i).nodeName );
    }*/
  }
}

//--------------------------------------------------------------------------
// Submete o formulario
//--------------------------------------------------------------------------
function submeter(){
  filtraVisiveis();
  document.getElementById('formIndicadores').submit();
}

//--------------------------------------------------------------------------
// Limpa as divs dos temas e dos indicadores e ainda "desabilita" o botao
// "gerar tabela"
//--------------------------------------------------------------------------
function limpaTemasEIndicadores(){
  var divTemas = document.getElementById('esquerda');
  var divIndicadores = document.getElementById('indicadores');
  var botaoGerarTabela = document.getElementById('gerarTabela');
  botaoGerarTabela.onclick = '';
  divTemas.innerHTML = '';
  divIndicadores.innerHTML = '';
}

