//
// Need to use a global variable to display last read message.  Possible downside affects guaranteed
// delivery.
//
var g_last_instant_message_read = 0;
var g_prompt_waiting = 3;
var g_chat_enable = 0;

//
// Standard client chat utilities.
//
function write_chat_message( p_address_id )
{
	if( !g_chat_enable )
	{
		alert( "Please wait until your chat request has been accepted." );
		document.getElementById( 'my_chat_line' ).value = "";
	}
	else
	{
		var l_text = escape( document.getElementById( 'my_chat_line' ).value );

		if( l_text.length > 0 )
		{
			write_instant_message( p_address_id, l_text );
			document.getElementById( 'my_chat_line' ).value = "";
		}
	}
}

function click_chat_button()
{
	document.getElementById('light').style.display='none';
	document.getElementById('fade').style.display='none';
}


function handle_chat_request( p_info_stream )
{
	document.getElementById('light').innerHTML = p_info_stream;
	document.getElementById('light').style.display='block';
	document.getElementById('fade').style.display='block';
}


function handle_chat_accept( p_calling_id, p_info_stream )
{
	var chatBox = document.getElementById('chat_box');
	var chatTab = document.getElementById('chat_tab');

	if( p_info_stream == 'ACCEPT' )
	{
		g_chat_enable = 1;
		chatBox.innerHTML = chatBox.innerHTML + "<br />... your request to chat has been accepted!";
		chatBox.scrollTop = chatBox.scrollHeight;
		var str = chatTab.innerHTML;
		chatTab.innerHTML = str.replace( /Waiting To Chat/, "Chatting" );
		read_instant_message( p_calling_id );
	}
	else if( p_info_stream == 'LATER' )
	{
		chatBox.innerHTML = chatBox.innerHTML + "<br />Your chat request cannot be taken now.  Please try again later.";
		chatBox.scrollTop = chatBox.scrollHeight;
		var str = chatTab.innerHTML;
		chatTab.innerHTML = str.replace( /Waiting To Chat/, "Try Later" );
	}
	else if( p_info_stream == 'DECLINE' )
	{
		chatBox.innerHTML = chatBox.innerHTML + "<br />I am sorry but your chat request has been declined.";
		chatBox.scrollTop = chatBox.scrollHeight;
		var str = chatTab.innerHTML;
		chatTab.innerHTML = str.replace( /Waiting To Chat/, "Unable To Chat" );
	}
	else if( p_info_stream == 'BLOCK' )
	{
		chatBox.innerHTML = chatBox.innerHTML + "<br />Your communication has been blocked!";
		chatBox.scrollTop = chatBox.scrollHeight;
		var str = chatTab.innerHTML;
		chatTab.innerHTML = str.replace( /Waiting To Chat/, "Communication Blocked" );
	}
	else if( p_info_stream == 'CALLING' )
	{
		var check_again = setTimeout( "wait_for_chat_accept( " + p_calling_id + " )", 2000 );
		g_prompt_waiting--;
		if( !g_prompt_waiting )
		{
			chatBox.innerHTML = chatBox.innerHTML + "<br />... waiting";
			chatBox.scrollTop = chatBox.scrollHeight;
			g_prompt_waiting = 3;
		}
	}
}

function handle_display_message( p_info_stream, p_address_id )
{
	var chatBox = document.getElementById('chat_box');
	var info_stream = p_info_stream;

	if( p_address_id )
	{
		g_last_instant_message_read = p_info_stream.substring( 0, p_info_stream.indexOf( "," ) );
		info_stream = p_info_stream.substring( ( p_info_stream.indexOf( "," ) + 1 ) );
	}
	chatBox.innerHTML = chatBox.innerHTML + info_stream;
	chatBox.scrollTop = chatBox.scrollHeight;
	if( p_address_id )
		var check_again = setTimeout( "read_instant_message( " + p_address_id + " )", 500 );
	else
		my_chat_line.focus();
}


function read_instant_message( p_address_id )
{
	var ajaxRequest;
	
	try
	{
		ajaxRequest = new XMLHttpRequest();
	}
	catch (e)
	{
		try
		{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				return false;
			}
		}
	}

	ajaxRequest.onreadystatechange = function()
	{
		if(ajaxRequest.readyState == 4)
		{
			if( ajaxRequest.responseText )
				handle_display_message( ajaxRequest.responseText, p_address_id );
		}
	}

	ajaxRequest.open( "GET", "/read-IM.php?a=" + p_address_id + "&m=" + g_last_instant_message_read, true );
	ajaxRequest.send( null ); 
}


function write_instant_message( p_address_id, l_text )
{
	var ajaxRequest;
	
	try
	{
		ajaxRequest = new XMLHttpRequest();
	}
	catch (e)
	{
		try
		{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				return false;
			}
		}
	}

	ajaxRequest.onreadystatechange = function()
	{
		if(ajaxRequest.readyState == 4)
		{
			if( ajaxRequest.responseText )
				handle_display_message( ajaxRequest.responseText, 0 );
		}
	}

	ajaxRequest.open( "GET", "/write-IM.php?a=" + p_address_id + "&m=" + l_text, true );
	ajaxRequest.send( null ); 
}


function check_for_callers( p_caller_id )
{
	var ajaxRequest = false;
	
	try
	{
		ajaxRequest = new XMLHttpRequest();
	}
	catch (e)
	{
		try
		{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				return false;
			}
		}
	}

	ajaxRequest.onreadystatechange = function()
	{
		if(ajaxRequest.readyState == 4)
		{
			if( ajaxRequest.responseText )
				handle_chat_request( ajaxRequest.responseText );
			else
				var check_again = setTimeout( "check_for_callers( " + p_caller_id + " )", 5000 );
		}
	}

	ajaxRequest.open( "GET", "/check-who-is-calling.php?request_id=" + p_caller_id, true );
	ajaxRequest.send( null ); 
}


function wait_for_chat_accept( p_calling_id )
{
	var ajaxRequest;
	
	try
	{
		ajaxRequest = new XMLHttpRequest();
	}
	catch (e)
	{
		try
		{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				return false;
			}
		}
	}

	ajaxRequest.onreadystatechange = function()
	{
		if(ajaxRequest.readyState == 4)
		{
			if( ajaxRequest.responseText )
				handle_chat_accept( p_calling_id, ajaxRequest.responseText );
		}
	}

	ajaxRequest.open( "GET", "/wait-for-chat-accept.php?request_id=" + p_calling_id, true );
	ajaxRequest.send( null ); 
}


function decline_chat_request( p_decline, p_caller_id )
{
	var ajaxRequest;
	
	try
	{
		ajaxRequest = new XMLHttpRequest();
	}
	catch (e)
	{
		try
		{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				return false;
			}
		}
	}

	ajaxRequest.onreadystatechange = function()
	{
		if(ajaxRequest.readyState == 4)
		{
			if( ajaxRequest.responseText )
				click_chat_button();
		}
	}

	ajaxRequest.open( "GET", "/decline-chat-request.php?request_id=" + p_caller_id + "&action=" + p_decline, true );
	ajaxRequest.send( null ); 
}
