// JavaScript Document
function thePackage()
{
	var arrItems = new Array();
	var arrContainers = new Array();
	
	arrContainers[0] = new Array("envelope",4.95);
	arrContainers[1] = new Array("smallBox",7.95);
	arrContainers[2] = new Array("largeBox",9.95);
	
	arrItems[0] = 0;
	arrItems[1] = 0;
	arrItems[2] = 0;

	this.items = arrItems;
	this.container = "";
	this.cost = 0;
	this.containers = arrContainers;
	
	this.itemCount = function()
	{
		return this.items[1] + this.items[2];
	}
	
	this.addItem = function(vIntShippingClass,vIntQuantity)
	{
		this.items[vIntShippingClass] += vIntQuantity;					
	}
	
	this.pack = function()
	{
		if(!this.envelope())
		{
			if(!this.smallBox())
			{
				if(!this.largeBox())
				 {
					alert("Error! Shipping calculations have failed.");
				 }
			}
		}
	}
	
	this.envelope = function()
	{
		if(this.itemCount()>4)
		{
			// Container must be larger than envelope. 
			return false;	
		} else if(this.itemCount()==4){
			if(this.items[1]==4)
			{
				this.container = this.containers[0][0];
				this.cost = this.containers[0][1];
				return true;
			} else {
				return false;
			}
		} else if(this.itemCount()==3){
			if(this.items[2]>=2)
			{
				// Container must be smallBox
				return false;
			} else {
				// less than 3 of any items fits in an envelope.
				this.container = this.containers[0][0];
				this.cost = this.containers[0][1];
				return true;
			}	
		} else {
			// 1 new and 2 old fits in an envelope.
			this.container = this.containers[0][0];
			this.cost = this.containers[0][1];
			return true;
		}
	}
	
	this.smallBox = function()
	{
		if(this.itemCount()>8)
		{
			return false;
		} else {
			this.container = this.containers[1][0];
			this.cost = this.containers[1][1];
			return true;
		}
	}

	this.largeBox = function()
	{
		if((this.itemCount()>=9)&&(this.itemCount()<=12))
		{
			this.container = this.containers[2][0];
			this.cost = this.containers[2][1];
			return true;
		} else {
			// this branch should never be hit.
			return false;
		}
	}
	
	this.inventory = function()
	{
		var intUnit;
		var intTotalQuantity = 0;
		var	intUnitCount = document.forms[0].rowsCount.value; //TODO: Replace this assignment with a runtime value set by ASP during writeProducts() routine.
		// Sum all quantity values.
		for(intUnit=1;intUnit<=intUnitCount;intUnit++)
		{
			intItemQuantity = parseInt(document.getElementById("unit" + intUnit).value);
			intShippingClass = parseInt(document.getElementById("shippingClass_" + intUnit).value);
			if(intItemQuantity>0)
			{
				this.addItem(intShippingClass,intItemQuantity);
			}
		}			
		// Return total quantity of items.
		return intTotalQuantity;
	}
}


// First pack largest items (Class 2).
// If totalitems <=4 Then
// If total class2 = 2 And total class 1 > 0 Then



