<!--

// Javascript for Essentially Wine Search
  
var json;
var bsjson;
var DisplayAs;

var minItems = 1;
var maxItems = 99;

function preparesearch()
{
  s=document.getElementById("allkeywordsID").value;
  if (s!="") { s=s+"+"; }
  s=s+document.getElementById("keywordsHID").value;
  search("Keywords="+s,1);
}

function addToBasket(Sku,Qty)
{
  if (typeof Qty == "undefined") {
    Qty = minItems;
  }else if(Qty > maxItems){
	Qty = maxItems;
  }else if(Qty < minItems){
	Qty = minItems;
  }
  if (window.XMLHttpRequest)
  {
    basketHTTPReq = new XMLHttpRequest();
  }
  else if (window.ActiveXObject)
  {
    basketHTTPReq = new ActiveXObject("Microsoft.XMLHTTP");
  }
  basketHTTPReq.abort();
  basketHTTPReq.onreadystatechange = function()
  {
    try
    {
      if (basketHTTPReq.readyState==4)
      {
      	if (basketHTTPReq.status==200)
        {
        s=basketHTTPReq.responseText;
        bsjson = eval("(" + s + ")");
          document.getElementById("basketstatusID").innerHTML=bsjson.basketitems+" item(s) &pound; "+bsjson.baskettotal;
          document.getElementById(Sku).style.backgroundColor="#EfEfEf";
		  document.getElementById('addlink_'+Sku).innerHTML='Update basket';
        }
        else
        {
          alert("basketHTTPReq Communication failure please contact support@thecreativestore.co.uk.\nIn all likelyhood your item "+Sku+" was not added to the basket.");
        }
      }			
    }
    catch (E) { }
  }
  var crp = Math.floor(Math.random() * 1001);
  basketHTTPReq.open("GET", "/basket/set.php?Sku="+Sku+"&qty="+Qty+"&=crap="+crp, true);
  basketHTTPReq.send(null);
}

function search(qs,rsp)
{
  if(rsp!=1){
    rsp=0;
  }
  if (window.XMLHttpRequest)
  {
    searchHTTPReq = new XMLHttpRequest();
  }
  else if (window.ActiveXObject)
  {
    searchHTTPReq = new ActiveXObject("Microsoft.XMLHTTP");
  }
  searchHTTPReq.abort();   // if a previous request is still executing kill it.
  var crp = Math.floor(Math.random() * 1001);
  searchHTTPReq.open("GET", "/search/ajax.php?"+qs+"&crap="+crp+"&rsp="+rsp, true);
  searchHTTPReq.onreadystatechange = function()
  {
    try
    {
      if (searchHTTPReq.readyState==4)
      {
      	if (searchHTTPReq.status==200)
	    {
	      document.getElementById("resultsID").innerHTML="Search returned empty or incorrect result!";
	      s=searchHTTPReq.responseText;
	      json = eval("(" + s + ")");
	      crit=json.crit;
	      DisplayResults();
	    }
      }			
    }
    catch (E) { }
  }
  searchHTTPReq.send(null);
  document.getElementById("resultsID").innerHTML="<img src='/grfx/loading.gif' alt='progress' />";
}

