//Java Script Document
var requisicao=null;
var valor=0;

function inicializarCalculoCustasJudiciais()
{
document.getElementById("tipoAcao").focus();
}

function verificarCampos()
{
document.getElementById("nomeParte").value=trim(document.getElementById("nomeParte").value);	

if (document.getElementById("tipoAcao").selectedIndex==0)
	{
	alert("Preencha o campo <Tipo de ação>!");
	document.getElementById("tipoAcao").focus();
	return false;
	}

if (document.getElementById("valorCausa").value=="")
	{
	alert("Preencha o campo <Valor da causa>!");
	document.getElementById("valorCausa").focus();
	document.getElementById("valorCausa").select();
	return false;
	}	

if (document.getElementById("nomeParte").value=="")
	{
	alert("Preencha o campo <Nome da parte>!");
	document.getElementById("nomeParte").focus();
	document.getElementById("nomeParte").select();
	return false;
	}	
	
if (document.getElementById("cpfcnpj").value=="")
	{
	alert("Preencha o campo <CPF/CNPJ>!");
	document.getElementById("cpfcnpj").focus();
	document.getElementById("cpfcnpj").select();
	return false;
	}		

var telefone=document.getElementById("telefone").value;			
	
if (document.getElementById("telefone").value=="")
	{
	alert("Preencha o campo <Telefone>!");
	document.getElementById("telefone").focus();
	document.getElementById("telefone").select();
	return false;
	}	
	
if ((telefone.length!=8) && (telefone.length!=10))
	{
	alert("<Telefone> incompleto!");
	document.getElementById("telefone").focus();
	document.getElementById("telefone").select();
	return false;
	}
	
if ((telefone.substring(0,1)!="3") && (telefone.substring(0,1)!="8"))	
	{
	alert("<Telefone> inválido!");
	document.getElementById("telefone").focus();
	document.getElementById("telefone").select();
	return false;
	}

return true;
}

function limpar()
{
document.getElementById("tipoCalculo").checked=true;
document.getElementById("tipoAcao").selectedIndex=0;
document.getElementById("valorCausa").value="";
document.getElementById("nomeParte").value="";
document.getElementById("cpfcnpj").value="";
document.getElementById("telefone").value="";
document.getElementById("codigoReceita").value="5762";
document.getElementById("valorCausa").value="";
document.getElementById("divValorCustas").innerHTML="Valor das custas: 0,00";
document.getElementById("tipoAcao").focus();
}

function calcular()
{
if (verificarCampos()==false)
	return false;

var url="ajaxCalculoCustasJudiciais.jsp?tipoAcao="+document.getElementById("tipoAcao").value+"&valorCausa="+document.getElementById("valorCausa").value;
requisicao=iniciarRequisicao();
requisicao.onreadystatechange=respostaCalcular;
requisicao.open("GET",url,false);
requisicao.send(null);
}

function respostaCalcular() 
{	
if (requisicao.readyState==4) 
	{
	if (requisicao.status==200) 
		{
		var xml=requisicao.responseXML;
		var html="Valor das custas: ";
		var registros=xml.getElementsByTagName("registros")
		
		valor=registros[0].getElementsByTagName("valor").item(0).firstChild.data;
		html+=valor;

		document.getElementById("divValorCustas").innerHTML="<h5>"+html+"</h5>";
		}
		else
		alert("Erro interno ao calcular as custas: "+requisicao.status)		
 	}
}

function alterarCodigoReceita(tipoCalculo)
{
document.getElementById("tipoCalculo").value=tipoCalculo;
	
if (tipoCalculo==0)
	document.getElementById("codigoReceita").value="5762";
	else
	document.getElementById("codigoReceita").value="5775";
}

function imprimir()
{
if (verificarCampos()==true)
	{		
	calcular();
	window.open("impressaoDARF.jsp?tipoCalculo="+document.getElementById("tipoCalculo").value+"&nomeParte="+document.getElementById("nomeParte").value+"&telefone="+document.getElementById("telefone").value+"&cpfcnpj="+document.getElementById("cpfcnpj").value+"&codigoReceita="+document.getElementById("codigoReceita").value+"&valor="+valor);
	}
}
