// object to manipulate order summary section
function OrderSummary(formobj)
{
	// get a ref to the form
	this.formobj = formobj;
	
	// give the form a reference to me
	this.formobj.ordersummaryobj = this;
	
	// find our add new row, clone it, and remember it
	var oTR = document.getElementById("OrderSummaryNewRow");
	this.newrow = oTR.cloneNode(true);
	
	// delete the old TR. it messes everything up.
	var oTbody = document.getElementById("OrderSummaryTable").getElementsByTagName("TBODY")[0];
	oTbody.removeChild(oTR);
}

// hook up the functions to the object
OrderSummary.prototype.addLineItem = _addLineItem;
OrderSummary.prototype.deleteSelected = _deleteSelected;


function _addLineItem(id,quantity,description,validationamount,isPacket)
{
	// get the order summary table
	var oTbody = document.getElementById("OrderSummaryTable").getElementsByTagName("TBODY")[0];
	
	// check if we already have a line item for this
	var NewRow;
	var oElement;
	
	if (isPacket == 'True')
		id = 'Packet-' + id;
	
	if (document.getElementById("NewRow-" + id))
	{
		NewRow = document.getElementById("NewRow-" + id);
	}
	else
	{
		// clone our generic add line item row
		NewRow = this.newrow.cloneNode(true);
		NewRow.id = "NewRow-" + id;
		
		// modify the ids
		var oInputs = NewRow.getElementsByTagName("input");
		for (var i=0; i< oInputs.length;i++)
		{
			// parse the non-AddNew portion of the id
			oElement = oInputs[i];
			var NameChunk = oElement.name.substring(0,oElement.name.indexOf("-"));
			oElement.id = NameChunk + "-" + id;
			oElement.name = NameChunk + "-" + id;
		}
		// now do the silly span
		oElement = NewRow.getElementsByTagName("span")[0];
		NameChunk = oElement.id.substring(0,oElement.id.indexOf("-"));
		oElement.id = NameChunk + "-" + id;
		
		oTbody.appendChild(NewRow);
	}
	
	// modify the contents of the td's
	// checkbox
	oElement = document.getElementById("InventoryIDSelect-" + id);
	oElement.value = id;
	
	// quantity
	oElement = document.getElementById("InventoryID-" + id);
	oElement.value = quantity;				
			
	// description
	oElement = document.getElementById("InventoryDesc-" + id);
	oElement.innerHTML = description;
	
	// add the validation
	var fieldexists = new FieldExists("InventoryID-" + id,"");
	fieldexists.silent = true;
	var validquantity = new RequireRegex("InventoryID-" + id, /^[1-9]\d*$/,"");
	validquantity.silent = true;
	FormVal.addrule(new DependentRules(fieldexists,true,validquantity,true,"Please enter a valid quantity greater than zero."));
	
	// some items do not have an order limit
	if (validationamount != "none")
	{
		var quantitycheck = new InequalityTest("InventoryID-" + id,"<=",validationamount,"",false);
		quantitycheck.silent = true;
		FormVal.addrule(new DependentRules(fieldexists,true,quantitycheck,true,"This order form will not allow placement of orders with quantities over " + validationamount + " pieces per item."));
	}
}

function _deleteSelected()
{
	// get a reference to the order summary table
	var oTbody = document.getElementById("OrderSummaryTable").getElementsByTagName("TBODY")[0];
	
	// loop the order summary table finding those that are checked.
	var aryDelete = Array();
	for(var i = 0; i < this.formobj.elements.length; i++) {
		
		if(this.formobj.elements[i].name.substring(0,18) == "InventoryIDSelect-") {
			// yep, this element is our checkbox
			if (this.formobj.elements[i].checked) {
				// and its checked
				// parse the id and remember it.
				var litid = this.formobj.elements[i].name.substring(18);
				aryDelete[aryDelete.length] = litid;
			}
		}
	}
	
	// delete all of the rows that we are supposed to.
	for (var i = 0;i < aryDelete.length;i++)
	{
		// remove the tr
		var oTR = document.getElementById("NewRow-" + aryDelete[i]);
		oTbody.removeChild(oTR);
	}
	
}