<!--
/**
 * @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/>.
 */

//initiates the XMLHttpRequest object
function 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;
}

function 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 = '';
		myObj.innerHTML = text;
	} else {
		alert('This website uses DHTML. We recommend you upgrade your browser.');
	}
}

//initiates the first data query
function updateList() {
	if (httpUpdater.readyState == 4 || httpUpdater.readyState == 0) {
		httpUpdater.open("GET", getURL, true);
		httpUpdater.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		httpUpdater.onreadystatechange = handlehttpUpdater;
		httpUpdater.send('');
	}
}

//deals with the servers reply to requesting new content
function handlehttpUpdater() {
	if (httpUpdater.readyState == 4) {
		if (httpUpdater.responseText.substr(0, 7) == 'allowed') {
			var newcontent = httpUpdater.responseText.substr(7, httpUpdater.responseText.length);
			var results = newcontent.split('|||');
			writeit(decodeURI(results[0]),'bc_module');
			//writeit(results[0],'bc_module');
			placeUsername(results[1]);
			if (refreshtime > 0)
				setTimeout("updateList()",refreshtime*1000);
		}
	}
}

function placeUsername(result) {
	var users = result.split("|;|");
	var user = null;
	var u = null;
	var c = null;
	var cb = null;
	var usernametext = null;
	for (var i=0; i < users.length; i++) {
		user = users[i].split(";,;");
		usernametext = document.createTextNode(decodeURI(user[1]));
		u = document.getElementById('user'+user[0]);
		c = document.getElementById('chatlink'+user[0]);
		cb = document.getElementById('cblink'+user[0]);
		if (u) {
			u.removeChild(u.firstChild);
			u.appendChild(usernametext);
		}
		if (c) {
			c.removeChild(c.firstChild);
			c.appendChild(usernametext);
		}
		if (cb) {
			cb.removeChild(cb.firstChild);
			cb.appendChild(usernametext);
		}
		usernametext = null;
		u = null;
		c = null;
		cb = null;
	}
}
//-->
