var prod_details = [
	{"product_number":"MSA225S-000-DU","choice1":"None","price":9450,"lc_prod_id":"242"},
	{"product_number":"MSA225S-000-DUIB","choice1":"Bluetooth (+ $210.00)","price":9660,"lc_prod_id":"243"}
];

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 menuChoice1OnItemClick(p_sType, p_aArgs, p_oItem) {
	if (typeof(p_oItem)=="string" && p_sType == "manual") {
		// Set this button menu label to the selected text
    	buttonChoice1.set("label", "<em><i>Wireless Interface:</i> " + p_oItem + "</em>");
    	buttonChoice1.set("value", p_oItem);
	} else {
		// Set this button menu label to the selected text
    	buttonChoice1.set("label", "<em><i>Wireless Interface:</i> " + p_oItem.cfg.getProperty("text") + "</em>");
    	buttonChoice1.set("value", p_oItem.cfg.getProperty("text"));
    }
    
    // Show the check box
    YAHOO.util.Dom.setStyle('buttonContainerChoice1', 'backgroundPosition', '250px 4px');
    
    // Put the other buttons in their default states
	buttonChoice2.set("label", "<em>Please select Printer</em>");
	buttonQuantity.set("label", "<em><i>Quantity:</i> 1</em>");
	buttonQuantity.set("value", 1);
	buttonQuantity.set("disabled", true);
	buttonSubmit.set("disabled", true);
	
	// Hide the choice 2 checkmark
    YAHOO.util.Dom.setStyle('buttonContainerChoice2', 'backgroundPosition', '-250px 0px');
    
	// Add menu items for second choice
	menuChoice2.clearContent();
	if (buttonChoice1.get("value") == "None") {
		menuChoice2.addItems([
			{ text: 'None', value: 0, onclick: { fn: menuChoice2OnItemClick } },
			{ text: 'Printer (+ $720.00)', value: 1, onclick: { fn: menuChoice2OnItemClick } }
		]);
	} else {
		menuChoice2.addItems([
			{ text: 'None', value: 0, onclick: { fn: menuChoice2OnItemClick } },
			{ text: 'Printer with Bluetooth (+ $840.00)', value: 2, onclick: { fn: menuChoice2OnItemClick } }
		]);
	}
	menuChoice2.render(document.body);
	
	// 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].choice1 == buttonChoice1.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
	buttonChoice2.set("disabled", false);
}

// "click" event handler for each item in the Button's menu
function menuChoice2OnItemClick(p_sType, p_aArgs, p_oItem) {
	if (typeof(p_oItem)=="string" && p_sType == "manual") {
		// Set this button menu label to the selected text
    	buttonChoice2.set("label", "<em><i>Printer:</i> " + p_oItem + "</em>");
    	buttonChoice2.set("value", p_oItem);
	} else {
		// Set this button menu label to the selected text
    	buttonChoice2.set("label", "<em><i>Printer:</i> " + p_oItem.cfg.getProperty("text") + "</em>");
    	buttonChoice2.set("value", p_oItem.cfg.getProperty("text"));
    }
    
    // Show the check box
    YAHOO.util.Dom.setStyle('buttonContainerChoice2', '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].choice1 == buttonChoice1.get("value")) {
			
			// Set the price and SKU information (keep in mind there could be a printer)
			if (buttonChoice2.get("value") == 'Printer (+ $720.00)') {
				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 + 720)) + "</span></p>\n";
			} else if (buttonChoice2.get("value") == 'Printer with Bluetooth (+ $840.00)') {
				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 + 840)) + "</span></p>\n";
			} else {
				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].choice1 == buttonChoice1.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
			if (buttonChoice2.get("value") == 'Printer (+ $720.00)') {
				var price_full = (prod_details[row].price + 720) * buttonQuantity.get("value");
			} else if (buttonChoice2.get("value") == 'Printer with Bluetooth (+ $840.00)') {
				var price_full = (prod_details[row].price + 840) * buttonQuantity.get("value");
			} else {
				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].choice1 == buttonChoice1.get("value")) {
			var lc_prod_id = prod_details[row].lc_prod_id;
		}
	}
	
	if (buttonChoice2.get("value") == 'Printer (+ $720.00)') {
		document.location = "http://www.vivaproducts.com/store/cart.php?target=category&action=bulk&category_id=21&product_qty[245]=" + buttonQuantity.get("value") + "&product_qty[" + lc_prod_id + "]=" + buttonQuantity.get("value");
	} else if (buttonChoice2.get("value") == 'Printer with Bluetooth (+ $840.00)') {
		document.location = "http://www.vivaproducts.com/store/cart.php?target=category&action=bulk&category_id=21&product_qty[244]=" + buttonQuantity.get("value") + "&product_qty[" + lc_prod_id + "]=" + buttonQuantity.get("value");
	} else {
		// 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 setDefaultChoice1MenuItems() {
	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].choice1)) {
			// add to the temp array
			tempArray[tempArray.length] = prod_details[row].choice1;
			
			// add to the menu itself
			menuChoice1.addItems([
				{ text: prod_details[row].choice1, value: prod_details[row].choice1, onclick: { fn: menuChoice1OnItemClick } }
			]);
		}
	}
	
	if (tempArray.length == 1){ return tempArray[0]; }
}

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

menuChoice2 = new YAHOO.widget.Menu('menuChoice2');

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 buttonChoice1 = new YAHOO.widget.Button({ type: "menu", label: "<em>Please select Wireless Interface</em>", title: "Wireless Interface", menu: menuChoice1, name: "buttonChoice1", container: "buttonContainerChoice1" });
var buttonChoice2 = new YAHOO.widget.Button({ type: "menu", label: "<em>Please select Printer</em>", title: "Printer", menu: menuChoice2, name: "buttonChoice2", container: "buttonContainerChoice2", 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 Choice1 has only one option, mimick clicking function
function run_initial_menu(){
	if(typeof(mem_choice) == "string"){
		menuChoice1OnItemClick( "manual" , "" , mem_choice);
	}
}

YAHOO.util.Event.onDOMReady(run_initial_menu);