﻿// ====================================================================================================
//
//          Page: AJAX WEB CHAT FUNCTIONS
//        Author: Michael Marzilli   ( http://www.linkedin.com/in/michaelmarzilli )
//       Created: Aug 11, 2011
//
//	Minify with: http://sundgaard.dk/javascript-minify.aspx
//
// VERS 1.0.000 : Aug 11, 2011 : Original File Created, split off from JSLibrary.js.
//			1.0.001 : Oct 24, 2011 : Added a Notify icon to tell the user when a new chat line has come in.
//															 Programmer can specify his own notify object (using "UseCustomNotify")
//															 or use the default object.
//
// ====================================================================================================



// WEB CHAT FUNCTIONS
// ----------------------------------------------------------------------------------------------------
var chatAJAX;
var strWebChatID    = 'WEBCHAT';
var strCustomNotify = '';
var intChatRefresh  = 30;
var intChatChk      = 0;
var intLastMessage  = 0;
var intLastRead     = 0;
var intLastNew      = 0;
var intCHID         = 1;
var intUID          = 0;
var intUseNotify    = 1;
var blnChatSent     = false;


function WriteWebChatCookie(blnUpdate)
{
	if (blnUpdate || blnChatSent)
		intLastRead = intLastNew;
	var strOut = intChatChk + '|' + intCHID + '|' + intLastRead + '|' + intLastNew;
	WriteCookie('WEBCHAT_SET', strOut, 2);
	if (intLastRead >= intLastNew || blnChatSent)
		chatAJAX.SetAJAXNotifyHidden();
	else
		chatAJAX.SetAJAXNotifyVisible();
}
function ReadWebChatCookie()
{
	var strIn = ReadCookie('WEBCHAT_SET');
	if (strIn == null || strIn == '')
		strIn = '0|1|0|0';
	var ca = strIn.split('|');
	for (var i = 0; i < ca.length; i++)
	{
		var c = ca[i];
		switch (i)
		{
			case 0: 	// NOTIFICATION CHECK
				intChatChk  = c - 0;
				break;
			case 1: 	// WEB CHAT CHANNEL
				intCHID     = c - 0;
				break;
			case 2: 	// LAST LINE READ
				intLastRead = c - 0;
				break;
			case 3: 	// LAST LINE ADDED
				intLastNew  = c - 0;
				break;
		}
	}
}
function ReadWebChatNewCount()
{
	var n = Number(chatAJAX.GetRecordCount());
	if (n > intLastNew)
		intLastNew = n;
	WriteWebChatCookie(false);
	blnChatSent = false;
}
function UpdateWebChatCookie()
{
	ReadWebChatNewCount();
	WriteWebChatCookie(true);
}

function SetChatChannel(ddl)
{
	if (GetDDLselectedItem(ddl) - 0 != intCHID - 0)
	{
		intLastRead = 0;
		intLastNew  = 0;
	}
	intCHID = GetDDLselectedItem(ddl);
	if (intCHID < 1)
		intCHID   = 1;
	WriteWebChatCookie(true);
	if (chatAJAX != null)
		chatAJAX.ResetDiv();
	GetChatTextBox().focus();
}
function SetChatCheck(chk)
{
	if (chk.checked)
		intChatChk = 1;
	else
		intChatChk = 0;
	WriteWebChatCookie(true);
	GetChatTextBox().focus();
}
function GetChatTextBox()
{
	return getObjectElementByID(strWebChatID, 'txtMessage');
}
function GetChatTextBoxText()
{
	var strReturn = GetChatTextBox().value;

	trim(strReturn);
	strReturn = strReturn.replace(/</g, "[");
	strReturn = strReturn.replace(/>/g, "]");
	strReturn = strReturn.replace(/&/g, "and");
	strReturn = chat_string_create_urls(strReturn);

	return strReturn;
}
function StartWebChat()
{
	setTimeout('AJAX_CHAT_START()', 250);
}



