/****************************************************************************************************************************

											TRATADORES DO fPesquisa

****************************************************************************************************************************/

var fBancos = null;//auxiliar utilizada na pesquisa por bancos ( tive que separar para conseguir exibir o "aguarde")
var fexibindodetalhes = false;

function setastatusbotaopesquisasimples(botao, status) 
{
	(status)?RetornaElementosComID(fPesquisa, botao)[0].src = sprintf("/imagens/pesquisa/%sON.gif", botao):RetornaElementosComID(fPesquisa, botao)[0].src = sprintf("/imagens/pesquisa/%sOFF.gif", botao);
}

function bPesquisarClick()
{
	if (!fPesquisa) fPesquisa = CriaJanela("pesquisa_simples.htm");
	if (fexibindodetalhes) ExibeJanela(fPesquisa, 611, 532);else ExibeJanela(fPesquisa, 611, 390);
	RetornaElementosComID(fPesquisa, 'ePesquisa')[0].focus();
}

function fPesquisabCancelarClick()
{
	FechaJanela(fPesquisa);
}

function fPesquisaMudaParaAvancada()
{
	FechaJanela(fPesquisa);
	fPesquisaAvancadabPesquisaAvancadaClick();
}

function fPesquisatrataclickradiogroup(grupo, selecionado)
{
	var coll = RetornaElementosComID(fPesquisa, grupo);
	for (var i = 0; i < coll.length; i++)
	  coll[i].src = (i==selecionado)?"/imagens/pesquisa/bRadioButtonON.gif":"/imagens/pesquisa/bRadioButtonOFF.gif";
}

function fPesquisatrataclickcheckbox(checkboxstr)
{
  var checkbox = RetornaElementosComID(fPesquisa, checkboxstr)[0];
  
  if (checkbox.src.indexOf("bCheckBoxOFF.gif") != -1)
   checkbox.src = checkbox.src.replace("bCheckBoxOFF", "bCheckBoxON");
   else
   checkbox.src = checkbox.src.replace("bCheckBoxON", "bCheckBoxOFF");
}

function fPesquisaMarcaCheckBox(checkbox, status)
{
	if (status)
		checkbox.src = checkbox.src.replace("bCheckBoxOFF", "bCheckBoxON");
	else
		checkbox.src = checkbox.src.replace("bCheckBoxON", "bCheckBoxOFF");
}


function fPesquisaTipoPesquisa()
{
	var coll = RetornaElementosComID(fPesquisa, "radiobutton");
	var i = 0;
	for (i = 0; i < coll.length; i++) 
  if (coll[i].src.indexOf("bRadioButtonON.gif") != -1) return i;
	
	return "0";//default "0", ou seja, opção "E"
}

function fPesquisaEscopoPesquisa()
{
	var coll = RetornaElementosComID(fPesquisa, "rbescopo");
	var i = 0;
	for (i = 0; i < coll.length; i++)
    if (coll[i].src.indexOf("bRadioButtonON.gif") != -1) return i;
	
	return "0";//default "0", ou seja, opção "E"
}

function ControlaExibirListaBancos()
{
	var escopo = fPesquisaEscopoPesquisa();
	var clbareasbancos = RetornaElementosComID(fPesquisa, 'clbareasbancos');
	
	switch(escopo)
	{
		case 0: 
			RetornaElementosComID(fPesquisa, 'clbareasbancos')[0].style.display = "none"; 
			break;
		case 1: 
			PopulaListaAreasBancos(ajax_listaareas(), RetornaElementosComID(fPesquisa, 'clbareasbancos')[0]);
			RetornaElementosComID(fPesquisa, 'clbareasbancos')[0].style.display = "block"; 			
			break;
		case 2: 
			PopulaListaAreasBancos(ajax_listatodosbancos(), RetornaElementosComID(fPesquisa, 'clbareasbancos')[0]);
			RetornaElementosComID(fPesquisa, 'clbareasbancos')[0].style.display = "block";
			break;
	}
}

function fPesquisaAreasBancosSelecionados()
{
	var i = 0;
	var coll = RetornaElementosComID(fPesquisa, 'checkboxareasbancos');
	var resultado = new Array();
	
	if (!coll) return resultado;
	
	for (i = 0; i < coll.length; i++)
		if (coll[i].checked) resultado[resultado.length] = coll[i].codigo;
		
	return resultado;
}

function TrataePesquisaKeyPress(evento)
{
  if (!evento) var evento = event;
	if (evento.keyCode == 13) fPesquisabPesquisarClick();
}


function fPesquisabPesquisarClick()
{
	var ateparagrafo = 0;
	
	if (RetornaElementosComID(fPesquisa, 'ePesquisa')[0].value == "")
	{
	 alert("Digite a(s) palavra(s) ou frase(s) desejadas para a pesquisa.");
	 RetornaElementosComID(fPesquisa, 'ePesquisa')[0].focus();
	 return;
  }
	
	if (RetornaElementosComID(fPesquisa, 'cbParagrafo')[0].src.indexOf("bCheckBoxON.gif") != -1)
	{
	  ateparagrafo = parseInt(RetornaElementosComID(fPesquisa, 'qtdparagrafospesquisasimples')[0].value);
    if (isNaN(ateparagrafo))
 	  {
  	   alert("Quantidade de parágrafos inválida.");
  	   RetornaElementosComID(fPesquisa, 'qtdparagrafospesquisasimples')[0].focus();
  	   return;
    }
  }
  
	ajax_pesquisarassincrono(RetornaElementosComID(fPesquisa, 'ePesquisa')[0].value, 
         									 fPesquisaTipoPesquisa(), 
         									 fPesquisaEscopoPesquisa(),
         									 (RetornaElementosComID(fPesquisa, 'cbIgnorarNotas')[0].src.indexOf("bCheckBoxON.gif") != -1),
         									 ateparagrafo,
         									 fPesquisaAreasBancosSelecionados(),
                           fPesquisaRecebeResultadoPesquisa);
                  
  ExibeAguarde('Pesquisando, aguarde...');
}

