function getXmlHttpRequestObject() {
var xhr;
try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
catch (e)
{
try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
catch (e2)
{
try {  xhr = new XMLHttpRequest();     }
catch (e3) {  xhr = false;   }
}
}
return xhr;
}
var sendReq = getXmlHttpRequestObject();
var receiveReq = getXmlHttpRequestObject();
var lastMessage = 0;
var lastUser = 0;
var userID = 0 ;
var mTimer;
function imageURL(id)
{
return "images/user"+(id%3+1)+".png";
}
//Function for initializating the page.

function startChat() {
//Set the focus to the Message Box.
document.getElementById('img_user').src = imageURL(userID);
document.getElementById('name_user').innerHTML = nickname;
document.getElementById('txt_message').focus();
//Start Recieving Messages.
getChatText();
}


//Gets the current messages from the server
function getChatText() {
if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {

receiveReq.open("POST", 'getChat.php?userid='+userID+'&name='+nickname+'&chat='+chat+'&lastuser='+lastUser+'&last=' + lastMessage, true);
receiveReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
receiveReq.onreadystatechange = handleReceiveChat;
var param = '';
param += 'name='+nickname;
param += '&chat='+chat;
	receiveReq.send(param);				
	
}			
}
function handleNewUser()
{
if (sendReq.readyState == 4) {					
						
	var xmldoc = sendReq.responseXML;
	
	var user_node = xmldoc.getElementsByTagName("userid"); 	
	//alert(user_node);
	//alert(xmldoc.getElementsByTagName("userid"));				
	
	userID = user_node[0].firstChild.nodeValue ;
	
	
	startChat();

}

				
}
function NewUser(){

if (sendReq.readyState == 4 || sendReq.readyState == 0) {
	sendReq.open("POST", 'newUser.php?chat='+chat, true);
sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
sendReq.onreadystatechange =  handleNewUser;
param = 'nickname='+nickname;
param += '&lang='+lang;
sendReq.send(param);


}
}

function RemoveUser(){
	if (sendReq.readyState == 4 || sendReq.readyState == 0) {
		sendReq.open("POST", 'removeUser.php?chat='+chat, true);
		sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		//sendReq.onreadystatechange =  ;
		param = 'nickname='+nickname;
		sendReq.send(param);
	}
}
//Add a message to the chat server.
function sendChatText() {
if(document.getElementById('txt_message').value == '') {
alert("You have not entered a message");
return;
}
if (sendReq.readyState == 4 || sendReq.readyState == 0) {

sendReq.open("POST", 'getChat.php?userid='+userID+'&chat='+chat+'&lastuser='+lastUser+'&last=' + lastMessage, true);
	sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	sendReq.onreadystatechange = handleSendChat; 
	var param = 'message=' + document.getElementById('txt_message').value;
	param += '&name='+nickname;
param += '&chat='+chat;
	sendReq.send(param);
	
	document.getElementById('txt_message').value = '';
}							
}
//When our message has been sent, update our page.
function handleSendChat() {
//Clear out the existing timer so we don't have 
//multiple timer instances running.
clearInterval(mTimer);
getChatText();
}
//Function for handling the return of chat text
function handleReceiveChat() {
if (receiveReq.readyState == 4) {
	
	var chat_div = document.getElementById('div_chat');
	var users_div = document.getElementById('div_users');
	
	var xmldoc = receiveReq.responseXML;
	var message_nodes = xmldoc.getElementsByTagName("message"); 
	var n_messages = message_nodes.length;
	
	for (i = 0; i < n_messages; i++) {
	 
		var user_node = message_nodes[i].getElementsByTagName("user");
		var text_node = message_nodes[i].getElementsByTagName("text");
		var time_node = message_nodes[i].getElementsByTagName("time");
		
		
		if (user_node[0].firstChild.nodeValue == "Note")
		{
			//chat_div.innerHTML += "<font class='chat_time' style='color:#777;'>(" + time_node[0].firstChild.nodeValue + ")</font> ";
			chat_div.innerHTML += "<font style='display: block; color:#f26522; margin-bottom: 7px;'>" + text_node[0].firstChild.nodeValue + "</font>";
			//chat_div.innerHTML += "<br/>";
		}
		else
		{
			chat_div.innerHTML += '<b style="display: block; color:#0257af; padding-bottom: 1px;"> '+user_node[0].firstChild.nodeValue + ' ' + ((lang == 'Ar')? 'يقول' : 'said') + ': </b>';
			//chat_div.innerHTML += '<font class="chat_time">(' + time_node[0].firstChild.nodeValue + ')</font>';
			chat_div.innerHTML += "<font style='display: block; margin-bottom: 5px;'>" + text_node[0].firstChild.nodeValue + "</font>";
			//chat_div.innerHTML += "<br/>";
		}
		
		chat_div.scrollTop = chat_div.scrollHeight;
		lastMessage = (message_nodes[i].getAttribute('id'));
	}


var users_nodes = xmldoc.getElementsByTagName("nickname");

var n_users= users_nodes.length;


userhtml = "<table cellpadding='0' cellspacing='0' width='100%'>";
for (i = 0; i < n_users; i++) {
	
	userhtml += ' <tr>';
	userhtml += ' 	<td style="padding: 2px; border-bottom: 1px solid #80ACD9;" width="20px"><img src="'+imageURL(users_nodes[i].getAttribute('id'))+'"></td>';
	userhtml += ' 	<td style="padding: 2px; border-bottom: 1px solid #80ACD9; vertical-align: bottom;"><b>'+users_nodes[i].firstChild.nodeValue + '</b></td>';
	userhtml += ' </tr>';
	lastUser= -1;//(users_nodes[i].getAttribute('id'));
}
userhtml += '</table>';
users_div.innerHTML = userhtml;

mTimer = setTimeout('getChatText();',2000); //Refresh our chat in 2 seconds

}
}
//This functions handles when the user presses enter.  Instead of submitting the form, we
//send a new message to the server and return false.
function blockSubmit() {
sendChatText();
return false;
}