AJAX_TARGET = 'index.php?';

// make an AJAX call specifying the url and the respons handling function
function ajaxCall(url, f)
{
  var xmlHttp;
  try
  {
    // Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  }
  catch (e)
  {
    // Internet Explorer
    try
    {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      try
      {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  }

  xmlHttp.onreadystatechange = function()
  {
    if (xmlHttp.readyState == 4)
    {
      f(xmlHttp);
    }
  }

  xmlHttp.open("GET", url, true);
  xmlHttp.send(null);
}

// add product to cart
function add(offerid, aid)
{
  ajaxCall(AJAX_TARGET + "a=" + encodeURI(offerid) + "&asin=" + encodeURI(aid),
    function(xmlHttp)
    {
      targetDiv = document.getElementById('cart-content');
      targetDiv.innerHTML = xmlHttp.responseText;
    });
}

// remove product from cart
function remove(itemid)
{
  ajaxCall(AJAX_TARGET + "r=" + encodeURI(itemid),
    function(xmlHttp)
    {
      targetDiv = document.getElementById('cart-content');
      targetDiv.innerHTML = xmlHttp.responseText;
    });
}

// modify product quantity in cart
function modify(itemid, qty)
{
  ajaxCall(AJAX_TARGET + "mq=" + encodeURI(itemid) + "&q=" + encodeURI(qty),
    function(xmlHttp)
    {
      targetDiv = document.getElementById('cart-content');
      targetDiv.innerHTML = xmlHttp.responseText;
    });
}

// view cart content
function view()
{
  ajaxCall(AJAX_TARGET + "v",
    function(xmlHttp)
    {
      targetDiv = document.getElementById('cart-content');
      targetDiv.innerHTML = xmlHttp.responseText;
    });
}

// clear cart content
function clear()
{
  ajaxCall(AJAX_TARGET + "c",
    function(xmlHttp)
    {
      targetDiv = document.getElementById('cart-content');
      targetDiv.innerHTML = xmlHttp.responseText;
    });
}

