var prod_details = [
	{"product_number":"61316100F05M1A","tank_size":"100L tank with all consumables","price":"5637.00","lc_prod_id":"230"},
	{"product_number":"61316070F05M1A","tank_size":"70L tank with all consumables","price":"5007.00","lc_prod_id":"231"},
	{"product_number":"61316050F05M1A","tank_size":"50L tank with all consumables","price":"4959.00","lc_prod_id":"232"},
	{"product_number":"61316030F05M1A","tank_size":"30L tank with all consumables","price":"4911.00","lc_prod_id":"233"},
	{"product_number":"61316","tank_size":"No tank or consumables","price":"2980.00","lc_prod_id":"241"}
];

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	
	if(cents<10)
		cents = "0" + cents;
	
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
	
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function checkArrayforDupe(array, item) {
	for (i = 0; i < array.length; ++ i) {
		if (array[i] == item) {
			return true;
		}
	}
	
	return false;
}

// "click" event handler for each item in the Button's menu
function menuTankOnItemClick(p_sType, p_aArgs, p_oItem) {
	if (typeof(p_oItem)=="string" && p_sType == "manual") {
		// Set this button menu label to the selected text
    	buttonTank.set("label", "<em><i>Tank Size:</i> " + p_oItem + "</em>");
    	buttonTank.set("value", p_oItem);
	} else {
		// Set this button menu label to the selected text
    	buttonTank.set("label", "<em><i>Tank Size:</i> " + p_oItem.cfg.getProperty("text") + "</em>");
    	buttonTank.set("value", p_oItem.cfg.getProperty("text"));
    }
    
    // Show the check box
    YAHOO.util.Dom.setStyle('buttonContainerTank', 'backgroundPosition', '250px 4px');
    
    // Put the other buttons in their default states
	buttonQuantity.set("label", "<em><i>Quantity:</i> 1</em>");
	buttonQuantity.set("value", 1);
	buttonQuantity.set("disabled", true);
	buttonSubmit.set("disabled", true);
	
	// Return the price box to showing nothing
	document.getElementById('priceContainer').innerHTML = "&nbsp;<br />&nbsp;";
	
	// Set the bulk discount information to their default states
	var bulk_inner_html = "";
	if (document.getElementById('bulkDiscountInfo')) document.getElementById('bulkDiscountInfo').style.display = "none";
	if (document.getElementById('bulkDiscountInfo')) document.getElementById('bulkDiscountInfo').innerHTML = "";
	
	// cycle through the array of SKUs and find out which ones match
	for (row = 0; row < prod_details.length; ++ row) {
		if (prod_details[row].tank_size == buttonTank.get("value")) {
			// Set the price and SKU information
			document.getElementById('priceContainer').innerHTML = "<span style='font-weight: normal;'>" + prod_details[row].product_number + "</span>\n<p><span style='color: red; font-size: 116%;'>" + formatCurrency(prod_details[row].price) + "</span></p>\n";
			
			// Check if there is a bulk discount available
			if (typeof prod_bulk_discount != 'undefined') {
				// Cycle through all bulk discounts, see if there is a SKU match
				for (row2 = 0; row2 < prod_bulk_discount.length; ++ row2) {
					if (prod_details[row].product_number == prod_bulk_discount[row2].product_number) {
						bulk_inner_html = bulk_inner_html + "* Buy " + prod_bulk_discount[row2].bulk_qty + " for <strong>$" + prod_bulk_discount[row2].bulk_price + "</strong> each and <strong><em>save " + prod_bulk_discount[row2].bulk_save + "%</em></strong><br />\n";
					}
				}
				
				// If we found a bulk discount, then display it
				if (bulk_inner_html != "") {
					document.getElementById('bulkDiscountInfo').style.display = "block";
					document.getElementById('bulkDiscountInfo').innerHTML = bulk_inner_html;
				} else {
					document.getElementById('bulkDiscountInfo').style.display = "none";
				}
			}
		}
	}
	
	// Enable the next menu item
	buttonQuantity.set("disabled", false);
	
	// enable the submit button
	buttonSubmit.set("disabled", false);
}

