Event.observe(window, 'load', StartClient);

var show_user_list = 1;
var show_conversation = new Array();
var showTimeStamps = new Array();
var g_message = 0;
var g_typing = 0;
var conversations_list = new Array();
var conversations_list_me = new Array();
var conversations_list_typing = new Array();
var conversations_list_typing_tome = new Array();
var new_conversation = new Array();
var previous_text_length = new Array();
var blank_text_box = new Array();
var checkIfReceived = new Array();
var change_title_bar = new Array();
var checkAutoScroll = new Array();
var closed_chat_box = new Array();
var conversations_list_last_was_me = new Array();
var checkEmptyText = new Array();
var changeTitleBar = 0;
var just_started = 1;
var main_title = '';
var current_change_title = 0;

var time_stamp_count = 1;

var screenW = 0
var screenH = 0;
var max_conversation_height = 0;

var how_many_open = 0;
var user_browser = '';
var main_url = '';

// keeps track of the last request for new messages
var g_lastTime = 0;
// tracks whether there already is a request going on
var g_onCall_typing = false;
var g_onCall_message = false;

var month_titles = 'filler,Jan,Feb,Mar,Apr,Jun,Jul,Aug,Sep,Oct,Nov,Dec';
month_titles = month_titles.split(',');

function StartClient()
{
	if ( $F('users_id') )
	{
		total_chats_open = document.getElementById('how_many_chats').value;
		user_browser = document.getElementById('user_browser').value;
		main_url = document.getElementById('main_url').value;
		main_title = document.title;
		change_title_bar[0] = main_title;

		// load the chats
		var i = 1;
		while ( document.getElementById('chat_users_id_' + i) )
		{
			clearTimeout(showTimeStamps[i]);
			var user_id = document.getElementById('chat_users_id_' + i).value;
			var chat_toggle = document.getElementById('chat_toggle_' + i).value;
			var chat_history = document.getElementById('chat_history_' + i).value;

			//clean it up
			chat_history = chat_history.replace(/\[b\]/g, '<b>');
			chat_history = chat_history.replace(/\[endb\]/g, '</b>');
			chat_history = chat_history.replace(/\[br\]/g, '<br>');
			chat_history = chat_history.replace(/\[endspan\]/g, '</span>');
			chat_history = chat_history.replace(/\[span1\]/g, '<span style="word-wrap: break-word; color: #000000; font-family: Tahoma, Geneva, sans-serif; font-size: 12px;">');
			chat_history = chat_history.replace(/\[span2\]/g, '<span style="padding-left: 10px; text-indent: -10px; color: #000000; font-family: Tahoma, Geneva, sans-serif; font-size: 12px;">');
			chat_history = chat_history.replace(/\[span3\]/g, '<span style="color: #777777; font-family: Tahoma, Geneva, sans-serif; font-size: 12px;">');
			window.frames['chat_messages_' + user_id].document.body.innerHTML = chat_history;

			checkAutoScroll[user_id] = setTimeout("autoScroll(" + user_id + ")", 5);

			show_conversation[i] = 0;
			conversations_list[i] = user_id;
			conversations_list_typing[i] = 0;
			show_conversation[i] = 0;
			conversations_list_me[i] = 0;
			previous_text_length[i] = 0;

			Element.show('conversation_' + user_id);
			//document.write(user_id + '<br>' + chat_toggle)
			if ( chat_toggle == 2 )
			{
				toggleConversation(user_id);
			}
			else
			{
				Element.hide('conversation_title_' + user_id);
				Element.hide('conversation_text_' + user_id);
				Element.show('conversation_title_closed_' + user_id);
			}

			i++;
		}

		just_started = 0;

		new Ajax.PeriodicalUpdater('usernameContainer', main_url + 'get_users.php', {
			method: 'post',
			parameters: { users_id: $F('users_id'), main_url: main_url },
			frequency: 0.5
		});
		g_message = setInterval(AjaxDisplayMessages, 500);
		g_typing = setInterval(AjaxCheckTyping, 500);
	}

	screenW = 640
	screenH = 480;

	if ( parseInt(navigator.appVersion) > 3 )
	{
		screenW = screen.width;
		screenH = screen.height;
	}
	else if ( navigator.appName == "Netscape" && parseInt(navigator.appVersion)==3 && navigator.javaEnabled() )
	{
		var jToolkit = java.awt.Toolkit.getDefaultToolkit();
		var jScreenSize = jToolkit.getScreenSize();
		screenW = jScreenSize.width;
		screenH = jScreenSize.height;
	}

	max_conversation_height = Math.round(screenH / 40);
}

