url_root = "http://www.get2bed.co.uk/";
function AJAXInteraction(url,pid,obj_id) 
{
    var req = init();
    req.onreadystatechange = processRequest;
    function init() 
		{
      if (window.XMLHttpRequest) 
			{
        return new XMLHttpRequest();
      } 
			else if (window.ActiveXObject)
			{
        return new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
    
    function processRequest () 
		{
      if (req.readyState == 4) 
			{
        if (req.status == 200) 
				{
				 req = req.responseText
				 ident = req.substr(0,1)
				 switch(ident)
				 {
				  case 'a':
					 var spl  = req.split("|");
					 var c    = spl[1];
					 var amt  = spl[2];
					 var it;
					 it	 		 				  =' Items ';
					 if(c == 1) it = ' Item ';				 
					 
					 document.getElementById('b'+pid).innerHTML   	 = '<a href="javascript:void(0);" onclick="javascript:void(0);add_to_cart(\'' + pid + '\');"><img src="' + url_root + 'images/gif/addme.gif" style="border:0;"></a>';
					 document.getElementById('result').innerHTML     = '';//it + 'added cart';
					 document.getElementById('cart_amt').innerHTML   = amt;
					 document.getElementById('cart_count').innerHTML = c + it;
					 popup();
					 break;
				 }
				 
        }
      }
    }

    this.doGet = function() 
		{
      req.open("GET", url, true);
      req.send(null);
    }
    
    this.doPost = function(body) 
		{
      req.open("POST", url, true);
      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      req.send(body);
    }
}

function add_to_cart(pid, slug) 
{
  document.getElementById('result').innerHTML = '';
  document.getElementById('b'+pid).innerHTML = '<img src="' + url_root + 'images/gif/load.gif">';
	var qty = document.getElementById('q'+pid).value;
	if(slug == '')
	{
  	var ai  = new AJAXInteraction(url_root + 'index.php?controller=cart&op=add&pid=' + pid + '&qty=' + qty,pid);
	}
  else
  	var ai  = new AJAXInteraction(url_root + 'index.php?controller=cart&op=add&pid=' + slug + '&qty=' + qty,pid);
  ai.doGet();
}

function remove_item(pid) 
{
  document.getElementById('replace'+pid).innerHTML = '<img src="' + url_root + 'images/gif/load.gif">';
  var ai  = new AJAXInteraction(url_root + 'index.php?controller=cart&op=remove_item&pid=' + pid + '&qty=' + qty,pid);
  ai.doGet();
}

function add_jewell(pid,qty) 
{
  document.getElementById('janswer').innerHTML = '<img src="ajax.gif">';
  var ai = new AJAXInteraction('cart.php?action=addj&qty='+qty+'&pid='+pid,pid);
  ai.doGet();
}


function AJAXResults(url) 
{
    var req = init();
    req.onreadystatechange = processRequest;
    function init() 
		{
      if (window.XMLHttpRequest) 
			{
        return new XMLHttpRequest();
      } 
			else if (window.ActiveXObject)
			{
        return new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
    
    function processRequest () 
		{
      if (req.readyState == 4) 
			{
        if (req.status == 200) 
				{
				 req = req.responseText
				 document.getElementById('ResultContainer').innerHTML = req;
        }
      }
    }


    this.doPost = function(body) 
		{
      req.open("POST", url, true);
      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      req.send(body);
    }
}


