/*
 * Adds an item, identified by its product code, 
 * to the shopping cart
 * itemCode - product code of the item to add.
 */
function executeCartRequest() {
  cartRequest('viewCart',null,null);
}

function executeAddCartRequest(productId,quantity) {
  cartRequest('addToCart',productId,quantity);
}

function executeRemoveItemRequest(productId) {
  cartRequest('removeItem',productId,null);
}

function executeClearCartRequest() {
  cartRequest('clearCart',null,null);
}

function cartRequest(action,productId,quantity) {

  // Obtain an XMLHttpRequest instance
  var req = newXMLHttpRequest();

  // Set the handler function to receive callback notifications
  // from the request object
  var handlerFunction = getReadyStateHandler(req, displayCartList);
  req.onreadystatechange = handlerFunction;
  
  // Open an HTTP POST connection to the shopping cart servlet.
  // Third parameter specifies request is asynchronous.
  req.open("POST", "cart.do", true);

  // Specify that the body of the request contains form data
  req.setRequestHeader("Content-Type", 
                       "application/x-www-form-urlencoded");

  // Send form encoded data stating that I want to add the 
  // specified item to the cart.
  if(productId != null)
    req.send("action="+action + "&product.id="+productId + "&quantity="+quantity);
  else
    req.send("action="+action);
    
}

function displayCartList(responseXML) {
    // Get the root "categories" element from the document
    var root = responseXML.getElementsByTagName("order")[0];

    // Clear the HTML list used to display the cart contents
    setCartContents("");
    var buffer = "";

    // Loop over the items in the cart
    var itemsCount = root.getElementsByTagName("item-count")[0].firstChild.nodeValue;
    var itemsTotal = root.getElementsByTagName("total")[0].firstChild.nodeValue;
    buffer += "<p>You cart contains <b>" + itemsCount + "</b> items (<b>" + itemsTotal + "</b>).</p>";
    
    //built cart
    verticalOffset = -260;
    slide_out_speed = 1000;
    slide_in_speed = 1000;
    if(itemsCount > 0) {
        buffer += "<div id='cartItemsPanel'><table cellpadding='2' cellspacing='1' border='0' width='100%'>";
        buffer += "<tr><td class='headerCell'>Product</td><td class='headerCell'>Quantity</td><td class='headerCell'>Price</td><td class='headerCell'>Remove</td></tr>";
        for (var i = 0 ; i < itemsCount ; i++) {
            verticalOffset -= 50;
            slide_in_speed += 50;
            slide_out_speed += 50;
            var item = root.getElementsByTagName("items")[0].getElementsByTagName("order-item")[i];
            var productId = item.getElementsByTagName("id")[0].firstChild.nodeValue;
            var productName = item.getElementsByTagName("name")[0].firstChild.nodeValue;
            var quantity = item.getElementsByTagName("quantity")[0].firstChild.nodeValue;
            var price =item.getElementsByTagName("price")[0].firstChild.nodeValue;
            buffer += "<tr><td valign='top' align='left' class=''>" + productName + "</td>";
            buffer += "<td valign='top' align='center' class=''>" + quantity + "</td><td valign='top' align='center' class=''>" + price + "</td>";
            buffer += "<td valign='top' align='center' class=''><a href='javascript:removeProduct("+productId+");'><img src='images/acf_delete.gif' alt='Remove " + productName + "?'/></a></td></tr>";
        }
         buffer += "</table></div>";
    }
    
    buffer += "<p><hr/><div  align='center'><a href='javascript:cart();'>Hide Cart</a>";
    if(itemsCount > 0)
        buffer += " | <a href='javascript:cartEmpty();'>Empty Cart</a> | <a href='customerLogin.jsp'>Checkout</a>";
    buffer += "</div></p>";
    

    // Update the cart's total using the value from the cart document
    setCartContents(buffer);
}

function setCartContents(html) {
    var contents = document.getElementById(cartcontentDiv);
    contents.innerHTML = html;
}

function setItemsContents(html) {
    var contents = document.getElementById("cartItemsPanel");
    contents.innerHTML = html;
}

/*************************************************************************
  This code is from Dynamic Web Coding at www.dyn-web.com
  Copyright 2002-4 by Sharon Paine 
  See Terms of Use at www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!
*************************************************************************/

// change speed of slide here
var slide_in_speed = 1000;	// millisecond duration of slide into view
var slide_out_speed = 1000;// millisecond duration of slide out of view

var shoppingCart;
var cartOut = false;
var initialized = false;
var verticalOffset = -260;

var cartcontentDiv = "cartcontent";

var winW = document.body.offsetWidth;
if(winW % 2 == 1)
    winW = winW -1;

function init() {
  // Arguments to constructor: id, x, y, w, y
  shoppingCart = new dynObj(cartcontentDiv, winW/2, verticalOffset, 402, 0);
  shoppingCart.show();
  // Arguments to slideTo method: destination x, destination y, duration of slide, acceleration factor 
  // which should be number between -1 and 1 ( -1 full decelerated, 1 full accelerated, 0 linear, i.e. no acceleration)
  //shoppingCart.slideTo(500, 10, 1000, -1); 
  initialized = true;
}

function slideOut() {
    shoppingCart.slideTo(winW/2, 115, 1000, -1);
    cartOut = true;
}

function slideIn() {
    shoppingCart.slideTo(winW/2, verticalOffset, 1000, -1);
    setTimeout("setCartContents('')",500);
    cartOut = false;
}

function cart() {
    if(cartOut)
        slideIn();
    else {
        executeCartRequest();
        slideOut();
    }
}

function cartEmpty() {
    executeClearCartRequest();
    verticalOffset = -260;
    slide_out_speed = 1000;
    slide_in_speed = 1000;
}

function removeProduct(productId) {
    executeRemoveItemRequest(productId);
}

function addToCart(productId,quantity) {
        executeAddCartRequest(productId,quantity);
        slideOut();
}

//initialize
init();