//clearOrderMath.js was created by Julianne Harrington
//Acoclear Consulting Company, La Jolla, CA   Toll-free: 1.877.738.7111
//jharrington@acoclear.com
//Copyright 1999, 2000, 2001. All rights reserved.

function doExtPrice(whichItem)
{
    var theItem = eval("document.bfypordermail.item" + whichItem);
    var theQty = eval("document.bfypordermail.qty" + whichItem + ".value");

    if (isNaN(theQty))
    {
        alert ("Please enter a number in the quantity field.");
        eval("document.bfypordermail.qty" + whichItem + ".focus()");
        return;
    }

    if (theQty == 0 )
    {
        var theExtPriceField = eval("document.bfypordermail.ext" + whichItem);
        theExtPriceField.value = "0.00";
        ComputeTotals();
        return;
    }

    var theItemStr = theItem.options[theItem.selectedIndex].text;
    var thePrice = theItemStr.split("$")[1];
    if (thePrice)
    {
        if (thePrice.indexOf(".") >= 0)
        {
            var lastDigit = thePrice.indexOf(".") + 3;
            thePrice = thePrice.substr(0, lastDigit);
        }

        var extPriceCalc = thePrice * theQty;
        var extPriceCalcStr = new String(extPriceCalc);
        var extPriceStr = AdjustFloatingPointMath(extPriceCalcStr);

        var theExtPriceField = eval("document.bfypordermail.ext" + whichItem);
        theExtPriceField.value = extPriceStr;

        theExtPriceField.blur();
        ComputeTotals();
    }
}

function recalcTax()
{
    ComputeTotals();
}

function ComputeTotals()
{
    var subTotal = ComputeSubtotal();

    theTaxField = document.bfypordermail.ext082;
    theStateField = document.bfypordermail.state;
    theStateFlag = document.bfypordermail.stateFlag;

    if (theStateField.selectedIndex == 0)
    {
        alert("Please enter your STATE before proceeding to order. Thank you.");
        theStateField.focus();
	theStateFlag.src="/ima/reddot.gif";
        return;
    }
    
    if (theStateField.options[theStateField.selectedIndex].value == "CA")
    {
        var estTax = subTotal * 0.08;
    }
    else
    {
        estTax = 0;
    }
    estTax = SetDecimalLength(estTax);
    theTaxField.value = estTax;

    theShippingField = document.bfypordermail.ext083;
    var estShip = Math.max(5, Math.ceil(subTotal * 0.09));
    estShip = SetDecimalLength(estShip);
    theShippingField.value = estShip;

    theTotalField = document.bfypordermail.ext0832;
    estTot = SetDecimalLength(eval(subTotal) + eval(estTax) + eval(estShip));
    theTotalField.value = estTot;

}

function ComputeSubtotal()
{
    var subTotal = 0;
    var curVal;
    var i;
    
    for (i = 1; i < 9; i++)
    {
        var theExtPriceField = eval("document.bfypordermail.ext0" + i);
        if (!theExtPriceField.value) break;

        curVal = eval(theExtPriceField.value);
        subTotal += curVal;
    }
    return subTotal;
}

function AdjustFloatingPointMath(extPriceStr)
{
    extPriceStr = SetDecimalLength(extPriceStr);

    // Adjust for floating point weirdness. All prices must end in 5 or 0.
    var lengthMinusOne = extPriceStr.length - 1;
    var lengthMinusTwo = extPriceStr.length - 2;
    if (extPriceStr.charAt(lengthMinusOne) == "0")
    {
        return extPriceStr;
    }
    else if (extPriceStr.charAt(lengthMinusOne) != "5")
    {
        var distFrom5 = Math.abs(5 - extPriceStr.charAt(lengthMinusOne));
        var distFrom0 = Math.abs(extPriceStr.charAt(lengthMinusOne) - 10);
        if (distFrom5 < distFrom0)
        {
            var tmpStr = new String(extPriceStr.substr(0, lengthMinusOne));
            extPriceStr = tmpStr + "5";
        }
        else 
        {  
            var tmpStr = new String(extPriceStr.substr(0, lengthMinusTwo));
            if (extPriceStr.charAt(lengthMinusOne) > 5)
            {
                tmpStr += (eval(extPriceStr.charAt(lengthMinusTwo)) + 1);
            }
            else
            {
                tmpStr += extPriceStr.charAt(lengthMinusTwo);
            }
            tmpStr += "0";
            extPriceStr = tmpStr;
        }
    }

    return extPriceStr;
}

function SetDecimalLength(theAmountNum)
{
    if (theAmountNum == 0) return "0.00";

    var theAmount = new String(theAmountNum);
    if (theAmount.indexOf(".") >= 0)
    {
        var lastDigit = theAmount.indexOf(".") + 3;
        theAmount = theAmount.substr(0, lastDigit);
    }

    var lengthMinusOne = theAmount.length - 1;
    var lengthMinusTwo = theAmount.length - 2;

    var zeroStr = new String(Math.round(eval(theAmount) * 100));
    if (zeroStr.charAt(zeroStr.length - 1) == "0")
    {
        if (zeroStr.charAt(zeroStr.length - 2) == "0")
        {
            theAmount += ".00";
        }
        else
        {
            theAmount += "0";
        }
    }
    return theAmount;
}

