// preload accessories buttons
var select_coffee = new Image();
select_coffee.src = "/images/common/cart/select_coffee.gif";

var select_products = new Image();
select_products.src = "/images/common/cart/select_products.gif";


var total = 0.00;
var rowId = 0;

function getProduct()
{
	// get the values of the drop-downs
	var productID = $( "select#selectCoffee" ).val();
	var quantity = $( "select#selectQuantity" ).val();
	var frequency = $( "select#selectFrequency" ).val();

	if( ( productID != "" ) && ( quantity != "" ) && ( frequency != "" ) )
	{
		// get the product data
		$.get( "/include/getProduct.php", { product: productID }, addRow );
	}
}

function addRow( productData )
{
	// check the product data
	if( productData != "0.00" )
	{
		// break up the product data
		var productDataArray = productData.split( "|" );
		var productID = productDataArray[0];
		var productPrice = productDataArray[1];
		var productDesc = productDataArray[2];
		var productPic = productDataArray[3];
		var productCat = productDataArray[4];

		// get the quantity
		var quantity = $( "select#selectQuantity" ).val();
		var price = parseFloat( productPrice * quantity );
	
		// calculate the total price
		total = total + parseFloat( price );
		
		// get the frequency
		var frequency = $( "select#selectFrequency" ).val();

		// build up the row
		var newRow = "<tr id='row" + rowId + "'>" +
			"<td class='label-col' valign='top'><img src='images/products/" + productPic + "' width='40' height='50'></td>" +
			"<td class='coffee-col' valign='top'><h4>" + productCat.toUpperCase() + ":</h4>" + productDesc + "</td>" +
			"<td class='quantity-col' valign='top'><h4>QUANTITY:</h4>" + quantity;

		// check if the product is coffee
		if( productCat == "coffee")
			newRow = newRow + " lb.";
			
		newRow = newRow + "</td><td class='frequency-col' valign='top'><h4>DELIVERY:</h4>" + frequency + "</td>" +
			"<td class='price-col' valign='top'><h4>PRICE:</h4>$<span>" + price.toFixed( 2 ) + "</span></td>" +
			"<td class='remove-col' valign='middle'><a href='#' onclick='removeRow(\"" + rowId + "\"); return false;'><img src='images/common/cart/remove.gif' border='0'></a></td>" +
			"</tr>";
		
		// build up the hidden input
		var hiddenInput = "<input type='hidden' name='hiddenRow[]' id='hiddenRow" + rowId + "' value='" + productID + "-" + quantity + "-" 
		+ frequency + "-" + productPrice + "'>";

		// append the new row
		$( "table#cart-table" ).append( newRow );
	
		// append the hidden input
		$( "form#cartForm" ).append( hiddenInput );
	
		// set the new total
		$( "div#total span#total-amount" ).html( total.toFixed( 2 ) );

		// increment the row id
		rowId++;
		
		// reset the drop-downs
		selectAnother();
	}
}

function removeRow( id )
{
	// get the price for the row
	var price = $( "tr#row" + id + " span" ).html();
	
	// calculate and set the new total
	total = total.toFixed( 2 ) - parseFloat( price );
	total = Math.round( total * 100 ) / 100;
	$( "div#total span#total-amount" ).html( total.toFixed( 2 ) );
	
	// remove the row
	$( "table#cart-table tbody tr#row" + id ).remove();
	
	// remove the hidden input
	$( "form#cartForm input#hiddenRow" + id ).remove();
}

function selectAnother()
{
	document.getElementById( 'selectCoffee' ).selectedIndex = 0;
	document.getElementById( 'selectQuantity' ).selectedIndex = 0;
	document.getElementById( 'selectFrequency' ).selectedIndex = 0;
}

function checkTotal()
{
	if( total != 0.00 )
	{
		document.getElementById( "cartForm" ).submit();
	}
}

function selectProducts()
{
	// build the coffee/accessories div
	var accessoriesString = "<h2>SELECT PRODUCT</h2>"
		+ "<p>Select the product you<br />would like to have delivered.</p>"
		+ "<select name='selectCoffee' id='selectCoffee'>"
		+ "<option value=''> -- Select -- </option>"
		+ "<optgroup label='Chemex Coffeemakers'>"
		+ "<option value='21'>Chemex One to Three Cup</option>"
		+ "<option value='22'>Chemex One to Three Filters</option>"
		+ "</optground>"
		+ "<optgroup label='Accessories'>"
		+ "<option value='36'>Mug</option>"
		+ "<option value='29'>Burlap Coffee Bags</option>"
		+ "</optground>"
		+ "</select>";
	
	// rebuild the quantity div
	var quantityString = "<h2>SELECT QUANTITY</h2>"
		+ "<p>Select the quantity.<br /><br /></p>"
		+ "<select name='selectQuantity' id='selectQuantity'>"
		+ "<option value=''> -- Select -- </option>"
		+ "<option value='1'>1</option>"
		+ "<option value='2'>2</option>"
		+ "<option value='3'>3</option>"
		+ "<option value='4'>4</option>"
		+ "<option value='5'>5</option>"
		+ "</select>";
		
	// rebuild the frequency string
	var frequencyString = "<select style='display: none;' name='selectFrequency' id='selectFrequency'>"
		+ "<option value='Once'>Once</option>"
		+ "</select>";
		
	// write out the coffee, quantity and frequency divs
	$( "div#coffee" ).html( accessoriesString );
	$( "div#quantity" ).html( quantityString );
	$( "div#frequency" ).html( frequencyString );
	
	// set up the buttons
	$( "input#selectProducts" ).attr( { src: "images/common/cart/select_coffee.gif", width: "128", height: "35" } );
	
	// set the onclick
	$("input#selectProducts").click( function() { selectCoffee(); return false; } );
}