function AJAX_CHAT_START()
{
	// READ WEB CHAT COOKIE INFORMATION
	ReadWebChatCookie();

	// DETERMINE CHAT CHANNEL
	if (!SetDDLSelectedItem(strWebChatID, 'ddlChannel', intCHID))
	{
		intCHID = 1;
		WriteWebChatCookie(false);
	}

	// DETERMINE AUDIO NOTIFICATION WHEN NEW MESSAGES ARRIVE
	var c = getObjectElementByID('', 'chkChatNotify');
	if (c != null)
	{
		c.value   = intChatChk;
		c.checked = (intChatChk == '1');
	}

	// CREATE AND INIT THE CHAT OBJECT
	chatAJAX = new AJAX();
	chatAJAX.SetID('chatAJAX');
	chatAJAX.SetAJAXpoll(intChatRefresh);
	chatAJAX.SetAJAXscrolling(true);
	chatAJAX.SetAJAXwaitObject('', 'webchatwait');
	if (intUseNotify == 1)
		chatAJAX.SetAJAXnotifyObject('', 'webchatnotify');
	else if (strCustomNotify != '')
		chatAJAX.SetAJAXnotifyObject('', strCustomNotify);
	chatAJAX.SetAJAXgetFunction('AJAX_CHAT_GET');
	chatAJAX.SetAJAXtarget('AJAX_Chat.aspx');
	chatAJAX.SetTargetDiv('', 'div_chat');
	chatAJAX.SetTargetTempDiv('div_chat_temp');
	chatAJAX.SetAJAXbeepPlay(1);
	chatAJAX.SetAJAXbeep(0);
	chatAJAX.SetAJAXbeepOverride(1);
	window.setTimeout('AJAX_CHAT_GET()', 1000);
}
function AJAX_CHAT_GET()
{
	chatAJAX.ClearParams();
	chatAJAX.AddParam('app',    'chat');
	chatAJAX.AddParam('action', 'request');
	chatAJAX.AddParam('uid',  intUID);
	chatAJAX.AddParam('chid', intCHID);
	chatAJAX.AddParam('last', intLastMessage);

	// GET NEW CHAT MESSAGE COUNT
	setTimeout('ReadWebChatNewCount()', 2000);

	// PLAY AUDIO NOTIFICATION (IF APPLICABLE)
	var c = getObjectElementByID('', 'chkChatNotify');
	if (c != null)
		if (c.checked)
			chatAJAX.SetAJAXbeep(1);
		else
			chatAJAX.SetAJAXbeep(0);

	chatAJAX.GetAJAX();
}
function AJAX_CHAT_SEND()
{
	var strMsg = GetChatTextBoxText();
	chatAJAX.SetAJAXWaitVisible();
	chatAJAX.SetAJAXbeepOverride(1);
	chatAJAX.SetAJAXbeep(0);
	chatAJAX.ClearParams();
	chatAJAX.AddParam('app',    'chat');
	chatAJAX.AddParam('action', 'send');
	chatAJAX.AddParam('uid',  intUID);
	chatAJAX.AddParam('chid', intCHID);
	chatAJAX.AddParam('last', intLastMessage);
	chatAJAX.AddParam('msg',  strMsg);
	chatAJAX.SendAJAXtext();
	GetChatTextBox().value = '';
	GetChatTextBox().focus();
	blnChatSent = true;
	AJAX_CHAT_ACKNOWLEDGE(true);
}
function AJAX_CHAT_REFRESH()
{
	chatAJAX.ClearDiv();
	GetChatTextBox().value = '';
	GetChatTextBox().focus();
	AJAX_CHAT_ACKNOWLEDGE(true);
}
function AJAX_CHAT_RESET()
{
	chatAJAX.SetAJAXWaitVisible();
	chatAJAX.SetAJAXbeep(0);
	chatAJAX.ClearDiv();
	chatAJAX.ClearParams();
	chatAJAX.AddParam('app',    'chat');
	chatAJAX.AddParam('action', 'reset');
	chatAJAX.AddParam('uid',  intUID);
	chatAJAX.AddParam('chid', intCHID);
	chatAJAX.AddParam('last', intLastMessage);
	chatAJAX.SendAJAXtext();
	GetChatTextBox().value = '';
	GetChatTextBox().focus();
	AJAX_CHAT_ACKNOWLEDGE(true);
}
function AJAX_CHAT_ACKNOWLEDGE(useTimeout)
{
	window.setTimeout('UpdateWebChatCookie()', 2000);
	if (!useTimeout)
		chatAJAX.SetAJAXNotifyHidden();
}

