
var g_oChatExceptions = true;
var g_oChat = null;
var g_sSession = "";

function CreateChat( _idUser, _idLanguage )
{
	if ( document.getElementById( "chat-container" ) == null )
	{
		var oContainer = document.getElementById( "container" );
		var oChatContainer = document.createElement( "div" );
		oContainer.appendChild( oChatContainer );
		oChatContainer.id = "chat-container";
	}
	if ( document.getElementById( "chat-separator" ) == null )
	{
		$('#pos-div').after('<div id="chat-separator">');
	}
	if ( _idLanguage != 0 )
	{
		g_aoChatTexts[ 0 ] = g_aoChatTexts[ _idLanguage ];
	}
	if ( g_oChat == null )
	{
		try
		{
			g_oChat = new Chat( _idUser );
			g_oChat.Start();
		}
		catch ( exc )
		{
			if ( !exc.message )
			{
				exc.message = exc;
			}
			if ( g_oChatExceptions && ( this.m_hConsole != null ) )
			{
				this.m_hConsole.appendChild( document.createTextNode( "exception" ) );
				this.m_hConsole.appendChild( document.createElement( "br" ) );
				this.m_hConsole.appendChild( document.createTextNode( exc.fileName + "(" + exc.lineNumber + "): " + exc.message ) );
				this.m_hConsole.appendChild( document.createElement( "br" ) );
				this.m_hConsole.appendChild( document.createTextNode( exc.stack ) );
				this.m_hConsole.appendChild( document.createElement( "br" ) );
			}
		}
	}
}

g_oaChatProtocol = 
{
	chat: new Array( "id", "userId", "roomId", "created", "text" ),
	rooms: new Array( "->", "!isPrivate", "*,userIds", "name" ),
	users: new Array( "->", "name", "hero" ),
	input: new Array( "->", "created", "userId", "status", "headline", "text" ),
	output: new Array( "->", "created", "userId", "status", "headline", "*,recv", "text" ),
	searchuser: new Array( "id", "name", "hero" ),
	search: new Array( "text" ),
	allrooms: new Array( "->", "count", "name" ),
	options: new Array( "->", "setting" ),
	error: new Array( "topic", "message" ),
	session: new Array( "session" )
}

g_aoChatTexts = new Array
(
	null,
	{
		chatListTitle: "Chats",
		mailComposeTitle: "Neue Mail",
		inboxTitle: "Eingang",
		outboxTitle: "Ausgang",
		
		mailReceiver: "Empfänger:",
		mailSubject: "Betreff:",
		mailText: "Text:",
		mailSend: "Senden",
		mailFixtures: "Kampfpaarungen",
		
		mailButtonDelete: "Mail löschen",
		mailButtonReply: "Mail beantworten",
		mailButtonClose: "Zurück",
		mailRecvTime: "Gesendet:",
		
		userHero: "Held: ",
		userMany: "mehrere",
		
		boxDate: "Datum",
		boxAuthor: "Autor",
		boxRecv: "Empfänger",
		boxSubject: "Betreff",
		
		chatText: "Text:",
		chatSend: "Senden",
		
		listOptionAcceptContacts: "Private Chats von anderen erlauben",
		listJoinText: "Neuer Raum:",
		listJoin: "Raum erstellen",
		
		errorPrefix: "Fehler: ",
		errorQuery: "Benutzer %s kann nicht kontaktiert werden.",
		
		chatMessages: new Array( "", "tritt ein", "tritt aus", "Timeout", "ist eingeladen" )
	},
	{
		chatListTitle: "Chats",
		mailComposeTitle: "New Mail",
		inboxTitle: "Inbox",
		outboxTitle: "Sent",
		
		mailReceiver: "Recipient:",
		mailSubject: "Subject:",
		mailText: "Text:",
		mailSend: "Send",
		mailFixtures: "Duel Fixtures",
		
		mailButtonDelete: "Delete mail",
		mailButtonReply: "Reply to sender",
		mailButtonClose: "Back",
		mailRecvTime: "Sent:",
		
		userHero: "Hero: ",
		userMany: "divers",
		
		boxDate: "Date",
		boxAuthor: "Author",
		boxRecv: "Recipient",
		boxSubject: "Subject",
		
		chatText: "Text:",
		chatSend: "Send",
		
		listOptionAcceptContacts: "Permit private chats from others",
		listJoinText: "New room:",
		listJoin: "Create room",
		
		errorPrefix: "Error: ",
		errorQuery: "User %s cannot be contacted.",
		
		chatMessages: new Array( "", "joins", "leaves", "timeout", "is invited" )
	}
);

function ChatList( _oChat )
{
	this.m_sId = "%%list";
	this.m_sName = g_aoChatTexts[ 0 ].chatListTitle;
	this.m_bRedraw = false;
	this.m_oRooms = new Array();
	this.m_hTab = document.createElement( "div" );
	this.m_hTab.appendChild( document.createTextNode( this.GetDisplayName( _oChat ) ) );
	this.m_hTab.className = "chat-tab";
	this.m_hTab.onclick = function() { g_oChat.RoomActivate( this.id.substring( 5 ) ); };
	this.m_hTab.id = "xyzXY" + this.m_sId;
	var oDiv = document.createElement( "div" );
	oDiv.className = "chat-tab-end";
	this.m_hTab.appendChild( oDiv );
}

ChatList.prototype.GetDisplayName = function( _oChat )
{
	return this.m_sName;
}

ChatList.prototype.AddMessages = function( _aMessages )
{
}

ChatList.prototype.SetRooms = function( _oChat, _oRooms, _bRedraw )
{
	for ( var id in _oRooms )
	{
		if ( typeof( this.m_oRooms[ id ] ) == "undefined" )
		{
			this.m_bRedraw = _bRedraw;
			this.m_oRooms = _oRooms;
			this.Redraw( _oChat );
			return;
		}
	}
	for ( var id in this.m_oRooms )
	{
		if ( typeof( _oRooms[ id ] ) == "undefined" )
		{
			this.m_bRedraw = _bRedraw;
			this.m_oRooms = _oRooms;
			this.Redraw( _oChat );
			return;
		}
	}
	// copy the user counts if necessary but don't change the DOM structure
	for ( var id in _oRooms )
	{
		if ( this.m_oRooms[ id ].count != _oRooms[ id ].count )
		{
			var oDivName = document.getElementById( "aslko" + id );
			if ( oDivName != null )
			{
				oDivName.nextSibling.innerHTML = _oRooms[ id ].count;
			}
			this.m_oRooms[ id ].count = _oRooms[ id ].count;
		}
	}
}

ChatList.prototype.GetChatRoomName = function( _idChatRoom )
{
	return this.m_oRooms[ _idChatRoom ].name;
}

ChatList.prototype.Activate = function( _oChat )
{
	_oChat.SwitchTo( _oChat.m_hListContent, _oChat.m_hListContentPane );
	this.m_hTab.className = "chat-tab chat-tab-active";
	this.m_bRedraw = true;
	this.Redraw( _oChat );
}

ChatList.prototype.Deactivate = function()
{
	this.m_hTab.className = "chat-tab";
}

ChatList.prototype.Redraw = function( _oChat )
{
	if ( this.m_bRedraw )
	{
		var iNextY = _oChat.m_oScroller.getY();
		while ( _oChat.m_hListContentSettings.nextSibling != _oChat.m_hListContentJoining )
		{
			_oChat.m_hListContentPane.removeChild( _oChat.m_hListContentSettings.nextSibling );
		}
		var aRoomIds = new Array();
		for ( var id in this.m_oRooms )
		{
			aRoomIds.push( parseInt( id, 10 ) );
		}
		aRoomIds.sort( function( _a, _b ) { return _a - _b; } );
		for ( var i = 0; i < aRoomIds.length; ++i )
		{
			var id = aRoomIds[ i ];
			var oDivName = document.createElement( "div" );
			oDivName.className = "chat-listcontent-chatroom";
			oDivName.appendChild( document.createTextNode( this.m_oRooms[ id ].name ) );
			oDivName.id = "aslko" + id;
			oDivName.onclick = function() { g_oChat.JoinChatRoom( this.id.substring( 5 ) ); };
			_oChat.m_hListContentPane.insertBefore( oDivName, _oChat.m_hListContentJoining );
			var oDivUsers = document.createElement( "div" );
			oDivUsers.className = "chat-listcontent-users";
			oDivUsers.innerHTML = this.m_oRooms[ id ].count;
			_oChat.m_hListContentPane.insertBefore( oDivUsers, _oChat.m_hListContentJoining );
		}
		_oChat.m_oScroller.setNextY( iNextY );
		_oChat.m_oScroller.init( _oChat.m_hListContent.clientHeight, _oChat.m_hListContentPane.clientHeight );
		_oChat.m_hListContentJoin.innerHTML = g_aoChatTexts[ 0 ].listJoin + "<div>&nbsp;</div>";
		_oChat.m_hListContentJoinText.innerHTML = g_aoChatTexts[ 0 ].listJoinText;
		_oChat.m_hListSettingsAcceptContact.nextSibling.innerHTML = g_aoChatTexts[ 0 ].listOptionAcceptContacts;
	}
	this.m_bRedraw = false;
}

function MailCompose( _oChat )
{
	this.m_sId = "%%compose";
	this.m_sName = g_aoChatTexts[ 0 ].mailComposeTitle;
	this.m_bRedraw = false;
	this.m_hTab = document.createElement( "div" );
	this.m_hTab.appendChild( document.createTextNode( this.GetDisplayName( _oChat ) ) );
	this.m_hTab.className = "chat-tab";
	this.m_hTab.onclick = function() { g_oChat.RoomActivate( this.id.substring( 5 ) ); };
	this.m_hTab.id = "xyzXY" + this.m_sId;
	var oDiv = document.createElement( "div" );
	oDiv.className = "chat-tab-end";
	this.m_hTab.appendChild( oDiv );
}

MailCompose.prototype.GetDisplayName = function( _oChat )
{
	return this.m_sName;
}

MailCompose.prototype.AddMessages = function( _aMessages )
{
}

MailCompose.prototype.Activate = function( _oChat )
{
	_oChat.SwitchTo( _oChat.m_hComposeContent );
	this.m_hTab.className = "chat-tab chat-tab-active";
	this.m_bRedraw = true;
	this.Redraw( _oChat );
}

MailCompose.prototype.Deactivate = function()
{
	this.m_hTab.className = "chat-tab";
}

