var old_num_memos = -1;
var old_num_messages = -1;
var IClastSignificantEvent = (new Date()).getTime();
var ICstatusDisabled = 0;
var ICstatusInterval = 55000;

function ICupdateMemoMesg(num_memos, num_messages)
{
  if ((num_memos == old_num_memos) && 
      (num_messages == old_num_messages)) return;

  var topbox_obj = document.getElementById('topbox');
  var adbox_obj = document.getElementById('adbox');
  var contentbox_obj = document.getElementById('contentbox');
  var footerbox_obj = document.getElementById('footerbox');
  var memo_obj = document.getElementById('topboxmemos');
  var memo_num_obj = document.getElementById('topboxmemosnum');
  var mesg_obj = document.getElementById('topboxmessages');
  var mesg_num_obj = document.getElementById('topboxmessagesnum');
      
  if ((topbox_obj == null) || (memo_obj == null) || (mesg_obj == null)) return;

  if ((num_memos > 0) || (num_messages > 0))
  {
    if (!ICsmartphone)
    {
      adbox_obj.style.paddingTop = "30px";
      topbox_obj.style.position = "fixed";
      topbox_obj.style.width = "100%";
    }
    topbox_obj.style.backgroundColor = "#800000";
    contentbox_obj.style.borderColor = "#800000";
    footerbox_obj.style.borderColor  = "#800000";
  }
  else
  {
    if (!ICsmartphone)
    {
      adbox_obj.style.paddingTop = "0px";
      topbox_obj.style.position = "inherit";
      topbox_obj.style.width = "inherit";
    }
    topbox_obj.style.backgroundColor = "#000040";
    contentbox_obj.style.borderColor = "#000080";
    footerbox_obj.style.borderColor  = "#000080";
  }

  if (num_memos > 0)
  {
       memo_obj.style.padding = '5px';
       memo_obj.style.color = '#800000';
       memo_obj.style.backgroundColor = '#FFFFFF';
       memo_num_obj.innerHTML = ' (' + num_memos + ')';
  }
  else 
  {
       memo_obj.style.padding = '';
       memo_obj.style.color = '';
       memo_obj.style.backgroundColor = '';
       memo_num_obj.innerHTML = '';
  }

  if (num_messages > 0)
  {
       mesg_obj.style.padding = '5px';
       mesg_obj.style.color = '#800000';
       mesg_obj.style.backgroundColor = '#FFFFFF';
       mesg_num_obj.innerHTML = ' (' + num_messages + ')';
  }
  else 
  {
       mesg_obj.style.padding = '';
       mesg_obj.style.color = '';
       mesg_obj.style.backgroundColor = '';
       mesg_num_obj.innerHTML = '';
  }

  old_num_memos = num_memos;
  old_num_messages = num_messages;
}

function IChandleStatusResponse(http_status)
{
  if ((http_status.readyState == 4) && (http_status.status == 200))
  {
    var response = http_status.responseText;

    ICupdateMemoMesg(parseInt(response.substring(3,6),10), 
                     parseInt(response.substring(6,9),10));
  }

  if ((http_status.readyState == 4) && (http_status.status == 403))
  {
    ICstatusDisabled = 1;
  }
}

function ICcheckStatus()
{
   if (ICstatusDisabled) return;

   if (IClastSignificantEvent > (new Date()).getTime() - ICstatusInterval)
   {
      var allcookies = document.cookie;
      var http_status;

      if (navigator.appName == "Microsoft Internet Explorer")
      {
        http_status = new ActiveXObject("Microsoft.XMLHTTP");
      }
      else http_status = new XMLHttpRequest();

      if ((http_status != null) && 
          (allcookies.indexOf("IC3name=") >= 0) &&
          (allcookies.indexOf("IC3name=Guest") == -1))
      {
        http_status.open('POST', '/chat/', true);
        http_status.setRequestHeader('Content-Type',
                                'application/x-www-form-urlencoded');
        http_status.onreadystatechange = function() { IChandleStatusResponse(http_status); } ;

        http_status.send("action=getstatus");
      }
      else if (http_status != null)
      {
        http_status.open('GET', '/chat/stillonline/', true);
        http_status.send("");
      }
   }

   setTimeout("ICcheckStatus()", ICstatusInterval);
}

function ICinit()
{
  setTimeout("ICcheckStatus()", ICstatusInterval);
}


if ((location.pathname.substr(0,17) != "/settings/sign-in")
    && ((document.cookie.indexOf("IC3splashshown") == -1) 
        || (document.cookie.indexOf("IC3session1") == -1)))
{
   document.cookie = "IC3splashshown=1; domain=informedconsent.co.uk; expires=Wed, 1 Jan 2020 12:00:00 GMT";
   document.cookie = "IC3splashshown=1; domain=informedconsent.co.uk";
    
   if (document.cookie.indexOf("IC3splashshown") != -1)
   {
      document.write('<div style="position: absolute; z-index: 100; background-color:white">');
      location.replace("/settings/sign-in" + location.pathname);
   }    
}

