// Script for the indicators of the chat status
/////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////
// Constants
////////////////////////////////////////////////////////

var g_script = "http://www.lowemfoffice.com/chat/indicator.php";
var g_chat_url = "http://www.lowemfoffice.com/chat/index.htm";
var g_chat_title="LowEMFOffice Direct Chat";
var g_icon_url_on = "http://www.lowemfoffice.com/chat/online.png";
var g_icon_url_off = "http://www.lowemfoffice.com/chat/offline.png";
var g_tip_on = "The Direct Chat is available";
var g_tip_off = "The Direct Chat is offline";

////////////////////////////////////////////////////////
// Global variables
////////////////////////////////////////////////////////

var g_indicator_request;          // The XMLHttpRequest object
var g_indicator_timer;            // The Timer object
var g_indicator_status;           // The chat status

////////////////////////////////////////////////////////
// Create a new XMLHttpRequest and set some of its headers
////////////////////////////////////////////////////////

function createNewRequest()
{
  g_indicator_request = false;
	
  // For Safari, Firefox, and other non-MS browsers
  if (window.XMLHttpRequest) 
  {
    try
    {
      g_indicator_request = new XMLHttpRequest();
    }
    catch (e) 
    {
      g_indicator_request = false;
	  } 
	} 
	else if (window.ActiveXObject) 
	{
		// For Internet Explorer on Windows
		try 
		{
			g_indicator_request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try 
			{
				g_indicator_request = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) 
			{
				g_indicator_request = false;
			}
		}
	}

	if (!g_indicator_request) 
	{
		location.href = "apology.html";
		return;
	}

}

////////////////////////////////////////////////////////
// Functions to send the request and process the response
////////////////////////////////////////////////////////

function sendRequest()
{
  // Create a new request
	createNewRequest();
	
  // Launches the request
  var body = "";
  g_indicator_request.onreadystatechange = onRequestReadyStateChange;
	g_indicator_request.open("POST", g_script, true);
	g_indicator_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	g_indicator_request.send(body);
	
}


function onRequestReadyStateChange()
{
  var DONE = 4;
  if (g_indicator_request.readyState != DONE )
  {
    return;
  }

  g_indicator_status = g_indicator_request.responseText;
  
  img = document.getElementById('chat_icon');
  
  if ( g_indicator_status == "off" )
  {
    img.src = g_icon_url_off;
    img.alt = g_tip_off;
    img.title = g_tip_off;
  }
  else if ( g_indicator_status == "on")
  {
    img.src = g_icon_url_on;
    img.alt = g_tip_on;
    img.title = g_tip_on;
  }

  // Reset the request
  g_indicator_request = null;
  
}

////////////////////////////////////////////////////////
// Function to open the chat window
////////////////////////////////////////////////////////

function openChatWindow()
{
  if ( g_indicator_status == "on" )
  {
    chatwindow = window.open(g_chat_url,"chat",
    "location=0,status=1,scrollbars=0,resizable=0,toolbar=0,directories=0,menubar=0,copyhistory=0,width=400,height=800");
    chatwindow.moveTo(0,0);
  } 
}

////////////////////////////////////////////////////////
// Initialization
////////////////////////////////////////////////////////

function init()
{
  // Start the timer for polling
  g_indicator_timer = setInterval("sendRequest()", 1000);
}

////////////////////////////////////////////////////////
// EOF
////////////////////////////////////////////////////////





