

//Update Prices into the relevant form fields
document.observe("dom:loaded", function() {
	$$("input").each(function(item) {
		Event.observe(item, 'change', function(d) {    	  	  	
			var total = 0;
			var GST = 15;//GST Factor here
			var orderTotal = 0;
			var orderSubTotal = 0;
			var gstSubTotal=0; 
			$$('#order-table tr.prod').each(function(d) {
				var price = parseFloat(d.select('span')[0].innerHTML);
				var prodTotal = parseFloat(d.select('.num-pallets-input')[0].value) * price;
				d.select('.row-total-input')[0].value = prodTotal;
				orderSubTotal += prodTotal;	  
			});
			var typeValue = $('orderForm').getInputs('radio','shipping').find(function(radio) { return radio.checked; }).value;
			var shippingCost = parseFloat(typeValue);
			gstSubTotal = (orderSubTotal + shippingCost)*GST/100;
			orderTotal = (orderSubTotal + shippingCost) + gstSubTotal;
			$('shippingSubTotal').value = CurrencyFormatted(orderSubTotal + shippingCost);
			$('orderTotal').value = CurrencyFormatted(orderTotal);
			$('gstSubTotal').value = CurrencyFormatted(gstSubTotal);	  
			$('orderSubTotal').value = CurrencyFormatted(orderSubTotal) ;	    	  
      	});
    }.bind(this)); 	 
  });

  function CurrencyFormatted(amount) {
	    var i = parseFloat(amount);
	    if (isNaN(i)) { i = 0.00; }
	    var minus = '';
	    if (i < 0) { minus = '-'; }
	    i = Math.abs(i);
	    i = parseInt((i + .005) * 100);
	    i = i / 100;
	    s = new String(i);
	    if (s.indexOf('.') < 0) { s += '.00'; }
	    if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
	    s = minus + s; //s is now Neutral Decimal
	    var regEx = /(\d+)(\d{3})/;
	    while (regEx.test(s)) {
	        s = s.replace(regEx, '$1' + ',' + '$2');
	    }
	    var padding = "";//possible padding 	    
	    s = "$" + padding + s;    
	    return s;
	}


  
