/*
  AJAX Shopping Cart Functions
  M.Miner - Invoke Media - Aug 28, 2007
*/

//********************************************************************************

function AddToCart(varAction, varProductID, varQuantity)
{
	varAction = varAction || ""
	varProductID = varProductID || ""
	//varEATSID = varEATSID || ""

  if (varAction == '' || varProductID == '')
  {
	  //alert('Missing "eatsid" parameter to function AddToCart');
  }
  else {
	// Retrieve Ajax Feeds
	  //varQuantity = document.getElementById("quantity" + varProductID).value;
	  varQuantity = varQuantity || "0";

      $.ajax({
          url: 'ajax_cart_functions.php',
          type: 'post',
          data: 'action='+varAction+'&products_id='+varProductID+'&quantity='+varQuantity,
          success: CartSuccess,
          error: CartError
      });
  }
}

function RemoveFromCart(varAction, varProductID)
{
	varAction = varAction || ""
	varProductID = varProductID || ""
	//varEATSID = varEATSID || ""

  if (varAction == '' || varProductID == '')
  {
	  //alert('Missing "eatsid" parameter to function RemoveFromCart');
  }
  else {
	// Retrieve Ajax Feeds
	  varQuantity = "0";

      $.ajax({
          url: 'ajax_cart_functions.php',
          type: 'post',
          data: 'action='+varAction+'&products_id='+varProductID+'&quantity='+varQuantity,
          success: CartSuccess,
          error: CartError
      });
  }
}

function getCart()
{
  varAction = "ajax_get_cart";
  //varEATSID = varEATSID || "";

  if (varAction == '')
  {
	  //alert('Missing "eatsid" parameter to function AddToCart');
  }
  else {
	  // Retrieve Ajax Feeds
      $.ajax({
          url: 'ajax_cart_functions.php',
          type: 'post',
          data: 'action='+varAction,
          success: CartSuccess,
          error: CartError
      });
  }
}

function CartNameValuePairs(nameValuePair)
{
  var trimspaces = /(\s)+/;
  var returnData = new Array();
  var pairs      = nameValuePair.split('%&%');
  
  for (var i = 0; i < pairs.length; i++)
  {
    var pair = pairs[i].split('%=%');
    returnData[pair[0].replace(trimspaces, "")] = pair[1];
  }
  
  return returnData;
}

function CartSuccess(requestObj)
{
	var returnData = CartNameValuePairs(requestObj);
	document.getElementById("cartboxContents").innerHTML = returnData['code'];
	setBodyHeightToCartHeight(parseInt(returnData['cartBottomOffset']));

	if(returnData['error'])
	{
		alert(returnData['error'].replace(/#/g,"\n"));
		return;
	}
	
		elements = document.getElementsByName("quantity" + returnData['pid'] );
		for (i = 0; i < elements.length; i++) {
			elements[i].value = returnData['quantity'];
			$(elements[i]).effect("highlight", {}, 1000);
		}
		
	setCartOffset();
}

function CartError()
{
}


//******************************************************************************
		 function findPos(obj) {
			var curleft = curtop = 0;
			if (obj.offsetParent) {
				curleft = obj.offsetLeft
				curtop = obj.offsetTop
				while (obj = obj.offsetParent) {
					curleft += obj.offsetLeft
					curtop += obj.offsetTop
				}
			}
			return [curleft,curtop];
		 }
         function setBodyHeightToCartHeight(bottomOffset) {
                 // set the height of the surrounding content box to be equal to the cart box to prevent the cart box from popping out the bottom on short pages
             if(document.getElementById('cartCheckoutButton')) {
                 cartHeight = findPos(document.getElementById('cartCheckoutButton'))[1] + (2 * document.getElementById('cartCheckoutButton').offsetHeight) - findPos(document.getElementById('shoppingcart'))[1];		   
                 if ( document.getElementById( 'content' ).offsetHeight < cartHeight + bottomOffset ){
                         document.getElementById( 'content' ).style.height = cartHeight + bottomOffset + 'px';
                 }
             }
         }
