/* 
@author Joaquin Marti Garcia 
@link http://www.joaquinmarti.com
@email joaquinmarti@gmail.com
*/

var CALC = {
	
	total : 0,
	interest : 0,
	amortization : 0,
	quota : 0,
	
	calcTotal : function() {
		CALC.total = CALC.quota / ( CALC.interest * Math.pow((CALC.interest + 1.0), CALC.amortization) / ( Math.pow((CALC.interest + 1.0), CALC.amortization) - 1.0 ) );
		CALC.total = Math.round(CALC.total * 100.00) / 100.00;
		
		$('#calc_total').val(CALC.total);
	},
	
	calcInterest : function() {
		CALC.interest = 20.0 / 1200.0;
		inc_monthly_type = 10.0 / 1200.0;

		y = 1.0 + CALC.interest;
		actual_quota = Math.round(CALC.total * CALC.interest * Math.pow(y, CALC.amortization) / ( Math.pow(y, CALC.amortization) - 1.0 ));
		
		while (actual_quota != CALC.quota) {			
			y = 1.0 + CALC.interest;
			actual_quota = Math.round(CALC.total * CALC.interest * Math.pow(y, CALC.amortization) / ( Math.pow(y, CALC.amortization) - 1.0 ));
		
			if (actual_quota > CALC.quota) {
				CALC.interest = CALC.interest - inc_monthly_type;
			}
			else {
				CALC.interest = CALC.interest + inc_monthly_type;
			}
			
			if ((CALC.interest * 1200.0) > 20.0)
				break;
			
			inc_monthly_type = inc_monthly_type / 2.0;
		}
		
		CALC.interest = CALC.interest * 1200.0;
		CALC.interest  = Math.round(CALC.interest  * 100.00) / 100.00;
		
		$('#calc_interest').val(CALC.interest);
	},
	
	calcAmortization : function() {
		CALC.amortization = 1;
	
		y = 1.0 + CALC.interest;
		actual_quota = Math.round(CALC.total * CALC.interest * Math.pow(y, CALC.amortization) / ( Math.pow(y, CALC.amortization) - 1.0 ));
		
		while (actual_quota > CALC.quota) {
			last_quota = actual_quota;

			y = 1.0 + CALC.interest;
			actual_quota = Math.round(CALC.total * CALC.interest * Math.pow(y, CALC.amortization) / ( Math.pow(y, CALC.amortization) - 1.0 ));

			CALC.amortization++;
		}
		
		$('#calc_amortization').val(CALC.amortization);
	}, 
	
	calcQuota : function() {
		CALC.quota = CALC.total * CALC.interest * Math.pow((CALC.interest + 1.0), CALC.amortization) / ( Math.pow((CALC.interest + 1.0), CALC.amortization) - 1.0 );	
		CALC.quota = Math.round(CALC.quota * 100.00) / 100.00;
		
		$('#calc_quota').val(CALC.quota);
	},
	
	initCalc : function() {
		if (!$('div#calc_inner').length) return;
		
		$('div#calc_inner form').submit(function() {
			
			CALC.total = parseInt($('#calc_total').val());
			CALC.interest = parseFloat($('#calc_interest').val()) / 1200.0;
			CALC.amortization = parseInt($('#calc_amortization').val()) * 12;
			CALC.quota = parseInt($('#calc_quota').val());

			if (isNaN(CALC.total) && !isNaN(CALC.interest) && !isNaN(CALC.amortization) && !isNaN(CALC.quota)) {
				CALC.calcTotal();
			}
			else if (!isNaN(CALC.total) && isNaN(CALC.interest) && !isNaN(CALC.amortization) && !isNaN(CALC.quota)) {
				CALC.calcInterest();
			}
			else if (!isNaN(CALC.total) && !isNaN(CALC.interest) && isNaN(CALC.amortization) && !isNaN(CALC.quota)) {
				CALC.calcAmortization();
			}
			else if (!isNaN(CALC.total) && !isNaN(CALC.interest) && !isNaN(CALC.amortization) && isNaN(CALC.quota)) {
				CALC.calcQuota();
			}
			
			return false;		
		});
		
		$('div#calc_inner form fieldset input.delete').click(function() {
			$('#calc_total').val('');
			$('#calc_interest').val('');
			$('#calc_amortization').val('');
			$('#calc_quota').val('');
		});
	
	}
	
}