<!--
var m_nextProdId = 0;
var m_currQty, m_currQtyQual;
var m_bEditOn;

function GetNextProductId()
{
	return(m_nextProdId++);
}

// Category Class

function Category(sCategory)
{
	// Properties
	
	this._category = sCategory;
	this._products = new Array();
	
	// Methods
	
	this.Category = Category_Category;
	this.AddProduct = Category_AddProduct;
	this.GetProduct = Category_GetProduct;
}

function Category_Category()
{
	// Return the Category Name
	
	return this._category;
}

function Category_AddProduct(oProduct)
{
	// Add a Product object to the Category's Products collection
	
	this._products[this._products.length] = oProduct;
	return this._products.length;
}

function Category_GetProduct(iIndex)
{
	// Get the Product object specified by the index
	
	return this._products[iIndex];
}

// Product Class

function Product(iId, sProduct, sQuantityText, sQuantityQual, fUnitCost)
{
	// TODO Get rid of QuantityQual
	
	// Properties
	
	this._Id = iId;
	this._product = sProduct;
	this._quantityText = sQuantityText;
	this._quantityQual = sQuantityQual;
	this._sizes = new Array();
	this._descriptions = new Array();
	this._unitCost = fUnitCost;
	
	// Methods
	
	this.Id = Product_Id;
	this.Product = Product_Product;
	this.AddSize = Product_AddSize;
	this.AddDescription = Product_AddDescription;
	this.QuantityText = Product_QuantityText;
	this.QuantityQual = Product_QuantityQual;
	this.GetSizes = Product_GetSizes;
	this.GetDescriptions = Product_GetDescriptions;
	this.UnitCost = Product_UnitCost;
}

function Product_Id()
{
	// Return the Product Name
	
	return this._Id;
}

function Product_Product()
{
	// Return the Product Name
	
	return this._product;
}

function Product_QuantityText()
{
	// Return the Product's Text to display in the dialog box
	
	return this._quantityText;
}

function Product_QuantityQual()
{
	// Return the Product's Text to display after the Quantity value
	
	return this._quantityQual;
}

function Product_GetSizes()
{
	// Return the Product Sizes array
	
	return this._sizes;
}

function Product_AddSize(oSize)
{
	// Add a Description object to the Product's Descriptions collection
	
	this._sizes[this._sizes.length] = oSize;
	return this._sizes.length;
}

function Product_GetDescriptions()
{
	// Return the Product Descriptions array
	
	return this._descriptions;
}

function Product_AddDescription(oDescription)
{
	// Add a Description object to the Product's Descriptions collection
	
	this._descriptions[this._descriptions.length] = oDescription;
	return this._descriptions.length;
}

function Product_UnitCost()
{
	// Return the Unit Cost 
	
	return this._unitCost;
}

// Size Class

function Size(sSize, fUnitCost)
{
	// Properties
	
	this._size = sSize;
	this._unitCost = fUnitCost;
	
	// Methods
	
	this.Size = Size_Size;
	this.UnitCost = Size_UnitCost;
}

function Size_Size()
{
	// Return the Size 
	
	return this._size;
}

function Size_UnitCost()
{
	// Return the Unit Cost 
	
	return this._unitCost;
}

// Description Class

function Description(sDescription, fUnitCost)
{
	// Properties
	
	this._description = sDescription;
	this._unitCost = fUnitCost;
	
	// Methods
	
	this.Description = Description_Description;
	this.UnitCost = Size_UnitCost;
}

function Description_Description()
{
	// Return the Description Name
	
	return this._description;
}

function Description_UnitCost()
{
	// Return the Unit Cost 
	
	return this._unitCost;
}

// Order Class

function Order()
{
	// Properties
	
	this._orderItems = new Array();			// Order Items collection
	
	// Methods
	
	this.AddOrderItem = Order_AddOrderItem;
	this.GetOrderItems = Order_GetOrderItems;
	this.RemoveOrderItem = Order_RemoveOrderItem;
}

