function toogle_chat()
{
	var chat = document.getElementById("chat");
	if(chat.className == "show")
	{
		if(document.getElementById("written").value == "1")
			chat.className = "scale";
		else
			chat.className = "hide";
	}
	else
	{
		chat.className = "show";
		document.getElementById("chat_header").className = "";
	}
	
	var http = null;
	http = erzXMLHttpRequestObject()
	if (http != null) 
	{
	   http.open("GET", "/ajax.php?action=chat_window_state&status="+chat.className, true);
	   http.send(null);
	}

	addEvent(document.getElementById("chat_input"),"keydown",check_submit);
}

function check_submit(event)
{
	if(event.keyCode == 13)
	{
		chat_send();
	}
}

function chat_send()
{
	var question = document.getElementById("chat_input").value;
	var linid = document.getElementById("linid").value;
	
	var http = null;
	http = erzXMLHttpRequestObject()
	if (http != null) 
	{
	   http.open("POST", "/ajax.php?action=chat_save&question="+question+"&linid="+linid, true);
	   http.send(null);
	   http.onreadystatechange = display;
	}
	function display() 
	{
		if (http.readyState == 4)
		{
			document.getElementById("chat_input").value = "";
			window.setInterval("chat_check_update()", 1000);
		}
	}
}

function chat_check_update()
{
	var http = null;
	http = erzXMLHttpRequestObject()
	if (http != null) 
	{
	   http.open("GET", "/ajax.php?action=chat_get_crc", true);
	   http.send(null);
	   http.onreadystatechange = check_response;
	}
	function check_response() 
	{
		if (http.readyState == 4)
		{
			var crc = http.responseText;
			crc = parseInt(crc);
			
			saved_crc = document.getElementById("chat_length").value;		
			
			if(saved_crc != crc)
			{
				document.getElementById("chat_length").value = crc;
				chat_update();
				chat_header_update();		
			}
		}
	}
}

function chat_update()
{
	var http = null;
	http = erzXMLHttpRequestObject()
	if (http != null) 
	{
	   http.open("GET", "/ajax.php?action=chat_get_content", true);
	   http.send(null);
	   http.onreadystatechange = display;
	}
	function display() 
	{
		if (http.readyState == 4)
		{
			var content = http.responseText;
			document.getElementById("chat_content").innerHTML = content;
			document.getElementById("chat_content").scrollTop = document.getElementById("chat_content").scrollHeight;
		}
	}
}

function chat_header_update()
{
	var http = null;
	http = erzXMLHttpRequestObject()
	if (http != null) 
	{
	   http.open("GET", "/ajax.php?action=chat_header_update", true);
	   http.send(null);
	   http.onreadystatechange = display;
	}
	function display() 
	{
		if (http.readyState == 4)
		{
			if(http.responseText == '1')
			{
				if(document.getElementById("chat").className != "show")
				{
					document.getElementById("chat_header").className = "header_active";
					document.getElementById("chat").className = "scale";
				}
			}
		}
	}
}


function start_new()
{
	var http = null;
	http = erzXMLHttpRequestObject()
	if (http != null) 
	{
	   http.open("GET", "/ajax.php?action=chat_start_new", true);
	   http.send(null);
	}
}
