function JSONRPCMethod(url,method) {
	if (arguments.length == 1) {
		var j = url.indexOf('#');
		if (j == -1) throw "Usage: new JSONRPCMethod(\"http://site/json.cgi#method\");";
		method = url.substr(j+1,url.length-(j+1));
		url = url.substr(0, j);
	}
	if (method.indexOf('(') > -1) {
		method = method.substr(0, method.indexOf('('));
		if (method == '') throw "Usage: new JSONRPCMethod(\"http://site/json.cgi#method\");";
	}
	var m = {
		'\b': '\\b',
		'\t': '\\t',
		'\n': '\\n',
		'\f': '\\f',
		'\r': '\\r',
		'"' : '\\"',
		'\\': '\\\\'
	};
        var s = {
		array: function (x) {
			var a = ['['], b, f, i, l = x.length, v;
			for (i = 0; i < l; i += 1) {
				v = x[i];
				f = s[typeof v];
				if (f) {
					v = f(v);
					if (typeof v == 'string') {
						if (b) a[a.length] = ',';
						a[a.length] = v;
						b = true;
					}
				}
			}
			a[a.length] = ']';
			return a.join('');
		},
		'boolean': function (x) {
			return String(x);
		},
		'null': function (x) {
			return "null";
		},
		number: function (x) {
			return isFinite(x) ? String(x) : 'null';
		},
		object: function (x) {
			if (x) {
				if (x instanceof Array || typeof x == 'array') {
					return s.array(x);
				}
				var a = ['{'], b, f, i, v;
				for (i in x) {
					v = x[i];
					f = s[typeof v];
					if (f) {
						v = f(v);
						if (typeof v == 'string') {
							if (b) a[a.length] = ',';
							a[a.length]=s.string(i);
							a[a.length]=':';
							a[a.length]=v;
							b = true;
						}
					}
				}
				a[a.length] = '}';
				return a.join('');
			}
			return 'null';
		},
		string: function (x) {
			if (/["\\\x00-\x1f]/.test(x)) {
				x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
					var c = m[b];
					if (c) {
						return c;
					}
					c = b.charCodeAt();
					return '\\u00' +
						Math.floor(c / 16).toString(16) + (c % 16).toString(16);
						
				});
			}
			return '"' + x + '"';
		}
	};

	function parseJSON(x) {
		var z;
		if (x=='') return z;
		try {
			eval('z='+x);
		} catch(e) {
			throw e;
		};
		return z;
	}

	var f = function() {
		var i, a = [];
		a[0] = method;
		for (i = 0; i < arguments.length; i++) {
			a[i+1] = arguments[i];
		}
		var ser = s.array(a);
                var x=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
 try {
  x = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   x = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   x = false;
  }
 }