MailCompose.prototype.Redraw = function( _oChat )
{
	if ( this.m_bRedraw )
	{
		_oChat.m_hComposeText.value = "";
		_oChat.m_hComposeHeadline.value = "";
		_oChat.m_hComposeLabelAddress.innerHTML = g_aoChatTexts[ 0 ].mailReceiver;
		_oChat.m_hComposeLabelHeadline.innerHTML = g_aoChatTexts[ 0 ].mailSubject;
		_oChat.m_hComposeLabelText.innerHTML = g_aoChatTexts[ 0 ].mailText;
		_oChat.m_hComposeSend.innerHTML = g_aoChatTexts[ 0 ].mailSend + "<div>&nbsp;</div>";
		_oChat.m_hComposeFixtures.innerHTML = g_aoChatTexts[ 0 ].mailFixtures + "<div>&nbsp;</div>";
	}
	_oChat.FillComposeAutoComplete();
	_oChat.OnCheckFixtures();
	this.m_bRedraw = false;
}

function MailList( _sId, _sName, _oChat, _aMails )
{
	this.m_sId = _sId;
	this.m_sName = _sName;
	this.m_aMails = _aMails;
	this.m_bRedraw = false;
	this.m_hTab = document.createElement( "div" );
	this.m_hTab.appendChild( document.createTextNode( this.GetDisplayName( _oChat ) ) );
	this.m_hTab.className = "chat-tab";
	this.m_hTab.onclick = function() { g_oChat.RoomActivate( this.id.substring( 5 ) ); };
	this.m_hTab.id = "xyzXY" + _sId;
	var oDiv = document.createElement( "div" );
	oDiv.className = "chat-tab-end";
	this.m_hTab.appendChild( oDiv );
	this.CheckNewInput();
}

MailList.prototype.GetDisplayName = function( _oChat )
{
	return this.m_sName;
}

MailList.prototype.CheckNewInput = function()
{
	if ( this.m_sId != "%%inbox" )
	{
		return;
	}
	var bNewInput = false;
	for ( var m in this.m_aMails )
	{
		if ( this.m_aMails[ m ].status == "new" )
		{
			bNewInput = true;
			break;
		} 
	}
	var iPos = this.m_hTab.className.indexOf( " chat-tab-new-input" );
	if ( bNewInput != ( iPos >= 0 ) )
	{
		this.m_hTab.className = bNewInput ? this.m_hTab.className + " chat-tab-new-input" : this.m_hTab.className.substr( 0, iPos );
	}
}

MailList.prototype.AddMails = function( _aMails )
{
	for ( var m in _aMails )
	{
		if ( typeof( this.m_aMails[ m ] ) == "undefined" )
		{
			this.m_aMails[ m ] = _aMails[ m ];
			this.m_bRedraw = true;
		}
	}
	this.CheckNewInput();
}

MailList.prototype.AddMessages = function( _aMessages )
{
}

MailList.prototype.Activate = function( _oChat )
{
	_oChat.SwitchTo( _oChat.m_hBoxContent, _oChat.m_hBoxContentTextPane );
	this.m_hTab.className = "chat-tab chat-tab-active";
	this.m_bRedraw = true;
	this.Redraw( _oChat );
	this.CheckNewInput();
}

MailList.prototype.Deactivate = function()
{
	this.m_hTab.className = "chat-tab";
	this.CheckNewInput();
}

MailList.prototype.Redraw = function( _oChat )
{
	if ( this.m_bRedraw )
	{
		_oChat.m_hBoxContentBar.innerHTML = "";
		_oChat.m_hBoxContentTextPane.innerHTML = "";
		var oTime = document.createElement( "div" );
		var oAuthor = document.createElement( "div" );
		var oHeadline = document.createElement( "div" );
		oTime.className = "chat-box-time chat-box-title";
		oAuthor.className = "chat-box-author chat-box-title";
		oHeadline.className = "chat-box-headline chat-box-title";
		oTime.innerHTML = g_aoChatTexts[ 0 ].boxDate;
		oAuthor.innerHTML = ( this.m_sId == "%%inbox" ) ? g_aoChatTexts[ 0 ].boxAuthor : g_aoChatTexts[ 0 ].boxRecv;
		oHeadline.innerHTML = g_aoChatTexts[ 0 ].boxSubject;
		_oChat.m_hBoxContentBar.appendChild( oTime );
		_oChat.m_hBoxContentBar.appendChild( oAuthor );
		_oChat.m_hBoxContentBar.appendChild( oHeadline );
		for ( var m in this.m_aMails )
		{
			oTime = document.createElement( "div" );
			oAuthor = document.createElement( "div" );
			oHeadline = document.createElement( "div" );
			oTime.className = "chat-box-time";
			oAuthor.className = "chat-box-author";
			oHeadline.className = ( this.m_aMails[ m ].status == "new" ) ? "chat-box-headline chat-box-new" : "chat-box-headline";
			var sTime = this.m_aMails[ m ].created;
			oTime.innerHTML = sTime.substr( 8, 2 ) + "." + sTime.substr( 5, 2 ) + ".";
			if ( this.m_sId == "%%inbox" )
			{
				oAuthor.innerHTML = _oChat.GetUserName( this.m_aMails[ m ].userId );
				var sHero = _oChat.GetUserHeroName( this.m_aMails[ m ].userId );
				if ( ( sHero != null ) && ( sHero != "" ) )
				{
					oAuthor.title = g_aoChatTexts[ 0 ].userHero + sHero;
				}
			}
			else if ( this.m_aMails[ m ].recv.length == 1 )
			{
				oAuthor.innerHTML = _oChat.GetUserName( this.m_aMails[ m ].recv[ 0 ] );
				var sHero = _oChat.GetUserHeroName( this.m_aMails[ m ].recv[ 0 ] );
				if ( ( sHero != null ) && ( sHero != "" ) )
				{
					oAuthor.title = g_aoChatTexts[ 0 ].userHero + sHero;
				}
			}
			else
			{
				oAuthor.innerHTML = g_aoChatTexts[ 0 ].userMany;
				var aRecv = new Array();
				for ( var i = 0; i < this.m_aMails[ m ].recv.length; ++i )
				{
					var sName = _oChat.GetUserName( this.m_aMails[ m ].recv[ i ] );
					var sHero = _oChat.GetUserHeroName( this.m_aMails[ m ].recv[ 0 ] );
					if ( ( sHero != null ) && ( sHero != "" ) )
					{
						sName += " (" + g_aoChatTexts[ 0 ].userHero + sHero + ")";
					}
					aRecv.push( sName );
				}
				oAuthor.title = aRecv.join( ", " );
			}
			var sText = this.m_aMails[ m ].headline;
			sText = sText.replace( new RegExp( "&", "g" ), "&amp;" );
			sText = sText.replace( new RegExp( "<", "g" ), "&lt;" );
			sText = sText.replace( new RegExp( ">", "g" ), "&gt;" );
			sText = sText.replace( new RegExp( "\"", "g" ), "&quot;" );
			oHeadline.innerHTML = sText + "&nbsp;";
			oHeadline.id = "abcD" + this.m_sId.substr( 2, 1 ) + m;
			oHeadline.onclick = function() { g_oChat.SelectMail( this.id.substring( 5 ) ); };
			_oChat.m_hBoxContentTextPane.appendChild( oTime );
			_oChat.m_hBoxContentTextPane.appendChild( oAuthor );
			_oChat.m_hBoxContentTextPane.appendChild( oHeadline );
		}
		_oChat.m_oScroller.init( _oChat.m_hBoxContentText.clientHeight, _oChat.m_hBoxContentTextPane.clientHeight );
	}
	this.m_bRedraw = false;
}

MailList.prototype.SelectMail = function( _oChat, _sMailId )
{
	_oChat.SwitchTo( _oChat.m_hMailContent, _oChat.m_hMailContentTextPane );
	if ( this.m_sId == "%%inbox" )
	{	
		_oChat.m_hMailContentDelete.tgtg = _sMailId;
		_oChat.m_hMailContentDelete.style.display = "block";
		_oChat.m_hMailContentReply.tgtg = _sMailId;
		_oChat.m_hMailContentReply.style.display = "block";
	}
	else
	{
		_oChat.m_hMailContentDelete.style.display = "none";
		_oChat.m_hMailContentReply.style.display = "none";
	}
	_oChat.m_hMailContentDelete.title = g_aoChatTexts[ 0 ].mailButtonDelete;
	_oChat.m_hMailContentReply.title = g_aoChatTexts[ 0 ].mailButtonReply;
	_oChat.m_hMailContentClose.title = g_aoChatTexts[ 0 ].mailButtonClose;
	
	var sTime = this.m_aMails[ _sMailId ].created;
	_oChat.m_hMailContentTime.innerHTML = g_aoChatTexts[ 0 ].mailRecvTime + " " + sTime.substr( 8, 2 ) + "." + sTime.substr( 5, 2 ) + ".";
	
	var sText = this.m_aMails[ _sMailId ].headline;
	sText = sText.replace( new RegExp( "&", "g" ), "&amp;" );
	sText = sText.replace( new RegExp( "<", "g" ), "&lt;" );
	sText = sText.replace( new RegExp( ">", "g" ), "&gt;" );
	sText = sText.replace( new RegExp( "\"", "g" ), "&quot;" );
	_oChat.m_hMailContentHeadline.innerHTML = g_aoChatTexts[ 0 ].mailSubject + " " + sText;
	
	if ( this.m_sId == "%%inbox" )
	{
		_oChat.m_hMailContentAuthor.innerHTML = g_aoChatTexts[ 0 ].boxAuthor + ": " + _oChat.GetUserName( this.m_aMails[ _sMailId ].userId );
		var sHero = _oChat.GetUserHeroName( this.m_aMails[ _sMailId ].userId );
		if ( ( sHero != null ) && ( sHero != "" ) )
		{
			_oChat.m_hMailContentAuthor.title = g_aoChatTexts[ 0 ].userHero + sHero;
		}
	}
	else if ( this.m_aMails[ _sMailId ].recv.length == 1 )
	{
		_oChat.m_hMailContentAuthor.innerHTML = g_aoChatTexts[ 0 ].boxRecv + ": " + _oChat.GetUserName( this.m_aMails[ _sMailId ].recv[ 0 ] );
		var sHero = _oChat.GetUserHeroName( this.m_aMails[ _sMailId ].recv[ 0 ] );
		if ( ( sHero != null ) && ( sHero != "" ) )
		{
			_oChat.m_hMailContentAuthor.title = g_aoChatTexts[ 0 ].userHero + sHero;
		}
	}
	else
	{
		_oChat.m_hMailContentAuthor.innerHTML = g_aoChatTexts[ 0 ].boxRecv + ": " + g_aoChatTexts[ 0 ].userMany;
		var aRecv = new Array();
		for ( var i = 0; i < this.m_aMails[ _sMailId ].recv.length; ++i )
		{
			var sName = _oChat.GetUserName( this.m_aMails[ _sMailId ].recv[ i ] );
			var sHero = _oChat.GetUserHeroName( this.m_aMails[ _sMailId ].recv[ 0 ] );
			if ( ( sHero != null ) && ( sHero != "" ) )
			{
				sName += " (" + g_aoChatTexts[ 0 ].userHero + sHero + ")";
			}
			aRecv.push( sName );
		}
		_oChat.m_hMailContentAuthor.title = aRecv.join( ", " );
	}
	
	sText = this.m_aMails[ _sMailId ].text;
	sText = sText.replace( new RegExp( "&", "g" ), "&amp;" );
	sText = sText.replace( new RegExp( "<", "g" ), "&lt;" );
	sText = sText.replace( new RegExp( ">", "g" ), "&gt;" );
	sText = sText.replace( new RegExp( "\"", "g" ), "&quot;" );
	sText = sText.replace( new RegExp( "\n", "g" ), "<br/>" );
	sText = sText.replace( new RegExp( "^&gt; ([^<>]*)$", "g" ), "<span>$1</span>" );
	sText = sText.replace( new RegExp( "^&gt; ([^<>]*)<", "g" ), "<span>$1</span><" );
	sText = sText.replace( new RegExp( ">&gt; ([^<>]*)$", "g" ), "><span>$1</span>" );
	sText = sText.replace( new RegExp( ">&gt; ([^<>]*)<", "g" ), "><span>$1</span><" );
	sText = sText.replace( new RegExp( "</span><br/><span>", "g" ), "<br/>" );
	sText = sText.replace( new RegExp( ">&gt; ([^<>]*)<", "g" ), "><span>$1</span><" );
	sText = sText.replace( new RegExp( "</span><br/><span>", "g" ), "<br/>" );
	sText = sText.replace( new RegExp( ">&gt; ([^<>]*)<", "g" ), "><span>$1</span><" );
	sText = sText.replace( new RegExp( "</span><br/><span>", "g" ), "<br/>" );
	sText = sText.replace( new RegExp( "</span><br/>", "g" ), "</span>" );
	sText = sText.replace( new RegExp( "\\*([^<>]+)\\*", "g" ), "<b>$1</b>" );
	sText = sText.replace( new RegExp( "_([^<>]+)_", "g" ), "<i>$1</i>" );
	_oChat.m_hMailContentTextPane.innerHTML = sText;
	if ( this.m_aMails[ _sMailId ].status == "new" )
	{
		this.m_aMails[ _sMailId ].status = "read";
		var oHeadline = document.getElementById( "abcDi" + _sMailId );
		if ( oHeadline )
		{
			oHeadline.className = "chat-box-headline";
		}
		this.CheckNewInput();
	}
	_oChat.m_oScroller.init( _oChat.m_hMailContentText.clientHeight, _oChat.m_hMailContentTextPane.clientHeight );
}

