var prod_details = [
	{"product_number":"VIC-511","price":"200.00","lc_prod_id":"239"}
];

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;
}

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) {
		// 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) {
		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;
}

//  Create the menu instances, along with their default menu items, if any
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 buttonQuantity = new YAHOO.widget.Button({ type: "menu", label: "<em><i>Quantity:</i> 1</em>", title: "Quantity", menu: menuQuantity, name: "buttonQuantity", container: "buttonContainerQuantity", disabled: false, value: 1 });

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