/* (c) 2009 Jean Luc BIELLMANN - EES - contact@adressesetservices.com */

/* network.http.max-connections-per-server */

function JSON () {
	this.url = 'index.php?json=true&';
	this.method = 'post';
	this.asynchronous = false; // if false, lock the script and wait the response 
	this.formid = '';
	this.onSuccess = function (json) {};
}
function onSuccessDefault (json) {
	if (json.status)
		I($('status'),json.status);
	if (json.warn && json.warn.length) 
		_warn.upd("Erreur",json.warn);
	if (json.info && json.info.length) 
		_info.upd("Erreur",json.info);	
}
JSON.prototype = {
	send : function (args) {
		try {
			//alert('index.php?'+args);
			var now = new Date();
			document.body.style.cursor='wait';
			new Ajax.Request(this.url+args+'&now='+now.getTime(), {
				method: this.method,
				asynchronous : this.asynchronous, 
				parameters: (this.formid.length ? $(this.formid).serialize() : ''),
				onSuccess: (function(transport) {
					document.body.style.cursor='default';
					var json = transport.responseText.evalJSON();
					onSuccessDefault(json);
					this.onSuccess(json);
				}).bind(this),
				onFailure: function(transport) {
					document.body.style.cursor='default';
					I($('failure'),transport.responseText);
				},
				onException: function(request, exception) {
					document.body.style.cursor='default';
				}
			}); // end Ajax.Request
		} catch (e) {
			I($('status'),e);
		}
	}
}
var _json = new JSON();
