window.onload = chat_begin;
var http_receive = ajax_begin();

function chat_begin() {
  document.getElementById("box").innerHTML = "<p>Your browser will display chatroom commentary.</p>";
  if (window.location.href.substring(window.location.href.length - 5) == "#chat") {
    document.getElementById('chat_message').focus();
  }
  chat_get();
}

function chat_get() {
	if (http_receive.readyState == 4 || http_receive.readyState == 0) {
  	http_receive.open("GET", 'index.php?chat=request', true);
    http_receive.onreadystatechange = chat_process;
  	http_receive.send(null);
	}
}

function chat_process() {
  if (http_receive.readyState == 4) {
    if ((document.getElementById('box').scrollHeight - document.getElementById('box').scrollTop) < 250) {
      document.getElementById('box').innerHTML = http_receive.responseText;
      document.getElementById('box').scrollTop = document.getElementById('box').scrollHeight;
    } else {
      document.getElementById('box').innerHTML = http_receive.responseText;
    }
    setTimeout('chat_get();', 5000);
  }
}

function ajax_begin() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        xmlhttp = false;
      }
    }
  @else
    xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}
