/* - - - - - - - 
Calculates the total of across across rows
	and then totals the columns. Adds shipping, etc
	to total total.
		Al DiMarzio, HB Graphics,  rev 1, Jan 03, Rev 2, Feb 04
- - - - - - - - */

function RunBooks() {
	var rowCount = 6;
	var subTotal = 0;
	
	if (document.ordering.shipping_b.value <= 0)  {
		document.ordering.shipping_b.value = 0;
	}
	if (document.ordering.salestax_b.value <= 0)  {
		document.ordering.salestax_b.value = 0;
	}
	
	for (var i = 1; i <= rowCount; i++)  {
		var qtyCol = eval("document.ordering.qty_b" + i).value;
		var priceCol = eval("document.ordering.price_b" + i).value;
		var amountCol = qtyCol * priceCol;
		eval("document.ordering.amount_b" + i).value = formatCurrency(amountCol);
		
		if (amountCol > 0)  {
			subTotal = subTotal + amountCol;
		}
	}
	
	document.ordering.subtotal_b.value = formatCurrency(subTotal);
	document.ordering.total_b.value = formatCurrency(parseFloat(document.ordering.subtotal_b.value) + parseFloat(document.ordering.shipping_b.value) + parseFloat(document.ordering.salestax_b.value));
}

function RunJournals() {
	var rowCount = 3;
	var subTotal = 0;
	
	if (document.ordering.shipping_j.value <= 0)  {
		document.ordering.shipping_j.value = 0;
	}
	if (document.ordering.salestax_j.value <= 0)  {
		document.ordering.salestax_j.value = 0;
	}
	
	for (var i = 1; i <= rowCount; i++)  {
		var qtyCol = eval("document.ordering.qty_j" + i).value;
		var priceCol = eval("document.ordering.price_j" + i).value;
		var amountCol = qtyCol * priceCol;
		eval("document.ordering.amount_j" + i).value = formatCurrency(amountCol)
		
		if (amountCol  > 0)  {
			subTotal = subTotal + amountCol;
		}
	}
	
	document.ordering.subtotal_j.value = formatCurrency(subTotal);
	document.ordering.total_j.value = formatCurrency(parseFloat(document.ordering.subtotal_j.value) + parseFloat(document.ordering.shipping_j.value) + parseFloat(document.ordering.salestax_j.value));
}

function formatCurrency(value)  {
	/* Generic routine for formatting numbers. See page 569 of
	the JavaScript Bible */ 
	var decplaces = 2;
	var strValue = "" + Math.round(eval(value)*Math.pow(10, decplaces));
	while(strValue.length <= decplaces)  {
		strValue = "0" + strValue;
	}
	var decpoint = strValue.length - decplaces;
	value = strValue.substring(0, decpoint) + "." + strValue.substring(decpoint, strValue.length);
	return value;  //return the value to the calling routine
}