/**
 * Composant Bank, permet de créer des objets Ext liés aux banques
 * 
 */

/**
 * Constructeur
 * 
 * @param String pType    Type de calcul, prêt ou mensualité
 * @param Object pValues  Valeurs pour le calcul
 */
var Calculator = function(pType, pValues) {
	/**
	 * @var String type de calcul
	 */
	this._calcType = '';
	
	/**
	 * @var Integer montant du prêt
	 */
	this._amount = 0;
	
	/**
	 * @var Integer montant du prêt
	 */
	this._term = 0;
	
	/**
	 * @var Float taux d'intérêt
	 */
	this._interestRates = 0;
	
	/**
	 * @var Float taux d'assurance
	 */
	this._insuranceRates = 0;

};

/**
 * Initializer
 * 
 * @param String pType    Type de calcul, prêt ou mensualité
 * @param Object pValues  Valeurs pour le calcul
 */
Calculator.prototype.initialize = function(pType, pValues) {		
	this.setCalcType(pType);
	
	if (typeof(pValues.amount) != 'undefined') {
		this.setAmount(pValues.amount);
	}
	
	if (typeof(pValues.monthly) != 'undefined') {
		this.setMonthly(pValues.monthly);
	}
	
	if (typeof(pValues.term) != 'undefined') {
		this.setTerm(pValues.term);
	}
	
	if (typeof(pValues.interestRates) != 'undefined') {
		this.setInterestRates(pValues.interestRates);
	}
	
	if (typeof(pValues.insuranceRates) != 'undefined') {
		this.setInsuranceRates(pValues.insuranceRates);
	}
};



/**
 * Set the type of calculation (monthly or amount)
 * 
 * @param String pValue
 */
Calculator.prototype.setCalcType = function(pValue) {		
	
	this._calcType = pValue.toString().toLowerCase();
};


/**
 * Set the desired Amount, based on the calculating Type
 * 
 * @param Integer pValue
 */
Calculator.prototype.setAmount = function(pValue) {		
	
	var reg_exp = new RegExp('/[^0-9]/');
	
	var amount = parseInt(pValue.toString().replace(reg_exp, ''), 10);
	this._amount = (amount == '') ? 0 : amount;	
};

/**
 * Set the loan term
 * 
 * @param Integer pValue
 */
Calculator.prototype.setTerm = function(pValue) {		
	
	var reg_exp = new RegExp('/[^0-9]/');
	
	var term = parseInt(pValue.toString().replace(reg_exp, ''), 10);
	this._term = (term == '') ? 0 : term;
};

/**
 * Set the interest rates
 * 
 * @param Float pValue
 */
Calculator.prototype.setInterestRates = function(pValue) {		
	
	var reg_exp = new RegExp('/[^0-9.]/');
	
	var rates = parseFloat(pValue.toString().replace(reg_exp, ''));
	this._interestRates = (rates == '') ? 0 : rates;
};

/**
 * Set the insurance rates
 * 
 * @param Float pValue
 */
Calculator.prototype.setInsuranceRates = function(pValue) {		
	
	var reg_exp = new RegExp('/[^0-9.]/');
	
	var rates = parseFloat(pValue.toString().replace(reg_exp, ''));
	this._insuranceRates = (rates == '') ? 0 : rates;
};

/**
 * Calculating result
 * 
 * @param void
 * @return Obj result
 */
Calculator.prototype.calculate = function() {			
	var result;
				
	var rates = (this._interestRates) / 1200;
	var div = parseFloat( (rates) / (1-(1/Math.pow(1+rates, this._term * 12))));
	var calc = 0;
	var totalCost = 0;
	if (this._calcType == 'amount') {
		calc = parseFloat(this._amount / (div + this._insuranceRates / 1200));
		totalCost = parseFloat(this._amount * this._term * 12 - calc);
	}
	else if (this._calcType == 'monthly') {
		calc = parseFloat(this._amount * (div + this._insuranceRates / 1200));
		totalCost = parseFloat(calc * this._term * 12 - this._amount);		
	}	
	calc = (isNaN(calc)) ? parseFloat(0.00) : calc;
	totalCost = (isNaN(totalCost)) ? parseFloat(0.00) : totalCost;

	result = {
	   amount: calc.toFixed(2),
	   totalCost: totalCost.toFixed(2)
	};
	
	return result;
};



