// JavaScript Document
window.onload = initForm;



function initForm() {
	var allTags = document.getElementsByTagName("*");

	for (var j=0; j<allTags.length; j++) {
		if (allTags[j].readOnly) {
			allTags[j].onfocus = skipField;
		}
	}
}

function skipField() {
	this.blur();
}

function calcamount() {
		var amount = Array();
		var amnt = Array();
		var formelement = Array();
		var total = 0;
		var nocards = 0;
		var subtot = 0;
		var grandcards = 0;
		var grandtot = 0;
		var amt = " ";
		var cost = document.forms.form1[0].value;
		
		
		for (var i = 2; i < 29; i=i+3) {
		
			document.forms.form1[i + 1].value = 0;				
			amnt[i] = parseFloat(document.forms.form1[i].value);
			
			if  (amnt[i] >= 0) {
				nocards = amnt[i];
				amount[i] = Math.round ((amnt[i]) * (cost) * 100);
				amount[i] /= 100;
				amt = cent(amount[i]);
				document.forms.form1[i + 1].value = amt;
				amt = " "; 	
				total = Math.round (amount[i] * 100);
				total /= 100;
				
				subtot = Math.round ((subtot + total) * 100);
				subtot = subtot / 100;
				amt = cent(subtot);
				document.forms.form1.subtot.value = amt;
				grandcards = grandcards + nocards;
				document.forms.form1.subno.value = grandcards;
				if (amt <= 10) {
					postage=2.14;}
				else if (amt <= 20) {
						postage=2.65;}
					else if (amt <= 30) {
						postage=3.25;}
					else {postage=0;}
				amt = " ";  
				postage = Math.round (postage * 100);
				postage = postage / 100;
				amt = cent(postage);
				document.forms.form1.postage.value = amt;
				grandtot = subtot + postage;
				grandtot = Math.round (grandtot * 100);
				grandtot = grandtot / 100;
				amt = cent(grandtot);
				document.forms.form1.grandtot.value = "£ " + amt;
				amt = " "; 
				}
			
			}
			
			
	}
	
function cent(amount) {
// returns the amount in the .99 format 
    amount -= 0;
    amount = (Math.round(amount*100))/100;
    return (amount == Math.floor(amount)) ? amount + '.00' : (  (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}
