function AscoobSld(id){
    $('.boxSld').slideUp('4000');
    $('#'+id).slideDown('3000');
}
function popUpWin(URL) {
    var width = 600;
    var height = 600;
    window.open(URL,'nome', 'width='+width+', height='+height+', scrollbars=yes, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no');
}

function parcelas(){
    $('#resultado-calculo').html('<div style="text-align: center;" >Aguarde Calculando.... <img src="'+URL+'imgs/ajax-loader.gif"></div>')
    $.post(URL+"ajax/parcelas.php", { 
        meses : $('#meses').val(), 
        prestacao: $('#prestacao').val(),
        montante: $('#montante').val(),
        juros: $('#juros').val(),
        mes_carencia: $('#mes_carencia').val(),
        tipo_cred: $('#tipo_cred').val()
    }, function(data) {
        $('#resultado-calculo').html(data)
    });
}


function checkFormToSend(){
	//alert('aqui');
	if(
		$('#montante').val() == '' ||
		$('#meses').val() == ''  ||
		$('#prestacao').val() == ''  ||
		$('#nome').val() == ''  ||
		$('#email').val() == ''  ||
		$('#cpf').val() == ''  ||
		$('#endereco').val() == ''  ||
		$('#cel').val() == ''  ||
        $('#tel').val() == '' 
	){
		return false;
	} else {
		return true;
	}

}

function sendToEamil(){
	if(checkFormToSend()==true){
		var juros = $('#juros').find('option').filter(':selected').text(); 
		$('#statusEnvioEmail').html('<div style="text-align: center;" >Aguarde Calculando.... <img src="'+URL+'imgs/ajax-loader.gif"></div>')
		$.post(URL+"ajax/sendToEmail.php", { 
			montante:$('#montante').val(),
			juros:juros,
			agencia:$('#agencia').val(),
			meses:$('#meses').val(),
			prestacao:$('#prestacao').val(),
			tabela: $('#resultado-calculo').html(),
			nome: $('#nome').val(),
			email: $('#email').val(),
			cpf: $('#cpf').val(),
			endereco: $('#endereco').val(),
			cel: $('#cel').val(),
			tel: $('#tel').val()
		}, function(data) {
			$('#statusEnvioEmail').html('');
			alert('Sua proposta foi enviada com sucesso \n Em breve entraremos em contato.');
		});
	} else {
		alert('Preencha todos os campos');
	}
}
/* --- */



var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var whitespace = " \t\n\r";
var decimalPointDelimiter = "."


var iDay = "O dia da data deve estar entre 1 e 31.  Por favor corrija."
var iMonth = "O mês da data deve estar 1 e 12.  Por favor corrija."
var icpfcgc = "O CPF/CNPJ é inválido. Por favor corrija."
var iYear = "O ano deve ter 2 ou 4 dígitos.  Por favor corrija."
var iDatePrefix = ""
var iDateSuffix = " não é uma data válida.  Por favor corrija."
var pEntryPrompt = "Por favor entre um "
var pDay = "número de dia entre 1 e 31."
var pMonth = "número de mês entre 1 e 12."
var pYear = "número de ano com 2 ou 4 dígitos."
var defaultEmptyOK = false


function makeArray(n) {
    //*** BUG: If I put this line in, I get two error messages:
    //(1) Window.length can't be set by assignment
    //(2) daysInMonth has no property indexed by 4
    //If I leave it out, the code works fine.
    //   this.length = n;
    for (var i = 1; i <= n; i++) {
        this[i] = 0
    } 
    return this
}



var daysInMonth = makeArray(12);
daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;



function isEmpty(s)
{
    return ((s == null) || (s.length == 0))
}


function isWhitespace (s)

{
    var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}



// Removes all characters which appear in string bag from string s.

function stripCharsInBag (s, bag)

{
    var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

function stripCharsNotInBag (s, bag)

{
    var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }

    return returnString;
}


function stripWhitespace (s)

{
    return stripCharsInBag (s, whitespace)
}


