// ver. 1.0

Function.prototype.bind = function(object) {
	
  var __method = this;
	
  return function() {
    __method.apply(object, arguments);
  }
}
if (!Function.prototype.apply) {
  Function.prototype.apply = function(oScope, args) {
    var sarg = [];
    var rtrn, call;
    if (!oScope) oScope = window;
    if (!args) args = [];
    for (var i = 0; i < args.length; i++) {
      sarg[i] = "args["+i+"]";
    }     call = "oScope.__applyTemp__(" + sarg.join(",") + ");";
    oScope.__applyTemp__ = this;
    rtrn = eval(call);
    return rtrn;
  }
}
function e3Ajax(){
	this.debug = false;
	this.done = null;
	this.http_request = null;
	this.error = false;
	this.cache = false;
	

	this.load = function(uri,toDo,loading,method) {
		
		if (window.XMLHttpRequest) { // Mozilla, Safari,...
			this.http_request = new XMLHttpRequest();
			if (this.http_request.overrideMimeType) {
				this.http_request.overrideMimeType('text/xml');
			}
		} else if (window.ActiveXObject) { // IE
			try {
				this.http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
				this.http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}
		if (!this.http_request){
			
			alert("Il browser è da aggiornare!");
			return false;
		}
		if (loading) loading();
		this.done = toDo;
		this.http_request.onreadystatechange = this.onStateChange.bind(this);
		this.xmlDom = null;
		if (this.debug){
			var winerr =window.open(uri,"winerr","width=400,height=200,resizable=yes,scrollbars=yes");
			winerr.focus();
		}
		if(!method) method = "GET";
		if (this.cache){
		
	  	  	this.http_request.open(method, uri, true);
		}else{
			
			var time = new Date();
			var timeUri ;
			if (uri.indexOf("?")>-1){
				timeUri = uri + "&ajaxCacheDate"+time.getTime();
			}else{
				timeUri = uri + "?ajaxCacheDate"+time.getTime();
				}
			this.http_request.open(method, timeUri, true);
			
		}
		if(method == 'POST'){
			
			
			var onlyUri;
		
			
			var onlyUri = uri.host();
			var query = uri.search()
			
			
			this.http_request.setRequestHeader("Method", "POST "+onlyUri+" HTTP/1.1");
			this.http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			this.http_request.send(query);
		}
		else this.http_request.send(null);
	};
	
  this.onStateChange = function() {
    if(this.http_request.readyState!=4)
		  return;
		
		var status=this.http_request.status;
		
		if ((status!=200)&&(status!=0)) {
			this.error = true;
			this.done(this);
		  //alert("errore di caricamento!\nstatus: "+status);
		  return;
		}

		this.responseText = this.http_request.responseText;		
		var objDom = new XMLDoc(this.responseText,this.errorFn);

		this.xmlDom = objDom.docNode
		

		this.done(this);
  }
  
	
}