function selectCoffee()
{
	// build the coffee/accessories div
	var accessoriesString = "<h2>SELECT COFFEE</h2>"
		+ "<p>Select the coffee you would<br />like to have delivered.</p>"
		+ "<select name='selectCoffee' id='selectCoffee'>"
		+ "<option value=''> -- Select -- </option>"
		+ "<optgroup label='Roaster Select'>"
		+ "<option value='38'>El Salvador Finca Santa Sofia</option>"
		+ "<option value='41'>Tanzania Blackburn Estate AA</option>"
		+ "</optground>"
		+ "<optgroup label='Single Origin Coffees'>"
		+ "<option value='18'>Coelho Gold</option>"
		+ "<option value='25'>Colombian Santander</option>"
		+ "<option value='4'>Costa Rican La Amistad</option>"
		+ "<option value='40'>Cuban Serrano</option>"
		+ "<option value='5'>Ethiopian Sidamo</option>"
		+ "<option value='16'>Ethiopian Yirgacheffe</option>"
		+ "<option value='11'>Kenya AA</option>"
		+ "<option value='17'>Sulawesi Toraja</option>"
		+ "<option value='6'>Sumatran Gayoland</option>"
		+ "</optgroup>"
		+ "<optgroup label='Blended Coffees'>"
		+ "<option value='19'>African Serengeti</option>"
		+ "<option value='1'>French Roast</option>"
		+ "<option value='2'>Mokha-Java</option>"
		+ "<option value='20'>West Coast Blend</option>"
		+ "</optgroup>"
		+ "<optgroup label='Decaffeinated Coffee'>"
		+ "<option value='8'>Komodo</option>"
		+ "</optgroup>"
		+ "<optgroup label='Espresso'>"
		+ "<option value='39'>Cuban Espresso</option>"
		+ "<option value='9'>Italian Espresso</option>"
		+ "<option value='7'>Sunrise Espresso</option>"
		+ "<option value='3'>Sunset Espresso</option>"
		+ "</optgroup>"
		+ "<optgroup label='Caf&eacute; Femenino'>"
		+ "<option value='12'>Caf&eacute; Femenino Peruvian</option>"
		+ "<option value='13'>Caf&eacute; Femenino Mexican</option>"
		+ "</optgroup>"
		+ "</select>";
	
	// rebuild the quantity div
	var quantityString = "<h2>SELECT QUANTITY</h2>"
		+ "<p>Select the quantity of coffee.<br /><br /></p>"
		+ "<select name='selectQuantity' id='selectQuantity'>"
		+ "<option value=''> -- Select -- </option>"
		+ "<option value='1'>1 lb.</option>"
		+ "<option value='2'>2 lb.</option>"
		+ "<option value='3'>3 lb.</option>"
		+ "<option value='4'>4 lb.</option>"
		+ "<option value='5'>5 lb.</option>"
		+ "</select>";
		
	// rebuild the frequency string
	var frequencyString = "<h2>FREQUENCY OF DELIVERY</h2>"
		+ "<p>How often would you like to receive the selected coffee?</p>"
		+ "<select name='selectFrequency' id='selectFrequency'>"
		+ "<option value=''> -- Select -- </option>"
		+ "<option value='Once'>Once</option>"
		+ "<option value='Weekly'>Weekly</option>"
		+ "<option value='Biweekly'>Biweekly</option>"
		+ "<option value='Monthly'>Monthly</option>"
		+ "</select>";
		
	// write out the coffee, quantity and frequency divs
	$( "div#coffee" ).html( accessoriesString );
	$( "div#quantity" ).html( quantityString );
	$( "div#frequency" ).html( frequencyString );
	
	// set up the buttons
	$( "input#selectProducts" ).attr( { src: "images/common/cart/select_products.gif", width: "146", height: "35" } );
	
	// set the onclick
	$("input#selectProducts").click( function() { selectProducts(); return false; } );
}