function processOrderForm() {
	// Constants
	var prices = { 'easter': 35.00, 'tempranillo': 35.00, 'shiraz': 35.00, 'grenache': 35.00, '06shiraz': 35.00, '06grenache': 35.00, 'gewurztraminer': 25.00, 'riesling' : 25.00 };
	var quantity_limits = { 'tempranillo': 100, 'grenache': 100 };
	
	// Process Quantities
	var quantities = document.getElementsByClassName('quantity');
	for(var i=0; i < quantities.length ;i++) {
		// Sanitize quanity
		quantities[i].value = Math.floor(quantities[i].value);
		if (quantities[i].value == 'NaN') {
			quantities[i].value = 0;
		}
		
		// Enforce quantity limit
		if (quantity_limits[quantities[i].id] != null && quantities[i].value > quantity_limits[quantities[i].id]) {
			quantities[i].value = quantity_limits[quantities[i].id];
		}
		
		// Display sub and final totals
		if (quantities[i].value > 0) {
			document.getElementById(quantities[i].id + '_total').value = quantities[i].value * prices[quantities[i].id];
		} else {
			quantities[i].value = '';
			document.getElementById(quantities[i].id + '_total').value = '';
		}
	}
	
	// Update Freight
	document.getElementById('freight').value = document.getElementById('freightcost').value;
	
	// Add values into total
	var total = 0.0;
	var totals = document.getElementsByClassName('total');
	for(var i=0; i < totals.length ;i++) {
		value = parseFloat(totals[i].value);			
		if (!isNaN(value)) {
			total += value;
			totals[i].value = '$' + value.toFixed(2);
		}
	}
	
	// Update Total
	document.getElementById('grandtotal').value = '$' + total.toFixed(2);
}

function initOrderForm() {
	// Set onchange on quantities
	var quantities = document.getElementsByClassName('quantity');
	for(var i=0; i < quantities.length ;i++) {
		element = quantities[i];

		element.onchange = function() {
			processOrderForm();
		}
	}
	
	// Set onchange on freight
	document.getElementById('freightcost').onchange = function() {
		processOrderForm();
	}
}

addEvent(window, 'load', initOrderForm);