function Order_AddOrderItem(oOrderItem)
{
	// Add an OrderItem object to the Order's OrderItems collection
	alert(this._orderItems.length);
	this._orderItems[this._orderItems.length] = oOrderItem;
	oOrderItem.SetIndex(this._orderItems.length);
	alert(this._orderItems.length);
	return this._orderItems.length;
}

function Order_GetOrderItems()
{
	// Return the OrderItems array
	
	return this._orderItems;
}

function Order_RemoveOrderItem(index)
{
	delete this._orderItems(index);
}

// OrderItem Class

function OrderItem()
{
	// Properties
	
	this._index = 0;
	this._category = "";
	this._product = "";
	this._quantity = 0;
	this._selectedSizeIndex = 0;
	this._selectedDescriptionIndex = 0;
	
	// Methods
	
	this.SetIndex = OrderItem_SetIndex;
	this.GetIndex = OrderItem_GetIndex;
	this.SetCategory = OrderItem_SetCategory;
	this.GetCategory = OrderItem_GetCategory;
	this.SetProduct = OrderItem_SetProduct;
	this.GetProduct = OrderItem_GetProduct;
	this.Quantity = OrderItem_Quantity;
	this.SetQuantity = OrderItem_SetQuantity;
	this.SelectedSizeIndex = OrderItem_SelectedSizeIndex;
	this.SetSelectedSizeIndex = OrderItem_SetSelectedSizeIndex;
	this.SelectedDescriptionIndex = OrderItem_SelectedDescriptionIndex;
	this.SetSelectedDescriptionIndex = OrderItem_SetSelectedDescriptionIndex;
	this.GetProductCategoryText = OrderItem_GetProductCategoryText;
	this.GetProductCategorySizeText = OrderItem_GetProductCategorySizeText;
}

function OrderItem_GetIndex()
{
	return this._index;
}

function OrderItem_SetIndex(index)
{
	this._index = index;
}

function OrderItem_SetCategory(oCategory)
{
	this._category = oCategory;
}

function OrderItem_SetProduct(oProduct)
{
	this._product = oProduct;
}

function OrderItem_GetProduct()
{
	return this._product;
}

function OrderItem_GetCategory()
{
	return this._category;
}

function OrderItem_Quantity()
{
	return this._quantity;
}

function OrderItem_GetProductCategoryText()
{
	var oProduct = this._product;
	var oCategory = this._category;
	return oCategory.Category() + ': ' + oProduct.Product();
}

function OrderItem_GetProductCategorySizeText()
{
	var oProduct = this._product;
	var oCategory = this._category;
	var text;
	
	if (oProduct.GetSizes().length == 0)
	{
		text = oCategory.Category() + ': ' + oProduct.Product();
	}
	else if (oProduct.GetSizes()[0].Size() == 'N/A')
	{
		text = oCategory.Category() + ': ' + oProduct.Product();
	}
	else
	{
		text = oCategory.Category() + ': ' + oProduct.Product() + ' - ' + oProduct.GetSizes()[this._selectedSizeIndex].Size();
	}
	
	return text;		
}

function OrderItem_SetQuantity(iQty)
{
	this._quantity = iQty;
}

function OrderItem_SelectedSizeIndex()
{
	return this._selectedSizeIndex;
}

function OrderItem_SetSelectedSizeIndex(i)
{
	this._selectedSizeIndex = i;
}

function OrderItem_SelectedDescriptionIndex()
{
	return this._selectedDescriptionIndex;
}

function OrderItem_SetSelectedDescriptionIndex(i)
{
	this._selectedDescriptionIndex = i;
}

function ProcessOrderItem(oCategory, iProductId)
{
	var returnValue;
	var oOrderItems;
	var oOrderItem;
	
	// Buy button has been clicked for a product. Display the dialog to request the 
	// product quantity/description/size
		
	oOrderItem = new OrderItem();
	oProduct = oCategory.GetProduct(iProductId);
	oOrderItem.SetCategory(oCategory);
	oOrderItem.SetProduct(oProduct);
	returnValue = window.showModalDialog("order/OrderItemDetails.htm", oOrderItem, "dialogHeight: 260px; dialogWidth: 400px; center: Yes; help: No; scroll: No; resizable: No; status: No;");
	if (returnValue == true)
	{
		AddItemToCart(oOrderItem);
	}
}

