function GenericPopup(src)
{
window.open(src, "win", "width=590,height=590,resizable=1,scrollbars=1,scrolling=auto")
}

function RemoveStr(src,target)
{
  var result = src;
  
  var i = result.indexOf(target);
  if ( i > -1 ) // if the elem is already stored, remove it first.
  {
    var left = result.substr(0,i);
    var right = result.substr(i + target.length);
    if (left.substr( left.length-1, 1 ) == ",")
    {
        left = left.substr(0, left.length - 1);
    }
    if (right.substr(0,1) == ",")
    {
        right = right.substr(1);
    }
    if (right.length > 0 && left.length > 0)
    {
      result = left + "," + right;
    }
    else if (left.length > 0)
    {
      result = left;
    }
    else
    {
      result = right;
    }
  }
  return (result);
}

function SaveSearch(frm)
{
  SaveTrail(frm.query.value,null,null,"Search",4);
}

function ShowSearches()
{
    var searches = GetCookie("Search");
    if (searches == null)
      searches = ""
    var trail = searches.split(",");
    while ( trail.length > 0 && trail[0] == "" )
    {
        trail.shift();
        l = trail.length;
    }

    if ( trail.length > 0 )
    {
        document.write("<label>Your Recent Searches</label>");
        for ( i = trail.length - 1; (i >= trail.length - 6) && (i >= 0); i-- )
        {
            document.write("<a href=http://search.store.yahoo.com/cgi-bin/nsearch?catalog=ytimes&query=" + escape(trail[i]) + ">" + unescape(trail[i]) + "</a><br>");
        }
    }
}

function SaveTrail(id,name,img,cookie,size)
{
  var path = "";
  var path = GetCookie(cookie);
  if (path == null)
    path = "";

  var elem;
  if (id && name)
  {
    elem = id + "|" + escape(name) + "|" + img;
  }
  else
  {
    elem = id;
  }
  path = RemoveStr(path,elem);
  var trail = path.split(",");
  var l = trail.push( elem );
  while ( trail.length > 0 && trail[0] == "" )
  {
    trail.shift();
    l = trail.length;
  }
  while ( l > parseInt(size) )
  {
    trail.shift();
    l = trail.length;
  }
  
  path = trail.toString();
  // make it expire in one day
  var dt = new Date();
  dt.setTime(dt.getTime() + 24 * 60 * 60 * 1000);
  SetCookie(cookie, path, dt);
}

function ShowTrail()
{
    var path = GetCookie("Path");
    if (path == null)
      path = "";

  var trail = path.split(",");
  while ( trail.length > 0 && trail[0] == "" )
  {
    trail.shift();
    l = trail.length;
  }

  if ( trail.length > 0 )
  {
      document.write("<b>Recently Viewed Categories:</b><br><div id=trail><ul>");
      for ( i = trail.length - 1; (i >= trail.length - 6) && (i >= 0); i-- )
      {
        elem = trail[i].split("|");
        if ( elem[0].length > 0 )
        {
            document.write ( "<li><a href=" + elem[0] + ".html>" + unescape(elem[1]) + "</a></li> ");
        }
      }
      document.write("</ul></div>");
  }
}

