var valor_minimo_imovel = "62,5 ";
var porcentagem_maxima_financiamento = "80% ";


// formata valor 
function formataValor(valor,decimais)
{
  var ndecimal;
  var campo = '';
  var resultado = '';
  var pos,sep,dec;  
  campo = valor.replace(".", ",");	      
  ndecimal = campo.indexOf(',');
  if (ndecimal != -1){
      sep = 0;
      for (pos=ndecimal-1;pos >= 0;pos--){
        sep ++;
        if (sep > 3){
           resultado = '.' + resultado;
           sep = 1;
        }
        resultado = campo.charAt(pos) + resultado;   
      }
      // Trata parte decimal
      if (parseInt(decimais) > 0 ){
         resultado = resultado + ',';      
         pos=ndecimal+1;
         for (dec = 1;dec <= parseInt(decimais); dec++){
           if (pos < campo.length){
              resultado = resultado + campo.charAt(pos);
              pos++;
           }
           else
              resultado = resultado + '0';   
         }
      } 
   }
   // Trata valor sem decimais
   else{
      sep = 0;
      for (pos=campo.length-1;pos >= 0;pos--){
        sep ++;
        if (sep > 3){
           resultado = '.' + resultado;
           sep = 1;
        }
        resultado = campo.charAt(pos) + resultado;   
      }     
      // Trata parte decimal
      if (parseInt(decimais) > 0 ){
         resultado = resultado + ',';
         for (dec = 1;dec <= parseInt(decimais); dec++) {
              resultado = resultado + '0';   
         }
      } 
   }
   return resultado;        
}

// Retorna percentual de financiamento para correntista ou nao correntista
function Percentual(p_perfil){
	if (p_perfil == 'S'){	// Correntista
		document.writeln(porcentagem_maxima_financiamento);
	}
	else{			// Nao correntista
		document.writeln(porcentagem_maxima_financiamento);	
	}
}

// retorna o valor da idade limite de Financiamento
function Get_idade(){
	document.writeln(parseInt(vidadeMeses)/12);
}

// Retorna taxa de juros (AO ANO) - SAC - imovel RESIDENCIAL < 350 mil - Usado para todos produtos ' Concertar isso
function Get_Taxa_Juros_ResidMenor(){
	document.writeln('11,5%');
}

// Retorna taxa de juros (AO ANO) - SAC - imovel RESIDENCIAL < 350 mil - Usado para todos produtos ' Concertar isso
function Get_Taxa_Juros_RedMenor(){
	document.writeln('12%');
}
// Retorna taxa de juros (AO ANO) - SAC - imovel RESIDENCIAL < 350 mi
function Get_Taxa_Juros_SFH_CH_Menor(){
	document.writeln('11,5%');
}
// Retorna taxa de juros (AO ANO) - SAC - imovel RESIDENCIAL > 350 mi
function Get_Taxa_Juros_ResidMaior(){
	document.writeln('12%');
}

// Retorna taxa de juros (AO ANO) - imovel COMERCIAL
function Get_Taxa_Juros_Comercial(){
	document.writeln('18%');
}

// Retorna taxa de juros (AO MES) - SAC - imovel RESIDENCIAL < 350 mi
function Get_Taxa_Juros_ResidMenor_AM(){
	document.writeln('0,91%');
}

// Retorna taxa de juros (AO MES) - Taxa Reduzida
function Get_Taxa_Juros_RedMenor_AM(){
	document.writeln('0,95%');
}


// Retorna taxa de juros (AO MES) - SAC - imovel RESIDENCIAL > 350 mi
function Get_Taxa_Juros_ResidMaior_AM(){
	document.writeln('0,95%');
}

// Retorna taxa de juros (AO MES) - imovel COMERCIAL
function Get_Taxa_Juros_Comercial_AM(){
	document.writeln('1,38%');
}

// Retorna valor multa apos carencia
function Get_Vl_Multa_Apos_Carencia(){
	document.writeln(vamorti);
}

