	/**
	*	Javascript functions to manage shopping basket functions.
	*
	*/
	
	function addToBasket(productid,categoryid,baseurl,language,messageTitle) {
		
		// Fetch product form 
		var productForm = $('#product'+productid+'basketform');
		
		// Fetch quantity value
		var quantity = $(productForm).find('.quantityfield').val();
		
		// Fetch last selected varation or set to empty string if not found
		var selectedvariant = '';
		if(($(productForm).find(".variation").length) > 0) {
			selectedvariant = $(productForm).find(".variation:last").val();
		}
		
		var datastring = "productid="+productid+"&quantity="+quantity+"&selectedvariant="+selectedvariant+"&language="+language;
		if(categoryid != null) {
			datastring += "&categoryid="+categoryid;
		}
		
		if($(productForm).find('.lengthfield')) {
			datastring += "&length="+$(productForm).find('.lengthfield').val();
		}
		
		$.ajax({
			type: "GET",
			url: baseurl+"/includes/ajaxfunctions/add_to_basket.php",
			data: datastring,
			async: false,
			success: function(message) {
				
				$.ajax({
					type: "GET",
					url: baseurl+"/includes/ajaxfunctions/shopping_basket_summary.php",
					data: "language="+language,
					async: false,
					success: function(newsummary) {
						
						displaySystemMessage(message, messageTitle, 'infoMessage', null);
						
						if($('#shopping_basket_summary').length > 0) {
							
							$('#shopping_basket_summary').fadeOut(500, function() {
								$('#shopping_basket_summary').html(newsummary);
								$('#shopping_basket_summary').fadeIn(500);
							});
						}
					},
					error: function(data) {
						alert('An error has occured. Please contact us and let us know.');
					}
				});
			},
			error: function(data) {
				alert('An error has occured adding your item. Please contact us and let us know.');
			}
		});
	}
	
	/**
	*	Function to update a variation selector based on the details passed.
	*
	*	productid - Used to indicate the product for which the variation corresponds
	*	selectedvariant - Indacates the ID of the variant that has been selected
	*	language - Indicates the current selected language to allow the new variation selector to be output in the appropriate language
	*	baseurl - Used to locate the PHP script that will return the new selector
	**/
	function updateVarationSelectors(productid,selectedvariant,language,baseurl) {
		
		$.ajax({
			type: "GET",
			url: baseurl+"/includes/ajaxfunctions/update_variations.php",
			data: "productid="+productid+"&selectedvariant="+selectedvariant+"&language="+language,
			async: false,
			success: function(newSelectors) {
				var variationsDiv = $('#product'+productid+'basketform .productvariations');
				var existingSelectorCount = $(variationsDiv).find('li').length;
				var newSelectorCount = $(newSelectors).find('li').length;
				
				if(existingSelectorCount != newSelectorCount) {
					$(variationsDiv).fadeOut(500, function() {
						$(variationsDiv).html(newSelectors);
						$(variationsDiv).fadeIn(500);
					});
				} else {
					$(variationsDiv).html(newSelectors);
				}
			},
			error: function(data) {
				alert('An error has occured. Please contact us and let us know.');
			}
		});
	}
	
	/**
	*	Function to update a price based on the details passed.
	*
	*	productid - Used to indicate the product for which the price corresponds
	*	selectedvariant - Indacates the ID of the variant that has been selected
	*	language - Indicates the current selected language to allow the new price to be output in the appropriate language
	*	baseurl - Used to locate the PHP script that will return the new price
	**/
	function updatePrice(productid,selectedvariant,language,vatrate,baseurl) {
		
		$.ajax({
			type: "GET",
			url: baseurl+"/includes/ajaxfunctions/update_product_price.php?",
			data: "productid="+productid+"&selectedvariant="+selectedvariant+"&language="+language,
			async: false,
			success: function(newPrice) {
				
				var productForm = $('#product'+productid+'basketform');
					
				var vatElement = $(productForm).find('.productexcvatprice');
				if(vatElement.length == 1) {
					
					incVatPrice = Math.round(newPrice*(1+(vatrate/100))*100)/100;
					incVatString = new String(incVatPrice);
					if(incVatString.indexOf('.') < 0) {
						incVatString += '.';
					}
					while(incVatString.indexOf('.') >  incVatString.length-3) {
						incVatString += '0';
					}
					
					if($(productForm).find('.productexcvatprice').html() !== newPrice) {
						$(productForm).find('.productprice').fadeOut(500, function() {
							$(productForm).find('.productprice').html(incVatString.toString());
							$(productForm).find('.productprice').fadeIn(500);
						});
						
						$(productForm).find('.productexcvatprice').fadeOut(500, function() {
							$(productForm).find('.productexcvatprice').html(newPrice);
							$(productForm).find('.productexcvatprice').fadeIn(500);
						});
					}
					
				} else {
					
					if($(productForm).find('.productprice').html() !== newPrice) {
						$(productForm).find('.productprice').fadeOut(500, function() {
							$(productForm).find('.productprice').html(newPrice);
							$(productForm).find('.productprice').fadeIn(500);
						});
					}
					
				}
			},
			error: function(data) {
				alert('An error has occured. Please contact us and let us know.');
			}
		});
	
	}
