// convert all characters to lowercase to simplify testing
var agt = navigator.userAgent.toLowerCase();

var is_nav = (agt.indexOf('mozilla') != -1);

var is_ie = (agt.indexOf("msie") != -1);

var next_year = new Date();
next_year.setFullYear(next_year.getFullYear() + 1);

function ltrim(str)
{
   // We don't want to trim JUST spaces, but also tabs,
   // line feeds, etc.
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with a leading whitespace character...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

function rtrim(str)
{
   // We don't want to trim JUST spaces, but also tabs,
   // line feeds, etc.
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with a trailing whitespace character...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

function trim(str)
{
   return rtrim(ltrim(str));
}

function getCookie(name)
{
  var start = document.cookie.indexOf(name+"=");
  if (start == -1) return null;
  var len = start+name.length+1;
  if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
  var end = document.cookie.indexOf(";",len);
  if (end == -1) end = document.cookie.length;
  return unescape(document.cookie.substring(len,end));
}

function setCookie(name,value,expires,path,domain,secure)
{
  document.cookie = name + "=" + escape(value) +
    ( (expires) ? ";expires=" + expires.toGMTString() : "") +
    ( (path) ? ";path=" + path : "") + 
    ( (domain) ? ";domain=" + domain : "") +
    ( (secure) ? ";secure" : "");
}

function deleteCookie(name,path,domain)
{
  if (get_cookie(name)) document.cookie = name + "=" +
       ( (path) ? ";path=" + path : "") +
       ( (domain) ? ";domain=" + domain : "") +
       ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function addPage(pageStr)
{
  var currentPages = getCookie('abchistory');
  if (currentPages == null)
  {
    setCookie('abchistory', pageStr, next_year);
  }
  else
  {
    var newPage = false;
    if (currentPages.indexOf(pageStr) == -1)
      newPage = true;
    var pagesArray = currentPages.split('|');
    if (newPage && (pagesArray.length == 5))
      pagesArray.shift();
    currentPages = '';
    for (i=0; i<pagesArray.length; i++)
    {
      if (newPage || (pagesArray[i] != pageStr))
      {
        currentPages += (pagesArray[i] + '|');
      }
    }
    currentPages += pageStr;
    setCookie('abchistory', currentPages, next_year);
  }
}