/*
Copyright 2009 Sleepless Software Inc.  All Rights Reserved

Example:

	var foo = document.getElementById('foo');
	function get_foo()
	{
		var f = function(a) {
			foo.value = a.result;
			foo.style.backgroundColor = '#fff';
		}
		comet("x.php?act=get_foo", f, null);
	}
	function set_foo()
	{
		var f = function(a) {
			setTimeout('get_foo()', 10);
		}
		comet("x.php?act=set_foo&val="+escape(foo.value), f);
		foo.style.backgroundColor = '#ddd';
	}
	get_foo();


Usage:

	url = The URL  to summon, including any query args. Example: "x.php?foo=bar"
	okfunc = function to call if all goes well
	errfunc = [optional] function to call otherwise

	An asynchronous AJAX style request will be made to the server using the given URL.

	If errfunc is not provided, then a default function will be used which presents an
	alert dialog with the error and explanation.

	The response from the server is expected to be in a certain format.
	Namely, some text that looks something like this:

		code explanation\n
		result
	
	An example of a successful response might look something like this:

		0 ok\n
		Some useful foo\n
		for you.\n

	An example of a failed response might look like this:

		1 Access denied\n
		You do not possess adequate clearance to receive the requested foo\n 

	The okfunc will be called if the HTTP request receives a "200 OK" response from 
	the server and the code in the response text is 0.  Otherwise, the errfunc will be 
	called.

	A single argument is supplied to the function in both cases.  The argument consists
	of an object containing these member variables:

		ok
			Always true for okfunc.
			Always false for errfunc.
		code
			Contains HTTP response code (404, etc.) if the server responded with something
			other than 200.  Otherwise, contains the code number from the response text.
		explanation
			Contains the explanation from the response text
		result
			Contains the result from the response text.

*/
function comet(url, okfunc, errfunc)
{
	if(!errfunc)
		errfunc = function(a) { alert(a.explanation+"\n{"+a.code+"}\n"+a.result+"\n"); }

	try {
		if(typeof ActiveXObject != "undefined")
			this.request = new ActiveXObject("Microsoft.XMLHTTP");	// ie
		else
		if(window.XMLHttpRequest)
			this.request = new XMLHttpRequest();		// firefox
		else
			alert("Error: Unable to construct AJAX request!");
	} catch(exc) { alert("what?"); }

	request.open("GET", url, true);
	request.onreadystatechange = function()
		{
			if(request.readyState != 4)
				return;
			var c = parseInt(request.status);
			var t = request.responseText;
			var a = { ok: false, code: c, explanation: 'Communication error', result: t, };
			if(c == 200)
			{
				a.code = 1;
				a.explanation = 'Unknown error';
				var m = t.match(/^(\d+) ([^\n]+)\n([^$]*)/);
				if(m)
				{
					a.code = parseInt(m[1]);
					a.explanation = m[2];
					a.result = m[3];
					a.ok = (a.code == 0);
				}
			}
			if(a.ok)
				okfunc(a);
			else
				errfunc(a);
			request.onreadystatechange = function() {}
		};
	request.send(null);
}


