var xmlHttp;

function isUnsignedInteger(s)
{
  return (s.toString().search(/^[0-9]+$/) == 0);
}

function bmwClick()
{
  xmlHttp=GetXmlHttpObject();
  if (xmlHttp==null)
  {
    alert ("Browser does not support HTTP Request");
    return;
  }
  var url="/ajax/bmwClick.php";
  var post_str="click=1";
  xmlHttp.open("POST",url,true);
  xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  xmlHttp.send(post_str);
}

function updateCart(id,session,action)
{
  if (action=='Remove')
  {
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
      alert ("Browser does not support HTTP Request");
      return;
    }
    var url="/ajax/updateCart.php";
    var post_str="id="+id;
    post_str=post_str+"&quantity=0";
    post_str=post_str+"&session="+session;
    post_str=post_str+"&action="+action;
    xmlHttp.onreadystatechange=stateChangedCart;
    xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xmlHttp.send(post_str);
    window.scrollBy(0,-999);
  }
  else
  {
    if (isUnsignedInteger(document.getElementById("quantity"+id).value))
    {
      xmlHttp=GetXmlHttpObject();
      if (xmlHttp==null)
      {
        alert ("Browser does not support HTTP Request");
        return;
      }
      var url="/ajax/updateCart.php";
      var post_str="id="+id;
      post_str=post_str+"&quantity="+document.getElementById("quantity"+id).value;
      post_str=post_str+"&session="+session;
      post_str=post_str+"&action="+action;
      xmlHttp.onreadystatechange=stateChangedCart;
      xmlHttp.open("POST",url,true);
      xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
      xmlHttp.send(post_str);
      document.getElementById("quantity"+id).value = "";
      document.getElementById("instruct"+id).style.display = "inline";
    }
    else
    {
      alert("You must enter a number in the quantity field to add to cart.");
      document.getElementById("quantity"+id).value="";
      document.getElementById("quantity"+id).focus();
    }
  }
}

function stateChangedCart() 
{
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  { 
    document.getElementById("divCart").innerHTML=xmlHttp.responseText;
  } 
}

function GetXmlHttpObject()
{
  var xmlHttp=null;

  try
  {
    xmlHttp=new XMLHttpRequest();
  }

  catch (e)
  {
    try
    {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }

  return xmlHttp;
}