<!--
/**
 * @version		$Id: common.js 2009-01-01 15:24:18Z $
 * @package		BlastChat Client module
 * @author 		BlastChat
 * @copyright	Copyright (C) 2004-2009 BlastChat. All rights reserved.
 * @license		GNU/GPL, see mod_blastchatc_LICENSE.php
 * @HomePage 	<http://www.blastchat.com>

 * This file is part of BlastChat Client module.

 * BlastChat Client module is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * BlastChat Client module is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with BlastChat Client module.  If not, see <http://www.gnu.org/licenses/>.
 */

var bc_httpUpdater = null;
var bc_getURL = null;
var bc_refreshtime = 0;

//initiates the XMLHttpRequest object
function bc_getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

document.getElementsByClassName = function (cl) {
	var retnode = [];
	var myclass = new RegExp('\\b' + cl + '\\b');
	var elem = this.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i ++) {
		var classes = elem[i].className;
		if (myclass.test(classes)) {
			retnode.push(elem[i]);
		}
	}
	return retnode;
}

function bc_writeit(text,objId) {
	//alert(text);
	if (document.layers) { //Netscape 4
		myObj = eval('document.' + objId);
		myObj.document.open();
		myObj.document.write(text);
		myObj.document.close();
	} else 	if ((document.all && !document.getElementById) || navigator.userAgent.indexOf("Opera") != -1) { //IE 4 & Opera
		myObj = eval('document.all.' + objId);
		myObj.innerHTML = text;
	} else if (document.getElementById) { //Netscape 6 & IE 5
		myObj = document.getElementById(objId);
		myObj.innerHTML = text;
	} else {
		alert('This website uses DHTML. We recommend you upgrade your browser.');
	}
}

//initiates the first data query
function bc_updateList() {
	if (bc_httpUpdater.readyState == 4 || bc_httpUpdater.readyState == 0) {
		var t = new Date();
		bc_httpUpdater.open("GET", bc_getURL + "&t=" + t.getTime(), true);
		bc_httpUpdater.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		bc_httpUpdater.onreadystatechange = bc_handlehttpUpdater;
		bc_httpUpdater.send('');
	}
}

//deals with the servers reply to requesting new content
function bc_handlehttpUpdater() {
	if (bc_httpUpdater.readyState == 4) {
		if (bc_httpUpdater.responseText.substr(0, 7) == 'allowed') {
			if (bc_refreshtime > 0) {
				setTimeout("bc_updateList()",bc_refreshtime*1000);
			}
			var newcontent = bc_httpUpdater.responseText.substr(7, bc_httpUpdater.responseText.length);
			var results = newcontent.split('|||');
			
			var gcount = parseInt(results[0]);
			var mcount = parseInt(results[1]);
			var gccount = parseInt(results[2]);
			var mccount = parseInt(results[3]);
			if (gcount > -1) {
				bc_writeit(gcount, "bc_module_guest_count");
				bc_writeit(gcount, "bc_module_guests_count");
			}
			if (mcount > -1) {
				bc_writeit(mcount, "bc_module_member_count");
				bc_writeit(mcount, "bc_module_members_count");
			}
			if (gccount > -1) {
				bc_writeit(gccount, "bc_module_chatguest_count");
				bc_writeit(gccount, "bc_module_chatguests_count");
			}
			if (mccount > -1) {
				bc_writeit(mccount, "bc_module_chatmember_count");
				bc_writeit(mccount, "bc_module_chatmembers_count");
			}
			
			if (gcount > 0 || mcount > 0) {
				document.getElementById("bc_module_online").style.display = "block";
				if (gcount == 0 || gcount > 1) {
					document.getElementById("bc_module_online_guest").style.display = "none";
					document.getElementById("bc_module_online_guests").style.display = "inline";
				} else {
					document.getElementById("bc_module_online_guest").style.display = "inline";
					document.getElementById("bc_module_online_guests").style.display = "none";
				}
				if (mcount == 0 || mcount > 1) {
					document.getElementById("bc_module_online_member").style.display = "none";
					document.getElementById("bc_module_online_members").style.display = "inline";
				} else {
					document.getElementById("bc_module_online_member").style.display = "inline";
					document.getElementById("bc_module_online_members").style.display = "none";
				}
				if (mcount == 0) {
					document.getElementById("bc_module_and").style.display = "none";
				} else {
					document.getElementById("bc_module_and").style.display = "inline";
				}
			} else {
				document.getElementById("bc_module_online").style.display = "none";
			}
			if (gccount > 0 || mccount > 0) {
				document.getElementById("bc_module_chatting").style.display = "block";
				if (gccount == 0 || gccount > 1) {
					document.getElementById("bc_module_chatting_guest").style.display = "none";
					document.getElementById("bc_module_chatting_guests").style.display = "inline";
				} else {
					document.getElementById("bc_module_chatting_guest").style.display = "inline";
					document.getElementById("bc_module_chatting_guests").style.display = "none";
				}
				if (mccount == 0 || mccount > 1) {
					document.getElementById("bc_module_chatting_member").style.display = "none";
					document.getElementById("bc_module_chatting_members").style.display = "inline";
				} else {
					document.getElementById("bc_module_chatting_member").style.display = "inline";
					document.getElementById("bc_module_chatting_members").style.display = "none";
				}
				if (mccount == 0) {
					document.getElementById("bc_module_chatand").style.display = "none";
				} else {
					document.getElementById("bc_module_chatand").style.display = "inline";
				}
			} else {
				document.getElementById("bc_module_chatting").style.display = "none";
			}
			bc_writeit(decodeURI(results[4]),'bc_module_names');
			//bc_writeit(results[4],'bc_module');
			
			var tt = document.getElementsByClassName("tool-tip");
			for (var i = 0; i < tt.length; i ++) {
				if (tt[i] && tt[i].parentNode && tt[i].parentNode.removeChild) {
					tt[i].parentNode.removeChild(tt[i]);
				}
			}
			JTooltips = null;
			JTooltips = new Tips($$('.hasTip'), { maxTitleChars: 50, fixed: false});
		}
	}
}

function bc_start(bct) {
		if (bct > 0) {
			bc_refreshtime = bct;
			bc_httpUpdater = bc_getHTTPObject();
			bc_getURL = "index.php?option=com_blastchatc&bc_task=updatelist&tmpl=component&format=raw";
			setTimeout("bc_updateList()",bc_refreshtime*1000);
		}
}
//-->