
	function tagariStore ()
	{
		this.checkout = false;
		
		this.form = YAHOO.util.Dom.get('checkoutForm');		
		
		/*
		this.updateCart = function (item_id)
		{
			location.href = '/cart/update/'+item_id+'?quantity='+document.getElementById('cart_quantity['+item_id+']').value+'&return='+this.returnURL;
		}
		*/
		
		this.addToCart = function (item_id, show)
		{
			if (typeof show == 'undefined')
				show = 'item_added_'+item_id;
			
			var quantity = 1;
			
			var cartQuantity = YAHOO.util.Dom.get('item_quantity_'+item_id);
			if (cartQuantity)
				quantity += parseInt(cartQuantity.value);
			
			this.setQuantity(item_id, quantity);
			showBox(show);
			//location.href = '/cart/add/'+item_id+'?quantity='+document.getElementById('quantity['+item_id+']').value+'&return='+this.returnURL;
		}
		
		this.validate = function ()
		{
			//if (!this.form)
				this.form = YAHOO.util.Dom.get('checkoutForm');
			
			if (!document.getElementById('wholesale'))
			{
				if (!validRequired(this.form.name_first,"First Name"))
					return false;

				if (!validRequired(this.form.name_last,"Last Name"))
					return false;

				if (!validEmail(this.form.email,"Email",true))
					return false;
			}
			
			if (!validRequired(this.form.details_Address,"Address"))
				return false;
	
			if (!validRequired(this.form.details_Town,"Town"))
				return false;	

			if (!validRequired(this.form.details_State,"State/Province"))
				return false;	

			//if ((this.form.details_Country == 'AU') && !validRequired(this.form.details_Postcode,"Postcode"))
			if (!validRequired(this.form.details_Postcode,"Postcode"))
				return false;
	
			if (!validRequired(this.form.details_Phone,"Phone"))
				return false;		
				
			if (!this.form.sameAsBilling.checked)
			{
				if (!validRequired(this.form.details_DeliveryName,"Delivery Name"))
					return false;
		
				if (!validRequired(this.form.details_DeliveryAddress,"Delivery Address"))
					return false;
					
				if (!validRequired(this.form.details_DeliveryTown,"Delivery Town"))
					return false;										
					
				if (!validRequired(this.form.details_DeliveryState,"Delivery State/Province"))
					return false;					
								
				//if ((this.form.details_DeliveryCountry == 'AU') && !validRequired(this.form.details_DeliveryPostcode,"Delivery Postcode"))
				if (!validRequired(this.form.details_DeliveryPostcode,"Delivery Postcode"))
					return false;
		
			}	
			
			return true;			
		}
		
		this.validatePayment = function ()
		{
			//if (!this.form)
				this.form = YAHOO.util.Dom.get('checkoutForm');
				
			/*
			if (parseInt(YAHOO.util.Dom.get('cartTotal').value) < 40)
			{
				alert('Sorry, you need at least $40 worth of goods in your cart before you can complete your order.');

                $bulkDiscount = '<em>'.$bulkDiscount.'</em>';
				return false;
			}
			*/

            // zak: If user is opting to complete payment over the phone, then we won't need to verify credit card details
            if (this.form.cc_alternative.checked) {
                return true;
            } else {
                if (!validRequired(this.form.cc_number,"Credit Card Number"))
                    return false;

                if (!validRequired(this.form.cc_code,"Credit Card CVV Code"))
                    return false;
                    
                if ((this.form.cc_exp_month.value == '0') || (this.form.cc_exp_year.value == '0'))
                {
                    alert('Please enter your credit card expiry details.');
                    return false;
                }
                
                return true;			
            }
		}		
		
		this.submitForm = function (validateFunction)
		{
			if (validateFunction)
				this.form.submit();
		}
		
		this.goToCheckout = function ()
		{
			/*
			if (parseInt(YAHOO.util.Dom.get('cartTotal').value) < 40)
			{
				alert('Sorry, you need at least $40 worth of goods in your cart before you can complete your order.');
				return false;
			}
			*/
			
			location.href = '/checkout';
		}
		
		this.drawCart = function (o)
		{
			data = xml2array(o.responseXML);
			
			if (data['xml']['items'])
				parentBranch = data['xml']['items']['item'];
			else
				parentBranch = data['xml']['item']
			
			if (!this.checkout)
			{
				//hideBox('cart_placeholder');
				showBox('cart_wrapper');
				
				wrapper = YAHOO.util.Dom.get('cart_items');
			}
			
			for (x in parentBranch)
			{
				if (isNaN(x))
					branch = parentBranch;
				else
					branch = parentBranch[x];

				if (branch['amount'] == '0')
					continue;
					
				if (!this.checkout)
				{
					itemWrapper = YAHOO.util.Dom.get('item_wrapper_'+branch['id']);
					if (!itemWrapper)
					{
						itemWrapper = document.createElement('div');
						YAHOO.util.Dom.addClass(itemWrapper, 'cartItem');
						
						itemWrapper.id = 'item_wrapper_'+branch['id'];
						selectHTML = '<span>'+branch['title']+'</span>'+
                            '<div id="item_cost_discount_'+branch['id']+'"></div>'+
                            '<div align="right" class="quantity">Quantity: <select id="item_quantity_'+branch['id']+'" onChange="store.setQuantity('+branch['id']+', this.value)">';
						
						for (i = 0; i < 21; i++)
						{
							selectHTML += '<option value="'+i+'"';
							if (i == branch['quantity'])
								selectHTML += ' selected="selected"';
							selectHTML += '>'+i+'</option>';
						}
						
						selectHTML += '</select>';
						
						itemWrapper.innerHTML = selectHTML;
						
						/*
						quantity = document.createElement('select');
						quantity.id = 'item_quantity_'+branch['id'];
										
						for (i = 0; i < 21; i++)
						{
							quantityOption = document.createElement('option');
							quantityOption.value = i;
							quantityOption.innerHTML = i;
	
							if (i == branch['quantity'])
								quantityOption.selected = 'selected';
								
							quantity.appendChild(quantityOption);
						}
						
						quantity.onchange = store.setQuantity(branch['id'], null);						
						
						itemWrapper.appendChild(quantity);
						*/
						
						wrapper.insertBefore(itemWrapper, YAHOO.util.Dom.get('cartTotalText'));
					}
					else
						YAHOO.util.Dom.get('item_quantity_'+branch['id']).value = branch['quantity'];

                        var bulkDiscount = branch['bulk_discount_taxfree'];
                        //alert(branch['bulk_discount_taxfree']);
                        if (bulkDiscount > 0)
                        {
                            YAHOO.util.Dom.get('item_cost_discount_'+branch['id']).innerHTML = '<em>Discount -$'+bulkDiscount+'</em>';
                        }
                        else
                        {
                            YAHOO.util.Dom.get('item_cost_discount_'+branch['id']).innerHTML = '';
                        }
				}
				else
				{	
					YAHOO.util.Dom.get('item_quantity_'+branch['id']).value = branch['quantity'];
					YAHOO.util.Dom.get('item_cost_'+branch['id']).innerHTML = branch['cost_taxfree'];
					
					var bulkDiscount = branch['bulk_discount_taxfree'];
					if (bulkDiscount > 0)
					{
						YAHOO.util.Dom.get('item_cost_discount_'+branch['id']).innerHTML = bulkDiscount;
						showBox('item_discount_label_'+branch['id']);
						showBox('item_discount_amount_'+branch['id']);
					}
					else
					{
						hideBox('item_discount_label_'+branch['id']);
						hideBox('item_discount_amount_'+branch['id']);
					}
				}
			}
			
			if (this.checkout)
			{		
				YAHOO.util.Dom.get('total_cart').innerHTML = data['xml']['totals']['cart_taxfree'];
				YAHOO.util.Dom.get('total_discount').innerHTML = data['xml']['discounts']['bulk']['cost_taxfree'];
				YAHOO.util.Dom.get('total_shipping').innerHTML = data['xml']['shipping']['cost_taxfree'];
				YAHOO.util.Dom.get('total_handling').innerHTML = data['xml']['handling']['cost_taxfree'];
				YAHOO.util.Dom.get('total_gst').innerHTML = data['xml']['totals']['gst'];
				YAHOO.util.Dom.get('total_usa_surcharge').innerHTML = data['xml']['usa_surcharge'];
				//YAHOO.util.Dom.get('total_credit').innerHTML = data['xml']['totals']['credit'];
				YAHOO.util.Dom.get('total_grand').innerHTML = data['xml']['totals']['grand'];
				
				if (data['xml']['shipping']['debug'])
				{
					console.log('drawing debug link');
					showBox('debugLink');
					YAHOO.util.Dom.get('debugLink').innerHTML = '<a href="'+data['xml']['shipping']['debug']+'">Debug CSV</a>';
				}
				
				if (data['xml']['usa_surcharge'] == '0.00')
				{
					hideBox('surcharge_cost');
				}
				else
				{
					showBox('surcharge_cost');
				}
				
				if (data['xml']['shipping']['unavailable'] == '1')
				{
					showBox('shipping_unavailable');
					hideBox('shipping_cost');
				}
				else
				{
					hideBox('shipping_unavailable');
					showBox('shipping_cost');
				}
			}
			else
			{
				YAHOO.util.Dom.get('total_cart').innerHTML = data['xml']['totals']['cart'];				
				YAHOO.util.Dom.get('total_cart_taxfree').innerHTML = data['xml']['totals']['cart_taxfree'];
			}
		}
		
		this.getShippingCodes = function ()
		{
			this.form = YAHOO.util.Dom.get('checkoutForm');
			
			if (!this.form)
				return [0, 0, 0];
			
			var postcode = null;
			
			if (this.form.sameAsBilling.checked && this.form.details_Postcode.value.length)
				var postcode = this.form.details_Postcode.value;
			else if (this.form.details_DeliveryPostcode.value.length)
				var postcode = this.form.details_DeliveryPostcode.value;
				
			var billing_countrycode = this.form.details_Country.value;
				
			var shipping_countrycode = null;
			
			if (this.form.sameAsBilling.checked && this.form.details_Country.value.length)
				var shipping_countrycode = this.form.details_Country.value;
			else if (this.form.details_DeliveryCountry.value.length)
				var shipping_countrycode = this.form.details_DeliveryCountry.value;				
				
			var codes = [postcode, shipping_countrycode, billing_countrycode];
				
			return codes;
		}
		
		this.updateShipping = function ()
		{
			shippingCodes = this.getShippingCodes();
			
			var callback = 
			{
				success: store.drawCart,
				failure: store.failure,
				scope: store
			}			
			
			this.cObj = YAHOO.util.Connect.asyncRequest('GET', '/cart/request/update?shipping_postcode='+shippingCodes[0]+'&shipping_country='+shippingCodes[1]+'&billing_country='+shippingCodes[2], callback,null);
		}
		
		this.setQuantity = function (item_id, quantity)
		{
			if (quantity == null)
			{
				selector = YAHOO.util.Dom.get('item_quantity_'+item_id);
				if (!selector)
					return;
					
				quantity = selector.value;
			}
			
			if (quantity == 0)
				deleteBox2('item_wrapper_'+item_id);
			
			var callback = 
			{
				success: store.drawCart,
				failure: store.failure,
				scope: store
			}
			
			var shippingCodes = this.getShippingCodes();
			
			this.cObj = YAHOO.util.Connect.asyncRequest('GET', '/cart/request/update/'+item_id+'?quantity='+quantity+'&shipping_postcode='+shippingCodes[0]+'&shipping_country='+shippingCodes[1]+'&billing_country='+shippingCodes[2], callback,null);
		}
		
		/*
		this.removeItem = function (item_id)
		{
			el = YAHOO.util.Dom.get('item_'+item_id);
			if (el)
				el.parentNode.removeChild(el);
		
			this.setQuantity(item_id, 0);
		}	
		*/
	}
	
	var store = new tagariStore();
	

