/* vim:ts=4:sts=4:sw=2:noai:noexpandtab
 *
 * JavaScript Remoting.
 * Copyright (c) 2005 Steven McCoy <fnjordy@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

function jsr_status() {
	if (arguments.length > 0)
		this.obj = document.getElementById(arguments[0]);
	else
		this.obj = document.getElementById('jsr_status');
}

jsr_status.prototype.clear = function() {
	this.obj.style.display = 'none';
}

jsr_status.prototype.loading = function() {
	this.obj.style.display = 'block';
	this.obj.innerHTML = 'loading ...';
}

jsr_status.prototype.error = function(e) {
	this.obj.innerHTML = e;
}


function JSR() {
	if (arguments.length > 0)
		this.status = arguments[0];
	else
		this.status = new jsr_status();

/* iframe for non-XmlHttpRequest() browsers */
	this.iframe = null;

/* update extern mapping array for rpc reply */
	this.id = _jsr_map_add(this);
}

/* helper functions for webserver rpc processing */
var _jsr_map = new Array();
function _jsr_map_add(jsr) {
	var id = _jsr_map.length;
	_jsr_map[id] = jsr;
	return id;
}

JSR.prototype.send_request = function(url) {
/* add thunking id */
	if (url.indexOf('?') == -1)
		url += '?jsr=' + this.id;
	else
		url += '&jsr=' + this.id;

/* initiate new call */
	if (this.iframe == null) {
		var request = this.XMLHttpRequest();
		if (request == null) {
			var iframe = document.createElement('IFRAME');
			iframe.src = url + '&jsri=1';
			iframe.style.width = '0px';
			iframe.style.height = '0px';
			this.iframe = document.body.appendChild(iframe);
/* get focus here */
			window.focus();
		} else {
/* send XmlHttpRequest */
			var JSR = this;
			request.onreadystatechange = function() {
				if (request.readyState == 4) {
					try {
						if (request.status != 200) {
/* no http ok status */
							JSR.status.error('http error: '+request.status);
						} else if (request.responseText.charAt(0) == '<') {
/* some server side error */
							alert(request.responseText);
						} else {
							eval(request.responseText);
						}
					} catch (e) {}
				}
			}
			request.open("GET", url);
			request.send(null);
		}
	} else {
/* re-submit iframe */
		this.iframe.src = url + '&jsri=1';
/* get focus here */
		window.focus();
	}

	this.status.loading();
}

/* create object for rpc call */
JSR.prototype.XMLHttpRequest = function() {
	var request = null;
	if (typeof XMLHttpRequest != 'undefined') {
		request = new XMLHttpRequest();
	} else {
		try {
			request = new ActiveXObject('Msxml2.XMLHTTP')
		} catch(e) {
			try {
				request = new ActiveXObject('Microsoft.XMLHTTP')
			} catch(e) {
				request = null
			}
		}
	}
	return request;
}

/* iframe or XmlHttpRequest() callback */
function _jsr_rpc() {
	var id = arguments[0];
	if (_jsr_map[id]) {
		_jsr_map[id].status.clear();

/* we cannot shift() arguments as it is an object :( */
		var newarguments = Array();
		if (arguments.length > 1)
			for (i = 1; i < arguments.length; i++)
				newarguments[i-1] = arguments[i];
		_jsr_map[id].reply.apply(_jsr_map[id],newarguments);
	}
}

JSR.prototype.reply = function() {
}


