var sessid = ''; var __jssep = /\n--js--\n/;

function SItem(what, seqno, done, target, sel)
{
	this.what = what;
	this.seqno = seqno;
	this.done = done;
	this.target = target;
	this.sel = sel;
	this.next = null;
}

__xmlhttp = null;
__sendurl = "index.php";

__seqno = (new Date()).getTime();
__queue = null;
__qtail = null;
__ajax = true;

function __addQueue(what, seqno, done, target, sel)
{
	if(!__ajax)
	{
		return true;
	}
	var busy = __queue;
	var item = new SItem(what, seqno, done, target, sel);
	if(__queue)
	{
		__qtail.next = item;
	}
	else
	{
		__queue = item;
	}
	__qtail = item;
	if(!busy)
	{
		var r = __sendData(item.what, item.seqno, item.done);
		if(r)
		{
			__PopQueue();
			return true;
		}
	}
	return false;
}

function __popQueue()
{
	var item = __queue;
	if(item)
	{
		__queue = __queue.next;
		if(!__queue)
		{
			__qtail = null;
		}
	}
	return item;
}

function __LoadNext()
{
	if(__queue)
	{
		var item = __queue;
		__sendData(item.what, item.seqno, item.done);
	}
}

/* the functionality of sendData may in the future be replaced with
 * code that complies with the W3C proposed DOM level 3 "Load and Save".
 *
 * Currently this is not supported in any browser, hence use the non-standard
 * XMLHttpRequest
 *
 */

function __sendData(what, seqno, done)
{
	var request = what;
	if(seqno)
	{
		request += "&seq=" + seqno;
	}
	request += "&ajax=1";
	// code for Mozilla, etc.
	if(window.XMLHttpRequest)
	{
		__xmlhttp = new XMLHttpRequest();
		__xmlhttp.onreadystatechange = __done;
		__xmlhttp.open("GET", request, true);
		__xmlhttp.send(null);
	}
	// code for IE
	else if(window.ActiveXObject)
	{
		__xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		if(__xmlhttp)
		{
			__xmlhttp.onreadystatechange = __done;
			__xmlhttp.open("GET", request, true);
			__xmlhttp.send();
		}
	}
	else
	{
// alert("No XMLHttpRequest");
		return true;
	}
	return false;
}

function __done()
{
	if(__xmlhttp.readyState == 4)
	{
		var item = __queue;
		if(__xmlhttp.status == 200)
		{
			var item = __popQueue();
			item.text = __xmlhttp.responseText;
			item.done(item);
		}
		else
		{
			__popQueue();
			// alert("Problem retrieving XML data " + __xmlhttp.status);
		}
		__LoadNext();
	}
}

function __xmlhttpPop(item)
{
	var title = "Title";
	var requires = "";
	var txt = item.text;
	if(txt.match(/^<!--/))
	{
		var idx = txt.indexOf('\n');
		if(idx)
		{
			requires = txt.substr(0, idx);
			txt = txt.substr(idx);
		}
	}
	title = __addReq(requires);
// lg("title = " + title + " text = " + txt);
	window.popup(title, txt);
}

function __xmlhttpPage(item)
{
	var j = item.text.split(__jssep);
	if(j[0] || j[1])
	{
		if(j[0] && j[0] != " ")
		{
			item.target.innerHTML = j[0];
			item.target.style.visibility = "visible";
		}
		if(j[1])
		{
			try
			{
				eval(j[1]);
			} 
			catch(aa)
			{
				alert(aa + " in " + j[1]);
			}
		}
	}
	else
	{
		item.target.style.visibility = "hidden";
	}
}

function __xmlhttpAny(item)
{
	var caller = item.target;
	var j = item.text.split(__jssep);
	for(var i = 0; i < j.length; i++)
	{
		var m = j[i].match(/([^=]*)=(.*)/);
		if(m)
		{
			var what = m[1];
			var text = m[2].replace(/<BR>/, "\n");
			switch(what)
			{
				case 'code':
					eval(text);
					break;
				case 'body':
					caller.setBody(text);
					break;
				default:
					var elem = document.getElementById(what);
					if(elem)
					{
						elem.innerHTML = text;
						elem.style.visibility = "visible";
					}
					break;
			}
		}
	}
}

function __xmlhttpCode(item)
{
	var txt = item.text;
	var j = txt.split(__jssep);
	var a = j[0].split(/[\r\n]/);
	var cclass = a.shift();
	var msg = a.shift();
	txt = a.join("\n");
	item.target.innerHTML = txt;
	var elem = document.getElementById("msgdiv");
	if(elem)
	{
		elem.innerHTML = msg;
	}
	if(cclass)
	{
		elem = document.getElementById("ndat");
		if(elem)
		{
			elem.className = cclass;
		}
	}
	note.hidetip();
	if(j[1])
	{
		eval(j[1]);
	}
}