function InitOrderItemsWindow()
{
	// Initialise the OrderItemDetails Dialog box
	
	var ParentDoc;
	var td;
	var value, newValue;
	var index;
	var oCategory, oProduct, oOrderItem, oSizes, oSize, oDescriptions, oDescription;
	var arr;
	var ProductCategory, arrProducts, ProductIndex, QuantityText;
	var lbSizes, lbDescriptions;
	var i;

	// Extract the invoking window's Order Item object
	
	oOrderItem = window.dialogArguments;
	oCategory = oOrderItem.GetCategory();
	oProduct = oOrderItem.GetProduct();
	
	lbSizes = document.frmOrderItemDetails.lbSizes;
	lbDescriptions = document.frmOrderItemDetails.lbDescriptions;
	
	// Substitute 'Product' with the supplied Product/Category and Size
	
	td = document.getElementById('Product');
	value = td.innerHTML;
	index = value.indexOf('Product');
	newValue = value.substr(0, index);
	newValue = newValue + oOrderItem.GetProductCategoryText();
	newValue = newValue + value.substr(index + 7, value.length - index - 7);
	td.innerHTML = newValue;
	
	// Set the Quantity Label text

	td = document.getElementById('tdQty');
	value = td.innerHTML;
	index = value.indexOf('Quantity');
	newValue = value.substr(0, index);
	newValue = newValue + oProduct.QuantityText();
	newValue = newValue + value.substr(index + 8, value.length - index - 8);
	td.innerHTML = newValue;
	
	// Populate the Sizes Drop-down List Box IE ONLY??
	
	oSizes = oProduct.GetSizes();
	
	// Check if a size has been specified. Change to 0 length test when Description pricing has been specified
	
	if (oSizes.length == 0)
	{
		document.getElementById('trSize').style.display = 'none';
	}
	else
	{
		for (i = 0; i < oSizes.length; i++)
		{
			oSize = oSizes[i];
			var opt = new Option(oSize.Size(), i);
			lbSizes.add(opt);
		}
		if (i == 1)
		{
			lbSizes.disabled = true;
		}
	}
	
	// Get the available descriptions and populate the Drop-down List Box IE ONLY??
	
	oDescriptions = oProduct.GetDescriptions();
	if (oDescriptions.length == 0)
	{
		document.getElementById('trDescription').style.display = 'none';
	}
	else
	{
		for (i = 0; i < oDescriptions.length; i++)
		{
			oDescription = oDescriptions[i];
			var opt = new Option(oDescription.Description(), i);
			lbDescriptions.add(opt);
		}
		if (i == 1)
		{
			lbDescriptions.disabled = true;
		}
	}
}

function AddItemToCart(oOrderItem)
{
	// Populate the hidden fields with the requested quantity, etc, and submit the form
	// for processing by the server.
	
	var txt;
	var oProduct;
	var oSizes;
	var oSize;
	var oDescriptions;
	var price;
	var qty;
	var cost;
	var sDescription;

	oProduct = oOrderItem.GetProduct();
	oSizes = oProduct.GetSizes();
	if (oSizes.length != 0)
	{
		oSize = oSizes[oOrderItem.SelectedSizeIndex()];
	}
	oDescriptions = oProduct.GetDescriptions();
	if (oDescriptions.length != 0)
	{
		oDescription = oDescriptions[oOrderItem.SelectedDescriptionIndex()];
	}
	
	if (oProduct.UnitCost() != undefined)
	{
		price = parseFloat(oProduct.UnitCost());
	}
	
	if (price == undefined)
	{
		if (oSize != undefined)
		{
			if (oSize.UnitCost() != undefined)
			{
				price = parseFloat(oSize.UnitCost());
			}
		}
	}
	
	if (price == undefined)
	{
		price = parseFloat(oDescription.UnitCost());
	}
	
	qty = parseInt(oOrderItem.Quantity());
	cost = ConvertToCurrency(price * qty);
	
	if (oDescriptions.length > 0)
	{
		sDescription = '; ' + oDescription.Description();
	}
	else
	{
		sDescription = '';
	}
	
	txt = document.getElementById('TblCartDesc');
	txt.value = oOrderItem.GetProductCategorySizeText() + sDescription;
	txt = document.getElementById('TblCartQty');
	txt.value = qty + oProduct.QuantityQual();
	txt = document.getElementById('TblCartCost');
	txt.value = cost;
	
	frmCart.submit();
}

