// JavaScript Document

			function setChatName(chat_name) {
				if (chat_name != null) {
					document.cookie = 'chat_name = ' + escape(chat_name);
				}
			}
			
			function getChatName() {
				if (document.cookie.length>0) {
					c_start=document.cookie.indexOf("chat_name" + "=");
					if (c_start!=-1) {
						c_start=c_start + 10;
						c_end=document.cookie.indexOf(";",c_start);
						if (c_end==-1) c_end=document.cookie.length;
						return unescape(document.cookie.substring(c_start,c_end));
					}
				}
				return "";
			}
			
			var sendReq = getXmlHttpRequestObject();
			var receiveReq = getXmlHttpRequestObject();
			var lastMessage = 0;
			var mTimer;
			//Function for initializating the page.
			function startChat() {
				//Set the focus to the Message Box.
				// document.getElementById('uzenet_mezo').focus();
				//Start Recieving Messages.
				getChatText();
				document.getElementById("name_box").value = getChatName();
			}	
			//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", 'ajax/getchat.php?chat=1&last=' + lastMessage, true);
					receiveReq.onreadystatechange = handleReceiveChat; 
					receiveReq.send(null);
				}			
			}
			//Add a message to the chat server.
			function sendChatText() {
				if(document.getElementById('uzenet_mezo').value == '') {
					alert("You have not entered a message");
					return;
				}
				name=document.getElementById("name_box").value;
				if (name == "") {
					name="anonymus";
				}
				if (sendReq.readyState == 4 || sendReq.readyState == 0) {
					sendReq.open("POST", 'ajax/getchat.php?chat=1&last=' + lastMessage, true);
					sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
					sendReq.onreadystatechange = handleSendChat; 
					var param = 'message=' + document.getElementById('uzenet_mezo').value;
					param += '&name='+name;
					param += '&chat=1';
					sendReq.send(param);
					document.getElementById('uzenet_mezo').value = '';
				}
				setChatName(document.getElementById("name_box").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);
				if (sendReq.responseText == "flooded") {
					alert("6 másodpercenként 5 üzenetet szabad elküldeni. Kérlek próbáld újra néhány másodperc múlva!");
				}
				getChatText();
			}
			function handleReceiveChat() {
				if (receiveReq.readyState == 4) {
					//Get a reference to our chat container div for easy access
					var chat_div = document.getElementById('chat_box');
					//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++) {
						if (response.messages.message[i].user == "SITE ADMIN") {
							user = '<b><font color="#660000">SITE ADMIN</font></b>';	
						} else {
							user = response.messages.message[i].user;
						}
						chat_div.innerHTML += '<span class="uzenet_felado">'+user+'</span>';
						chat_div.innerHTML += '&nbsp;&nbsp;<span class="uzenet_ido">' +  response.messages.message[i].time + '</span><br />';
						chat_div.innerHTML += '<div class="uzenet_uzenet">'+response.messages.message[i].text + '</div>';
						chat_div.scrollTop = chat_div.scrollHeight;
						lastMessage = response.messages.message[i].id;
					}
					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 function handles the response after the page has been refreshed.
			function handleResetChat() {
				document.getElementById('chat_box').innerHTML = '';
				getChatText();
			}
