function setCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString();
}

function readCookie(cookieName) {
 var theCookie=""+document.cookie;
 var ind=theCookie.indexOf(cookieName);
 if (ind==-1 || cookieName=="") return ""; 
 var ind1=theCookie.indexOf(';',ind);
 if (ind1==-1) ind1=theCookie.length; 
 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

function showAddToCartDialog(model)
{
  object = document.getElementById("add_to_cart_dialog");

  newTop = document.body.scrollTop + (document.body.clientHeight -  object.clientHeight)/2;
  if(newTop < document.body.scrollTop) {
     newTop = document.body.scrollTop;
  }
  
  newLeft = document.body.scrollLeft + (document.body.clientWidth -  object.clientWidth)/2;
  if(newLeft < 0) {
     newLeft = 0;
  }

  object.style.top = newTop;
  object.style.left = newLeft;
  
  object.style.visibility = "visible";

  object = document.getElementById("cart_product_model");
  object.innerHTML = model;
  
  object = document.getElementById("cart_model_image");
  setThumbnailURL(object, model);
}

function hideAddToCartDialog() {
  object = document.getElementById("add_to_cart_dialog");
  object.style.visibility = "hidden";
}


function showAddItemErrorDialog(model, errMsg)
{
  object = document.getElementById("item_not_added_dialog");

  newTop = document.body.scrollTop + (document.body.clientHeight -  object.clientHeight)/2;
  if(newTop < document.body.scrollTop) {
     newTop = document.body.scrollTop;
  }
  
  newLeft = document.body.scrollLeft + (document.body.clientWidth -  object.clientWidth)/2;
  if(newLeft < 0) {
     newLeft = 0;
  }

  object.style.top = newTop;
  object.style.left = newLeft;
  
  object.style.visibility = "visible";

  object = document.getElementById("err_cart_product_model");
  object.innerHTML = model;

  object = document.getElementById("cart_err_model_image");
  setThumbnailURL(object, model);
  
  object = document.getElementById("error_reason");
  object.innerHTML = errMsg;

}

function hideAddItemErrorDialog() {
  object = document.getElementById("item_not_added_dialog");
  object.style.visibility = "hidden";
}

function addToCart(model)
{
  var size_option = document.getElementById(model+"_size");
  var size = "";
  if(size_option != null) {
  	size = size_option.value;
  }

  var xmlHttp;
  
  //Show waiting icon
  document.cart_img.src = "images/wait.gif";
  object = document.getElementById("cart_model_image");
  object.src = "images/wait.gif";
  object = document.getElementById("cart_err_model_image");
  object.src = "images/wait.gif";
  
  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)
    {
	  //Show cart icon
	  document.cart_img.src = "images/cart.png";
	  
	  var responseText = xmlHttp.responseText;
	  var responseTokens = responseText.split( "|" );
	  var itemAdded = false;
	  
	  if(responseTokens[0].indexOf("true") == 0) {
	  	itemAdded = true;
	  }
  
      document.getElementById("itemCount").innerHTML=responseTokens[1];
      if(itemAdded) {
      	showAddToCartDialog(model);
      }
      else {
      	showAddItemErrorDialog(model, responseTokens[0]);
      }
      hideDetails();
            
      var countTipShown = readCookie("cart_tip_count");
      if(countTipShown == "")
      {
      	countTipShown = 0;
      }
      
      if(countTipShown < 5)
      {
	      showCartTip();
	      setTimeout("hideCartTip();",5000);
	      countTipShown++;
	      setCookie("cart_tip_count",countTipShown,30)
	  }
    }
  }
  xmlHttp.open("GET","add_to_cart.php?model="+model+"&size="+size+"&sid="+Math.random(),true);
  xmlHttp.send(null);
}

function addToCartFromDetailsPane(model)
{
  var size_option = document.getElementById("details_size_option");
  var size = "";
  if(size_option != null) {
  	size = size_option.value;
  }
  

  var xmlHttp;
  
  //Show waiting icon
  document.cart_img.src = "images/wait.gif";
  object = document.getElementById("cart_model_image");
  object.src = "images/wait.gif";
  object = document.getElementById("cart_err_model_image");
  object.src = "images/wait.gif";
  
  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)
    {
	  //Show cart icon
	  document.cart_img.src = "images/cart.png";
	  
	  var responseText = xmlHttp.responseText;
	  var responseTokens = responseText.split( "|" );
	  var itemAdded = false;
	  
	  if(responseTokens[0].indexOf("true") == 0) {
	  	itemAdded = true;
	  }
  
      document.getElementById("itemCount").innerHTML=responseTokens[1];
      if(itemAdded) {
      	showAddToCartDialog(model);
      }
      else {
      	showAddItemErrorDialog(model, responseTokens[0]);
      }
      hideDetails();
            
      var countTipShown = readCookie("cart_tip_count");
      if(countTipShown == "")
      {
      	countTipShown = 0;
      }
      
      if(countTipShown < 5)
      {
	      showCartTip();
	      setTimeout("hideCartTip();",5000);
	      countTipShown++;
	      setCookie("cart_tip_count",countTipShown,30)
	  }
    }
  }
  xmlHttp.open("GET","add_to_cart.php?model="+model+"&size="+size+"&sid="+Math.random(),true);
  xmlHttp.send(null);
}

function setThumbnailURL(imageObject, model)
{
  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)
    {
	  //Show cart icon
	  imageObject.src = xmlHttp.responseText;
    }
  }
  xmlHttp.open("GET","get_thumbnail.php?model=" + model,true);
  xmlHttp.send(null);
}

