/*
 * Copyright 2006, Peter Rowntree. All Rights Reserved.
 * http://www.hdyn.com/wr/common/contact.php?addr=pr
 */
var g_qs="?grp";

//flags: 1:check qs; 2:append qs; 4:add jump to history;
function goRandom(rootID,flags)
{  
   if((flags & 1) != 0 && !qsStartsWith(g_qs))
      return;
   var rootNode=document.getElementById(rootID);
   var url=randLink(rootNode);
   if(url == null)
      return;
   if((flags & 2) != 0)
      url+=g_qs;
   if((flags & 4) != 0)
      window.location.href=url;
   else if(typeof window.location.replace != "undefined")
   {
      rootNode.innerHTML="getting a random link..."
      window.location.replace(url); //see notes re: IE6 & httpd AddDefaultCharset
   }
   else
      window.location=url;
}

function qsStartsWith(s)
{  
   var qs=window.location.search;
   return qs != null && qs.indexOf(s) >= 0;
}

function randLink(node)
{  
   var nds=node.getElementsByTagName("a");
   if(nds == null || nds.length < 1)
      return null;
   return nds.item(rand(nds.length)).href;
}

function rand(lt)
{
   return Math.floor(Math.random()*0.99999*lt);
}