
var nextMessageColour;

function updateMessage()
	{
	var messageDiv = document.getElementById('messageBox');
	var basketMessageDiv = document.getElementById('basketMessage');
    var result='<span style="border:1px solid #cccccc;padding:2px;background-color:'+nextMessageColour+';">'
	result+=basketMessageDiv.innerHTML;
	result+='</span>';
	messageDiv.innerHTML = result;
	}


function updateBasket (req)
{
  var basketDiv = document.getElementById('basket');
  basketDiv.innerHTML = req.responseText;
  updateMessage();
}


function addToBasket (productCode, quantity, size, colour)
{
  // Obtain an XMLHttpRequest instance
  var req = newXMLHttpRequest();

  // Set the handler function to receive callback notifications
  // from the request object
  var handlerFunction = getReadyStateHandlerResponse(req, updateBasket);
  req.onreadystatechange = handlerFunction;
  
  // Open an HTTP POST connection to the shopping basket servlet.
  // Third parameter specifies request is asynchronous.
  req.open("POST", "/UpdateBasket.do;jsessionid="+jSession, 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 basket.
  req.send("action=add&p="+productCode+"&q="+quantity+"&s="+size+"&c="+colour);
  nextMessageColour="#ffff99";
}


function removeFromBasket (productIndex, productCode)
{
  // Obtain an XMLHttpRequest instance
  var req = newXMLHttpRequest();

  // Set the handler function to receive callback notifications
  // from the request object
  var handlerFunction = getReadyStateHandlerResponse(req, updateBasket);
  req.onreadystatechange = handlerFunction;
  
  // Open an HTTP POST connection to the shopping basket servlet.
  // Third parameter specifies request is asynchronous.
  req.open("POST", "/UpdateBasket.do;jsessionid="+jSession, 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 basket.
  req.send("action=remove&p="+productCode+"&i="+productIndex);
    nextMessageColour="#99ffff";
}


function addOne (productIndex, productCode)
{
  // Obtain an XMLHttpRequest instance
  var req = newXMLHttpRequest();

  // Set the handler function to receive callback notifications
  // from the request object
  var handlerFunction = getReadyStateHandlerResponse(req, updateBasket);
  req.onreadystatechange = handlerFunction;
  
  // Open an HTTP POST connection to the shopping basket servlet.
  // Third parameter specifies request is asynchronous.
  req.open("POST", "/UpdateBasket.do;jsessionid="+jSession, 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 basket.
  req.send("action=addOne&p="+productCode+"&i="+productIndex);
  nextMessageColour="#ffff99";
}


function removeOne (productIndex, productCode)
{
  // Obtain an XMLHttpRequest instance
  var req = newXMLHttpRequest();

  // Set the handler function to receive callback notifications
  // from the request object
  var handlerFunction = getReadyStateHandlerResponse(req, updateBasket);
  req.onreadystatechange = handlerFunction;
  
  // Open an HTTP POST connection to the shopping basket servlet.
  // Third parameter specifies request is asynchronous.
  req.open("POST", "/UpdateBasket.do;jsessionid="+jSession, 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 basket.
  req.send("action=removeOne&p="+productCode+"&i="+productIndex);
    nextMessageColour="#99ffff";
}


function updateLocation (locationId)
{
  // Obtain an XMLHttpRequest instance
  var req = newXMLHttpRequest();

  // Set the handler function to receive callback notifications
  // from the request object
  var handlerFunction = getReadyStateHandlerResponse(req, updateBasket);
  req.onreadystatechange = handlerFunction;
  
  // Open an HTTP POST connection to the shopping basket servlet.
  // Third parameter specifies request is asynchronous.
  req.open("POST", "/UpdateBasket.do;jsessionid="+jSession, 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 basket.
  req.send("action=updateLocation&p="+productCode+"&i="+productIndex);
}

function saveOrder ()
{
  // Obtain an XMLHttpRequest instance
  var req = newXMLHttpRequest();

  // Set the handler function to receive callback notifications
  // from the request object
  var handlerFunction = getReadyStateHandlerResponse(req, confirmSavedOrder);
  req.onreadystatechange = handlerFunction;
  
  // Open an HTTP POST connection to the shopping basket servlet.
  // Third parameter specifies request is asynchronous.
  req.open("POST", "/servlet/SaveOrder;jsessionid="+jSession, 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 basket.
  req.send("");
  nextMessageColour="#ffff99";
}

function confirmSavedOrder (req)
{
  var orderInfoDiv = document.getElementById('orderInfo');
  orderInfoDiv.innerHTML = req.responseText;
}