function $() { var elements = new Array(); for (var i = 0; i < arguments.length; i++) { var element = arguments[i]; if (typeof element == 'string') element = document.getElementById(element); if (arguments.length == 1) return element; elements.push(element); } return elements; }

var Class = { create: function() { return function() { this.initialize.apply(this, arguments); } } }

Function.prototype.bind = function() { var __method = this, args = $A(arguments), object = args.shift(); return function() { return __method.apply(object, args.concat($A(arguments))); } }

var $A = Array.from = function(iterable) { if (!iterable) return []; if (iterable.toArray) { return iterable.toArray(); } else { var results = []; for (var i = 0; i < iterable.length; i++) results.push(iterable[i]); return results; } }

ajax = Class.create();
ajax.prototype = {
	initialize: function(url, options){
		this.transport = this.getTransport();
		this.postBody = options.postBody || '';
		this.method = options.method || 'GET';
		this.onComplete = options.onComplete || null;
		this.update = $(options.update) || null;
		this.loading = $(options.loading) || null;
		this.onError = options.onError || null;
		this.request(url);
	},

	request: function(url){
		this.transport.open(this.method, url, true);
		this.transport.onreadystatechange = this.onStateChange.bind(this);
		if (this.method == 'POST') {
			this.transport.setRequestHeader('content-length', this.postBody.length);
			this.transport.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			if (this.transport.overrideMimeType) this.transport.setRequestHeader('Connection', 'close');
		}
		this.transport.send(this.postBody);
	},

	onStateChange: function(){
		if (this.transport.readyState != 4 ) {
			if(this.loading)
				setTimeout(function(){this.loading.style.display = 'block';}.bind(this), 10);
		}
		if (this.transport.readyState == 4 && this.transport.status != 200) {
			if(this.onError) {
				if(this.loading)
					setTimeout(function(){this.loading.style.display = 'none';}.bind(this), 10);
				alert( this.onError );
			}
		}
		if (this.transport.readyState == 4 && this.transport.status == 200) {			
			if(this.loading)
				setTimeout(function(){this.loading.style.display = 'none';}.bind(this), 10);
			if (this.update)
				setTimeout(function(){this.update.innerHTML = this.transport.responseText;}.bind(this), 10);
			if (this.onComplete) 
				setTimeout(function(){this.onComplete(this.transport);}.bind(this), 10);
			this.transport.onreadystatechange = function(){};
		}
	},

	getTransport: function() {
		if (window.ActiveXObject) return new ActiveXObject('Microsoft.XMLHTTP');
		else if (window.XMLHttpRequest) return new XMLHttpRequest();
		else return false;
	}
};