function autoScroll(receiver_id)
{
	document.getElementById('chat_messages_' + receiver_id).contentWindow.scrollTo(0,document.getElementById('chat_messages_' + receiver_id).contentWindow.document.body.scrollHeight);
}

function AjaxDisplayMessages()
{
	if ( !g_onCall_message )
	{
		g_onCall_message = true;
		new Ajax.Request(main_url + 'get_messages.php', {
			method: 'post',
			parameters: {
				users_id: $F('users_id'),
				user_browser: user_browser,
				lasttime: g_lastTime,
				main_url: main_url
			},
			onSuccess: function (xhrResponse) {
				if ( xhrResponse.responseText != '' )
				{
					var message_response = xhrResponse.responseText;
					var message_response_position = message_response.indexOf('-eboltajaxmedia-');
					var message_response_plus = (message_response_position + 16); // 16 because -eboltajaxmedia- is 16 chars

					var sender_id = message_response.substring(0, message_response_position);
					var message_text = message_response.substring(message_response_plus, message_response.length);

					var i = 1;
					var old_chat_box = 0;
					while ( conversations_list[i] )
					{
						if ( conversations_list[i] == sender_id )
						{
							old_chat_box = i;
						}

						i++;
					}

					clearTimeout(showTimeStamps[old_chat_box]);
					showTimeStamps[old_chat_box] = setTimeout("showTimeStamp(" + sender_id + ")", 50000);

					if ( old_chat_box == 0 )
					{
						openChat(sender_id, 0, message_text);
					}
					else
					{
						var chat_id = find_chat_id(sender_id);
						var chat_conversation_id = conversations_list[chat_id];

						userTyping(chat_conversation_id, 0);
						conversations_list_typing[chat_id] = 0;
						conversations_list_last_was_me[chat_id] = 0;
						Element.hide('typing_box_' + chat_conversation_id);

						if ( chat_conversation_id != $F('users_id') )
						{
							$('conversation_box_' + chat_conversation_id).style.backgroundColor = '#f99d39';
							$('conversation_box_closed_' + chat_conversation_id).style.backgroundColor = '#f99d39';
							highlightUser(chat_conversation_id, 2);

							change_title_bar[chat_id] = 2;
							changeTitle(chat_conversation_id);

							highlightButton(chat_conversation_id, 1, 'minimize', 1);
							highlightButton(chat_conversation_id, 1, 'close', 1);
						}
						else
						{
							conversations_list_me[chat_id] = 0;
						}

						var conversation_div = window.frames['chat_messages_' + chat_conversation_id].document.body;
						var new_div = document.createElement('div');
						new_div.innerHTML = message_text;
						conversation_div.appendChild(new_div);

						document.getElementById('chat_messages_' + chat_conversation_id).contentWindow.scrollTo(0,document.getElementById('chat_messages_' + chat_conversation_id).contentWindow.document.body.scrollHeight);
					}

					var d = new Date();
					g_lastTime = d.getTime();
				}

				g_onCall_message = false;
			},
			onFailure: function() {
				new Insertion.Bottom('chat_messages_' + conversations_list, '<p class="errorMessage">Could not retrieve messages.</p>');
				g_onCall_message = false;
			}
		});
	}
}

function changeTitle(receiver_id)
{
	clearTimeout(changeTitleBar);
	current_change_title = receiver_id;
	var find_new = 0;

	if ( receiver_id == 0 )
	{
		var chat_id = 0;
		document.title = main_title;
	}
	else
	{
		var chat_id = find_chat_id(receiver_id);

		if ( change_title_bar[chat_id] == 2 )
		{
			var users_name = document.getElementById('conversation_user_name_' + receiver_id).value;
			var users_first_name = users_name.split(' ');
			document.title = users_first_name[0] + ' says...';
		}
		else
		{
			find_new = 1;
		}
	}

	var total_new = 0;
	var show_next = 0;
	var i = 1;
	while ( change_title_bar[i] )
	{
		if ( change_title_bar[i] == 2 )
		{
			if ( chat_id <= i && show_next == 0 && conversations_list[i] != receiver_id )
			{
				show_next = i;
			}

			total_new++;
		}

		i++;
	}

	if ( total_new > 0 && find_new == 0 )
	{
		changeTitleBar = setTimeout("changeTitle(" + ( show_next == 0 ? 0 : conversations_list[show_next] ) + ")", 3000);
	}
	else if ( find_new == 1 )
	{
		changeTitle(show_next);
	}
}

