function update(name)
{
	sizeField = "s_" + name;
	size = eval("document.forms[0]." + sizeField + ".value");
	
	qtyField = "q_" + name;
	qty = eval("document.forms[0]." + qtyField + ".value");

	totalField = "t_" + name;
	
	// Check up on the qty field to check that it is a number
	if (!isInteger(qty))
	{
		alert ("You must enter a number for the quantity");
		
		eval("document.forms[0]." + totalField + ".value = 0");
	}
	else
	{
		totalSize = qty * size;
		
		totalSize = roundOff(totalSize, 2);
		
		eval("document.forms[0]." + totalField + ".value = " + totalSize);
		
		updateGrandTotal();
	}
}


function updateGrandTotal()
{
	numberTotals = document.forms[0].elements.length;
	grandTotal = 0;
	
	for (i = 0; i < numberTotals; i++)
	{
		if (document.forms[0].elements[i].name.substr(0,2) == "t_")
		{
			grandTotal += eval(document.forms[0].elements[i].value);
		}
	}

	grandTotal = roundOff(grandTotal, 2);
	document.forms[0].total.value = grandTotal;

}


function getIndex(input)
{
	var index = -1, i = 0, found = false;
	while (i < input.form.length && index == -1)
	{
		if (input.form[i] == input)
		{
			index = i;
		}
		else
		{
			i++;
		}
	}
	return index;
}


function nextFocus(element)
{
	formIndex = (getIndex(element) + 2) % element.form.length;
	element.form[formIndex].focus();	
}


//-------------------------------------------------------------------
// isInteger(value)
//   Returns true if value contains all digits
//-------------------------------------------------------------------
function isInteger(val) {
	for (var i=0; i < val.length; i++) {
		if (!isDigit(val.charAt(i))) { return false; }
		}
	return true;
	}

//-------------------------------------------------------------------
// isDigit(value)
//   Returns true if value is a 1-character digit
//-------------------------------------------------------------------
function isDigit(num) {
	var string="1234567890";
	if (string.indexOf(num) != -1) {
		return true;
		}
	return false;
	}


function roundOff(value, precision)
{
        value = "" + value //convert value to string
        precision = parseInt(precision);

        var whole = "" + Math.round(value * Math.pow(10, precision));

        var decPoint = whole.length - precision;

        result = whole.substring(0, decPoint);
        result += ".";
        result += whole.substring(decPoint, whole.length);

        return result;
}