MailList.prototype.DeleteMail = function( _oChat, _sMailId )
{
	delete this.m_aMails[ _sMailId ];
	this.m_bRedraw = true;
}

MailList.prototype.GetMail = function( _sMailId )
{
	return this.m_aMails[ _sMailId ];
}

function ChatRoom( _sId, _sName, _bPrivate, _oChat )
{
	this.m_sId = _sId;
	this.m_bIsPrivate = _bPrivate;
	this.m_sName = _sName;
	this.m_aUserIds = new Array();
	this.m_aMessages = new Array();
	this.m_bRedraw = false;
	this.m_hTab = document.createElement( "div" );
	this.m_hTab.appendChild( document.createTextNode( this.GetDisplayName( _oChat ) ) );
	this.m_hTab.className = "chat-tab chat-tab-with-close";
	this.m_hTab.onclick = function() { g_oChat.RoomActivate( this.id.substring( 5 ) ); };
	this.m_hTab.id = "xyzXY" + _sId;
	var oClose = document.createElement( "div" );
	oClose.className = "chat-close";
	oClose.onclick = function() { g_oChat.RoomClose( this.parentNode.id.substring( 5 ) ); };
	this.m_hTab.appendChild( oClose );
	var oDiv = document.createElement( "div" );
	oDiv.className = "chat-tab-end";
	this.m_hTab.appendChild( oDiv );
}

ChatRoom.prototype.GetDisplayName = function( _oChat )
{
	if ( this.m_sName.substr( 0, 1 ) == "%" )
	{
		var aUsers = this.m_sName.split( "%" );
		if ( aUsers.length >= 3 )
		{
			return _oChat.GetUserName( aUsers[ ( aUsers[ 1 ] == _oChat.GetOwnUserId() ) ? 2 : 1 ] ); 
		}
	}
	return this.m_sName;
}

ChatRoom.prototype.AddMessages = function( _aMessages )
{
	for ( var i = 0; i < _aMessages.length; ++i )
	{
		if ( _aMessages[ i ].roomId == this.m_sId )
		{
			var bFound = false;
			for ( var j = 0; j < this.m_aMessages.length; ++j )
			{
				if ( this.m_aMessages[ j ].id == _aMessages[ i ].id )
				{
					bFound = true;
					break;
				}
			}
			if ( !bFound )
			{
				this.m_aMessages.push( _aMessages[ i ] );
				this.m_bRedraw = true;
				if ( this.m_hTab.className.indexOf( 'chat-tab-active' ) < 0 )
				{
					this.m_hTab.className = "chat-tab chat-tab-with-close chat-tab-new-input";
				}
			}
		}
	}
	if ( this.m_aMessages.length > 100 )
	{
		this.m_aMessages.splice( 0, this.m_aMessages.length - 100 );
		this.m_bRedraw = true;
	}
}

ChatRoom.prototype.Remove = function()
{
	this.m_hTab.parentNode.removeChild( this.m_hTab );
}

ChatRoom.prototype.Activate = function( _oChat )
{
	_oChat.SwitchTo( _oChat.m_hChatContent, _oChat.m_hChatTextContentPane );
	this.m_hTab.className = "chat-tab chat-tab-with-close chat-tab-active";
	this.m_bRedraw = true;
	this.Redraw( _oChat );
	_oChat.m_oScroller.setToEnd();
}

ChatRoom.prototype.Deactivate = function()
{
	this.m_hTab.className = "chat-tab chat-tab-with-close";
}

ChatRoom.prototype.ParseMessageText = function( _sText )
{
	if ( _sText.substr( 0, 1 ) == "\\" )
	{
		var saMessages = g_aoChatTexts[ 0 ].chatMessages;
		var i = _sText.indexOf( '\\', 1 );
		if ( i >= 1 )
		{
			var j = parseInt( _sText.substr( 1, i - 1 ) );
			if ( j < saMessages.length )
			{
				_sText = "<i>" + saMessages[ j ] + "</i>" + _sText.substr( i + 1 );
			}
		}
	}
	return _sText;
}

ChatRoom.prototype.Redraw = function( _oChat )
{
	if ( this.m_bRedraw )
	{
		var bAtEnd = _oChat.m_oScroller.isAtEnd();
		var iNextY = _oChat.m_oScroller.getY();
		_oChat.m_hChatTextContentPane.innerHTML = "";
		for ( var i = 0; i < this.m_aMessages.length; ++i )
		{
			var oTime = document.createElement( "span" );
			oTime.innerHTML = this.m_aMessages[ i ].created;
			oTime.className = "chat-chattext-time";
			var oAuthor = document.createElement( "span" );
			var sName = _oChat.GetUserName( this.m_aMessages[ i ].userId );
			oAuthor.innerHTML = sName;
			oAuthor.className = "chat-chattext-author";
			oAuthor.tgtg = this.m_aMessages[ i ].userId;
			if ( this.m_aMessages[ i ].userId != _oChat.GetOwnUserId() )
			{
				oAuthor.onclick = function() { g_oChat.Query( this.tgtg ); };
			}
			else
			{
				oAuthor.style.cursor = "default";
			}
			var sTitle = _oChat.GetUserHeroName( this.m_aMessages[ i ].userId );
			if ( ( sTitle != null ) && ( sTitle != "" ) )
			{
				oAuthor.title = sName + " (" + g_aoChatTexts[ 0 ].userHero + sTitle + ")";
			}
			else
			{
				oAuthor.title = sName;
			}
			var oText = document.createElement( "span" );
			var sText = this.m_aMessages[ i ].text;
			sText = sText.replace( new RegExp( "&", "g" ), "&amp;" );
			sText = sText.replace( new RegExp( "<", "g" ), "&lt;" );
			sText = sText.replace( new RegExp( ">", "g" ), "&gt;" );
			sText = sText.replace( new RegExp( "\"", "g" ), "&quot;" );
			oText.innerHTML = this.ParseMessageText( sText );
			oText.className = "chat-chattext-text";
			_oChat.m_hChatTextContentPane.appendChild( oTime );
			_oChat.m_hChatTextContentPane.appendChild( oAuthor );
			_oChat.m_hChatTextContentPane.appendChild( oText );
			_oChat.m_hChatTextContentPane.appendChild( document.createElement( "br" ) );
		}
		if ( !bAtEnd )
		{
			_oChat.m_oScroller.setNextY( iNextY );
		}
		_oChat.m_oScroller.init( _oChat.m_hChatTextContent.clientHeight, _oChat.m_hChatTextContentPane.clientHeight );
		if ( bAtEnd )
		{
			_oChat.m_oScroller.setToEnd();
		}
		_oChat.m_hChatInputText.innerHTML = g_aoChatTexts[ 0 ].chatText;
		_oChat.m_hChatSend.innerHTML = g_aoChatTexts[ 0 ].chatSend + "<div>&nbsp;</div>";
		_oChat.m_hChatFixtures.innerHTML = g_aoChatTexts[ 0 ].mailFixtures + "<div>&nbsp;</div>";
		_oChat.m_hChatFixtures.style.display = ( this.m_sName.substr( 0, 1 ) == "%" ) ? "block" : "none";
	}
	this.m_bRedraw = false;
}

