function SortShopProducts(sortoption) {
	
	if (sortoption == "pricelowtohigh") {
		$('.shop-browseproduct').sortElements(function(a, b) {				
		    return parseFloat($(a).html().replace(/\D+/, ""), 10) > parseFloat($(b).html().replace(/\D+/, ""), 10) ? 1 : -1;
		});		
	} else if (sortoption == "pricehightolow") {
		$('.shop-browseproduct').sortElements(function(a, b) {
		    return parseFloat($(a).html().replace(/\D+/, ""), 10) < parseFloat($(b).html().replace(/\D+/, ""), 10) ? 1 : -1;
		});		
	} else if (sortoption == "nameatoz") {
		
		$('.shop-browseproduct').sortElements(function(a, b) {
		    return $(a).text() > $(b).text() ? 1 : -1;
		});		
	} else if (sortoption == "nameztoa") {
		
		$('.shop-browseproduct').sortElements(function(a, b) {
		    return $(a).text() < $(b).text() ? 1 : -1;
		});		
	}
	
	return;
}

function ChangeQty(action) {
	
	if ($('#product-qty')) {
			
		var newvalue;
		var value = parseInt($('#product-qty').html());
		
		if (value > 1 && action == 'minus') {
			newvalue = value - 1;
		} else if (value < 99 && action == 'plus') {
			newvalue = value + 1;
		}
		
		if (value != newvalue) {
			$('#product-qty').text(newvalue);
			
			if ($('#quantity')) {
				$('#quantity').val(newvalue);
			}
		}
	}
	
	return;
}

function UpdateBasketDisplay() {
	
	var products;
	
	if ($('.shop-basket-row')) {
		products = $('.shop-basket-row').length;
	}
	
	if ($('.shop-basket-totalitems') && products == 0) {
		products = parseInt($('.shop-basket-totalitems').text());
	}
		
	// We have products in the shopping cart
	if (products > 0) {	
		
		$('#shopping-cart-wrapper').show();
		$('#shopping-cart-empty').hide();
		
	// We don't have any products in the shopping cart
	} else {
		$('#shopping-cart-wrapper').hide();
		$('#shopping-cart-empty').show();
	}
	
}

function RemoveProductFromBasket(id) {
	
	var data = "id=" + id;
	
	$.ajax({
	  url: 		"/ajax/removefromcart.php",
	  type: 	"POST",
	  dataType:	"json",
	  data: 	data,
	  success: 	function(response) {	  
			  if (response.success == "Y") {
		   	 			    
			    // Remove the basket row.
			    if ($('#shop-basket-' + id)) {
				  $('#shop-basket-' + id).fadeOut("slow", function() {
				  		  
				      $('#shop-basket-' + id).remove();
				      
				      UpdateBasketDisplay();
				      
				      // Set the total price
				      if ($(".shop-basket-totalprice")) {
				        $(".shop-basket-totalprice").html(response.totalprice);
				      }				      
				  });
		   	    }			    
			    
			  } else {
			    alert(response.message);	  
			  }
			  
			}
	});
}

function AddProductToBasket(spinner, gotourl) {
	
	var originalimage;
	if ($('#product-addtobasket img') && spinner) {
		originalimage = $('#product-addtobasket-button').attr("src");
		$('#product-addtobasket-button').attr('src', spinner);
	}
	
	if ($('#basket-options')) {
				
		var productid 	= $('#productid').val();
		var quantity	= $('#quantity').val();
		
		var options = new Array;
		$(":input").each(function(index) {
			if (this.name.match(/^question/)) {
				options.push(this.name + "=" + this.value);
			}
		});
		
		var data = "productid=" + productid + "&quantity=" + quantity + "&" + options.join("&") + "&originalimage=" + originalimage;
		
		$.ajax({
		  url: 		"/ajax/addtocart.php",
		  type: 	"POST",
		  dataType:	"json",
		  data: 	data,
		  success: 	function(response) {
		  	  	  if (response.success == "Y") {
		  	  	   		  	  	    
		  	  	    // Add the new product row to the basket.
		  	  	    $("#shop-basket-header").after(response.producthtml);	  	  	    

				    // Remove the basket row.
				    if ($('#' + response.elementid)) {
					  $('#' + response.elementid).fadeIn("slow");
				    }

			            // Set the total price
			            if ($(".shop-basket-totalprice")) {
				      $(".shop-basket-totalprice").html(response.totalprice);
				    }

				    // Set the total items
			            if ($(".shop-basket-totalitems")) {
				      $(".shop-basket-totalitems").html(response.totalitems);
				    }
				    
				    UpdateBasketDisplay();
				    
				    if ($('#product-addtobasket-button') && response.originalimage) {
				      $('#product-addtobasket-button').attr('src', response.originalimage);
				    }

				    if (gotourl) { window.location = gotourl; }				    
		  	  	    
		  	  	  } else {
		  	  	  	 alert(response.message);
		  	  	  }
		  	  	  
				}
		});	
	}
}

function ChangeProductQuantity(id, value) {

        var qty = parseInt(value);
        if (qty != 'NaN') {

        	if (id > 0 && qty > 0) {
  
			var data = "id=" + id + "&qty=" + qty;
			
			$.ajax({
			  url: 		"/ajax/updatebasketquantity.php",
			  type: 	"POST",
			  dataType:	"json",
			  data: 	data,
			  success: 	function(response) {
					  if (response.success == "Y") {
	
					    // Set the total price
					    if ($(".shop-basket-totalprice")) {
					      $(".shop-basket-totalprice").html(response.totalprice);
					    }
	
					    // Set the total items
					    if ($(".shop-basket-totalitems")) {
					      $(".shop-basket-totalitems").html(response.qty);
					    }
					    
					    // Go through the extras we got back
					    // and update the pricing on these.
					    if (response.products) {
					    	    for (x = 0; x < response.products.length; x++) {
					    	    	    var product = response.products[x];
					    	    	    if ($('#productprice-' + product.id)) {
					    	    	    	    $('#productprice-' + product.id).html(product.grossprice);
					    	    	    }
					    	    }
					    }					    
					    
					    // Go through the extras we got back
					    // and update the pricing on these.
					    if (response.extras) {
					    	    for (x = 0; x < response.extras.length; x++) {
					    	    	    var extra = response.extras[x];
					    	    	    if ($('#extraprice-' + extra.id)) {
					    	    	    	    $('#extraprice-' + extra.id).html(extra.grossprice);
					    	    	    }
					    	    }
					    }
					    
					  } else {
						 alert(response.message);
					  }
					  
					}
			});        		
        		
        		
        	}
        }
	
	return;
}

function UpdateBasketPrice(field) {

  (function($) {
	$.ajax({
	  url: 		"/ajax/calculatebasketprice.php",
	  type: 	"POST",
	  dataType:	"json",
	  data: 	"vouchercode=" + $('#vouchercode').val(),
	  success: 	function(response) {	
	  	  		if (response.success == "Y") {
	  	  			Redirect('/shop/basket/');
	  	  		}
	  		}
	});
  })(jQuery);	
}

function showshippingoptions() {
  (function($) {
     if ($('#diffshippingaddress-1').attr('checked') != true) {
       $('#shippingoptions-wrap').slideUp('slow',  function() { $('#header-shippingoptions').hide(); });
     } else {
       $('#header-shippingoptions').show();
       $('#shippingoptions-wrap').slideDown('slow');
     }
  })(jQuery);
}

