<!--

// Differs from common.js just by the checking for shop Id in cookie
var searchString = "";
var my_id_is = "";
var wptid = "";
homepagearrival();

// Cookie of form sponsor=shopname:firstname:familyname
// firstname and familyname now redundant - can use for
// other info.
function getSponsorFromCookie()
{
  var cookieValue = document.cookie;
  var valueStart = cookieValue.indexOf("sponsor=");

  if (valueStart == -1)
  {
    cookieValue = null;
    //alert("No cookie received");
  }
  else
  {
    valueStart = cookieValue.indexOf("sponsor=", valueStart) + 8;
    var valueEnd = cookieValue.indexOf(";", valueStart);
    if (valueEnd == -1)
    {
      valueEnd = cookieValue.length;
    }

    cookieValue = cookieValue.substring(valueStart, valueEnd);
    //alert("Cookie received >" + cookieValue);
  }

  return cookieValue;
}

function homepagearrival()
{
  var cookieString = getSponsorFromCookie();

  // If no search string, get Id from cookie
  if (location.search.length == 0)
  {
    /* Check cookie to see whether came here by mistake, if so
     * send to affiliates own shop. */
    if (cookieString != null)
    {
      // If not "hw" then change to that sponsor. Note we can't assume that
      // the sponsor still exists - we may have deleted their shop if we
      // have lost touch - so don't try to go through their index file, just
      // change to the values from the cookie.
      var sponsorFields = cookieString.split(":");
      if (sponsorFields.length == 3)
      {
        sponsorFields[0] = unescape(sponsorFields[0]);
        sponsorFields[1] = unescape(sponsorFields[1]);
        sponsorFields[2] = unescape(sponsorFields[2]);

        my_id_is = sponsorFields[0];
        searchString = sponsorFields[0];
      }
    }
    else // no cookie either
    {
      my_id_is = "usahw";
      searchString = "usahw";
    }
  }
  else // There is a search string
  {
    // Extract relevant parts of search string
    searchString = unescape(location.search.substring(1,location.search.length));
    if (searchString.match(/^([a-zA-Z0-9]+)$/) != null)
    {
      // Search String is JUST the shop owner's Id
      my_id_is = searchString;
      // alert("in arrival - my_id_is: " + my_id_is);

      if (my_id_is != "hw")
      {
        // Save shop Id in cookie, may overwrite previous version
        var expires = new Date();
        expires.setYear(expires.getYear() + 2);
  
        document.cookie = "sponsor=" + escape(my_id_is) + ":" + escape("blank") + ":" + escape("blank") + ";expires=" + expires.toGMTString();
        // alert("Set new cookie with value: " + "sponsor=" + escape(my_id_is) + ":" + escape("blank") + ":" + escape("blank") + ";expires=" + expires.toGMTString());
      }
    }
    else if (searchString.match(/^sponsor=([a-zA-Z0-9]+)\&wptid=([a-zA-Z0-9]+)$/) != null)
    {
      // Search string is sponsor's Id plus transaction Id for registration
      my_id_is = RegExp.$1;
      wptid = RegExp.$2;
      // alert("in arrival - my_id_is: " + my_id_is + ", wptid: " + wptid);
    }
    else // Unexpected search string
    {
      my_id_is = "usaHealthwize";
    }
  }
}

function gone(directory,filename)
{
  // alert("in gone - my_id_is set to " + my_id_is);
  location.href = directory + "/" + filename + ".html?" + escape(searchString); // Pass whole search string
}

function addToBasket(desc,code,price)
{
  var resume = window.location.href;
  
  window.location='http://www.hwize.com/cgi-bin/hllc-usd-cart.cgi?actiontype=add&amp;quantity=1&amp;productdesc='+desc+'&amp;productid='+code+'&amp;hwid='+my_id_is+'&amp;productprice='+price+'&amp;basketReturn='+resume;
}

function viewBasket()
{
  var resume = window.location.href;
  
  window.location='http://www.hwize.com/cgi-bin/hllc-usd-cart.cgi?actiontype=view&amp;basketReturn='+resume;
}

function tabBars(current,level)
{
  var tabs = new Array ("Home","Anti-Ageing","Antioxidants","Arthritis","Cancer","Fitness","Heart",null,"Immune","Ladies","Men","Mind","Pain Relief","Stomach","Vision","Weight");
  var tabJump = new Array ("index.html","anti-ageing/anti-ageing.html","antioxidant/antioxidant.html","arthritis/arthritis.html","cancer/cancer.html","health/fitness.html","heart/heart.html",null,"immune-system/immune-system.html","ladieshealth/ladieshealth.html","menshealth/menshealth.html","mindhealth/mindhealth.html","pain-relief/pain-relief.html","stomach-ulcer/stomach-ulcer.html","vision/vision.html","weight-loss/weight-loss.html");

  var directory = "";
  
  if (level == 1)
  {
    directory = "../";
  }
  
  document.write('<ul class="nav">');
  for (i=0;i < tabs.length;i++)
  {
    // null means split tabs to a new line 
    if (tabs[i] == null)
    {
      document.write('</ul><div class="tabholder"></div><ul class="nav">');
    }
    else
    {
      if (current == tabs[i])
      {
        // this is the currently selected tab
        document.write('<li id="current"><a href="' + directory + tabJump[i] + '" onclick="departure(\'' + directory + tabJump[i] + '\'); return false;"><span>' + tabs[i] + '</span></a></li>');
      }
      else
      {
        document.write('<li><a href="' + directory + tabJump[i] + '" onclick="departure(\'' + directory + tabJump[i] + '\'); return false"><span>' + tabs[i] + '</span></a></li>');
      }
    }
  }
  document.write('</ul><div class="tabholder"></div>');
}

//-->

