﻿//------------------
//	CHAT.JS
//------------------

var mlchatid = null;
var mlchatinterval = null;
var mbchatadmin = false;

function chatadminavailable()
{
	mbchatadmin = true;
	chatintervalcreate();
	chatmonitorcreate();
}


function showchat(ssubject, badmin)
{
	// show the div and start a chat, informing the user if no admins are available
	try
	{
		chatintervalclear();
		var div = document.getElementById("divchat");
		div.style.display = "inline";
		if (badmin)
		{
			// nothing to do, the ajax call was made already; so we just make the box visible
		}
		else
		{
			mladmininterval = null;
			var stext = httpget("chatajax.aspx?status=1");
			feedchattext(stext);
		}
	}
	catch (e)
	{
		alert("Chat is not available : " + e.description);
	}
	chatintervalcreate();
}

function hidechat()
{
	// end the chat session and hide the div
	try
	{
		var div = document.getElementById("divchat");
		div.style.display = "none";
		if (mlchatid)
		{
			chatintervalclear();
			httpget("chatajax.aspx?chatid=" + mlchatid + "&status=4");
			mlchatid = null;
		}
	}
	catch (e)
	{
	}
}

function httpget(surl)
{
    var xmlhttp;    
    try    
    {        
		xmlhttp = new XMLHttpRequest();
	}    
	catch (e)    
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.xmlhttp");
		}
		catch (e)
		{
			return null;
		}
	}
	
	try
	{
		xmlhttp.open("GET", surl, false);
		xmlhttp.setRequestHeader("content-type", "text/plain;charset=utf-8");		// makes no difference to the try catch below
		xmlhttp.send(null);
	}
	catch (e)
	{
		window.status = "Chat is not working. Check this error:" + e.description;
		return null;
	}
	
	var ret = xmlhttp.responseText;
	return ret;
}

function chatsend()
{
	// send the piece of text typed
	var bret;
	try
	{
		chatintervalclear();
		var txt = document.getElementById("txtchat");
		if (txt)
		{
			var svalue = txt.value;
			if (svalue.toLowerCase() == "ask a question" || svalue == "")
			{
				bret = false;
			}
			else if(svalue.indexOf("|#|") > 0)
			{
				alert("Chat and forum posts cannot contain the character sequence |$|");
				bret = false;
			}
			else if(svalue.indexOf("|;|") > 0)
			{
				alert("Chat and forum posts cannot contain the character sequence |;|");
				bret = false;
			}
			var stext = httpget("chatajax.aspx?chatid=" + mlchatid + "&text=" + escape(svalue));
			feedchattext(stext);
			txt.value = "";
			bret = true;
		}
	}
	catch (e)
	{
		alert("Could not send the text requested: " + e.description);
		bret = false;
	}
	chatintervalcreate();
	return bret;
}


function chatkeyup(e)
{
	var keynum;

	if(window.event) 
	{
		keynum = e.keyCode;
	}
	else if (e.which) 
	{
		keynum = e.which;
	}
	keychar = String.fromCharCode(keynum);

	if (keynum == 13)
	{
		chatsend();
	}
	else if (keynum == 27)
	{
		if (confirm("Hide chat now ?\n\nThis will lose the chat text shown"))
		{
			hidechat();
		}
	}
	else
	{
		clearaskaquestion();
	}
}

function clearaskaquestion()
{
	try
	{
		var txt = document.getElementById("txtchat");
		if (txt) if (txt.value.toLowerCase() == "ask a question")
		{
			window.setTimeout("clearaskaquestion2()", 100);
		}
	}
	catch (e)
	{
	}
}

function clearaskaquestion2()
{
	try
	{
		var txt = document.getElementById("txtchat");
		if (txt)
		{
			txt.value = "";
		}
	}
	catch (e)
	{
	}
}


function chatrefresh()
{
	try
	{
		chatintervalclear();
		// person on chat is still here
		if (mlchatid || mbchatadmin)
		{
			var stext = httpget("chatajax.aspx?registercontact=1&chatid=" + mlchatid);
			if (mbchatadmin) 
			{
				if (stext != "")
				{
					if (mlchatid == null) alert("New Chat Starting");
					showchat(null, 1);		// otherwise its already showing
				}
			}
			feedchattext(stext);
		}
	}
	catch (e)
	{
	}
	chatintervalcreate();
}

var slasthtml = "";
function feedchattext(stext)
{
	try 
	{
		if (stext) if (stext != "")
		{
			if (stext.substr(0, 17).toLowerCase() == "chat server error")
			{
				window.status = stext;
				return;
			}
			var ssplit = stext.split("|;|");		// one record per thread
			var shtml = "";
			for (var i = 0; i < ssplit.length; i++)
			{
				var sthreadrecord = ssplit[i];
				if (sthreadrecord != "")
				{
					var ssplitthread = sthreadrecord.split("|$|");

					var sthreadid = ssplitthread[0];
					mlchatid = sthreadid;
					var sthreadlineid = ssplitthread[1];
					var suser = ssplitthread[2];
					if (suser == "") suser = "Unknown User";
					var sstatus = ssplitthread[3];
					var stextadd = ssplitthread[4];
		
					if (stextadd != "")
					{
						shtml += "<span class=\"chatuser\">" + suser + "</span>&nbsp;&nbsp;" + stextadd + "";
					}
					
					if (sstatus == "4")
					{
						mlchatid = null;
					}
				}
			}
			if (shtml != slasthtml)
			{
				var div = document.getElementById("divchattext");
				div.innerHTML = shtml;
				div.scrollTop = div.scrollHeight;
				slasthtml = shtml;
			}
		}
	}
	catch (e)
	{
		alert("chat error : " + e.description);
	}
}

function chatintervalcreate()
{
	if (mbchatadmin == false || mlchatinterval == null)
	{
		chatintervalclear();
		mlchatinterval = setInterval("chatrefresh()", 2000);
	}
}

function chatintervalclear()
{
	if (mbchatadmin == false)
	{
		if (mlchatinterval)
		{
			clearInterval(mlchatinterval);
		}
		mlchatinterval = null;
	}
}

function chatmonitorcreate()
{
	// admins only
	window.setInterval("chatcheck()", 3600);
}

var mlchatcount = 0;
function chatcheck()
{
	try 
	{
		// admins only
		if (mlchatinterval == null)
		{
			mlchatcount++;
			if (mlchatcount > 3)
			{
				chatintervalcreate();
			}
		}
		else
		{
			mlchatcount = 0;
		}
	}
	catch (e)
	{
	}
}

// COPYRIGHT 2001-2009 XASSETS.COM LIMITED