function showTimeStamp(conversation_id)
{
	var chat_id = find_chat_id(conversation_id);
	clearTimeout(showTimeStamps[chat_id]);
	
	conversations_list_last_was_me[chat_id] = 0;

	var d = new Date();
	var new_time = (d.getTime() - 50000);
	d.setTime(+new_time);

	var show_hour = ( d.getHours() >= 12 ? ( d.getHours() - 12 ) : d.getHours() );
	show_hour = ( show_hour == 0 ? 12 : show_hour );
	var show_minute = d.getMinutes();
	show_minute = ( show_minute.length == 1 ? '0' + show_minute : show_minute );
	var show_am_pm = ( d.getHours() >= 12 ? 'PM' : 'AM' );
	var show_month = d.getMonth();
	var show_day = d.getDate();
	var show_suffix = ( show_day == 1 || show_day == 11 || show_day == 21 || show_day == 31 ? 'st' : '' );
	show_suffix = ( show_day == 2 || show_day == 22 ? 'nd' : show_suffix );
	show_suffix = ( show_day == 3 || show_day == 23 ? 'rd' : show_suffix );
	show_suffix = ( show_suffix ? show_suffix : 'th' );

	var conversation_div = window.frames['chat_messages_' + conversation_id].document.body;
	var new_div = document.createElement('div');
	new_div.innerHTML = '<span style="color: #777777; font-family: Tahoma, Geneva, sans-serif; font-size: 12px;">Sent at ' + show_hour + ':' + show_minute + show_am_pm + ' ' + month_titles[show_month] + ' ' + show_day + show_suffix + '</span><br />';
	conversation_div.appendChild(new_div);
	document.getElementById('chat_messages_' + conversation_id).contentWindow.scrollTo(0,document.getElementById('chat_messages_' + conversation_id).contentWindow.document.body.scrollHeight);

	new Ajax.Request(main_url + 'user_time_stamp.php', {
		method: 'post',
		parameters: {
			users_id: $F('users_id')
		},
		onSuccess: function(xhrResponse) {

		},
		onFailure: function() {

		}
	});
}

function AjaxCheckTyping()
{
	if ( !g_onCall_typing )
	{
		g_onCall_typing = true;
		new Ajax.Request(main_url + 'get_typing.php', {
			method: 'post',
			parameters: {
				users_id: $F('users_id'),
				lasttime: g_lastTime
			},
			onSuccess: function (xhrResponse) {
				if ( xhrResponse.responseText != '' )
				{
					var typing_response = xhrResponse.responseText;
					var typing_response_split = typing_response.split(',');
	
					var j = 1;
					while ( conversations_list[j] )
					{
						var found_one = 0;
						var i = 0;
						while ( typing_response_split[i] )
						{
							if ( typing_response_split[i] == conversations_list[j] )
							{
								Element.show('typing_box_' + conversations_list[j]);
								found_one = j;
							}
	
							i++;
						}
	
						if ( found_one == 0 )
						{
							Element.hide('typing_box_' + conversations_list[j]);
						}
	
						j++;
					}
	
					var d = new Date();
					g_lastTime = d.getTime();
				}
				else
				{
					var j = 1;
					while ( conversations_list[j] )
					{
						Element.hide('typing_box_' + conversations_list[j]);
	
						j++;
					}
				}
	
				g_onCall_typing = false;
			},
			onFailure: function() {
				new Insertion.Bottom('chat_messages_' + conversations_list, '<p class="errorMessage">Could not retrieve messages.</p>');
				g_onCall_typing = false;
			}
		});
	}
}

function emptyText(receiver_id)
{
	$('chat_text_' + receiver_id).value = '';
	userTyping(receiver_id, 0);
}

