var co = new Object;

function recalc_onclick(ctl) {

  if (document.formc.automatic_recalc.checked || ctl=='') {
    co.pA1B = eeparseFloatTh(document.formc.pA1B.value);
    co.pA2B = eeparseFloatTh(document.formc.pA2B.value);
    calc(co);
    document.formc.pA2E.value = eedisplayFloatNDTh(co.pA2E,2);
    document.formc.pA3B.value=eeisnumber(co.pA3B)?eedisplayPercentND(co.pA3B,4):
    co.pA3B;
    document.formc.pA3E.value = eedisplayFloatNDTh(co.pA3E,2);
    document.formc.pA4B.value = eedisplayPercentND(co.pA4B,2);
    document.formc.pA4E.value = eedisplayFloatNDTh(co.pA4E,2);
    document.formc.pA5B.value = eedisplayFloatNDTh(co.pA5B,2);
    document.formc.pA5E.value = eedisplayFloatNDTh(co.pA5E,2);
    document.formc.pA6B.value = eedisplayFloatNDTh(co.pA6B,2);
    document.formc.pA6E.value = eedisplayFloatNDTh(co.pA6E,2);
    document.formc.pA7B.value = eedisplayFloatNDTh(co.pA7B,2);
    document.formc.pA7E.value = eedisplayFloatNDTh(co.pA7E,2);
    document.formc.pA8E.value = eedisplayFloatNDTh(co.pA8E,2);
  };
};

var eeisus=0;
var eetrue = "VERDADEIRO";
var eefalse="FALSO";
var eedec=",";
var eeth=".";
var eedecreg = new RegExp(",","g");
var eethreg = new RegExp("[.]","g");


function calc(data){
  var cA1B = data.pA1B;
  var cA2B=data.pA2B;
  var cA2E=(((((((fv((0.0100),(1),(0),(((cA1B)*(-1))),(0)))-(cA1B)))*(0.775)))+(cA1B)));
  var cA3B=(((((cA2B)<(30)))?("Prazo mínimo: 30 dias"):(((((cA2B)<(60)))?(0.008323):(((((cA2B)<(91)))?(0.008323):(((((cA2B)<(120)))?(0.008414):(((((cA2B)<(150)))?(0.008414):(((((cA2B)<(181)))?(0.008414):(((((cA2B)<(211)))?(0.008414):(((((cA2B)<(241)))?(0.008414):(0.008414)))))))))))))))));
  var cA3E=(((((((fv((0.0100),(2),(0),(((cA1B)*(-1))),(0)))-(cA1B)))*(0.775)))+(cA1B)));
  var cA4B=(((((cA2B)<(181)))?(0.225):(((((cA2B)<=(360)))?(0.2):(((((cA2B)<=(720)))?(0.175):(0.15)))))));
  var cA4E=(((((((fv((0.0102),(3),(0),(((cA1B)*(-1))),(0)))-(cA1B)))*(0.775)))+(cA1B)));
  var cA5B=(((fv((v2n(cA3B)),(((cA2B)/(30))),(0),(((cA1B)*(-1))),(0)))-(cA1B)));
  var cA5E=(((((((fv((0.0095),(4),(0),(((cA1B)*(-1))),(0)))-(cA1B)))*(0.775)))+(cA1B)));
  var cA6E=(((((((fv((0.0098),(5),(0),(((cA1B)*(-1))),(0)))-(cA1B)))*(0.775)))+(cA1B)));
  var cA7E=(((((((fv((0.0098),(6),(0),(((cA1B)*(-1))),(0)))-(cA1B)))*(0.775)))+(cA1B)));
  var cA8E=(((((((fv((0.0105),(7),(0),(((cA1B)*(-1))),(0)))-(cA1B)))*(0.775)))+(cA1B)));
  var cA6B=(((cA5B)-(((cA5B)*(cA4B)))));
  var cA7B=(((cA1B)+(cA6B)));
  data.pA2E = cA2E;
  data.pA3B = cA3B;
  data.pA3E = cA3E;
  data.pA4B = cA4B;
  data.pA4E = cA4E;
  data.pA5B = cA5B;
  data.pA5E = cA5E;
  data.pA6B = cA6B;
  data.pA6E = cA6E;
  data.pA7B = cA7B;
  data.pA7E = cA7E;
  data.pA8E = cA8E;
};


