/** HANDLES SHOPPING CART ADD BUTTON (BLINKS THE SHOPPING CART MENU ITEM WHEN SOMETHING IS ADDED) **/
var currColor   = "";
var cycles      = 0;
var timer;
var interval    = 125;
var running     = false;

function fnBlinkCart() {
    var cartArea    = findDom("tdShoppingCart",0);

    if ( currColor == "" ) {
        // set class to menuitemhvr
        cartArea.style.backgroundColor	= "#D8E673";
        currColor           = "brownish";
    } else if ( currColor == "brownish" ) {
        // set class to menuitemwhite
        cartArea.style.backgroundColor	= "#ffffff";
        currColor           = "whiteish";
    } else if ( currColor == "whiteish" ) {
        // set class to menuitemdarkgrey
        cartArea.style.backgroundColor	= "#c4c4c4";
        currColor           = "darkgreyish";
    } else {
        // reset class to menuitem
        currColor           = "";
        cartArea.style.backgroundColor	= "#ffffff";
        cycles = cycles + 1;
        if ( cycles == 2 ) {
            fnEndBlink();
            cycles = 0;
        }
    }

    
    var cartArea    = null;
}

function fnORIGBlinkCart() {
    var cartArea    = findDom("tdShoppingCart",0);

    if ( currColor == "" ) {
        // set class to menuitemhvr
        cartArea.className  = "menuitemhvr";
        currColor           = "greenish";
    } else if ( currColor == "greenish" ) {
        // set class to menuitemwhite
        cartArea.className  = "menuitemwhite";
        currColor           = "whiteish";
    } else if ( currColor == "whiteish" ) {
        // set class to menuitemdarkgrey
        cartArea.className  = "menuitemdarkgrey";
        currColor           = "darkgreyish";
    } else {
        // reset class to menuitem
        currColor           = "";
        cartArea.className  = "menuitem";
        cycles = cycles + 1;
        if ( cycles == 2 ) {
            fnEndBlink();
            cycles = 0;
        }
    }

    
    var cartArea    = null;
}

function fnStartBlink() {
    timer   = setInterval("fnBlinkCart()",interval);
    running = true;
}

function fnEndBlink() {
    clearTimeout(timer);
    running = false;
}

function fnGetIsRunning() {
    return running;
}
/** END HANDLING SHOPPING CART ADD BUTTON (BLINKS THE SHOPPING CART MENU ITEM WHEN SOMETHING IS ADDED) **/

/** THIS FUNCTION IS USED TO ADD ITEMS TO A CUSTOMER'S SHOPPING CART ON THE SHOP PAGE **/
function fnAddToCart(counterValue) {
    var prodIdn     = findDom("hdnProductIdn" + counterValue,0);
    var prodQty     = findDom("txtQty" + counterValue,0);
    var prodPrice   = findDom("hdnProductPrice" + counterValue,0);
    var prodName    = findDom("hdnProductName" + counterValue,0);
    var cartIdns    = findDom("hdnCartProductIdns",0);
    var cartPrices  = findDom("hdnCartProductPrices",0);
    var cartQties   = findDom("hdnCartProductQuantities",0);
    var cartNames   = findDom("hdnCartProductNames",0);
    
    //alert(findDom("tdShoppingCart",0).childNodes[0].childNodes[0].nodeValue);
    //findDom("tdShoppingCart",0).childNodes[0].nodeValue = "changed";
    
    if ( trim(prodQty.value) == "" ) {
        alert("Please enter a quantity amount to add this product to your shopping cart.");
        prodQty.focus();
        return;
    }

    // check to see if shopping cart fields already have anything
    if ( trim(cartIdns.value) != "" ) {
        //append current selection to shopping cart
        cartIdns.value  += "|" + trim(prodIdn.value);
        cartPrices.value+= "|" + trim(prodPrice.value);
        cartQties.value += "|" + trim(prodQty.value);
        cartNames.value += "|" + trim(prodName.value);
        
        // get total price
        aryPrices   = trim(cartPrices.value).split("|");
        aryQty      = trim(cartQties.value).split("|");
        totalPrice  = 0.00;
        idx         = 0;
        num         = aryPrices.length;
        
        while ( idx < num ) {
            totalPrice  += eval(aryPrices[idx]) * eval(aryQty[idx]);
            idx += 1;
        }
    } else {
        //add current selection to shopping cart
        cartIdns.value  = trim(prodIdn.value);
        cartPrices.value= trim(prodPrice.value);
        cartQties.value = trim(prodQty.value);
        cartNames.value = trim(prodName.value);
        
        totalPrice  = eval(cartPrices.value) * eval(cartQties.value);
    }

    // update the price for the shopping cart menu item
    //findDom("tdShoppingCart",0).childNodes[0].nodeValue = "Shopping Cart ($" + totalPrice + ")";
    findDom("tdShoppingCart",0).childNodes[0].childNodes[0].nodeValue = "Shopping Cart ($" + totalPrice + ")";

    // clear out the qty value
    prodQty.value   = "";

    //alert(prodIdn.value + " " + prodQty.value);
    //alert(findDom("tdShoppingCart",0).childNodes[0].nodeValue);
    
    // add the string values to the hidden fields
    

    var prodIdn     = null;
    var prodQty     = null;
    var prodPrice   = null;
    var prodName    = null;
    var cartIdns    = null;
    var cartPrices  = null;
    var cartQties   = null;
    var cartNames   = null;
    
    // blink cart area so that customer knows they added something
    fnStartBlink();
}

