// based on prototype's ajax class
// to be used with prototype.lite, moofx.mad4milk.net.
// moo.ajax Library

/*
each: function(iterator, context){

  for (var i = 0; i < this.length; i++)

    iterator.call(context, this[i], i);

}
*/

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

/**
* @class
*/
function ajax(url, options) {
	this.initialize = function(url, options){
		this.transport = this.getTransport();
		this.postBody = options.postBody || '';
		this.method = options.method || 'post';
		this.onComplete = options.onComplete || null;
//		this.update = $(options.update) || null;
		this.request(url);
	}
	this.getTransport = function(){
		if (window.ActiveXObject) return new ActiveXObject('Microsoft.XMLHTTP');
		else if (window.XMLHttpRequest) return new XMLHttpRequest();
		else return false;
	}
	this.onStateChange = function(p){
		if (this.transport.readyState == 4 && this.transport.status == 200) {
			if (this.onComplete) 
				this.onComplete(this.transport.responseText);
//			if (this.update)
//				setTimeout(function(){this.update.innerHTML = this.transport.responseText;}.bind(this), 10);
//			this.transport.onreadystatechange = function(){};
		}
	}
	this.request = function(url){
		this.transport.onreadystatechange = this.onStateChange.bind(this);
		this.transport.open(this.method, url, true);
		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.postBody);
	}
	this.initialize(url, options);
}

sendRequest = function(url, func, get, post) {
	get = get || '';
	new ajax(url+'?'+get, {
		postBody: post||'',
		onComplete: func
	});
}
/*
new ajax('sleep.php', {postBody: 'sleep=3', update: $('myelementid'), onComplete: myFunction});
function myFunction(request){
  alert(request.responseText);
}
*/
