function txt_onkeypress(allowdot) {
	var isNS4 = (navigator.appName=="Netscape" || parseInt(navigator.appVersion) == 4)?1:0;
	if(!isNS4) {
		if (!(event.keyCode == 46 && allowdot) && (event.keyCode < 48 || event.keyCode > 57)) {
			event.returnValue = false;
			return false;
		}
	} else {
		if (!(event.which == 46 && allowdot) && (event.which < 48 || event.which > 57)) {
			return false;
		}
	}
}

// some global vars (icky, but it makes life sooo much easier!)

var oldcustomers = 150000;
var oldgb = 5;

var gbdownstream = new Array();
var gb = new Array();
var portcost = new Array();
var bandwidthcost = new Array();
var totalcostpm = new Array();

// important baseline figures

var bt622cost = 1758693 / 12; // £ per month
var totalthroughputpm = 622 * 1.5 / 8 * 60 * 60 * 24 * 30 / 1000;
var costpergb = bt622cost / totalthroughputpm;

function repeatstr(str, numtimes) {
	return (new Array(numtimes + 1)).join(str);
}

function decimalplaces(num, places) {
	num = Math.round(num * Math.pow(10, places)) / Math.pow(10,places);
	// that was the easy bit.. now to add extra 0's...
	var str = num.toString();
	i = str.indexOf(".");
	if (i == -1 && places > 0) {
		str += "." + repeatstr("0", places);
	}
	if (places - (str.length - i - 1) > 0) {
		str += repeatstr("0", places - (str.length - i - 1));
	}
	return str;
}

function insertcommas(num) {
	var str = num.toString();
	var dot = str.indexOf(".");
	if (dot == -1) {
		dot = str.length;
	}
	if (dot <= 3) {
		return str;
	}
	
	var newstr = "";
	if (dot != str.length) {
		newstr = str.substring(dot); // put decimal part into newstr
	}

	var i = dot;
	
	while (i > 3) {
		newstr = "," + str.substring(i-3, i) + newstr;
		i -= 3;
	}

	newstr = str.substring(0, i) + newstr;
	return newstr;
	
}

function filltable(id, idx, format) {
	var obj = document.getElementById(id + idx);
	var num = eval(id + "[" + idx + "]");

//	obj.innerHTML = (eval(id));
	
	if (format < 100) {
		str = decimalplaces(num, format);
	}
	
	if (format >= 100) {
		var places = format - 100;
		str = decimalplaces(num, places);
		str = insertcommas(str);
		str = "£" + str;
	}
	obj.innerHTML = str;

}

function btnCalculate_onclick() {
	gb[1] = document.frmCosts.txtGB.value;
	
	
	if (isNaN(parseFloat(gb[1]))) {
		window.alert("Please enter valid numbers!");
		gb[1] = oldgb;
		document.frmCosts.txtGB.value = oldgb;
		oldgb = gb[1];
		return false;
	}

	gb[1] = parseFloat(gb[1]);
	
	if (document.frmCosts.rdoDirection[0].checked) {
		// adjust total GB accordingly
		gb[1] *= 1.5;
	}

	oldgb = gb[1];


//	var costpergb = 0.489; // in £


	gbdownstream[1] = gb[1] * 2/3;
	portcost[1] = 9.87; // per annum, assume ipstream home
	bandwidthcost[1] = costpergb * gb[1];
	totalcostpm[1] = bandwidthcost[1] + portcost[1];
	
	filltable("gbdownstream", 1, 2);
	filltable("gb", 1, 2);
	filltable("portcost", 1, 102);
	filltable("bandwidthcost", 1, 102);
	filltable("totalcostpm", 1, 102);
}

function doAllCalculations() {
	btnCalculate_onclick();
	for (var i = 2; i <= 6; i++) {
		gb[i] = 5 * (i-1);
		if (document.frmCosts.rdoDirection[0].checked) {
			// adjust total GB accordingly
			gb[i] *= 1.5;
		}
		gbdownstream[i] = gb[i] * 2/3;
		portcost[i] = 9.87; // per annum, assume ipstream home
		bandwidthcost[i] = costpergb * gb[i];
		totalcostpm[i] = bandwidthcost[i] + portcost[i];
		
		filltable("gbdownstream", i, 2);
		filltable("gb", i, 2);
		filltable("portcost", i, 102);
		filltable("bandwidthcost", i, 102);
		filltable("totalcostpm", i, 102);
	}

}
