var sendReqObj = createRequestObject();
var receiveReqObj = createRequestObject();
var lastMessage = 0;
var mTimer;

/* Function to make the DIV holding the Chat Room visible. */
function showContDiv()
{
/* Showing the chat holder div */
	if (document.getElementById('chatHolder').style.display == "block")
	{
	/* Hiding the Div holding response */
		document.getElementById('chatHolder').style.display = "none";
	}
 
	else
	{
	/* Showing the Div holding the chat room */
		document.getElementById('chatHolder').style.display = "block";
	/* Calling the  function init() */
		init();
	}
}

/* Function for initializating the page. */
function init() 
{
/* Setting the focus to the Message Box. */
	document.getElementById('txtMessageID').focus();
/* Calling the function getText for recieving messages. */
	getText();
}

/* Making a request. */
function createRequestObject()
{
/* Initialising the variable xmlhttp */
	var xmlhttp = false;
/* Try and catch block for creating xmlhttp object according to the browser */
	try
	{
/* The xmlhttp object is created using the Microsoft's XML Parser. */
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		try 
		{
		/* The xmlhttp object is created for Microsoft IE. */
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch (E) 
		{
			xmlhttp = false;
		}
	}
/* The xmlhttp object is created for browsers other than Microsoft IE. */
	if (!xmlhttp && typeof XMLHttpRequest!='undefined')
	{
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

/* Getting the current messages from the server */
function getText() 
{
	receiveReqObj.open("GET", 'doSql.php?last=' + lastMessage, true);
	receiveReqObj.onreadystatechange = handleReceiveChat; 
	receiveReqObj.send(null);
}

/* Function for handling the return of chat text. */
function handleReceiveChat() 
{
	if (receiveReqObj.readyState == 4) 
	{
		if(receiveReqObj.status == 200) 
		{
			var chat_div = document.getElementById('divChat');
		
			var xmldoc = receiveReqObj.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");
	
				chat_div.innerHTML += '<FONT COLOR=#660000><B><U>'+user_node[0].firstChild.nodeValue + ':<U></B></FONT>&nbsp;';
				chat_div.innerHTML += '<FONT CLASS="chat_time">' + time_node[0].firstChild.nodeValue + '</FONT>&nbsp;';
				chat_div.innerHTML += text_node[0].firstChild.nodeValue + '<BR />';
	
				chat_div.scrollTop = chat_div.scrollHeight;
				lastMessage = (message_nodes[i].getAttribute('id'));
			}
		}
	/* Refreshing the chat in every 2 seconds */
		mTimer = setTimeout('getText();',2000);
	}
}

/* Adding a message to the chat server. */
function sendText()
{
	if(document.getElementById('txtMessageID').value == '')
	{
		alert("Enter a message");
		return;
	}
		sendReqObj.open("POST", 'doSql.php?last=' + lastMessage, true);
		sendReqObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		sendReqObj.onreadystatechange = handleSendChat; 
		var param = 'message=' + document.getElementById('txtMessageID').value;

		if(document.getElementById("txtNickNameID").value != '')
			var name = document.getElementById("txtNickNameID").value;
		else
			var name = 'Guest';
	
		param += '&name='+ name;
		sendReqObj.send(param);
		document.getElementById('txtMessageID').value = "";
}

/* After sending the message the page is updated. */
function handleSendChat() 
{
/* Clearing the existing timer so that there are no multiple timer instances running. */
	clearInterval(mTimer);
	getText();
}

/* Clearing the whole the database so that a new chat session can be started. */
function resetChat() 
{
	sendReqObj.open("POST", 'doSql.php?last=' + lastMessage, true);
	sendReqObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	sendReqObj.onreadystatechange = handleResetChat; 
	var param = 'action=reset';
	sendReqObj.send(param);
	document.getElementById('txtMessageID').value = "";
}

/* Function for handling the response after the page has been refreshed. */
function handleResetChat() 
{
	document.getElementById('divChat').innerHTML = '';
	getText();
}