function charInString (c, s)
{
    for (i = 0; i < s.length; i++)

    {
            if (s.charAt(i) == c) return true;
        }
    return false
}


function stripInitialWhitespace (s)

{
    var i = 0;

    while ((i < s.length) && charInString (s.charAt(i), whitespace))
        i++;
    
    return s.substring (i, s.length);
}

function isLetter (c)
{
    return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

function isDigit (c)
{
    return ((c >= "0") && (c <= "9"))
}

function isLetterOrDigit (c)
{
    return (isLetter(c) || isDigit(c))
}

function isInteger (s)

{
    var i;

    if (isEmpty(s)) 
        if (isInteger.arguments.length == 1) return defaultEmptyOK;
        else return (isInteger.arguments[1] == true);


    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}


function isSignedInteger (s)

{
    if (isEmpty(s)) 
        if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
        else return (isSignedInteger.arguments[1] == true);

    else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        if (isSignedInteger.arguments.length > 1)
            secondArg = isSignedInteger.arguments[1];

        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
            startPos = 1;    
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}

function isPositiveInteger (s)
{
    var secondArg = defaultEmptyOK;

    if (isPositiveInteger.arguments.length > 1)
        secondArg = isPositiveInteger.arguments[1];


    return (isSignedInteger(s, secondArg)
        && ( (isEmpty(s) && secondArg)  || (parseInt (s,10) > 0) ) );
}

function isNonnegativeInteger (s)
{
    var secondArg = defaultEmptyOK;

    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];

    return (isSignedInteger(s, secondArg)
        && ( (isEmpty(s) && secondArg)  || (parseInt (s,10) >= 0) ) );
}


function isNegativeInteger (s)
{
    var secondArg = defaultEmptyOK;

    if (isNegativeInteger.arguments.length > 1)
        secondArg = isNegativeInteger.arguments[1];

    // The next line is a bit byzantine.  What it means is:
    // a) s must be a signed integer, AND
    // b) one of the following must be true:
    //    i)  s is empty and we are supposed to return true for
    //        empty strings
    //    ii) this is a negative, not positive, number

    return (isSignedInteger(s, secondArg)
        && ( (isEmpty(s) && secondArg)  || (parseInt (s,10) < 0) ) );
}


function isNonpositiveInteger (s)
{
    var secondArg = defaultEmptyOK;

    if (isNonpositiveInteger.arguments.length > 1)
        secondArg = isNonpositiveInteger.arguments[1];

    // The next line is a bit byzantine.  What it means is:
    // a) s must be a signed integer, AND
    // b) one of the following must be true:
    //    i)  s is empty and we are supposed to return true for
    //        empty strings
    //    ii) this is a number <= 0

    return (isSignedInteger(s, secondArg)
        && ( (isEmpty(s) && secondArg)  || (parseInt (s,10) <= 0) ) );
}


function isFloat (s)

{
    var i;
    var seenDecimalPoint = false;

    if (isEmpty(s)) 
        if (isFloat.arguments.length == 1) return defaultEmptyOK;
        else return (isFloat.arguments[1] == true);

    if (s == decimalPointDelimiter) return false;

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}


function isSignedFloat (s)

{
    if (isEmpty(s)) 
        if (isSignedFloat.arguments.length == 1) return defaultEmptyOK;
        else return (isSignedFloat.arguments[1] == true);

    else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        if (isSignedFloat.arguments.length > 1)
            secondArg = isSignedFloat.arguments[1];

        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
            startPos = 1;    
        return (isFloat(s.substring(startPos, s.length), secondArg))
    }
}

function isAlphabetic (s)

{
    var i;

    if (isEmpty(s)) 
        if (isAlphabetic.arguments.length == 1) return defaultEmptyOK;
        else return (isAlphabetic.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-alphabetic character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is letter.
        var c = s.charAt(i);

        if (!isLetter(c))
            return false;
    }

    // All characters are letters.
    return true;
}


function isAlphanumeric (s)