function myIsNaN(x){
  return(isNaN(x)||(typeof x=='number'&&!isFinite(x)));
};

function round(n,nd){
  if(isFinite(n)&&isFinite(nd)){
    var sign_n=(n<0)?-1:1;
    var abs_n=Math.abs(n);
    var factor=Math.pow(10,nd);
    return sign_n*Math.round(abs_n*factor)/factor;
  }else{
    return NaN;
  }
};

function s2n(str){
  str=String(str).replace(eedecreg,".");
  return parseFloat(str);
}

function v2n(v){
  switch(typeof v){
    case "number":return v;
    case "string":return s2n(v);
    case "boolean":return v?1:0;
    case "object":

      if(v.constructor==Number){
        return v;
      };
      if(v.constructor==String){
        return s2n(v);
      };
      if(v.constructor==Boolean){
        return v?1:0;
      };
      return Number.NaN;
      default:return Number.NaN;
  }
};

function eedisplayFloatND(x,nd){
  if(myIsNaN(x)){
    return Number.NaN;
  }else{
    var res=round(x,nd);
    if(nd>0){
      var str=String(res);
      if(str.indexOf('e')!=-1)
        return str;
      if(str.indexOf('E')!=-1)
        return str;var parts=str.split('.');
      if(parts.length<2){
        var decimals=('00000000000000').substring(0,nd);
        return(parts[0]).toString()+eedec+decimals;
      }else{
        var decimals=((parts[1]).toString()+'00000000000000').substring(0,nd);
        return(parts[0]).toString()+eedec+decimals;
      }
    }else{
      return res;
    }
  }
};

function eedisplayPercentND(x,nd){
  if(myIsNaN(x)){
    return Number.NaN;
  }else{
    return eedisplayFloatND(x*100,nd)+'%';
  }
}

function eeparseFloatTh(str){
  str = String(str).replace(eethreg,"");
  str = String(str).replace(eedecreg,".");
  var res = parseFloat(str);

  if(isNaN(res)){
    return 0;
  }else{
    return res;
  }
};

function eedisplayFloatNDTh(x,nd){
  if(myIsNaN(x)){
    return Number.NaN;
  }else{
    var res = round(x,nd);
    if(nd>0){
      var str = String(res);
      if(str.indexOf('e')!=-1)
        return str;
      if(str.indexOf('E')!=-1)
        return str;
      var parts = str.split('.');
      var res2 = eeinsertThousand(parts[0].toString());
      if(parts.length<2){
        var decimals=('00000000000000').substring(0,nd);
        return(res2+eedec+decimals);
      }else{
        var decimals=((parts[1]).toString()+'00000000000000').substring(0,nd);
        return(res2+eedec+decimals);
      }
    }else{
      return(eeinsertThousand(res.toString()));
    }
  }
};

function eeinsertThousand(whole){
  if(whole==""||whole.indexOf("e")>=0){
    return whole;
  }else{
    var minus_sign="";
    if(whole.charAt(0)=="-"){
      minus_sign = "-";
      whole = whole.substring(1);
    };
    var res = "";
    var str_length = whole.length-1;
    for(var ii=0;ii<=str_length;ii++){
      if(ii>0&&ii%3==0){
        res = eeth+res;
      };
      res = whole.charAt(str_length-ii)+res;
    };
    return minus_sign+res;
  }
};

function eeisnumber(v){
  if(isNaN(v)||v==Number.NEGATIVE_INFINITY||v==Number.POSITIVE_INFINITY){
    return false;
  }else{
    switch(typeof v){
      case "number":return true;
      case "object":return v.constructor==Number;
      default:return false;
    }
  }
};

function fv(rate,nper,pmt,pv,type){
  if(!isFinite(type)||!isFinite(pv)||!isFinite(pmt)||!isFinite(nper)||!isFinite(rate))
    return Number.NaN;
  if(rate==0)
    return-(pv+nper*pmt);
  var type1=(type!=0)?1:0;
  var pvif=Math.pow(1+rate,nper);
  var fvifa=(Math.pow(1+rate,nper)-1)/rate;
  return(-((pv*pvif)+pmt*(1+rate*type1)*fvifa));
};

