var cartUpdater = { 
	init: function(prices, minBags, payPalBuiness)
	{
		this.payPalBuiness = payPalBuiness;
		this.prices = prices;
		this.minBags = minBags;
		
		this.hardwoodQty = dojo.byId("hardwoodQty");
				
		this.hardwoodUnitPrice = dojo.byId("hardwoodUnitPrice");
		this.hardwoodTotalPrice = dojo.byId("hardwoodTotalPrice");

		this.cypressQty = dojo.byId("cypressQty");
		this.cypressUnitPrice = dojo.byId("cypressUnitPrice");
		this.cypressTotalPrice = dojo.byId("cypressTotalPrice");
		
		this.totalPrice = dojo.byId("totalPrice");
		this.checkoutButton = dojo.byId("checkout");
		
		this.grandTotal = 0;
		
	    dojo.connect(this.hardwoodQty, "onchange",this, "onChanged");	    
	    dojo.connect(this.cypressQty, "onchange",this, "onChanged");	    
		
		//dojo.connect(dojo.byId("update"), "onclick", this, "updatePrices");
		//dojo.connect(dojo.byId("checkout"), "onclick", this, "onCheckout");
		
//		dojo.connect(dojo.byId("mulchform"), "onsubmit", function(evt) {
//			evt.preventDefault();
//		});		
	},	
	onChanged: function(evt){ 
		var controlName = evt.target.name;
		var qty = evt.target.value - 0;
		
		if (isNaN(qty)) {
			evt.target.value = 0;
			if( evt.target.value.length) {
				alert("Please enter only numbers, to clear, enter zero (0)");				
			}
		} else if ( qty < 1) {
			evt.target.value = 0;
		} else if ( qty < this.minBags) {
			evt.target.value = this.minBags;			
		} else {
			evt.target.value = qty;
		}	
//		this.updatePrices();		
//	},
//	updatePrices: function()
//	{
		this.grandTotal = 0;
		var rtnValue = cartUpdater.setPrice( this.hardwoodQty.value - 0, this.prices.hardwood);
		
		this.hardwoodUnitPrice.innerHTML = "$" + rtnValue.unit.toFixed(2);
		this.hardwoodTotalPrice.innerHTML = "$" + rtnValue.total.toFixed(2);
		this.grandTotal = rtnValue.total;
		
		rtnValue = cartUpdater.setPrice( this.cypressQty.value - 0, this.prices.cypress);

		this.cypressUnitPrice.innerHTML = "$" + rtnValue.unit.toFixed(2);
		this.cypressTotalPrice.innerHTML = "$" + rtnValue.total.toFixed(2);
		
		this.grandTotal += rtnValue.total;
		
		this.totalPrice.innerHTML  = "$" + this.grandTotal.toFixed(2);
		
//		if(this.grandTotal > 0)
//			this.checkoutButton.disabled = false;
//		else
		this.checkoutButton.disabled = true;
	},
	setPrice: function( qty, ar)
	{
		var rtnValue = Object();
		rtnValue.qty = 0;
		rtnValue.unit = 0;
		rtnValue.total = 0;
		
		for( var idx = 0; idx < ar.length; idx++)
		{
			var priceSet = ar[idx];
			var maxQty = priceSet[0] - 0;
			var unitPrice = priceSet[1] - 0;
			
			if( qty < maxQty)
			{
				rtnValue.qty = qty;
				rtnValue.unit = unitPrice;
				rtnValue.total = unitPrice * qty;
				return rtnValue;
			}			
		}
		return rtnValue;
	}/*,
	onCheckout: function()
	{
		if( this.grandTotal == 0)
			return 0;
			
		var mulchForm = dojo.query("form#mulchform");
		mulchForm.addContent("<input type='hidden' name='cmd' value='_cart'>", "end");
		mulchForm.addContent("<input type='hidden' name='upload' value='1'>", "end");
		mulchForm.addContent("<input type='hidden' name='business' value='" + this.payPalBuiness + "'>", "end");

		var hardwoodValues = cartUpdater.setPrice( this.hardwoodQty.value - 0, this.prices.hardwood);
		var cnt = this.addPriceNode(1, "hardwood", hardwoodValues);
		
		var cypresValues = cartUpdater.setPrice( this.cypressQty.value - 0, this.prices.cypress);
		this.addPriceNode(cnt, "cypress", cypresValues);
	},
	addPriceNode: function( cnt, name, values)
	{
		var mulchForm = dojo.query("form#mulchform");
		
		if(mulchForm && values.qty)
		{
			mulchForm.addContent("<input type='hidden' name='item_name_" + cnt + "' value='" + name + "'>", "end");
			mulchForm.addContent("<input type='hidden' name='quantity_" + cnt + "' value='" + values.qty + "'>", "end");
			mulchForm.addContent("<input type='hidden' name='amount_" + cnt + "' value='" + values.unit + "'>", "end");
			return cnt + 1;
		}
	}*/
};
