// JavaScript Document
function trim(str) {
	var strAux = str.replace(/^\s*/, "").replace(/\s*$/, "");
	return strAux.replace(/^\xA0*/, "").replace(/\xA0*$/, "");
}

function apagaEspacos() {
	var els = document.getElementById('form').elements;
	for(var i=0;i<els.length;i++) {
		if(els[i].value && trim(els[i].value).length==0)
			els[i].value='';
		if(els[i].type == 'select-one' && trim(els[i].options[0].value).length==0)
			els[i].options[0].value='';
	}
}
var MSG_DATA_INVALIDA="Data Inváida";
var MSG_FORMULARIO_IVALIDO="É necessário selecionar pelo menos um tribunal e preencher um dos campos de pesquisa!";
var FMT_DATA="dd/MM/yyyy";
function criaMascaraData(id) {
	new DateMask(FMT_DATA, id).validationMessage=MSG_DATA_INVALIDA;
}
function abrePopup(titulo, url) {
	window.open(url,titulo,
	'resizable=false,scrollbars=yes,toolbar=false,location=false,directories=false,status=false,menubar=false,width=735,height=500');
}
function getElementoPorId(id) {
	return document.getElementById(id);
}
function getElementoPorNome(nome) {
	var elements = document.getElementsByName(nome);
	var element = null;
	if(elements.length > 0)
		element = elements[0];
	return element;
}

//Funções para manipulação dos popups.
function fechar(idPopup){
    popup.style.display = 'none';
	getElementoPorId('fundo').style.display = 'none';
	document.body.style.overflow='';
}

function fechar_sf(idPopup){
    popup_sf.style.display = 'none';
	//getElementoPorId('fundo').style.display = 'none';
	//document.body.style.overflow='auto';
}

function abrir(idPopup){
	popup = getElementoPorId(idPopup)
	
	var sobra = ((document.body.clientHeight - 406)/2);
	popup.style.top = sobra + 'px';
	popup.style.bottom = sobra + 'px';

    var sobraw = ((document.body.clientWidth - 755)/2);
    popup.style.left = sobraw + 'px';
    popup.style.right = sobraw; + 'px';

    popup.style.display = 'block';
	document.getElementById('fundo').style.display = 'block';
	if(document.getElementById('item_resultado')!=null){
		document.getElementById('item_resultado').style.display = 'block';
	}
	
	document.body.style.overflow='hidden';
	posicionaPopup();

}
function abrir_sf(idPopup){
	popup_sf = getElementoPorId(idPopup)
	
	var sobra = ((document.body.clientHeight - 406)/2);
	popup_sf.style.top = sobra + 'px';
	popup_sf.style.bottom = sobra + 'px';

    var sobraw = ((document.body.clientWidth - 755)/2);
    popup_sf.style.left = sobraw + 'px';
    popup_sf.style.right = sobraw; + 'px';

    popup_sf.style.display = 'block';
	document.getElementById('fundo').style.display = 'block';
	if(document.getElementById('item_resultado')!=null){
		document.getElementById('item_resultado').style.display = 'block';
	}
	
	document.body.style.overflow='hidden';
	if(popup_sf != null) {
		sobra = ((document.documentElement.clientHeight - 459)/2);
		if(sobra < 0) sobra = 0;
		var top = document.documentElement.scrollTop;
		popup_sf.style.top = top + sobra + 'px';

		sobraw = ((document.body.clientWidth - 670)/2);
		popup_sf.style.left = sobraw + 'px';
	}

}
function posicionaPopup() {
	if(popup != null) {
		var sobra = ((document.documentElement.clientHeight - 459)/2);
		if(sobra < 0) sobra = 0;
		var top = document.documentElement.scrollTop;
		popup.style.top = top + sobra + 'px';

		var sobraw = ((document.body.clientWidth - 670)/2);
		popup.style.left = sobraw + 'px';
	}
}
function imprimir() {
	var popupImp = window.open('../popupImpressao.jsp','_blank',
		'resizable=false,scrollbars=yes,toolbar=false,location=false,directories=false,status=false,menubar=false,width=650,height=410');
}
function alternaAbas(idAba1, idConteudo1, idAba2, idConteudo2){
	document.getElementById(idAba1).style.height='19px';
	document.getElementById(idConteudo1).style.display = 'block';
	document.getElementById(idConteudo2).style.display = 'none'
	document.getElementById(idAba2).style.height='18px';
}

function OmiteConteudoAvancado(idCheck, idLabelSimples, idLabelAvancada)
{

		
		if(document.getElementById("pesquisaAvancada").style.display='none')
		{
			document.getElementById("pesquisaAvancada").style.display='block';
			document.getElementById(idLabelAvancada).style.display='none';
			document.getElementById(idLabelSimples).style.display='block';
			document.getElementById(idLabelSimples).style.marginLeft='555px';
		}
		
	
}

function OmiteConteudoSimples(idCheck, idLabelSimples, idLabelAvancada)
{
	
		if(document.getElementById("pesquisaAvancada").style.display='block')
		{
			document.getElementById("pesquisaAvancada").style.display='none';
			document.getElementById(idLabelAvancada).style.display='block';
			document.getElementById(idLabelSimples).style.display='none';
		}
		
}

function escolhaDaPesquisa(tipo){
	document.getElementById('pesquisaAS').value = tipo;
}

function retornaTipoPesquisa(){
	if(document.getElementById('pesquisaAS').value != null){
	if(document.getElementById('pesquisaAS').value == 'A'){
		OmiteConteudoAvancado('ckbMostra', 'ckbOmite', 'ckbMostra');
	}else{
		OmiteConteudoSimples('ckbMostra', 'ckbOmite', 'ckbMostra');
	}
	}
}
