/**
 * Functions used by the supplies.shtml page to keep track of order totals. *  
 */

var cntr = 0;  // keeps track of the number items being ordered.
var tamt=0,tqty=0 // keeps track of the total dollar and quantity amount.

/**
 * Method which forces that a valid dollar amount displays
 * @param val The dollar amount to clean up.
 * @return A valid dollar amount String
 */
function Dollar (val)
{
	var str,pos,rnd=0;	
	str = escape (val*1.0 + 0.005001 + rnd);  // float, round, escape
	pos = str.indexOf (".");  
	if (pos > 0) str = str.substring (rnd, pos + 3);
	return str;               
}

/**
 * Collects the order items amount and qty being ordered
 * @param id The id of item being ordered
 * @param amt The dollar amount of the item being ordered 
 */
function GetOrder (id,amt) 
{ 	
	tamt=0,tqty=0;  
	var nr = id.substring (2);  
	var qty = g_el(["qty" + nr]).value;
	if (isNaN (qty)) 
	{      
		alert ("That is not a valid number!  Try again.");
		return;
	}
	
	g_el(["prc" + nr]).value = Dollar (qty * amt);  
	if (cntr == 0) order = new Object (); // create order object
	cntr = cntr + 1;               
	order[id] = new Object ();     // create new entry 
	order[id].amt = Dollar (amt);
	order[id].qty = qty;
	
	var i;
	for (i in order) //loop through the elements of the properties of order object.
	{            
		qty = order[i].qty*1.0;
		tamt = tamt + order[i].amt * qty; 
		tqty = tqty + qty;                
	}
	g_el('sbttl').value = Dollar (tamt); // value to be placed in sub total input element
}

/**
 * Replacement method for document.getElementById(elementId).
 * @param e The id of the element to retrieve.
 * @return A DOM element corresponding to the provided id, or null if that object does not exist.
 */
function g_el(e)
{
    return document.getElementById(e);
}

