var BSVAR;
if (! BSVAR) BSVAR = new Object();

var pagetype;
if (! pagetype) pagetype = "horse";

// push new element 'ne' after sibling 'oe' old element
function addAfter (oe, ne) {
    if (oe.nextSibling) {
        oe.parentNode.insertBefore(ne, oe.nextSibling);
    } else {
        oe.parentNode.appendChild(ne);
    }
}

// hsv to rgb
// h, s, v = [0, 1), [0, 1], [0, 1]
// r, g, b = [0, 255], [0, 255], [0, 255]
function hsv_to_rgb (h, s, v)
{
    if (s == 0) {
   v *= 255;
   return [v,v,v];
    }

    h *= 6;
    var i = Math.floor(h);
    var f = h - i;
    var p = v * (1 - s);
    var q = v * (1 - s * f);
    var t = v * (1 - s * (1 - f));

    v = Math.floor(v * 255 + 0.5);
    t = Math.floor(t * 255 + 0.5);
    p = Math.floor(p * 255 + 0.5);
    q = Math.floor(q * 255 + 0.5);

    if (i == 0) return [v,t,p];
    if (i == 1) return [q,v,p];
    if (i == 2) return [p,v,t];
    if (i == 3) return [p,q,v];
    if (i == 4) return [t,p,v];
    return [v,p,q];
}

// stops the bubble
function stopBubble (e) {
    if (e.stopPropagation)
        e.stopPropagation();
    if ("cancelBubble" in e)
        e.cancelBubble = true;
}

// stops the bubble, as well as the default action
function stopEvent (e) {
    stopBubble(e);
    if (e.preventDefault)
        e.preventDefault();
    if ("returnValue" in e)
        e.returnValue = false;
    return false;
}

function scrollTop () {
    if (window.innerHeight)
        return window.pageYOffset;
    if (document.documentElement && document.documentElement.scrollTop)
        return document.documentElement.scrollTop;
    if (document.body)
        return document.body.scrollTop;
}

function scrollLeft () {
   if (window.innerWidth)
      return window.pageXOffset;
   if (document.documentElement && document.documentElement.scrollLeft)
      return document.documentElement.scrollLeft;
   if (document.body)
      return document.body.scrollLeft;
}

function getElementPos (obj)
{
   var pos = new Object();
   if (!obj)
      return null;

   var it;

   it = obj;
   pos.x = 0;
   if (it.offsetParent) {
      while (it.offsetParent) {
         pos.x += it.offsetLeft;
         it = it.offsetParent;
      }
   }
   else if (it.x)
      pos.x += it.x;

   it = obj;
   pos.y = 0;
   if (it.offsetParent) {
      while (it.offsetParent) {
         pos.y += it.offsetTop;
         it = it.offsetParent;
      }
   }
   else if (it.y)
      pos.y += it.y;

   return pos;
}

function getEventPos (e, fallBack)
{
   var pos = { x:0, y:0 };

   if (!e) var e = window.event;
   if (e.pageX && e.pageY) {
      // useful case (relative to document)
      pos.x = e.pageX;
      pos.y = e.pageY;
   }
   else if (e.clientX && e.clientY) {
      // IE case (relative to viewport, so need scroll info)
      pos.x = e.clientX + scrollLeft();
      pos.y = e.clientY + scrollTop();
   } else {
      var targ = fallBack || getTarget(e);
      var pos = getElementPos(targ);
      return pos;
   }
   return pos;
}

var curPopup = null;
var curPopup_id = 0;

function killPopup () {
    if (!curPopup)
        return true;

    var popup = curPopup;
    curPopup = null;

    var opp = 1.0;

    var fade = function () {
        opp -= 0.15;

        if (opp <= 0.1) {
            popup.parentNode.removeChild(popup);
        } else {
            popup.style.filter = "alpha(opacity=" + Math.floor(opp * 100) + ")";
            popup.style.opacity = opp;
            window.setTimeout(fade, 20);
        }
    };
    fade();

    return true;
}

function docClicked (e) {
    if (!curPopup) 
      return true;
    killPopup();
    return true;
}

