
var sendReq = getXmlHttpRequestObject();
var receiveReq = getXmlHttpRequestObject();
var lastMessage = 0;
var mTimer;

window.onload=start_init;
function start_init(){
	document.getElementById('div_chat').scrollTop = document.getElementById('div_chat').scrollHeight;
	getChatText();
}


//Gets the browser specific XmlHttpRequest Object
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) {
		if (lastMessage==0) lastMessage = document.getElementById('txt_last_msg_id').value;
		receiveReq.open("GET", 'index.php?shoutbox=on&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('div_chat');
		var xmldoc = receiveReq.responseXML;
		var message_nodes = xmldoc.getElementsByTagName("message"); 
		var n_messages = message_nodes.length
		for (i = 0; i < n_messages; i++) {
			var uid_node = message_nodes[i].getElementsByTagName("uid");
			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="#990000">' + time_node[0].firstChild.nodeValue + '&nbsp;-&nbsp;';
			chat_div.innerHTML += '<a href="./index.php?comp=profil&select=' + uid_node[0].firstChild.nodeValue + '">'  + user_node[0].firstChild.nodeValue + '</a>:&nbsp;</font>';
			chat_div.innerHTML += text_node[0].firstChild.nodeValue + '<br />';
			lastMessage = (message_nodes[i].getAttribute('id'));
		}
		if(n_messages>0){
			chat_div.scrollTop = chat_div.scrollHeight;
		}
		mTimer = setTimeout('getChatText();',2000);
	}
}
	
//Add a message to the chat server.
function sendChatText() {
	if (sendReq.readyState == 4 || sendReq.readyState == 0) {
		sendReq.open("POST", 'index.php?shoutbox=on&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=' + document.getElementById('txt_name').value;
		param += '&uid=' + document.getElementById('txt_uid').value;
		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();
}

//This cleans out the database so we can start a new chat session.
function resetChat() {
	if (sendReq.readyState == 4 || sendReq.readyState == 0) {
		sendReq.open("POST", 'index.php?shoutbox=on&last=' + lastMessage, true);
		sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		sendReq.onreadystatechange = handleResetChat; 
		var param = 'action=reset';
		sendReq.send(param);
		document.getElementById('txt_message').value = '';
	}							
}

//This function handles the response after the page has been refreshed.
function handleResetChat() {
	document.getElementById('div_chat').innerHTML = '';
	clearInterval(mTimer);
	getChatText();
}

//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;
}

//Function for initializating the page.
function startChat() {
	//Set the focus to the Message Box.
	document.getElementById('txt_message').focus();
	//Start Recieving Messages.
	getChatText();
}