// Retorna Percentual do Valor Amortizado
function Get_Perc_Vl_Amort(p_tipo){
	var aux = new Array();
	if (p_tipo == 'unica')
		aux = pu
	else
		aux = pd	
		
	document.writeln('<TABLE cellpadding="0" cellspacing="0" width="100" height="100%" border="0" align="left">');
	
	for (v_linha=0; v_linha<aux.length;v_linha++){
	
		if (aux[v_linha] != undefined){
			document.writeln('<TR>');
			document.writeln('<TD width="60" class="txt_cinza_n" align="right" valign="top" nowrap>' + v_linha + ' meses</TD><TD width="70" nowrap class="txt_cinza_n" valign="top">&nbsp;&nbsp;&nbsp;' + aux[v_linha] + '%'  + '</TD>');
			document.writeln('</TR>');			
			
			if (v_linha != (aux.length-1)) { 						
			
				document.writeln('<TR>')
				document.writeln('<TD colspan="2">')

				document.writeln('<table background="../../images/pontilhado_tabela.gif" border="0" cellspacing="0" cellpadding="0" width="100%">')
				document.writeln('<tr>')
				document.writeln('<td><img src="../../images/space.gif"  height="5" border="0"></td>')
				document.writeln('</TR>')	
				document.writeln('</table>');
				document.writeln('</TD>')
				document.writeln('<TR>')
			}					
		}		
	}
	document.writeln('</TABLE>');	
}

// Retorna taxa de juros unica ou decrescente - prestacao fixa
function Get_Taxa_Juros(p_tipo,p_prazo){
	
	var flag350 = false; 
	try {
		if (RetiraVirgPonto(frmSimula.valorImovel.value) >= 35000000){
			flag350 = true;
		}
		else{
			flag350 = false;
		}
		
	
	}
	catch(e){
		flag350 = false;
	}
	
		if (flag350 == true){
		
			var Tu350 = new Array();
			var Td350 = new Array();
			Tu350[60] = '18,64';
			Tu350[120] = '19,22';
			Tu350[180] = '19,61';
			Td350[120] = new Array();
			Td350[120][1] = '19,22';
			Td350[120][2] = '18,71';
			Td350[120][3] = '18,30';
			Td350[120][4] = '0,00';
			Td350[180] = new Array();
			Td350[180][1] = '19,86';
			Td350[180][2] = '19,35';
			Td350[180][3] = '18,84';
			Td350[180][4] = '18,34';
			Td350[180][5] = '17,84';
			
			if (p_tipo == 'unica'){
				document.writeln(Tu350[p_prazo] + '% a.a');
			}	

			if (p_tipo == 'decrescente'){

				var arr_Linhas = new Array();

				arr_Linhas[120] = new Array();
				arr_Linhas[120][0] = '1º ao 36º mês:';
				arr_Linhas[120][1] = '37º ao 72º mês:';
				arr_Linhas[120][2] = '73º ao 120º mês:';

				arr_Linhas[180] = new Array();
				arr_Linhas[180][0] = '1º ao 36º mês:';
				arr_Linhas[180][1] = '37º ao 72º mês:';
				arr_Linhas[180][2] = '73º ao 108º mês:';
				arr_Linhas[180][3] = '109º ao 144º mês:';
				arr_Linhas[180][4] = '145º ao 180º mês:';

				document.writeln('<TABLE cellpadding="0" cellspacing="0" width="100" height="100%" border="0" align="left">');

				for (v_linha=0; v_linha < arr_Linhas[p_prazo].length;v_linha++){
					
					document.writeln('<TR>');
					document.writeln('<TD class="txt_cinza_n" nowrap align="left" valign="top" width="100%">' + arr_Linhas[p_prazo][v_linha] + '&nbsp;' + Td350[p_prazo][v_linha + 1] + '% a.a'  + '</TD>');
					document.writeln('</TR>');
				}

				document.writeln('</TABLE>');
			}
			
		}
		else{
		
	
			if (p_tipo == 'unica'){
				document.writeln(Tu[p_prazo] + '% a.a');
			}	

			if (p_tipo == 'decrescente'){

				var arr_Linhas = new Array();

				arr_Linhas[120] = new Array();
				arr_Linhas[120][0] = '1º ao 36º mês:';
				arr_Linhas[120][1] = '37º ao 72º mês:';
				arr_Linhas[120][2] = '73º ao 120º mês:';

				arr_Linhas[180] = new Array();
				arr_Linhas[180][0] = '1º ao 36º mês:';
				arr_Linhas[180][1] = '37º ao 72º mês:';
				arr_Linhas[180][2] = '73º ao 108º mês:';
				arr_Linhas[180][3] = '109º ao 144º mês:';
				arr_Linhas[180][4] = '145º ao 180º mês:';

				document.writeln('<TABLE cellpadding="0" cellspacing="0" width="100" height="100%" border="0" align="left">');

				for (v_linha=0; v_linha < arr_Linhas[p_prazo].length;v_linha++){
					
					document.writeln('<TR>');
					document.writeln('<TD class="txt_cinza_n" nowrap align="left" valign="top" width="100%">' + arr_Linhas[p_prazo][v_linha] + '&nbsp;' + Td[p_prazo][v_linha + 1] + '% a.a'  + '</TD>');
					document.writeln('</TR>');
				}

				document.writeln('</TABLE>');
			}
		
		}
}


