///////////////////////////////////////////////////////////////////////
// Modify Section: alter these values depending on Order Page content
var subMax = 10;                    // Set to number of line items (relative to 1) on Order Page (and, hence, in Arrays below)
taxRate = 0.00;                     // Set to .05 if MA selected
internationalHandlingRate = "";     // Set to "10.00" if International selected


// Array for Item Descriptions; note format is:
// "Item A Description","Item B Description", ..., "Item J Description"
var myDescr = new Array(
"Oil of Tara (1 oz.)",
"Oil of Tara Therapy Pack (4 oz.)",
"The Eyes of Tara (.5 oz.)",
"Eyes of Tara and Oil of Tara (1 oz.) Gift Set",
"The Face of Tara (1 oz.)",
"Face of Tara and Oil of Tara (1 oz.) Gift Set",
"The Touch of Tara (1 oz.)",
"Touch of Tara and Oil of Tara (1 oz.) Gift Set",
"World of Tara Gift Basket (1 lb.)",
"Ayurvedic Beauty Care Book (1 lb.)"
);

// Array for highlighted message next to Order Form description
var mySpecial = new Array(
"",
"",
"",
" SAVE $4.95! ",
"",
" SAVE $5.95!",
"",
" SAVE $4.95!",
" SAVE $11.00! ",
"");

// Array for individual unit prices
var myPrice = new Array(
19.95,
64.95,
24.95,
39.95,
21.95,
35.95,
14.95,
29.95,
69.95,
17.95);

// Array for shipping amt for 1st of each item
var myShip1 = new Array(
5.85,
5.85,
5.85,
5.85,
5.85,
5.85,
5.85,
5.85,
8.00,
5.85);

// Array for shipping amt for 2nd thru Nth of each item
var myShip2 = new Array(
3.75,
3.75,
3.75,
3.75,
3.75,
3.75,
3.75,
3.75,
3.75,
3.75);

var myAlpha = new Array("a","b","c","d","e","f","g","h","i","j");

var sub;
var grandTot, grandTotShipping, grandTotTax;
var taxTemp, taxRate, alphaTemp, alphaTemp2;
// End of Modify Section
///////////////////////////////////////////////////////////////////////



function paintPage() {
    alphaTemp = "my_price_label_" + myAlpha[0];
    document.getElementById(alphaTemp).innerHTML=toCurrency(myPrice[0],"");
    document.getElementById("my_price_label_b").innerHTML=toCurrency(myPrice[1],"");
    document.getElementById("my_price_label_c").innerHTML=toCurrency(myPrice[2],"");
    document.getElementById("my_price_label_d").innerHTML=toCurrency(myPrice[3],"");
    document.getElementById("my_price_label_e").innerHTML=toCurrency(myPrice[4],"");
    document.getElementById("my_price_label_f").innerHTML=toCurrency(myPrice[5],"");
    document.getElementById("my_price_label_g").innerHTML=toCurrency(myPrice[6],"");
    document.getElementById("my_price_label_h").innerHTML=toCurrency(myPrice[7],"");
    document.getElementById("my_price_label_i").innerHTML=toCurrency(myPrice[8],"");
    document.getElementById("my_price_label_j").innerHTML=toCurrency(myPrice[9],"");
    document.getElementById("my_descr_label_a").innerHTML="<nobr>" + myDescr[0] + "<span class=hilite>" + mySpecial[0] + "</nobr></span>";
    document.getElementById("my_descr_label_b").innerHTML="<nobr>" + myDescr[1] + "<span class=hilite>" + mySpecial[1] + "</nobr></span>";
    document.getElementById("my_descr_label_c").innerHTML="<nobr>" + myDescr[2] + "<span class=hilite>" + mySpecial[2] + "</nobr></span>";
    document.getElementById("my_descr_label_d").innerHTML="<nobr>" + myDescr[3] + "<span class=hilite>" + mySpecial[3] + "</nobr></span>";
    document.getElementById("my_descr_label_e").innerHTML="<nobr>" + myDescr[4] + "<span class=hilite>" + mySpecial[4] + "</nobr></span>";
    document.getElementById("my_descr_label_f").innerHTML="<nobr>" + myDescr[5] + "<span class=hilite>" + mySpecial[5] + "</nobr></span>";
    document.getElementById("my_descr_label_g").innerHTML="<nobr>" + myDescr[6] + "<span class=hilite>" + mySpecial[6] + "</nobr></span>";
    document.getElementById("my_descr_label_h").innerHTML="<nobr>" + myDescr[7] + "<span class=hilite>" + mySpecial[7] + "</nobr></span>";
    document.getElementById("my_descr_label_i").innerHTML="<nobr>" + myDescr[8] + "<span class=hilite>" + mySpecial[8] + "</nobr></span>";
    document.getElementById("my_descr_label_j").innerHTML="<nobr>" + myDescr[9] + "<span class=hilite>" + mySpecial[9] + "</nobr></span>";
}


