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 menuMembraneOnItemClick(p_sType, p_aArgs, p_oItem) {
	//	SZELLEY: rigs
	if (typeof(p_oItem)=="string" && p_sType == "manual") {
		// Set this button menu label to the selected text
    	buttonMembrane.set("label", "<em><i>Type:</i> " + p_oItem + "</em>");
    	buttonMembrane.set("value", p_oItem);
	} else {
		// Set this button menu label to the selected text
    	buttonMembrane.set("label", "<em><i>Type:</i> " + p_oItem.cfg.getProperty("text") + "</em>");
    	buttonMembrane.set("value", p_oItem.cfg.getProperty("text"));
    }
    
    // Show the check box
    YAHOO.util.Dom.setStyle('buttonContainerMembrane', 'backgroundPosition', '250px 4px');
    
    // Put the other two buttons in their default states
	buttonPoreSize.set("label", "<em>Please select Membrane Pore Size</em>");
	buttonPackSize.set("label", "<em>Please select Pack Size</em>");
	buttonPackSize.set("disabled", true);
	buttonQuantity.set("label", "<em><i>Quantity:</i> 1</em>");
	buttonQuantity.set("value", 1);
	buttonQuantity.set("disabled", true);
	buttonSubmit.set("disabled", true);
	
	// Hide the pore size and pack size checkmarks
    YAHOO.util.Dom.setStyle('buttonContainerPoreSize', 'backgroundPosition', '-250px 0px');
    YAHOO.util.Dom.setStyle('buttonContainerPackSize', 'backgroundPosition', '-250px 0px');
	
	// Return the price box to showing nothing
	document.getElementById('priceContainer').innerHTML = "&nbsp;<br />&nbsp;";
	
	// clear the current menu items
	menuPoreSize.clearContent();
	
	// create a temporary array to store the items for the next drop down, to prevent dupes
	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 (prod_details[row].membrane_type == buttonMembrane.get("value") && !checkArrayforDupe(tempArray, prod_details[row].pore_size)) {
			// add to the temp array
			tempArray[tempArray.length] = prod_details[row].pore_size;
			
			// add to the menu itself
			menuPoreSize.addItems([
				{ text: prod_details[row].pore_size, value: prod_details[row].pore_size, onclick: { fn: menuPoreSizeOnItemClick } }
			]);
		}
	}
	
	// re-render the menu
	menuPoreSize.render(document.body);
	
	// Enable the next menu item
	buttonPoreSize.set("disabled", false);
	
	//	SZELLEY: if there is only one option, mimick click function - open up next menu
	if (tempArray.length == 1){ menuPoreSizeOnItemClick("manual","",tempArray[0]); }
}

function menuPoreSizeOnItemClick(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
    	buttonPoreSize.set("label", "<em><i>Pore Size:</i> " + p_oItem + "</em>");
    	buttonPoreSize.set("value", p_oItem);
	} else {
		// Set this button menu label to the selected text
		buttonPoreSize.set("label", "<em><i>Pore Size:</i> " + p_oItem.cfg.getProperty("text") + "</em>");
		buttonPoreSize.set("value", p_oItem.cfg.getProperty("text"));
	}
	
	// Show the pore size checkmark
    YAHOO.util.Dom.setStyle('buttonContainerPoreSize', 'backgroundPosition', '250px 4px');
	
    // Put the pack size and quantity buttons in t default states
	buttonPackSize.set("label", "<em>Please select Pack Size</em>");
	buttonQuantity.set("label", "<em><i>Quantity:</i> 1</em>");
	buttonQuantity.set("value", 1);
	buttonQuantity.set("disabled", true);
	buttonSubmit.set("disabled", true);
	
	// Hide the pack size checkmark
    YAHOO.util.Dom.setStyle('buttonContainerPackSize', 'backgroundPosition', '-250px 0px');
    
    // Return the price box to showing nothing
	document.getElementById('priceContainer').innerHTML = "&nbsp;<br />&nbsp;";
	
	// clear the current menu items
	menuPackSize.clearContent();
	
	// create a temporary array to store the items for the next drop down, to prevent dupes
	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 (prod_details[row].pore_size == buttonPoreSize.get("value") && prod_details[row].membrane_type == buttonMembrane.get("value") && !checkArrayforDupe(tempArray, prod_details[row].pack_size)) {
			// add to the temp array
			tempArray[tempArray.length] = prod_details[row].pack_size;
			
			// add to the menu itself
			menuPackSize.addItems([
				{ text: prod_details[row].pack_size, value: prod_details[row].pack_size, onclick: { fn: menuPackSizeOnItemClick } }
			]);
		}
	}
	
	// re-render the menu
	menuPackSize.render(document.body);
	
	// Enable the next menu item
	buttonPackSize.set("disabled", false);
	
	//	SZELLEY: if there is only one option, mimick click function - open up next menu
	if (tempArray.length == 1){ menuPackSizeOnItemClick("manual","",tempArray[0]); }
}

