function submeter(){
	formulario = document.pesquisa;
	if (formulario.tipoData.value=="J") {
		formulario.dataDecisaoInicio.value = formulario.dataInicio.value;
		formulario.dataDecisaoFim.value = formulario.dataFim.value;
			
		formulario.dataPubInicio.value = "";
		formulario.dataPubFim.value = "";
		}
		else{
			formulario.dataPubInicio.value = formulario.dataInicio.value;
			formulario.dataPubFim.value = formulario.dataFim.value;
			
			formulario.dataDecisaoInicio.value = "";
			formulario.dataDecisaoFim.value = "";
		}
	  	if(
	  			formulario.item1.value != '' ||
	  			formulario.item2.value != '' ||
	  			formulario.item3.value != '' ||
	  			formulario.item4.value != '' || 
	  			formulario.refLegNormaSigla.value  != '' ||
	  			formulario.refLegNormaNumero.value != '' ||
	  			formulario.refLegNormaAno.value    != ''
	    	){
			maisref() ;
	  	}

	  	formulario.submit();
}

function abreInteiroTeor( tribunal, processoNumero, processoClasse, decisaoData, decisaoNumero ){
	url = tribunal == 'TSE' ? 'http://www.tse.gov.br/servicos/inteiroTeor/actionGetBinary.do?':
                             'http://www.tse.gov.br/servicos/inteiroTeor/actionGetTREBinary.do?';

	url += 'tribunal=' + tribunal;
	if(processoNumero != null && processoNumero != 'null' ) url += '&processoNumero=' + processoNumero;
	if(processoClasse != null && processoClasse != 'null' ) url += '&processoClasse=' + processoClasse;
	if(decisaoData != null && decisaoData != 'null' ) url += '&decisaoData=' + decisaoData;
	if(decisaoNumero != null && decisaoNumero != 'null' ) url += '&decisaoNumero=' + decisaoNumero;
	url += '&noCache=' + Math.random();
   //alert( url );
	showWindow( url, 'inteiroTeor', 600, 300 );
}

function abreORSE( tipo, numero, data ){
	url = 'http://www.tse.gov.br/servicos/baseDados/inteiroTeor/actionGetBinary.do?';
	url += 'tipo=' + tipo;
	url += '&numero=' + numero;
	url += '&data=' + data;
	url += '&noCache=' + Math.random();
   //alert( url );
	showWindow( url, 'inteiroTeor', 600, 300 );
}

/**
 * Mostra a janela de largura e altura indicado e posiciona no centro da tela.
 */
function showWindowSJud( url, target, width, height ){
	x = ( screen.height - height ) / 2
	y = ( screen.width - width ) / 2
	config =  'top=' + x +
                  ',left=' + y +
                  ',height=' + height +
                  ',width=' + width +
                  ',scrollbars=yes' +
                  ',statusbar=yes';
	return window.open( url, target, config );
}

function showWindowValueSJud( url, target, width, height ){
	var hWnd = showWindowSJud( url, target, width, height );
	if( ( document.window != null ) && !hWnd.opener ){
		hWnd.opener = document.window;
	}
}

function navegar( isToc, index ){
	formulario = document.nav;
	formulario.toc.value = isToc;
	formulario.docIndexString.value = index;
	//alert( form.toc.value + ' ' + form.docIndexString.value )
	formulario.submit();
}

function acao( isToc, index ){
	formulario = document.formHidden
	formulario.toc.value = isToc;
	formulario.docIndexString.value = index;
	formulario.submit();
}

function acaoTarget( isToc, index, target ){
   if( target != null )
       form.target = target
   acao( isToc, index )
}

function goSearch( page ){
	formulario = document.formHidden;
	formulario.action = page;
	formulario.target = "";
	formulario.submit();
}

function teste(){
   alert( "teste" );
}

function showWinUtilSJud( url, frame, w, h ){
	formulario = document.pesquisa
   if( formulario.sectionServers.value.indexOf( ',' ) > -1 ){
      alert( "Este recurso só está disponível quando só existe um tribunal selecionado" );
      return false;
   }else{
      showWindowValueSJud( url, frame, h, w )
      return true;
   }
}

