// Check for blank fields
function isFilled(input) {
	if (input.value=="" || input.value==null) {
		return false;
	} else {
		return true;
	}
}
//checks that data are valid 
function checkValues(input,min,max,msg) {
	msg=msg + " field has invalid data\n" + input.value;
	// Check that the values entered are numeric
	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 + " is not a numeric value");
			return false;
		}
	}
	// Check that values lie between the min and max values allowed
	var num=0 + str
	if (num < min || max < num) {
		alert(msg + " is outside permitted range [" + min + ".." + max + "]");
		return false;
	}
	input.value=str;
	return true;
}
 
// Calculate monthly repayment figures
function calculateForm(form) {
	var amount=form.amount.value;
	var term=form.term.value;
	var interestRate=form.interestRate.value;
	// Check that an entry has been made in each field.
	if (isFilled(form.amount)==false) {
		alert("Mortgage required field cannot be blank");
		form.amount.focus();
		return false;
	}
	if (isFilled(form.term)==false) {
		alert("Repayment term field cannot be blank");
		form.term.focus();
		return false;
	}
	if (isFilled(form.interestRate)==false) {
		alert("Interest rate field cannot be blank");
		form.interestRate.focus();
		return false;
	}
	// Check that entries are valid
	if (!checkValues(form.amount, 1000, 10000000, "Mortgage required") || !checkValues(form.term, 5, 25, "Repayment term") || !checkValues(form.interestRate, 1, 15, "Interest rate")) {
		form.capitalAndInterestMonthlyFigure.value="Invalid";
		form.interestOnlyMonthlyFigure.value="Invalid";
		form.higherCapitalAndInterestMonthlyFigure.value="Invalid";
		form.higherInterestOnlyMonthlyFigure.value="Invalid";
		return;
	}
	// Calculate capital and interest monthly repayment figure
	interestRate=interestRate/100;
	var capitalAndInterest=((amount*interestRate)/12) * (1/(1-(Math.pow(1/(1+interestRate),term))));
	form.capitalAndInterestMonthlyFigure.value=poundsPence(capitalAndInterest);
	// Calculate interest only monthly repayment figure
	var interestOnly=(amount*interestRate)/12;
	form.interestOnlyMonthlyFigure.value=poundsPence(interestOnly);
	// Calculate capital and interest monthly repayment for 1% higher interest rate
	var higherInterestRate=interestRate+0.01;
	var higherCapitalAndInterest=((amount*higherInterestRate)/12) * (1/(1-(Math.pow(1/(1+higherInterestRate),term))));
	form.higherCapitalAndInterestMonthlyFigure.value=poundsPence(higherCapitalAndInterest);
	// Calculate interest only monthly repayment for 1% higher interest rate
	higherInterestOnly=(amount*higherInterestRate)/12;
	form.higherInterestOnlyMonthlyFigure.value=poundsPence(higherInterestOnly);
}
// convert values to pounds and pence
function poundsPence( N ) {
	// Doesn't work with Microsoft IE3 browser
	if ((navigator.appName.indexOf('Microsoft')>-1)	&& (navigator.appVersion.indexOf('3.0')>-1)) {
		return N;
	}
	S=new String( N );
	var i=S.indexOf('.');
	if (i != -1) {
		S=S.substr( 0, i+3 );
		if (S.length-i < 3) {
			S=S + '0';
		}
	}
	return S;
}
// Clear form
function resetForm(form) {
	form.amount.value="";
	form.term.value="";
	form.interestRate.value="";
	form.capitalAndInterestMonthlyFigure.value="";
	form.interestOnlyMonthlyFigure.value="";
	form.higherCapitalAndInterestMonthlyFigure.value="";
	form.higherInterestOnlyMonthlyFigure.value="";
}