function Chat( _idUser )
{
	this.m_oChatRooms = new Object();
	this.m_oUsers = new Object();
	this.m_oAutoCompleteUsers = new Object();
	this.m_iLastChatId = 0;
	this.m_iLastMailId = 0;
	this.m_iComposeCursor = 0;
	this.m_idComposeUser = "-1";
	this.m_idUser = _idUser;
	this.m_sLastMouseOver = "-1";
	this.m_iMailListNextY = 0;
	this.m_oActChatRoom = null;
	this.m_oHttpRequest = null;
	this.m_afnActCallbacks = new Array();
	this.m_afnNextCallbacks = new Array();
	this.m_hConsole = document.getElementById( "console" );
	this.m_hContainer = document.getElementById( "chat-container" );
	this.m_hBackground = document.createElement( "div" );
	this.m_hContainer.appendChild( this.m_hBackground );
	this.m_hBackground.id = "chat-background";
	this.m_hTabsLeft = document.createElement( "div" );
	this.m_hContainer.appendChild( this.m_hTabsLeft );
	this.m_hTabsLeft.id = "chat-tabs-left";
	this.m_hTabsLeft.style.display = "none";
	this.m_hTabsLeft.onclick = function () { g_oChat.OnTabsLeft(); };
	this.m_hTabsRight = document.createElement( "div" );
	this.m_hContainer.appendChild( this.m_hTabsRight );
	this.m_hTabsRight.id = "chat-tabs-right";
	this.m_hTabsRight.style.display = "none";
	this.m_hTabsRight.onclick = function () { g_oChat.OnTabsRight(); };
	this.m_hTabs = document.createElement( "div" );
	this.m_hContainer.appendChild( this.m_hTabs );
	this.m_hTabs.id = "chat-tabs";
	this.m_hTabsPane = document.createElement( "div" );
	this.m_hTabs.appendChild( this.m_hTabsPane );
	this.m_hTabsPane.id = "chat-tabs-pane";
	this.m_hTabsPane.style.left = "0px";
	this.m_hTabs2 = document.createElement( "div" );
	this.m_hContainer.appendChild( this.m_hTabs2 );
	this.m_hTabs2.id = "chat-tabs2";
	this.m_hChatContent = document.createElement( "div" );
	this.m_hContainer.appendChild( this.m_hChatContent );
	this.m_hChatContent.id = "chat-chatcontent";
	this.m_hChatTextContent = document.createElement( "div" );
	this.m_hChatContent.appendChild( this.m_hChatTextContent );
	this.m_hChatTextContent.id = "chat-chattextcontent";
	this.m_hChatTextContentPane = document.createElement( "div" );
	this.m_hChatTextContent.appendChild( this.m_hChatTextContentPane );
	this.m_hChatTextContentPane.id = "chat-chattextcontent-pane";
	this.m_hChatForm = document.createElement( "form" );
	this.m_hChatContent.appendChild( this.m_hChatForm );
	this.m_hChatForm.id = "chat-chatform";
	this.m_hChatInputText = document.createElement( "div" );
	this.m_hChatInputText.id = "chat-chatinputtext";
	this.m_hChatInput = document.createElement( "input" );
	this.m_hChatInput.type = "text";
	this.m_hChatInput.onkeypress = function ( e ) { g_oChat.CheckInputText( "m_hChatInput", 250 ); e = ( e ) ? e : window.event; var kc = ( e.keyCode ) ? e.keyCode : e.which; if ( kc == 13 ) { g_oChat.OnSend(); return false; }; };
	this.m_hChatInput.onkeyup = function() { g_oChat.CheckInputText( "m_hChatInput", 250 ); };
	this.m_hChatForm.appendChild( this.m_hChatInput );
	this.m_hChatForm.appendChild( this.m_hChatInputText );
	this.m_hChatInput.id = "chat-chatinput";
	this.m_hChatSend = document.createElement( "div" );
	this.m_hChatSend.onclick = function () { g_oChat.OnSend(); };
	this.m_hChatForm.appendChild( this.m_hChatSend );
	this.m_hChatSend.id = "chat-chatsend";
	this.m_hChatFixtures = document.createElement( "div" );
	this.m_hChatFixtures.onclick = function () { g_oChat.OnChatFixtures(); };
	this.m_hChatForm.appendChild( this.m_hChatFixtures );
	this.m_hChatFixtures.id = "chat-chatfixtures";
	this.m_hScroll = document.createElement( "div" );
	this.m_hScrollEnd = document.createElement( "div" );
	this.m_hScrollBar = document.createElement( "div" );
	this.m_hScrollFrame = document.createElement( "div" );
	this.m_hScroller = document.createElement( "div" );
	this.m_hScrollImg = document.createElement( "img" );
	this.m_hScroll.className = "chat-scroll sb_scroll";
	this.m_hScrollEnd.className = "sb_scrollend";
	this.m_hScrollBar.className = "sb_scrollbar";
	this.m_hScrollFrame.className = "sb_scrollframe";
	this.m_hScroller.className = "sb_scroller";
	this.m_hScrollImg.onmousedown = function( _event ) { if ( _event.preventDefault ) _event.preventDefault(); };
	this.m_hScrollImg.ondrag = function() { return false; };
	this.m_hScrollImg.src = "../dsa_game/gfx/scrollbar.png";
	this.m_hScrollImg.alt = "Scroller";
	this.m_hScroll.appendChild( this.m_hScrollEnd );
	this.m_hScroll.appendChild( this.m_hScrollBar );
	this.m_hScrollBar.appendChild( this.m_hScrollFrame );
	this.m_hScrollFrame.appendChild( this.m_hScroller );
	this.m_hScroller.appendChild( this.m_hScrollImg );
	this.m_hContainer.appendChild( this.m_hScroll );
	this.m_oScroller = new Scroller( this.m_hScroll, this.m_hChatTextContent );
	this.m_hBoxContent = document.createElement( "div" );
	this.m_hContainer.appendChild( this.m_hBoxContent );
	this.m_hBoxContent.id = "chat-boxcontent";
	this.m_hBoxContent.style.display = "none";
	this.m_hBoxContentBar = document.createElement( "div" );
	this.m_hBoxContent.appendChild( this.m_hBoxContentBar );
	this.m_hBoxContentBar.id = "chat-boxcontent-bar";
	this.m_hBoxContentText = document.createElement( "div" );
	this.m_hBoxContent.appendChild( this.m_hBoxContentText );
	this.m_hBoxContentText.id = "chat-boxcontent-text";
	this.m_hBoxContentTextPane = document.createElement( "div" );
	this.m_hBoxContentText.appendChild( this.m_hBoxContentTextPane );
	this.m_hBoxContentTextPane.id = "chat-boxcontent-text-pane";
	this.m_hMailContent = document.createElement( "div" );
	this.m_hContainer.appendChild( this.m_hMailContent );
	this.m_hMailContent.className = "chat-mailcontent";
	this.m_hMailContent.style.display = "none";
	this.m_hMailContentBar = document.createElement( "div" );
	this.m_hMailContent.appendChild( this.m_hMailContentBar );
	this.m_hMailContentBar.className = "chat-mail-bar";
	this.m_hMailContentClose = document.createElement( "div" );
	this.m_hMailContentBar.appendChild( this.m_hMailContentClose );
	this.m_hMailContentClose.className = "chat-mail-close";
	this.m_hMailContentClose.onclick = function() { g_oChat.CloseMail(); };
	this.m_hMailContentReply = document.createElement( "div" );
	this.m_hMailContentBar.appendChild( this.m_hMailContentReply );
	this.m_hMailContentReply.className = "chat-mail-reply";
	this.m_hMailContentReply.onclick = function() { g_oChat.ReplyMail( this.tgtg ); };
	this.m_hMailContentAuthor = document.createElement( "div" );
	this.m_hMailContentBar.appendChild( this.m_hMailContentAuthor );
	this.m_hMailContentAuthor.className = "chat-mail-author";
	this.m_hMailContentHeadline = document.createElement( "div" );
	this.m_hMailContentBar.appendChild( this.m_hMailContentHeadline );
	this.m_hMailContentHeadline.className = "chat-mail-headline";
	this.m_hMailContentText = document.createElement( "div" );
	this.m_hMailContent.appendChild( this.m_hMailContentText );
	this.m_hMailContentText.className = "chat-mail-text";
	this.m_hMailContentTextPane = document.createElement( "div" );
	this.m_hMailContentText.appendChild( this.m_hMailContentTextPane );
	this.m_hMailContentTextPane.className = "chat-mail-text-pane";
	this.m_hMailContentFooter = document.createElement( "div" );
	this.m_hMailContent.appendChild( this.m_hMailContentFooter );
	this.m_hMailContentFooter.className = "chat-mail-footer";
	this.m_hMailContentDelete = document.createElement( "div" );
	this.m_hMailContentFooter.appendChild( this.m_hMailContentDelete );
	this.m_hMailContentDelete.className = "chat-mail-delete";
	this.m_hMailContentDelete.onclick = function() { g_oChat.DeleteMail( this.tgtg ); };
	this.m_hMailContentTime = document.createElement( "div" );
	this.m_hMailContentFooter.appendChild( this.m_hMailContentTime );
	this.m_hMailContentTime.className = "chat-mail-time";
	this.m_hListContent = document.createElement( "div" );
	this.m_hContainer.appendChild( this.m_hListContent );
	this.m_hListContent.id = "chat-listcontent";
	this.m_hListContent.style.display = "none";
	this.m_hListContentPane = document.createElement( "div" );
	this.m_hListContent.appendChild( this.m_hListContentPane );
	this.m_hListContentPane.id = "chat-listcontent-pane";
	this.m_hListContentSettings = document.createElement( "form" );
	this.m_hListContentPane.appendChild( this.m_hListContentSettings );
	this.m_hListContentSettings.id = "chat-listcontent-settings";
	this.m_hListSettingsAcceptContact = document.createElement( "input" );
	this.m_hListSettingsAcceptContact.type = "checkbox";
	this.m_hListContentSettings.appendChild( this.m_hListSettingsAcceptContact );
	this.m_hListSettingsAcceptContact.className = "chat-listcontent-checkbox";
	this.m_hListSettingsAcceptContact.onclick = function() { g_oChat.Command( "/setoption AcceptContacts " + ( g_oChat.m_hListSettingsAcceptContact.checked ? "1" : "0" ) ); };
	var oDiv = document.createElement( "div" );
	oDiv.className = "chat-listcontent-text";
	this.m_hListContentSettings.appendChild( oDiv );
	this.m_hListContentJoining = document.createElement( "form" );
	this.m_hListContentPane.appendChild( this.m_hListContentJoining );
	this.m_hListContentJoining.id = "chat-listcontent-joining";
	this.m_hListContentJoinText = document.createElement( "div" );
	this.m_hListContentJoinText.id = "chat-listcontent-jointext";
	this.m_hListContentJoining.appendChild( this.m_hListContentJoinText );
	this.m_hListContentJoinRoom = document.createElement( "input" );
	this.m_hListContentJoinRoom.type = "text";
	this.m_hListContentJoinRoom.id = "chat-listcontent-joinroom";
	this.m_hListContentJoinRoom.onkeypress = function ( e ) { g_oChat.CheckInputText( "m_hListContentJoinRoom", 25 ); e = ( e ) ? e : window.event; var kc = ( e.keyCode ) ? e.keyCode : e.which; if ( kc == 13 ) { g_oChat.OnJoinNewChatRoom(); return false; }; };
	this.m_hListContentJoinRoom.onkeyup = function() { g_oChat.CheckInputText( "m_hListContentJoinRoom", 25 ); };
	this.m_hListContentJoining.appendChild( this.m_hListContentJoinRoom );
	this.m_hListContentJoin = document.createElement( "div" );
	this.m_hListContentJoining.appendChild( this.m_hListContentJoin );
	this.m_hListContentJoin.id = "chat-listcontent-join";
	this.m_hListContentJoin.onclick = function () { g_oChat.OnJoinNewChatRoom(); };
	this.m_hListContentErrorPane = document.createElement( "div" );
	this.m_hListContentPane.appendChild( this.m_hListContentErrorPane );
	this.m_hListContentErrorPane.id = "chat-listcontent-errorpane";
	this.m_hListContentErrorPane.style.display = "none";
	this.m_hComposeContent = document.createElement( "div" );
	this.m_hContainer.appendChild( this.m_hComposeContent );
	this.m_hComposeContent.id = "chat-composecontent";
	this.m_hComposeContent.style.display = "none";
	this.m_hComposeForm = document.createElement( "form" );
	this.m_hComposeContent.appendChild( this.m_hComposeForm );
	this.m_hComposeForm.id = "chat-composeform";
	this.m_hComposeLabelAddress = document.createElement( "div" );
	this.m_hComposeForm.appendChild( this.m_hComposeLabelAddress );
	this.m_hComposeLabelAddress.className = "chat-composelabel";
	this.m_hComposeAddress = document.createElement( "input" );
	this.m_hComposeAddress.type = "text";
	this.m_hComposeForm.appendChild( this.m_hComposeAddress );
	this.m_hComposeAddress.id = "chat-composeaddress";
	this.m_hComposeAddress.onfocus = function () { g_oChat.OnComposeFocus(); };
	this.m_hComposeAddress.onblur = function () { g_oChat.OnComposeBlur(); };
	this.m_hComposeAddress.onkeydown = function ( _event ) { g_oChat.OnComposeKeyDown( _event ); };
	this.m_hComposeAddress.onkeyup = function () { g_oChat.CheckInputText( "m_hComposeAddress", 20 ); g_oChat.OnComposeChange(); };
	this.m_hComposeLabelHeadline = document.createElement( "div" );
	this.m_hComposeForm.appendChild( this.m_hComposeLabelHeadline );
	this.m_hComposeLabelHeadline.className = "chat-composelabel";
	this.m_hComposeHeadline = document.createElement( "input" );
	this.m_hComposeHeadline.type = "text";
	this.m_hComposeHeadline.onkeyup = function() { g_oChat.CheckInputText( "m_hComposeHeadline", 250 ); };
	this.m_hComposeForm.appendChild( this.m_hComposeHeadline );
	this.m_hComposeHeadline.id = "chat-composeheadline";
	this.m_hComposeLabelText = document.createElement( "div" );
	this.m_hComposeForm.appendChild( this.m_hComposeLabelText );
	this.m_hComposeLabelText.className = "chat-composelabel";
	this.m_hComposeText = document.createElement( "textarea" );
	this.m_hComposeText.setAttribute( "wrap", "off" );
	this.m_hComposeForm.appendChild( this.m_hComposeText );
	this.m_hComposeText.id = "chat-composetext";
	this.m_hComposeText.onkeyup = function() { g_oChat.CheckInputText( "m_hComposeText", 2000 ); };
	this.m_hComposeSend = document.createElement( "div" );
	this.m_hComposeSend.onclick = function () { g_oChat.OnCompose(); };
	this.m_hComposeForm.appendChild( this.m_hComposeSend );
	this.m_hComposeSend.id = "chat-composesend";
	this.m_hComposeAutoComplete = document.createElement( "div" );
	this.m_hComposeForm.appendChild( this.m_hComposeAutoComplete );
	this.m_hComposeAutoComplete.id = "chat-composeautocomplete";
	this.m_hComposeFixtures = document.createElement( "div" );
	this.m_hComposeFixtures.onclick = function () { g_oChat.OnComposeFixtures(); };
	this.m_hComposeForm.appendChild( this.m_hComposeFixtures );
	this.m_hComposeFixtures.id = "chat-composefixtures";
	this.m_hTimeout = null;
	this.m_iLastTimeout = 500;
	this.m_oChatRooms[ "%%list" ] = new ChatList( this );
	this.m_hTabs2.appendChild( this.m_oChatRooms[ "%%list" ].m_hTab );
	this.m_oChatRooms[ "%%compose" ] = new MailCompose( this );
	this.m_hTabs2.appendChild( this.m_oChatRooms[ "%%compose" ].m_hTab );
	this.RoomActivate( "%%list" );
}