{
    var i;

    if (isEmpty(s)) 
        if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
        else return (isAlphanumeric.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-alphanumeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number or letter.
        var c = s.charAt(i);

        if (! (isLetter(c) || isDigit(c) ) )
            return false;
    }

    // All characters are numbers or letters.
    return true;
}



function reformat (s)

{
    var arg;
    var sPos = 0;
    var resultString = "";

    for (var i = 1; i < reformat.arguments.length; i++) {
        arg = reformat.arguments[i];
        if (i % 2 == 1) resultString += arg;
        else {
            resultString += s.substring(sPos, sPos + arg);
            sPos += arg;
        }
    }
    return resultString;
}

function isIntegerInRange (s, a, b)
{
    if (isEmpty(s)) 
        if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
        else return (isIntegerInRange.arguments[1] == true);

    // Catch non-integer strings to avoid creating a NaN below,
    // which isn't available on JavaScript 1.0 for Windows.
    if (!isInteger(s, false)) return false;

    // Now, explicitly change the type to integer via parseInt
    // so that the comparison code below will work both on 
    // JavaScript 1.2 (which typechecks in equality comparisons)
    // and JavaScript 1.1 and before (which doesn't).
    var num = parseInt (s,10);
    return ((num >= a) && (num <= b));
}

function CompletaString(s,i)
{
    var t,u
    u = new String()
	
    u = s
	
    if (u.length > i)
    {
		
        t = u.substring(0,i)
    }
    else
    {
        t = u	
        for (j=u.length;j<i;j++)
        {
            t = t + " "	
        }
    }
    return t
}

function CompletaNumero2(s,i)
{
    var t,u
    u = new String(s)
	
	
    t = ""
	
	
    if (u.length > i)
    {
		
        t = u.substring(0,i)
    }
    else
    {
        t = u	
        for (j=u.length;j<i;j++)
        {
            t = "0" + t	
        }
    }
 	
    return t
}
	




function checkRadio(r)
{
    for (var i=0; i < r.length; i++)
    {
        if (r[i].checked)
        {
            return true
        }
    }
    return false
}

function RadioValue(r)
{
    for (var i=0; i < r.length; i++)
    {
        if (r[i].checked)
        {
            return r[i].value
        }
    }
    return ""
}

function checkInput(i)

{
	
    if (i.value == "" || isWhitespace(i.value))
    {
		
        return false
    }
    else
    {
        return true
    }
	
	
}

function Limpar()
{
    document.form1.periodo.value = ""
    document.form1.juros.value = ""
    document.form1.parcela.value = ""
    document.form1.montante.value = ""
}


