function getCookieVal (offset) {
   var endstr = document.cookie.indexOf (";", offset);

   if (endstr == -1)
      endstr = document.cookie.length;

   return unescape(document.cookie.substring(offset, endstr));
}

function SetTargetCookie (name, value) {
   // set expiry date to 1 year from now.
   var expires = new Date ();
   expires.setTime(expires.getTime() + (30 * 24 * 3600 * 1000));

   document.cookie = name + "=" + escape (value) +
                     "; expires=" + expires.toGMTString();
}

function updateTargetCookieT (obj) {
   if (obj.checked) {
      SetCookie("settarget", "true");
   } else {
      SetCookie("settarget", "false");
   }
   location.reload();
}

function updateTargetCookie (obj) {
   if (obj.checked) {
      SetTargetCookie("bbgclr", "true");
   } else {
      SetTargetCookie("bbgclr", "false");
   }
   location.reload();
}

function GetCookie (name) {
   var arg = name + "=";
   var alen = arg.length;
   var clen = document.cookie.length;
   var i = 0;

   while (i < clen) {
      var j = i + alen;

      if (document.cookie.substring(i, j) == arg)
         return getCookieVal (j);

      i = document.cookie.indexOf(" ", i) + 1;
      if (i == 0)
         break;
   }

   return null;
}


// Logo launch code by Jason "loonyboi" Bergman and his team of infinite monkeys
function launchlogo() {
   logoinfo = window.open('/popup.shtml','logoinfo','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=360,height=350,left=50,top=50');
}

// Smilies launch code by Frans P. de Vries, based on the above
function launchsmilies() {
   smileyinfo = window.open('/cgi-bin/smilies.pl','smileyinfo','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=768,height=400,left=50,top=50');
}

// Check for messageboard updates by Richard Smith/James Furness
function board_checkupdate(updatedunix, boardid, threadid) {
   // Get chatbear session cookie
   var lastvisitcookie = GetCookie('chatbear-session-' + boardid);

   // If the session cookie exists they have been reading threads this session
   if (lastvisitcookie) {
      // Split into viewed threads and the time of the last session
      var lastvisitpos = lastvisitcookie.indexOf(':');
      var lastvisit = lastvisitcookie.substring(0, lastvisitpos);
      var sessioncontents = lastvisitcookie.substring(lastvisitpos + 1);

      // If their last session was before the thread was updated and have not viewed it this session then print
      // a red *
      if (updatedunix > lastvisit) {
         if (sessioncontents.indexOf(threadid + '-') == -1) {
            document.write('<font color="red">*</font>');
         }
      }

   // This is a new session, anything updated since last session counts
   } else {
      var lastvisit = GetCookie('chatbear-lastvisit-' + boardid);

      if (updatedunix > lastvisit) {
         document.write('<font color="red">*</font>');
      }
   }

   return null;
}

var settarget = GetCookie('settarget');

if (settarget == "true") {
   document.write('<base target="_blank">');
} else {
   document.write('<base target="_top">');
}


// popup

function getCookieValue(cookieName)
{
   var cookieValue = document.cookie;
   var cookieStartsAt = cookieValue.indexOf(" " + cookieName + "=");
   if (cookieStartsAt == -1)
   {
       cookieStartsAt = cookieValue.indexOf(cookieName + "=");
   }
   if (cookieStartsAt == -1)
   {
       cookieValue = null;
   }
   else
   {
       cookieStartsAt = cookieValue.indexOf("=",  cookieStartsAt) + 1;
       var cookieEndsAt = cookieValue.indexOf(";",  cookieStartsAt);
       if (cookieEndsAt == -1)
       {
            cookieEndsAt = cookieValue.length;
       }
       cookieValue = unescape(cookieValue.substring(cookieStartsAt, cookieEndsAt));
   }
   return cookieValue;
}

function setCookie(cookieName, cookieValue, cookiePath, cookieExpires)
{
     cookieValue = escape(cookieValue);
     if (cookieExpires == "")
     {
         var nowDate = new Date();
         nowDate.setMinutes(nowDate.getMinutes() + 26);
         cookieExpires = nowDate.toGMTString();
     }
     if (cookiePath != "")
     {
        cookiePath = ";Path=" + cookiePath;
     }
     document.cookie = cookieName + "=" + cookieValue + ";expires=" + cookieExpires + cookiePath;
}