@end @*/
                if (!x && typeof XMLHttpRequest!='undefined') {
                        x = new XMLHttpRequest();
                }

		var async = arguments.callee.onreturn||arguments.callee.onReturn||arguments.callee.onreply||arguments.callee.onReply||undefined;
		var errup = arguments.callee.onerror||arguments.callee.onError;
		if (!async) {
			if (errup) {
				try {
					x.open('POST', url, false);
					x.send(ser);
					if (x.readyState != 4) return false;
					if (x.status && x.status != 200) throw ''+x.status+' '+x.statusText;
					var txt = x.responseText+'';
					return parseJSON(txt);
				} catch(e) {
					errup(e);
				};
				return false;
			}
			x.open('POST', url, false);
			x.send(ser);
			if (x.readyState != 4) return false;
			if (x.status && x.status != 200) throw ''+x.status+' '+x.statusText;
			var txt = x.responseText+'';
			return parseJSON(txt);
		}
		if (!errup) {
			errup = function(e) {
				window.alert(e);
				throw e;
			};
		}
		(function(casync,cerrup) {
			try {
				x.open('POST', url, true);
				x.onreadystatechange = function() {
					try {
						if (x.readyState != 4) return;
						if (x.status && x.status != 200) throw ''+x.status+' '+x.statusText;
						var txt = x.responseText+'';
						casync(parseJSON(txt));
					} catch(e) {
						cerrup(e);
					};
				};
				x.send(ser);
			} catch(e) {
				cerrup(e);
			};
		})(async,errup);
		return true;
	};
	f.full_url = url + '#' + method;
	f.toSource = function() { return '[JSONRPCMethod ' + this._full_url + ']'; };
	f.toValue = f.toSource;
	f.valueOf = f.toSource;
	f.url = url;
	f.method = method;
	return f;
}
window['httpmail'] = {};
window['httpmail'].feed = {};
window['httpmail'].config = {};
window['httpmail'].async = {};
window['httpmail'].mailbox = {};
window['httpmail'].message = {};
window['httpmail'].calendar = {};
window['httpmail'].contacts = {};
window['httpmail'].calendar.subscribe = new JSONRPCMethod('backend.cgi', 'calendar.subscribe');
window['httpmail'].config.get = new JSONRPCMethod('backend.cgi', 'config.get');
window['httpmail'].expunge = new JSONRPCMethod('backend.cgi', 'expunge');
window['httpmail'].message.setFlags = new JSONRPCMethod('backend.cgi', 'message.setFlags');
window['httpmail'].mailbox.listingJAVA = new JSONRPCMethod('backend.cgi', 'mailbox.listingJAVA');
window['httpmail'].message.fetchTHUMBNAIL = new JSONRPCMethod('backend.cgi', 'message.fetchTHUMBNAIL');
window['httpmail'].contacts.update = new JSONRPCMethod('backend.cgi', 'contacts.update');
window['httpmail'].feed.unsubscribe = new JSONRPCMethod('backend.cgi', 'feed.unsubscribe');
window['httpmail'].message.forwardSet = new JSONRPCMethod('backend.cgi', 'message.forwardSet');
window['httpmail'].mailbox.remove = new JSONRPCMethod('backend.cgi', 'mailbox.remove');
window['httpmail'].message.fetchHTML = new JSONRPCMethod('backend.cgi', 'message.fetchHTML');
window['httpmail'].message.forward = new JSONRPCMethod('backend.cgi', 'message.forward');
window['httpmail'].message.fetch = new JSONRPCMethod('backend.cgi', 'message.fetch');
window['httpmail'].calendar.todo = new JSONRPCMethod('backend.cgi', 'calendar.todo');
window['httpmail'].message.fetchJAVA = new JSONRPCMethod('backend.cgi', 'message.fetchJAVA');
window['httpmail'].async.listen = new JSONRPCMethod('backend.cgi', 'async.listen');
window['httpmail'].config.setPassword = new JSONRPCMethod('backend.cgi', 'config.setPassword');
window['httpmail'].send = new JSONRPCMethod('backend.cgi', 'send');
window['httpmail'].spellcheck = new JSONRPCMethod('backend.cgi', 'spellcheck');
window['httpmail'].feed.burn = new JSONRPCMethod('backend.cgi', 'feed.burn');
window['httpmail'].mailbox.moveto = new JSONRPCMethod('backend.cgi', 'mailbox.moveto');
window['httpmail'].message.markspam = new JSONRPCMethod('backend.cgi', 'message.markspam');
window['httpmail'].message.structure = new JSONRPCMethod('backend.cgi', 'message.structure');
window['httpmail'].mailbox.ping = new JSONRPCMethod('backend.cgi', 'mailbox.ping');
window['httpmail'].contacts.list = new JSONRPCMethod('backend.cgi', 'contacts.list');
window['httpmail'].contacts.create = new JSONRPCMethod('backend.cgi', 'contacts.create');
window['httpmail'].message.fetchVCARD = new JSONRPCMethod('backend.cgi', 'message.fetchVCARD');
window['httpmail'].mailbox.summary = new JSONRPCMethod('backend.cgi', 'mailbox.summary');
window['httpmail'].async.unlisten = new JSONRPCMethod('backend.cgi', 'async.unlisten');
window['httpmail'].message.reply = new JSONRPCMethod('backend.cgi', 'message.reply');
window['httpmail'].mailbox.list = new JSONRPCMethod('backend.cgi', 'mailbox.list');
window['httpmail'].calendar.fetch = new JSONRPCMethod('backend.cgi', 'calendar.fetch');
window['httpmail'].mailbox.copyto = new JSONRPCMethod('backend.cgi', 'mailbox.copyto');
window['httpmail'].mailbox.create = new JSONRPCMethod('backend.cgi', 'mailbox.create');
window['httpmail'].config.set = new JSONRPCMethod('backend.cgi', 'config.set');
window['httpmail'].calendar.unsubscribe = new JSONRPCMethod('backend.cgi', 'calendar.unsubscribe');
window['httpmail'].mailbox.messages = new JSONRPCMethod('backend.cgi', 'mailbox.messages');
window['httpmail'].message.notspam = new JSONRPCMethod('backend.cgi', 'message.notspam');
window['httpmail'].feed.subscribe = new JSONRPCMethod('backend.cgi', 'feed.subscribe');
window['httpmail'].license = new JSONRPCMethod('backend.cgi', 'license');
window['httpmail'].calendar.file = new JSONRPCMethod('backend.cgi', 'calendar.file');
window['httpmail'].message.archive = new JSONRPCMethod('backend.cgi', 'message.archive');
window['httpmail'].mailbox.post = new JSONRPCMethod('backend.cgi', 'mailbox.post');
window['httpmail'].now = new JSONRPCMethod('backend.cgi', 'now');
window['httpmail'].message.remove = new JSONRPCMethod('backend.cgi', 'message.remove');
window['httpmail'].calendar.alarms = new JSONRPCMethod('backend.cgi', 'calendar.alarms');
window['httpmail'].message.resume = new JSONRPCMethod('backend.cgi', 'message.resume');
window['httpmail'].calendar.update = new JSONRPCMethod('backend.cgi', 'calendar.update');
