// Calculators

// Mortgage Calculator

function mortgagecalculate() {
	if (document.getElementById('mr').value == "") {
		alert("Please enter a number into the mortgage required field");
		document.getElementById('mr').focus();
	return false;
	} else {
	var present_value = parseFloat(document.getElementById('mr').value);
	}
	if (document.getElementById('rp').value == "") {
		alert("Please enter a number into the repayment period field");
		document.getElementById('rp').focus();
	return false;
	} else {
	var loan_term = parseFloat(document.getElementById('rp').value);
	}
	if (document.getElementById('ir').value == "") {
		alert("Please enter a number into the interest rate field");
		document.getElementById('ir').focus();
	return false;
	} else {
	var interest_rate = parseFloat(document.getElementById('ir').value);
	var interest_rate = interest_rate / 100;
	}
	
	

function calculate_payment(PV, IR, NP) {
  var PMT = (PV * IR) / (1 - Math.pow(1 + IR, -NP));
  return round_decimals(PMT, 2);
}

function round_decimals(original_number, decimals) {
  var result1 = original_number * Math.pow(10, decimals);
  var result2 = Math.round(result1);
  var result3 = result2 / Math.pow(10, decimals);
  return (result3);
}

var monthly_payment = calculate_payment(present_value,interest_rate / 12, loan_term * 12);

	var totalpercent = present_value * interest_rate;
	
	var monthlyinterest = totalpercent / 12;
	
	monthlyinterest = Math.round(monthlyinterest*100)/100;
	
	
	document.getElementById('result1').innerHTML = "<p>Your monthly repayments would be <strong>£"+monthly_payment+"</strong> of which <strong>£"+monthlyinterest+"</strong> is interest</p>";

	return false;
}

// Investments Calculator One

function savingscalculate1() {
	if (document.getElementById('input1').value == "") {
		alert("Please enter a number into the \"How much you will be saving each month?\" field.");
		document.getElementById('input1').focus();
	return false;
	} else {
	var input1 = parseFloat(document.getElementById('input1').value);
	}
	if (document.getElementById('input2').value != "") {
	var input2 = parseFloat(document.getElementById('input2').value);
	}
	if (document.getElementById('input3').value == "") {
		alert("Please enter a number into the \"How long would you like to save for?\" field.");
		document.getElementById('input3').focus();
	return false;
	} else {
	var input3 = parseFloat(document.getElementById('input3').value);
	}
	if (document.getElementById('input4').value == "") {
		alert("Please enter a number into the \"What is your predicted / target annual growth rate?\" field.");
		document.getElementById('input4').focus();
	return false;
	} else {
	var input4 = parseFloat(document.getElementById('input4').value);
	}
	
	total = 0;
	if (input2) {
	total = total + input2;
	}
	for (i = 0; i < input3; i++) {
		
		var yearssavings = input1*12;
		
		total = total + yearssavings;
		
		interest = (total / 100)*input4;
		
		total = total + interest;
	}
	
	total = Math.round(total*100)/100;
	
	document.getElementById('result').innerHTML = "<p>After <strong>"+input3+"</strong> years your savings could be worth <strong>£"+total+"</strong></p>";
	
	return false;
}

// Investments Calculator Two

function savingscalculate2() {
	if (document.getElementById('input5').value == "") {
		alert("Please enter a number into the \"How much are you aiming to save in total?\" field.");
		document.getElementById('input5').focus();
	return false;
	} else {
	var input5 = parseFloat(document.getElementById('input5').value);
	}
	if (document.getElementById('input6').value != "") {
	var input6 = parseFloat(document.getElementById('input6').value);
	}
	if (document.getElementById('input7').value == "") {
		alert("Please enter a number into the \"How long would you like to save for?\" field.");
		document.getElementById('input7').focus();
	return false;
	} else {
	var input7 = parseFloat(document.getElementById('input7').value);
	}
	if (document.getElementById('input8').value == "") {
		alert("Please enter a number into the \"What is your predicted / target annual growth rate?\" field.");
		document.getElementById('input8').focus();
	return false;
	} else {
	var input8 = parseFloat(document.getElementById('input8').value);
	}
	
	var total = input5;
	
	if (input6) {
	total = total - input6;
	}
	
	for (i = 0; i < input7; i++) {
		
		var percentage = 100+input8;
		
		var onepercent = total / percentage;
		
		oncepercent = onepercent * input8;
		
		total = total - oncepercent;
	}
	
	var totaltime = input7 * 12;
	
	var result = total / totaltime;
	
	result = Math.round(result*100)/100;
	
	var text = "<p>To save a total of <strong>£"+input5+"</strong> in <strong>"+input7+"</strong> years";
	if (input6) {
	text = text+" with an initial saving of <strong>"+input6;
	}
	text = text+"</strong>, you will need to save <strong>£"+result+"</strong> per month</p>";
	
	document.getElementById('result2').innerHTML = text;
	
	return false;
}

// Investments Calculator Three

function savingscalculate3() {
	if (document.getElementById('input9').value == "") {
		alert("Please enter a number into the \"How much will you be saving each month?\" field.");
		document.getElementById('input9').focus();
	return false;
	} else {
	var input9 = parseFloat(document.getElementById('input9').value);
	}
	if (document.getElementById('input10').value != "") {
	var input10 = parseFloat(document.getElementById('input10').value);
	}
	if (document.getElementById('input11').value == "") {
		alert("Please enter a number into the \"How much are you aiming to save in total?\" field.");
		document.getElementById('input11').focus();
	return false;
	} else {
	var input11 = parseFloat(document.getElementById('input11').value);
	}
	if (document.getElementById('input12').value == "") {
		alert("Please enter a number into the \"What is your predicted / target annual growth rate?\" field.");
		document.getElementById('input12').focus();
	return false;
	} else {
	var input12 = parseFloat(document.getElementById('input12').value);
	}
	
	var total = 0;
	
	var limit = input11;
	
	var cleantotal = input11;
	
	total = total + input10;
	
	var increment = input9;
	
	var interest = input12;
	
	var smallinterest = interest/12;
	
	var con = 1;
	
	
	for (i = 0; con == 1; i++) {
		
		total = total + increment;
		
		var minterest = (total / 100)*smallinterest;
		
		total = total + minterest;
		
		if (total > limit) {
			con = 0;
			var totalmonths = i + 1;
		}
			
	}
	
	var years = totalmonths / 12;
	
	years = Math.floor(years);
	
	b = 12 * years;
	
	var months = totalmonths - b;
	
	document.getElementById('result3').innerHTML = "<p>To get to your total of <strong>£"+cleantotal+"</strong> you would need to save for <strong>"+years+"</strong> years and <strong>"+months+"</strong> months</p>";
	
	
	return false;
}


