﻿
// Dx.Store.js
// (c) Webfuel

// Webfusion Store Client Code

Store = {};

(function($) {

	$.extend(Store, {

		renderBasket: function(basket) {
		},

		addToBasket: function(contentId, jumpToBasket) {

			// Get and check the quantity
			var quantity = 1;
			var q_ctrl = document.getElementById("q_" + contentId);
			if(q_ctrl) {
				quantity = parseInt(q_ctrl.value);   // q_ctrl.selectedIndex + 1;
				if (isNaN(quantity) || quantity <= 0) {
					alert("Please enter a valid quantity.");
					return;
				} 
			}

			// Build configuration string

			// Get and check the variations
			var productVariationOptions = "";
			var cancel = false;
			$(".v_" + contentId).each(function(index, element) {
				if (cancel) return;
				if (!element.value || element.value == '') {
					alert("There are options/variations you need to choose from before this product can be added to the order.");
					cancel = true;
					return;
				}
				var value = parseInt(element.value);
				if (!isNaN(value)) {
					productVariationOptions += value + ",";
				}
			});
			if (cancel)
				return;

			// Call back to the server
			$.ajax({
				url: "/WebServices/LowTrust/FusionWebService.asmx/Invoke",
				data: { service: "IStoreBasketService", method: "AddProduct", parameters:
					{
						ContentId: contentId,
						Quantity: quantity,
						Configuration: productVariationOptions
					}
				},
				success: function(result) {
                    if(jumpToBasket) {
					    $.redirectTo('/basket.aspx');
                    } else {
                        // TODO: Update render of the basket
					    $.redirectTo('/basket.aspx');
                    }
				}
			});
		}

	});

	$(".checkoutconfirm").hide();
})(jQuery);