function Checkout()
{
	// Don't allow Checkout to be clicked when in the Checkout process
	
	if (window.document.title == 'Checkout')
	{
		return;
	}
	
	submit_basket();
}

function SubmitOrderItems(win)
{
	var i;
	var oOrderItem;
	var oQty, oSizes, oDescriptions, iQty;
	
	oOrderItem = window.dialogArguments;

	oQty = window.frmOrderItemDetails.Qty;
	oSizes = window.frmOrderItemDetails.lbSizes;
	oDescriptions = window.frmOrderItemDetails.lbDescriptions;
	
	iQty = oQty.value;
	if (iQty == '')
	{
		alert('Please enter a Quantity');
		oQty.focus();
	}
	else if (isNaN(iQty))
	{
		alert('Please enter a valid Quantity');
		oQty.value = '';
		oQty.focus();
	}
	else if (oSizes.length > 0 && oSizes.selectedIndex < 0)
	{
		alert('Please select a Size');
		oSizes.focus();
	}
	else if (oDescriptions.length > 0 && oDescriptions.selectedIndex < 0)
	{
		alert('Please select a Description');
		oDescriptions.focus();
	}
	else
	{
		oOrderItem.SetQuantity(iQty);
		if (oSizes.length > 0)
		{
			oOrderItem.SetSelectedSizeIndex(oSizes.selectedIndex);
		}
		else
		{
			oOrderItem.SetSelectedSizeIndex = 0;
		}
		oOrderItem.SetSelectedDescriptionIndex(oDescriptions.selectedIndex);
		win.returnValue = true;
		win.close();
	}
}

function ProcessDialogKey(e, win)
{
	var key;
	
	// Exit the Dialog if ESC is pressed
	
	key = win.event.keyCode;
	if (key == 27)
	{
		win.returnValue = false;
		win.close();
	}
}

function ProcessEditOrderKey(doc, win)
{
	var key, elem, i, iNumbItems, id, tbl, tBody, rows, row, cells, cell;
	
	// Exit the Dialog if ESC is pressed
	
	key = win.event.keyCode;
	if (key == 27)
	{
		if (m_bEditOn)
		{
			elem = doc.getElementById('OrdevalueberItems')
			iNumbItems = parseInt(elem.value);
			for (i = 1; i <= iNumbItems; i++)
			{
				elem = doc.getElementById('EditQty' + i);
				if (elem.style.display == 'inline')
				{
					elem.value = m_currQty;
					elem.style.display = 'none';
					id = "editCart" + i;
					elem = doc.getElementById(id);
					elem.style.display = 'inline';
					id = "updateCart" + i;
					elem = doc.getElementById(id);
					elem.style.display = 'none';
					tbl = doc.getElementById('tblCheckout');
					tBody = tbl.getElementsByTagName('tbody')[0];
					rows = tBody.rows;
					row = rows[i + 2];
					cells = row.cells;
					cell = cells[2];
					SetTextNodeValue(cell, m_currQtyQual);
				}
			}
		}
	}
}

