// JavaScript Document
var AJAX = function(action,callback,data,method,headers){
	this.method 	= method 	|| this.config.method;
	this.action 	= action 	|| this.config.action;
	this.callback 	= callback 	|| this.config.callback;
	this.headers	= headers	|| this.config.headers;
	this.error		= false;
	
	this.obj		= this.create();
	
	this.params		= data;
	
	this.run();
	
	return false;
};

AJAX.prototype.config = {
	method   : "POST",
	action   : "pagina.php",
	callback : '',
	XMLHttp  : [
		function () {return new XMLHttpRequest()},
		function () {return new ActiveXObject("Msxml2.XMLHTTP")},
		function () {return new ActiveXObject("Msxml3.XMLHTTP")},
		function () {return new ActiveXObject("Microsoft.XMLHTTP")}],
	headers  : [
		['Content-Type'		,'application/x-www-form-urlencoded'],
		['User-Agent'		,'XMLHTTP/1.1'],
		['Accept-Charset'	,'iso-8859-1'],
		['Accept-Encoding'	,'*'],
		['Cache-Control'	,'no-store,no-cache,must-revalidate'],
		['Date'				,new Date()],
		['Pragma'			,'no-cache'],
		['Connection'		,'close']]
}

AJAX.prototype.create = function(){
	var obj = false;
	for(var x=0,y=this.config.XMLHttp.length;x<y;x++){
		try {
			obj = this.config.XMLHttp[x]();
		} catch (e) {continue;}
		break;
	}	
	return obj;
}

AJAX.prototype.parseHeaders = function(headers){
	for(var x=0, y=headers.length; x<y;x++){
		this.obj.setRequestHeader(headers[x][0],headers[x][1]);
	}
}

AJAX.prototype.run = function(){
	var self = this;
	
	this.method = this.method.toLowerCase();
	
	this.obj.open(
		this.method,
		(this.method!='post') ? this.action+'?'+this.params : this.action,
		true
		);
	
	if(this.method=='post')
		this.parseHeaders(this.headers);
	
	this.obj.onreadystatechange = function(){ self.handler() };
	
	this.obj.send((this.method=='post') ? this.params : null);
}

AJAX.prototype.handler = function(callback){
	if(this.obj.readyState != 4)
		return;
		
	if(this.obj.status != 200 && this.obj.status !=304)
		this.obj.error = true;
		
	this.callback.call(this,this);
}

AJAX.prototype.toString = function(){
	var r;
		r  = "Status: "			+ this.obj.status;
		r += "\nStatus Text: "	+ this.obj.statusText;
		r += "\nResposta: "		+ this.obj.responseText;
		
	return r;
}

