
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.Organisation = 'Other';
		this.Discount = 1.0;
		this.LateDeliveryFee = 0.00;
		this.DeliveryFee = 0.00;
		
		//this.CorporateDeliveryFee = 0.00;
		//this.SchoolDeliveryFee = 0.00;
		//this.KaringalDeliveryFee = 0.00;
		//this.MinOrderCost = 0.00;
	},

	updateTotal: function() {

        if ($('subtotal') == null) return;
        
		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 = parseFloat(this.DeliveryFee);
	
		// Organisation discount
		var discount = 0;
		if ($('discount')) {
			discount = (subtotal * this.Discount);
			$('discount').innerHTML = '$' + discount.toFixed(2);
		}

		$('delivery').innerHTML = '$' + delivery.toFixed(2);

		var total = delivery + subtotal - discount;

		total = total + this.LateDeliveryFee;

		$('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);
	}

});
