var winW = 0;
var winH = 0;
var chatID = 0;
var loginOn = 0;
var chatFocus = 0;
var chatBusy = 0;
var chatChecker = 0;

function killUmlaut(text) {
	text=text.replace(/\u00fc/g, "&uuml;");
	text=text.replace(/\u00f6/g, "&ouml;");
	text=text.replace(/\u00e4/g, "&auml;");
	text=text.replace(/\u00dc/g, "&Uuml;");
	text=text.replace(/\u00d6/g, "&Ouml;");
	text=text.replace(/\u00c4/g, "&Auml;");
	text=text.replace(/\u00df/g, "&szlig;");
	text=text.replace(/ü/g, "&uuml;");
	text=text.replace(/ö/g, "&ouml;");
	text=text.replace(/ä/g, "&auml;");
	text=text.replace(/Ü/g, "&Uuml;");
	text=text.replace(/Ö/g, "&Ouml;");
	text=text.replace(/Ä/g, "&Auml;");
	text=text.replace(/ß/g, "&szlig;");
	return text;
}

if (parseInt(navigator.appVersion)>3) {
	if (navigator.appName=="Netscape")
	{
  		winW = window.innerWidth;
  		winH = window.innerHeight;
 	}
	if (navigator.appName.indexOf("Microsoft")!=-1)
	{
  		winW = document.body.offsetWidth;
  		winH = document.body.offsetHeight;
 	}
}

function chatEnter(e, id)
{
	var keynum;
	if(window.event)
		keynum = e.keyCode;
	else if(e.which)
		keynum = e.which;
	if(keynum == 13)
		chatSend(id); 
}

function chatSend(id)
{
	if(chatBusy == 0)
	{
		if($('chat_input').value != '')
		{
			new Ajax.Request('/ajax',
			{
				method: 'post',
				parameters: {chat_send_id: id, text: $('chat_input').value},
				onSuccess: function()
				{
					$('chat_input').value = '';
				}
			}
			);
		}
		$('chat_input').focus();
	}
	else
		setTimeout("chatSend("+id+")",100);
}

function chatLogin()
{
	chatBusy = 1;
	new Ajax.Request('/ajax',
	{
		method: 'post',
		parameters: {chat_get_id: 0},
		onSuccess: function(transport)
		{
			chatID = transport.responseText;
			if(chatID == null || chatID == '') chatID = 0;
			setTimeout('chatUpdate()', 1000);
		}
	}
	);
	chatBusy = 0;
}

function chatUpdate()
{
	new Ajax.Updater('chatWindow', '/ajax',
	{
		method: 'post',
		parameters: {chat_update: chatID},
		insertion: Insertion.Bottom,
		onSuccess: function(transport)
		{
			if(transport.responseText != '' || chatChecker > 3)
			{
				chatChecker = 0;
				chatLogin();
			}
			else
			{
				chatChecker += 1;
				setTimeout('chatUpdate()', 1000);
			}
		}
	}
	);
}

function chatScroll()
{
	if($('chatWindow'))
	{
		var obj = $('chatWindow');
		obj.scrollTop = obj.scrollHeight;
		setTimeout("chatScroll()", 100);
	}
}

function readMessage(id)
{
	$('m_b_' + id).style.fontWeight = "normal";
	new Ajax.Request('/ajax',
		{
			method: 'post',
			parameters: {read_message_id: id}
		}
		);
}

function showHide(id)
{
	if($(id).style.display == 'block')
		$(id).style.display = 'none';
	else $(id).style.display = 'block';
}
