
function calculateProductSubtotal(product_ident) {
	var product_row = null;
	$('/tr[@class="order_form_product_container"]').each(
		function() {
			if ( $(this).attr('xname') == product_ident ) {
				product_row = $(this);
			}
		}
	);
	pr_quantity = $('*[@xname="quantity"]', product_row).val();
	pr_price = $('*[@xname="unitprice"]', product_row).text();
	return( (Number(pr_price).toFixed(2) * Number(pr_quantity).toFixed(0)).toFixed(2) );
}

function updateProductSubtotal(evt) {
	elem = $(evt.target);
	opt_xname = elem.parent().parent().attr('xname');

	subtotal_elem = $('//*[@xname="' +opt_xname+ '"]//*[@xname="subtotal"]');
	subtotal_price = Number(calculateProductSubtotal(opt_xname)).toFixed(2);

	fmt = function(price) { return ((price>0) && ('$' + Number(price).toFixed(2)) || '-' ); }
	subtotal_elem.html( fmt(subtotal_price) );
}

function calculateOrderSubtotal() {
	var running_total = 0;
	$('//table[@class="order_form_product_list"]//tr[@class="order_form_product_container"]').each(
		function () {
			running_total += Number(calculateProductSubtotal( $(this).attr('xname') ))
		}
	);
	return( Number(running_total).toFixed(2) );
}

function getShippingCosts() {
	shippingSelect = $('//table[@class="order_form_product_list"]//*[@xname="shipping_option"]')[0];
	shippingOpt = $( shippingSelect.options[shippingSelect.selectedIndex] );
	shippingRural = $('//table[@class="order_form_product_list"]//*[@xname="shipping_rural"]');
	
	ret = { 
		optionPrice: Number( $(shippingOpt).attr('price') ),
		ruralPrice: Number( shippingRural[0].checked && $(shippingRural[0]).attr('price') ),
		totalPrice: 0
	};
	ret.totalPrice = ret.optionPrice + ret.ruralPrice;
	
	return ret;
}

function updateOrderTotals(evt) {
	tax_amount = (0.125).toFixed(3);
	shipping = getShippingCosts();
	subtotal = (Number(calculateOrderSubtotal()) + shipping.totalPrice).toFixed(2);
	tax = (subtotal * tax_amount).toFixed(2);
	total = (Number(subtotal) + Number(tax)).toFixed(2);
		
	fmt = function(price) {
		return ((price>0) && ('$' + Number(price).toFixed(2)) || '-' );
	}
	
	fld_order_shipping = $('//table[@class="order_form_product_list"]//tr[@class="order_form_freight"]//*[@xname="subtotal"]');
	fld_order_shipping.html( fmt(shipping.optionPrice) );
	
	fld_order_shipping_rural = $('//table[@class="order_form_product_list"]//tr[@class="order_form_freight_rural"]//*[@xname="subtotal"]');
	fld_order_shipping_rural.html( fmt(shipping.ruralPrice) );
	
	fld_order_subtotal = $('//table[@class="order_form_product_list"]//tr[@class="order_form_subtotal"]//*[@xname="order_subtotal"]');
	fld_order_subtotal.html( fmt(subtotal) );
	
	fld_order_tax = $('//table[@class="order_form_product_list"]//tr[@class="order_form_gst"]//*[@xname="order_gst"]');
	fld_order_tax.html( fmt(tax) );
	
	fld_order_total = $('//table[@class="order_form_product_list"]//tr[@class="order_form_total"]//*[@xname="order_total"]');
	fld_order_total.html( fmt(total) );
}


function configureOrderForm() {
	evt_handler = function(evt){updateProductSubtotal(evt);updateOrderTotals(evt);}
	$('.order_form_product_container').each(
		function() {
			q_field = $('td/*[@xname="quantity"]', $(this));
			q_field.val('');
			updateProductSubtotal({target:q_field});
			q_field.bind('keyup', evt_handler);
		}
	);
	
	$('//tr[@class="order_form_freight"] | //tr[@class="order_form_freight_rural"]').each(
		function() {
			q_field = $('td/*[@xname="shipping_option"] | td/*[@xname="shipping_rural"]', $(this));
			q_field.bind('change', evt_handler);
		}
	);
	
	updateOrderTotals();
}
