
/* this function performs the physical updates to the fields */
function updateItem(quantityNode)
{
	try
	{
		var highlight = "#ffc0c0";
		var priceNode = quantityNode.parentNode.nextSibling.firstChild;
		var totalNode = priceNode.parentNode.nextSibling.firstChild;
		
		var quantity = quantityNode.value;
		var price = priceNode.getAttribute("value");
		var currentTotal = totalNode.getAttribute("value");
		var newTotal = price * quantity;
		
		quantity = Math.round(quantity);
		
		if (isNaN(newTotal)) {
			if (isNaN(price))
			{
				newTotal = "0.00";
			}
			else {
				quantityNode.style.background = highlight;
				newTotal = "-";
			}
		}
		else {
			quantityNode.style.background = "";
			var diff = newTotal - parseFloat(currentTotal);
			if (isNaN(diff)) diff = 0;
			updateSubtotal(diff);
			newTotal = toCurrency(newTotal);
		}
		
		totalNode.setAttribute("value", newTotal);
	}
	catch(e)
	{
		alert(e);
	}
}

function updateSubtotal(value)
{
	try
	{
		var currentSubTotal = parseFloat(document.forms[0].elements['dtotal'].value);
		if (isNaN(value)) value = 0;
		
		var newSubTotal = currentSubTotal + value;
		var grandTotal = newSubTotal * 1.1;
		var gst = grandTotal-newSubTotal;
		
		document.forms[0].elements['dtotal'].value = toCurrency(newSubTotal);
		// document.forms[0].elements['gst'].value = toCurrency(gst);
		// document.forms[0].elements['grandtotal'].value = toCurrency(grandTotal);
	}
	catch(e)
	{
		alert(e);
	}
}

/*function update(id) {

	var f = 0;
    var i = 1;
	var subtotal = 0;
	var discount = 100;
	var discounttotal = 0;
	var gsttotal = 0;
	var grandtotal = 0;
//	var highlight = "#ff6464";
	var highlight = "#ffc0c0";	

	
	while (i <= document.forms[0]th - 1) {
		//alert("test");
		alert (document.forms.0.total[1]);
		
		var quantity = document.forms[f].quantity[i].value;
		var price = document.forms[f].price[i].value;
		var total = 0.00;

		quantity = Math.round(quantity);
		total = toCurrency(price * quantity);

		if (!isNaN(total)) {
			document.forms[f].quantity[i].style.background = "";
			document.forms[f].price[i].style.background = "";
			document.forms[f].total[i].style.background = "";
			document.forms[f].quantity[i].parentNode.parentNode.style.background = "";
			subtotal = subtotal + parseFloat(total);
		} else {
			document.forms[f].quantity[i].style.background = highlight;
			document.forms[f].price[i].style.background = highlight;
			document.forms[f].total[i].style.background = highlight;
			document.forms[f].quantity[i].parentNode.parentNode.style.background = highlight;


			total = "-";
		}

		document.forms[f].total[i].value = total;

		i++;

	}

	discount = document.forms[f].discount.value;

	if ((isNaN(discount)) || (discount > 100) || (discount < 0)) {
		document.forms[f].discount.style.background = highlight;
		document.forms[f].discount.parentNode.parentNode.style.background = highlight;
		discount = 0;
	} else {
		document.forms[f].discount.style.background = "";
		document.forms[f].discount.parentNode.parentNode.style.background = "";
	}

	discounttotal = addDiscount(subtotal, discount);
	grandtotal = addGST(discounttotal);
	gsttotal = grandtotal - discounttotal;

//	document.forms[f].subtotal.value = toCurrency(subtotal);
	document.forms[f].dtotal.value = toCurrency(discounttotal - (subtotal) / 11);

	document.forms[f].gst.value = toCurrency(subtotal / 11);
	document.forms[f].grandtotal.value = toCurrency(subtotal);

}*/

/* calculates the GST for a given value */
function addGST(input) {
	input = toCurrency(input);
	var output = toCurrency(input * 1.10);
	return output;
}


function addDiscount(total, discount) {
	discount = (1 - (discount / 100));
	output = total * discount;
	return toCurrency(output);
}


/* I REALLY do not want to touch this code. it is the devil */
function toCurrency (input) {
	input += "";
	var original_input = input;
	if (input . charAt (0) == "$") input = input . substring (1, input . length); else if ( input . substring (0, 2) == "-$" || input . substring (0, 2) == "+$" ) input = input . charAt (0) + input . substring (2, input . length);
	var amount = parseFloat (input);
	
	if (isNaN (amount)) return original_input;
	
	amount = Math . round (100 * amount);
	
	var string;
	
	if (amount < 10) string = "00" + amount; else if (amount < 100) string = "0" + amount; else string = "" + amount;
	
			string = string . substring (0, string . length - 2) +"." + string . substring (string . length - 2, string . length);
	
			return string;
}

function display(number) {

    var f = number;
	var i = 1;
	
	while (i <= 5) {
		if (i == f)
			document.forms[f].style.display="";
		else
			document.forms[i].style.display="none";
		i++;
	}

	
	//}
	
}