function SendMessage(receiver_id)
{
	var chat_id = find_chat_id(receiver_id);
	conversations_list_me[chat_id] = 1;
	previous_text_length[chat_id] = 0;

	var message_text = '';
	if ( conversations_list_last_was_me[chat_id] == 1 )
	{
		message_text = '<span style="word-wrap: break-word; color: #000000; font-family: Tahoma, Geneva, sans-serif; font-size: 12px;"><span style="text-indent: -10px; padding-left: 10px;">' + $F('chat_text_' + receiver_id) + '</span></span><br />';
	}
	else
	{
		message_text = '<span style="word-wrap: break-word; color: #000000; font-family: Tahoma, Geneva, sans-serif; font-size: 12px;"><b>me:</b><span style="text-indent: -10px; padding-left: 10px;">' + $F('chat_text_' + receiver_id) + '</span></span><br />';
		conversations_list_last_was_me[chat_id] = 1;
	}

	clearTimeout(showTimeStamps[chat_id]);
	showTimeStamps[chat_id] = setTimeout("showTimeStamp(" + receiver_id + ")", 50000);
	
	var send_message = $F('chat_text_' + receiver_id);
	$('chat_text_' + receiver_id).value = '';
	var conversation_div = window.frames['chat_messages_' + receiver_id].document.body;
	var new_div = document.createElement('div');
	new_div.innerHTML = message_text;
	conversation_div.appendChild(new_div);

	checkEmptyText[chat_id] = setTimeout("emptyText(" + receiver_id + ")", 5);

	document.getElementById('chat_messages_' + receiver_id).contentWindow.scrollTo(0,document.getElementById('chat_messages_' + receiver_id).contentWindow.document.body.scrollHeight);

	var d = new Date();
	new Ajax.Request(main_url + 'put_messages.php', {
		method: 'post',
		parameters: {
			users_id: $F('users_id'),
			message: send_message,
			receiver_id: receiver_id,
			lasttime: d.getTime()
		},
		onSuccess: function(xhrResponse) {
			if ( xhrResponse.responseText == '0' )
			{
				// display error
			}
		},
		onFailure: function() {
			// display error
		}
	});
}

function QuitChat(e)
{
	Event.stop(e);
	new Ajax.Request(main_url + 'logout.php', {
		method: 'post',
		parameter: { users_id: $F('users_id') },
		onSuccess: function() {
			window.location = 'index.php';
		},
		onFailure: function() {
			window.location = 'index.php';
		}
	});
}

function loginUser(user_id)
{
	new Ajax.Request(main_url + 'login.php', {
		method: 'post',
		parameters: { user_id: user_id },
		onSuccess: function(xhrResponse) {
			parent.location = 'account_chat.php';
		},
		onFailure: function (xhrResponse) {
			
		}
	});
}

function toggleUserList()
{
	if ( show_user_list == 1 )
	{
		Element.hide('user_list');
		show_user_list = 0;
	}
	else
	{
		Element.show('user_list');
		show_user_list = 1;
	}
}

function find_chat_id(chat_id)
{
	var i = 1;
	var find_chat_id = 0;
	while ( conversations_list[i] )
	{
		if ( conversations_list[i] == chat_id )
		{
			find_chat_id = i;
		}

		i++;
	}

	return find_chat_id;
}

function toggleConversation(conversation_id)
{
	var chat_id = find_chat_id(conversation_id);
	if ( show_conversation[chat_id] == 1 )
	{
		Element.hide('conversation_title_' + conversation_id);
		Element.hide('conversation_text_' + conversation_id);
		Element.show('conversation_title_closed_' + conversation_id);
		show_conversation[chat_id] = 0;
	}
	else
	{
		Element.show('conversation_title_' + conversation_id);
		Element.show('conversation_text_' + conversation_id);
		Element.hide('conversation_title_closed_' + conversation_id);
		show_conversation[chat_id] = 1;
		document.getElementById('chat_messages_' + conversation_id).contentWindow.scrollTo(0,document.getElementById('chat_messages_' + conversation_id).contentWindow.document.body.scrollHeight);
		
		clearTimeout(checkAutoScroll[conversation_id]);
		checkAutoScroll[conversation_id] = setTimeout("autoScroll(" + conversation_id + ")", 5);
	}

	$('conversation_box_' + conversation_id).style.backgroundColor = '#3D9DFE';
	$('conversation_box_closed_' + conversation_id).style.backgroundColor = '#3D9DFE';

	change_title_bar[chat_id] = 1;

	highlightButton(conversation_id, 0, 'minimize');
	highlightButton(conversation_id, 0, 'close');

	if ( current_change_title == conversation_id )
	{
		var show_next = 0;
		var i = (conversation_id + 1);
		while ( change_title_bar[i] )
		{
			if ( change_title_bar[i] == 2 && show_next == 0 )
			{
				show_next = i;
			}

			i++;
		}

		changeTitle(show_next);
	}
	
	new Ajax.Request(main_url + 'toggle_conversation.php', {
		method: 'post',
		parameters: {
			users_id: $F('users_id'),
			conversation_id: conversation_id,
			just_started: just_started,
			toggle_conversation: show_conversation[chat_id]
		}
	});
}

