function Ajax(address, xml) {
	this.address = address;
	this.client =  richiestaXML();
	this.data = new Array();
	this.method = 'get';
	this.showloading = true;
	this.onload = false;
	this.onerror = false;
	this.json = false;
	this.send = function() {
		var self = this;
		this.client.onreadystatechange = function(){
			if(self.client.readyState  == 4) {
				if(self.showloading){
					hideLoader();
				}
				try { var status = self.client.status }
				catch (e) { var status = 500 }

				if(status == 200) {
					if(typeof self.onload == 'function'){
						if(xml){
								self.onload(self.client.responseXML);
						}
						else {
							if(self.json){
								self.onload(self.client.responseText.parseJSON());
							}
							else{
								self.onload(self.client.responseText);
							}
						}
					}
					
					if(self.onload == 'refresh')				// refresh page
						window.location.reload(false);
					else {											// innerHTML 
						var output = $(self.onload);
						if(output)
							output.innerHTML = self.client.responseText;
					}
				}
				else{
					if(status == 404){
						alert("Error: " + status);
					}
				}
			}
		};

		var c = this.data.length;
		if(c > 0) {
			var args = this.data[0];
			for(var i=1; i<c; i++) {
				var name = this.data[i].substring(0,this.data[i].indexOf('='));
				var value = this.data[i].substring(this.data[i].indexOf('=') + 1);

				args = args + '&' + name + '=' + encodeURIComponent(value);
			}
		} else
			var args = false;

		if(this.showloading){
			showLoader();
		}
		if(this.method == 'get') {
			this.client.open('GET', this.address + (args ? '?' + args : ''), true);
			this.client.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');
			this.client.setRequestHeader('AJAX-Request', 'true');
			this.client.send(null);
		}
		if(this.method == 'post') {
			this.client.open('POST', this.address, true);
			this.client.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			this.client.setRequestHeader('AJAX-Request', 'true');
			this.client.send(args?args:null);
		}
	};
}


function richiestaXML() {
	var http_request = false;

	if (window.XMLHttpRequest) { 
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) {
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	
	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	return http_request;
}