function showSelected(){
	formulario = document.lista;
   docIndexString = "";
   for( i = 0; i < formulario.index.length; i++ ){
      if( formulario.index[ i ].checked ){
          if( docIndexString != "" )
              docIndexString += ",";
          docIndexString += formulario.index[ i ].value;
      }
   }
   if( docIndexString != "" ){
		//document.nav.target = '_blank'
	   showWindowValueSJud('', 'winDoc', 800,600); 
	   document.nav.target='winDoc';
       navegar( false, docIndexString );
   }else alert( "Selecione algum documento." );
}

function showWinUtil( url, frame, w, h ){
	formulario = document.pesquisa
   if( formulario.sectionServers.value.indexOf( ',' ) > -1 ){
      alert( "Este recurso só está disponível quando só existe um tribunal selecionado" );
      return false;
   }else{
      showWindowValue( url, frame, h, w )
      return false;
   }
}

function enableTodosCampos(){
   //form = document.pesquisa;
   //value = !form.textoTodos.checked;
   //form.textoEmenta.checked = value;
   //form.textoIndexacao.checked = value;
   //form.textoDecisao.checked = value;
   //form.textoObservacao.checked = value;
}

function checkAllTipos(){
	var formulario = document.pesquisa;
	var value = formulario.tipoTodas.checked;
	formulario.tipoAcordao.checked = value;
	formulario.tipoResolucao.checked = value;
	formulario.tipoDecisaoSemResolucao.checked = value;
	formulario.tipoDecisaoMonocratica.checked = value;

	}
function unCheckAllTipos(obj){
	if (!obj.checked){
		formulario.tipoTodas.checked = false;
	}
}

function disableTodosCampos(){
	formulario = document.pesquisa;
	formulario.textoTodos.checked = false;
}

function clearForm(){
   	document.pesquisa.reset();
   	document.pesquisa.sectionServers.value = 'TSE';
   	//document.getElementById("botaoRelator").src="<%= request.getContextPath() %>/imagens/dropdown.gif";
	document.pesquisa.textoTodos.checked = true;
}

function operador(op) {
	if (op != "$")
		document.pesquisa.livre.value = document.pesquisa.livre.value + ' ' + op + ' ';
	else
		document.pesquisa.livre.value = document.pesquisa.livre.value + op;
	document.pesquisa.livre.focus();
}

function addStrItem(strOrigem, tipoOp, itemOp){
  	var strRetorno = strOrigem;	
  	var valor = itemOp.value;
  	if (valor == "e" || valor == "E" ) {
		valor = "'" + valor + "'";
	}
	if (tipoOp.value != "" && valor != "" ){
		if (strOrigem!="") {
	  		strRetorno += " ADJ4 ";  	
		}
		strRetorno += "( " + tipoOp.value ;	
		strRetorno += " ADJ " + valor + " )";
	}
	else{
		if (valor != ""){
			strRetorno += adicionaItem(valor);
		}
	}
	return strRetorno;
}

function addStr(strOrigem, itemOp, tipoOp){
  	var strRetorno = strOrigem;	
	var valor = itemOp.value;
	if (valor == "e" || valor == "E" ) {
		valor = "'" + valor + "'";
	}
	if (valor != "") {
		if (strOrigem!="" && strOrigem!="(" ) {
	  		strRetorno += " " + tipoOp + " ";  	
		}
		strRetorno += "'" + valor + "'";	
	}
	return strRetorno;
}

