// Preload Nav Images
var home_ov = new Image();
home_ov.src = '/images/cart/buttons/home_ov.gif';

// Preload Cart Images
var remove_ov = new Image();
remove_ov.src = '/images/cart/buttons/remove_ov.gif';

var checkout_ov = new Image();
checkout_ov.src = '/images/cart/buttons/checkout_ov.gif';

var update_ov = new Image();
update_ov.src = '/images/cart/buttons/update_ov.gif';


var subTotal = 0.00;
var rowId = 0;

function readCookie( name ) 
{
	var nameEQ = name + "=";
	var ca = document.cookie.split( ';' );
	
	for( var i = 0; i < ca.length; i++ ) 
	{
		var c = ca[ i ];
		while ( c.charAt( 0 ) == ' ' ) c = c.substring( 1, c.length );
		if ( c.indexOf( nameEQ ) == 0 ) return c.substring( nameEQ.length, c.length );
	}
	
	return null;
}

function loadCart( merchant )
{
	var sid = readCookie( merchant + "_sid" );
	
	if( sid )
	{
		// cookie exists, load cart contents
		$.post( "/cart/loadCart.php", { sid: sid, m: merchant }, loadCartContents );
	}
}

function loadCartContents( cartContents )
{
	if( cartContents != "" )
	{
		// clear the subtotal
		subTotal = 0.00;
		
		// clear the cart table contents
		$( "table#cart_table tbody" ).empty();
		
		// break up the cart contents
		var contentsArray = cartContents.split( "|" );
		
		// build up the shopping cart
		for( var x = 0; x < contentsArray.length; x++ )
		{
			// break up the cart item
			// itemArray[ 0 ] = productID
			// itemArray[ 1 ] = quantity
			// itemArray[ 2 ] = price
			// itemArray[ 3 ] = description
			// itemArray[ 4 ] = picture
			var itemArray = contentsArray[ x ].split( ":" );

			// calculate the total price
			var price = parseFloat( itemArray[ 2 ] * itemArray[ 1 ] );
			subTotal = subTotal + parseFloat( price );
			
			// build up the row
			var newRow = "<tr id='row" + x + "'>" +
				"<td class='glitter_col'><img src='images/products/" + itemArray[ 4 ] + "' width='25' height='25'></td>" +
				"<td class='item_col'><span class='partNumber'>" + itemArray[ 0 ] + "</span></td>" +
				"<td class='description_col'>" + itemArray[ 3 ] + "</td>" +
				"<td class='quantity_col'><input id='quantity" + x + "' class='txt_quantity' maxlength='5' type='text' value='" + itemArray[ 1 ] + "'></td>" +
				"<td class='price_col'>$<span class='price'>" + price.toFixed( 2 ) + "</span></td>" +
				"<td class='remove_col' align='center'><a href='#' onclick='removeProduct( \"lit\", \"" + x + "\" ); return false;'>" + 
				"<img src='images/cart/buttons/remove.gif' width='15' height='15' border='0' onmouseover='this.src=\"images/cart/buttons/remove_ov.gif\"' onmouseout='this.src=\"images/cart/buttons/remove.gif\"'/></a></td>" +
				"</tr>";

			$( "table#cart_table tbody" ).append( newRow );
		}

		// set the totals
		var gst = subTotal * 0.05;
		var total = subTotal + gst;
		
		$( "span#cart_subtotal" ).html( subTotal.toFixed( 2 ) );
		$( "span#cart_gst" ).html( gst.toFixed( 2 ) );
		$( "span#cart_total" ).html( total.toFixed( 2 ) );
	}
	else
	{
		$( "table#cart_table tbody" ).append( "<tr><td colspan='6'>Your cart is empty</td></tr>" );
	}
}

function addProduct( merchant, productID, quantity )
{
	// add a product to the customer's shopping cart
	$.post( "/product/add.php", { m: merchant, p: productID, q: quantity }, function( data ) {
		addLitKit( productID );
		loadCart( merchant );
	});
}

function removeProduct( merchant, rowIndex )
{
	// get the price and part number of the product
	var price = $( "tr#row" + rowIndex + " td span.price" ).html();
	var partNumber = $( "tr#row" + rowIndex + " td span.partNumber" ).html();
	
	// remove a product from the customer's shopping cart
	$.post( "/product/remove.php", { m: merchant, p: partNumber } );
	
	// remove the row
	$( "table#cart_table tbody tr#row" + rowIndex ).remove();

	// calculate and set the new totals
	subTotal = subTotal.toFixed( 2 ) - parseFloat( price );
	subTotal = Math.round( subTotal * 100 ) / 100;
	var gst = subTotal * 0.06;
	var total = subTotal + gst;
	
	$( "span#cart_subtotal" ).html( subTotal.toFixed( 2 ) );
	$( "span#cart_gst" ).html( gst.toFixed( 2 ) );
	$( "span#cart_total" ).html( total.toFixed( 2 ) );
	
	if( $( "table#cart_table tbody" ).html() == "" )
	{
		$( "table#cart_table tbody" ).append( "<tr><td colspan='6'>Your cart is empty</td></tr>" );
	}
}

function updateCart( merchant )
{
	// get the values of the quantity inputs
	var numElements = document.cartForm.length;
	
	if( numElements > 0 )
	{
		var quantityArray = new Array();
	
		for( var x = 0; x < numElements; x++ )
		{
			var element = document.cartForm.elements[ x ];
			quantityArray[ x ] = element.value;
		}
	
		var quantityString = quantityArray.join( "|" );
	
		// update the quantities in the db
		$.post( "/cart/updateCart.php", { m: merchant, q: quantityString }, 
			function() {
				// clear the cart form
				$( "table#cart_table tbody" ).empty();
			
				// reload the cart
				loadCart( merchant );
			}
		);
	}
	else
	{
		$( "table#cart_table tbody" ).empty();
		loadCart( merchant );
	}
}

function submitCart( merchant )
{
	if( subTotal != 0.00 )
	{
		var sid = readCookie( merchant + "_sid" );

		if( sid )
		{
			// remove a product from the customer's shopping cart
			$.post( "/cart/checkCart.php", { sid: sid, m: merchant },
				function( data ) {
					if( data == "DONE" )
						window.location = "shipping.html";
					else
						alert( data );
				}
			);
		}
	}
}

function addLitKit( productID )
{
	switch( productID )
	{
		case "MK-01":
			alert( "You have selected the MiniMe LIT Kit.  Please select your 1 glitter colour now." );
			break;
		case "SK-01":
			alert( "You have selected the Sample Kit.  Please select your 1 glitter colour now." );
			break;
		case "LK3-01":
			alert( "You have selected the 3 Stack LIT Kit.  Please select your 3 glitter colours now." );
			break;
		case "LK4-01":
			alert( "You have selected the 4 Stack LIT Kit.  Please select your 4 glitter colours now." );
			break;
		case "LK5-01":
			alert( "You have selected the 5 Stack LIT Kit.  Please select your 5 glitter colours now." );
			break;
	}
}