var http = xmlHttpRequest();
function xmlHttpRequest() {
	var xmlHttp;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
	    {
	    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    }
	  catch (e)
	    {
	    try
	      {
	      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	      }
	    catch (e)
	      {
	      alert("Your browser does not support AJAX!");
	      return false;
	      }
	    }
	  }
	  return xmlHttp;
}

function updateChat() {
	if (!http) { 
		var http = xmlHttpRequest(); 
	}
	http.onreadystatechange = function() {
		if (http.readyState==4) {
			document.getElementById("chat_div").innerHTML = http.responseText;
			setTimeout("updateChat()",1000);
		}
	}
	http.open("post","chat_server.php",true);
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	http.send("action=update");
};
function addBuddy(buddy) {
  if (!http) {
    var http = xmlHttpRequest();
  }
  http.onreadystatechange = function() {
    if (http.readState == 4) {
      var chatdiv = document.getElementById("chat_div")
      if (!chatdiv) {
        alert("Error finding chat_div object.");
      }
      chatdiv.innerHTML = "This is a test";
    }
  }
	http.open("post","chat_server.php",true);
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	http.send("action=addbuddy&buddy="+encodeURI(buddy));
}
function sendMessage()
{
	if (!http) { 
		var http = xmlHttpRequest(); 
	}
	http.onreadystatechange = function() {
		if (http.readyState==4)
		{
			updateChat();
			document.getElementById("chat_send").value = "";
		}
	}
	var message = document.getElementById("chat_send").value;
	if (message=="") {
		alert("Please fill in the text box..");
		return 0;
	}
	http.open("post","chat_server.php",true);
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	http.send("action=send&message="+encodeURI(message));
}


