
	function tasteStore ()
	{
		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)
		{
			this.setQuantity(item_id, document.getElementById('quantity['+item_id+']').value);
			showBox('item_added_'+item_id);
			//location.href = '/cart/add/'+item_id+'?quantity='+document.getElementById('quantity['+item_id+']').value+'&return='+this.returnURL;
		}
		
		this.addExtraToCart = function (item_id)
		{
			this.setQuantity(item_id, 1);
			showBox('item_extra_added');
			//location.href = '/cart/add/'+item_id+'?quantity='+document.getElementById('quantity['+item_id+']').value+'&return='+this.returnURL;
		}

		this.toggleItem = function (item_id)
		{
			var description = document.getElementById('description['+item_id+']');
			
			if (description.style.display == 'none')
				description.style.display = 'block';
			else
				description.style.display = 'none';
		}
		
		this.validate = function ()
		{
			//if (!this.form)
			this.form = YAHOO.util.Dom.get('checkoutForm');
			
			//alert('validate');
			//Added on 05-08-09;
			/*var currencySymbol = YAHOO.util.Dom.get('currencySymbol').value
			var multicurrencycartTotalValue = parseInt(YAHOO.util.Dom.get('muticurrencycartTotal').value);
			var lowestCartTotalValue = parseInt(YAHOO.util.Dom.get('lowestCartTotal').value);
			alert(multicurrencycartTotalValue);
			alert(currencySymbol);
			alert(lowestCartTotalValue);
			if (multicurrencycartTotalValue < lowestCartTotalValue)
			{
				alert('Sorry, you need at least '+currencySymbol+lowestCartTotalValue' worth of goods in your cart before you can complete your order.');
				return false;
			}*/
			
			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;
			}
			
			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/Suburb"))
				return false;
	
			if (!validRequired(this.form.details_State,"State"))
				return false;
	
			if (!validRequired(this.form.details_Postcode,"Postcode"))
				return false;
	
			if (!validRequired(this.form.details_Phone,"Phone"))
				return false;		
				
			if (YAHOO.util.Dom.get('details_HowDidYouHear').value == '0')
			{
				alert('Please select an option for \'How did you hear about us?\'');
				return false;
			}

			if (YAHOO.util.Dom.get('shipping_id').value == '0')
			{
				alert('Please select a shipping location');
				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/Suburb"))
					return false;
		
				if (!validRequired(this.form.details_DeliveryState,"Delivery State"))
					return false;
		
				if (!validRequired(this.form.details_DeliveryPostcode,"Delivery Postcode"))
					return false;
		
				//if (!validRequired(this.form.details_DeliveryPhone,"Delivery Phone"))
					//return false;
			}		
			
			return true;			
		}
		
		this.submitForm = function (validateFunction)
		{
			if (validateFunction)
				this.form.submit();
		}
		
		this.goToCheckout = function ()
		{
			//Modified on 05-08-09;
			/*alert("check out");
			var multicurrencycartTotalValue = parseInt(YAHOO.util.Dom.get('muticurrencycartTotal').value;
			if (multicurrencycartTotalValue < 40)
			{
				alert('Sorry, you need at least '.multicurrencycartTotalValue.' worth of goods in your cart before you can complete your order.');
				return false;
			}
			*/
			//alert('helloo');
			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('li');
						
						itemWrapper.id = 'item_wrapper_'+branch['id'];
						selectHTML = branch['title']+' x <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'];
				}
				else
				{	
					YAHOO.util.Dom.get('item_quantity_'+branch['id']).value = branch['quantity'];
					YAHOO.util.Dom.get('item_cost_'+branch['id']).innerHTML = branch['cost'];
				}
			}
			
			/*//currency symbol js siva
			
			var curval=document.getElementById("currency").value;
			var cursym="&#36;";
			if(curval==1)
			{
				cursym="&#36;";
			}
			else if(curval==2)
			{
				cursym="&#8364;";
			}
			else if(curval==3)
			{
				cursym="&#36;";
			}
			else if(curval==4)
			{
				cursym="&#163;";
			}
			else if(curval==5)
			{
				cursym="&#36;";
			}
					
			YAHOO.util.Dom.get('currency_symbol').innerHTML = cursym;
			
			//currency symbol

			if(document.getElementById("xcngid").value!=null && document.getElementById("xcngid").value > 0 )
			{
				var crtdata = data['xml']['totals']['cart'] * (document.getElementById("xcngid").value);
				var egval=crtdata.toFixed(2);
				YAHOO.util.Dom.get('total_cart').innerHTML=egval;
			}
			else
			{
				YAHOO.util.Dom.get('total_cart').innerHTML=data['xml']['totals']['cart'];
			}*/

			/*if(document.getElementById("shipping_cost").innerHTML !=null && document.getElementById("shipping_cost").innerHTML > 0 )) {
				data['xml']['totals']['cart'] *= document.getElementById("shipping_cost").innerHTML;
				data['xml']['totals']['cart'] = data['xml']['totals']['cart'].toFixed(2);
			}*/
			
			
			var exRate = 1;
			if(YAHOO.util.Dom.get('exRate') != null)
				exRate = YAHOO.util.Dom.get('exRate').innerHTML;
			data['xml']['shipping']['cost'] = (data['xml']['shipping']['cost'] * exRate);
			data['xml']['shipping']['cost'] = data['xml']['shipping']['cost'].toFixed(2);

			data['xml']['totals']['cart'] = (data['xml']['totals']['cart'] * exRate);
			data['xml']['totals']['cart'] = data['xml']['totals']['cart'].toFixed(2);

			
			YAHOO.util.Dom.get('total_cart').innerHTML=data['xml']['totals']['cart'];
			YAHOO.util.Dom.get('cartTotal').value = data['xml']['totals']['cart'].split(',').join('');
			
			if (this.checkout)
			{
				
				
				YAHOO.util.Dom.get('shipping_id').value = data['xml']['shipping']['id'];								
				YAHOO.util.Dom.get('shipping_cost').innerHTML = data['xml']['shipping']['cost'];
				YAHOO.util.Dom.get('total_shipping').innerHTML = data['xml']['shipping']['cost'];				
				YAHOO.util.Dom.get('total_gst').innerHTML = data['xml']['totals']['gst'];
				YAHOO.util.Dom.get('total_credit').innerHTML = data['xml']['totals']['credit'];
				YAHOO.util.Dom.get('total_grand').innerHTML = data['xml']['totals']['grand'];
			}
		}
		
		this.setShipping = function (shipping_id)
		{
			var callback = 
			{
				success: store.drawCart,
				failure: store.failure,
				scope: store
			}			
			
			this.cObj = YAHOO.util.Connect.asyncRequest('GET', '/cart/request/shipping/'+shipping_id, 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
			}			
			
			this.cObj = YAHOO.util.Connect.asyncRequest('GET', '/cart/request/update/'+item_id+'?quantity='+quantity, 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 tasteStore();
	