function Get_prazos(p_produto,p_estsel,p_cidsel){
	document.frmSimula.hPrazosPrestFixaU.value="";
	document.frmSimula.hPrazosPrestFixaD.value="";
	// Busca os Prazos do Produto Selecionado
	for (i=0;i<pu.length;i++){
			if(pu[i] != undefined){
					if  (document.frmSimula.hPrazosPrestFixaU.value== "") {
						document.frmSimula.hPrazosPrestFixaU.value = i 
						}
					else
						{
							document.frmSimula.hPrazosPrestFixaU.value = document.frmSimula.hPrazosPrestFixaU.value + ',' + i 
						}
			}
	}
	for (i=0;i<pd.length;i++){
			if(pd[i] != undefined){
				if  (document.frmSimula.hPrazosPrestFixaD.value== "") {
						document.frmSimula.hPrazosPrestFixaD.value = i 
						}
					else
						{
							document.frmSimula.hPrazosPrestFixaD.value = document.frmSimula.hPrazosPrestFixaD.value + ',' + i 
						}
			}
		}
}

// retorna valor minimo do imóvel a ser financiado - prestações fixas
function Get_Valor_Mini_PrestFixa()
{
	document.write(formataValor(vminiprestfixa,2));
}

// retorna valor minimo do imovel a ser financiado - SAC
function Get_Valor_Mini()
{
	document.write(formataValor(vmini,2));
}

//retorna valor maximo do imovel a ser financiamento - Taxa reduzida
// Get que tem esta sendo utilizado, porem temos que tirar e usar o que esta abaixo
function Get_Valor_Max_TaxaRed()
	{
		document.write(formataValor(vmaxired,2));
		//document.write("120.000,00");
}
//retorna valor maximo do imovel a ser financiamento - Taxa reduzida 8,5 %
function Get_Valor_Max_TaxaRed1(tipo) //100.000,00
{
	// tipo por ser n = numerico ou m = com descrição dos Milhares
	if (tipo=="n"){document.write(formataValor(vmaxired,2));}
	else if (tipo=="m"){DescreveValor(formataValor(vmaxired,2));}
	//document.write(formataValor(vmaxired,2));
	//document.write("120.000,00");
}
//retorna valor maximo do imovel a ser financiamento - Taxa reduzida 8,5 %
function Get_Valor_Max_TaxaRed2(tipo) // 120.000,00
{
	// tipo por ser n = numerico ou m = com descrição dos Milhares
	if (tipo=="n"){document.write(formataValor(vmaxired_MaiorTaxa,2));}
	else if (tipo=="m"){DescreveValor(formataValor(vmaxired_MaiorTaxa,2));}
	//document.write(formataValor(vmaxired_MaiorTaxa,2));
}

//retorna valor maximo do imovel a ser financiamento - Taxa reduzida 9 %
function Get_Valor_Max_TaxaRed3(tipo) // 120.000,00
{
	// tipo por ser n = numerico ou m = com descrição dos Milhares
	if (tipo=="n"){document.write(formataValor("120000,00",2));}
	else if (tipo=="m"){DescreveValor(formataValor("120000,00",2));}
	//document.write(formataValor(vmaxired_MaiorTaxa,2));
}


/* Retorna valor minimo a ser financiado 20.000 */
function Get_Valor_Min_Financ(tipo) // 20.000,00
{
	// tipo por ser n = numerico ou m = com descrição dos Milhares
	if (tipo=="n"){
		document.write(formataValor(vminfin,2));
	}
	else if (tipo=="m"){
		DescreveValor(formataValor(vminfin,2));
	}	
}
//Retorna a taxa anual FGTS com vínculo
function Get_Taxa_Fgts_Cvin()
{
	document.write(taxa_fgts_cvin);
}

//Retorna a taxa anual FGTS sem vínculo
function Get_Taxa_Fgts_Svin()
{
	document.write(taxa_fgts_svin);
}
// retorna a taxa Reduzida menor - 8%
function Get_Taxa_Red1() // 8%
{
	//document.write(porc_red_MenorTaxa);
	Limpa(porc_red_MenorTaxa);
}

