function addToCart(productId, path, refreshPath) {
	if(!$("#quantity-"+productId).val()) {
		alert('Please select a quantity before adding to cart.');
		return false;
	}
	else if(!$("#language-"+productId+" input:checked").val()) {
		alert('Please select a language before adding to cart.');
		return false;
	}
	else {
		$.ajaxSetup ({
    		// Disable caching of AJAX responses */
    		cache: false
		});
		$.get(path+'/'+productId+'/'+$("#quantity-"+productId).val()+'/'+$("#language-"+productId+" input:checked").val());
		$('#cart-products').html('<p class="loading">Refreshing...</p>');
		$.ajax({
			type: 'GET',
			cache: false,
			url: refreshPath+'/'+$("#category_id").val(),
			success: function(data) {
				$('#cart-products').load(refreshPath+'/'+$("#category_id").val()+'/'+(new Date()).getTime())+'/'+Math.random();
				alert("This product has been added to the cart.");
			}
		});
		return false;
	}
}

function addCopyToCart(productId, path, refreshPath) {
	// Validate all the fields (might need to take some out if not required 
	if(!$("#copy-"+productId+"-description").val()) {
		alert('Please enter a description for the original file.');
		return false;
	}
	else if(!$("input[name='copy-"+productId+"-contains']:checked").val() || $("input[name='copy-"+productId+"-sided']:checked").val() == 'undefined') {
		alert('Please select what the original file contains.');
		return false;
	}
	else if(!$("#copy-"+productId+"-num_copies").val()) {
		alert('Please enter the number of copies for each original file.');
		return false;
	}
	else {
		$.ajaxSetup ({
    		// Disable caching of AJAX responses */
    		cache: false
		});
		// Building the ajax path so we can add this copy to
		// the list of products in the cart
		path += '/'+productId;
		path += '/'+stripHashes($("#copy-"+productId+"-description").val());
		path += '/'+stripHashes($("input[name='copy-"+productId+"-contains']:checked").val());
		path += '/'+stripHashes($("input[name='copy-"+productId+"-same']:checked").val());
		path += '/'+stripHashes($("#copy-"+productId+"-reduce_from").val());
		path += '/'+stripHashes($("#copy-"+productId+"-reduce_to").val());
		path += '/'+stripHashes($("#copy-"+productId+"-magnify_from").val());
		path += '/'+stripHashes($("#copy-"+productId+"-magnify_to").val());
		path += '/'+stripHashes($("#copy-"+productId+"-num_copies").val());
		path += '/'+stripHashes($("input[name='copy-"+productId+"-sided']:checked").val());
		path += '/'+stripHashes($("input[name='copy-"+productId+"-paper_stock']:checked").val());
		path += '/'+stripHashes($("#copy-"+productId+"-paper_colour").val());
		path += '/'+stripHashes($("input[name='copy-"+productId+"-cover_stock']:checked").val());
		path += '/'+stripHashes($("#copy-"+productId+"-cover_colour").val());
		// Either its a selectable tab or a custom one
		var tabs = ($("input[name='copy-"+productId+"-tabs']:checked").val() != "Custom") ? stripHashes($("input[name='copy-"+productId+"-tabs']:checked").val()) : stripHashes($("#copy-"+productId+"-tabs_other").val());
		path += '/'+tabs;
		path += '/'+stripHashes($("input[name='copy-"+productId+"-binding']:checked").val());
		path += '/'+stripHashes($("#copy-"+productId+"-binding_colour").val());
		path += '/'+stripHashes($("input[name='copy-"+productId+"-binding_protective']:checked").val());
		path += '/'+stripHashes(escape($("#copy-"+productId+"-notes").val()));
		// Add random to further foil cache
		path += '/'+Math.random();
		// Make a GET ajax call to the path to add the new product 
		$.get(path);
		$('#cart-products').html('<p class="loading">Refreshing...</p>');
		$.ajax({
			type: 'GET',
			cache: false,
			url: refreshPath+'/'+$("#category_id").val(),
			success: function(data) {
				$('#cart-products').load(refreshPath+'/'+$("#category_id").val()+'/'+(new Date()).getTime())+'/'+Math.random();
				alert("This product has been added to the cart.");
				$("#reset-form").click();
				$("#copy-7-description").focus();
			}
		});
		return false;
	}
}

function removeFromCart(productId, language, path, refreshPath) {
	$.ajaxSetup ({
    	// Disable caching of AJAX responses */
    	cache: false
	});
	$.get(path+'/'+productId+'/'+language);
	$('#cart-products').html('<p class="loading">Refreshing...</p>');
	$.ajax({
		type: 'GET',
		cache: false,
		url: refreshPath+'/'+$("#category_id").val(),
		success: function(data) {
			$('#cart-products').load(refreshPath+'/'+$("#category_id").val()+'/'+(new Date()).getTime())+'/'+Math.random();
			alert("This product has been removed from the cart.");
		}
	});
	//$('#cart-products').load(refreshPath+'/'+$("#category_id").val()+'/'+(new Date()).getTime())+'/'+Math.random;
	return false;
}

function stripHashes(str) {
	if(!str) return 'Not Applicable';
	return str.replace('#', '');
}