function focusTextArea(receiver_id)
{
	var chat_id = find_chat_id(receiver_id);
	change_title_bar[chat_id] = 1;

	if ( current_change_title == receiver_id )
	{
		var show_next = 0;
		var i = (receiver_id + 1);
		while ( change_title_bar[i] )
		{
			if ( change_title_bar[i] == 2 && show_next == 0 )
			{
				show_next = i;
			}

			i++;
		}

		changeTitle(show_next);
	}

	$('conversation_box_' + receiver_id).style.backgroundColor = '#3D9DFE';
	$('conversation_box_closed_' + receiver_id).style.backgroundColor = '#3D9DFE';

	highlightButton(receiver_id, 0, 'minimize');
	highlightButton(receiver_id, 0, 'close');
}

function openChat(receiver_id, new_chat, message)
{
	var i = 1;
	var old_chat_box = 0;
	while ( conversations_list[i] )
	{
		if ( conversations_list[i] == receiver_id )
		{
			old_chat_box = i;
		}

		i++;
	}

	if ( old_chat_box )
	{
		var chat_id = find_chat_id(receiver_id);
		if ( closed_chat_box[chat_id] == 1 ) // lets put it at the end
		{
			var open_conversations_div = document.getElementById('openConversations');
			var move_div = document.getElementById('conversation_' + receiver_id);
			open_conversations_div.appendChild(move_div);

			Element.show('conversation_' + receiver_id);
			closed_chat_box[chat_id] = 0;
		}
		toggleConversation(receiver_id);
	}
	else
	{
		how_many_open++;
		clearTimeout(showTimeStamps[how_many_open]);
		clearTimeout(checkIfReceived[how_many_open]);

		var users_name = document.getElementById('conversation_user_name_' + receiver_id).value;
		var users_first_name = users_name.split(' ');

		var open_conversations_div = document.getElementById('openConversations');
		var html_message = '<div id="conversation_' + receiver_id + '" style="display: none; float: right; right: 0px; padding: 0px 5px 0px 0px;"><div id="conversation_title_' + receiver_id + '"><table border="0" cellpadding="0" cellspacing="0" width="225"><tr><td width="100%" class="conversationBox" id="conversation_box_' + receiver_id + '" style="cursor: pointer" onclick="toggleConversation(' + receiver_id + ');"><div style="float: left;">' + users_name + '</div><div id="close_chat_' + receiver_id + '" style="float: right; padding: 0px 2px 0px 2px; background-color: #3D9DFE; width: 10px; text-align: center;"><a style="color: #FFFFFF; text-decoration: none;" onclick="closeChat(' + receiver_id + ');" onmouseover="highlightButton(' + receiver_id + ', 1, \'close\');" onmouseout="highlightButton(' + receiver_id + ', 0, \'close\');">X</a></div><div style="float: right; cursor: default;"> </div><div id="minimize_chat_' + receiver_id + '" style="float: right; padding: 0px 2px 0px 2px; background-color: #3D9DFE; width: 10px; text-align: center;" onmouseover="highlightButton(' + receiver_id + ', 1, \'minimize\');" onmouseout="highlightButton(' + receiver_id + ', 0, \'minimize\');">_</div></td></tr></table></div><div id="conversation_text_' + receiver_id + '"><table border="0" cellpadding="0" cellspacing="0" width="223" height="200" style="background-color: #FFFFFF;"><tr><td width="100%" class="conversationBorder" height="200" valign="top"><table border="0" cellpadding="0" cellspacing="0" width="223"><tr><td height="150" valign="top"><iframe name="chat_messages_' + receiver_id + '" id="chat_messages_' + receiver_id + '" height="150" width="223" frameborder="0" style="overflow-x: hidden; margin: 0px;"></iframe></td></tr><tr><td height="' + ( user_browser == 'firefox' ? 20 : 18 ) + '" style="padding-left: 5px;"><div id="typing_box_' + receiver_id + '" style="display: none;"><span class="dateStamp">' + users_first_name[0] + ' is typing...</span></div></td></tr><tr><td style="padding: 5px;" width="100%" align="center"><div id="chatTextbox"><textarea id="chat_text_' + receiver_id + '" name="chat_text_' + receiver_id + '" class="textbox" cols="' + ( user_browser == 'firefox' ? 30 : ( user_browser == 'msie' ? 41 : 37 ) ) + '" rows="' + ( user_browser == 'firefox' ? 2 : 3 ) + '" onkeydown="checkConversationEnter(this, event, ' + receiver_id + ');" onfocus="focusTextArea(' + receiver_id + ');" style="height: ' + ( user_browser == 'firefox' ? 49 : 53 ) + 'px; width: ' + ( user_browser == 'firefox' ? 210 : 205 ) + 'px;"></textarea></div></td></tr></table></td></tr></table></div><div id="conversation_title_closed_' + receiver_id + '" style="display: none; margin-top: ' + ( user_browser == 'firefox' ? 13 : 18 ) + 'px;"><table border="0" cellpadding="0" cellspacing="0" width="223" height="229"><tr><td width="100%" height="200" valign="top"><img src="images/spacer.gif"></td></tr></table><table border="0" cellpadding="0" cellspacing="0" width="225"><tr><td width="100%" class="conversationBox" id="conversation_box_closed_' + receiver_id + '" style="cursor: pointer" onclick="toggleConversation(' + receiver_id + ');"><div style="float: left;">' + users_name + '</div><div id="close_chat_closed_' + receiver_id + '" style="float: right; padding: 0px 2px 0px 2px; background-color: #3D9DFE; width: 10px; text-align: center;"><a style="color: #FFFFFF; text-decoration: none;" onclick="closeChat(' + receiver_id + ');" onmouseover="highlightButton(' + receiver_id + ', 1, \'close\');" onmouseout="highlightButton(' + receiver_id + ', 0, \'close\');">X</a></div><div style="float: right; cursor: default;"> </div><div id="minimize_chat_closed_' + receiver_id + '" style="float: right; padding: 0px 2px 0px 2px; background-color: #3D9DFE; width: 10px; text-align: center;" onmouseover="highlightButton(' + receiver_id + ', 1, \'minimize\');" onmouseout="highlightButton(' + receiver_id + ', 0, \'minimize\');">_</div></td></tr></table></div></div>';

		var new_div = document.createElement('div');
		new_div.innerHTML = html_message;
		open_conversations_div.appendChild(new_div);

		Element.show('conversation_' + receiver_id);
		Element.show('conversation_text_' + receiver_id);

		conversations_list[how_many_open] = receiver_id;
		conversations_list_typing[how_many_open] = 0;
		show_conversation[how_many_open] = 0;
		conversations_list_me[how_many_open] = 0;
		change_title_bar[how_many_open] = 1;

		show_conversation[how_many_open] = 1;

		if ( message != '' )
		{
			showTimeStamps[how_many_open] = setTimeout("showTimeStamp(" + receiver_id + ")", 50000);

			var chat_id = how_many_open;
			var chat_conversation_id = conversations_list[chat_id];

			userTyping(chat_conversation_id, 0);
			conversations_list_typing[chat_id] = 0;
			Element.hide('typing_box_' + chat_conversation_id);

			if ( conversations_list_me[chat_id] == 0 )
			{
				$('conversation_box_' + chat_conversation_id).style.backgroundColor = '#f99d39';
				$('conversation_box_closed_' + chat_conversation_id).style.backgroundColor = '#f99d39';
				highlightUser(chat_conversation_id, 2);

				change_title_bar[chat_id] = 2;
				changeTitle(chat_conversation_id);

				highlightButton(chat_conversation_id, 0, 'minimize');
				highlightButton(chat_conversation_id, 0, 'close');
			}
			else
			{
				conversations_list_me[chat_id] = 0;
			}

			var conversation_div = window.frames['chat_messages_' + chat_conversation_id].document.body;
			var new_div = document.createElement('div');
			new_div.innerHTML = message;
			conversation_div.appendChild(new_div);

			checkIfReceived[chat_conversation_id] = setTimeout("checkReceived(" + receiver_id + ", '" + message + "')", 500);

			document.getElementById('chat_messages_' + chat_conversation_id).contentWindow.scrollTo(0,document.getElementById('chat_messages_' + chat_conversation_id).contentWindow.document.body.scrollHeight);
		}

		if ( new_chat )
		{
			document.getElementById('chat_text_' + receiver_id).focus();
		}
	}
}