function isNumberKey(evt)   {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}



function nullIfZero(field) {
    var temp;
    var tempNum = 0;
    if (document.getElementById(field).innerHTML == "0") {
        document.getElementById(field).innerHTML="";
    }
    else {
        tempNum = document.getElementById(field).innerHTML;
        document.getElementById(field).innerHTML = toCurrency(tempNum,"");
    }
}


// toCurrency formats the on-screen totals to proper currency format
function toCurrency(val,currency) {
    val=(val+"").replace(/\,/g,"");
    return  ((currency? currency:"") + (Math.round(val*100) + (val<0?-0.1:+0.1))/ 100).replace(/(.*\.\d\d)\d*/,'$1');
}


function calcTax() {
    taxRate = 0.00;
    if (document.getElementById("my_tax").checked) {
        // USA - MA selected
        taxRate = 0.05;
    }
    calcTot(document.getElementById("myForm"));
}


function calcHandling() {
    internationalHandlingRate = "";
    if (document.getElementById("my_handling").checked) {
        // International
        internationalHandlingRate = "10.00";
    }
    calcTot(document.getElementById("myForm"));
}



function buildOrder(form) {
    //alert ("buildOrder started");
    var pp, loopCtr;
    var sub;
    var valueTemp, shipping, shipping2;
    loopCtr = 0;

    if (((form.my_qty_line_a.value == "0") || (form.my_qty_line_a.value == "")) &
        ((form.my_qty_line_b.value == "0") || (form.my_qty_line_b.value == "")) &
        ((form.my_qty_line_c.value == "0") || (form.my_qty_line_c.value == "")) &
        ((form.my_qty_line_d.value == "0") || (form.my_qty_line_d.value == "")) &
        ((form.my_qty_line_e.value == "0") || (form.my_qty_line_e.value == "")) &
        ((form.my_qty_line_f.value == "0") || (form.my_qty_line_f.value == "")) &
        ((form.my_qty_line_g.value == "0") || (form.my_qty_line_g.value == "")) &
        ((form.my_qty_line_h.value == "0") || (form.my_qty_line_h.value == "")) &
        ((form.my_qty_line_i.value == "0") || (form.my_qty_line_i.value == "")) &
        ((form.my_qty_line_j.value == "0") || (form.my_qty_line_j.value == "")))    {
            alert ("Note: please enter a Quantity for one or more items before clicking CHECKOUT button");
            return false;
    }
    else {
        //alert ("at least one qty is nonzero, proceeding with further checking");
    }

    //alert ("buildOrder-1");
    // We are about to dynamically generate <INPUT TYPE=HIDDEN...> fields (to send to PayPal)
    // for every incoming item that has nonzero (and nonnull) quantity field.
    //
    // First init "pp" in which we will build these PayPal-targeted <INPUT TYPE=HIDDEN> fields
    pp = "";
    document.getElementById("OrderSpan").innerHTML = pp;

    // Loop thru each incoming quantity field (one on each line of the Order Form)
    // and if quantity is nonnull and nonzero then calculate quantity * price and place
    // result in that line's subtotal field
    //
    // Note use of the "eval" command to dynamically locate the quantity field on each line
    // which has an alpha suffix ("a", "b", etc.) appended for uniqueness
    //
    // Note Handling Charge is applied to first item in cart only
    //
    // Loop will thus process "my_qty_line_a", "my_qty_line_b", ..., "my_qty_line_x"
    // for as many lines as subMax indicates
    for (sub = 0; sub < subMax; sub++)  {
        valueTemp = eval("form.my_qty_line_" + myAlpha[sub] + ".value");
        if ((valueTemp != "0") && (valueTemp != "")) {
            shipping = myShip1[sub];
            shipping2 = myShip2[sub];
            //calcTaxAndHandling();
            taxTemp = toCurrency(myPrice[sub]*taxRate,"");
            loopCtr++;
            pp = pp + "<input type='hidden' name='item_name_" + loopCtr + "' value='" + myDescr[sub] + "'>";
            pp = pp + "<input type='hidden' name='item_number_" + loopCtr + "' value='" + myDescr[sub] + "'>";
            pp = pp + "<input type='hidden' name='amount_" + loopCtr + "' value='" + myPrice[sub] + "'>";
            pp = pp + "<input type='hidden' name='quantity_" + loopCtr + "' value='" + valueTemp + "'>";
            pp = pp + "<input type='hidden' name='shipping_" + loopCtr + "' value='" + shipping + "'>";
            pp = pp + "<input type='hidden' name='shipping2_" + loopCtr + "' value='" + shipping2 + "'>";
            pp = pp + "<input type='hidden' name='tax_" + loopCtr + "' value='" + taxTemp + "'>";
            // Only include 'handling rate' for 1st entry only (hence check for loopCtr==1)
            if (loopCtr == 1) {
                pp = pp + "<input type='hidden' name='handling_1' value='" + internationalHandlingRate + "'>";
            }
            document.getElementById("OrderSpan").innerHTML += pp;
        }
    }
    calcTot(form);
    //alert ("Internal test datadump:" + pp);
    return true;
}