function EditOrderItemQty(document, index)
{
	var id, tbl, tBody, rows, row, cell;
	
	tbl = document.getElementById('tblCheckout');
	tBody = tbl.getElementsByTagName('tbody')[0];
	rows = tBody.rows;
	
	// Get the Qty cell and remove the quantity value before displaying the edit box
	
	row = rows[index + 2];
	cells = row.cells;
	cell = cells[2];
	m_currQtyQual = GetTextNodeValue(cell);
	SetTextNodeValue(cell, '');
	id = "editQty" + index;
	elem = document.getElementById(id);
	m_currQty = elem.value;
	elem.style.display = 'inline';
	elem.focus();
	elem.select();
	id = "editCart" + index;
	elem = document.getElementById(id);
	elem.style.display = 'none';
	id = "updateCart" + index;
	elem = document.getElementById(id);
	elem.style.display = 'inline';
	m_bEditOn = true;
//	document.frmCheckout.cmdSubmit.enabled = false;
}

function UpdateOrderItemQty(document, index)
{
	var tbl, tBody, rows, row, cell, i, node;
	var id, elem, qty, qtyQual, totalQtyCell, totalQty, curr, cost, totalCostCell, totalCost, iNumbItems, bDisplay;
	var oDoc, oWin;
	
	// Access the row element for this item
	
	tbl = document.getElementById('tblCheckout');
	tBody = tbl.getElementsByTagName('tbody')[0];
	rows = tBody.rows;
	row = rows[rows.length - 1];
	cells = row.cells;
	
	// Get the new quantity

	id = "editQty" + index;
	elem = document.getElementById(id);
	if (elem.value == '')
	{
		alert('Please enter a Quantity');
		elem.focus();
		return;
	}
	else if (isNaN(elem.value))
	{
		alert('Please enter a valid Quantity');
		elem.value = '';
		elem.focus();
		return;
	}
	qty = parseInt(elem.value);
	if (qty == 0)
	{
		if (!confirm("This will result in the item being removed from your shopping cart. Click OK to continue, or cancel to leave it"))
		{
			row = rows[index + 2];
			cells = row.cells;
			cell = cells[2];
			SetTextNodeValue(cell, m_currQty);
			elem.value = m_currQty;
			elem.style.display = 'none';
			id = "updateCart" + index;
			elem = document.getElementById(id);
			elem.style.display = 'none';
			id = "editCart" + index;
			elem = document.getElementById(id);
			elem.style.display = 'inline';
			elem = document.getElementById('Qty' + i);
			return;
		}
	}
	
	// get the old totals
	
	totalCostCell = cells[1];
	totalCost = parseFloat(totalCostCell.firstChild.innerHTML.substr(1));
	totalQtyCell = cells[2];
	totalQty = parseInt(totalQtyCell.firstChild.innerHTML);
	
	// Update the item's cost and new totals
	
	row = rows[index + 2];
	cells = row.cells;
	cell = cells[1];
	cost = parseFloat(cell.innerHTML.substr(1));
	newCost = cost / m_currQty * qty;
	SetTextNodeValue(cell, ConvertToCurrency(newCost));
	cell = cells[2];
	qtyQual = qty + m_currQtyQual.substr(m_currQty.length, m_currQtyQual.length - (m_currQty.length));
	SetTextNodeValue(cell, qtyQual);
	totalCost = totalCost + newCost - cost;
//	SetTextNodeValue(totalCostCell, ConvertToCurrency(totalCost));
	totalCostCell.firstChild.innerHTML = ConvertToCurrency(totalCost);

	if (parseInt(qty) == 0)
	{
		// TODO Remove orderItem for Order object
		document.getElementById('trCheckout' + index).style.display = 'none';
		var elemQty = document.getElementById('Qty' + index);
		elemQty.value = 0;
	}
	else
	{
		elem.style.display = 'none';
		id = "updateCart" + index;
		elem = document.getElementById(id);
		elem.style.display = 'none';
		id = "editCart" + index;
		elem = document.getElementById(id);
		elem.style.display = 'inline';
	}
	
	// Update the hidden input objects

	elem = document.getElementById('Cost' + index);
	elem.value = ConvertToCurrency(newCost);
	if (parseInt(qty) != 0)
	{
		elem = document.getElementById('Qty' + index);
		elem.value = qtyQual;
	}

	// Update the Cart's row

	oWin = window.top.main;
	oDoc = oWin.document;
	row = oDoc.getElementById('tblCartItemstrItem' + index);
	
	if (parseInt(qty) == 0)
	{
		row.style.display = 'none';
	}
	else
	{
		cells = row.cells;
		cells[1].innerHTML = '<font face="Times New Roman, Times, serif">' + qtyQual + '</font>';
		cells[2].innerHTML = '<font face="Times New Roman, Times, serif">' + ConvertToCurrency(newCost) + '</font>';
	}
	
	// Update the Cart Total
	
		row = oDoc.getElementById('tblCartItemstrTotalCost');
		cells = row.cells;
		cells[1].innerHTML = ConvertToCurrency(totalCost);

	// See if all items havwe been removed
	
	iNumbItems = parseInt(document.getElementById('OrdevalueberItems').value);
	bDisplay = false;
	for (i = 1; i <= iNumbItems; i++)
	{
		if (parseInt(document.getElementById('qty' + i).value) != '0')
		{
			bDisplay = true;
			break;
		}
	}
	
	if (!bDisplay)
	{
		CleanupOrder();           
	}

	m_bEditOn = false;
	document.getElementById('txtUpdateQtyIndex').value = index;
	document.forms.frmCheckout.submit();
}

