// VAT calculator JavaScript code

function calculate() {
	var vat, total, vatTotal, finalTotal;
	
	var frm = document.calculator;	

	total = evalnum( frm.total.value );
	if (total == "NaN") total=0;
	if (total == 0) {
		alert("You must enter a figure.");
		frm.total.focus()
		return
	}
	else

		var vatRate = ( frm.vatrate[0].checked )? 0.175 : 0.075;
		var vatFraction = ( frm.vatrate[0].checked )? (1-(1/1.175)) : (1-(1/1.075));

		frm.total.value = evalpound( total );
		if (frm.chkvat.checked)	
		{
			vatTotal = total * vatFraction;
			finalTotal = total - vatTotal;
			frm.calcvat.value = evalpound( vatTotal );
			frm.calctotal.value = evalpound( finalTotal );
			document.getElementById("netgross").firstChild.nodeValue = 'Net amount (excluding VAT)';
		}
		else	
		{
			vatTotal = total * vatRate;
			vatTotal = Math.round(vatTotal * 100) / 100;
			frm.calcvat.value = evalpound( vatTotal );
			frm.calctotal.value = evalpound( total + vatTotal );
			document.getElementById("netgross").firstChild.nodeValue = 'Gross amount (including VAT)';
		}
	}

	function autocalc () {

		if ( document.calculator.total.value != '' ) {
			calculate();
		}

	}