Chat.prototype.Start = function()
{
	this.Command( "/ping", null );
}

Chat.prototype.CheckInputText = function( _sVar, _iLength )
{
	if ( this[ _sVar ].value.length > _iLength )
	{
		this[ _sVar ].value = this[ _sVar ].value.substr( 0, _iLength );
	}
}

Chat.prototype.GetOwnUserId = function()
{
	return this.m_idUser;
}

Chat.prototype.SwitchTo = function( _hPane, _hScrollPane )
{
	this.m_hChatContent.style.display = "none";
	this.m_hBoxContent.style.display = "none";
	this.m_hMailContent.style.display = "none";
	this.m_hComposeContent.style.display = "none";
	this.m_hListContent.style.display = "none";
	_hPane.style.display = "block";
	if ( _hScrollPane )
	{
		this.m_oScroller.setPane( _hScrollPane );
	}
	this.m_oScroller.init( 0, 0 );
}

Chat.prototype.AddError = function( _sText )
{
	var oDiv = document.createElement( "div" );
	oDiv.className = "chat-listcontent-error";
	oDiv.appendChild( document.createTextNode( g_aoChatTexts[ 0 ].errorPrefix + _sText ) );
	this.m_hListContentErrorPane.appendChild( oDiv );
	this.m_hListContentErrorPane.style.display = "block";
}

Chat.prototype.JoinChatRoom = function( _idChatRoom )
{
	if ( typeof( this.m_oChatRooms[ _idChatRoom ] ) != "undefined" )
	{
		this.RoomActivate( _idChatRoom );
	}
	else
	{
		var sName = this.m_oChatRooms[ "%%list" ].GetChatRoomName( _idChatRoom );
		this.m_sNextRoom = _idChatRoom;
		this.m_iLastChatId = 0;
		this.Command( "/join " + sName, function() { g_oChat.RoomActivate( g_oChat.m_sNextRoom ); } );
	}
}

Chat.prototype.OnJoinNewChatRoom = function()
{
	var sName = this.m_hListContentJoinRoom.value;
	var oRegExp = new RegExp( "[^\\w\\-\\+\\*]", "g" );
	sName = sName.replace( oRegExp, "" );
	this.m_hListContentJoinRoom.value = sName;
	if ( sName != "" )
	{
		for ( var s in this.m_oChatRooms )
		{
			if ( this.m_oChatRooms[ s ].m_sName == sName )
			{
				this.RoomActivate( s );
				return;
			}
		}
		this.m_sNextRoom = sName;
		this.m_iLastChatId = 0;
		this.Command( "/join " + sName, function() { g_oChat.RoomActivateByName( g_oChat.m_sNextRoom ); g_oChat.m_hListContentJoinRoom.value = ""; } );
	}
}

Chat.prototype.OnSend = function()
{
	var sTxt = this.m_hChatInput.value;
	if ( sTxt == "" )
	{
		return;
	}
	if ( sTxt.charAt( 0 ) != "/" )
	{
		sTxt = "/send " + sTxt;
	}
	this.m_hChatInput.value = "";
	this.Command( sTxt, null );
}

Chat.prototype.OnChatFixtures = function()
{
	var aUsers = this.m_oActChatRoom.m_sName.split( "%" );
	var sUser = aUsers[ ( aUsers[ 1 ] == this.GetOwnUserId() ) ? 2 : 1 ];
	appObj.fixturesReturnURL = '/duels/duel';
	applicationRawContent({url:'/duels/fixturesUser/' + sUser});
}

Chat.prototype.OnComposeFixtures = function()
{
	if ( parseInt( this.m_idComposeUser, 10 ) > 0 )
	{
		appObj.fixturesReturnURL = '/duels/duel';
		applicationRawContent({url:'/duels/fixturesUser/' + this.m_idComposeUser});
	}
}

Chat.prototype.OnCheckFixtures = function()
{
	var bVisible = IsCheckChallengeRunning() && (document.getElementById("application-raw-content") != null);
	var bComposeVisible = bVisible && ( parseInt( this.m_idComposeUser, 10 ) > 0 );
	this.m_hChatFixtures.style.visibility = bVisible ? "visible" : "hidden";
	this.m_hComposeFixtures.style.visibility = bComposeVisible ? "visible" : "hidden";
}

Chat.prototype.OnCompose = function()
{
	var sTxt = this.m_hComposeText.value;
	var sHeadline = this.m_hComposeHeadline.value;
	var sDest = ( parseInt( this.m_idComposeUser, 10 ) > 0 ) ? this.m_idComposeUser : this.m_hComposeAddress.value;
	sHeadline = sHeadline.replace( new RegExp( "&", "g" ), "&amp;" );
	sHeadline = sHeadline.replace( new RegExp( "\"", "g" ), "&quot;" );
	this.Command( "/sendmail " + sDest + " \"" + sHeadline + "\" " + sTxt, null );
	this.m_hComposeText.value = "";
	this.m_hComposeHeadline.value = "";
}

Chat.prototype.GetComposeAutoCompleteString = function( _sString )
{
	var sText = _sString;
	sText = sText.toLowerCase();
	var iPos = 0;
	var sChars = "abcdefghijklmnopqrstuvwxyzäöüß0123456789-_";
	while ( iPos < sText.length )
	{
		var c = sText.charAt( iPos );
		iPos += 1;
		if ( c == " " )
		{
			sText = sText.substr( 0, iPos - 1 ) + "_" + sText.substr( iPos );
		}
		else if ( sChars.indexOf( c ) < 0 )
		{
			sText = sText.substr( iPos );
			iPos = 0;
		}
	}
	return sText;
}

