var ICstatusCountdownStart = 300000;
var ICstatusCountdown = ICstatusCountdownStart;
var old_status_code = ' ';

function ICupdateMemoMesg(status_code)
{
  if (status_code == old_status_code) return;

  var memo_obj = document.getElementById('newmemo');
  var mesg_obj = document.getElementById('newchatmesg');
      
  if ((memo_obj == null) || (mesg_obj == null)) return;

  if ((status_code == "1") || (status_code == "3"))
       memo_obj.innerHTML = '<img src="http://img.informedconsent.co.uk/icons/new_memo.gif" width=27 height=13 border=0>';
  else memo_obj.innerHTML = '<xxx>';

  if ((status_code == "2") || (status_code == "3"))
       mesg_obj.innerHTML = '<img src="http://img.informedconsent.co.uk/icons/new_chat_mesg.gif" width=27 height=13 border=0>';
  else mesg_obj.innerHTML = '<xxx>';

  old_status_code = status_code;
}

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

    ICupdateMemoMesg(response.substring(4,5));
  }

  if ((http_status.readyState == 4) && (http_status.status == 403))
  {
    ICstatusCountdownStart = 0;
  }
}

function ICcheckStatus()
{
  var allcookies = document.cookie;
  var http_status;
  
  ICstatusCountdown -= 1000;

  if (ICstatusCountdown <= 0)
  {
      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("");
      }

      ICstatusCountdown = ICstatusCountdownStart;
  }

  if (ICstatusCountdownStart > 0) setTimeout("ICcheckStatus()", 1000);
}

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