function DisplayResults()		
{
  // we call this each time the search results need to alter... it updates the LeftNav, Search results and the Refine Search
  var i;
  var ii;
  var s="";
  
  // ************************************
  // Update left Navigation
  // ************************************

  // use crit.ProductType to update the catNav
  oCatNav=document.getElementById("catNav");
  oLIarr=oCatNav.getElementsByTagName("li");
  for (i=0; i<oLIarr.length; i++)
  {
    oAarr=oLIarr[i].getElementsByTagName('a');
    for (ii=0; ii<oAarr.length; ii++)
    {
      arr=oAarr[ii].href.split("=");
      if ((oAarr[ii].className!="cat") && (oAarr[ii].className!="jscript"))
      { 
        oAarr[ii].className=""; 
      }			
      if (arr[1]==undefined) { k=arr[0]; }
      else
      {
        if (arr[1]==crit.ProductType)
        {	// we need to expand the menu
          //var reg=/\"[a-zA-Z1-9]*\"/;   //"
          //var res=reg.exec(k);
          //reg=/\"/g;			//"
          //res=res[0].replace(reg,"");
          //expand(res);
          // we need to 'select' the nav item
          oAarr[ii].className="selected";
        }
      }
    }
  }

  // use crit.Brand to update the brandNav
  oBrandNav=document.getElementById("brandNav");
  oLIarr=oBrandNav.getElementsByTagName("li");
  for (i=0; i<oLIarr.length; i++)
  {
    oAarr=oLIarr[i].getElementsByTagName('a');
    arr=oAarr[0].href.split("=");
    if (arr[1]==crit.Brand)
    {
      oAarr[0].className="selected";
    }
    else
    {
      oAarr[0].className="";
    }
  }

  // ************************************
  // Update Grid/List
  // ************************************
  if(document.cookie.length>0){
	  if (document.cookie.substring(0,10)=='showAsList'){
		  DisplayAs='list';
	  }else{
		  DisplayAs='grid';
	  }
  }
  
  if (DisplayAs=="list")
  {
    document.getElementById("list").style.backgroundPosition="left top";
    document.getElementById("grid").style.backgroundPosition="left bottom";
    document.getElementById("list").style.fontWeight="bold";
    document.getElementById("grid").style.fontWeight="normal";
  }
  else
  {
    document.getElementById("grid").style.backgroundPosition="left top";
    document.getElementById("list").style.backgroundPosition="left bottom";
    document.getElementById("grid").style.fontWeight="bold";
    document.getElementById("list").style.fontWeight="normal";
  }

  // ************************************
  // Search Results
  // ************************************
  s="";
  for (key in json.sr)
  {
    tmp="";
	addText="Add to basket";
	// Sort out stock level and stock level message
	stockMessage='No stock information';
	stockIcon='s r';
	if (json.sr[key].ToOrder==1) {
		stockMessage='Shipped to order';
		stockIcon='s b';
	}else if (json.sr[key].QtyInStock<=0){
		stockMessage='Out of stock';
		stockIcon='s r';
	}else if (json.sr[key].QtyInStock<5){
		stockMessage='Low stock';
		stockIcon='s o';
	}else if (json.sr[key].QtyInStock>=5){
		stockMessage='In stock';
		stockIcon='s g';
	}
	
    if (json.sr[key].sid=="InBasket") { tmp=" style='background-color: #EfEfEf;'"; addText="Update basket"; }
    if (DisplayAs=="list")
    {
	  if(json.sr[key].SpecialOffer>0){
		  prevPrice="<span style='text-decoration: line-through; color: #999999;'>&pound;"+json.sr[key].SalePrice+"</span> &pound;"+json.sr[key].SpecialOffer;
	  }else{
		  prevPrice="&pound;"+json.sr[key].SalePrice;
	  }
      s=s+"\n\
        <div class='product list' style='background: #ffffff url(/img.php?img140x150,"+json.sr[key].Sku+") 10px 10px no-repeat;'>\n\
          <div id='"+json.sr[key].Sku+"' class='basket'"+tmp+">\n\
            <p class='c'>Code: "+json.sr[key].Sku+"</p>\n\
            <p class='p'>"+prevPrice+"</p>\n\
            <p class='"+stockIcon+"'>"+stockMessage+"</p>\n\
            <form>\n\
              Qty <input type='text' value='"+json.sr[key].qty+"' id='addqty_"+json.sr[key].Sku+"' onchange='this.value=makeInt(this.value,1,99);' />\n\
              <a href=\"javascript:addToBasket('"+json.sr[key].Sku+"',document.getElementById('addqty_"+json.sr[key].Sku+"').value)\" id=\"addlink_"+json.sr[key].Sku+"\">"+addText+"</a>\n\
            </form>\n\
          </div>\n\
          <div class='detail'>\n\
            <h5><a style='color: #A07E56;' href='/product/?p="+json.sr[key].Sku+"'>"+json.sr[key].name+"</a></h5>\n\
            <p>"+json.sr[key].LongDescription+"</p>\n\
            <p><a class='m' href='/product/?p="+json.sr[key].Sku+"&amp;d=list'D>More info</a><a class='e' href='/img.php?img495x530,"+json.sr[key].Sku+"' target='_blank' rel='lightbox' title='"+json.sr[key].name+"'>Enlarge image</a></p>\n\
          </div>\n\
        </div>\n";
    }
    else
    {
	  if(json.sr[key].SpecialOffer>0){
		  prevPrice="<span style='text-decoration: line-through; color: #999999;'>&pound;"+json.sr[key].SalePrice+"</span> &pound;"+json.sr[key].SpecialOffer;
	  }else{
		  prevPrice="&pound;"+json.sr[key].SalePrice;
	  }
      s=s+"\n\
        <div class='product grid'>\n\
          <div class='detail'>\n\
            <h5><a href='/product/?p="+json.sr[key].Sku+"'>"+json.sr[key].name+"</a></h5>\n\
            <a href='/product/?p="+json.sr[key].Sku+"'><img src='/img.php?img140x150,"+json.sr[key].Sku+"' alt='Wine Accessories' /></a>\n\
            <p><a class='m' href='/product/?p="+json.sr[key].Sku+"'>More info</a><a class='e' href='/img.php?img495x530,"+json.sr[key].Sku+"' target='_blank' rel='lightbox' title='"+json.sr[key].name+"'>Enlarge image</a></p>\n\
          </div>\n\
          <div id='"+json.sr[key].Sku+"' class='basket'"+tmp+">\n\
            <p class='c'>Code: "+json.sr[key].Sku+"</p>\n\
            <p class='p'>"+prevPrice+"</p>\n\
            <p class='"+stockIcon+"'>"+stockMessage+"</p>\n\
            <form>\n\
              Qty <input type='text' value='"+json.sr[key].qty+"' id='addqty_"+json.sr[key].Sku+"' onchange='this.value=makeInt(this.value,1,99);' />\n\
              <a href=\"javascript:addToBasket('"+json.sr[key].Sku+"',document.getElementById('addqty_"+json.sr[key].Sku+"').value)\" id=\"addlink_"+json.sr[key].Sku+"\">"+addText+"</a>\n\
            </form>\n\
          </div>\n\
        </div>\n";
    }
  }
  if (s=="") {s="No results were returned";} else { /*alert('There are results');*/ }
  document.getElementById("resultsID").innerHTML=s;

  // ************************************
  // Refine Search 
  // ************************************
  
  // keyword list
  kwlist="<ul><li><a href='javascript:search(\"Keywords=ALL\")'>All results</a></li>";
  s="";
  for (key in json.terms_Keywords)
  {
    kwlist=kwlist+"<li> &gt; <a href='javascript:search(\"Keywords="+json.terms_Keywords[key].link+"\")'>"+json.terms_Keywords[key].Keyword+"</a> </li>\r\n";
    s=json.terms_Keywords[key].link;
  }
  kwlist=kwlist+"</ul>";
//  alert(kwlist);
  if (kwlist.length>77) 
  { document.getElementById("keywordsLISTID").style.display="block"; document.getElementById("keywordsLISTID").innerHTML=kwlist; } 
  else 
  { document.getElementById("keywordsLISTID").style.display="none"; }

  if (s=="ALL") { s=""; }
  kwlist="<form name='searchfrm' action='/search/' method='get'>\n\
	  <input name='AllKeywords' id='allkeywordsID' value='"+s+"' type='hidden' />\n\
	  <input name='Keywords' id='keywordsHID' value='' type='text' />\n\
          <a class='moth' href='javascript:preparesearch()'>Refine Results</a>&nbsp;&nbsp;&nbsp;\n\
          <a class='butt' href='javascript:document.searchfrm.submit();'>New Search</a>\n\
	</form>\n";
  document.getElementById("keywordlistID").innerHTML=kwlist;	

  // product type list
  ptlist="<ul>";
  for (key in json.terms_ProductTypes)
  {
    ptlist=ptlist+"<li><a class='"+json.terms_ProductTypes[key].sel+"' href='javascript:search(\"ProductType="+json.terms_ProductTypes[key].Custom2+"\",1)'>"+json.terms_ProductTypes[key].Descr+json.terms_ProductTypes[key].cnt+"</a></li> ";
  }
  ptlist=ptlist+"</ul>";
  document.getElementById("ptlistID").innerHTML=ptlist;

  // price range list
  prlist="<ul>";
  for (key in json.terms_PriceRanges)
  {
    prlist=prlist+"<li><a class='"+json.terms_PriceRanges[key].sel+"' href='javascript:search(\"PriceRange="+json.terms_PriceRanges[key].PriceRange+"\",1)'>"+json.terms_PriceRanges[key].Descr+json.terms_PriceRanges[key].cnt+"</a></li> ";
  }
  prlist=prlist+"</ul>";
  document.getElementById("prlistID").innerHTML=prlist;

  // brand list
  brandlist="<ul>";
  for (key in json.terms_Brands)
  {
    brandlist=brandlist+"<li><a class='"+json.terms_Brands[key].sel+"' href='javascript:search(\"Brand="+json.terms_Brands[key].Custom3+"\",1)'>"+json.terms_Brands[key].Descr+json.terms_Brands[key].cnt+"</a></li> ";
  }
  brandlist=brandlist+"</ul>";
  document.getElementById("brandlistID").innerHTML=brandlist;
  
  // order by
  orderbylookup=new Array();
  orderbylookup[0]="Price, Low to High";
  orderbylookup[1]="Price, High to Low";
  orderbylookup[2]="A-Z";
  orderbylookup[3]="Z-A";
  orderbylookup[4]="Special Offers";
  orderbylist="<ul>";
  for (i=0; i<5; i++)
  {
    if (parseInt(crit.OrderBy) == i) { tmp="class='selected'"; } else { tmp=""; }
    orderbylist=orderbylist+"<li><a "+tmp+" href='javascript:search(\"OrderBy="+i+"\")'>"+orderbylookup[i]+"</a></li>";
  }
  orderbylist=orderbylist+"</ul>";
  document.getElementById("orderbylistID").innerHTML=orderbylist;
  
  // page management
  START=parseInt(crit.ResultsSTART)+1;
  if(START<crit.ResultsPP){
	  START=1;
  }
  END=parseInt(crit.ResultsPP)+START-1;
  if (END > parseInt(crit.TotalRecords)) { END=parseInt(crit.TotalRecords); }
  
  s="Showing <b>"+START+"-"+END+"</b> of <b>"+crit.TotalRecords+"</b> | Results per page <select id='rppID' onchange='ChangeResultsPP()'>\n";
  for (i=6; i<=36; i=i+6)
  {
    if (crit.ResultsPP == i) { tmp="selected"; } else { tmp=""; }
    s=s+"  <option "+tmp+" value="+i+">"+i+"\n";
  }
  s=s+"</select>";
  document.getElementById("pagemgmtID").innerHTML=s;
  TP=Math.floor(crit.TotalRecords/crit.ResultsPP)+1;
  s="";
  for (i=0; i<TP; i++)
  {
    if (i==Math.floor(START/parseInt(crit.ResultsPP))) 
    { 
      tmp=" class='sel'"; 
      prev=(i-1)*parseInt(crit.ResultsPP);
      if (prev >= 0 ) { prev="< <a href=\"javascript:search('ResultsSTART="+prev+"')\">Previous</a> &nbsp;"; }
      else { prev="< Previous &nbsp;"; }
      next=(i+1)*parseInt(crit.ResultsPP);
      if (next <= END && next < crit.TotalRecords) { next="&nbsp; <a href=\"javascript:search('ResultsSTART="+next+"')\">Next</a> >"; }
      else { next="&nbsp; Next >"; }
    } 
    else 
    { 
      tmp=""; 
    }
	if(i*crit.ResultsPP<crit.TotalRecords){
      s=s+" <a"+tmp+" href=\"javascript:search('ResultsSTART="+(i*parseInt(crit.ResultsPP))+"')\">"+(i+1)+"</a> ";
	}
  }
  document.getElementById("pagemgmt2ID").innerHTML=prev+s+next;
  document.getElementById("pagemgmt22ID").innerHTML=prev+s+next;
  
  if(typeof initLightbox == 'function'){
    setTimeout(initLightbox, 750);
  }
//  alert(TP);
}

function ChangeResultsPP()
{
  search("ResultsPP="+document.getElementById("rppID").value);
}

function ListView() 
{ 
  document.cookie='showAsList';
  DisplayAs="list"; 
  DisplayResults(); 
}
function GridView() 
{ 
  document.cookie='showAsGrid';
  DisplayAs="grid";
  DisplayResults(); 
}
function ToggleRefine()
{
  if (document.getElementById("refineID").style.display!="block")
  {
    document.getElementById("refineID").style.display="block";
    document.getElementById("refine").innerHTML="Hide refine <img src='/grfx/refineDown.png' />";
  }
  else
  {
    document.getElementById("refineID").style.display="none";
    document.getElementById("refine").innerHTML="Refine results <img src='/grfx/refine.png' />";
  }
}


// -->