// retorna a taxa Reduzida menor - 8,5%
function Get_Taxa_Red2() // 8,5%
{
	
	Limpa(porc_red_MaiorTaxa);
}

// retorna a taxa Reduzida 3 - 9%
function Get_Taxa_Red3() // 9%
{
	//Limpa(porc_red_MaiorTaxa);
	document.write("9");
}

// Retorna a taxa Juros Efetiva Mensal para 


// Prazo Maximo para Prestação Fixa em meses
function Get_Prazo_Max_Prest_Meses()
{	
	document.write(180);	
}

// Prazo Maximo para Prestação Fixa em anos 
function Get_Prazo_Max_Prest_Anos()
{	
	document.write((180)/12);	
}

// retorna prazo máximo de financiamento (em anos)
function Get_Prazo_Max_Finan_Anos()
{	
	// document.write((pu.length-1)/12);
	document.write((300)/12);	
}

// retorna prazo máximo de financiamento (em meses)
function Get_Prazo_Max_Finan_Meses()
{	
	//document.write(pu.length-1);
	document.write("300");
}

// retorna prazo máximo de financiamento (em meses) para Taxa Reduzida -8,5%
function Get_Prazo_Max_TaxaRed_Meses() 
{	
	//Limpa(prazomaxred_maiorTaxa)
	document.write("60");
}

// retorna prazo máximo de financiamento (em meses) para Taxa Reduzida - 8%
function Get_Prazo_Mini_TaxaRed_Meses()
{	
	//prazored
	Limpa(prazored)
	//document.write("36");
}

// retorna prazo máximo de financiamento (em meses) para Taxa Reduzida - 9%
function Get_Prazo_TaxaRed3_Meses()
{	
	//Limpa(prazored)
	document.write("300");
}

function Get_Prazo_TaxaRed3_Max_Finan_Anos()
{
	document.write((300)/12);
}


// retorna prazo máximo de financiamento (em meses) para Taxa Reduzida
function Get_Prazo_Maxi_Comercial_Meses()
{	
	//prazomaxfin_com
	Limpa(prazomaxfin_com)
	//document.write("144");
}

function Get_Prazo_Maxi_Comercial_Anos()
{
	document.write(144/12);
}

// retorna Taxa Efetiva Mensal para Taxa Reduzida 9% - 0,72%
function Get_TaxaEfetivaMensal3() 
{	
	document.write("0,72");
}

// retorna valor minimo da prestação - Prestações Fixas
function Get_Valor_Mini_Prest()
{
//	alert(vminprest)
	document.write(formataValor(vminprest,2));
}

// retorna % de compromentimento de renda
function Get_PercentualCompRenda()
{
	document.write("35%");
}
//Tira os zeros que estão vindo a esquerda da porcentagem ou de outro numero 
function Limpa(valor)
{
	var ndecimal;
	var NewValor;
	var Valoraux;
	var aux = new Array();
	aux = valor.split(",");
	NewValor = aux[0];
	Valoraux="";
	if (aux[1].substring(2,1)!=0)// Verifica se a segunda casa tem valor diferente que zero - se tiver retorno todo valor.
		{Valoraux=aux[1];}
	else{
			for (i=0;i<aux[1].length;i++){
					if ((aux[1].substring(i,i+1))!=0) {Valoraux = Valoraux + aux[1].substring(i,i+1);}
				}
		}
	if (Valoraux!=""){ Valoraux="," + Valoraux;} 
	NewValor = aux[0] + Valoraux;
	document.write(NewValor); 
}

function DescreveValor(valor)
	{
		var QuebraValor = new Array();
		var QuebraMilhares  = new Array();
		var NewValor,Descritivo;
		Descritivo="";
		QuebraValor = valor.split(",");
		QuebraMilhares = QuebraValor[0].split(".");
		if (QuebraMilhares.length==1){ // Retorna o valor com ,00 pois veioo valor do tipo 234,00
				NewValor=valor;
			}
		else{
			switch (QuebraMilhares.length)
				{ 
					case 2:
						Descritivo="mil";
						break;
					case 3:
						Descritivo="Milhão";
						break;
					case 4:
						Descritivo="Bilhão";
						break;
				}
			
			NewValor=QuebraMilhares[0] + " " + Descritivo; 
		}
		document.write(NewValor); 
	}											