Chat.prototype.OnComposeKeyDown = function( _event )
{
	if ( _event == null )
	{
		_event = window.event;
	}
	this.m_idComposeUser = "-1";
	var aDivs = this.m_hComposeAutoComplete.childNodes;
	switch ( _event.keyCode )
	{
		case 38: // cursor up
			if ( this.m_iComposeCursor != 0 )
			{
				aDivs[ this.m_iComposeCursor ].className = "";
				this.m_iComposeCursor -= 1;
				aDivs[ this.m_iComposeCursor ].className = "chat-composeautocomplete-selected";
			}
			break;
		case 40: // cursor down
			if ( this.m_iComposeCursor < aDivs.length - 1 )
			{
				aDivs[ this.m_iComposeCursor ].className = "";
				this.m_iComposeCursor += 1;
				aDivs[ this.m_iComposeCursor ].className = "chat-composeautocomplete-selected";
			}
			break;
		case 9: // tab
			if ( this.m_iComposeCursor < aDivs.length )
			{
				var sId = aDivs[ this.m_iComposeCursor ].id.substr( 11 );
				this.m_idComposeUser = sId;
				this.m_hComposeAddress.value = this.GetUserName( sId );
			}
			break;
		case 13: // enter
			if ( this.m_iComposeCursor < aDivs.length )
			{
				var sId = aDivs[ this.m_iComposeCursor ].id.substr( 11 );
				this.m_idComposeUser = sId;
				this.m_hComposeAddress.value = this.GetUserName( sId );
				this.m_hComposeHeadline.focus();
			}
			break;
	}
	if ( ( this.m_iComposeCursor >= aDivs.length ) && ( aDivs.length > 0 ) )
	{
		this.m_iComposeCursor = aDivs.length - 1;
		this.m_oActChatRoom.Redraw( this );
	}
}

Chat.prototype.OnComposeChange = function()
{
	var sOriginal = this.GetComposeAutoCompleteString( this.m_hComposeAddress.value );
	var sText = sOriginal;
	while ( ( sText.length > 0 ) && ( typeof( this.m_oAutoCompleteUsers[ sText ] ) == "undefined" ) )
	{
		sText = sText.substr( 0, sText.length - 1 );
	}
	if ( sText == sOriginal )
	{
		this.m_oActChatRoom.Redraw( this );
		return;
	}
	if ( sText.length > 0 )
	{
		var i = this.m_oAutoCompleteUsers[ sText ].length - 1;
		if ( sOriginal < this.GetComposeAutoCompleteString( this.m_oAutoCompleteUsers[ sText ][ i ].name.substr( 0, sOriginal.length ) ) )
		{
			// if the existing query contains all users starting with sOriginal, we must not make a new query
			this.m_oActChatRoom.Redraw( this );
			return;
		}
	}
	this.Command( "/searchuser " + sOriginal, null );
}

Chat.prototype.OnComposeFocus = function()
{
	this.m_hComposeAutoComplete.style.display = "block";
	var aDivs = this.m_hComposeAutoComplete.childNodes;
	if ( ( this.m_iComposeCursor >= aDivs.length ) && ( aDivs.length > 0 ) )
	{
		this.m_iComposeCursor = aDivs.length - 1;
		this.m_oActChatRoom.Redraw( this );
	}
}

Chat.prototype.OnComposeBlur = function()
{
	window.setTimeout( function() { g_oChat.m_hComposeAutoComplete.style.display = "none"; g_oChat.OnCheckFixtures(); }, 100 );
}

Chat.prototype.FillComposeAutoComplete = function()
{
	var sOriginal = this.GetComposeAutoCompleteString( this.m_hComposeAddress.value );
	var sText = sOriginal;
	while ( ( sText.length > 0 ) && ( typeof( this.m_oAutoCompleteUsers[ sText ] ) == "undefined" ) )
	{
		sText = sText.substr( 0, sText.length - 1 );
	}
	if ( ( sText.length == 0 ) || ( this.m_oAutoCompleteUsers[ sText ].length == 0 ) )
	{
		this.m_hComposeAutoComplete.innerHTML = "";
		this.m_hComposeAutoComplete.style.height = "22px";
	}
	else
	{
		this.m_hComposeAutoComplete.innerHTML = "";
		var n = 0;
		for ( var i = 0; i < this.m_oAutoCompleteUsers[ sText ].length; ++i )
		{
			if ( sOriginal == this.GetComposeAutoCompleteString( this.m_oAutoCompleteUsers[ sText ][ i ].name.substr( 0, sOriginal.length ) ) )
			{
				var oDiv = document.createElement( "div" );
				this.m_hComposeAutoComplete.appendChild( oDiv );
				var sName = this.m_oAutoCompleteUsers[ sText ][ i ].name;
				if ( this.m_oAutoCompleteUsers[ sText ][ i ].hero.length > 0 )
				{
					sName += " (" + this.m_oAutoCompleteUsers[ sText ][ i ].hero + ")";
				}
				oDiv.innerHTML = sName;
				oDiv.id = "composeuser" + this.m_oAutoCompleteUsers[ sText ][ i ].id;
				if ( this.m_iComposeCursor == n )
				{
					oDiv.className = "chat-composeautocomplete-selected";
				}
				oDiv.onmousedown = function() { g_oChat.SelectAutoCompleteEntry( this ); };
				oDiv.onmouseover = function() { g_oChat.MouseOverAutoCompleteEntry( this ); };
				n += 1;
			}
		}
		n = ( n == 0 ) ? 1 : n;
		this.m_hComposeAutoComplete.style.height = String( ( n * 20 ) + 2 ) + "px";
	}
}

Chat.prototype.SelectAutoCompleteEntry = function( _oDiv )
{
	var sId = _oDiv.id.substr( 11 );
	this.m_idComposeUser = sId;
	this.m_hComposeAddress.value = this.GetUserName( sId );
	this.m_hComposeHeadline.focus();
}

Chat.prototype.MouseOverAutoCompleteEntry = function( _oDiv )
{
	if ( _oDiv.id.substr( 0, 11 ) != "composeuser" )
	{
		return;
	}
	if ( this.m_sLastMouseOver == _oDiv.id.substr( 11 ) )
	{
		return;
	}
	this.m_sLastMouseOver = _oDiv.id.substr( 11 );
	var aDivs = this.m_hComposeAutoComplete.childNodes;
	for ( var i = 0; i < aDivs.length; ++i )
	{
		if ( _oDiv == aDivs[ i ] )
		{
			if ( i != this.m_iComposeCursor )
			{
				aDivs[ this.m_iComposeCursor ].className = "";
				this.m_iComposeCursor = i;
				aDivs[ i ].className = "chat-composeautocomplete-selected";
			}
			return;
		}
	}
}

Chat.prototype.SelectMail = function( _sMailId )
{
	this.m_iMailListNextY = this.m_oScroller.getY();	
	this.m_oActChatRoom.SelectMail( this, _sMailId );
	if ( this.m_oActChatRoom.m_sId == "%%inbox" )
	{
		this.Command( "/readmail " + _sMailId, null );
	}
}

Chat.prototype.CloseMail = function()
{
	this.m_hBoxContent.style.display = "block";
	this.m_hMailContent.style.display = "none";
	this.m_oScroller.setPane( this.m_hBoxContentTextPane );
	this.m_oScroller.setNextY( this.m_iMailListNextY );
	this.m_oScroller.init( this.m_hBoxContentText.clientHeight, this.m_hBoxContentTextPane.clientHeight );
}

Chat.prototype.DeleteMail = function( _sMailId )
{
	this.m_oActChatRoom.DeleteMail( this, _sMailId );
	this.CloseMail();
	if ( this.m_oActChatRoom.m_sId == "%%inbox" )
	{
		this.Command( "/deletemail " + _sMailId, null );
	}
}

Chat.prototype.ReplyMail = function( _sMailId )
{
	var oMail = this.m_oActChatRoom.GetMail( _sMailId );
	var sUserId = oMail.userId;
	this.CloseMail();
	this.RoomActivate( "%%compose" );
	this.m_idComposeUser = sUserId;
	this.m_hComposeAddress.value = this.GetUserName( sUserId );
	this.m_hComposeHeadline.value = "Re: " + oMail.headline;
	
	var sText = oMail.text.replace( new RegExp( "\n", "g" ), "\n> " );
	this.m_hComposeText.value = "> " + sText;
}

Chat.prototype.Query = function( _sUserId, _sName, _sHero )
{
	if ( parseInt( this.m_idUser, 10 ) < parseInt( _sUserId, 10 ) )
	{
		this.m_sNextRoom = "%" + this.m_idUser + "%" + _sUserId + "%";
	}
	else
	{
		this.m_sNextRoom = "%" + _sUserId + "%" + this.m_idUser + "%";
	}
	for ( var s in this.m_oChatRooms )
	{
		if ( this.m_oChatRooms[ s ].m_sName == this.m_sNextRoom )
		{
			this.RoomActivate( s );
			return;
		}
	}
	this.Command( "/query " + _sUserId, function() { g_oChat.RoomActivateByName( g_oChat.m_sNextRoom ); } );
}

Chat.prototype.IsUserOnline = function( _sUserId )
{
	for ( var s in this.m_oChatRooms )
	{
		if ( this.m_oChatRooms[ s ].IsUserOnline( _sUserId ) )
		{
			return true;
		}
	}
	return false;
}

Chat.prototype.AddUsers = function( _oUsers )
{
	for ( var i in _oUsers )
	{
		if ( typeof( this.m_oUsers[ i ] ) == "undefined" )
		{
			this.m_oUsers[ i ] = _oUsers[ i ];
		}
	}
}

Chat.prototype.RoomActivateByName = function( _sName )
{
	for ( var s in this.m_oChatRooms )
	{
		if ( this.m_oChatRooms[ s ].m_sName == _sName )
		{
			this.RoomActivate( s );
		}
	}
}

Chat.prototype.RoomActivate = function( _sRoom )
{
	if ( typeof( this.m_oChatRooms[ _sRoom ] ) != "undefined" )
	{
		var oRoom = this.m_oChatRooms[ _sRoom ];
		if ( oRoom != this.m_oActChatRoom )
		{
			if ( this.m_oActChatRoom != null )
			{
				this.m_oActChatRoom.Deactivate();
			}
			this.m_oActChatRoom = oRoom;
			oRoom.Activate( this );
		}
	}
}

