
/**
 * Ampdraw - functions_cart.js
 *
 * Javascript functions to support the shopping cart
 * processes.
 *
 * @author     Shane Archer <futureal@rctech.net>
 * @copyright  2005-2007 RC Tech, LLC
 * @version    svn: $Revision:$ $Date:$
 * @package    AmpdrawPublic
 */

/**
 * Do the labor, meaning make the call via AJAX to add a
 * product to the shopping cart.
 */
function addToCart(productid,quantity,map) {

  var js_imageContainer = document.getElementById("addToCart_" + productid);
  var realQuantity = 1;

  // figure out quantity
  if (quantity > 0) {

    realQuantity = quantity;

  } else {

    var js_addToCartValue = document.getElementById("addToCartValue[" + productid + "]");

    if (validateInt(js_addToCartValue.value)) {
      realQuantity = js_addToCartValue.value;
    }

  }

  // save the old button and whatnot
  var oldButton = js_imageContainer.cloneNode(true);

  // create a preloader
  var js_preloader = createPreloader(js_imageContainer.style.textAlign);

  // clear the element and then attach the preloader
  clearElement(js_imageContainer);
  js_imageContainer.appendChild(js_preloader);

  // check to see if we need to include session in call
  if (sId != "") {
    var sessionString = "&s=" + sId;
  } else {
    var sessionString = "";
  }

  // init the ajax and make the call
  newAjaxCall();

  http_request.onreadystatechange = (function() {

    // need to bind and save this
    var button = oldButton;

    return function() {

      doAddToCart(productid,realQuantity,button,map);

    };

  })();

  http_request.open("GET", "/cart.php?amp_action=do_ajax_addtocart" + sessionString + "&productid=" + productid + "&quantity=" + realQuantity);
  http_request.send(null);

}

/**
 * Add a product to the shopping cart.
 */
function doAddToCart(productId,quantity,data,map) {

  // only do something if the request is complete
  if (http_request.readyState == 4) {

    // check for HTTP status code 200
    if (http_request.status == 200) {

      var response = http_request.responseText.split("|");

      if (response[0] == "ok") {

        var js_imageContainer = document.getElementById("addToCart_" + productId);

        // check to see if we need to include session in any links
        if (sId != "") {
          var sessionString = "&s=" + sId;
        } else {
          var sessionString = "";
        }

        // clear the container element of the preloader
        clearElement(js_imageContainer);

        // update the map price if necessary
        if (map == 'true') {

          document.getElementById('priceContainerMAP_' + productId).style.display = 'none';
          document.getElementById('priceContainerNoMAP_' + productId).style.display = 'block';

        }

        // create the link to the cart
        var js_cartLink = document.createElement("a");
        js_cartLink.setAttribute("href","/cart.php?amp_action=view" + sessionString);
        js_cartLink.appendChild(document.createTextNode("shopping cart"));

        // assemble the text into a container div
        var js_textContainer = document.createElement("div");
        js_textContainer.style.textAlign = js_imageContainer.style.textAlign;

        // fix for browse pages
        if (js_imageContainer.style.textAlign == "center") {
          js_textContainer.style.width = "90%";
        }

        js_textContainer.style.fontWeight = "bold";
        js_textContainer.appendChild(document.createTextNode("This item is in your "));
        js_textContainer.appendChild(js_cartLink);
        js_textContainer.appendChild(document.createTextNode("."));

        // append the entire thing to the main container
        js_imageContainer.appendChild(js_textContainer);

        // now it's time to update the cart string
        var js_cartString = document.getElementById("cartString");
        clearElement(js_cartString);

        var js_cartStringLink = document.createElement("a");
        js_cartStringLink.setAttribute("href","cart.php?amp_action=view" + sessionString);
        js_cartStringLink.appendChild(document.createTextNode(response[2] + " item" + (parseInt(response[2]) == 1 ? "" : "s")));

        js_cartString.appendChild(document.createTextNode("Cart: "));
        js_cartString.appendChild(js_cartStringLink);
        js_cartString.appendChild(document.createTextNode(" ($" + response[3] + ")"));

        // and finally, update and display the sidebar
        var js_cartSidebarItems = document.getElementById("cartSidebarItems");
        clearElement(js_cartSidebarItems);
        js_cartSidebarItems.appendChild(document.createTextNode(response[2] + " item" + (parseInt(response[2]) == 1 ? "" : "s")));

        var js_cartSidebarTotal = document.getElementById("cartSidebarTotal");
        clearElement(js_cartSidebarTotal);
        js_cartSidebarTotal.appendChild(document.createTextNode(response[3]));

        var js_cartSidebar = document.getElementById("cartSidebar");

        if (js_cartSidebar.style.display != "block") {
          js_cartSidebar.style.display = "block";
        }

      } else {

        alert("Error: " + response[1]);

        // need to remove the preloader image and restore the state
        var js_imageContainer = document.getElementById('addToCart_' + productId);

        clearElement(js_imageContainer);

        js_imageContainer.parentNode.replaceChild(data,js_imageContainer);

      }

    }

  }

}