function montaref () {
	doc = document.pesquisa;
	doc.ref.value='';
	parametroLeg = '';	
	parametroArt = '';
	var temRefLeg = false;
	
	if (doc.refLegNormaSigla.value != "" || doc.refLegNormaNumero.value!="" || doc.refLegNormaAno.value!="")
	{
		parametroLeg += "("; 
		temRefLeg = true;
	}  
	parametroLeg = addStr(parametroLeg, doc.refLegNormaDescricao, "ADJ");
	parametroLeg = addStr(parametroLeg, doc.refLegNormaNumero, "ADJ");
	parametroLeg = addStr(parametroLeg, doc.refLegNormaAno, "ADJ");
	if (temRefLeg){
		parametroLeg += ")"; 
	}
	
	parametroArt = addStrItem(parametroArt, doc.tipo1, doc.item1);
	parametroArt = addStrItem(parametroArt, doc.tipo2, doc.item2);
	parametroArt = addStrItem(parametroArt, doc.tipo3, doc.item3);
	parametroArt = addStrItem(parametroArt, doc.tipo4, doc.item4);
	
	if ((parametroLeg != '' ) && (parametroArt != '' )) {
		doc.ref.value='( '+ parametroLeg + ' MESMO (' + parametroArt + '))[REFL]'; 
	}else if  (parametroLeg != '') {
		doc.ref.value='( '+ parametroLeg + ')[REFL]';
	}else if  (parametroArt != '') {	
		doc.ref.value='( '+ parametroArt + ')[REFL]';
	}	
	
	return true;
}

function maisref () {
	montaref();
	if (document.pesquisa.ref.value != '') {
		if (document.pesquisa.livre.value != '')
			document.pesquisa.livre.value=document.pesquisa.livre.value + ' E ' + document.pesquisa.ref.value;
		else
			document.pesquisa.livre.value=document.pesquisa.ref.value;
	}
	else
		alert('Preencha os campos referentes à Legislação.');
	document.pesquisa.ref.value = '';
	document.pesquisa.refLegNormaSigla.value = '';
	document.pesquisa.refLegNormaNumero.value = '';
	document.pesquisa.refLegNormaAno.value = '';
	document.pesquisa.refLegNormaDescricao.value = '';	
	document.pesquisa.tipo1.selectedIndex = 1;
	document.pesquisa.tipo2.selectedIndex = 2;
	document.pesquisa.tipo3.selectedIndex = 3;
	document.pesquisa.tipo4.selectedIndex = 4;
	document.pesquisa.item1.value = '';
	document.pesquisa.item2.value = '';
	document.pesquisa.item3.value = '';
	document.pesquisa.item4.value = '';
}

function maisorg () {
	if (document.pesquisa.orgao.value != '') {
		if (document.pesquisa.livre.value != '')
			if (document.pesquisa.livre.value.indexOf('.org.') != -1)
				document.pesquisa.livre.value=document.pesquisa.livre.value + ' OU "' + document.pesquisa.orgao.value + '".org.';
			else
				document.pesquisa.livre.value=document.pesquisa.livre.value + ' E "' + document.pesquisa.orgao.value + '".org.';			
		else
			document.pesquisa.livre.value='"' + document.pesquisa.orgao.value + '".org.';
	}
	document.pesquisa.orgao.value = '';
}

function adicionaItem(numeroItem){
	var str;
	str = "( (Art ADJ " + numeroItem + ") ou ";
	str += "(PAR ADJ " + numeroItem + ") ou ";
	str += "(INC ADJ " + numeroItem + ") ou ";
	str += "(LET ADJ " + numeroItem + ") ou ";						
	str += "(ITEM ADJ " + numeroItem + ") )";						
	return str;		
}

function limpar() {
	document.pesquisa.livre.value = '';
//	document.pesquisa.refinar.value = '';
	document.pesquisa.processo.value = '';
	document.pesquisa.relator.value = '';
	document.pesquisa.ementa.value = '';
	document.pesquisa.refLegNormaSigla.value = '';
//  document.pesquisa.orgao.value = '';
	document.pesquisa.refLegNormaNumero.value = '';
	document.pesquisa.refLegNormaAno.value = '';
	document.pesquisa.refLegNormaDescricao.value = '';
	document.pesquisa.numero_art1.value = '';
	document.pesquisa.numero_art2.value = '';
	document.pesquisa.numero_art3.value = '';
	document.pesquisa.numero_art4.value = '';
}
