var noloading = 'noidontwanttoseethatloadingbox';

function Ajax(method, url, title, text)
{
	var http = null;
	if (window.XMLHttpRequest) {
		http = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		http = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (!http) {
		//todo: error message
		return null;
	}
	this.fields = new Array();
	http.open(method, url);
	if (method == 'post') {
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	}
	http.setRequestHeader('Referer', document.location);
	if (!title) {
		title = 'Opslaan van gegevens';
	}
	if (!text) {
		text = 'Bezig met verwerken...';
	}
	this.title = title;
	this.text = text;
	this.form = null;
	var currentinstance = this;
	http.onreadystatechange = function() { Ajax.prototype.handle(currentinstance); };//this.handle;//function() { alert(this.parentNode);};//this.handle
	this.http = http;
}

Ajax.prototype.execute = function()
{
	for (var i = 0; i < this.fields.length; i++) {
		this.fields[i] = encodeURIComponent(this.fields[i][0]) + '=' + encodeURIComponent(this.fields[i][1]);
	}
	this.http.send(this.fields.join('&'));
	this.addLoading();
};

Ajax.prototype.add_value = function(name, value)
{
	this.fields.push([name, value]);
};

Ajax.prototype.add_form = function(form)
{
	for (var i = 0; i < form.elements.length; i++) {
		this.add_value(form.elements[i].name, form.elements[i].value);
	}
	this.form = form;
}

Ajax.prototype.set_readystatehandler = function(fn)
{
	this.readystatehandler = fn;
};

Ajax.prototype.handle = function(ajax)
{
	if (ajax.http.readyState == 4) {
//		if (alertbox) {
//			closeAlertBox();
//		}
		if (ajax.readystatehandler) {
			ajax.readystatehandler();
		}
	}
};

Ajax.prototype.addLoading = function()
{
//	if (this.title == noloading) return;
//	createAlertBox(this.title, this.text);
};

