// JavaScript Document

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num);
} 
function fullCostHelp(){
	var helpText = "<p><strong>Help on full cost</strong></p>";
	helpText = helpText + "<p align='left'>Traditionally, people paid full cost for funds, receiving no rebates for any of the fees. Commonly this would mean a 5% initial charge, and then an annual charge of around 1.5%. If you don't know the cost of the specific fund you're after, try inputting these values to get a picture of what it would cost.</p>";
	//document.getElementById("fundsRresults").innerHTML  = helpText;
	document.getElementById("fundsResultBox").style.display= "block";
	document.getElementById("fundsFullCostHelp").style.display= "block";	
	document.getElementById("fundsDiscountHelp").style.display= "none";	
}
function discountHelp(){
	var helpText = "<p><strong>Help on discount brokers</strong></p>";
	var helpText = helpText + "<p align='left'>Discount Brokers slash the cost of buying funds, by rebating the entire fee, or at least a proportion. Buying through one, the typical costs are a 0% initial charge, and an annual fee of 1.25%. If you don't know the cost of the specific fund you're after, try inputting these values to see how much cheaper this way would be.</p>";
	//document.getElementById("fundsRresults").innerHTML  = helpText;
	document.getElementById("fundsResultBox").style.display= "block";
	document.getElementById("fundsDiscountHelp").style.display= "block";
	document.getElementById("fundsFullCostHelp").style.display= "none";
}
function yearString(value){
	if(value > 1){
		return "years";
	} else {
		return "year";
	} 
}

function calculateCost(){
	document.getElementById("fundsFullCostHelp").style.display= "none";	
	document.getElementById("fundsDiscountHelp").style.display= "none";	
	
	var validated = true;
	var	Amount			=	document.getElementById("fundAmount").value;
	Amount				=	parseFloat(Amount.replace(/[^0-9.]/g, ''));

	if(isNaN(Amount)){
		document.getElementById("fundAmount").style.border = "#ff0000 solid 1px";	
		validated = false;
	} else {
		document.getElementById("fundAmount").style.border = "";		
	}

	
	var	Growth			=	document.getElementById("growth").value;
	
	var	Years			=	document.getElementById("years").value;
	Years				=	parseFloat(Years.replace(/[^0-9.]/g, ''));
	
	
	var	annualCharge1	=	document.getElementById("annualCharge1").value;
	annualCharge1		=	parseFloat(annualCharge1.replace(/[^0-9.]/g, ''));

	if(isNaN(annualCharge1)){
		document.getElementById("annualCharge1").style.border = "#ff0000 solid 1px";	
		validated = false;
	} else {
		document.getElementById("annualCharge1").style.border = "";			
	}

	var	initialCharge1	=	document.getElementById("initialCharge1").value;
	initialCharge1		=	parseFloat(initialCharge1.replace(/[^0-9.]/g, ''));
	
	if(isNaN(initialCharge1)){
		document.getElementById("initialCharge1").style.border = "#ff0000 solid 1px";	
		validated = false;
	} else {
		document.getElementById("initialCharge1").style.border = "";			
	}
	
	var	annualCharge2	=	document.getElementById("annualCharge2").value;
	annualCharge2		=	parseFloat(annualCharge2.replace(/[^0-9.]/g, ''));
	
	if(isNaN(annualCharge2)){
		document.getElementById("annualCharge2").style.border = "#ff0000 solid 1px";	
		validated = false;
	} else {			
		document.getElementById("annualCharge2").style.border = "";				
	}

	var	initialCharge2	=	document.getElementById("initialCharge2").value;
	initialCharge2		=	parseFloat(initialCharge2.replace(/[^0-9.]/g, ''));			

	if(isNaN(initialCharge2)){
		document.getElementById("initialCharge2").style.border = "#ff0000 solid 1px";	
		validated = false;
	} else {
		document.getElementById("initialCharge2").style.border = "";			
	}

	if(validated){
				
		var totalWithCharges1 = Math.round((Amount * (1-(initialCharge1/100))) * Math.pow(( 1 + (Growth/100))*(1-(annualCharge1/100)),Years));
		
		var totalWithCharges2 = Math.round((Amount * (1-(initialCharge2/100))) * Math.pow(( 1 + (Growth/100))*(1-(annualCharge2/100)),Years));
	
		var totalWithoutCharges = (Amount * (Math.pow(1 + (Growth/100),Years)));
	
		
		var resultText = "<p><strong>After <span style='color:red; font-weight:bold;'>"+ Years + "</span> " + yearString(Years) + "</strong></p>";
		resultText = resultText + "<p><strong>Buy at full cost:</strong> You'll have <span style='color:#3716FD; font-weight:bold;'>£"+formatCurrency(totalWithCharges1)+ "</span><br />";
		resultText = resultText + "<strong>Buy via Discount Broker:</strong> You'll have <span style='color:#3716FD; font-weight:bold;'>£"+formatCurrency(totalWithCharges2)+"</span></p>";
		resultText = resultText + "<p><strong>Use a Discount Broker and you'll have <span style='color:#3716FD; font-weight:bold;'>£"+formatCurrency(totalWithCharges2 - totalWithCharges1)+"</span> more.</strong></p>";
		resultText = resultText + "<p style='font-size:8pt; font-style:italic;'>REMINDER: This is a simple calculator focused on growth and charges.  It excludes and other costs and dividends, and should only be used as an indication.</p>";
	} else {
		var resultText = "<p>Please insert values in every box.</p>";
	}
	document.getElementById("fundsRresults").innerHTML  = resultText;
	document.getElementById("fundsResultBox").style.display= "block";
}