var WMX = WMX || {};
WMX.Calc = WMX.Calc || {};

WMX.Calc.Payment = Class.create({
	initialize: function(id) {
		this.wrapper = $(id);
		if (!this.wrapper) return;
		this.wrapper.select('input.fc_calc')[0].observe('click', this._calculate.bind(this));
		this.wrapper.select('select').each(this._bindKeyUp.bind(this));
		this.wrapper.select('input, select').each(this._bindBlur.bind(this));
		this._calculate();
	},
	_bindKeyUp: function (e) {
		e.observe('keyup', this._calculate.bind(this));
	},
	_bindBlur: function (e) {
		e.observe('blur', this._calculate.bind(this));
	},
	_getInput: function(name) {
		return this.wrapper.select('.' + name)[0];
	},
	_getInt: function (name) {
		var input = this._getInput(name);
		var intVal = parseInt($F(input));
		return intVal;
	},
	_getFloat: function (name) {
		var input = this._getInput(name);
		var floatVal = parseFloat($F(input));
		if (isNaN(floatVal)) {
			return 0;
		}
		return floatVal.toFixed(2);
	},
	_getMoney: function(name) {
		var input = this._getInput(name);
		var inVal = $F(input);
		var val = '';
		for (var i = 0; i < inVal.length; i++) {
			var c = inVal.charAt(i);
			if (!isNaN(c) || (c === '.' ))
				val += c;
		}
		input.value = this._formatCurrency(val);
		if (isNaN(parseFloat(val))) {
			return 0;
		}
		return parseFloat(val);
	},
	_formatCurrency: function(val) {
		var i = parseFloat(val).toFixed(2);
		var s = new String(i);
		var delimiter = ','; // replace comma if desired
		var a = s.split('.',2)
		var d = a[1];
		var i = parseInt(a[0]);
		if (isNaN(i)) { return ''; }
		var minus = '';
		if (i < 0) { minus = '-'; }
		i = Math.abs(i);
		var n = new String(i);
		var a = [];
		while (n.length > 3) {
			var nn = n.substr(n.length-3);
			a.unshift(nn);
			n = n.substr(0,n.length-3);
		}
		if (n.length > 0) { a.unshift(n); }
		n = a.join(delimiter);
		if (d.length < 1) { s = n; }
		else { s = n + '.' + d; }
		return minus + s;
	},
	_calculate: function() {
		var down = this._getMoney('fc_down');
		var rate = this._getFloat('fc_rate');
		var term = this._getInt('fc_term');
		var price;
		if(this.wrapper.select('.fc_price') && this.wrapper.select('.fc_price') != '')
		{
			price = this._getMoney('fc_price');
		}
		else
		{
			price = 0;
		}
		var trade;
		if(this.wrapper.select('.fc_trade') && this.wrapper.select('.fc_trade') != '')
		{
			trade = this._getMoney('fc_trade');
		}
		else
		{
			trade = 0;
		}
		var payoff;
		if(this.wrapper.select('.fc_payoff') && this.wrapper.select('.fc_payoff') != '')
		{
			payoff = this._getMoney('fc_payoff');
		}
		else
		{
			payoff = 0;
		}
		var title;
		if(this.wrapper.select('.fc_title') && this.wrapper.select('.fc_title') != '')
		{
			alert(this.wrapper.select('.fc_title'));
			title = this._getMoney('fc_title');
		}
		else
		{
			title = 0;
		}
		var tax;
		if(this.wrapper.select('.fc_tax') && this.wrapper.select('.fc_tax') != '')
		{
			tax = this._getFloat('fc_tax');
		}
		else
		{
			tax = 0;
		}
		var monthly;
		if(this.wrapper.select('.fc_monthly') && this.wrapper.select('.fc_monthly') != '')
		{
			monthly = this._getMoney('fc_monthly');
		}
		var payment;
		var rateFactor
		/*alert(price);
		alert(down);
		alert(rate/100);
		alert(term);

		rateFactor = Math.pow(1+rate/100,term)
		alert(rateFactor)*/
		
		//payment = ((price - down) / term ) * ((rate/100)+1)
		
		//Add PST and GST
		price = price + (0.08*price) + (0.05*price)
		
		payment = (price-down) * (  (rate/12/100*Math.pow(1+rate/12/100,term)) / (Math.pow(1+rate/12/100,term)-1) )
		
		
		/*
		if(!monthly)
		{
			if(rate == 0)
				payment = (price * (1 + (tax / 100)) + title - down - trade + payoff) / term;
			else
				payment = (price * (1 + (tax / 100)) + title - down - trade + payoff) * ((rate / 100 / 12) / (1 - (1 / Math.pow((1 + rate / 100 / 12), term))));
		}
		else
		{
			if(rate == 0)
				payment = ((monthly * term) - title - trade + down + payoff) / (1 + tax /100);
			else
				payment = (monthly / ((rate / 1200) / (1 - (1 / Math.pow((1 + rate / 1200), term)))) - title + down - trade + payoff) * (1 / (1 + tax / 100));
		}*/
		this.wrapper.select('.fc_results')[0].update(this._formatCurrency(payment));
	}
});