function checkReceived(receiver_id, message)
{
	if ( window.frames['chat_messages_' + receiver_id].document.body.innerHTML == '' )
	{
		var conversation_div = window.frames['chat_messages_' + receiver_id].document.body;
		var new_div = document.createElement('div');
		new_div.innerHTML = message;
		conversation_div.appendChild(new_div);
	}
}

function userTyping(receiver_id, keypress)
{
	var chat_id = find_chat_id(receiver_id);
	conversations_list_typing[chat_id] = keypress;
	if ( keypress == 1 )
	{
		new Ajax.Request(main_url + 'user_typing.php', {
			method: 'post',
			parameters: {
				users_id: $F('users_id'),
				receiver_id: receiver_id,
				type: 1
			}
		});
	}
	else
	{
		new Ajax.Request(main_url + 'user_typing.php', {
			method: 'post',
			parameters: {
				users_id: $F('users_id'),
				receiver_id: receiver_id,
				type: 0
			}
		});
	}
}

function checkBlankBox(receiver_id)
{
	var chat_id = find_chat_id(receiver_id);
	if ( document.getElementById('chat_text_' + receiver_id).value == '' )
	{
		previous_text_length[chat_id] = 0;
		userTyping(receiver_id, 0);
	}
}

function checkConversationEnter(inField, e, receiver_id)
{
	var chat_id = find_chat_id(receiver_id);
	var chat_text = inField.value;
	var key_pressed;

	if ( e && e.which )
	{
		key_pressed = e.which;
	}
	else if ( window.event )
	{
		e = window.event;
		key_pressed = e.keyCode;
	}

	if ( key_pressed == 13 )
	{
		SendMessage(receiver_id);
	}

	if ( key_pressed == 8 || key_pressed == 46 ) // backspace = 8, delete = 46
	{
		if ( previous_text_length[chat_id] > 0 )
		{
			previous_text_length[chat_id]--;
		}

		clearTimeout(blank_text_box[chat_id]);
		blank_text_box[chat_id] = setTimeout("checkBlankBox(" + receiver_id + ")", 500);
	}
	else
	{
		previous_text_length[chat_id]++;
	}

	var keypress = ( previous_text_length[chat_id] == 0 ? 0 : 1 );
	userTyping(receiver_id, keypress);
}

