
function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

function CommaFormatted(amount)
{
	var delimiter = ","; // replace comma if desired
	var a = amount.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) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
}

function pensionCalc() {
	var uf_investment, annual_investment, years, interest_rate, future_value, lump_sum, total_value, start_year, end_year;
	var calc1, calc2, calc3, calc4, calc5, calc6, calc7, full_amount;
	
	start_year = document.getElementById('start_date').value;
	end_year = document.getElementById('end_date').value;
	
	if ((start_year.length == 4 && end_year.length != 4) || (start_year.length != 4 && end_year.length == 4)) {
		//Entries do not match
		alert('Please make sure the dates/ages you enter for your start and end values are in the same format');
		return 0;
	} else {
		years = end_year - start_year;
	}
	
	uf_investment = document.getElementById('monthy_savings').value;
	uf_investment = (uf_investment.replace(/,/, ""));

	uf_investment = uf_investment * 12;

	if(uf_investment > 255000) {
		alert('The pension limit is currently £255,000 annually');
		return 0;
	}
	
	annual_investment = uf_investment;
	form_interest_rate = 6;
	
	lump_sum = document.getElementById('lump_sum').value
	lump_sum = (lump_sum.replace(/,/, ""));
	
	full_amount = (parseInt(uf_investment) * years) + parseInt(lump_sum);

 
	interest_rate = (1+ form_interest_rate/100);  //== 1.05 

	function futureValue(years) {
		//Future value of savings
		calc1 = uf_investment * Math.pow(interest_rate,years);  //== 6171.43 
		calc2 = 1 - Math.pow(interest_rate,years); //== -9.2857 
		calc3 = (calc2 / -(form_interest_rate/100) - 1);
		calc4 = calc3 * annual_investment; 
		calc5 = calc4 + calc1;
		return calc5;
	}
	
	//Future value
	calc6 = Math.pow(interest_rate,years) * lump_sum;
	
	//Total Value
	total_value = futureValue(years) + calc6;
	feedback_text = "";
	if (total_value > 1800000 || full_amount > 1800000) {
		feedback_text ="<span style='color:red'>Be careful - Currently there is a lifetime limit of £1.8 million which will increase annually in line with the RPI. Any amounts above the lifetime limit could be taxed at 55%.</span><br /><br />";
	}
	
	feedback_text += '<p>Annual Investment: &pound;' + annual_investment + '<br />Number of years to retirement: ' + years + '<br /><strong>Future value of savings: &pound;' + CommaFormatted(CurrencyFormatted(futureValue(years))) + '</strong><br /><strong>Lump Sum Future value: &pound;' + CommaFormatted(CurrencyFormatted(calc6)) + '</strong><br /><strong>Total: &pound;' + CommaFormatted(CurrencyFormatted(total_value)) + '</strong></p>';
	
	document.getElementById('feedback').innerHTML = feedback_text;
	
	document.getElementById('5y_value').innerHTML = '&pound;' + CommaFormatted(CurrencyFormatted((futureValue(years - 5))));
	document.getElementById('10y_value').innerHTML = '&pound;' + CommaFormatted(CurrencyFormatted((futureValue(years - 10))));
	document.getElementById('15y_value').innerHTML = '&pound;' + CommaFormatted(CurrencyFormatted((futureValue(years - 15))));
	
	document.getElementById('5y_cost').innerHTML = '&pound;' + CommaFormatted(CurrencyFormatted((futureValue(years - 5) - futureValue(years)) * -1));
	document.getElementById('10y_cost').innerHTML = '&pound;' + CommaFormatted(CurrencyFormatted((futureValue(years - 10) - futureValue(years)) * -1));
	document.getElementById('15y_cost').innerHTML = '&pound;' + CommaFormatted(CurrencyFormatted((futureValue(years - 15) - futureValue(years)) * -1));
	
	document.getElementById('costs_table').style.display = 'block';
	
	
	/*alert('future value = ' + futureValue(years));
	var future_val_cost = futureValue(years - 5) - futureValue(years);
	alert('future value -5 = ' + futureValue(years - 5) + ' cost: ' + future_val_cost);*/
}
