_$=function() {
        elements=[];
        for(i=0;i<arguments.length;i++) {
                arg=arguments[i];
                if(typeof(arg)=='string') arg=document.getElementById(arg);
                if(arg) elements.push(arg);
        }
        if(elements.length==1) return elements[0];

        return elements;
};

_empty=function() {
};

_class=function() {
	return function() {
		this.initialize.apply(this,arguments);
	};
};

_try=function() {
	result=false;
	for(i=0;i<arguments.length;i++) {
		try {
			result=arguments[i]();
			break;
		} catch(e) {}
	}
	
	return result;
};

Function.prototype.bind=function(obj) {
	var __method=this;
	return function() {
		return __method.apply(obj,arguments);
	}
};

ajax=new _class;
ajax.states=['Uninitialized','Loading','Loaded','Interactive','Complete'];
ajax.prototype={
	initialize:function(url,options){
		this.transport=this.getTransport();
		this.body=options.body||'';
		this.method=options.method||'get';
		this.onCreate=options.onCreate||null;
		this.onComplete=options.onComplete||null;
		this.evalResponse=options.evalResponse||null;
		this.update=_$(options.update)||null;
		this.request(url);
	},

	request:function(url){
		this.transport.open(this.method,url,true);
		this.transport.onreadystatechange=this.onStateChange.bind(this);
		this.transport.setRequestHeader('X-Requested-With','XMLHttpRequest');
		if(this.method=='post') {
			this.transport.setRequestHeader('Content-type','application/x-www-form-urlencoded');
			if(this.transport.overrideMimeType) {
				this.transport.setRequestHeader('Connection','close');
			}
		}
		this.transport.send(this.body);
	},

	onStateChange:function(){
		state=ajax.states[this.transport.readyState];
		if(this['on'+state]) {
			setTimeout(function(){this['on'+state](this.transport);}.bind(this),10);
		}
		if(state=='Complete') {
			if(this.evalResponse)
				setTimeout(function(){try {return eval(this.transport.responseText)}catch(e){};}.bind(this),10);
			if(this.update)
				setTimeout(function(){this.update.innerHTML=this.transport.responseText;}.bind(this),10);
			this.onreadystatechange=function(){};
		}
	},
	
	getTransport:function() {
		return _try(
			function() {return new XMLHttpRequest()},
			function() {return new ActiveXObject('Msxml2.XMLHTTP')},
			function() {return new ActiveXObject('Microsoft.XMLHTTP')}
		)||false;
	}
};