function limpaMeses(valor)
	{
		var aux	= new Array();
		aux = valor.split(",");
		aux[0]=aux[0]/12; 
		document.write(aux[0]); 
	}
function Get_VMSACH() 
{	
	// Antiga tarifa document.write("26,51");
	document.write("25,00");
}

// Get para produto Funding - FGTS itaú

// Valor do Imovel
function Get_vlMin_FGTS(tipo) // 40.000,00
{
	// tipo por ser n = numerico ou m = com descrição dos Milhares
	if (tipo=="n"){
		document.write(formataValor(vminiFGTS,2));
		//document.write("40.000,00");
	}
	else if (tipo=="m"){
		DescreveValor(formataValor(vminiFGTS,2));
		//document.write("40 mil");
	}	
}


function Get_vlMax_FGTSUsado(tipo) // 120.000,00
{
	// tipo por ser n = numerico ou m = com descrição dos Milhares
	if (tipo=="n"){
		document.write(formataValor(vmaxFGTSUsado,2));
		//document.write("120.000,00");
	}
	else if (tipo=="m"){
		DescreveValor(formataValor(vmaxFGTSUsado,2));
		//document.write("120 mil");
	}	
}

function Get_vlMax_FGTS(tipo) // 120.000,00
{
	// tipo por ser n = numerico ou m = com descrição dos Milhares
	if (tipo=="n"){
		document.write(formataValor(vmaxFGTS,2));
		//document.write("120.000,00");
	}
	else if (tipo=="m"){
		DescreveValor(formataValor(vmaxFGTS,2));
		//document.write("120 mil");
	}	
}

// VAlor de Financiamento 
function Get_FincMin_FGTS(tipo) // 25.000,00
{
	// tipo por ser n = numerico ou m = com descrição dos Milhares
	if (tipo=="n"){
		document.write(formataValor(vFincminFGTS,2));
		//document.writeln("25.000,00");
	}
	else if (tipo=="m"){
		DescreveValor(formataValor(vFincminFGTS,2));
		//document.writeln("25 mil");
	}	
}

// Renda bruta 
function Get_RendaMax_FGTSUsado() // 4.900,00
{
	document.write(formataValor(vrendamaxFGTSUsado,2));
	//document.write("4.900,00");
}

// Renda bruta 
function Get_RendaMax_FGTS() // 4.900,00
{
	document.write(formataValor(vrendamaxFGTS,2));
	//document.write("4.900,00");
}
function Get_RendaMin_FGTS() // 380,00 - Não parametrizada P8
{
	document.write(formataValor(vrendaminFGTS,2));
	//document.write("1875,00");
}

//Prazo(meses) FGTS
function Get_Prazo_FGTS()
{
	document.write(prazo_ms_fgts);
}

//Prazo Máximo de Financiamento 25 anos ( 300 meses)
function Get_Prazo_Max_FGTS_Meses()
{	
	Limpa(prazomaxfin_res);
	//document.write(300);	
}

// Prazo Maximo de Financiamento em anos 
function Get_Prazo_Max_FGTS_Anos()
{	
	limpaMeses(prazomaxfin_res);
	//document.write(300/12);	
}

// Tarifa inicial 1%  
function Get_tarifaInicial_FGTS()
{	
	//document.write("1");
	//document.write(vtaxaIniFGTS);
	Limpa(vtaxaIniFGTS)	
}

// Taxa de administração 21,43 
function Get_vmsaFGTS(){
	//document.writeln("21,43");
	 document.writeln(vmsaFGTS);
}


//Renda Familiar com conta vinculada com FGTS a mais de 3 anos - de 380 a 4900,00 
function Get_txAnual3anos_FGTS1(){
	//document.writeln("7,97"); 
	document.writeln(vanual3anosFGTS1);
}

function Get_txmensal3anos_FGTS1(){
	//document.writeln("0,64"); 
	document.writeln(vmes3anosFGTS1);
}

// Renda Familiar não tem conta vinculada com FGTS a mais de 3 anos  - de 380,00 a 4900,00 
function Get_txAnual_FGTS1(){
	//document.writeln("8,47"); 
	document.writeln(vanualFGTS1);
}
function Get_txmensal_FGTS1(){
	//document.writeln("0,68");
	document.writeln(vmesFGTS1);
}

function  Get_AnosVinculoFGTS(){
	//document.write("3");
	//document.writeln(AnosVinculoFGTS);
	Limpa(AnosVinculoFGTS)
}