// JavaScript Document//essa funcao ja tem no arquivo mas serve para o site q tem que ter os links de forma diferente
function Mandavaloresite(prazo,valor,prestacao){
 if (document.cadastro.vemprestimo.value != '' && document.cadastro.vparcela.value != ''){
	 alert("Escolha ou o Valor do emprestimo ou o Valor da prestação");
 }
 
 
 calculasite('acoes/calcula.php?prazo='+prazo+'&valor='+valor+'&prestacao='+prestacao);

}
function calculasite(url){
	
	httpajax = null;
	
	
	if (window.XMLHttpRequest){
		httpajax = new XMLHttpRequest();
		document.getElementById("resultado").innerHTML= '';
		httpajax.onreadystatechange = calcularsite;
		httpajax.open("GET", url, true);
		httpajax.send(null);
	   
	}
	else if (window.ActiveXObject) {
		httpajax = new ActiveXObject("Microsoft.XMLHTTP");
		if (httpajax) { 
		document.getElementById("resultado").innerHTML= '';
		httpajax.onreadystatechange = calcularsite;
		httpajax.open("GET",  url , true);
		httpajax.send();
		}
	}
}
function calcularsite(){
 var mensagem =  document.getElementById('msg'); 
	if (httpajax.readyState == 1){
		mensagem.innerHTML = "Calculando...!";
	}
	
	if (httpajax.readyState == 4) {
		mensagem.innerHTML = "";
		if (httpajax.status == 200){ 
		
		var dataArrayPrazo   = httpajax.responseXML.getElementsByTagName("prazo");		
		
		     
			var item = dataArrayPrazo[0];
			
			//contéudo dos campos no arquivo XML
			var codigo    =  item.getElementsByTagName("conta")[0].firstChild.nodeValue;
			var emprestimo    =  item.getElementsByTagName("emprestimo")[0].firstChild.nodeValue;
			
			
			//codigo  = number_format(codigo, 2, ',', '.');
			if (codigo == 0.00 ){				
	        document.getElementById("resultado").innerHTML= "Nenhum fator cadastrado, favor cadastrar";			
			}
						
			if (emprestimo == 1 ){
			codigo  = number_format(codigo, 2, ',', '.'); 	
	        document.getElementById("resultado").innerHTML= "O Valor da prestação é R$ "+codigo;			
	
			}
			else {
			codigo  = number_format(codigo, 2, ',', '.'); 	
	        document.getElementById("resultado").innerHTML= "O Valor liberado do empréstimo é R$ "+codigo;
			}
			
			
		      }//fecha if
		 }//fecha if
			  else {}
		}
	
function MandaParcelasite(tipo,banco){
 PreenchePrazosite('acoes/preenchePrazo.php?tipo='+tipo+'&banco='+banco);
}
function PreenchePrazosite(url){
	
	httpx = null;
	
	
	if (window.XMLHttpRequest){
		httpx = new XMLHttpRequest();
		document.cadastro.prazo.options.length = 1;
		httpx.onreadystatechange = tratarDadosPrazosite;
		httpx.open("GET", url, true);
		httpx.send(null);
	   
	}
	else if (window.ActiveXObject) {
		httpx = new ActiveXObject("Microsoft.XMLHTTP");
		if (comunicacaoP) { 
		document.cadastro.prazo.options.length = 1;
		httpx.onreadystatechange = tratarDadosPrazosite;
		httpx.open("GET",  url , true);
		httpx.send();
		}
	}
}
function tratarDadosPrazosite(){
 var mensagem =  document.getElementById('msg'); 
	if (httpx.readyState == 1){
		mensagem.innerHTML = "Carregando...!";
	}
	
	if (httpx.readyState == 4) {
		mensagem.innerHTML = "";
		if (httpx.status == 200){ 
		var dataArrayPrazo   = httpx.responseXML.getElementsByTagName("prazo");		
		
		if(dataArrayPrazo.length > 1) {  
	 
         for(var i = 0 ; i < dataArrayPrazo.length ; i++) {
            
			var item = dataArrayPrazo[i];
			
			//contéudo dos campos no arquivo XML
			var codigo    =  item.getElementsByTagName("codigo")[0].firstChild.nodeValue;
			var descricao =  item.getElementsByTagName("descricao")[0].firstChild.nodeValue;
			
	        			
			//cria um novo option dinamicamente  
			var novo = document.createElement("option");
			    //atribui um ID a esse elemento
			    novo.setAttribute("id", "prazo");
				//atribui um valor
			    novo.value = codigo;
				//atribui um texto
			    novo.text  = descricao;
				//finalmente adiciona o novo elemento
				document.cadastro.prazo.options.add(novo);
		      }
		 }
			  else {}
		}
	}
}


function AtualizaBanco(){
	PreencheB("acoes/preencheBanco.php");
}

function PreencheB(url){
	
	comunicacaoB = null;
	
	if (window.XMLHttpRequest){
		comunicacaoB = new XMLHttpRequest();
		comunicacaoB.onreadystatechange = tratarBanco;
		comunicacaoB.open("GET", url, true);
		comunicacaoB.send(null);
	   
	}
	else if (window.ActiveXObject) {
		comunicacaoB = new ActiveXObject("Microsoft.XMLHTTP");
		if (comunicacaoP) { 
		comunicacaoB.onreadystatechange = tratarBanco;
		comunicacaoB.open("GET", url , true);
		comunicacaoB.send();
		}
	}
}

function tratarBanco(){
  	if (comunicacaoB.readyState == 4) {
		if (comunicacaoB.status == 200){ 
		var dataArray3   = comunicacaoB.responseXML.getElementsByTagName("banco");		
		
		if(dataArray3.length > 0) {  
	 
         for(var i = 0 ; i < dataArray3.length ; i++) {
            
			var item = dataArray3[i];
			
			//contéudo dos campos no arquivo XML
			var codigo    =  item.getElementsByTagName("codigo")[0].firstChild.nodeValue;
			var descricao =  item.getElementsByTagName("descricao")[0].firstChild.nodeValue;
			
	        			
			//cria um novo option dinamicamente  
			var novo = document.createElement("option");
			    //atribui um ID a esse elemento
			    novo.setAttribute("id", "banco");
				//atribui um valor
			    novo.value = codigo;
				//atribui um texto
			    novo.text  = descricao;
				//finalmente adiciona o novo elemento
				document.cadastro.banco.options.add(novo);
		      }
		 }
			  else {
				
			}
		}
	}
}

function number_format( number, decimals, dec_point, thousands_sep ) {
    
 
    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "." : dec_point;
    var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}