function __xmlhttpUser()
{
	var txt = item.text;
	if(txt)
	{
		var j = txt.split(__jssep);
		var a = j[0].split(/[\r\n]/);
		for(var i = 0; i < a.length; i++)
		{
			var b = txt.split(/,/);
			item.sel.options[i + 1] = new Option(b[0], b[1], false, false);
		}
		item.target.style.visiblity = "visible";
		if(j[1])
		{
			eval(j[1]);
		}
	}
	else
	{
		item.target.style.visiblity = "hidden";
	}
}

function load(what)
{
	return __sendData(what, ++__seqno, __xmlhttpPage);
}

function resRequest(what, target)
{
	return __addQueue(what, ++__seqno, __xmlhttpCode, target);
}

function roomRequest(what, target)
{
	return __addQueue(what, ++__seqno, __xmlhttpPage, target);
}

function fillUserRequest(what, sel, target)
{
	return __addQueue(what, ++__seqno, __xmlhttpUser, target, sel);
}

function __xmlhttpAction(item)
{
        var txt = item.text;
// System.lg("text = " + txt);
// alert("Got : " + txt);
        if(txt)
        {
                var target = item.target;
try
{
                eval(txt);
}
catch(x) { alert(x); }
                target.wordloaded();
        }
}

function __request(cmd, target)
{
        __addQueue("action.php?" + cmd, __seqno++, __xmlhttpAction, target);
}

function __getwords(text, element)
{
        __request("cmd=getwords&start=" + text, element);
}

function getAny(cmd, caller)
{
	return __addQueue(cmd, __seqno++, __xmlhttpAny, caller);
}


function ajax(request, target)
{
	if(target)
	{
		var p = ajax.queue;
		while(p)
		{
			if(p.target == target)
			{
				p.request = request;
				return;
			}
			p = p.next;
		}
	}
	this.target = target;
	this.id = ajax.id++;
	this.sending = false;
	if(request.match(/\?/))
	{
		request += "&AJID=" + this.id;
	}
	this.request = request;
	ajax.append(this);
	ajax.trySend();
}

ajax.id = (new Date()).getTime();
ajax.queue = null;
ajax.tail = null;
ajax.jssep = /\n--js--\n/;

ajax.append = function(item)
{
	item.next = null;
	if(ajax.tail)
	{
		ajax.tail.next = item;
	}
	else
	{
		ajax.queue = item;
	}
	ajax.tail = item;
}

ajax.pop = function()
{
	var item = ajax.queue;
	if(item)
	{
		ajax.queue = ajax.queue.next;
		if(!ajax.queue)
		{
			ajax.tail = null;
		}
		item.next = null;
	}
	return item;
}

ajax.trySend = function()
{
	if(ajax.queue)
	{
		if(!ajax.queue.sending)
		{
			ajax.queue.send();
		}
	}
}

ajax.done = function()
{
	var item = ajax.queue;
	if(item)
	{
		if(item.xmlhttp.readyState == 4)
		{
			if(item.xmlhttp.status == 200)
			{
				item.done();
			}
			else
			{
				// alert("Problem retrieving XML data " + item.xmlhttp.status);
			}
			ajax.pop();
			// send next request (if any)
			ajax.trySend();
		}
	}
}

ajax.prototype.send = function()
{
	this.sending = true;
	if(window.XMLHttpRequest)
	{
		this.xmlhttp = new XMLHttpRequest();
		this.xmlhttp.onreadystatechange = ajax.done;
		this.xmlhttp.open("GET", this.request, true);
		this.xmlhttp.send(null);
	}
	// code for IE
	else if(window.ActiveXObject)
	{
		this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		if(this.xmlhttp)
		{
			this.xmlhttp.onreadystatechange = ajax.done;
			this.xmlhttp.open("GET", this.request, true);
			this.xmlhttp.send();
		}
	}
	else
	{
// alert("No XMLHttpRequest");
		return true;
	}
	return false;
}

ajax.prototype.done = function()
{
	var caller = this.target;
	var j = this.xmlhttp.responseText.split(ajax.jssep);
	for(var i = 0; i < j.length; i++)
	{
		var m = j[i].match(/([^=]*)=(.*)/);
		if(m)
		{
			var what = m[1];
			var text = m[2].replace(/<BR>/, "\n");
			switch(what)
			{
				case 'OK':
					// do nothing
					break;
				case 'code':
					eval(text);
					break;
				case 'dialog':
					movable.getDialog(text);
					break;
				case 'body':
					caller.setBody(text);
					break;
				default:
					if(what.match(/^set/) || what.match(/^confirm/))
					{
						if(caller[what])
						{
							caller[what](text);
						}
						else
						{
							alert("no such method: " + what);
						}
					}
					else
					{
						var elem = document.getElementById(what);
						if(elem)
						{
							elem.innerHTML = text;
							elem.style.visibility = "visible";
						}
						else
						{
							alert("no such element: " + what);
						}
					}
					break;
			}
		}
	}
}

