// JavaScript Document

function Validate() {
	if (document.getElementById('total').value=='0.00') return false;
	return true;
}

function Clean(o) {
	o.value = o.value.replace(/\D/g,"");
}

function Update(o) {
	v = parseInt(o.value);
	if (v>0) {
		o.style.backgroundColor='#FFFFFF';
	} else {
		o.style.backgroundColor='#CCCCCC';
	}
	
	UpdateAll();
	
	//o.parentElement.nextSibling.lastChild.value = pence((o.value*(o.expandoUnitCost*100))/100);
}


function UpdateAll() {
	
	x = document.getElementsByTagName('input');
	
	var subtotal = 0;
	var postage = '';
	
	for (i=0; i<x.length; i++) {
		if (x[i].name.substring(0,3)=='qty') {
			if (parseInt(x[i].value)>0) {
				
				itemtotal = x[i].value*(x[i].expandoUnitCost);
				
				document.getElementById(x[i].id.replace('qty','cost')).value = pence(itemtotal);
				
				subtotal+=itemtotal;
				
				if (postage.indexOf(x[i].expandoPostage)==-1) postage+=x[i].expandoPostage+",";
			}
		}
		
	}
	document.getElementById('subtotal').value = pence(subtotal);
	
	postageArray = postage.split(',');
	postageAmount = 0;
	for (i=0; i<postageArray.length; i++) if (postageArray[i]!='') postageAmount+=parseInt(postageArray[i]);
		
	document.getElementById('postage').value = pence(postageAmount);
	
	document.getElementById('total').value = pence(subtotal+postageAmount);
}


function pence(amount) {
	// returns the amount in the .99 format
	amount -= 0;
	amount = amount / 100;
	amount = Math.round(amount*Math.pow(10,2))/Math.pow(10,2);
	return (amount == Math.floor(amount)) ? amount + '.00' : (  (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}