/***************************
* Copyright (c) GrocerySavings.ca
* All Rights Reserved.
*
* Author: Ben Cho
* Website: grocerysavings.ca, localgrocerydeals.com
* CopyRight: 2007
*
* a fifo data structure
****************************/

// Global variables
var var_fifo_buffer
var var_fifo_temp
var var_fifo_sepchar = "~";
var var_fifo_errorstatus
var varBackupBuffer

var domShoppingCart

function saveShoppingCartDom(shoppingCartDom)
{
	domShoppingCart = shoppingCartDom;
}

// Call to initialise system
function fifo_initialise()
{
	var_fifo_buffer="";
	var_fifo_temp="";
	varBackupBuffer="";
}

// Push a new element into the buffer
function fifo_push(newelement)
{
	output = true
	var_fifo_temp = newelement
	if (var_fifo_temp.length == 0)
	{
		fifo_seterror("there is nothing to push!")
		output=false
	} 
	else 
	{
		if (var_fifo_temp.indexOf(var_fifo_sepchar) > 0)
		{
			fifo_geterror("illegal character");
		} 
		else 
		{
			var_fifo_buffer = var_fifo_buffer + var_fifo_temp + var_fifo_sepchar
		}
	}
	return(output)
}

// Pop an element from the buffer
function fifo_popAll()
{
	newlen = 0
	var_fotjeff_temp = ""
	if (var_fifo_buffer.length == 0)
	{
		fifo_geterror("there is nothing to pop!")
	} 
	else 
	{
		if (var_fifo_buffer.indexOf(var_fifo_sepchar) == -1)
		{
			fifo_geterror("the " + var_fifo_sepchar + " character is illegal in the buffer")
		} 
		else 
		{
			var_fifo_temp = var_fifo_buffer.substr(0, (var_fifo_buffer.indexOf(var_fifo_sepchar)))
			newlen = var_fifo_buffer.length - (var_fifo_buffer.indexOf(var_fifo_sepchar))
			var_fifo_buffer = var_fifo_buffer.substr((var_fifo_buffer.indexOf(var_fifo_sepchar) + 1), newlen)
		}
	}
	return(var_fifo_temp)
}

// pop an element specified by the name
function fifo_pop(newelement)
{
	newlen = 0
	var_fotjeff_temp = ""
	var_fifo_temp = ""
	if (var_fifo_buffer.length == 0)
	{
	 fifo_geterror("there is nothing to pop!")
	} 
	else 
	{
		if (var_fifo_buffer.indexOf(var_fifo_sepchar) == -1)
		{
			fifo_geterror("the " + var_fifo_sepchar + " character is illegal in the buffer")
		} 
		else 
		{
			var len = newelement.length
			var t = var_fifo_buffer.indexOf(newelement)		// returns the index of element
			var t1 = var_fifo_buffer.indexOf(var_fifo_sepchar, t);

			if (t != -1)
			{
				var_fifo_temp = var_fifo_buffer.substr(t, t1);
				var_fifo_buffer = var_fifo_buffer.substr(0,t) + var_fifo_buffer.substr(t1+1)
			}
		}
	}
	return(var_fifo_temp)
}



// Read the current buffer
function fifo_readbuffer()
{
	return(var_fifo_buffer)
}

// Set the current buffer
function fifo_writebuffer(newvalue)
{
	var_fifo_buffer=newvalue
}

// Reset the current buffer
function fifo_reset()
{
	var_fifo_buffer=""
}

// Count the number of elements in the buffer
function fifo_countelements()
{
	count=0
	for (i=0; i<var_fifo_buffer.length; i++)
	{
		if (var_fifo_buffer.substr(i,1)==var_fifo_sepchar)
		{
			count=count+1
		}
	}
	return(count)
}

// Set the error status flag
function fifo_seterror(message)
{
	var_fifo_errorstatus=message
}

// Get the error status flag
function fifo_geterror(message)
{
	return(var_fifo_errorstatus)
} 

