function calcTotalInvest(loanAmount, loanInterest, years) {
	var loanInterestParse = loanInterest / 100;
	var totalInvestment = (loanAmount * loanInterestParse) * years;
	return totalInvestment.toFixed(2)
}

function calcTaxRefund(totalInvestment, marginTaxRate) {
	var marginTaxRateParse = marginTaxRate / 100;
	var taxRefund = totalInvestment * marginTaxRateParse;
	return taxRefund.toFixed(2);
}

function calcNewBalance(loanAmount, investInterest, years) {
	var investInterestParse = investInterest / 100;
	var newBal = 0;
	var la = loanAmount;
	for(var i = 0; i < years; i++) {
		newBal = la + (la * investInterestParse);
		la = newBal;
	}
	
	return newBal.toFixed(2);
}

function calcInvestReturn(curBalance, loanAmount, totalInvested) {
	var investProfit = curBalance - loanAmount;
	var profitPerc = (investProfit / totalInvested) * 100;
	return profitPerc.toFixed(2);
}

function calcAll(loanAmount, loanInterest, investInterest, years, marginTaxRate) {

	var totalInvested = calcTotalInvest(loanAmount, loanInterest, years);
	var taxRefund = calcTaxRefund(totalInvested, marginTaxRate);
	var accountBalance = calcNewBalance(loanAmount, investInterest, years);
	var returnAmount = calcInvestReturn(accountBalance, loanAmount, totalInvested);
	var all = new Array(totalInvested, taxRefund, accountBalance, returnAmount);
	
	return all;
}

function formCalc() {
	var calc = document.getElementById('calc');
	
	var f1 = parseFloat(calc.loanAmount.value);
	var f2 = parseFloat(calc.loanInterest.value);
	var f3 = parseFloat(calc.investInterest.value);
	var f4 = parseFloat(calc.investYears.value);
	var f5 = parseFloat(calc.marginalTaxRate.value);
	
	var out = calcAll(f1,f2,f3,f4,f5);
	
	calc.totalInvest.value = out[0];
	calc.taxRefund.value = out[1];
	calc.accBalance.value = out[2]
	calc.totalReturn.value = out[3]
	
	
}
