var sendReq = getXmlHttpRequestObject();
var receiveReq = getXmlHttpRequestObject();
var lastMessage = 0;
var mTimer;
var scrollbox1;

window.addEvent('domready', function() {	
	getChatText();	
});		

function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		document.getElementById('p_status').innerHTML = 
		'Status: Cound not create XmlHttpRequest Object.' +
		'Consider upgrading your browser.';
	}
}

// Gets the current messages from the server
function getChatText() {
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		receiveReq.open("GET", 'ajax/chat_get_messages.php?last=' + lastMessage, true);
		receiveReq.onreadystatechange = handleReceiveChat; 
		receiveReq.send(null);
	}			
}

//Function for handling the return of chat text
function handleReceiveChat(){
	if (receiveReq.readyState == 4) {
		var chat_div = document.getElementById('chat_messages_container');
		var xmldoc = receiveReq.responseXML;
		var message_nodes = xmldoc.getElementsByTagName("message");
		var n_messages = message_nodes.length;
		var new_messages = "";
		for (i = 0; i < n_messages; i++) {
			var user_node = message_nodes[i].getElementsByTagName("user");
			var profile_node = message_nodes[i].getElementsByTagName("profile_pic");
			var text_node = message_nodes[i].getElementsByTagName("text");
			var time_node = message_nodes[i].getElementsByTagName("time");
			if (user_node[0].firstChild.nodeValue == "jwwisgerhof" || user_node[0].firstChild.nodeValue == "Scratchie" || user_node[0].firstChild.nodeValue == "mahdan") {
				chat_div.innerHTML += "<div id='chat_each_message'><table cellpadding='0' cellspacing='0' border='0' width='100%'><tr><td valign='top' width='40'><img src='" + profile_node[0].firstChild.nodeValue + "' /></td><td valign='top'><strong><font color='#FF7225'>" + user_node[0].firstChild.nodeValue + ":</font></strong> " + text_node[0].firstChild.nodeValue + "</td></tr></table></div>";
			} else {
				chat_div.innerHTML += "<div id='chat_each_message'><table cellpadding='0' cellspacing='0' border='0' width='100%'><tr><td valign='top' width='40'><img src='" + profile_node[0].firstChild.nodeValue + "' /></td><td valign='top'><strong>" + user_node[0].firstChild.nodeValue + ":</strong> " + text_node[0].firstChild.nodeValue + "</td></tr></table></div>";
			}
			lastMessage = (message_nodes[i].getAttribute('id'));
		}
		
		if (n_messages > 0) {
			chat_div.scrollTop = chat_div.scrollHeight;
		}
		
		mTimer = setTimeout('getChatText();', 500);
	}
}

//Add a message to the chat server.
function sendChatText() {
	if (sendReq.readyState == 4 || sendReq.readyState == 0) {
		sendReq.open("POST", 'ajax/chat_add_message.php', true);
		sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		sendReq.onreadystatechange = handleSendChat; 
		var param = 'message=' + document.getElementById('chat_message').value;
		sendReq.send(param);
		document.getElementById('chat_message').value = '';
	}							
}

//When our message has been sent, update our page.
function handleSendChat() {
	clearInterval(mTimer);
	getChatText();
}

function keyup(arg1) {
	if (arg1 == 13) sendChatText(); 
}