function getOptions(productID, attributes, attributeChanged, block, hidePrivate, successFunction) {
	if (block != null && block) {
		$('#options').block({ message: "loading options..." });
	}
	var data = "";
	data += attributes == null ? "" : "attributes=" + attributes + "&";
	data += attributeChanged == null ? "" : "attributeChanged=" + attributeChanged + "&";
	data += productID == null ? "" : "productID=" + productID + "&";
	data += hidePrivate == null ? "" : "hidePrivate=" + hidePrivate + "&";
	
	$.ajax({
		url: "/product/options/options.xml",
		type: "POST",
		data: data,
		dataType: "xml",
		success: successFunction != null ? successFunction : function(xml) {
        	$(xml).find('attribute').each(function() {
        		// format XML as HTML
        		var label = $(this).find('label').text();
        		var id = $(this).find('id').text();
        		var html = '<div id="attribute-' + id + '-container" class="attribute-container">';
        		html += '<label for="attribute-' + id + '">' + label + '</label>';
        		html += '<select id="attribute-' + id + '" name="attribute-' + id + '" class="attribute-select" onchange="updateOptions(' + productID + ', this.id, ' + (block != null ? block : '0') + ',  ' + (hidePrivate != null ? hidePrivate : '0') + ')">';
        		var selected = $(this).find('options set').text();
        		$(this).find('options option').each(function() {
        			var optionValue = $(this).find('value').text();
        			var optionDisplay = $(this).find('display').text();
        			var isSelected = selected && selected == optionValue ? ' selected="selected"' : '';
        			html += '<option value="' + optionValue + '"' + isSelected + '>' + optionDisplay + '</option>';
        		});
        		html += '</select></div>';
        		var existing = $('#options').find('#attribute-' + id + '-container');
        		if (existing.length) {
        			existing.replaceWith(html);
        		} else {
        			$('#options').append(html);
        		}
        	});
        	var price = getPriceForProduct(productID);
        	if (price !== false) {
            	$('#options div.price-container').remove();
            	var html = '<div class="price-container"><label for="price-div">Price: </label><div id="price-div">' + price + '</div></div>';
    			$('#options').append(html);
        	}
        	if (block != null && block) {
        		$('#options').unblock();
        	}
		},
		error: function() {
        	if (block != null && block) {
        		$('#options').unblock();
        	}
		}
	});
}

function getPriceForProduct(productID) {
	var priceReturn = "";
	$.ajax({
		url: "/product/options/price",
		type: "POST",
		async: false,
		data: "attributes=" + getAttributes() + "&productID=" + productID,
		success: function(price) {
        	if (price != 'POA') { price = '$' + price; }
        	priceReturn = price;
		},
		error: function() {
		}
	});
	return priceReturn;
}

function getAttributes() {
	var attributes = "";
	$('.attribute-select').each(function() {
		if (attributes.length > 0) {
			attributes += ",";
		}
		attributes += $(this).attr('id').substring(10) + ":";
		attributes += $(this).find('option:selected').val();
	});
	return attributes;
}

function updateOptions(productID, selectID, block, hidePrivate, successFunction) {
	var attributeChanged = selectID.substring(10) + ':' + $('#' + selectID + ' option:selected').val();
	getOptions(productID, getAttributes(), attributeChanged, block, hidePrivate, successFunction);
}

function addToCart(productID, optionID, quantity, clearCart, successFunction, customisations) {
    var data = new Object();
    
    data.productID = productID;
    if (optionID != null) data.optionID = optionID;
    if (quantity != null) data.quantity = quantity;
    if (clearCart != null) data.clearCart = clearCart;
    if (customisations != null) data.customisations = customisations;
	
	successFunction = successFunction != null ? successFunction : defaultAddToCartSuccessFunction;
	
	$.ajax({
		url: "/order/add-to-cart",
		type: "POST",
		data: data,
		success: successFunction,
		error: function(request, textStatus, errorThrown) {
			alert("An error occurred while trying to add the item to the cart");
		}
	});
}

function defaultAddToCartSuccessFunction(msg) {
	if (msg = "success") {
		document.location = document.location;
	} else {
		alert("An error occurred while trying to purchase item");
	}
}

function defaultRemoveFromCartSuccessFunction(msg) {
}

function purchaseProduct(productID, checkoutURL) {
	var data = "productID=" + productID + "&clearCart=true";
	
	$.ajax({
		url: "/order/add-to-cart",
		type: "POST",
		data: data,
		success: function(msg) {
			if (msg = "success") {
				document.location = '/' + checkoutURL;
			} else {
				alert("An error occurred while trying to purchase item");
			}
		},
		error: function(request, textStatus, errorThrown) {
			alert("An error occurred while trying to purchase item");
		}
	});
}

function removeFromCart(key, successFunction) {
	if (key) {
		successFunction = successFunction != null ? successFunction : defaultRemoveFromCartSuccessFunction;
		
		$.ajax({
			url: "/order/remove-from-cart?key=" + key,
			type: "POST",
			success: successFunction,
			error: function(request, textStatus, errorThrown) {
				alert("An error occurred while trying to remove item from cart");
			}
		});
	}
}