function createPopup (ae, dItemid) {
   return function (e) {
      if (!e) e = window.event;
      var FS = arguments.callee;

      var finalHeight = 70;

      var doIT = 0;

      if (! bs_popinfo)
         return true;

      var clickTarget = getTarget(e);
      var used_keyboard = clickTarget.nodeName == "A";

      var pos = used_keyboard ? getElementPos(ae) : getEventPos(e);
      var lx = pos.x + 5 - 125;
      if (lx < 5) lx = 5;
      var de;

      if (curPopup && curPopup_id == dItemid) {
         de = curPopup;
         de.style.left = lx + "px";
         de.style.top = (pos.y + 10) + "px";
         return stopEvent(e);
      }

      de = document.createElement("div");
      de.style.textAlign = "left";
      de.className = 'bspopmanage';
      de.style.height = "10px";
      de.style.overflow = "hidden";
      de.style.position = "absolute";
      de.style.left = lx + "px";
      de.style.top = (pos.y + 10) + "px";
      de.style.width = "250px";

      regEvent(de, "click", function (e) {
         e = e || window.event;
         stopBubble(e);
         return true;
      });

      var inHTML = "<form method='post' action='retailers.php?type=" + pagetype + "' style='display: inline' id='bsdelopts" + dItemid + "'><span style='font-face: Arial; font-size: 8pt'><b>enter your postcode</b><br />";
      var lbl;
inHTML += "   <table cellpadding='0' cellspacing='0'>";

/* inHTML += "    <tr>";
inHTML += "      <td><b><font face='Arial' size='1'>name</font></b></td>";
inHTML += "      <td><input type='text' name='FullName' size='25'></td>";
inHTML += "    </tr>";
inHTML += "    <tr>";
inHTML += "      <td><b><font face='Arial' size='1'>address</font></b></td>";
inHTML += "      <td><input type='text' name='Address1' size='25'></td>";
inHTML += "    </tr>";
inHTML += "    <tr>";
inHTML += "      <td>&nbsp;</td>";
inHTML += "      <td><input type='text' name='Address2' size='25'></td>";
inHTML += "    </tr>";
inHTML += "    <tr>";
inHTML += "      <td><b><font face='Arial' size='1'>town</font></b></td>";
inHTML += "      <td><input type='text' name='Town' size='25'></td>";
inHTML += "    </tr>";
inHTML += "    <tr>";
inHTML += "      <td><b><font face='Arial' size='1'>county</font></b></td>";
inHTML += "      <td><input type='text' name='County' size='25'></td>";
inHTML += "    </tr>"; */

inHTML += "    <tr>";
 // inHTML += "      <td><b><font face='Arial' size='1'>post code:&nbsp;</font></b></td>";
inHTML += "      <td><input type='text' name='post' size='25'></td>";
inHTML += "    </tr>";
inHTML += "   </table>";

            
      inHTML += "<input type='submit' value='go'> <input type='button' value='cancel' onclick='killPopup()' /></span><br /></form>";
      de.innerHTML = inHTML;

      // we do this so keyboard tab order is correct:
      addAfter(ae, de);

      curPopup = de;
      curPopup_id = dItemid;

      var height = 10;
      var grow = function () {
         height += 7;
         if (height > finalHeight) {
            de.style.height = null;
            de.style.filter = "";
            de.style.opacity = 1.0;
         } else {
            de.style.height = height + "px";
            window.setTimeout(grow, 20);
         }
      };
      grow();

      return stopEvent(e);
   }
}

function getTarget (ev) {
    var target;
    if (ev.target)
        target = ev.target;
    else if (ev.srcElement)
        target = ev.srcElement;

    // Safari bug:
    if (target && target.nodeType == 3)
        target = target.parentNode;

    return target;
}

function setupAjax () {
   var ct = document.links.length;
   for (var i=0; i<ct; i++) {
      var ae = document.links[i];
      if (ae.href.indexOf("retailers.php") != -1) {

         var findIDre = /id=(\d+)/;
         var reMatch = findIDre.exec(ae.href);
         if (! reMatch) return true;

         var dItemid = reMatch[1];
         var todel = document.getElementById("bspop" + dItemid);
         if (! todel) return true;

         ae.onclick = createPopup(ae, dItemid);
      }
   }
}

function regEvent (target, evt, func) {
   if (! target) return;
   if (target.attachEvent)
        target.attachEvent("on"+evt, func);
   if (target.addEventListener)
        target.addEventListener(evt, func, false);
}

if (document.getElementById) {
   regEvent(window, "load", setupAjax);
   regEvent(document, "click", docClicked);
   document.write("<style>div.bspopmanage { color: #000000; background: #e0e0e0; border: 2px solid #00000; padding: 3px; }</style>");
}

