//is the service administratively down? test
function _isChatServiceEnabled()
{
	return true;
}

function initChat()
{
	//showMaintenanceNotice();
	
	if( typeof(console) == "undefined"){
		console = new Object();
		console.debug = function(str){ }
		console.log = function(str) { }
		console.warn = function(str) { }
		console.error = function(str) { }
		console.info = function(str) { }
	}
	
	var chatAvailDomNode = document.getElementById('chatAvailable');
	var menuDomNode = document.getElementById('chatMenuLinkContainer');
	var graphicDomNode = document.getElementById('chatGraphicContainer');

	if (_isChatServiceEnabled())
	{
		if (chatAvailDomNode && (chatAvailDomNode.value == "0" || chatAvailDomNode.value == "1"))
		{
			//we know from the html / template chat's status
			if (menuDomNode)
				menuDomNode.innerHTML = _getChatMenuHtml(chatAvailDomNode.value == "1");
			
			if (graphicDomNode)
				graphicDomNode.innerHTML = _getChatGraphicHtml(chatAvailDomNode.value == "1");
		}
		else
		{
	
			//need to make a call for chat's status
			var chatHttp = false;
			if(navigator.appName == "Microsoft Internet Explorer") {
				chatHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} else {
				chatHttp = new XMLHttpRequest();
			}
		
			chatHttp.onreadystatechange = function() {
	
				if (menuDomNode && chatHttp.readyState == 4)
					menuDomNode.innerHTML = _getChatMenuHtml(chatHttp.responseText == "online");
					
				if (graphicDomNode && chatHttp.readyState == 4)
					graphicDomNode.innerHTML = _getChatGraphicHtml(chatHttp.responseText == "online");
				
			};
			
			chatHttp.open("GET", "/chatstatus/status.asp?action=groupstatus&groupid=3999", true);
			chatHttp.send(null);
		}
	}
	else
	{
		if (menuDomNode)
			menuDomNode.innerHTML = _getChatMenuHtml(false);
		
		if (graphicDomNode)
			graphicDomNode.innerHTML = _getChatGraphicHtml(false);		
	}
}

function _getChatMenuHtml(bolAvailable)
{
	var html = '<a href="#" style="color:#666666">Chat Unavailable</a>';
	if (bolAvailable)
		html = '<a href="#" onclick="openChatWindow();">Online Chat Help</a>';
	return html;
}

function _getChatGraphicHtml(bolAvailable)
{
	var html = '<img src="/images/online-chat/chat_off.jpg" alt="Online Chat Help Offline" border="0"></a>';
	if (bolAvailable)
		html = '<a href="#" onclick="openChatWindow();"><img src="/images/online-chat/chat_on.jpg" alt="Online Chat Help Avialable" border="0"></a>';
	return html;
}

function openChatWindow()
{
	window.open('https://www.websitealive3.com/3999/rRouter.asp?groupid=3999&websiteid=0&departmentid=2354&dl='+escape(document.location.href),'','width=400,height=400');
}

//not all pages have jQuery imports so pure javascript here
function showMaintenanceNotice()
{
	var notice = document.createElement('div');
	notice.innerHTML = "<b>Maintenance Notice:</b> MedicAlert's Website will be undergoing routine maintenance from January 26th at 11pm until January 27th at 1am PST.<br/>The site will not be accessible during this time though our 24/7 Emergency Response Service will continue uninterruptedly.  Thank you for your patience.";
	notice.style.color = "black";
	notice.style.backgroundColor = "#FEFBEA";
	notice.style.padding = "5px";
	notice.style.textAlign = 'center';
	notice.style.fontFamily = 'Arial';
	notice.style.fontSize = '12px';
	
	var body = document.getElementsByTagName('body')[0];
	body.insertBefore(notice, body.childNodes[0]);
}