function menuPackSizeOnItemClick(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
    	buttonPackSize.set("label", "<em><i>Pack Size:</i> " + p_oItem + "</em>");
    	buttonPackSize.set("value", p_oItem);
	} else {
		// Set this button menu label to the selected text
		buttonPackSize.set("label", "<em><i>Pack Size:</i> " + p_oItem.cfg.getProperty("text") + "</em>");
		buttonPackSize.set("value", p_oItem.cfg.getProperty("text"));
	}
	
	// Show the pack size checkmark
    YAHOO.util.Dom.setStyle('buttonContainerPackSize', 'backgroundPosition', '250px 4px');
	
    // Put the pack size and quantity buttons in t default states
	buttonQuantity.set("label", "<em><i>Quantity:</i> 1</em>");
	buttonQuantity.set("value", 1);
	
	// 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].pack_size == buttonPackSize.get("value") && prod_details[row].pore_size == buttonPoreSize.get("value") && prod_details[row].membrane_type == buttonMembrane.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 != "") {
					bulk_inner_html = "<span style='color: #333366; font-weight: bold;'>Bulk Discounts</span><br />" + 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].pack_size == buttonPackSize.get("value") && prod_details[row].pore_size == buttonPoreSize.get("value") && prod_details[row].membrane_type == buttonMembrane.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].pack_size == buttonPackSize.get("value") && prod_details[row].pore_size == buttonPoreSize.get("value") && prod_details[row].membrane_type == buttonMembrane.get("value")) {
			var lc_prod_id = prod_details[row].lc_prod_id;
			var lc_product_number = prod_details[row].product_number;
		}
	}
	
	// redirect to the cart
	if ((lc_product_number == "VN01H03" || lc_product_number == "VN01H23" || lc_product_number == "VN01H33" || lc_product_number == "VN01H43" || lc_product_number == "VN01H93") && buttonQuantity.get("value") >= 6) {
		document.location = "http://www.vivaproducts.com/store/cart.php?target=category&action=bulk&category_id=20&product_qty[223]=1&product_qty[" + lc_prod_id + "]=" + buttonQuantity.get("value");
	} else {
		document.location = "http://www.vivaproducts.com/store/cart.php?target=cart&action=add&product_id=" + lc_prod_id + "&amount=" + buttonQuantity.get("value");
	}
	
	// URL to add bulk products (category_id must be real)
	// http://www.vivaproducts.com/store/cart.php?target=category&action=bulk&product_qty[224]=2&product_qty[223]=10&category_id=20
	
	// cancel the form submit
	return false;
}

function setDefaultMembraneMenuItems() {
	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].membrane_type)) {
			// add to the temp array
			tempArray[tempArray.length] = prod_details[row].membrane_type;
			
			// add to the menu itself
			menuMembrane.addItems([
				{ text: prod_details[row].membrane_type, value: prod_details[row].membrane_type, onclick: { fn: menuMembraneOnItemClick } }
			]);
		}
	}
	
	if (tempArray.length == 1){ return tempArray[0]; }
}

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

menuPoreSize = new YAHOO.widget.Menu('menuPoreSize');
menuPoreSize.render(document.body);

menuPackSize = new YAHOO.widget.Menu('menuPackSize');
menuPackSize.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 buttonMembrane = new YAHOO.widget.Button({ type: "menu", label: "<em>Please select Membrane Type</em>", title: "Membrane Type", menu: menuMembrane, name: "buttonMembrane", container: "buttonContainerMembrane" });
var buttonPoreSize = new YAHOO.widget.Button({ type: "menu", label: "<em>Please select Membrane Pore Size</em>", title: "Membrane Pore Size", menu: menuPoreSize, name: "buttonPoreSize", container: "buttonContainerPoreSize", disabled: true });
var buttonPackSize = new YAHOO.widget.Button({ type: "menu", label: "<em>Please select Pack Size</em>", title: "Membrane Pack Size", menu: menuPackSize, name: "buttonPackSize", container: "buttonContainerPackSize", disabled: true });
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 membrane has only one option, mimick clicking function
function run_initial_menu(){
	if(typeof(mem_choice) == "string"){
		menuMembraneOnItemClick( "manual" , "" , mem_choice);
	}
}

YAHOO.util.Event.onDOMReady(run_initial_menu);