﻿// JScript File


function isArray(obj) {
    if (obj) 
    {
       if (obj.constructor.toString().indexOf("Array") == -1)
          return false;
       else
          return true;
    } 
    return true;
}

/*  ========================================================================
    This function is called from client side from the button is first clicked
    ========================================================================
*/
function FM_AddToCart_click ( elem_id, ajaxCallback, qtyElemId ) 
{
    
    // Set the image to waiting ... 
    var img_elem = document.getElementById(elem_id);
    if (img_elem )
    {
        img_elem.src = img_elem.src.replace(/AddToCart_save.gif/,'AddToCart_saving.gif');
        img_elem.disabled=true;
        img_elem.style.cursor = "default";
    }
    var qtyElem = document.getElementById(qtyElemId);
    var quantity = "1"; 
    if (qtyElem)
    {
        quantity = qtyElem.value;
    }
    
    ajaxCallback = ajaxCallback.replace(/%Qty%/,quantity);
    
    //alert (ajaxCallback);
    
    eval(ajaxCallback);
    return false;
}

 
/*  ========================================================================
    This function is called when after the XML postback caused 
    by the  click of the "Add to AddToCart" button 
    
    .. which calls the ASP.NET page using AJAX
    .. which calls this function 
    
    event_args is an array as follows:
    EArgs[0] = "added" if item was added to cart 
              "addtell-##" (## is the quantity added)  if it is the first time 
                added to cart in this session
    EArgs[1] = new img source 
    EArgs[2] = Cart Summary Message 
    
    event_context 
        contains the element id of the button 
        
    ========================================================================
*/
function FM_AddToCart_callback_handler (event_args, event_context) 
{ 
    var EArgs=1;
    try {    
        eval (event_args); 
        if (isArray(EArgs))
        {
            var img_elem = document.getElementById(event_context);
            if (img_elem  && EArgs[1] )
            {
                var addtell = false; 
                //alert (EArgs[0]);
                if ( EArgs[0].substring(0,7)== 'addtell')
                {
                    addtell = true;
                    qty = EArgs[0].substring(8); 
                }
                if (EArgs[0]=='added' || addtell) 
                {
                    img_elem.src = EArgs[1];
                    img_elem.disabled=true;
                    img_elem.style.cursor = "default";
                    
                    // disable the quantity 
                    var txt_qty_elem_id = event_context.replace (/btnAddToCart/,'txtQuantity');
                    var qty_elem = document.getElementById(txt_qty_elem_id);
                    
                    if (qty_elem)
                    {
                        qty_elem.style.display='none';
                        qty_elem.disabled=true;
                    }
                    
                    // update the cart summary 
                    var cart_summary_elem_id = 'dnn_dnnCART_lblCartSummary';
                    var cart_summary_elem = document.getElementById(cart_summary_elem_id);
                    
                    if (cart_summary_elem && EArgs[2])
                    {
                        cart_summary_elem.innerHTML = cart_summary_elem.innerHTML.replace(/\d+ items \(\d+ pts\)/,EArgs[2]);
                    }
                    else
                    {
                        if (addtell) 
                        {
                            alert (qty + ' item(s) were added to your cart. The Cart Summary will not be refreshed until you reload the page.');
                        }
                    }
                }
            }
        }
    }
    catch (err)
    {
        if (err.description )
            alert ('FM_AddToCart_callback_handler error:' + err.description);
        else
            alert ('FM_AddToCart_callback_handler error:' + err.message);
    }
} 


/*  ========================================================================
    This function is called if an error occurs
    ======================================================================== 
*/
function FM_AddToCart_error_handler (statusCodes) 
{ 
    alert ("An error occured adding the item to the Cart:" + statusCodes);
} 