function menuQuantityOnItemClick(p_sType, p_aArgs, p_oItem) {
	//	SZELLEY: rig for manual running
	if ( typeof(p_oItem)=="string" && p_sType == "manual") {
		// Set this button menu label to the selected text
    	buttonQuantity.set("label", "<em><i>Quantity:</i> " + p_oItem + "</em>");
    	buttonQuantity.set("value", p_oItem);
	} else {
		// Set this button menu label to the selected text
		buttonQuantity.set("label", "<em><i>Quantity:</i> " + p_oItem.cfg.getProperty("text") + "</em>");
		buttonQuantity.set("value", p_oItem.cfg.getProperty("text"));
	}
	
	// Set the discount logic flag to default state
	var use_discount_price = 0;
	
	// cycle through the array of SKUs and find out which ones match
	for (row = 0; row < prod_details.length; ++ row) {
		if (prod_details[row].tank_size == buttonTank.get("value")) {
			// Check if there is a bulk discount available
			if (typeof prod_bulk_discount != 'undefined') {
				// Cycle through all bulk discounts, see if there is a SKU match
				for (row2 = 0; row2 < prod_bulk_discount.length; ++ row2) {
					if (prod_details[row].product_number == prod_bulk_discount[row2].product_number) {
						// Look to see if the selected quantity qualifies for a discount - if it does, use this price instead of the default
						if (parseFloat(buttonQuantity.get("value")) >= parseFloat(prod_bulk_discount[row2].bulk_qty)) {
							use_discount_price = 1;
							var price_discount = prod_bulk_discount[row2].bulk_price * buttonQuantity.get("value");
						}
					}
				}
			}
			
			// Determine the base full price
			var price_full = prod_details[row].price * buttonQuantity.get("value");
			
			// If the discount flag is tripped, use the discount price - otherwise use the base full price
			if (use_discount_price == 1) {
				document.getElementById('priceContainer').innerHTML = "<span style='font-weight: normal;'>" + prod_details[row].product_number + "</span>\n<p><span style='font-size: 93%; font-weight: normal; color: #999999; text-decoration: line-through;'>" + formatCurrency(price_full) + "</span><br />\n<span style='font-size: 108%; color: red;'>" + formatCurrency(price_discount) + "</span></p>\n";
			} else {
				var temp_price = prod_details[row].price * buttonQuantity.get("value");
				
				document.getElementById('priceContainer').innerHTML = "<span style='font-weight: normal;'>" + prod_details[row].product_number + "</span>\n<p><span style='font-size: 108%; color: red;'>" + formatCurrency(price_full) + "</span></p>\n";
			}
			
		}
	}
	
	// enable the submit button
	buttonSubmit.set("disabled", false);
}

function addToCart() {
	// cycle through the array of SKUs and find out which ones match
	for (row = 0; row < prod_details.length; ++ row) {
		if (prod_details[row].tank_size == buttonTank.get("value")) {
			var lc_prod_id = prod_details[row].lc_prod_id;
		}
	}
	
	// redirect to the cart
	document.location = "http://www.vivaproducts.com/store/cart.php?target=cart&action=add&product_id=" + lc_prod_id + "&amount=" + buttonQuantity.get("value");
	
	// cancel the form submit
	return false;
}

function setDefaultTankMenuItems() {
	var tempArray = new Array();
	
	// cycle through the array of SKUs and find out which ones match
	for (row = 0; row < prod_details.length; ++ row) {
		if (!checkArrayforDupe(tempArray, prod_details[row].tank_size)) {
			// add to the temp array
			tempArray[tempArray.length] = prod_details[row].tank_size;
			
			// add to the menu itself
			menuTank.addItems([
				{ text: prod_details[row].tank_size, value: prod_details[row].tank_size, onclick: { fn: menuTankOnItemClick } }
			]);
		}
	}
	
	if (tempArray.length == 1){ return tempArray[0]; }
}

//  Create the menu instances, along with their default menu items, if any
menuTank = new YAHOO.widget.Menu('menuTank');
mem_choice	 = setDefaultTankMenuItems();
menuTank.render(document.body);

menuQuantity = new YAHOO.widget.Menu('menuQuantity');
menuQuantity.addItems([
	{ text: '1', value: 1, onclick: { fn: menuQuantityOnItemClick } },
	{ text: '2', value: 2, onclick: { fn: menuQuantityOnItemClick } },
	{ text: '3', value: 3, onclick: { fn: menuQuantityOnItemClick } },
	{ text: '4', value: 4, onclick: { fn: menuQuantityOnItemClick } },
	{ text: '5', value: 5, onclick: { fn: menuQuantityOnItemClick } },
	{ text: '6', value: 6, onclick: { fn: menuQuantityOnItemClick } },
	{ text: '7', value: 7, onclick: { fn: menuQuantityOnItemClick } },
	{ text: '8', value: 8, onclick: { fn: menuQuantityOnItemClick } },
	{ text: '9', value: 9, onclick: { fn: menuQuantityOnItemClick } },
	{ text: '10', value: 10, onclick: { fn: menuQuantityOnItemClick } }
]);
menuQuantity.render(document.body);

// Create the button instances
var buttonTank = new YAHOO.widget.Button({ type: "menu", label: "<em>Please select Tank Size</em>", title: "Tank Size", menu: menuTank, name: "buttonTank", container: "buttonContainerTank" });
var buttonQuantity = new YAHOO.widget.Button({ type: "menu", label: "<em><i>Quantity:</i> 1</em>", title: "Quantity", menu: menuQuantity, name: "buttonQuantity", container: "buttonContainerQuantity", disabled: true, value: 1 });

var buttonSubmit = new YAHOO.widget.Button("buttonSubmit", { value: "buttonSubmit", disabled: true, onclick: { fn: addToCart } }); 

//	SZELLEY: after creating menu, if tank has only one option, mimick clicking function
function run_initial_menu(){
	if(typeof(mem_choice) == "string"){
		menuTankOnItemClick( "manual" , "" , mem_choice);
	}
}

YAHOO.util.Event.onDOMReady(run_initial_menu);
