var x54ajax = {
	xhistory: [],
	xencode: function(args) {
		var data = "";
		for (var arg in args) {
			if (data.length > 1) {data += ",";}
			data += arg + ":" + this.base64encode(this.utf8encode(args[arg]));
		}
		return data;
	},
	xcall: function(uri, args, callback, max) {
		//uri = "http://www.nethaggler.com/" + uri;
		var id = 'x' + Math.random().toString().substr(2);
		max = max || 1024;
		var data = this.xencode(args);
		var len = data.length;
		var count = Math.floor((len+max-1)/ max);
		this.xhistory[id] = {callback:callback, scripts:[]};
		var cur = 0;
		var script;
		for (var n = 0; n < len; n += max) {
			script = document.createElement('script');
			script.src = uri + "?id=" + id + "&idx=" + cur + "&total=" + count + "&callback=x54ajax.xcallback&data=" + data.substr(n, max);
			//alert(uri + "?id=" + id + "&idx=" + cur + "&total=" + count + "&callback=x54ajax.xcallback&data=" + data.substr(n, max));
			script.type = 'text/javascript';
			script.charset = 'utf-8';
			script = document.getElementsByTagName('head')[0].appendChild(script);
			this.xhistory[id].scripts.push(script);
			cur++;
		}
	},
	xcallback: function(id, res) {
		var callback = this.xhistory[id].callback;
		var scripts = this.xhistory[id].scripts;
		for(var i = 0; i < scripts.length; i++) {
			if(scripts[i].parentNode) {
				//scripts[i].parentNode.removeChild(scripts[i]);
			}
		}
		//this.xhistory[id] = null;
		//delete this.xhistory[id];
		callback(res);
	},
	base64encode: function(input) {
		var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
		var output = "", chr1, chr2, chr3, enc1, enc2, enc3, enc4, i = 0;
		input = input + ["", "  ", " "][input.length % 3];
		do {
			chr1 = input.charCodeAt(i++) & 0xff;
			chr2 = input.charCodeAt(i++) & 0xff;
			chr3 = input.charCodeAt(i++) & 0xff;
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
			output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
		} while (i < input.length);
		return output;
	},
	utf8encode: function(input) {
		if ('string' != typeof input) { return '';}
		input = input.replace(/\r\n/g,"\n");
		var output = "";
		var c;
		for(var n = 0; n < input.length; n++) {
			c = input.charCodeAt(n);
			if('null' != typeof c) {
			if (c < 128) {
				output += String.fromCharCode(c); }
			else if((c > 127) && (c < 2048)) {
				output += String.fromCharCode((c >> 6) | 192);
				output += String.fromCharCode((c & 63) | 128); }
			else {
				output += String.fromCharCode((c >> 12) | 224);
				output += String.fromCharCode(((c >> 6) & 63) | 128);
				output += String.fromCharCode((c & 63) | 128);}
			}
		}
		return output;
	}
};