// preload images
var remove_ov = new Image();
remove_ov.src = "/bc_express/images/buttons/remove_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( "/bc_express/cart/loadCart.php", { sid: sid, m: merchant }, loadCartContents );
	}
}

function loadCartContents( cartContents )
{
	if( cartContents != "" )
	{
		// clear the subtotal
		subTotal = 0.00;
		
		// 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
			// itemArray[ 5 ] = colour
			// itemArray[ 6 ] = size
			// itemArray[ 7 ] = category
			// itemArray[ 8 ] = subcategory
			var itemArray = contentsArray[ x ].split( ":" );

			// calculate the total price
			var price = parseFloat( itemArray[ 2 ] * itemArray[ 1 ] );
			subTotal = subTotal + parseFloat( price );
			
			var newRow = "<div id='item" + x + "' class='cart_item'>" + 
				"<table cellpadding='0' cellspacing='0' border='0' class='cart_table'>" + 
				"<tr>" +
				"<td class='pic_col' rowspan='2'><img src='/bc_express/images/products/" + itemArray[ 7 ] + 
				"/" + itemArray[ 8 ] + "/" + itemArray[ 4 ] + "_th.jpg'" + 
				" width='53' height='30'></td>" +  
				"<td class='part_col'>Item #</td>" + 
				"<td class='desc_col'>Name</td>" +
				"<td class='size_col'>Size</td>" +
				"<td class='colour_col'>Colour</td>" +
				"<td class='quantity_col'>Quantity</td>" +
				"<td class='price_col'>Price</td>" + 
				"<td class='remove_col' rowspan='2'><a href='#' onclick='removeProduct( \"bm\", \"" + x + "\" ); return false;'>" + 
				" <img src='/bc_express/images/buttons/remove.gif' width='40' height='19' border='0'" +
				" onmouseover='this.src=\"/bc_express/images/buttons/remove_ov.gif\";' " + 
				" onmouseout='this.src=\"/bc_express/images/buttons/remove.gif\";'></a>" + 
				"</td>" +
				"</tr>" + 
				"<tr id='row" + x + "'>" +
				"<td><span class='partNumber'>" + itemArray[ 0 ] + "</span></td>" +
				"<td>" + itemArray[ 3 ] + "</td>" +
				"<td>" + itemArray[ 6 ] + "</td>" +
				"<td>" + itemArray[ 5 ] + "</td>" +
				"<td><input id='quantity" + x + "' class='txt_quantity' maxlength='5' type='text' value='" + itemArray[ 1 ] + "'></td>" +
				"<td>$<span class='price'>" + price.toFixed( 2 ) + "</span></td>" +
				"</tr>" + 
				"</table>" + 
				"</div>";
			
			$( "form#cart_form" ).append( newRow );
		}

		// set the totals
		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 ) );
	}
	else
	{
		$( "form#cart_form" ).append( "<h2>Your cart is empty</h2>" );
	}
}

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( "/bc_express/product/remove.php", { m: merchant, p: partNumber } );
	
	// remove the row
	$( "form#cart_form div#item" + 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( $( "form#cart_form" ).html() == "" )
	{
		$( "form#cart_form" ).append( "<h2>Your cart is empty</h2>" );
	}
}

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

function submitCart()
{
	if( subTotal != 0.00 )
	{
		window.location = "shipping.php";
	}
}

// PRODUCT DETAIL FUNCTIONS
function addProduct( merchant, productID, quantity )
{
	// check to make sure that a quantity was inputted
	if( quantity.match( "^([1-9]|[1-9]\d|100)$" ) )
	{
		// add a product to the customer's shopping cart
		$.post( "/bc_express/product/add.php", { m: merchant, p: productID, q: quantity }, 
			function(){ loadSideCart( merchant ); tb_remove(); } );
	}
	else
	{
		alert( "Please enter a valid quantity." );
	}
}

function getColours( merchant, name, manufacturer, size )
{
	// hide the quantity input field
	hideQuantityInput();
	
	// clear the productID input value
	$( "input#productID" ).val( "" );
	
	// reset the highlights
	$( "a.size" ).css( "border-color", "#666666" ).css( "color", "#666666" );
	
	// highlight the chosen size
	$( "a#size_" + size ).css( "border-color", "#CC0000" ).css( "color", "#CC0000" );
	
	// add a product to the customer's shopping cart
	$.post( "/bc_express/product/getColours.php", { m: merchant, n: name, man: manufacturer, s: size }, showColourSelector );
}

function showColourSelector( colourString )
{
	var colourArray = colourString.split( "|" );
	var selectorString = "<h2>2. CHOOSE YOUR COLOUR</h2><p>";
	
	for( var x = 0; x < colourArray.length; x++ )
	{
		// break up the colour array item
		// 0 = productID
		// 1 = colour
		var colourItem = colourArray[ x ].split( ";" );
		
		selectorString = selectorString + "<a href='javascript:void(0);' class='colour' id='colour_" + 
			colourItem[ 1 ] + "' onclick='showQuantityInput(\"" + colourItem[ 0 ] + "\", \"" + colourItem[ 1 ] + "\");'>" + colourItem[ 1 ].toUpperCase() + "</a> ";
	}
	
	selectorString = selectorString + "</p>";
	
	$( "div#choose_colour" ).html( selectorString );
	$( "div#choose_colour" ).css( "display", "block" );
}

function showQuantityInput( productID, colour )
{
	// reset the highlights
	$( "a.colour" ).css( "border-color", "#666666" ).css( "color", "#666666" );
	
	// highlight the chosen size
	$( "a#colour_" + colour ).css( "border-color", "#CC0000" ).css( "color", "#CC0000" );
	
	$( "div#enter_quantity" ).css( "display", "block" );
	
	// put the productID into the input value
	$( "input#productID" ).val( productID );
}

function hideQuantityInput()
{
	$( "div#enter_quantity" ).css( "display", "none" );
}

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

function loadSideCartContents( cartContents )
{
	if( cartContents != "" )
	{
		// clear the subtotal
		subTotal = 0.00;
		
		// clear the existing side cart table content
		$( "table#side_cart_table" ).empty();
		
		// break up the cart contents
		var contentsArray = cartContents.split( "|" );
		
		// set the number of cart items
		var numItemsString = contentsArray.length + " item";
		
		if( contentsArray.length > 1 )
			numItemsString = numItemsString + "s";
		
		$( "span#num_items" ).html( numItemsString );
		
		// 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
			// itemArray[ 5 ] = colour
			// itemArray[ 6 ] = size
			var itemArray = contentsArray[ x ].split( ":" );

			// calculate the total price
			var price = parseFloat( itemArray[ 2 ] * itemArray[ 1 ] );
			subTotal = subTotal + parseFloat( price );
			
			var newRow = "<tr>" +
				"<td><strong>" + itemArray[ 3 ] + "</strong><br />" + 
				"Quantity [" + itemArray[ 1 ] + "]&nbsp;&nbsp;Price $" + price + "</td>" +
				"</tr>";
			
			$( "table#side_cart_table" ).append( newRow );
		}

		// set the totals
		$( "span#subtotal" ).html( subTotal.toFixed( 2 ) );
	}
	else
	{
		$( "form#cart_form" ).append( "<h2>Your cart is empty</h2>" );
	}
}