// holds an instance of XMLHttpRequest
var ESUxmlHttp = createXmlHttpRequestObject();
var product_id = 0;
var box_id = "related_0";

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject() {
  var xmlHttp;
  try {
    xmlHttp = new XMLHttpRequest(); }
  catch(e)
    { var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0', 'MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP');
      for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) {
        try { xmlHttp = new ActiveXObject(XmlHttpVersions[i]);} 
        catch (e) {} } }
  
  // return the created object or display an error message
  if (!xmlHttp) { alert("Error creating the XMLHttpRequest object."); }
  else { return xmlHttp; }
}

function add_product() {  
  if (ESUxmlHttp) {
    try
      { ESUxmlHttp.open("GET", "add_related.php?products_id=" + escape(product_id), true);
        ESUxmlHttp.onreadystatechange = handleRequestStateChange;
        ESUxmlHttp.send(null); }
    catch (e) {}
  }
}

function product_detail() {  
  if (ESUxmlHttp) {
    try
      { ESUxmlHttp.open("GET", "product_detail.php?products_id=" + escape(product_id), true);
        ESUxmlHttp.onreadystatechange = handleRequestStateChange2;
        ESUxmlHttp.send(null); }
    catch (e) {}
  }
}

function handleRequestStateChange() { 
  try {
	// when readyState is 4, we are ready to read the server response
	if (ESUxmlHttp.readyState == 4) {
	  // continue only if HTTP status is "OK"
	  if (ESUxmlHttp.status == 200) {
	    try {
		  // do something with the response from the server
		  handleServerResponse(); }
	    catch(e) {
		  alert("Error reading the response: " + e.toString()); } } 
	  else { // display status message
	    alert("There was a problem retrieving the data:\n" + 
		ESUxmlHttp.statusText); } } }
   catch(e) { }
}

function handleRequestStateChange2() { 
  try {
	// when readyState is 4, we are ready to read the server response
	if (ESUxmlHttp.readyState == 4) {
	  // continue only if HTTP status is "OK"
	  if (ESUxmlHttp.status == 200) {
	    try {
		  // do something with the response from the server
		  handleServerResponse2(); }
	    catch(e) {
		  alert("Error reading the response: " + e.toString()); } } 
	  else { // display status message
	    alert("There was a problem retrieving the data:\n" + 
		ESUxmlHttp.statusText); } } }
   catch(e) { }
}

function handleServerResponse() {
  var XMLtest = ESUxmlHttp.responseText;
  if (XMLtest.indexOf("OK")>=0) { 
    message = "<img src='../../images/added.gif' width=51 height=9>";
    eval("document.getElementById('txt_"+product_id+"')").innerHTML=message;
	eval("document.cart_quantity."+box_id).disabled=true;
	
	document.getElementById('related_message').innerHTML = "You can adjust quantities from your shopping cart.";
	return true; }
  else { return false; }
}

function handleServerResponse2() {
  var XMLtest = ESUxmlHttp.responseText;
  if (XMLtest!="") { document.getElementById('lyrText').innerHTML = XMLtest + "<p align='right' class='smallText' onClick='hideLayer(\"lyrText\");'>Close This Window [X]</p>"; return true; }
  else { return false; }
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function getObject( obj )
{ if ( document.getElementById ) { obj = document.getElementById( obj ); }
  else if ( document.all ) { obj = document.all.item( obj ); }
  else { obj = null; }
  return obj; }

function showLayer(obj, e)
 { var tempX = 0; var tempY = 0; var xoffset = -500; var yoffset = 0; var objHolder = obj;
   obj = getObject( obj ); if (obj==null) return;

   if (document.all) { tempX = event.clientX + document.body.scrollLeft; tempY = event.clientY + document.body.scrollTop; }
   else { tempX = e.pageX; tempY = e.pageY; }

   if ((tempX + xoffset) < 0) {tempX = 500}
   if (tempY < 0) {tempY = 0}

   obj.style.top  = (tempY + yoffset) + 'px';
   obj.style.left = (tempX + xoffset) + 'px';

   MM_showHideLayers(objHolder,'','show')
 }
 
function showLayer2(obj, e)
 { if (!e) var e = window.event;
   var tempX = 0; var tempY = 0; var xoffset = 0; var yoffset = 0; var objHolder = obj;
   obj = getObject( obj ); if (obj==null) return;

   if (document.all) { tempX = event.clientX + document.body.scrollLeft; tempY = event.clientY + document.body.scrollTop; }
   else { tempX = e.pageX; tempY = e.pageY; }
   
   if ((tempX + xoffset) < 0) {tempX = 500; }
   if ((tempX + xoffset) > 900) {tempX = tempX-350; }
   
   if (tempY < 0) {tempY = 0}

   obj.style.top  = (tempY + yoffset) + 'px';
   obj.style.left = (tempX + xoffset) + 'px';

   MM_showHideLayers(objHolder,'','show')
 }
 
function hideLayer(obj)
  { MM_showHideLayers(obj,'','hide') }

function related(box,pid) {
  product_id = pid
  box_id = box
  eval("document.getElementById('txt_"+product_id+"')").innerHTML="<img src='../../images/wait.gif' width=51 height=9>";
  if (eval("document.cart_quantity."+box_id).checked) { add_product(); }
}

function product_popup(pid) {
  // display product detail in layer
  document.getElementById('lyrText').innerHTML = "<span class='main'>Loading.... Please Wait.</span>";
  product_id = pid;
  product_detail();
  hideLayer('lyrText', event)
  hideLayer('lyrGallery', event)
  showLayer('lyrText', event);
}

function gallery(img) {
  hideLayer('lyrText', event)
  hideLayer('lyrGallery', event)
  document.getElementById('lyrGallery').innerHTML = "<br><p align='center'><img src='/shop/catalog/thumbnails/xl/"+img+"' alt='Click on this image to close it.' onClick='hideLayer(\"lyrGallery\", event)'><br><br>If you would like to publish this image in our forum.<br>The URL is:<br><br><b>http://www.brew-it-yourself.co.uk/shop/catalog/thumbnails/xl/"+img+"</b><br><br>Click on the image to close the pop-up window.<br><br></p>";
  showLayer2('lyrGallery', event);
}

function verify_free_wherry() {
  code = document.getElementById('txtVoucherCode').value;
  if (code!="") {
    if (confirm("Once you code has been verified a free Woodfordes Wherry will be added to your shopping cart and the code registered as 'used'. You must complete the checkout process as we can not re-issue vocuher codes.")) { document.location="verify_free_wherry.php?code="+code; }
  } else {
    alert ("No code entered");
  }
}
