var http_request = false;

// Load an XML document using XMLHttpRequest.
function getXMLDocument(url, callback)
{
	if (!window.XMLHttpRequest)
		return false;

	var myDoc = new XMLHttpRequest();
	if (typeof(callback) != "undefined")
	{
		myDoc.onreadystatechange = function ()
		{
			if (myDoc.readyState != 4)
				return;

			if (myDoc.responseXML != null && myDoc.status == 200)
				callback(myDoc.responseXML);
		};
	}
	myDoc.open('GET', url, true);
	myDoc.send(null);
	return true;
}

// Send a post form to the server using XMLHttpRequest.
function sendXMLDocument(url, content, callback)
{
	if (!window.XMLHttpRequest)
		return false;

	var sendDoc = new window.XMLHttpRequest();
	if (typeof(callback) != "undefined")
	{
		sendDoc.onreadystatechange = function ()
		{
			if (sendDoc.readyState != 4)
				return;

			if (sendDoc.responseXML != null && sendDoc.status == 200)
				callback(sendDoc.responseXML);
			else
				callback(false);
		};
	}
	sendDoc.open('POST', url, true);
	if (typeof(sendDoc.setRequestHeader) != "undefined")
		sendDoc.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	sendDoc.send(content);

	return true;
}


//monitor the database
//if we get a message while online, pop up a window
function XmlLog()
{
	var data = new Array();
	var text = "";
	if (window.XMLHttpRequest)
	{
/*		data[data.length] = "uid=" + uid;
		data[data.length] = "sesc=" + escape(textToEntities(sesc));
		text = data.join("&");
		sendXMLDocument("http://localhost/sites/inc/index.php", text, onDocLog);
*/		
		getXMLDocument("http://localhost/sites/inc/index.php", onDocLog);
	}
}

function onDocLog(XMLDoc)
{
	alert('xmldoc');
	if(XmlError(XMLDoc)) return ;
	var lognode = XMLDoc.getElementsByTagName("log");	
	if(!lognode) return;	  

	var logtime = lognode[0].getElementsByTagName("logtime");
	var online = lognode[0].getElementsByTagName("online");
	var offline = lognode[0].getElementsByTagName("offline");
	
	if(offline[0].firstChild.nodeValue > 0)
	{
		OpenOffline(uid);						
	}
	ltime = logtime[0].firstChild.nodeValue;
	online = online[0].firstChild.nodeValue;
	ofline = offline[0].firstChild.nodeValue;
//			window.status = "logtime: " + ltime + " online: " + online + " offline: " + ofline;
}		
	
function XmlError(XMLDoc)
{
	var errornode = XMLDoc.getElementsByTagName("errmsg");	
	if(!errornode) return
	
	if(errornode.length > 0) 
	{
		var msg = errornode[0].getElementsByTagName("msg");
		var act = errornode[0].getElementsByTagName("action");
		message = msg[0].firstChild.nodeValue;
		action  = act[0].firstChild.nodeValue;
		if(action == "status")
			window.status = message;
		else
			alert(message);
		return true;
	}
	return false;
}

