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

ajax = Class.create();
ajax.prototype = {
	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);
	},

	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-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 && this.transport.status == 200) {
			if (this.onComplete) 
				setTimeout(function(){this.onComplete(this.transport);}.bind(this), 10);
			if (this.update)
				setTimeout(function(){this.update.innerHTML = this.transport.responseText;}.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;
	}
};

//

function templates(obj){

var new_title = escape($F('title'));
var new_textos = escape($F('textos'));
var new_author = escape($F('author'));
var new_email = escape($F('email'));
var new_website = escape($F('website'));
var new_codigo = escape($F('codigo'));

obj.innerHTML	= "Processing...";
cleanUp('form');

var success	= function(t){editComplete(t, obj);}
var failure	= function(t){editFailed(t, obj);}

var url = 'posttem.php';

var pars = 'title='+new_title+'&textos='+new_textos+'&author='+new_author+'&email='+new_email+'&website='+new_website+'&codigo='+new_codigo;
var myAjax = new Ajax.Request(url, {method:'post', postBody:pars, onSuccess:success, onFailure:failure});
}

function fvotes(obj){

var new_vote = escape($F('voteid'));

cleanUp('votos');
obj.innerHTML	= "Processing...";

var success	= function(t){editCompleteVote(t, obj);}
var failure	= function(t){editFailed(t, obj);}

var url = 'postvote.php';

var pars = 'vid='+new_vote;
var myAjax = new Ajax.Request(url, {method:'post', postBody:pars, onSuccess:success, onFailure:failure});
}

function cleanUp(obj){
	Element.remove(obj);
}
	
function editComplete(t, obj){
obj.innerHTML	= 'Sent.';
}

function editCompleteVote(t, obj){
obj.innerHTML = t.responseText;
}
	
function editFailed(t, obj){
obj.innerHTML	= 'Error.';
}