/* - - - - - - - - - - - - - - - - - - - - - - -
 JavaScript
 vendredi 24 juillet 2009 11:51:13
 HAPedit 3.1.11.111
 - - - - - - - - - - - - - - - - - - - - - - - */

/* class javascript creditSim lancé par sim_rac.js // utile */

function creditSim(f_,t_,e_,a_,d_,b_,tp_)
{
    this.amounts = a_;             /* valeur crédit en cours   */
    this.durations = d_;           /* tableau des nb mois       */
    this.expenses = e_;            /* taux fixe chez médiatis      */
    this.tegs = t_;                /* multi tableau en taux (0.00) mois -> mensualité       */
    this.monthlyFees = f_;         /* valeur inconnu                                    */
    this.brackets = b_;            /* (parenthèses) marge pour changé taux => mois     */
    this.type = tp_!=null?tp_:"";

    this.amountIndex = 0;
    this.durationIndex = 0;
    this.feeIndex = 0;

    this.isInitState = true;

    this.getMonthlyFeeCount = function()
    {
        return this.monthlyFees.length;
    }

    this.getAmountCount = function()  /* utile */
    {
        return this.amounts.length;
    }

    this.getAmountMin = function()
    {
        return this.amounts[0];
    }

    this.getAmountMax = function()
    {
        return this.amounts[this.amounts.length-1];
    }

    this.getMonthlyFeeIndexFromAmountIndexAndDurationIndex = function(a_i, d_i)  /* utile */
    {
        return a_i + d_i * this.getAmountCount();    /* function ligne 30 */
    }

    this.getDurationCount = function()  /* utile */
    {
        return this.durations.length;
    }

    this.getDurationMin = function()
    {
        return this.durations[0];
    }

    this.getDurationMax = function()
    {
        return this.durations[this.durations.length-1];
    }

    this.getFeeCount = function()
    {
        return this.monthlyFees.length;
    }

    this.getIndexFromAmount = function(a)
    {
        return this.indexOfContainingInterval(this.amounts,a); /* function ligne 94 */
    }

    this.getIndexFromDuration = function(d)  /* utile */
    {
        var i = this.indexOfContainingInterval(this.durations,d); /* function ligne 94  */
        if(d<this.durations[this.durations.length-1] && i>0 && d-this.durations[i-1]<this.durations[i]-d) {
            return i-1;
        }
        return i;
    }

    this.getAmountFromIndex = function(i)   /* utile */
    {
        return this.amounts[i];
    }

    this.getDurationFromIndex = function(i)
    {
        return this.durations[i];
    }

    this.indexOfContainingInterval=function(intervals,value)  /* utile */
    {
      var i=utils_indexOfContainingInterval(intervals, value); /* function ligne 16 fichier utils.js */

      if(i==_INDEXOFCONTAININGINTERVAL_UNDERLOWERBOUND) /* valeur ligne 12 fichier utils.js */
          i=0;
      else if(i==_INDEXOFCONTAININGINTERVAL_OVERUPPERBOUND) /* valeur ligne 13 fichier utils.js */
          i=intervals.length-1;
      return i;
    }

    this.getAmountIndexFromMonthlyFeeIndexAndDurationIndex = function(f_i, d_i)
    {
      var i = -1;
      if (f_i >= 0 && d_i >= 0)
         i = f_i - d_i * this.getAmountCount();  /* function ligne 30 */
      return i;
    }
    this.getMonthlyFees=function()
    {
        var start = this.durationIndex * this.getAmountCount(); /* function ligne 30 */
        return new Array(0.0).concat(this.monthlyFees.slice(start,start+this.getAmountCount()));  /* function ligne 30 */
    }

    this.getMonthlyFeeMin=function()
    {
        var start = this.durationIndex * this.getAmountCount();  /* function ligne 30 */
        return this.monthlyFees[start];
    }

    this.getMonthlyFeeMax=function()
    {
        var end = (this.durationIndex+1) * this.getAmountCount();  /* function ligne 30 */
        return this.monthlyFees[end-1];
    }

    this.getAmount = function()  /* utile */
    {
        return this.getAmountFromIndex(this.amountIndex);    /* function ligne 84 */
    }

    this.getTotalCost = function()  /* utile */
    {                          /* function ligne 151 & 140 & 162 & 304 */
        return (this.getDuration() * sim.getMonthlyFee()) + this.getExpense() + this.getMandat() ;
    }

    this.getMonthlyFee = function()  /* utile */
    {                                /* function ligne 187 */
        var teg =  Math.pow((1 + sim.getTeg()),(1/12)) - 1;
                           /* function ligne 120 & 151 */
        var cal1 = (sim.getAmount() * (Math.pow((1 + teg),0)));
        var cal2 = (teg / (1 - Math.pow((1 + teg),-(sim.getDuration() - 0))));
        var cal3 = (cal1 * cal2);
        
        return cal3;
    }

    this.getDuration = function()  /* utile */
    {                              /* function ligne 120 & 151 */
        return this.getDurationFromIndex(this.durationIndex);
    }

    this.getBracketIndex = function()   /* utile */
    {                          /* function ligne 77 fichier utils.js */
        var i = utils_getIndexTEGFromInterval(this.brackets,this.getAmount());
        return i;
    }

    this.getExpense = function()  /* utile */
    {
       if(this.type==""){
         if(this.durationIndex==0)
             return 0;
         else{
             var i = (this.getAmount()*1.5)/100;     /* function ligne 120 */
             if (i >= 1000) {
                return 1000;
             }
             else {
                return i;
             }
         }
       }
       else{
         if(this.durationIndex==0)
             return 0;
         else{
             return 76.0;
         }
       }

    }

    this.getTeg = function()     /* utile */
    {                                                        /* function ligne 50 */
        if(this.durationIndex >= 0 && this.durationIndex < this.getDurationCount() )
        return this.tegs[this.durationIndex][this.getBracketIndex()];
    }                               /* function ligne 156 */

    this.setDurationIndexFromValue = function(d) /* utile */
    {
        this.setDurationIndex(this.getIndexFromDuration(d));    /* function ligne 198 & function ligne 75 */
    }

    this.setDurationIndex = function(i)    /* utile */
    {
       this.durationIndex = i;                  /* function ligne 45 */
        this.feeIndex = this.getMonthlyFeeIndexFromAmountIndexAndDurationIndex(this.amountIndex, this.durationIndex);
    }

    this.setAmountIndexFromValue = function(a)  /* utile */
    {
        this.setAmountIndex(this.getIndexFromAmount(a));
    }

    this.setAmountIndex = function(i)
    {
        this.amountIndex = i;
        this.feeIndex = this.getMonthlyFeeIndexFromAmountIndexAndDurationIndex(this.amountIndex, this.durationIndex);
    }

    this.setMonthlyFeeIndexFromValue = function(f)
    {
        var i = utils_indexOfContainingInterval(this.getMonthlyFees(), f);
        while (i == _INDEXOFCONTAININGINTERVAL_OVERUPPERBOUND && this.durationIndex>0)
        {
            this.durationIndex--;
            i = utils_indexOfContainingInterval(this.getMonthlyFees(), f);
        }
        if(i == _INDEXOFCONTAININGINTERVAL_OVERUPPERBOUND)
             i = this.getMonthlyFeeIndexEnd()-1;
        else if(i == _INDEXOFCONTAININGINTERVAL_UNDERLOWERBOUND || i==0)
             i = this.getMonthlyFeeIndexStart();
        else
        {
            i += this.getMonthlyFeeIndexStart();
            if (f - this.monthlyFees[i-1] < this.monthlyFees[i] - f)
            i--;
        }
        this.setMonthlyFeeIndex(i);
    }

    this.setMonthlyFeeIndex=function(index)
    {
        this.feeIndex = index;
        this.amountIndex = this.getAmountIndexFromMonthlyFeeIndexAndDurationIndex(this.feeIndex, this.durationIndex);
    }

    this.getMonthlyFeeIndexStart=function()
    {
        return this.durationIndex * this.getAmountCount();
    }

    this.getMonthlyFeeIndexEnd=function()
    {
        return (this.durationIndex+1) * this.getAmountCount();
    }

    this.getType=function()
    {
        return this.type;
    }

    this.moreAmount=function()
    {
        if(this.amountIndex<this.getAmountCount()-1)
            this.setAmountIndex(this.amountIndex+1);
        return this.amountIndex;
    }

    this.lessAmount=function()
    {
       if(this.amountIndex>0)
           this.setAmountIndex(this.amountIndex-1);
       return this.amountIndex;
    }

    this.moreDuration=function()
    {
       if(this.durationIndex<this.getDurationCount()-1)
           this.setDurationIndex(this.durationIndex+1);
       return this.durationIndex;
    }

    this.lessDuration=function()
    {
       if(this.durationIndex>0)
           this.setDurationIndex(this.durationIndex-1);
       return this.durationIndex;
    }

    this.moreFee=function()
    {
       if(this.feeIndex<this.getMonthlyFeeIndexEnd()-1)
           this.setMonthlyFeeIndex(this.feeIndex+1);
       else if(this.durationIndex>0)
       {
           this.durationIndex--;
           this.setMonthlyFeeIndexFromValue(this.monthlyFees[this.feeIndex]);
       }
       return this.feeIndex;
    }

    this.lessFee=function()
    {
       if(this.feeIndex> this.getMonthlyFeeIndexStart())
           this.setMonthlyFeeIndex(this.feeIndex-1);
       return this.feeIndex;
    }

    this.getMandat = function()       /* utile */
    {
        var i = (this.getAmount()*5)/100;    /* function ligne 130 */
           return i;
    }

    return this;
} 