/*	<script language="JavaScript" type="text/javascript"><!-- // --><![CDATA[			
		var uid = '.ChatScriptGetData('uid').';
		var sesc = "'.ChatScriptGetData('sesc').'";

		var rlist = document.getElementById("RoomList");
		var clist = document.getElementById("CatList");
		
		ChatListCat();

		function ChatListCat()
		{
			if (window.XMLHttpRequest)
			{
				getXMLDocument("xml.php?mode=chatcatget", onCatDocGet);
			}
		}

		function onCatDocGet(XMLDoc)
		{
			var clist = document.getElementById("CatList");
			clist.length = 0;

			if(ChatXmlError(XMLDoc)) return ;
			var catnode = XMLDoc.getElementsByTagName("room");	
			if(!catnode) 
			{
				clist.options[rlist.length] = new Option("No room categories found...", 0);
				return;	  
			}			

			for (i = 0; i < catnode.length; i++)
			{
				var rid = catnode[i].getElementsByTagName("rid");
				var cid = catnode[i].getElementsByTagName("cid");
				var name = catnode[i].getElementsByTagName("name");
				var desc = catnode[i].getElementsByTagName("desc");
				var users = catnode[i].getElementsByTagName("users");

				txt = name[0].firstChild.nodeValue + " [" + users[0].firstChild.nodeValue +"]";
				value = rid[0].firstChild.nodeValue
				clist.options[clist.length] = new Option(txt, value);
			} 		
			if(catnode.length) clist.selectedIndex = 0;		
			ChatSelectCat();
		}
		
		function ChatSelectCat()
		{
			var clist = document.getElementById("CatList");
			sel = clist.selectedIndex;
			if(clist.selectedIndex == -1) return;
			cat = clist.options[clist.selectedIndex].value;
			if (window.XMLHttpRequest)
			{
				getXMLDocument("xml.php?mode=chatroomsget&cat="+cat, onRoomDocGet);
			}
		}

		function onRoomDocGet(XMLDoc)
		{
			var rlist = document.getElementById("RoomList");
			rlist.length = 0;

			if(ChatXmlError(XMLDoc)) return ;
			var roomnode = XMLDoc.getElementsByTagName("room");	
			if(!roomnode) 
			{
				rlist.options[rlist.length] = new Option("No room found...", 0);
				return;	  
			}			

			for (i = 0; i < roomnode.length; i++)
			{
				var rid = roomnode[i].getElementsByTagName("rid");
				var cid = roomnode[i].getElementsByTagName("cid");
				var name = roomnode[i].getElementsByTagName("name");
				var desc = roomnode[i].getElementsByTagName("desc");
				var users = roomnode[i].getElementsByTagName("users");

				txt = name[0].firstChild.nodeValue + " [" + users[0].firstChild.nodeValue +"]";
				value = rid[0].firstChild.nodeValue
				rlist.options[rlist.length] = new Option(txt, value);
			} 				
		}		
		
		function ChatCreatCat()
		{
			var data = new Array();
			var text = "";
			var nbox = document.forms.FormCat.CatName;
			var dbox = document.forms.FormCat.CatDesc;
			var clist = document.getElementById("CatList");
			
			name = nbox.value;
			desc = dbox.value;
			if(name.length < 3)
			{
				alert("Enter the room name (min 4 characters)");
				nbox.focus();
				return;
			}

			if(confirm("Are you sure you want to create this category [" + name + "]") == false) return 
			if (window.XMLHttpRequest)
			{
				data[data.length] = "uid=" + uid;
				data[data.length] = "name=" + escape(textToEntities(name));
				data[data.length] = "desc=" + escape(textToEntities(desc));
				data[data.length] = "sesc=" + escape(textToEntities(sesc));
				text = data.join("&");
				sendXMLDocument("xml.php?mode=chatcatput", text, onDocCatCreate);
			}
		}

		function onDocCatCreate(XMLDoc)
		{
			if(ChatXmlError(XMLDoc)) return ;
			window.status = "Category " + document.forms.FormCat.CatName.value + " created...";
			ChatListCat();
			document.forms.FormCat.CatName.value = "";
			document.forms.FormCat.CatDesc.value = "";
		}		
		
		function ChatDeleteCat()
		{
			var clist = document.getElementById("CatList");
			var data = new Array();
			var text = "";

			if(clist.selectedIndex == -1)
			{
				alert("Select a room category");
				clist.focus();
				return;
			}
			
			if(confirm("Are you sure you want to create this category [" + clist.options[clist.selectedIndex].text + "]") == false) return 

			cat = clist.options[clist.selectedIndex].value;
			if (window.XMLHttpRequest)
			{
				data[data.length] = "cat=" + cat;
				data[data.length] = "uid=" + uid;
				data[data.length] = "sesc=" + escape(textToEntities(sesc));
				text = data.join("&");
				sendXMLDocument("xml.php?mode=chatcatcut", text, onDocCatDelete);
			}
		}

		function onDocCatDelete(XMLDoc)
		{
			var clist = document.getElementById("CatList");
			if(ChatXmlError(XMLDoc)) return ;
			window.status = "Category " + clist.options[clist.selectedIndex].text + " deleted...";
			document.forms.FormCat.CatName.value = "";
			document.forms.FormCat.CatDesc.value = "";
			ChatListCat();
		}		
		
		function ChatCreatRoom()
		{
			var data = new Array();
			var text = "";
			var clist = document.getElementById("CatList");
			var nbox = document.forms.FormRoom.RoomName;
			var dbox = document.forms.FormRoom.RoomDesc;
			
			if(clist.selectedIndex == -1)
			{
				alert("Select a room category");
				clist.focus();
				return;
			}
			cat = clist.options[clist.selectedIndex].value;

			name = nbox.value;
			desc = dbox.value;
			if(name.length < 3)
			{
				alert("Enter the room name (min 4 characters)");
				nbox.focus();
				return;
			}

			if (window.XMLHttpRequest)
			{
				data[data.length] = "cat=" + cat;
				data[data.length] = "uid=" + uid;
				data[data.length] = "sesc=" + escape(textToEntities(sesc));
				data[data.length] = "desc=" + escape(textToEntities(desc));
				data[data.length] = "name=" + escape(textToEntities(name));
				text = data.join("&");
				sendXMLDocument("xml.php?mode=chatroomsput", text, onDocRoomCreate);
			}
		}

		function onDocRoomCreate(XMLDoc)
		{
			if(ChatXmlError(XMLDoc)) return ;
			window.status = "Room " + document.forms.FormRoom.RoomName.value + " created...";
			document.forms.FormRoom.RoomName.value = "";
			document.forms.FormRoom.RoomDesc.value = "";
			ChatListCat();
		}			
		
		function ChatDeleteRoom()
		{
			if(rlist.selectedIndex == -1)
			{
				alert("Select a room");
				rlist.focus();
				return;
			}
			roomid = rlist.options[rlist.selectedIndex].value;

			var data = new Array();
			var text = "";
			if (window.XMLHttpRequest)
			{
				data[data.length] = "rid=" + roomid;
				data[data.length] = "uid=" + uid;
				data[data.length] = "sesc=" + escape(textToEntities(sesc));
				text = data.join("&");
				sendXMLDocument("xml.php?mode=chatroomscut", text, onDocRoomDelete);
			}
		}

		function onDocRoomDelete(XMLDoc)
		{
			if(ChatXmlError(XMLDoc)) return ;
			window.status = "Room " + document.forms.FormRoom.RoomName.value + " deleteted...";

			document.forms.FormRoom.RoomName.value = "";
			document.forms.FormRoom.RoomDesc.value = "";
			ChatListCat();
		}		

		function ChatSelectRoom()
		{
			var rlist = document.getElementById("RoomList");
			if(rlist.selectedIndex == -1)
			{
				alert("No room selected!");
				return;
			}
			roomid = rlist.options[rlist.selectedIndex].value;
			OpenPublicRoom(roomid);
		}

	// ]]></script>';
*/

