function ShoppingCart()
{
	this.items = new Array();
	this.lastIndex = 0;
}

function shoppingcart_addItem(itemToAdd)
{
	this.items[this.items.length] = itemToAdd;		
}

function shoppingcart_contains(item)
{
	var contains = false;

	for(var i = 0; i < this.items.length; i++)
	{
		if(item.photo.num == this.items[i].photo.num)
		{
			if(this.items[i].paper.equals(item.paper))
			{
				this.lastIndex = i;
				contains = true;
				break;
			}
		}
	}

	return contains;
}

function shoppingcart_fix(amount, type)
{
	var fixedNumber = new String(Math.round(amount * 100) / 100);
	if(fixedNumber.lastIndexOf(".") == -1)
	{
		fixedNumber += ".";
	}

	var zeros = 2 - (fixedNumber.length - fixedNumber.lastIndexOf(".") - 1);
	for( ; zeros > 0; zeros--)
	{
		fixedNumber += "0";
	}

	if(type == "soft")
	{
		var index = fixedNumber.indexOf(".");
		while(index > 3)
		{
			var comma = index - 3;
			var first = fixedNumber.substring(0, comma);
			fixedNumber = first + "," + fixedNumber.substring(comma);
			index = fixedNumber.indexOf(",");
		}
	}

	return fixedNumber;
}

function shoppingcart_getHtml()
{
	var sc = "<table>";
	sc += "<tr><th>Photograph</th><th>Paper</th><th>Image</th><th>Price</th>";
	sc += "<th>Qty</th><th>Total</th><th colspan='2'></th></tr>";

	for(var i = 0; i < this.items.length; i++)
	{
		sc += "<tr>";

		sc += "<td class='cart'><nobr><img src='" + tPath;
		sc += this.items[i].photo.num + ".jpg' ";
		sc += "width='60' height='60' border='1' align='middle'> ";
		sc += this.items[i].photo.toString() + "</nobr></td>";

		sc += "<td align='center' class='cart'><nobr>";
		sc += this.items[i].paper.toString() + "</nobr></td>";

		sc += "<td align='center' class='cart'><nobr>";
		sc += this.items[i].paper.getImageSize() + "</nobr></td>";

		sc += "<td align='right' class='cart'>$";
		sc += ShoppingCart.fix(stock.getPrice(this.items[i].paper), "soft") + "</td>";

		sc += "<td align='center' class='cart'>" + this.items[i].qty + "</td>";

		sc += "<td align='right' class='cart'>$";
		sc += ShoppingCart.fix(this.getItemTotal(i), "soft") + "</td>";

		sc += "<td class='cart'><a href='' ";
		sc += "onclick='top.purchasePage.editItem(" + i + ")'>edit</a></td>";

		sc += "<td class='cart'><a href='' ";
		sc += "onclick='top.purchasePage.remove(" + i + ")'>remove</a></td>";

		sc += "</tr>";
	}


	sc += "<tr><td colspan=4 align=right><b>Sub Total:</b></td>"+
	"<td align=center>&nbsp;</td>"+
	"<td align=right><b>$" + ShoppingCart.fix(this.getSubtotal(), "soft") + "</td>"+
	"<td colspan=2></td>"+
	"</tr>"+

	"<tr><td colspan=4 align=right><b>Shipping:</b></td>"+
	"<td></td>"+
	"<td align=right><b>$" + ShoppingCart.fix(this.getShipping(), "soft") + "</td>"+
	"<td colspan=2></td>"+
	"</tr>"+

	"<tr><td colspan=4 align=right><b>Total:</b></td>"+
	"<td></td>"+
	"<td align=right><b>$" + ShoppingCart.fix(this.getTotal(), "soft") + "</td>"+
	"<td colspan=2></td>"+
	"</tr>"+

	"</table>";

	return sc;
}

function shoppingcart_getItemTotal(n)
{
	return stock.getPrice(this.items[n].paper) * this.items[n].qty;
}

function shoppingcart_getNumberOfItems()
{
	var cartitems = 0;

	for(var i = 0; i < this.items.length; i++)
	{
		cartitems += this.items[i].qty;
	}

	return cartitems;
}

function shoppingcart_getShipping()
{
	var ship = 0;
	var total = this.getSubtotal();

	if(total > 0 && total < 35) ship = 5;
	else if(total >= 35 && total < 100) ship = 10;
	else if(total >= 100 && total < 200) ship = 15;
	else if(total >= 200) ship = 18;

	return ship;
}

function shoppingcart_getSubtotal()
{
	var subtot = 0;

	for(var i = 0; i < this.items.length; i++)
	{
		subtot += this.getItemTotal(i);
	}

	return subtot;
}

function shoppingcart_getTotal()
{
	return (this.getSubtotal() + this.getShipping());
}

function shoppingcart_inc(n)
{
	this.items[this.lastIndex].qty += n;
}

function shoppingcart_remove(index)
{
	var temp = new Array();
	this.items[index] = null;

	for(var i = 0; i < this.items.length; i++)
	{
		if(this.items[i] != null)
		{
			temp[temp.length] = this.items[i];
		}
	}

	this.items = temp;
}

ShoppingCart.fix = shoppingcart_fix;
ShoppingCart.prototype.addItem = shoppingcart_addItem;
ShoppingCart.prototype.contains = shoppingcart_contains;
ShoppingCart.prototype.getHtml = shoppingcart_getHtml;
ShoppingCart.prototype.getItemTotal = shoppingcart_getItemTotal;
ShoppingCart.prototype.getNumberOfItems = shoppingcart_getNumberOfItems;
ShoppingCart.prototype.getShipping = shoppingcart_getShipping;
ShoppingCart.prototype.getSubtotal = shoppingcart_getSubtotal;
ShoppingCart.prototype.getTotal = shoppingcart_getTotal;
ShoppingCart.prototype.inc = shoppingcart_inc;
ShoppingCart.prototype.remove = shoppingcart_remove;