function highlightUser(chat_user_id, type)
{
	if ( type == 1 )
	{
		document.getElementById('chat_user_' + chat_user_id).style.backgroundColor = '#EAEAEA';
	}
	else if ( type == 2 )
	{
		document.getElementById('chat_user_' + chat_user_id).style.backgroundColor = '#f99d39';
		Effect.Pulsate('chat_user_' + chat_user_id, { pulses: 5, duration: 1.5 });
	}
	else
	{
		document.getElementById('chat_user_' + chat_user_id).style.backgroundColor = '#FFFFFF';
	}
}

function highlightButton(chat_user_id, type, which_one, first_time)
{
	var new_message = 0;
	var i = 1;
	while ( change_title_bar[i] )
	{
		if ( change_title_bar[i] == 2 && chat_user_id == conversations_list[i] )
		{
			new_message = i;
		}

		i++;
	}

	if ( type == 1 )
	{
		document.getElementById(which_one + '_chat_' + chat_user_id).style.backgroundColor = ( new_message ? ( first_time ? '#f99d39' : '#d58630' ) : '#3786d6' );
		document.getElementById(which_one + '_chat_closed_' + chat_user_id).style.backgroundColor = ( new_message ? ( first_time ? '#f99d39' : '#d58630' ) : '#3786d6' );
	}
	else
	{
		document.getElementById(which_one + '_chat_' + chat_user_id).style.backgroundColor = ( new_message ? '#f99d39' : '#3D9DFE' );
		document.getElementById(which_one + '_chat_closed_' + chat_user_id).style.backgroundColor = ( new_message ? '#f99d39' : '#3D9DFE' );
	}
}

function closeChat(receiver_id)
{
	Element.hide('conversation_' + receiver_id);

	var chat_id = find_chat_id(receiver_id);
	closed_chat_box[chat_id] = 1;

	new Ajax.Request(main_url + 'close_conversation.php', {
		method: 'post',
		parameters: {
			users_id: $F('users_id'),
			receiver_id: receiver_id
		}
	});
}
