/**
 * This is a simple AJAX class to handle adding items to the shopping cart
 * Author: Josh VanderLinden
 * Date: 27 Jun 2007
 */

function handleResponse()
{
   var status = null;

   switch (Ajax.request.readyState)
   {
      case 0:
         status = 'Error!';
         break;
      case 4:
         eval(Ajax.request.responseText);
         // If the feedback contains a product ID and a quantity,
         if (feedback.productId > 0)
         {
            if (feedback.quantity > 0)
            {
               var qty = ge('minicartItem' + feedback.productId + 'Quantity');

               // If this element exists, the product is already in the cart
               if (qty)
               {
                  qty.innerHTML = feedback.quantity;
               }
               else
               {
                  var list = ge('miniCartItems');
                  if (list)
                  {
                     // Add this element to the list of items
                     list.innerHTML += '<div id="minicartItemContainer' + feedback.productId + '" class="new"><div class="itemName"><a href="?a=VP&amp;id=' + feedback.productId + '">' + feedback.productName + '</a></div><div class="itemQuantity">Quantity: <span id="minicartItem' + feedback.productId + 'Quantity">' + feedback.quantity + '</span></div></div>';
                  }
               }
               status = '<font color=red><b>Item added.<b></font>';

               var empty = ge('minicartEmpty');
               if (empty) empty.style.display = 'none';
            }
            else if (feedback.quantity <= 0)
            {
               // Should be removed from DOM, but it doesn't seem to like that
               var e = ge('minicartItemContainer' + feedback.productId);
               if (e) e.parentNode.removeChild(e);
               status = 'Item removed.';
            }
            status += '  <a href="?a=SC">view cart</a>';

            ge('p' + feedback.productId + 'Status').innerHTML = status;
         }
         else
         {
            status = 'Error adding product to cart.';
         }
         break;
      default:
         status = '...';
   }

//    if (feedback != null)
//    {
//       ge('p' + feedback.productId + 'Status').innerHTML = status;
//    }
}

function addToCart(productId)
{
   Ajax.post('index.php?a=XATC&id=' + productId + '&qty=' + ge('p' + productId + 'Qty').value + '&ajax=1' );
}

function ge(name)
{
   return document.getElementById(name);
}

