var errtxt = "";
var errnum = 0;

// paint coverage figures
// Updated by M Glancy 30-05-02
var coverages = new Array(
	0,	// Select paint type option - none selected
	0,	// Blank optiuon - none selected
	16,	// liquid gloss
	13,	// nondrip gloss
	12,	// eggshell
	15,	// matt emulsion
	14,	// silk emulsion
	12,	// non-drip emulsion
	11,	// primer/undercoat
	15	// varnish
);


// the scale conversion factor in metres/unit
var scale = 1;

function addErr(txt) {
	errtxt += txt + '. ';
	errnum++;
}

function clearErr() {
	errtxt = "";
	errnum = 0;
}

function float2currency(rawcost) {
	var pesos = Math.floor( rawcost );
	var pence = Math.round( (rawcost - pesos) * 100 );
	if ( pence < 10 ) {
		pence = "0" + pence;
	}
	var rv = pesos + "." + pence;
	return rv;
}

function cannotContinue() {
	if ( errnum ) {
		if (errnum == 1) {
			alert( "Sorry, there was a problem: " + errtxt );
		} else {
			alert( "Sorry, there were " + errnum + " problems: " + errtxt );
		}
		return 1;
	} else {
		return 0;
	}
}

function doConv() {
	document.calc1.OUTPUT.value = Math.round(((document.calc1.INPUTft.value * 0.3048) + (document.calc1.INPUTin.value * 0.0254))*100)/100;
}

// all figures are in metric
// Modified 30-05-02 by M Glancy - added extra windows and doors
function doCalc() {
	clearErr();
	var roomheight = document.calc.roomheight.value || addErr("Please enter the height of the room");
	var roomwalllength = document.calc.roomwalllength.value || addErr("Please enter the total length of all the walls");
	var numcoats = document.calc.numcoats.value || 1;

	var maxwallarea = roomwalllength * roomheight;
	var windowarea = (document.calc.window1height.value * document.calc.window1width.value) + (document.calc.window2height.value * document.calc.window2width.value);
	var doorarea = (document.calc.door1width.value * document.calc.door1height.value) + (document.calc.door2width.value * document.calc.door2height.value) + (document.calc.door3width.value * document.calc.door3height.value);
	var painttype = document.calc.paint.selectedIndex || addErr("Please choose a paint type from the drop-down list");

	var netarea = maxwallarea - windowarea - doorarea;

	if ( netarea < 0 ) {
		addErr("There's not enough wall and too much window/door area! Check that your measurements are correct");
	}
	if ( document.calc.UNITCOST.value < 0 ) {
		addErr("The price should be a positive number, in pounds sterling");
	}

	if ( cannotContinue() ) {
		document.calc.QTYNEEDED.value = "";
		document.calc.TOTALCOST.value = "";
		return;
	}

	document.calc.QTYNEEDED.value = Math.round( 100 * numcoats * netarea / coverages[ painttype ] ) / 100;

	if ( document.calc.UNITCOST.value ) {
		document.calc.TOTALCOST.value = float2currency( document.calc.UNITCOST.value * document.calc.QTYNEEDED.value );
	} else {
		document.calc.TOTALCOST.value = "";
	}
}


function Morgcal()
{
	form = document.myform
	LoanAmount= form.LoanAmount.value
		
	DownPayment= "0"
	AnnualInterestRate = form.InterestRate.value/100
	Years= form.NumberOfYears.value
		MonthRate=AnnualInterestRate/12
	NumPayments=Years*12
	Prin=LoanAmount-DownPayment
	
	MonthPayment=Math.floor((Prin*MonthRate)/(1-Math.pow((1+MonthRate),(-1*NumPayments)))*100)/100
	
	form.NumberOfPayments.value=NumPayments
	form.MonthlyPayment.value=MonthPayment
}