﻿	// Validate the input
	function checkValid(input, minVal, maxVal, msg)
	{
		msg = "The " + msg + " field is invalid. ";

		// Is this actually a number?
		var str = input.value;
		for (var i = 0; i < str.length; i++) 
		{
			var ch = str.substring( i, i + 1)
			if ((ch < "0" || "9" < ch) && ch != '.') 
			{
				alert(msg);
				return false;
			}
		}

		// Check for min and max levels
		var num = 0 + str
		if (num < minVal || maxVal < num) 
		{
			alert(msg + "The value must be between " + minVal + " and " + maxVal + ".");
			return false;
		}
		input.value = str;
		return true;
	}

	// Calculate the changes
	function calculateField(input)
	{
		if (input.value != null && input.value.length != 0)
		{
			input.value = "" + eval(input.value);
		}
		calculateForm(input.form);

		return false;
	}

	// Calculate all of the values on the form
	function calculateForm(form)
	{
		var txtRequiredMortgage = form.txtRequiredMortgage.value; // Principal amount
		var txtRepaymentPeriod = form.txtRepaymentPeriod.value; // Term - number of years
		var txtInterestRate = form.txtInterestRate.value; // Rate of interest

		// Check all entries
		if ((txtRequiredMortgage == null || txtRequiredMortgage.length == 0) ||
			(txtRepaymentPeriod == null || txtRepaymentPeriod.length == 0) || 
			(txtInterestRate == null || txtInterestRate.length == 0)) 
		{
			//alert('You have not filled in all of the required fields');
			return;
		}

		// Validate entries
		if (!checkValid(form.txtRequiredMortgage, 1, 99999999, "'Mortgage required'") ||
			!checkValid(form.txtInterestRate, .001, 1000, "Interest rate") ||
			!checkValid(form.txtRepaymentPeriod, 5, 40, "Repayment period")) 
		{
			form.outMonthlyRepayment.value = "Invalid";
			return;
		}

		// Do the maths...
		
		// Calculate the rates
		txtInterestRate = txtInterestRate / 100;
		var monthlyRepayment = ((txtRequiredMortgage * txtInterestRate)/12) * (1/(1-(Math.pow(1/(1+txtInterestRate), txtRepaymentPeriod))));
		form.outMonthlyRepayment.value = convertToPoundsPence(monthlyRepayment);
		
		var monthlyInterest = (txtRequiredMortgage * txtInterestRate)/12;
		form.outMonthlyInterest.value = convertToPoundsPence(monthlyInterest);
		
		// Plus 1%
		var plusInterestRate = txtInterestRate + 0.01;
		var plusMonthlyRepayment = ((txtRequiredMortgage * plusInterestRate)/12) * (1/(1-(Math.pow(1/(1+plusInterestRate), txtRepaymentPeriod))));
		form.outPlusMonthlyRepayment.value = convertToPoundsPence(plusMonthlyRepayment);

		var plusMonthlyInterest = (txtRequiredMortgage * plusInterestRate)/12;
		form.outPlusMonthlyInterest.value = convertToPoundsPence(plusMonthlyInterest);

		// Minus 1%
		var minusInterestRate = txtInterestRate - 0.01;
		var minusMonthlyRepayment = ((txtRequiredMortgage * minusInterestRate)/12) * (1/(1-(Math.pow(1/(1+minusInterestRate), txtRepaymentPeriod))));
		form.outMinusMonthlyRepayment.value = convertToPoundsPence(minusMonthlyRepayment);

		var minusMonthlyInterest = (txtRequiredMortgage * minusInterestRate)/12;
		form.outMinusMonthlyInterest.value = convertToPoundsPence(minusMonthlyInterest);
		
		return false;
	}

	// Add zeros to make it look like a proper value
	function convertToPoundsPence(value)
	{
		penceValue = new String(value);
		var i = penceValue.indexOf('.');
		if (i != -1) 
		{
			// Looks like there is a point
			penceValue = penceValue.substr(0, i + 3);
			if (penceValue.length-i < 3)
			{
				// Not enough zeros at the end so add one
				penceValue = penceValue + '0';
			}
		}
		else
		{
			// No zeros at the end
			penceValue = penceValue + '.00';
		}
		return penceValue;
	}

	// Clear the form
	function clearForm(form)
	{
		form.txtRequiredMortgage.value = "";
		form.txtRepaymentPeriod.value = "";
		form.txtInterestRate.value = "";
		
		form.outMonthlyRepayment.value = "";
		form.outMonthlyInterest.value = "";
		form.outPlusMonthlyRepayment.value = "";
		form.outPlusMonthlyInterest.value = "";
		form.outMinusMonthlyRepayment.value = "";
		form.outMinusMonthlyInterest.value = "";
		return false;
	}