cli.lib.namespace("cli.ajax");

cli.ajax.contexts = new Array();
cli.ajax.lastcontext = false;

cli.ajax.stopall = function () {

	for (key in cli.ajax.contexts) {
		cli.ajax.iscontext(key);
	}

}

cli.ajax.deletecontext = function (context) {
	//cli.gui.outln('Error: Operation timed out. ' + context);
	//if (!document.all) cli.gui.outln(cli.gui.less('If you use the noscript firefox-extension, add "ajax.googleapis.com" to the whitelist.'));
	//cli.gui.outln('');
	cli.ajax.contexts[context] = false;

	var d = document.getElementById(context);
	if (d) document.body.removeChild(d);

	cli.gui.showinput();
	cli.gui.focusinput();
	cli.gui.scroll();
	if (!document.all) stop();

}

cli.ajax.iscontext = function (name) {

	if (cli.ajax.contexts[name]) {

		clearTimeout(cli.ajax.contexts[name]);
		cli.ajax.contexts[name] = false;

		var d = document.getElementById(name);
		if (d) document.body.removeChild(d);

		return true;
	}
	else return false;

}

cli.ajax.getcontext = function (name) {
	var d = new Date();
	var context = d.getTime();
	if (name) context = name;

	cli.ajax.contexts[context] = setTimeout("cli.ajax.deletecontext('" + context + "');", 1000 * cli.config.timeout);

	return context;
}

cli.ajax.query = function (url, nohide) {
	var context = "none";
	if (!nohide) {
		context = cli.ajax.getcontext();
		cli.ajax.lastcontext = context; // more elegant with return, but doesnt work in opera
		cli.gui.hideinput();
	}

	var script = document.createElement("script");
	document.body.appendChild(script);
	script.src = url + '&context=' + context + '&';
	script.id = context;

}
