window.jQuery = window.jQuery || {};
jQuery.rpc = function(url, dataType, onLoadCallback, version, methods) {
return new (function(url, dataType, onLoadCallback, version) {
version = version || "1.0";
dataType = dataType || "json";
if(dataType != "json" && dataType != "xml") {
new Error("IllegalArgument: Unsupported data type");
}
var _self = this;
function pad2(f) {
if(f<=9) {
return "0"+f;
} else {
return ""+f;
}
}
var serializeToXml = function(data) {
switch (typeof data) {
case 'boolean':
return ''+ ((data) ? '1' : '0') +'';
case 'number':
var parsed = parseInt(data);
if(parsed == data) {
return ''+ data +'';
}
return ''+ data +'';
case 'string':
return ''+ data +'';
case 'object':
if(data instanceof Date) {
return ''+ data.getFullYear() + pad2(data.getMonth()) + pad2(data.getDate()) +'T'+ pad2(data.getHours()) +':'+ pad2(data.getMinutes()) +':'+ pad2(data.getSeconds()) +'';
} else if(data instanceof Array) {
var ret = ''+"\n";
for (var i=0; i < data.length; i++) {
ret += ' '+ serializeToXml(data[i]) +"\n";
}
ret += '';
return ret;
} else {
var ret = ''+"\n";
jQuery.each(data, function(key, value) {
ret += " "+ key +"";
ret += serializeToXml(value) +"\n";
});
ret += '';
return ret;
}
}
}
var xmlRpc = function(method, params) {
var ret = ''+method+'';
for(var i=0; i"+serializeToXml(params[i])+"";
}
ret += "";
return ret;
}
var parseXmlValue = function(node) {
var jnode = jQuery(node);
childs = jnode.children();
// String not enclosed in a tag
if(childs.length==0) {
var s="";
for(var j=0; j data > value", childs[i]).each(
function() {
ret.push(parseXmlValue(this));
}
);
return ret;
case "struct":
var ret = {};
jQuery("> member", childs[i]).each(
function() {
ret[jQuery( "> name", this).text()] = parseXmlValue(jQuery("value", this));
}
);
return ret;
case "dateTime.iso8601":
/* TODO: fill me :( */
return 0;
}
}
}
var parseXmlResponse = function(data) {
var ret = {};
ret.version = version;
jQuery("methodResponse params param > value", data).each(
function(index) {
ret.result = parseXmlValue(this);
}
);
jQuery("methodResponse fault > value", data).each(
function(index) {
ret.error = parseXmlValue(this);
}
);
return ret;
}
var rpc_contents = {
'xml':'text/xml'
,'json':'application/json'
};
var _rpc = function(method) {
var params = [];
var async; // whether we're async or not
var callback;
var nargs=arguments.length;
if(nargs > 1 && typeof(arguments[nargs-1])=="function") {
callback = arguments[nargs-1];
nargs=nargs-1;
async=true;
} else {
async=false;
}
for (var i=1; i