var TinyAJAX = {
	_settings : new Array(),

	init : function(s) {
		var n;

		this._settings = {
			remote_url : "ajax_test.aspx"
		};

		for (n in s)
			this._settings[n] = s[n];
	},

	execCommand : function(command, args, callback, cs) {
		var st = "", n, s = this._settings, ns;

		for (n in args)
			st += this.escape(n) + "=" + this.escape(args[n]) + "&";

		st += "&command=" + this.escape(command);

		if (st.charAt(0) == '&')
			st = st.substring(1);

		if (typeof(cs) != "undefined") {
			ns = new Array();

			for (n in s)
				ns[n] = s[n];

			for (n in s)
				ns[n] = cs[n];

			s = ns;
		}

		this.sendAJAX(s.remote_url, callback, 'POST', st);
	},

	escape : function(s) {
		return escape(s).replace(/\+/g, '%2B');
	},

	unescape : function(s) {
		return unescape(s).replace(/\+/g, ' ');
	},

	getAJAXHTTP : function() {
		try {
			return new ActiveXObject('Msxml2.XMLHTTP')
		} catch (e) {
			try {
				return new ActiveXObject('Microsoft.XMLHTTP')
			} catch (e) {
				return new XMLHttpRequest();
			}
		}

		return null;
	},

	onLoadIframe : function() {
		var ie = document.getElementById('__tinyajax');
		var d = !ie.contentDocument ? frames['__tinyajax'].document : ie.contentDocument;

		TinyAJAX._callback(d.body.innerHTML, null);
	},

	addEvent : function(o, n, h) {
		if (o.attachEvent)
			o.attachEvent("on" + n, h);
		else
			o.addEventListener(n, h, false);
	},

	sendAJAX : function(u, f, m, a) {
		var x = this.getAJAXHTTP(), d = document, ie;

		// Use fallback
		if (x == null) {
			ie = d.getElementById('__tinyajax');

			if (!ie) {
				ie = d.createElement('iframe');
				ie.id = '__tinyajax';
				ie.style.display = 'none';
				ie.src = u;
				this.addEvent(ie, 'load', TinyAJAX.onLoadIframe);
				d.body.appendChild(ie);

				this._callback = f;
			}

			return;
		}

		x.open(m, u, true);

		x.onreadystatechange = function() {
			if (x.readyState == 4)
				f(x.responseText, x.responseXML);
		};

		if (m == 'POST')
			x.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

		x.send(a);
	},

	parseQueryString : function(s) {
		var c = s.replace(/^\s*|\s*$/g, '').split('&'), v, i, o = {};

		for (i=0; i<c.length; i++) {
			if (c[i].length > 0) {
				v = c[i].split('=');

				if (v.length > 1)
					o[this.unescape(v[0])] = this.unescape(v[1]);
			}
		}

		return o;
	}
};

TinyAJAX.init({}); // Preinit

/**
 * Example:
 *
 * TinyAJAX.execCommand("dostuff", {myarg1 : 3, myarg2 : 2}, function(xml, text) {alert(xml);alert(text)});
 */
