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=abg2=" + escape(trail[i]) + ">" + unescape(trail[i]) + "</a><br>");
        }
    }
}

function SaveTrail(id,name,img,cookie,size,price,flag) {
	
	var path = GetCookie(cookie);
	if (path == null)
		path = "";
	
	var elem;
	if (id && name) {
		elem = id + "|" + escape(name) + "|" + img + "|" + price + "|" + flag;
	}
	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 )
  {
      document.write("<div id=recenthistory>");

      document.write('<div class=recentTitle><img src="/lib/yhst-39931238769344/recently-viewed.gif" /></div>');
	  document.write('<table><tr>');
      for ( i = trail.length - 1; i >= 0; i-- )
      {
        elem = trail[i].split("|");
		var id = elem[0];
		var name = unescape(elem[1]);
		var image = elem[2];
		var price = elem[3];
		var flag = elem[4];
		
        if ( id.length > 0 ) {
            document.write("<td><div class=item-wrapper>");
            if (image.length > 0) {
                document.write ( "<div class=item-image><a href=" + id + ".html><img border=0 src=" + image + " /></a></div>" );
            }
            document.write( "<div class=item-bottom><img src=" + flag + " /><div class=name><a href=" + id + ".html>" + name + "</a></div><div class=price>");
            if (typeof(price) != 'undefined' && price != '' && price != 'undefined')
                document.write(price);
            document.write("</div></div>");
        }
        document.write("</div></td>");
      }
	  document.write("</tr></table>");
      document.write("</div>");	 
  }
}

