var xmlHttp=createXmlHttpRequestObject();
var xmlHttpMsg=createXmlHttpMsgRequestObject();

function createXmlHttpRequestObject()
{
  var xmlHttp;

  try {xmlHttp = new XMLHttpRequest();}
  catch(e)
  {
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP");
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
    {
      try{xmlHttp = new ActiveXObject(XmlHttpVersions[i]);}
      catch (e) {}
    }
  }
  if (!xmlHttp) alert("Error creating the XMLHttpRequest object."); else return xmlHttp;
}

function createXmlHttpMsgRequestObject()
{
  var xmlHttpMsg;

  try {xmlHttpMsg = new XMLHttpRequest();}
  catch(e)
  {
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP");
    for (var i=0; i<XmlHttpVersions.length && !xmlHttpMsg; i++)
    {
      try{xmlHttpMsg = new ActiveXObject(XmlHttpVersions[i]);}
      catch (e) {}
    }
  }
  if (!xmlHttpMsg) alert("Error creating the XMLHttpRequest object."); else return xmlHttpMsg;
}


function update_chat()
{
  if (xmlHttp.readyState==4 || xmlHttp.readyState==0)
  {
    xmlHttp.open("GET","mod/chat/updatechat.php", true);
	xmlHttp.onreadystatechange=ChatServerResponse;
    xmlHttp.send(null);
  }
  else {setTimeout("update_chat()", 300);}
}

function ChatServerResponse()
{
  if (xmlHttp.readyState==4)
  {
    if (xmlHttp.status==200)
    {
      document.getElementById('chat_content').innerHTML=xmlHttp.responseText;
      scroll_down();
    }
	else {alert("Ошибка при обращении к серверу: "+xmlHttp.statusText);}
  };
}

function chat_insert()
{
  if (xmlHttpMsg.readyState==4 || xmlHttpMsg.readyState==0)
  {
    chat_msg=document.getElementById('chat_input').value;
    xmlHttpMsg.open("GET","mod/chat/inschat.php?msg="+chat_msg, true);
	xmlHttpMsg.onreadystatechange=ChatInsertServerResponse;
    xmlHttpMsg.send(null);
  }
  else {setTimeout("chat_insert()", 300);}
}

function ChatInsertServerResponse()
{
  if (xmlHttpMsg.readyState==4)
  {
    if (xmlHttpMsg.status==200)
    {
      document.getElementById('chat_input').value='';
      update_chat();
    }
	else {alert("Ошибка при обращении к серверу: "+xmlHttpMsg.statusText);}
  };
}
