var sendReq = getXmlHttpRequestObject();
var receiveReq = getXmlHttpRequestObject();
var lastMessage = 0;
var uTimer;
var mTimer;

//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.';
	}
}

//Function for initializating the page.
function startChat() {
	sendChatUser();
	document.getElementById('txt_message').focus();
}

//Add a user to the chat server.
function sendChatUser() {
	if (sendReq.readyState == 4 || sendReq.readyState == 0) {
		sendReq.open("POST", 'mcz_get_chat.php?chat=1', true);
		sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		sendReq.onreadystatechange = handleSendUser;
		var param = 'newUser='+ document.getElementById('chatUser').value;
		param += '&chat=1';
		sendReq.send(param);
	}
}

//When our user has been sent, update our page.
function handleSendUser() {
	if (sendReq.readyState == 4 || sendReq.readyState == 0) {
		//Clear out the existing timer so we don't have
		//multiple timer instances running.
		clearInterval(mTimer);
		clearInterval(uTimer);	
		getChatUser();
	}
}

//Gets the current users from the server
function getChatUser() {
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		receiveReq.open("GET", 'mcz_get_chat.php?chat=1&getuser=1&last=' + lastMessage, true);
		receiveReq.onreadystatechange = handleReceiveCUser;
		receiveReq.send(null);
	}
}

//Function for handling the return of chat user
function handleReceiveCUser() {
	if (receiveReq.readyState == 4) {
		document.getElementById('div_users').innerHTML = '';
		var xmldoc = receiveReq.responseXML;
		var cuser_div = document.getElementById('div_users');
		var cuser_nodes = xmldoc.getElementsByTagName("cuser");
		var n_cusers = cuser_nodes.length;
		for (j = 0; j < n_cusers; j++) {
			var cunm_node = cuser_nodes[j].getElementsByTagName("cuname");
			cuser_div.innerHTML += '<font class="chat_user"><li><i>' + cunm_node[0].firstChild.nodeValue + '</i></font><br />';
			cuser_div.scrollTop = cuser_div.scrollHeight;
		}
		uTimer = setTimeout('getChatUser();',100000);

		var chat_div = document.getElementById('div_chat');
		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 class="chat_user">' + user_node[0].firstChild.nodeValue + '</font>&nbsp;';
			chat_div.innerHTML += '<font class="chat_time">' + time_node[0].firstChild.nodeValue + '</font><br />';
			chat_div.innerHTML += '<font class="chat_message">' + text_node[0].firstChild.nodeValue + '</font><br />';
			chat_div.scrollTop = chat_div.scrollHeight;
			lastMessage = (message_nodes[i].getAttribute('id'));
		}
		mTimer = setTimeout('getChatText();',10000);
	}
}


//Gets the current messages from the server
function getChatText() {
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		receiveReq.open("GET", 'mcz_get_chat.php?chat=1&last=' + lastMessage, true);
		receiveReq.onreadystatechange = handleReceiveChat;
		receiveReq.send(null);
	}
}
//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", 'mcz_get_chat.php?chat=1&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('chatUser').value;
		param += '&chat=1';
		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 xmldoc = receiveReq.responseXML;
		var chat_div = document.getElementById('div_chat');
		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 class="chat_user">' + user_node[0].firstChild.nodeValue + '</font>&nbsp;';
			chat_div.innerHTML += '<font class="chat_time">' + time_node[0].firstChild.nodeValue + '</font><br />';
			chat_div.innerHTML += '<font class="chat_message">' + text_node[0].firstChild.nodeValue + '</font><br />';
			chat_div.scrollTop = chat_div.scrollHeight;
			lastMessage = (message_nodes[i].getAttribute('id'));
		}
		mTimer = setTimeout('getChatText();',10000);
	}
}

//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 closeChat() {
	if (sendReq.readyState == 4 || sendReq.readyState == 0) {
		sendReq.open("POST", 'mcz_get_Chat.php?chat=1&last=' + lastMessage, true);
		sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		sendReq.onreadystatechange = handleCloseChat; 
		var param = 'action=close';
		param += '&chatUser='+ document.getElementById('chatUser').value;
		sendReq.send(param);
	}							
}

function handleCloseChat() {
	closeMessage(1);
}	