Chat.prototype.RoomClose = function( _sRoom )
{
	var iLength = 0;
	for ( var i in this.m_oChatRooms )
	{
		iLength += 1;
	}
	if ( ( typeof( this.m_oChatRooms[ _sRoom ] ) != "undefined" ) && ( iLength > 1 ) )
	{
		this.m_oOldActChatRoom = this.m_oActChatRoom;
		this.RoomActivate( _sRoom );
		this.Command( "/part", ( this.m_oOldActChatRoom.m_sId != _sRoom ) ? function() { g_oChat.RoomActivate( g_oChat.m_oOldActChatRoom.m_sId ); g_oChat.m_oOldActChatRoom = null; } : function() { g_oChat.m_oOldActChatRoom = null; } );
	}
}

Chat.prototype.AddRooms = function( _oRooms )
{
	var bChanged = false;
	var bResize = false;
	for ( var i in _oRooms )
	{
		var oRoom = null;
		var oDesc = _oRooms[ i ];
		if ( typeof( this.m_oChatRooms[ i ] ) == "undefined" )
		{
			oRoom = new ChatRoom( i, oDesc.name, oDesc.isPrivate, this );
			this.m_oChatRooms[ i ] = oRoom;
			this.m_hTabsPane.appendChild( oRoom.m_hTab );
			bChanged = true;
			bResize = true;
		}
		else
		{
			oRoom = this.m_oChatRooms[ i ];
		}
		oRoom.m_aUserIds = oDesc.userIds;
		if ( this.m_oActChatRoom == null )
		{
			this.m_oActChatRoom = oRoom;
			oRoom.Activate( this );
			bChanged = false;
		}
	}
	if ( bChanged && ( this.m_oActChatRoom != null ) )
	{
		this.m_oActChatRoom.Activate( this );
	}
	if ( bResize )
	{
		this.ResizeTabs();
	}
}

Chat.prototype.RemoveOtherRooms = function( _oRooms )
{
	var bChanged = false;
	var bResize = false;
	var aList = new Array();
	for ( var i in this.m_oChatRooms )
	{
		if ( ( typeof( _oRooms[ i ] ) == "undefined" ) && ( i.substring( 0, 2 ) != "%%" ) )
		{
			aList.push( i );
		}
	}
	for ( var i = 0; i < aList.length; ++i )
	{
		bResize = true;
		var oRoom = this.m_oChatRooms[ aList[ i ] ];
		this.m_oChatRooms[ aList[ i ] ].Remove();
		delete this.m_oChatRooms[ aList[ i ] ];
		if ( oRoom == this.m_oActChatRoom )
		{
			this.m_oActChatRoom = null;
			bChanged = true;
		}
	}
	if ( bChanged )
	{
		for ( var j in this.m_oChatRooms )
		{
			this.m_oActChatRoom = this.m_oChatRooms[ j ];
			this.m_oActChatRoom.Activate( this );
			break;
		}
	}
	if ( bResize )
	{
		this.ResizeTabs();
	}
}

Chat.prototype.ResizeTabs = function()
{
	var a = this.m_hTabsPane.childNodes;
	this.m_hListContentJoining.style.display = ( a.length >= 5 ) ? "none" : "block";
	if ( a.length == 0 )
	{
		this.m_hTabsPane.style.left = "0px";
		this.m_hTabsLeft.style.display = "none";
		this.m_hTabsRight.style.display = "none";
		return;
	}
	for ( var i = 0; i < a.length; ++i )
	{
		a[ i ].style.width = "auto";
	}
	var w = 0;
	for ( var i = 0; i < a.length; ++i )
	{
		w += a[ i ].clientWidth;
	}
	if ( w < this.m_hTabs.clientWidth )
	{
		this.m_hTabsPane.style.left = "0px";
		this.m_hTabsLeft.style.display = "none";
		this.m_hTabsRight.style.display = "none";
		w = this.m_hTabs.clientWidth - w;
		for ( var i = 0; i < a.length; ++i )
		{
			var ww = ( w / ( a.length - i ) ) | 0;
			a[ i ].style.width = String( a[ i ].clientWidth + ww - 32 ) + "px";
			w -= ww;
		}
		return;
	}
	var x = -parseInt( this.m_hTabsPane.style.left, 10 );
	this.m_hTabsLeft.style.display = ( x == 0 ) ? "none" : "block";
	if ( x + this.m_hTabs.clientWidth >= w )
	{
		x = w - this.m_hTabs.clientWidth;
		this.m_hTabsPane.style.left = String( -x ) + "px";
		this.m_hTabsRight.style.display = "none";
	}
	else
	{
		this.m_hTabsRight.style.display = "block";
	}
}

Chat.prototype.OnTabsLeft = function()
{
	var a = this.m_hTabsPane.childNodes;
	var w = 0;
	for ( var i = 0; i < a.length; ++i )
	{
		w += a[ i ].clientWidth;
	}
	var x = -parseInt( this.m_hTabsPane.style.left, 10 );
	var xx = 0;
	for ( var i = 0; i < a.length; ++i )
	{
		if ( xx + a[ i ].clientWidth >= x )
		{
			break;
		}
		xx += a[ i ].clientWidth;
	}
	this.m_hTabsLeft.style.display = ( xx == 0 ) ? "none" : "block";
	this.m_hTabsRight.style.display = "block";
	this.m_hTabsPane.style.left = String( -xx ) + "px";
}
Chat.prototype.OnTabsRight = function()
{
	var a = this.m_hTabsPane.childNodes;
	var w = 0;
	for ( var i = 0; i < a.length; ++i )
	{
		w += a[ i ].clientWidth;
	}
	var x = -parseInt( this.m_hTabsPane.style.left, 10 );
	var xx = 0;
	for ( var i = 0; i < a.length; ++i )
	{
		xx += a[ i ].clientWidth;
		if ( xx > x )
		{
			break;
		}
	}
	if ( xx + this.m_hTabs.clientWidth >= w )
	{
		xx = w - this.m_hTabs.clientWidth;
		this.m_hTabsRight.style.display = "none";
	}
	this.m_hTabsPane.style.left = String( -xx ) + "px";
	this.m_hTabsLeft.style.display = "block";
}

Chat.prototype.AddChats = function( _aChats )
{
	_aChats.sort( function( _a, _b ) { return _a.id - _b.id; } );
	for ( var i in this.m_oChatRooms )
	{
		this.m_oChatRooms[ i ].AddMessages( _aChats );
	}
	if ( this.m_iLastChatId < _aChats[ _aChats.length - 1 ].id )
	{
		this.m_iLastChatId = _aChats[ _aChats.length - 1 ].id;
	}
}

Chat.prototype.AddMails = function( _sBox, _sName, _aMails )
{
	for ( var i in _aMails )
	{
		var j = parseInt( i );
		if ( j > this.m_iLastMailId )
		{
			this.m_iLastMailId = j;
		}
	}
	if ( typeof( this.m_oChatRooms[ _sBox ] ) == "undefined" )
	{
		var oRoom = new MailList( _sBox, _sName, this, _aMails );
		this.m_oChatRooms[ _sBox ] = oRoom;
		this.m_hTabs2.appendChild( oRoom.m_hTab );
		if ( this.m_oActChatRoom != null )
		{
			this.m_oActChatRoom.Activate( this );
		}
	}
	else
	{
		this.m_oChatRooms[ _sBox ].AddMails( _aMails );
	}
}

Chat.prototype.GetUserName = function( _sId )
{
	return ( this.m_oUsers[ _sId ] == null ) ? "" : this.m_oUsers[ _sId ].name;
}

Chat.prototype.GetUserHeroName = function( _sId )
{
	return ( this.m_oUsers[ _sId ] == null ) ? "" : this.m_oUsers[ _sId ].hero;
}

Chat.prototype.Command = function( _sCmd, _fnCallback )
{
	try
	{
		if ( this.m_hTimeout != null )
		{
			window.clearTimeout( this.m_hTimeout );
			this.m_hTimeout = null;
		}
		if ( this.m_oHttpRequest != null )
		{
			this.m_afnNextCallbacks.push( new Array( _sCmd, _fnCallback ) );
			return;
		}
		this.m_afnActCallbacks.push( new Array( _sCmd, _fnCallback ) );
		this.DoRequest();
	}
	catch ( exc )
	{
		if ( !exc.message )
		{
			exc.message = exc;
		}
		if ( g_oChatExceptions && ( this.m_hConsole != null ) )
		{
			this.m_hConsole.appendChild( document.createTextNode( "exception" ) );
			this.m_hConsole.appendChild( document.createElement( "br" ) );
			this.m_hConsole.appendChild( document.createTextNode( exc.fileName + "(" + exc.lineNumber + "): " + exc.message ) );
			this.m_hConsole.appendChild( document.createElement( "br" ) );
			this.m_hConsole.appendChild( document.createTextNode( exc.stack ) );
			this.m_hConsole.appendChild( document.createElement( "br" ) );
		}
	}
}

Chat.prototype.DoRequest = function()
{
	var xmlHttp = null;
	if ( typeof XMLHttpRequest != 'undefined' )
	{
	    xmlHttp = new XMLHttpRequest();
	}
	else
	{
	    try 
	    {
	        xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP" );
	    }
	    catch ( e )
	    {
	        try
	        {
	            xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );
	        }
	        catch ( e )
	        {
	            xmlHttp = null;
	        }
	    }
	}
	if ( xmlHttp )
	{
		this.m_oHttpRequest = xmlHttp;
		var sReq = "command=" + encodeURIComponent( this.m_afnActCallbacks[ this.m_afnActCallbacks.length - 1 ][ 0 ] );
		sReq += "&lastchatid=" + this.m_iLastChatId;
		sReq += "&lastmailid=" + this.m_iLastMailId;
		sReq += "&userid=" + this.m_idUser;
		sReq += "&rnd=" + String( ( new Date() ).getTime() );
		if ( this.m_oActChatRoom != null )
		{
			sReq += "&roomid=" + this.m_oActChatRoom.m_sId;
		}
		var aRooms = new Array();
		for ( var i in this.m_oChatRooms )
		{
			if ( i.substr( 0, 1 ) != "%" )
			{
				aRooms.push( i );
			}
		}
		if ( aRooms.length > 0 )
		{
			sReq += "&roomids=" + encodeURIComponent( aRooms.join( ',' ) );
		}
		sReq += "&session=" + encodeURIComponent( g_sSession );
		xmlHttp.onreadystatechange = function() { g_oChat.EndCommand(); };
	    xmlHttp.open( 'POST', "/chat/chat.php", true );
		xmlHttp.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
	    xmlHttp.send( sReq );
	}
}

