
var Cafe = Class.create({

	initialize: function() {
		var quantities = $$('input.quant');
		for (var i=0;i<quantities.length;i++)
		{
			Event.observe(quantities[i],'keyup',this.updateTotal.bind(this));
			Event.observe(quantities[i],'blur',this.updateTotal.bind(this));
			Event.observe(quantities[i],'focus',this.updateTotal.bind(this));
		}
		this.Group = '';
		this.CorporateDeliveryFee = 0.00;
		this.SchoolDeliveryFee = 0.00;
		this.KaringalDeliveryFee = 0.00;
		this.MinOrderCost = 0.00;
	},
	
	updateTotal: function() {

		var total_quantity= 0;
		var subtotal = 0.00;
		
		// loop through all quantities
		var quantities = $$('input.quant');
		for (var i=0;i<quantities.length; i++)
		{
			var val = parseInt(quantities[i].value);
			if (!isNaN(val) && val != undefined)
			{
				total_quantity += val;
				var cost = quantities[i].up(0).previous('td').down('span.cost');
				if (cost != undefined)
				{
					cost = cost.innerHTML.replace('$','');
					subtotal += cost * val;
				}
			}
			
		}
		$('subtotal').innerHTML = '$' + subtotal.toFixed(2);
		$('total_quantity').innerHTML = total_quantity;

		var delivery = 0.00;
		if (this.Group == 'School Lunch Orders')
		{
			//if (subtotal < this.MinOrderCost)
			//	delivery =  parseFloat(this.SchoolDeliveryFee);
		}
		else if (this.Group == 'Corporate Catering')
			delivery =  parseFloat(this.CorporateDeliveryFee);
		else if (this.Group == 'Karingal Catering') {
			if (subtotal < this.MinOrderCost) {
				delivery =  parseFloat(this.KaringalDeliveryFee);
			}
		}
			
		$('delivery').innerHTML = '$' + delivery.toFixed(2);
			
		var total = parseFloat($('delivery').innerHTML.replace('$','')) + subtotal ;
		$('total').innerHTML = '$' + total.toFixed(2);
		
		var gst = parseFloat(total / 11);
		// Karingal Catering doesn't get charged GST
		if (this.Group == 'Karingal Catering') {
			gst = 0.0;
		}
		$('gst').innerHTML = '$' + gst.toFixed(2);
	}
    
});