var http_request = false;
   
function makeRequest(url, parameters) 
{
	http_request = false;
	if (window.XMLHttpRequest)  // Mozilla, Safari,...
	{
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) 
		{
			http_request.overrideMimeType('text/xml');
		}
	} 
	else if (window.ActiveXObject)  // IE
	{
		try 
		{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try 
			{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) 
			{}
		}
	}
	
	if (!http_request) 
	{
		alert('Cannot create XMLHTTP instance');
		return false;
	}

	http_request.onreadystatechange = alertContents;
	http_request.open('GET', url + parameters, true);
	http_request.send(null);
}

function alertContents() 
{
	if (http_request.readyState == 4) 
	{
		alert('got: ' + http_request.readyState);
		if (http_request.status == 200 || http_request.status == 304) 
		{
			var xmldoc = http_request.responseXML;
			var root = xmldoc.getElementsByTagName('root').item(0);
			alert(http_request.responseText);
			for (var iNode = 0; iNode < root.childNodes.length; iNode++) 
			{
				var node = root.childNodes.item(iNode);
				
				for (i = 0; i < node.childNodes.length; i++) 
				{
					var sibl = node.childNodes.item(i);
					var len = parseInt(sibl.childNodes.length / 2);
					var arr = new Array(len);
					var cnt = 0;

					for (x = 0; x < sibl.childNodes.length; x++) 
					{
						var sibl2 = sibl.childNodes.item(x);
						var sibl3;
						if (sibl2.childNodes.length > 0) 
						{
							sibl3 = sibl2.childNodes.item(0);
							arr[cnt] = sibl3.data;   
							cnt++;
						}
					}
					addrow("mytable", arr);
				}
			}
		} 
		else 
		{
			alert('There was a problem with the request.');
		}
	}
}

function do_xml() 
{
	makeRequest('/sites/inc/', '?test=2');
}

function addrow(tablename, arr) 
{
	var tbl = document.getElementById(tablename);
	var lastRow = tbl.rows.length;
	var row = tbl.insertRow(lastRow);
	for (r = 0; r < arr.length; r++) 
	{		
		var cell = row.insertCell(r);
		cell.innerHTML = arr[r];
	}
}
/*
function makeRequest(url, parameters) 
{
	http_request = false;
	if (window.XMLHttpRequest)  // Mozilla, Safari,...
	{
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) 
		{
			http_request.overrideMimeType('text/xml');
		}
		} 
		else if (window.ActiveXObject) // IE
		{
			try 
			{
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} 
			catch (e) 
			{
				try 
				{
					http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} 
				catch (e) {}
			}
		}
		if (!http_request) 
		{
			alert('Cannot create XMLHTTP instance');
			return false;
		}

		http_request.onreadystatechange = alertContents;
		http_request.open('GET', url + parameters, true);
		http_request.send(null);
}

function alertContents() {
  if (http_request.readyState == 4) {
	 if (http_request.status == 200) {

		var xmldoc = http_request.responseXML;
		var root = xmldoc.getElementsByTagName('root').item(0);

		for (var iNode = 0; iNode < root.childNodes.length; iNode++) {
		   var node = root.childNodes.item(iNode);
		
		   for (i = 0; i < node.childNodes.length; i++) {
			  var sibl = node.childNodes.item(i);
			  var len = parseInt(sibl.childNodes.length / 2);
			  var arr = new Array(len);
			  var cnt = 0;
			  for (x = 0; x < sibl.childNodes.length; x++) {
				 var sibl2 = sibl.childNodes.item(x);
				 var sibl3;
				 if (sibl2.childNodes.length > 0) {
					sibl3 = sibl2.childNodes.item(0);
					arr[cnt] = sibl3.data;   
					cnt++;
				 }
			  }
			  addrow("mytable", arr);
		   }
		}
		
	 } else {
		alert('There was a problem with the request.');
	 }
  }
}
function do_xml() 
{
	makeRequest('xml.php', '?test=2');
}

function addrow(tablename, arr) 
{
	var tbl = document.getElementById(tablename);
	var lastRow = tbl.rows.length;
	var row = tbl.insertRow(lastRow);

	for (r = 0; r < arr.length; r++) 
	{   
		var cell = row.insertCell(r);
		cell.innerHTML = arr[r];
	}
}

*/