/* 
*****************************************************************************************************
	Empties the shopping cart
		removes the groceries in the fifo_buffer data structure
		uncheck all the displayed grocery products (products with input type='checkbox')
	After removing all the groceries, render this change 
*****************************************************************************************************
*/
function emptyList()
{
	aForm = document.getElementById("formGrocerySelectionList")

//	formGroceryListElementDomTree = document.getElementsByTagName("formGrocerySelectionList");

	// empty the cookie storing the grocery list
	setCookie('shoppingList', "", 365);

	if (fifo_countelements() == 0)
	{
		alert("Your grocery list is already empty!");
	}
	else
	{
//alert(formGroceryListElementDomTree.length);

		var confirmEmptyGroceryListAnswer = confirm("Are you sure you want to empty your grocery list?");
		if (confirmEmptyGroceryListAnswer)
		{		
			// clear the buffer in the fifo_buffer
			fifo_reset();

			// uncheck all the selected groceries 
			for (x=0; x<aForm.length; x++)
			{
				tempElement = aForm.elements[x];
				tempElement.checked = false;

			}
		}
	}
	render_list();
}

/*	
		for (x=0; x<formGroceryListElementDomTree.length; x++)
		{
			formGroceryListElementDomTree[x].checked = false;
		}
*/	


// for emptying the general grocery shopping cart list 
// a different function is needed b/c we don't need to store list in the cookie, we save it to database
function emptyGeneralGroceryList()
{
	aForm = document.getElementById("formGrocerySelectionList")

	if (fifo_countelements() == 0)
	{
		alert("Your grocery list is already empty.");
	}
	else
	{
		// uncheck all the selected groceries 
		for (x=1; x<aForm.length; x++)
		{
			tempElement = aForm.elements[x];
			tempElement.checked = false;
			// clear the buffer in the fifo_buffer
			fifo_reset();
		}
	}
	render_list();
}

/* 
*****************************************************************************************************
After adding or removing a grocery from the shopping cart, we must render the change out to html
@param	domShoppingCart		the dom on the page that displays the shopping cart
*****************************************************************************************************
*/
function render_list()
{
//alert('render_list()');
	var preserve=fifo_readbuffer();
	var elements=fifo_countelements();

	// displays the stored list on this page
//@@	html="<table class='tableGroceryList' cellspacing=0><tr><th class='ESList' colspan='2'>Grocery List</th></tr>";
	html = "<table class='tableGroceryList' cellspacing=0 width='205px'>";
	if (elements == 0)
	{
		html += "<tr><td>Empty</td></tr>"
	}
	for (i=1; i<=elements; i++)
	{
		var product = fifo_popAll();
		var tempProduct = convertToJavascriptUse(product);

		html += "<tr><td align='left'>" + product + "</td><td><img src='/images/delete.gif' class='cursorPointer' onClick=\"javascript:remove('" + tempProduct + "')\" title='remove' alt='remove' /></td></tr>";
	}
	html = html + "</table>"
	fifo_writebuffer(preserve);

//	domShoppingCart.innerHTML = html;
	document.getElementById("groceryListShoppingCart").innerHTML = html;
}

/* 
*****************************************************************************************************
Determine if the checkbox if checked or no first
	if checked then take out the item from the grocery list
	if not checked then add the item to the grocery list
*****************************************************************************************************
*/
function purchase_product(aProduct)
{
	var productInDom = document.getElementsByName(aProduct);
	aProduct = convertToHTMLUse(aProduct);

	if (productInDom.item(0).checked == true)
	{
		fifo_push(aProduct);
	}
	else
	{
		fifo_pop(aProduct);
	}
   render_list();
}

// remove this item from the grocery list
// a form this name must exist "formGrocerySelectionList"
function remove(thisProduct)
{
	var aForm = document.getElementById("formGrocerySelectionList");

	// name of this checkbox product was changed by replacing "'" with "/"
	var tempProduct = convertToJavascriptUse(thisProduct);

	// get the object in the dom from the form
	var domProduct = aForm[tempProduct];

	// if we try to uncheck a product that is not on the current category list, it will be undefined
	// only uncheck the product if it is in the current viewing category
	if (domProduct != null)
	{
		domProduct.checked = false;
	}						

	// remove the item from the fifo_buffer
	thisProduct = convertToHTMLUse(thisProduct);

	fifo_pop(thisProduct);

	render_list();
}