function preCalc(){
    var tipo_cred = $('#juros').find('option').filter(':selected').text(); 
    //var valJuros = $('#juros').find('option').filter(':selected').val();
    //$('#juros').val(valJuros);
    //var sexo = ((var_sexo == "M") ? "Masculino" : "Feminino") ;

    var flag = false;
    var mes_carencia    = $('#mes_carencia').val(); 
    var mesesIn         = $('#meses').val();
    var valor           = $('#montante').val();
    var meses           = '36';
    var juros           = '2';
    var msg             = 'Quantidade de meses não adequada ao tipo de cérdito';
    
    
    //if(tipo_cred == 'Micro Crédito' || tipo_cred == 'Crédito para MEI'){
        if(tipo_cred == 'CredAuto'){
                meses = '60';
                juros = '2,5';      
        }
        /************************************/
        if(tipo_cred == 'Micro Crédito - PROMIC'){ 
            if(valor <= 10000){
                meses = '12';
                juros = '2,5'; 
            }
        } 
        /************************************/
        if(tipo_cred == 'CAC'){ 
			if(mesesIn <= 3){
				meses = '3';
                juros = '3'; 
			}
			if(mesesIn>3 && mesesIn<=24){
				meses = '24';
                juros = '3,7'; 
			}
			if(mesesIn>24){
				alert('Quantidade de meses excedida !');
				return false;
			}
        } 
        /************************************/
        if(tipo_cred == 'CredInfo'){ 
            //if(valor <= 10000){
                meses = '24';
                juros = '2,5'; 
            //}
        } 
        /************************************/
        if(tipo_cred == 'Titulos Descontados PF'){ 
			if(valor <= 250 && mesesIn<=4){
				meses = '4';
                juros = '3'; 
			}
			if(valor >= 251 && valor <= 500 && mesesIn <= 4){
				meses = '4';
                juros = '3,7'; 
			}
			if(valor >= 501 && valor <= 1500 && mesesIn <= 4){
				meses = '4';
				juros = '3,7';
				
			
			}
        }
		/************************************/
        if(tipo_cred == 'Titulos Descontados PJ'){ 
			if(valor <= 250 && mesesIn<=4){
				meses = '4';
                juros = '3'; 
			}
			if(valor >= 251 && valor <= 500 && mesesIn <= 4){
				meses = '4';
                juros = '3,7'; 
			}
			if(valor >= 501 && valor <= 2000 && mesesIn <= 4){
				meses = '4';
				juros = '3,7';
				
			
			}
        }
		/************************************/
        if(tipo_cred == 'Giro Rápido'){
                meses = '6';
                juros = '3';            
        }
        /************************************/
        if(tipo_cred == 'Cheque Especial'){
                meses = '6';
                juros = '7,9';    
          
        }
        /************************************/
        if(tipo_cred == 'Crédito para MEI'){
            if(valor <= 2000){
                meses = '12';
                juros = '2';    
            }
            if(valor > 2000 && valor <= 4000){
                meses = '15';
                juros = '2,5';         
            } 
           
        }
        /************************************/
        if(tipo_cred == 'CredBens'){
                meses = '24';
                juros = '2,5';            
        }
        /************************************/
        if(tipo_cred == 'CredPescado'){
                meses = '12';
                juros = '2,5';            
        }
        /************************************/
        if(tipo_cred == 'CredEmergencial'){
                meses = '8';
                juros = '2,58';            
        }
        /************************************/
        var mm = parseInt(mes_carencia);
        var cc = parseInt(mesesIn);

        //alert(mm+cc);
        if(parseInt(meses) < (mm+cc) ){
            msg = 'A quantidade de meses maxima permitida para '+tipo_cred+' e de '+meses+' meses';
            alert(msg);
            return false;
        }

        /************************************/
        $('#juros').find('option').filter(':selected').val(juros);
        return true;
    //}
}

