var temp;
var state = "AZ";
var salesTaxMultiplier = .067;

function calculate(id) {
	var total = eval("document.forms.orderform.T_" + id);
	var quantity = eval("document.forms.orderform.Q_" + id);
	var price = eval("document.forms.orderform.P_" + id);
	var grandTotal = document.forms.orderform.GT;
	var subTotal = document.forms.orderform.ST;
	var salesTax = document.forms.orderform.STX;
	var taxTotal = document.forms.orderform.TT;
	if (quantity.value != "") {
		quantity.value = parseInt(quantity.value);
		price.value = parseFloat(price.value);
		if (isNaN(quantity.value) || isNaN(price.value)){
			document.forms.orderform.reset();
			alert("Cannot Complete Computation - NonInteger Value Present")
			return false;
		}
		if (!adder(total, quantity, price, id, grandTotal, subTotal, salesTax, taxTotal)) {
			alert("Cannot Complete Computation - NonInteger Value Present");
			return false;
		}
	} else if (total.value != "") {
		total.value = parseFloat(total.value);
		if (!isNaN(total.value)) {
			quantity.value = 0;
			if (!adder(total, quantity, price, id, grandTotal, subTotal, salesTax, taxTotal)) {
				alert("Cannot Complete Computation - NonInteger Value Present");
				return false;
			}
		}
	}
}

function adder (total, quantity, price, id, grandTotal, subTotal, salesTax, taxTotal) {
	if (isOK(total, subTotal, quantity)) {
		total.value = currency(parseFloat(price.value) * parseFloat(quantity.value));
		if (quantity.value == 0) {
			subTotal.value = currency(parseFloat(subTotal.value) - parseFloat(total.value));
			total.value = 0;
		} else {
			subTotal.value = currency(parseFloat(total.value) + parseFloat(subTotal.value));
		}
		grandTotal.value = currency(subTotal.value);
		var check = tax(subTotal, salesTax, grandTotal, taxTotal);
		return true;
	} else {
		return false;
	}
}

function tax (subTotal, salesTax, grandTotal, taxTotal) {
	subTotal.value = parseFloat(subTotal.value);
	grandTotal.value = parseFloat(grandTotal.value);
	taxTotal.value = parseFloat(taxTotal.value);
	if (isNaN(subTotal.value) || isNaN(grandTotal.value) || isNaN(taxTotal.value)) {
		return false;
	}
	if (salesTax.options[salesTax.selectedIndex].value == state) {
		grandTotal.value = currency(subTotal.value * (1+salesTaxMultiplier));
		taxTotal.value = currency(subTotal.value * salesTaxMultiplier);
		return true;
	} else if (taxTotal.value != 0) {
		grandTotal.value = currency(grandTotal.value - taxTotal.value);
		taxTotal.value = 0;
		return true;
	}
	return false;
}

function isOK (total, subTotal, quantity) {
	total.value = parseFloat(total.value);
	quantity.value = parseFloat(quantity.value);	
	subTotal.value = parseFloat(subTotal.value);
	if (isNaN(total.value) || isNaN(subTotal.value) || isNaN(quantity.value)) {
		return false;
	}
	if (total.value != "" && total.value != "0") {
		if (!isNaN(total.value) && !isNaN(subTotal.value)) {
			subTotal.value = currency(parseFloat(subTotal.value) - parseFloat(total.value));
			return true;
		}
		return false;
	}
	return true;
}

function currency(anynum) {
//		alert("currency: " + anynum);
		anynum = "" + anynum;
		if (anynum.charAt(0) == "0") {
			var foo = anynum.length - 1;
			anynum = anynum.substring(1, foo);
		}
		anynum = eval(anynum)
        var intnum = parseInt(anynum)
        intnum = Math.abs(intnum)
        var intstr = ""+intnum
        //add comma in thousands place.
        //if (intnum >= 1000) {
        //        var intlen = intstr.length
        //        var temp1=parseInt(""+(intnum/1000))
        //        var temp2=intstr.substring(intlen-3,intlen)
        //        intstr = temp1+","+temp2
		//
        //}
        //if (intnum >= 1000000) {
        //        var intlen = intstr.length
        //        var temp1=parseInt(""+(intnum/1000000))
        //        var temp2=intstr.substring(intlen-7,intlen)
        //        intstr = temp1+","+temp2
		//
        //}
        var decnum = Math.abs(parseFloat(anynum)-parseInt(anynum)) //isolate decimal portion
        decnum = decnum * 100 // multiply decimal portion by 100.
        var decstr = "" + Math.abs(Math.round(decnum))
        if (decstr.length>2) {decstr=decstr.substring(0,2)}
        while (decstr.length < 2) {decstr="0"+decstr}
        var retval = intstr + "." + decstr 
        if (anynum < 0) {
                retval="("+retval+")";
        }
        return retval;
		//return "$" + retval;
}
