
	var max_pokens 		= 0;
	var total_pokens 	= 0;
	var poken_pack 		= "";

	function calculate_chosen_pokens()
	{
		if ($("input[name=poken_assortment]:checked").val() == 'random')
		{
			$("div.ProductDetailsVariations input").val("Random assortment");
		}
		else
		{
			total_pokens = 0;
			poken_pack = "";
				
			$("select.poken-ct option:selected").each(
				function()
				{
					total_pokens += parseInt($(this).val());
					if (parseInt($(this).val()) != 0)
					{
						poken_pack += $(this).parent().attr("name").replace(/_/g, " ").replace("ct", "poken") + " (x" + parseInt($(this).val()) + "), ";
					}
				}
			);
			new_res = poken_pack.substr(0, poken_pack.length-2);
			$("#remaining_pokens").text(max_pokens - total_pokens);
			$("div.ProductDetailsVariations input").val(new_res);	
		}
	}
	
	$(document).ready(function()
	{
		// change the gift packaging to "none"
		$("select#ProductVariations_variantGroup1 option[varname=None]").attr("selected", "selected");
		max_pokens = $("span#max_pokens").text();
		//$("div.ProductDetailsVariations input").val("Random assortment");
		$("#btnAddToCart").click(
			function()
			{
		
				if ($("input[name=poken_assortment]:checked").val() == 'userchosen')
				{
					if (total_pokens == max_pokens)
					{
						// a. if the total of all inputs is equal to the page total, then we have a success and we process like a regular cart
						//alert("Total pokens: " + total_pokens);
						return;
					}
					else if (total_pokens < max_pokens)
					{
						// b. haven't chosen enough pokens yet
						alert("Please choose more pokens, you have " + (max_pokens - total_pokens) + " left to choose");
						return false;
					}
					else if (total_pokens > max_pokens) 
					{ 
						alert("Please choose fewer pokens, you have chosen " + Math.abs(max_pokens - total_pokens) + " too many");
						return false;
					}
				}
			}
		),
		$(".poken-ct").change(
			function()
			{
				calculate_chosen_pokens()
			}
		),
		$("input[name=poken_assortment]").change(
			function()
			{
				if ($("input[name=poken_assortment]:checked").val() == 'random')
				{
					$("#poken_counts").css("display", "none");
				}
    			else
    			{
	    			$("#poken_counts").css("display", "table");
    			}
    			calculate_chosen_pokens()
			}
		)
	});