/** THIS FUNCTION IS USED TO UPDATE THE SHOPPING CART MENU ITEM PRICES **/
function fnUpdateShoppingCartMenuItem() {
    var cartIdns    = findDom("hdnCartProductIdns",0);
    var cartPrices  = findDom("hdnCartProductPrices",0);
    var cartQties   = findDom("hdnCartProductQuantities",0);
    strCartVals     = trim(cartIdns.value);
    
    // check to see if shopping cart fields already have anything
    if ( strCartVals == "" ) {
        return;
    }
    
    if ( strCartVals != "" ) {
        if ( strCartVals.indexOf("|") == -1 ) {
            // figure out the current total
            totalPrice  = eval(cartPrices.value) * eval(cartQties.value);
        } else {
            // split into array
            aryPrices   = trim(cartPrices.value).split("|");
            aryQty      = trim(cartQties.value).split("|");
            totalPrice  = 0.00;
            idx         = 0;
            num         = aryPrices.length;

            while ( idx < num ) {
                totalPrice  += eval(aryPrices[idx]) * eval(aryQty[idx]);
                idx += 1;
            }
        }
    }

    // update the price for the shopping cart menu item
    findDom("tdShoppingCart",0).childNodes[0].childNodes[0].nodeValue = "Shopping Cart ($" + totalPrice + ")";

    var cartIdns    = null;
    var cartPrices  = null;
    var cartQties   = null;
}

function fnORIGUpdateShoppingCartMenuItem() {
    var cartIdns    = findDom("hdnCartProductIdns",0);
    var cartPrices  = findDom("hdnCartProductPrices",0);
    var cartQties   = findDom("hdnCartProductQuantities",0);
    strCartVals     = trim(cartIdns.value);
    
    // check to see if shopping cart fields already have anything
    if ( strCartVals == "" ) {
        return;
    }
    
    if ( strCartVals != "" ) {
        if ( strCartVals.indexOf("|") == -1 ) {
            // figure out the current total
            totalPrice  = eval(cartPrices.value) * eval(cartQties.value);
        } else {
            // split into array
            aryPrices   = trim(cartPrices.value).split("|");
            aryQty      = trim(cartQties.value).split("|");
            totalPrice  = 0.00;
            idx         = 0;
            num         = aryPrices.length;

            while ( idx < num ) {
                totalPrice  += eval(aryPrices[idx]) * eval(aryQty[idx]);
                idx += 1;
            }
        }
    }

    // update the price for the shopping cart menu item
    findDom("tdShoppingCart",0).childNodes[0].nodeValue = "Shopping Cart ($" + totalPrice + ")";

    var cartIdns    = null;
    var cartPrices  = null;
    var cartQties   = null;
}

/** UPDATES THE CART AT THE CHECKOUT PAGE **/
function fnUpdateCheckoutCart() {
    var tblCheckout = findDom("tblCartCheckoutContents",0);
    var cartIdns    = findDom("hdnCartProductIdns",0);
    var cartPrices  = findDom("hdnCartProductPrices",0);
    var cartQties   = findDom("hdnCartProductQuantities",0);
    var cartNames   = findDom("hdnCartProductNames",0);
    
    numrows         = tblCheckout.rows.length;
    oldIdns         = cartIdns.value;
    oldPrices       = cartPrices.value;
    oldQties        = cartQties.value;
    oldNames        = cartNames.value;
    newIdns         = "";
    newPrices       = "";
    newQties        = "";
    newNames        = "";
    hasItems        = false;
    
    idx             = 0;
    while ( idx < numrows ) {
        // if this isn't a dummy row, then process it
        if ( tblCheckout.rows[idx].id != "dummyRow" ) {
            hasItems    = true;
            idn         = tblCheckout.rows[idx].getElementsByTagName("td")[3].getElementsByTagName("input")[0].value;
            nme         = tblCheckout.rows[idx].getElementsByTagName("td")[3].getElementsByTagName("input")[1].value;
            price       = tblCheckout.rows[idx].getElementsByTagName("td")[3].getElementsByTagName("input")[2].value;
            qty         = tblCheckout.rows[idx].getElementsByTagName("td")[3].getElementsByTagName("input")[3].value;
            
            // only update if qty value is >=1
            if ( fnIsNumeric(trim(qty)) && trim(qty) >= 1 ) {
                if ( newIdns == "" ) {
                    //newIdns     = tblCheckout.rows[idx].getElementsByTagName("td")[3].getElementsByTagName("input")[0].value;
                    newIdns     = idn;
                    newPrices   = price;
                    newQties    = qty;
                    newNames    = nme;
                } else {
                    newIdns     += "|" + idn;
                    newPrices   += "|" + price;
                    newQties    += "|" + qty;
                    newNames    += "|" + nme;
                }
            }
        }
        idx     = idx + 1;
    }
    if ( hasItems ) {
        // reset the actual hidden fields that hold the cart information across multiple pages
        cartIdns.value  = newIdns;
        cartPrices.value= newPrices;
        cartQties.value = newQties;
        cartNames.value = newNames;
    }
    
    var cartIdns    = null;
    var cartPrices  = null;
    var cartQties   = null;
    var cartNames   = null;
    var tblCheckout = null;
    
    if ( hasItems ) {
        // submit this form
        goToMenu("shopping_cart.htm");
    }
}