function CleanupOrder()
{
	var oWinCart;
	var oDocCart;
	var iNumbItems;
	var tblItems, tblBody;
	var lastRow;
	var rowElem;
	var i;
	
	// Get reference to Cart window and document

	oWinCart = window.top.main;
	oDocCart = oWinCart.document;
	
	// Initialise the Shopping Cart
					
	tblItems = oDocCart.getElementById('tblCartItems');
	tblBody = tblItems.getElementsByTagName('tbody')[0];
	
	// Remove the rows which have been dynamically added
	
	lastRow = tblItems.rows.length;
	for (i = 3; i < lastRow; i++)
	{
		tblItems.deleteRow(2);
	}
	
	// Empty the template row containing the first item
	
	rowElem = tblBody.getElementsByTagName('tr')[1];
	rowElem.style.display = 'inline';			// In case it was hidden by updating quantity to 0
	cells = rowElem.cells;
	cells[0].innerHTML = '<font face="Times New Roman, Times, serif">&nbsp;</font>';
	cells[1].innerHTML = '<font face="Times New Roman, Times, serif">&nbsp;</font>';
	cells[2].innerHTML = '<font face="Times New Roman, Times, serif">&nbsp;</font>';
	
	// Hide the Shopping Cart
	
	oDocCart.getElementById('tblShoppingCart').style.display = 'none';
}

function SetTextNodeValue(cell, strValue)
{
	var i, cell;
	
	node = cell.firstChild;
	for (i = 0; i < cell.childNodes.length; i++)
	{
		if (node.nodeType == 3)
		{
			node.nodeValue = strValue;
			break;
		}
		node = node.nextSibling;
	}
}

function GetTextNodeValue(cell)
{
	var cell, i;
	
	node = cell.firstChild;
	for (i = 0; i < cell.childNodes.length; i++)
	{
		if (node.nodeType == 3)
		{
			return node.nodeValue;
			break;
		}
		node = node.nextSibling;
	}
}

function ConvertToCurrency(value)
{
	var cur;
	var index;
	
	// First, round it
	
	var rlength = 2; // The number of decimal places to round to
	if (value > 8191 && value < 10485) {
		value = value-5000;
		var newnumber = Math.round(value*Math.pow(10,rlength))/Math.pow(10,rlength);
		//newnumber = newnumber+5000;
	} else {
		var newnumber = Math.round(value*Math.pow(10,rlength))/Math.pow(10,rlength);
	}
	value = newnumber;
	
	cur = '$' + value;
	index = cur.indexOf('.');
	switch(index)
	{
		case -1:
			cur = cur + '.00';
			break;
		case cur.length - 2:
			cur = cur + '0';
			break;
	}
	return cur;
}
-->	
	
