function calc(cost) {
	if (cost.name.indexOf("cost_ex_vat")==0) {
		cost.value = fmtNumber(cost);
		if (isNaN(parseInt(cost.value))) 
			cost.value = "";
		if (cost.value=="" && document.calculator["cost_inc_vat_"+cost.name.substring("cost_ex_vat".length+1)].value!="") {
			cost.value = 0;
			document.calculator["cost_inc_vat_"+cost.name.substring("cost_ex_vat".length+1)].value = "";
		}
		if (cost.value!="") {		
			document.calculator["cost_inc_vat_"+cost.name.substring("cost_ex_vat".length+1)].value = fmtMoney(cost.value*1.175, 2, '.', ',' );			
			fmtCurrency(document.calculator["cost_inc_vat_"+cost.name.substring("cost_ex_vat".length+1)]);
			document.calculator[cost.name].value = fmtMoney(document.calculator[cost.name].value, 2, '.', ',' );
			fmtCurrency(document.calculator[cost.name]);	

			i=1;
			total_cost_ex_vat = 0;
			total_cost_inc_vat = 0;
			while (true) {
				if (document.calculator["cost_ex_vat_"+i]) {	
					if (document.calculator["cost_ex_vat_"+i].value!="")
						total_cost_ex_vat += fmtNumber(document.calculator["cost_ex_vat_"+i]);
					if (document.calculator["cost_inc_vat_"+i].value!="")
						total_cost_inc_vat += fmtNumber(document.calculator["cost_inc_vat_"+i]);
					++i;

				} else
					break;
			}

			monthly_2 = total_cost_ex_vat*0.04714;
			total_payments_2 = monthly_2*24;
			less_estimated_tax_releif_2 = total_payments_2*25/100;
			net_cost_2 = total_payments_2-less_estimated_tax_releif_2;

			monthly_3 = total_cost_ex_vat*0.03383;
			total_payments_3 = monthly_3*36;
			less_estimated_tax_releif_3 = total_payments_3*25/100;;
			net_cost_3 = total_payments_3-less_estimated_tax_releif_3;

			monthly_4 = total_cost_ex_vat*0.02727;
			total_payments_4 = monthly_4*48;
			less_estimated_tax_releif_4 = total_payments_4*25/100;;
			net_cost_4 = total_payments_4-less_estimated_tax_releif_4;

			monthly_5 = total_cost_ex_vat*0.02339;
			total_payments_5 = monthly_5*60;
			less_estimated_tax_releif_5 = total_payments_5*25/100;;
			net_cost_5 = total_payments_5-less_estimated_tax_releif_5;

			document.calculator["total_cost_ex_vat"].value = fmtMoney(total_cost_ex_vat, 2, '.', ',' );
			fmtCurrency(document.calculator["total_cost_ex_vat"]);
			document.calculator["total_cost_inc_vat"].value = fmtMoney(total_cost_inc_vat, 2, '.', ',' );
			fmtCurrency(document.calculator["total_cost_inc_vat"]);

			document.calculator["monthly_2"].value = fmtMoney(monthly_2, 2, '.', ',' );
			fmtCurrency(document.calculator["monthly_2"]);
			document.calculator["total_payments_2"].value = fmtMoney(total_payments_2, 2, '.', ',' );
			fmtCurrency(document.calculator["total_payments_2"]);
			document.calculator["less_estimated_tax_releif_2"].value = fmtMoney(less_estimated_tax_releif_2, 2, '.', ',' );
			fmtCurrency(document.calculator["less_estimated_tax_releif_2"]);
			document.calculator["net_cost_2"].value = fmtMoney(net_cost_2, 2, '.', ',' );
			fmtCurrency(document.calculator["net_cost_2"]);

			document.calculator["monthly_3"].value = fmtMoney(monthly_3, 2, '.', ',' );
			fmtCurrency(document.calculator["monthly_3"]);
			document.calculator["total_payments_3"].value = fmtMoney(total_payments_3, 2, '.', ',' );
			fmtCurrency(document.calculator["total_payments_3"]);
			document.calculator["less_estimated_tax_releif_3"].value = fmtMoney(less_estimated_tax_releif_3, 2, '.', ',' );
			fmtCurrency(document.calculator["less_estimated_tax_releif_3"]);
			document.calculator["net_cost_3"].value = fmtMoney(net_cost_3, 2, '.', ',' );
			fmtCurrency(document.calculator["net_cost_3"]);

			document.calculator["monthly_4"].value = fmtMoney(monthly_4, 2, '.', ',' );
			fmtCurrency(document.calculator["monthly_4"]);
			document.calculator["total_payments_4"].value = fmtMoney(total_payments_4, 2, '.', ',' );
			fmtCurrency(document.calculator["total_payments_4"]);
			document.calculator["less_estimated_tax_releif_4"].value = fmtMoney(less_estimated_tax_releif_4, 2, '.', ',' );
			fmtCurrency(document.calculator["less_estimated_tax_releif_4"]);
			document.calculator["net_cost_4"].value = fmtMoney(net_cost_4, 2, '.', ',' );
			fmtCurrency(document.calculator["net_cost_4"]);

			document.calculator["monthly_5"].value = fmtMoney(monthly_5, 2, '.', ',' );
			fmtCurrency(document.calculator["monthly_5"]);
			document.calculator["total_payments_5"].value = fmtMoney(total_payments_5, 2, '.', ',' );
			fmtCurrency(document.calculator["total_payments_5"]);
			document.calculator["less_estimated_tax_releif_5"].value = fmtMoney(less_estimated_tax_releif_5, 2, '.', ',' );
			fmtCurrency(document.calculator["less_estimated_tax_releif_5"]);
			document.calculator["net_cost_5"].value = fmtMoney(net_cost_5, 2, '.', ',' );
			fmtCurrency(document.calculator["net_cost_5"]);
		}
	}
}


function  fmtMoney( n, c, d, t ) {
 	var m = ( c = Math.abs( c ) + 1 ? c : 2, d = d || ",", t = t || ".", /(\d+)(?:(\.\d+)|)/.exec( n + "" ) ), x = m[1].length % 3; 
	return ( x ? m[1].substr( 0, x ) + t : "" ) + m[1].substr( x ).replace( /(\d{3})(?=\d)/g, "$1" + t ) + ( c ? d + ( +m[2] ).toFixed( c ).substr( 2 ) : "" ); 
}

function fmtCurrency(cost) {
	if (cost.value.substring(cost.value.length-2)==".N")
		cost.value=cost.value.replace(".N",".00");
	if (cost.value.substring(cost.value.length-3)==",.N")
		cost.value=cost.value.replace(",.N",".00");
	if (cost.value.substring(cost.value.length-4).substring(0,2)==",.")
		cost.value=cost.value.replace(",.",".");
	if (cost.value.charAt(0)=="£")
		return;
	cost.value='£'+cost.value;
}

function  fmtNumber(cost) {
	if (cost.value!="") {
		num = cost.value.replace("£","");
		while (true) {
			if (num.indexOf(",")>0)			 
				num = num.replace(",","");
			else	
				break;
		}
		return parseFloat(num);
	}
	return "";
}