var res;

function AjaxObj() {
	var args=arguments;
	var self=this;
	this.error=false;
	this.errormsg="";
	this.url=args[0];
	this.func=args[1]?args[1]:void(null);
	this.responseType=args[2] ? args[2] : "XML";
	this.callMethod=args[3] ? args[3] : "GET";
	this.debug=false;
	this.errorfunc=ajaxerror;
	this.request=null;

	var callpieces=this.url.split('?');
	switch(this.callMethod) {
		case "GET":
			this.callParams=null;
			this.callUrl=this.url;
			break;

		case "POST":
			this.callParams=callpieces[1];
			this.callUrl=callpieces[0];
			break;

		default:
			this.setError('Unknown request method');
			break;
	}

	try {
		this.request= new ActiveXObject("Msxml2.XMLHTTP");
	} catch(e) {
		try {
			this.request=new ActiveXObject("Microsoft.XMLHTTP");
		} catch(oc) {
			this.request=null;
		}
	}
	if(!this.request && typeof XMLHttpRequest != 'undefined')
		this.request=new XMLHttpRequest();
	if(!this.request)
		this.setError('Unable to create connection object!');

	this.request.onreadystatechange=function() {
		if(self.request.readyState != 4)
			return;
		if(self.debug)
			alert(self.request.responseText);
		if(self.isValidResponse()) {
			if(self.responseType=="XML") {
				if(self.request.responseXML){
					self.func(self.request.responseXML);
//  					alert(self.request.responseText);
				} else {
//   					alert(self.request.responseText);
					alert("NON HO CREATO IL DOCUMENTO XML!!!\nATTENZIONE: Nel file php c'è qualche problema...tipo NOTICE, ERRORS etc...");
				}
			} else {
				self.func(self.request.responseText);
			}
		} else {
			self.errorfunc(self.getError());
		}
	};

	this.isValidResponse = function() {		
// 		if(self.responseType=="XML") {			
// 			if(rescheck.getElementsByTagName("parsererror").length != 0) {
// 				self.setError(rescheck.getElementsByTagName("parsererror").item(0).firstChild.nodeValue);
// 				return false;
// 			}
// 			var err=rescheck.getElementsByTagName("ERROR").item(0).firstChild.nodeValue;
// 			if(err==1) {
// 				if(self.debug)
// 					alert(rescheck.getElementsByTagName("ERRORMESSAGE").item(0).firstChild.nodeValue);
// 				self.setError(rescheck.getElementsByTagName("ERRORMESSAGE").item(0).firstChild.nodeValue);
// 				return false;
// 			}
// 		}
		return(true);
	};
}

AjaxObj.prototype.setError = function(msg) {
	this.error=true;
	this.errormsg=msg;
}

AjaxObj.prototype.debugging = function(debug) {
	this.debug=debug;
}

AjaxObj.prototype.getError = function() {
	this.error=false;
	var msg=this.errormsg;
	this.errormsg="";
	return msg;
}

AjaxObj.prototype.ask = function() {
// 	var waitlayer=document.getElementById("waitmessage");
// 	waitlayer.className=waitlayer.className.replace(/hidden/i, "");

// 	var winWidth=!window.innerWidth ? document.body.clientWidth : window.innerWidth;
// 	var winHeight=!window.innerHeight ? document.body.clientHeight : window.innerHeight;
// 	var divW=waitlayer.offsetWidth;
// 	var divH=waitlayer.offsetHeight;
// 	waitlayer.style.left=(winWidth-divW)/2+"px";
// 	waitlayer.style.top=(winHeight-divH)/3+"px";
// 	indexwaitmessage=0;
// 	elapsedTime=0;
// 	setTimeout("animateMessage()", 100);
// 	setTimeout("elapseTime()", 100);
	
	this.request.open(this.callMethod, this.callUrl, true);
	if(this.callMethod=="POST")
		try {
			this.request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		} catch(err) {
			this.setError('This request cannot be executed because your browser does not support "POST" request method.');
		}
	this.request.send(this.callParams);
}

AjaxObj.prototype.setErrorFunction = function(func) {
	this.errorfunc=func;
}