function fPesquisaRecebeResultadoPesquisa(resultado)
{
	var frameresultado = null;
	var resultadopesquisa = new ListaResultadoPesquisa();
	
	ExibeAguarde();
	
	var qtd = retornavalor(resultado, "QTD");
	
	if (qtd == '0') alert("Nenhuma ocorrência encontrada!"); else 
	{
		resultadopesquisa.parseialista(retornavalor(resultado, "RESULTADO"));
		frameresultado = CriaAba(resultadopesquisa.item(0).codigo / 10000, retornavalor(resultado, "SID"), resultadopesquisa, qtd, retornavalor(resultado, "TEXTO"), (fPesquisaTipoPesquisa() == '2'));
		frameresultado.exibecodigo(resultadopesquisa.item(0).codigo);
		FechaJanela(fPesquisa);
		MarcaPalavrasTexto(retornavalor(resultado, "TEXTO"), (fPesquisaTipoPesquisa() == '2'));
		nav_oco_primeira();
	}
}


function fPesquisatrataqtdparagrafoskeypress()
{
  fPesquisaMarcaCheckBox(RetornaElementosComID(fPesquisa, 'cbParagrafo')[0], 
                         RetornaElementosComID(fPesquisa, 'qtdparagrafospesquisasimples')[0].value != "");	 
}


function fPesquisabDetalhesClick(expandir)
{
	if (expandir)
	{
		RetornaElementosComID(fPesquisa, 'bDetalhesExpandir')[0].style.display = "none";
		RetornaElementosComID(fPesquisa, 'bDetalhesRecolher')[0].style.display = "block";
		RetornaElementosComID(fPesquisa, 'bloco_detalhes')[0].style.display = "block";
		RetornaElementosComID(fPesquisa, 'imgfundo')[0].src = "/imagens/pesquisa/fundo_detalhes.jpg"; 
		RetornaElementosComID(fPesquisa, 'bAjuda')[0].style.top = "485px";
		RetornaElementosComID(fPesquisa, 'bPesquisar')[0].style.top = "485px";
		RetornaElementosComID(fPesquisa, 'bCancelar')[0].style.top = "485px";
		RetornaElementosComID(fPesquisa, 'bDetalhesRecolher')[0].style.top = "454px";
		RetornaElementosComID(fPesquisa, 'fundo')[0].style.top = (document.documentElement.clientHeight - 532) / 2 + "px";
		fexibindodetalhes = true;
	}
	else
	{
		RetornaElementosComID(fPesquisa, 'bDetalhesExpandir')[0].style.display = "block";
		RetornaElementosComID(fPesquisa, 'bDetalhesRecolher')[0].style.display = "none";
		RetornaElementosComID(fPesquisa, 'bloco_detalhes')[0].style.display = "none";
		RetornaElementosComID(fPesquisa, 'imgfundo')[0].src = "/imagens/pesquisa/fundo.jpg"; 
		RetornaElementosComID(fPesquisa, 'bAjuda')[0].style.top = "343px";
		RetornaElementosComID(fPesquisa, 'bPesquisar')[0].style.top = "343px";
		RetornaElementosComID(fPesquisa, 'bCancelar')[0].style.top = "343px";
		RetornaElementosComID(fPesquisa, 'bDetalhesRecolher')[0].style.top = "312px";
		RetornaElementosComID(fPesquisa, 'fundo')[0].style.top = (document.documentElement.clientHeight - 390) / 2 + "px";
		fexibindodetalhes = false;
	}
}

function ExecutarPesquisaSimplesNestesBancos(bancos)
{
 	bPesquisarClick();
 	ExibeAguarde('Selecionando bancos, aguarde...');
	fBancos = bancos;
	setTimeout("ExecutarPesquisaSimplesNestesBancos_Int()", 300);
}

function ExecutarPesquisaSimplesNestesBancos_Int()
{ 
	var ibancos = 0;
	var i = 0;
	
	fireClickEvent(RetornaElementosComID(fPesquisa, 'bDetalhesExpandir')[0]);//expandindo detalhes...
  fireClickEvent(RetornaElementosComID(fPesquisa, 'fPesquisaSimplesMarcarBancos')[0]);//selecionando "bancos"
	
	var coll = RetornaElementosComID(fPesquisa, 'checkboxareasbancos');
	
  ///
  //   marcando os elementos
  ///
  
  for (ibancos = 0;ibancos < fBancos.length;ibancos++)
    for (i = 0;i < coll.length; i++)
       if (coll[i].codigo == fBancos[ibancos].banco) coll[i].checked = true;
       
  ExibeAguarde();
}

function ExibeAguarde(texto)
{
  if (texto)
  {
    RetornaElementosComID(fPesquisa, 'mensagemcarregando')[0].innerHTML = texto;
    RetornaElementosComID(fPesquisa, 'loading')[0].style.display = "block";
  }
  else
    RetornaElementosComID(fPesquisa, 'loading')[0].style.display = "none";
}