function Calcular(opcao){
    var ret = preCalc();
    if(ret == false){
        return false;
    }
        
    var opcao;
    var s
    if (!checkInput(document.form1.periodo)) {
        if (!checkInput(document.form1.juros) ||
            !checkInput(document.form1.parcela) ||
            !checkInput(document.form1.montante)) {
            alert("Preencha 3 valores para calcular o 4º.");
            return false;
        }
        opcao = 1
    } else
    if (!checkInput(document.form1.juros)) {
        if (!checkInput(document.form1.periodo) ||
            !checkInput(document.form1.parcela) ||
            !checkInput(document.form1.montante)) {
            alert("Preencha 3 valores para calcular o 4º.");
            return false;
        }
        opcao = 2
    } else
    if (!checkInput(document.form1.parcela)) {
        if (!checkInput(document.form1.periodo) ||
            !checkInput(document.form1.juros) ||
            !checkInput(document.form1.montante)) {
            alert("Preencha 3 valores para calcular o 4º.");
            return false;
        }
        opcao = 3;
    } else
    if (!checkInput(document.form1.montante)) {
        if (!checkInput(document.form1.juros) ||
            !checkInput(document.form1.parcela) ||
            !checkInput(document.form1.periodo)) {
            alert("Preencha 3 valores para calcular o 4º.");
            return false;
        }
        opcao = 4;
    } else {
        alert("Preencha apenas 3 valores para calcular o 4º.");
        return false;
    }

    if (opcao != 1) {
        s = document.form1.periodo.value;
        s = s.replace(",", ".");
        if(!isFloat(s))
        {
            alert("Período deve ser um valor inteiro.");
            return false;
        }

        per_int = parseFloat(s)
    }

    if (opcao != 2) {

        s = document.form1.juros.value
        s = s.replace(",", ".")

        if(!isFloat(s))
        {
            alert("Taxa de Juros deve ser um valor númerico, tendo a vírgula(,) como delimitador da parte fracionária.");
            return false;
        }

        juros_float = parseFloat(s)/100;


    }

    if (opcao != 3) {
        s = document.form1.parcela.value;
        s = s.replace(",", ".");
        if(!isFloat(s))
        {
            alert("Valor da prestação deve ser um valor númerico, tendo a vírgula(,) como delimitador dos centavos.")
            return false;
        }

        parcela_float = parseFloat(s);


    }

    if (opcao != 4) {
        s = document.form1.montante.value;
        s = s.replace(",", ".");

        if(!isFloat(s))
        {
            alert("Valor do Financiamento deve ser um valor númerico maior que zero, tendo a vírgula(,) como delimitador dos centavos.")
            return false;
        }

        montante_float = parseFloat(s);


    }
    


    if (opcao==1) {

        per_int = Math.log(1-(montante_float*juros_float/parcela_float))/Math.log(1/(1+juros_float));
        per_int = Math.round(per_int*100)/100;

        var s = String(per_int);
        i = s.indexOf(".");
        if (i != -1)
        {
            s = s.substring(0,i) + "," + s.substring(i+1,s.length)

        }
        document.form1.periodo.value = s
    }

    if (opcao==2) {
        juros_inicial = parseFloat("-1")
        juros_final = parseFloat("99999")
        suposto_juros = parseFloat("0")
        suposto_parcela = parseFloat("0")
        var cont = 1
        var achou = false
        while (true) {
            suposto_juros = (juros_final + juros_inicial)/2
            suposto_parcela = (montante_float*suposto_juros)/(1-Math.pow(1/(1+suposto_juros),per_int))
            suposta_diferenca = Math.abs(parcela_float-suposto_parcela)
            if (suposta_diferenca > 0.000000001) {
                if (suposto_parcela > parcela_float) {
                    juros_final = suposto_juros
                }
                else {
                    juros_inicial = suposto_juros
                }
            }
            else {
                achou = true
                break
            }
            if (cont > 5000) {
                break
            }
            cont++;
        }
        if (achou==false) {
            document.form1.juros.value = "NaN";
        }
        else {
            if (suposto_juros!=-100) {
                suposto_juros = suposto_juros*100;
            }
            juros_float = Math.round(suposto_juros*100000)/100000;
            var s = String(juros_float)
            i = s.indexOf(".")
            if (i != -1) {
                s = s.substring(0,i) + "," + s.substring(i+1,s.length);
            }
            document.form1.juros.value = s;
            parcelas();
            return true;
        }
    }

    if (opcao==3) {
        parcela_float = (montante_float*juros_float)/(1 - Math.pow(1/(1+juros_float),per_int));
        parcela_float = Math.round(parcela_float*100)/100;
        var s = String(parcela_float);
        i = s.indexOf(".");
        if (i != -1) {
            s = s.substring(0,i) + "," + s.substring(i+1,s.length);
        }
        document.form1.parcela.value = s;
        parcelas();
        return true;
    }

    if (opcao==4) {
        montante_float = (parcela_float*(1 - Math.pow(1/(1+juros_float),per_int))/juros_float);
        montante_float = Math.round(montante_float*100)/100;
        var s = String(montante_float);
        i = s.indexOf(".");
        if (i != -1) {
            s = s.substring(0,i) + "," + s.substring(i+1,s.length);
        }
        document.form1.montante.value = s;
        parcelas();
        return true;
    }
    
    
}



function calc____(){
    $('#resultado-calculo').html(URL+'imgs/ajax-loader.gif">');
    $.post("ajax/calcular.php", { 
        tipo: $('#tipo').val(), 
        mes_carencia: $('#mes_carencia').val(), 
        meses : $('#meses').val(), 
        valor: $('#valor').val()
    }, function(data) {
        $('#resultado-calculo').html(data)
    });
}
/*****/
