/* - - - - - - - - - - - - - - - - - - - - - - -
 JavaScript
 vendredi 24 juillet 2009 11:51:13
 HAPedit 3.1.11.111
 - - - - - - - - - - - - - - - - - - - - - - - */

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()
    {
        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)
    {
        //alert(a_i +' : '+ d_i +' : '+ this.getAmountCount()+ ' =  ' +(a_i + d_i * this.getAmountCount()));
        return a_i + d_i * this.getAmountCount();
    }

 //this.feeIndex = this.getMonthlyFeeIndexFromAmountIndexAndDurationIndex(this.amountIndex,this.durationIndex);

    this.getDurationCount = function()
    {
        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)
    {
        /*
        if(a<=this.amounts[0])
        return 0;
        else if(a>=this.amounts[this.amounts.length-1])
        return this.amounts.length-1;
        else
        {
        var step = (this.amounts[this.amounts.length-1]-this.amounts[0])/(this.amounts.length-1);
        return Math.round(a/step);
        }
        */
        return this.indexOfContainingInterval(this.amounts,a);
    }

    this.getIndexFromDuration = function(d)
    {
        /*
        if(d<=12)
        return 0;
        if(d>=this.durations[this.durations.length-1])
        return this.durations[this.durations.length-1];
        return Math.round(d/12);
        */
        var i = this.indexOfContainingInterval(this.durations,d);
        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)
    {
        return this.amounts[i];
    }

    this.getDurationFromIndex = function(i)
    {
        return this.durations[i];
    }

    this.indexOfContainingInterval=function(intervals,value)
    {
      var i=utils_indexOfContainingInterval(intervals, value);

      if(i==_INDEXOFCONTAININGINTERVAL_UNDERLOWERBOUND)
          i=0;
      else if(i==_INDEXOFCONTAININGINTERVAL_OVERUPPERBOUND)
          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();
      return i;
    }
    this.getMonthlyFees=function()
    {
        var start = this.durationIndex * this.getAmountCount();
        return new Array(0.0).concat(this.monthlyFees.slice(start,start+this.getAmountCount()));
    }

    this.getMonthlyFeeMin=function()
    {
        var start = this.durationIndex * this.getAmountCount();
        return this.monthlyFees[start];
    }

    this.getMonthlyFeeMax=function()
    {
        var end = (this.durationIndex+1) * this.getAmountCount();
        return this.monthlyFees[end-1];
    }

    this.getAmount = function()
    {
        return this.getAmountFromIndex(this.amountIndex);
    }

    this.getTotalCost = function()
    {
	
		var teg = (1 + sim.getTeg());
		teg = Math.pow(teg, (1/12));
		teg = (teg - 1);
		
		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 (this.getDuration() * cal3) + this.getExpense() + this.getMandat() ;
        /*return this.getDuration() * this.getMonthlyFee() + this.getExpense() + this.getMandat() ;*/
    }

    this.getMonthlyFee = function()
    {
		var teg = (1 + sim.getTeg());
		teg = Math.pow(teg, (1/12));
		teg = (teg - 1);
		
		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;
		//return this.monthlyFees[this.feeIndex];
    }

    this.getDuration = function()
    {
        return this.getDurationFromIndex(this.durationIndex);
    }

    this.getBracketIndex = function()
    {
        //var brackets = new Array(10000.0, 46000.0, 80000.0);
        /*var i = utils_indexOfContainingInterval(this.brackets, this.getAmount());
        if(i==_INDEXOFCONTAININGINTERVAL_UNDERLOWERBOUND)
        i=0;
        else if(i==_INDEXOFCONTAININGINTERVAL_OVERUPPERBOUND)
        i=this.expenses.length-1;
        else if(i==_INDEXOFCONTAININGINTERVAL_EMPTYINTERVAL)
        i=0;
        return i;*/
        //alert(this.brackets+' '+this.getAmount());
        var i = utils_getIndexTEGFromInterval(this.brackets,this.getAmount());
        return i;
    }

    this.getExpense = function()
    {
       /* if(this.durationIndex >= 0 && this.durationIndex < this.getDurationCount())
        return this.durationIndex != 0? this.expenses[this.getBracketIndex()] : 0.0; */
       if(this.type==""){
       //alert('RAC');
         if(this.durationIndex==0)
             return 0;
         else{
             /*if(this.amountIndex >= 0 && this.amountIndex <= 7)
             return this.expenses[0];
             if(this.amountIndex > 7 && this.amountIndex <= 18)
             return this.expenses[1];
             if(this.amountIndex > 18 && this.amountIndex <= 43)
             return this.expenses[2];
             if(this.amountIndex > 43)
             return this.expenses[3];
             var i = this.getBracketIndex();
             if (this.expenses[i] >= 1000) {
                return 1000;
             }
             else {
                return this.expenses[i];
             }*/
			 var i = (this.getAmount()*1.5)/100;
             if (i >= 1000) {
                return 1000;
             }
             else {
                return i;
             }
         }
       }
       else{
         if(this.durationIndex==0)
             return 0;
         else{
             return 76.0;
         }
       }

    }

    this.getTeg = function()
    {
        if(this.durationIndex >= 0 && this.durationIndex < this.getDurationCount() )
        return this.tegs[this.durationIndex][this.getBracketIndex()];
    }

    this.setDurationIndexFromValue = function(d)
    {
        this.setDurationIndex(this.getIndexFromDuration(d));
    }

    this.setDurationIndex = function(i)
    {
       //alert('1 : '+ i +' : '+this.amountIndex+' : '+this.feeIndex);
       this.durationIndex = i;
        this.feeIndex = this.getMonthlyFeeIndexFromAmountIndexAndDurationIndex(this.amountIndex, this.durationIndex);
    }

    this.setAmountIndexFromValue = function(a)
    {
        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);
        //var i = utils_indexOfContainingInterval(this.monthlyFees, f,this.durationIndex * this.getAmountCount(),this.getAmountCount());
        while (i == _INDEXOFCONTAININGINTERVAL_OVERUPPERBOUND && this.durationIndex>0)
        {
            this.durationIndex--;
            i = utils_indexOfContainingInterval(this.getMonthlyFees(), f);
            //i = utils_indexOfContainingInterval(this.monthlyFees, f,this.durationIndex * this.getAmountCount(),this.getAmountCount());
        }
        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()//1 over max fee_index
    {
        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>0)
       if(this.feeIndex> this.getMonthlyFeeIndexStart())
           this.setMonthlyFeeIndex(this.feeIndex-1);
       return this.feeIndex;
    }

    this.getMandat = function()
    {
        var i = (this.getAmount()*5)/100;
           return i;
    }

    return this;
} 