			var sendReq = getXmlHttpRequestObject();
			var receiveReq = getXmlHttpRequestObject();
			var receiveReq2 = getXmlHttpRequestObject();
			var lastMessage = 0;
			var mTimer;
			var league;
			var ChatLock = 0;

			//Function for initializating the page.
			function startChat() {
				//Set the focus to the Message Box.
				//document.getElementById('txt_message').focus();
				//Start Recieving Messages.
				lastMessage = 0;
				document.getElementById('div_chat').innerHTML = '';
				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.';
				}
			}

			function SetLeague(x)
			{
				league = x;
			}

			function getOnlineList()
			{
				if (receiveReq2.readyState == 4 || receiveReq2.readyState == 0) {
					receiveReq2.open("GET", 'chat/online.php?league=' + league, true);
					receiveReq2.onreadystatechange = handleReceiveOnline; 
					receiveReq2.send(null);
				}
			}
			
			//Gets the current messages from the server
			function getChatText() {
				if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
					receiveReq.open("GET", 'chat/getChat.php?chat=1&last=' + lastMessage + '&league=' + league, 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", 'chat/getChat.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=Ryan Smith';
					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 handleReceiveChat() {
				clearInterval(mTimer);

				if(ChatLock)return;
				ChatLock = 1;

				if (receiveReq.readyState == 4) {
					//Get a reference to our chat container div for easy access
					var chat_div = document.getElementById('div_chat');
					chat_div.style.textAlign = 'left';
					if(!lastMessage)
						chat_div.innerHTML = '';
					//Get the AJAX response and run the JavaScript evaluation function
					//on it to turn it into a useable object.  Notice since we are passing
					//in the JSON value as a string we need to wrap it in parentheses
					var response = eval("(" + receiveReq.responseText + ")");

					for(i=0;i < response.messages.message.length; i++) {
							try {
							chat_div.innerHTML += response.messages.message[i].user;
							chat_div.innerHTML += '&nbsp;&nbsp;<font class="chat_time">' +  response.messages.message[i].time + '</font><br />';
							chat_div.innerHTML += response.messages.message[i].text + '<br />';
							lastMessage = response.messages.message[i].id;
							chat_div.scrollTop = chat_div.scrollHeight;
							}
							catch(err) {}
					}

					document.getElementById('chat_online').innerHTML = '<a href="javascript:SwitchToOnline();">Online (' + response.members + ')</a>';

					mTimer = setTimeout('getChatText();',2000); //Refresh our chat in 2 seconds
				}

				ChatLock = 0;
			}
			//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;
			}
			//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", 'chat/getChat.php?chat=1&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 = '';
				getChatText();
			}

			function handleReceiveOnline()
			{
				clearInterval(mTimer);
				
				if(ChatLock)return;
				ChatLock = 1;

				if (receiveReq2.readyState == 4) {
					//Get a reference to our chat container div for easy access
					var chat_div = document.getElementById('div_chat');
					chat_div.innerHTML = '';
					chat_div.style.textAlign = 'right';
					//Get the AJAX response and run the JavaScript evaluation function
					//on it to turn it into a useable object.  Notice since we are passing
					//in the JSON value as a string we need to wrap it in parentheses
					var response = eval("(" + receiveReq2.responseText + ")");

					for(i=0;i < response.members.member.length; i++) {
							try {
							chat_div.innerHTML += response.members.member[i].user;
							}
							catch(err) {}
					}

					document.getElementById('chat_online').innerHTML = '<a href="javascript:SwitchToOnline();">Online (' + response.membersqty + ')</a>';

					mTimer = setTimeout('getOnlineList();',2000); //Refresh our chat in 2 seconds
				}

				ChatLock = 0;
			}

			function SwitchToOnline()
			{
				getOnlineList();
			}