function calcTot(form) {
    // Now calc this form's totals by category, including:
    //
    //  - subtotal of all price x qty
    //  - subtotal of all shipping
    //  - subtotal of all tax
    //
    // Do this by looping thru each line and recalculating this lineitem's values
    // for each of these categories and adding to "subtot" and "grandtot" fields
    var tempQty = 0;
    var tempQtyShip1, tempQtyShip2, tempHandle;
    var valueTemp, shipping, shipping2, shippingQty, shippingQty2;
    var subTot, subTotShipping, subTotTax, subTotTaxString;

    //alert ("calcTot started, subMax=[" + subMax + "], form=[" + form + "]");

    tempHandle = internationalHandlingRate*1;   // converts string to num
    subTot = 0;                 // line item's subtotal qty x price
    subTotShipping = 0;         // line item's subtotal shipping (shipping x shippingQty) + (shipping2 x shippingQty2)
    subTotTax = 0;              // line item's subtotal tax
    grandTot = 0;               // running total qty x price
    grandTotShipping = 0;       // running total shipping (shipping x shippingQty) + (shipping2 x shippingQty2)
    grandTotTax = 0;            // running total tax
    grandGrandTot = 0;          // bottom line total
    for (sub = 0; sub < subMax; sub++)  {
        shipping = 0;
        shipping2 = 0;
        shippingQty = 0;        // no. items to be shipped at 'shipping' rate (always 1)
        shippingQty2 = 0;       // no. items to be shipped at 'shipping2' rate (remainder)
        valueTemp = eval("form.my_qty_line_" + myAlpha[sub] + ".value");
        //alert ("calcTot valueTemp=[" + valueTemp + "]");
        if ((valueTemp != "0") && (valueTemp != "")) {
            shipping = myShip1[sub];
            shipping2 = myShip2[sub];
            // since we have a different shipping rate for the 1st item vs. the 2nd-thru-Nth item
            // then we need to determine what the qty for the latter is
            shippingQty = 1;    // earlier 'if' guarantees we have at least qty of 1 here
            if (valueTemp > 1) {
                shippingQty2 = (valueTemp - 1); // result = qty to ship at shipping2 rate
            }
            //calcTax();
            //calcHandling();

            subTotTaxTemp = taxRate*myPrice[sub]*(shippingQty + shippingQty2);    // lineitem tot tax = rate x price x qty
            subTotTaxString = toCurrency(subTotTaxTemp, "");    // convert lineitem tot tax to string

            subTot = (valueTemp*myPrice[sub]);     // subtot for this lineitem's $ total (qty x price)
            subTotTax = subTotTaxTemp;             // subtot for this lineitem's tax total
            subTotTax = Math.round(subTotTax*100)/100
            subTotShipping = (shipping*shippingQty) + (shipping2*shippingQty2);
                                                    // subtot for this lineitem's shipping total

            grandTot += subTot;
            grandTotTax += subTotTax;
            grandTotShipping += subTotShipping;
            grandGrandTot += (subTot + subTotTax + subTotShipping);
            //alert ("Sub=[" + sub + "], shippingQty=[" + shippingQty + "], shippingQty2=[" + shippingQty2 + "], shipping=[" + shipping + "], shipping2=[" + shipping2 + "], subTot=[" + subTot + "], subTotShipping=[" + subTotShipping + "], subTotTax=[" + subTotTax + "], grandTot=[" + grandTot + "], grandTotShipping=[" + grandTotShipping + "], grandTotTax=[" + grandTotTax + "]");
        }
    }

    if (document.getElementById("my_handling").checked) {
        grandGrandTot += tempHandle;        // last step is to add handling, if any
    }

    // move totals to screen
    document.getElementById("my_subtot_label").innerHTML = toCurrency(grandTot, "");
    document.getElementById("my_tax_label").innerHTML = toCurrency(grandTotTax, "");
    document.getElementById("my_shipping_label").innerHTML = toCurrency(grandTotShipping, "");
    document.getElementById("my_handling_label").innerHTML = toCurrency(internationalHandlingRate, "");
    document.getElementById("my_grandtot_label").innerHTML = toCurrency(grandGrandTot, "");
}