Chat.prototype.EndCommand = function()
{
	if ( this.m_oHttpRequest.readyState != 4 )
	{
		return;
	}
	try
	{
		var aResponse = this.ParseResponse( this.m_oHttpRequest.responseText );
		while ( this.m_afnActCallbacks.length > 0 )
		{
			var fn = this.m_afnActCallbacks.shift();
			if ( ( fn != null ) && ( fn.length == 2 ) && ( fn[ 1 ] != null ) )
			{
				fn[ 1 ]( aResponse );
			}
		}
	}
	catch ( exc )
	{
		if ( !exc.message )
		{
			exc.message = exc;
		}
		if ( g_oChatExceptions && ( this.m_hConsole != null ) )
		{
			this.m_hConsole.appendChild( document.createTextNode( "exception" ) );
			this.m_hConsole.appendChild( document.createElement( "br" ) );
			this.m_hConsole.appendChild( document.createTextNode( exc.fileName + "(" + exc.lineNumber + "): " + exc.message ) );
			this.m_hConsole.appendChild( document.createElement( "br" ) );
			this.m_hConsole.appendChild( document.createTextNode( exc.stack ) );
			this.m_hConsole.appendChild( document.createElement( "br" ) );
		}
	}
	this.m_oHttpRequest = null;
	if ( this.m_afnNextCallbacks.length > 0 )
	{
		while ( ( this.m_afnNextCallbacks.length > 0 ) && ( this.m_afnNextCallbacks[ 0 ][ 0 ] == "/ping" ) )
		{
			this.m_afnActCallbacks.push( this.m_afnNextCallbacks.shift() );
		}
		if ( this.m_afnNextCallbacks.length > 0 )
		{
			this.m_afnActCallbacks.push( this.m_afnNextCallbacks.shift() );
		}
		this.DoRequest();
	}
	else
	{
		this.m_hTimeout = window.setTimeout( function() { g_oChat.m_hTimeout = null; g_oChat.Command( "/ping", null ); }, this.m_iLastTimeout | 0 );
	}
}

Chat.prototype.ParseResponseSetMember = function( _oMember, _sName, _sValue )
{
	if ( _sName.charAt( 0 ) == '*' )
	{
		_sValue = _sValue.split( _sName.charAt( 1 ) );
		_sName = _sName.substr( 2 );
		if ( _sName.charAt( 0 ) == '%' )
		{
			_sName = _sName.substr( 1 );
			for ( var i = 0; i < _sValue.length; ++i )
			{
				_sValue[ i ] = parseInt( _sValue[ i ], 10 );
			}
		}
	}
	else if ( _sName.charAt( 0 ) == '%' )
	{
		_sName = _sName.substr( 1 );
		_sValue = parseInt( _sValue, 10 );
	}
	else if ( _sName.charAt( 0 ) == '!' )
	{
		_sName = _sName.substr( 1 );
		_sValue = _sValue == "1";
	}
	_oMember[ _sName ] = _sValue;
}

function DecodeString( _sString )
{
	var aRes = new Array();
	for ( var iPos = _sString.indexOf( "\\" ); iPos >= 0; iPos = _sString.indexOf( "\\" ) )
	{
		aRes.push( _sString.substr( 0, iPos ) );
		_sString = _sString.substr( iPos + 1 );
		if ( _sString.length > 0 )
		{
			var c = _sString.substr( 0, 1 );
			if ( c == "\\" )
			{
				aRes.push( c );
			}
			else if ( c == "n" )
			{
				aRes.push( "\n" );
			}
			else if ( c == "p" )
			{
				aRes.push( "+" );
			}
			else if ( c == "m" )
			{
				aRes.push( "-" );
			}
			else if ( c == "c" )
			{
				aRes.push( "," )
			}
			_sString = _sString.substr( 1 );
		}
	}
	aRes.push( _sString );
	return aRes.join( "" );
}

Chat.prototype.ParseResponse = function( _sText )
{
	var aRes = new Object();
	for ( var i in g_oaChatProtocol )
	{
		aRes[ i ] = null;
	}
	var aChunks = _sText.split( "\n--" );
	for ( var i = 1; i < aChunks.length; ++i )
	{
		var sChunk = aChunks[ i ];
		var sPos = sChunk.indexOf( ":" );
		var sName = sChunk.substring( 0, sPos );
		sChunk = sChunk.substring( sPos + 2, sChunk.length );
		var aDesc = g_oaChatProtocol[ sName ];
		var aList = ( aDesc[ 0 ] == "->" ) ? new Object() : new Array();
		if ( sChunk.length > 0 )
		{
			var aElements = sChunk.split( "\n" );
			if ( aDesc[ 0 ] == "->" )
			{
				for ( var j = 0; j < aElements.length; ++j )
				{
					var aMembers = aElements[ j ].substr( 1 ).split( "+", aDesc.length );
					var oMembers = new Object();
					for ( var k = 1; k < aMembers.length; ++k )
					{
						this.ParseResponseSetMember( oMembers, aDesc[ k ], DecodeString( aMembers[ k ] ) );
					}
					aList[ aMembers[ 0 ] ] = oMembers;
				}
			}
			else
			{
				for ( var j = 0; j < aElements.length; ++j )
				{
					var aMembers = aElements[ j ].substr( 1 ).split( "+", aDesc.length );
					var oMembers = new Object();
					for ( var k = 0; k < aMembers.length; ++k )
					{
						this.ParseResponseSetMember( oMembers, aDesc[ k ], DecodeString( aMembers[ k ] ) );
					}
					aList.push( oMembers );
				}
			}
		}
		aRes[ sName ] = aList;
	}
	var iChanges = 0;
	if ( aRes.options != null )
	{
		this.m_hListSettingsAcceptContact.checked = aRes.options.AcceptContacts.setting != "0";
		var oTexts = g_aoChatTexts[ parseInt( aRes.options.Language.setting, 10 ) ];
		if ( g_aoChatTexts[ 0 ] != oTexts )
		{
			g_aoChatTexts[ 0 ] = oTexts;
			this.m_oChatRooms[ "%%list" ].m_sName = g_aoChatTexts[ 0 ].chatListTitle;
			this.m_oChatRooms[ "%%compose" ].m_sName = g_aoChatTexts[ 0 ].mailComposeTitle;
			this.m_oChatRooms[ "%%list" ].m_hTab.firstChild.data = this.m_oChatRooms[ "%%list" ].GetDisplayName( this );
			this.m_oChatRooms[ "%%compose" ].m_hTab.firstChild.data = this.m_oChatRooms[ "%%compose" ].GetDisplayName( this );
			if ( typeof( this.m_oChatRooms[ "%%inbox" ] ) != "undefined" )
			{
				this.m_oChatRooms[ "%%inbox" ].m_sName = g_aoChatTexts[ 0 ].inboxTitle;
				this.m_oChatRooms[ "%%inbox" ].m_hTab.firstChild.data = this.m_oChatRooms[ "%%inbox" ].GetDisplayName( this );
			}
			if ( typeof( this.m_oChatRooms[ "%%outbox" ] ) != "undefined" )
			{
				this.m_oChatRooms[ "%%outbox" ].m_sName = g_aoChatTexts[ 0 ].outboxTitle;
				this.m_oChatRooms[ "%%outbox" ].m_hTab.firstChild.data = this.m_oChatRooms[ "%%outbox" ].GetDisplayName( this );
			}
			if ( this.m_oActChatRoom != null )
			{
				this.m_oActChatRoom.Deactivate();
				this.m_oActChatRoom.Activate( this );
			}
		}
		g_oChatExceptions = aRes.options.Exceptions.setting != "0";
	}
	if ( aRes.session != null )
	{
		g_sSession = aRes.session[ 0 ].session;
	}
	if ( aRes.users != null )
	{
		this.AddUsers( aRes.users );
	}
	if ( aRes.rooms != null )
	{
		this.AddRooms( aRes.rooms );
		this.RemoveOtherRooms( aRes.rooms );
	}
	else
	{
		this.RemoveOtherRooms( new Object() );
	}
	if ( aRes.chat != null )
	{
		this.AddChats( aRes.chat );
		iChanges += aRes.chat.length;
	}
	if ( aRes.allrooms != null )
	{
		this.m_oChatRooms[ "%%list" ].SetRooms( this, aRes.allrooms, this.m_oActChatRoom == this.m_oChatRooms[ "%%list" ] );
	}
	if ( aRes.input != null )
	{
		this.AddMails( "%%inbox", g_aoChatTexts[ 0 ].inboxTitle, aRes.input );
	}
	if ( aRes.output != null )
	{
		this.AddMails( "%%outbox", g_aoChatTexts[ 0 ].outboxTitle, aRes.output );
	}
	if ( ( aRes.search != null ) && ( aRes.searchuser != null ) )
	{
		this.m_oAutoCompleteUsers[ aRes.search[ 0 ].text ] = aRes.searchuser;
		this.m_iComposeCursor = 0;
		for ( var i = 0; i < aRes.searchuser.length; ++i )
		{
			if ( typeof( this.m_oUsers[ aRes.searchuser[ i ].id ] ) == "undefined" )
			{
				this.m_oUsers[ aRes.searchuser[ i ].id ] = aRes.searchuser[ i ];
			}
		}
	}
	if ( aRes.error != null )
	{
		for ( var i = 0; i < aRes.error.length; ++i )
		{
			var sTopic = aRes.error[ i ].topic;
			var sMessage = aRes.error[ i ].message;
			if ( sTopic == "query" )
			{
				this.RoomActivate( "%%compose" );
				if ( typeof( this.m_oUsers[ sMessage ] ) != "undefined" )
				{
					this.m_idComposeUser = sMessage;
					this.m_hComposeAddress.value = this.GetUserName( sMessage );
				}
				else
				{
					this.m_idComposeUser = "-1";
					this.m_hComposeAddress.value = sMessage;
				}
				this.AddError( g_aoChatTexts[ 0 ].errorQuery.replace( "%s", this.m_hComposeAddress.value ) );
			}
		}
	}
	if ( this.m_oActChatRoom != null )
	{
		this.m_oActChatRoom.Redraw( this );
	}
	this.OnCheckFixtures();
	if ( iChanges == 0 )
	{
		this.m_iLastTimeout *= 1.2;
		if ( this.m_iLastTimeout > 2000 )
		{
			this.m_iLastTimeout = 2000;
		}
	}
	else
	{
		this.m_iLastTimeout *= 0.8;
		if ( this.m_iLastTimeout < 150 )
		{
			this.m_iLastTimeout = 150;
		}
	}
}

