function getCost(name) {
	var val = eval(name + ".cost");
	document.write( val.toFixed(2) );
}

function updateTotal() {
	updateTax();
	var sum = 0.0;
	var subtotal = document.getElementById("subtotal").value;
	var grandTotalEl = document.getElementById("grandtotal");
	var tax = document.getElementById("tax").value;

	if(subtotal != "")
		sum += parseFloat(subtotal);
	if(tax != "")
		sum += parseFloat(tax);
	grandTotalEl.value = sum.toFixed(2);
}

/**
This function is called primarily by the onChange method for an item to updated the total cost for an item, including shipping, once an item quantity has been modified.
**/
function getItemTotal(name) {
	var q = document.getElementById(name + "quantity").value.trim();
	var sCost = document.getElementById(name + "cost").value.trim();
	var sShipping = document.getElementById(name + "shipping").value.trim();
	if( isNaN( parseFloat(q) ) )
		alert("Please set the quantity to a numeric value");
	else {
		var quantity = parseFloat(q);
		var cost = parseFloat( sCost );
		var shipping = parseFloat( sShipping );
		var total = quantity * (cost + shipping);
		total = total.toFixed(2);
		var newNode = document.createTextNode(total);
		var totalElement = document.getElementById(name + "total");
		totalElement.value = total;
		calcSubTotal();
		calcShippingTotal();
	}
}



/**
Loop through all total fields on the order form to get the grand total for the order. This function will loop through
all the input tags looking for only those with the title attribute set to "totalField"
**/
function calcSubTotal() {
	var invoiceTotal = 0.0;
	var inputFields = document.getElementsByTagName("input");
	for(i = 0; i < inputFields.length; i++) {
		if(inputFields[i].title == "totalField" && inputFields[i].value != "") {
			invoiceTotal += parseFloat(inputFields[i].value);
		}
	}
	invoiceTotal = invoiceTotal.toFixed(2);
	var totalElement = document.getElementById("subtotal");
	totalElement.value = invoiceTotal;
	updateTotal();
}

/**
Loop through all shipping fields on the order form to get the grand total for the order. This function will loop through
all the input tags looking for only those with the title attribute set to "shipping"
**/
function calcShippingTotal() {
	var shippingTotal = 0.0;
	var inputFields = document.getElementsByTagName("input");
	for(i = 0; i < inputFields.length; i++) {
		if( inputFields[i].type == "hidden" ) {
			var name = inputFields[i].name;
			if(name.indexOf("shipping") != -1) {
				var shipCost = parseFloat( inputFields[i].value );	
				name = name.substring(name, name.indexOf("shipping"));
				var q = document.getElementById( (name + "quantity") );
				//alert(name + " q is " + q.value);
				if( !isNaN( parseFloat(q.value) ) ) {
					var shippingForItem = parseFloat(q.value) * parseFloat(shipCost);
					//alert("shipForItem is " + shippingForItem);
					shippingTotal += parseFloat(shippingForItem);
				}
			}
		}
	}
	shippingTotal = shippingTotal.toFixed(2);
	//var shippingElement = document.getElementById("ship_total");
	//shippingElement.value = shippingTotal;
	var hiddenShipElement = document.getElementById("x_ship_total");
	hiddenShipElement.value = shippingTotal;
}


function showTax() {
	var massResEl = document.getElementById("isMassRes");
	var taxRow = document.getElementById("salesTaxRow");
	if(massResEl.checked) {
		taxRow.style.display = "";		
	}
	else {
		taxRow.style.display = "none";
	}
	
	updateTotal();
}

function updateTax() {
	var subTotal = document.getElementById("subtotal").value;
	var taxEl = document.getElementById("tax");
	var taxVal = taxEl.value;
	var taxId = document.getElementById("tax_id").value;
	var massResEl = document.getElementById("isMassRes");

	if(taxId == "" && massResEl.checked) {
		if(subTotal != "") {
			subTotal = parseFloat(subTotal);
			var calcTax = subTotal * .05;
			taxVal = calcTax.toFixed(2);
		}
	}
	else
		taxVal = "";
		
	taxEl.value = taxVal;
}

String.prototype.trim = function() {
a = this.replace(/^\s+/, '');
return a.replace(/\s+$/, '');
};



/**
DEPRECATED
This function is called primarily by the onChange method for an item to updated the total cost for an item, including shipping, once an item quantity has been modified.
**/
function getItemTotalOld(name) {
	try {
		var quantity = parseFloat(document.getElementById(name + "quantity").value);
	}
	catch(exception) {
		alert("Please set the quantity to a numeric value");
	}
	var cost = parseFloat(document.getElementById(name + "cost").value);
	var shipping = parseFloat(document.getElementById(name + "shipping").value);
	var total = quantity * (cost + shipping);
	total = total.toFixed(2);
	var newNode = document.createTextNode(total);
	var totalElement = document.getElementById(name + "total");
	totalElement.value = total;
	calcSubTotal();
}

