function calcForm(f) {
		var b = f.borrow.value;
		var y = f.years.value;
		var r = f.rate.value;

		//making sure that an entry has been made in each field.
		if (b == "" || r == "") {
			//alert('not all fields filled in');
			return;
		}

		// making sure that entries are valid by using check number
		if (!checkNumber(f.borrow, 1, 99999999, "Amount") ||
			!checkNumber(f.rate, .001, 1000, "Interest Rate") ||
			!checkNumber(f.years, 5, 30, "Period")) 
		{
			f.Cm.value = "Invalid";
			return;
		}

		r = r / 100;
		var p = ((b*r)/12) * (1/(1-(Math.pow(1/(1+r),y))));
		f.Cm.value = poundsPence( p );
		p = ((b*rp)/12) * (1 / (1-(Math.pow((1/(rp + 1)),y))));
		f.CCm.value = poundsPence( p );
		p = (b*r)/12;
		f.CI.value = poundsPence( p );
		p = (b*rp)/12;
		f.CCI.value = poundsPence( p );
	}
	
	
function repaymentcalc() {

	var ma = parseInt( document.calc.mortgageamount.value);
	var y = parseInt( document.calc.years.value);
	var ir = parseInt( document.calc.interestrate.value);
	
	if ( !ma || ma < 1000 || ma > 9999999999)//check if number
		{
		alert('please enter the amount you wish to borrow');
		return false;
		}
	else if ( !ir || ir < 0.00001 || ir > 100 )//check if number/percentage
		{
		alert('please enter the interest rate');
		return false;
		}
	else if ( !y || y < 3 || y > 30 )//check if number in range
		{
		alert('please enter the period of the mortgage \(max 30 years\)');
		return false;
		}
	
	var irdec = (ir/100);//convert ir to decimal
	var mthirate = ir/12;
	var z = 1/(1+mthirate/100);
	var zcomp = 1/(1+(10/12)/100);//try 10% rate too
	var mths = y * 12;//number of mths of loan
	// capital repayment mortgage
	var mthrepayment = ((ma*irdec)/12) * (1/(1-(Math.pow(1/(1+irdec),y))));//doesnt quite work so..
	mthrepayment = ma * (z - 1)/(Math.pow(z,mths+1)-z);
	document.calc.capitalrepaymentmthly.value = makeprice( mthrepayment );
	document.calc.capitalinterestpaid.value = makeprice( (mthrepayment*mths) - ma);
	// comparison to 10% rate
	var comparerate = 0.10;
	//mthrepayment =  ((ma*comparerate)/12) * (1/(1-(Math.pow(1/(1+comparerate),y))));
	mthrepayment = ma * (zcomp - 1)/(Math.pow(zcomp,mths+1)-zcomp);
	document.calc.capitalrepaymentmthlycompare.value = makeprice( mthrepayment );
	document.calc.capitalinterestpaidcompare.value = makeprice( (mthrepayment*mths)-ma );
	// interest only repayment
	mthrepayment = (ma*irdec)/12;
	document.calc.interestonlymthly.value = makeprice( mthrepayment );
	document.calc.interestonlyinterestpaid.value = makeprice( (ma*irdec) * y );
	// comparison to 10% rate
	mthrepayment = (ma*comparerate)/12;
	document.calc.interestonlymthlycompare.value = makeprice( mthrepayment );
	document.calc.interestonlyinterestpaidcompare.value = makeprice( (ma*comparerate) * y );
	return true;
}
function makeprice(x) {

	var suffix = "";
	
	if ( x == 0 ) { return "0";}//.00"; }
	x = Math.round(x *100) / 100;
	
	if ( x * 10 == Math.round(x * 10) ) { suffix = "0"; }
	if ( x == Math.round(x) ) { suffix = "";}//".00"; }
	return x.toString() + suffix;


}

function borrowingscalc() {

	//if 1 income, the avg loan amount would be income x 3.25 and the highest would be income * 4
	// if joint income, avg is 2.5 x jointincome, highest 3 x joint income
	// if borrowing under rent-a-room type scheme add 4250 to income
	
	var income1 = parseInt( document.calc.income1.value);
	var income2 = parseInt( document.calc.income2.value);

	if ( !income1 || income1 <1 || income1>999999999)//check is number
		{
		alert('please enter your income');
		document.calc.income1.focus();
		return false;
		}
	
	if ( document.calc.lodger.checked)
		{
		income1 += 4250;
		}
	var avgloan = 0;
	var highestloan = 0;
	
	if ( !income2 || income2 <1 || income2>999999999)//check is number
		{
		// no second income
		avgloan = income1*3.25;
		highestloan = income1*4;
		}
	else
		{
		// joint income
		var jointinc = income1+income2;
		avgloan = jointinc*2.5;
		highestloan = jointinc*3;
		}
	document.calc.avgborrow.value = makeprice( avgloan );
	document.calc.highborrow.value = makeprice( highestloan );
	return true;
}


var averagehouseprice = 184924;

function calculatesaving() {
	if ( document.calc.propertyprice.value == 0 || document.calc.propertyprice.value=='' )
		{
		document.calc.propertyprice.value=averagehouseprice;
		}
	var exvatagentcost=(document.calc.propertyprice.value*document.calc.estateagentcommission.options[document.calc.estateagentcommission.selectedIndex].value)/100;
	var incvatagentcost = ( exvatagentcost * 117.5 )/100;
	document.calc.estateagentcharges.value = makeprice(incvatagentcost);
	var fsbomincost = 0;
	var fsbomaxcost = 200;
	
	
	//document.calc.nomoreagentsservice.options[document.calc.nomoreagentsservice.selectedIndex].value;

	document.calc.yourminsaving.value=makeprice(incvatagentcost-fsbomaxcost);
	document.calc.yourmaxsaving.value=makeprice(incvatagentcost-fsbomincost);
}