function ShowFullTrail(type)
{
    var ck = (type == "Items" ? "Path" : "Section");

    var path = GetCookie(ck);
    if (path == null)
      path = "";

  var trail = path.split(",");
  while ( trail.length > 0 && trail[0] == "" )
  {
    trail.shift();
    l = trail.length;
  }

  if ( trail.length > 0 )
  {
    var columns = 1;
    var NewRow = true;
    var InRow = false;
    var colwid = 100 / columns;
    
    if (typeof(layoutV2) != 'undefined')
    {
        document.write("<img border=0 width=148 height=21 src=/lib/yhst-81126207287951/recently-viewed-v2.gif><br>");
    }
    else
    {
        document.write("<img border=0 width=150 height=39 src=http://site.brickhousesecurity.com/images/recentlyviewed-top.gif><br>");
    }

    document.write("<div id=recenthistory>");
    document.write("<table border=0 cellpadding=0 cellspacing=3>");
    var j = 1;
    for ( i = trail.length - 1; i >= 0; i-- )
    {
        if (NewRow)
        {
            document.write("<tr valign=top>");
            NewRow = false;
            InRow = true;
        }
        elem = trail[i].split("|");
        if ( elem[0].length > 0 )
        {
            document.write("<td width=" + colwid + "%><table border=0 cellpadding=0 cellspacing=2 width=100%><tr><td align=center width=40>");
            if (elem[2].length > 0)
            {
                document.write ( "<img border=0 align=absmiddle src=" + elem[2] + ">&nbsp;" );
            }
            document.write("</td><td>");
            document.write( "<a href=" + elem[0] + ".html>" + unescape(elem[1].replace(/`/gi, "'").replace(/%26%2396%3B/gi,"'")) + "</a><br>");
            document.write("</td></tr></table></td>");
        }
        if (j % columns == 0)
        {
            document.write("</tr>");
            NewRow = true;
            InRow = false;
        }
        j++;
    }
    if (InRow)
        document.write("</tr>");
    document.write("</table></div>");
  }
}

function ShowFullTrail2010(type)
{
    var ck = (type == "Items" ? "Path" : "Section");

    var path = GetCookie(ck);
    if (path == null)
      path = "";

  var trail = path.split(",");
  while ( trail.length > 0 && trail[0] == "" )
  {
    trail.shift();
    l = trail.length;
  }

  if ( trail.length > 0 )
  {
    var columns = 6;
    var NewRow = true;
    var InRow = false;
    var colwid = 100 / columns;

	jQuery("#tab2btn3").css("display","block");
	jQuery("#tabsFrame #tabbtn2").css("display","block");
    document.write("<div id=recenthistory>");
    document.write("<table border=0 cellpadding=0 cellspacing=3>");
    var j = 1;
    for ( i = trail.length - 1; i >= 0; i-- )
    {
        if (NewRow)
        {
            document.write("<tr valign=top>");
            NewRow = false;
            InRow = true;
        }
        elem = trail[i].split("|");
        if ( elem[0].length > 0 )
        {
            document.write("<td width=150 align=left valign=top>");
            document.write("<div class=bestSellerItem>");
            if (elem[2].length > 0)
            {
                document.write ( "<a href=" + elem[0] + ".html><img border=0 align=absmiddle src=" + elem[2] + "></a>" );
            }
            document.write("<div class=bestSellerName><a href=" + elem[0] + ".html>" + unescape(elem[1].replace(/`/gi, "'").replace(/%26%2396%3B/gi,"'")) + "</a></div>");
            if (elem[3] && elem[3].length > 0)
            {
                document.write ( "<div class=bestSellerPrice>" + elem[3] + "</a>" );
            }
            document.write("</div>");
            document.write("</td>");
        }
        if (j % columns == 0)
        {
            document.write("</tr>");
            NewRow = true;
            InRow = false;
        }
        j++;
    }
    if (InRow)
        document.write("</tr>");
    document.write("</table></div>");
  }
}

function ShowFullTrailNav(type)
{
    var ck = (type == "Items" ? "Path" : "Section");

    var path = GetCookie(ck);
    if (path == null)
      path = "";

  var trail = path.split(",");
  while ( trail.length > 0 && trail[0] == "" )
  {
    trail.shift();
    l = trail.length;
  }

  if ( trail.length > 0 )
  {
    var columns = 1;
    var NewRow = true;
    var InRow = false;
    var colwid = 100 / columns;

	jQuery("label#recentlyNavLabel").css("display","block");
    document.write("<table border=0 cellpadding=0 cellspacing=0 id=itemRelatedTable>");
    var j = 1;
    for ( i = trail.length - 1; i >= 0; i-- )
    {
        if (NewRow)
        {
            document.write("<tr>");
            NewRow = false;
            InRow = true;
        }
        elem = trail[i].split("|");
        if ( elem[0].length > 0 )
        {
            document.write("<td align=center valign=top class=itemRelatedImg>");
            if (elem[2].length > 0)
            {
                document.write ( "<a href=" + elem[0] + ".html><img border=0 align=absmiddle src=" + elem[2] + " width=50 height=50></a>" );
            }
            document.write("</td><td align=left valign=top class=itemRelatedName>");
            document.write("<a href=" + elem[0] + ".html>" + unescape(elem[1].replace(/`/gi, "'").replace(/%26%2396%3B/gi,"'")) + "</a>");
            document.write("</td>");
        }
        if (j % columns == 0)
        {
            document.write("</tr>");
            NewRow = true;
            InRow = false;
        }
        j++;
    }
    if (InRow)
        document.write("</tr>");
    document.write("</table>");
  }
}
