			var domain = 'http://www.piratenuithetoosten.nl/';
			var sound_file_url = 'talk.wav';
			var user_id = 0;
			var sendReq = getXmlHttpRequestObject();
			var receiveReq = getXmlHttpRequestObject();
			var receiveWhoisReq = getXmlHttpRequestObject();
			var lastMessage = 0;
			var lastOnline = 0;
			var mTimer;
			
			
			function set_user_id(id){
				user_id = id;
			}
			
			//Function for initializating the page.
			function startChat() {
				//Set the focus to the Message Box.
				//document.getElementById('txt_message').focus();
				//Start Recieving Messages.
				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) {
					receiveReq.open("GET", domain + 'getChat.php?chat=1&last=' + lastMessage, true);
					receiveReq.onreadystatechange = handleReceiveChat; 
					receiveReq.send(null);
				}
				
				if (receiveWhoisReq.readyState == 4 || receiveWhoisReq.readyState == 0) {
					receiveWhoisReq.open("GET", domain + 'whoisonline.php?last=' + lastOnline, true);
					receiveWhoisReq.onreadystatechange = handleRecieveWhoisOnline; 
					receiveWhoisReq.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", domain + '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 for handling the return of chat text
			/*function handleReceiveChat_old() {
				if (receiveReq.readyState == 4) {
					var chat_div = document.getElementById('div_chat');
					var chat_div_big = document.getElementById('div_chat_big');
					var xmldoc = receiveReq.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 user_id_node = message_nodes[i].getElementsByTagName("user_id");
						var text_node = message_nodes[i].getElementsByTagName("text");
						var time_node = message_nodes[i].getElementsByTagName("time");
						chat_div.innerHTML += '<font class="chat_time">' + time_node[0].firstChild.nodeValue + '</font> ';
						chat_div.innerHTML += '<a href="index.php?page=rapporten&user_id=' + user_id_node[0].firstChild.nodeValue + '">' + user_node[0].firstChild.nodeValue + '</a>' + ':&nbsp;';
						chat_div.innerHTML += text_node[0].firstChild.nodeValue + '<br />';
						chat_div.scrollTop = chat_div.scrollHeight;
						
						chat_div_big.innerHTML += '<font class="chat_time">' + time_node[0].firstChild.nodeValue + '</font> ';
						chat_div_big.innerHTML += '<a href="index.php?page=rapporten&user_id=' + user_id_node[0].firstChild.nodeValue + '">' + user_node[0].firstChild.nodeValue + '</a>' + ':&nbsp;';
						chat_div_big.innerHTML += text_node[0].firstChild.nodeValue + '<br />';
						chat_div_big.scrollTop = chat_div_big.scrollHeight;
						
						lastMessage = (message_nodes[i].getAttribute('id'));
					}
					mTimer = setTimeout('getChatText();',2000); //Refresh our chat in 2 seconds
				}
			}*/
			
			function handleReceiveChat() {
				if (receiveReq.readyState == 4) {
					var chat_div = document.getElementById('div_chat');
					var chat_div_big = false;
					if(document.getElementById('div_chat_big'))
						chat_div_big = document.getElementById('div_chat_big');
					var xmldoc = receiveReq.responseXML;
					var message_nodes = xmldoc.getElementsByTagName("message"); 
					var n_messages = message_nodes.length;
					var content = '';
					
					var m_user_id=0;
					var playsound=true;
					var lastContent='';
					if(lastMessage==0)
						playsound=false;
					for (i = 0; i < n_messages; i++) {
						
						var user_node = message_nodes[i].getElementsByTagName("user");
						var user_id_node = message_nodes[i].getElementsByTagName("user_id");
						var text_node = message_nodes[i].getElementsByTagName("text");
						var time_node = message_nodes[i].getElementsByTagName("time");
						content = '';
						content += '<font class="chat_time">' + time_node[0].firstChild.nodeValue + '</font> ';
						content += '<a href="index.php?page=rapporten&user_id=';
						content += user_id_node[0].firstChild.nodeValue;
						content += '">';
						content += user_node[0].firstChild.nodeValue ;
						content += '</a>'  + ':&nbsp;';
						
						content += text_node[0].firstChild.nodeValue + '<br />';
						
						if(chat_div_big!=false){
							chat_div_big.innerHTML += content;
							chat_div_big.scrollTop = chat_div_big.scrollHeight;
						}
						lastContent = content;
						lastMessage = (message_nodes[i].getAttribute('id'));
						/*m_user_id =user_id_node[0].firstChild.nodeValue;
						if(user_id==m_user_id)	
							playsound=false;
						if(playsound==true)
							document.getElementById("sound_element").innerHTML= "<embed src='"+sound_file_url+"' hidden=true autostart=true loop=false width=0 heigth=0>";*/
					}
					//if(lastContent!='')
						//chat_div.innerHTML += '<font color=red><b>Laatste bericht</b>: </font>' + lastContent;
					//chat_div.scrollTop = chat_div.scrollHeight;
					
					mTimer = setTimeout('getChatText();',2000); //Refresh our chat in 2 seconds
				} /*else {
					var chat_div = document.getElementById('div_chat');
					chat_div.innerHTML = 'Verbinding maken is mislukt.';
				}*/
			}
			
			function handleRecieveWhoisOnline(){
				if (receiveReq.readyState == 4 && document.getElementById('div_chat_whois_online')) {
					var whois_div = document.getElementById('div_chat_whois_online');

					var xmldoc = receiveWhoisReq.responseXML;
					if(xmldoc!=null){
						var user_nodes = xmldoc.getElementsByTagName("user");
						var n_users = user_nodes.length;
						var content = '';
						
						
						for (i = 0; i < n_users; i++) {
							var user_id_node = user_nodes[i].getElementsByTagName("user_id");
							var username_node = user_nodes[i].getElementsByTagName("username");
							var user_naam_node = user_nodes[i].getElementsByTagName("user_naam");
							var request_uri_node = user_nodes[i].getElementsByTagName("request_uri");
							var ip_node = user_nodes[i].getElementsByTagName("ip");
							//lastOnline = (user_nodes[i].getAttribute('id'));
							//content = lastOnline+'';
							/*content += '<font class="chat_time">' + time_node[0].firstChild.nodeValue + '</font> ';*/
							if(i!=0)
								content +='<br>';
							content += '<a href="index.php?page=rapporten&user_id=';
							content += user_id_node[0].firstChild.nodeValue;
							content += '">';
							content += username_node[0].firstChild.nodeValue + ' (' + user_naam_node[0].firstChild.nodeValue + ')';
							
							content += '</a>'+ '&nbsp;';
							$ru = request_uri_node[0].firstChild.nodeValue;
							if($ru!='-1')
								content += $ru;
							$ip = ip_node[0].firstChild.nodeValue;
							if($ru!='-1')
								content += ' - ' + $ip;
							//content += text_node[0].firstChild.nodeValue + '<br />';
							
							
							
						}
						whois_div.innerHTML = content;
					}
					/*mTimer = setTimeout('getChatText();',2000); //Refresh our chat in 2 seconds*/
				}
			}
			//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", domain + '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();
			}
			
			
			
