var http_request = false;
function makePOSTRequest(url, parameters, handler) {
   //document.getElementById('myspan').innerHTML = "Attendere...";
	 http_request = false;
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {
         http_request.overrideMimeType('text/xml');
      }
   } else if (window.ActiveXObject) { // IE
      try {
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }
   if (!http_request) {
      alert('Cannot create XMLHTTP instance');
      return false;
   }
   
   http_request.onreadystatechange = handler;
   http_request.open('POST', url, true);
 
   http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   http_request.setRequestHeader("Content-length", parameters.length);
   //http_request.setRequestHeader("Connection", "close");
   http_request.send(parameters);
}
///////////////////// NEW ///////////////////////////
function Ajax(echoFunction, responseType){//version 1.1.0
this.request=null;
this.response=null;
this.callingMethod='';
this.responseType=(typeof(responseType)=='string' && responseType.toLowerCase()=='responsexml ')?
	'responseXML':'responseText';
this.echoFunction=echoFunction||null;

/********* M E T H O D *********/
this.initialize=function(){
this.response=null;
this.callingMethod='';
if(!this.request){
	if(window['XMLHttpRequest']){/*IE7, Mozillas*/ try{this.request=new XMLHttpRequest();}catch(e){this.request=null;}; }
	else if(window['ActiveXObject']){/*IE<IE7*/
	var ajaxMSversions=[
		/*'Msxml2.DOMDocument.5.0', 'Msxml2.DOMDocument.4.0', 'Msxml2.DOMDocument.3.0', 'MSXML2.DOMDocument',*/ 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'
	];
		for(var v=0; v<ajaxMSversions.length; v++){
			try{this.request=new ActiveXObject(ajaxMSversions[v]); return this.request;}catch(e){this.request=null;};
		}
	}
	else if(window['createRequest']){ try{this.request=window.createRequest();}catch(e){this.request=null;}; }
	else{alert('XMLHTTP not enabled. Impossible to proceed.');}
};
return this.request;
}

/********* M E T H O D *********/
this.get=this.send=function(address, query, echoFunction, responseType){
if(!address || !this.initialize()){return false;};
this.callingMethod='GET';
this.responseType=responseType||this.responseType;
query=query||'';
query=query.replace(/\?/, '');
//query=unescape(query);
this.request.open('GET', (address+'?'+query), true);
this.request.setRequestHeader('Content-Type', 'text/xml');
if(typeof(echoFunction)!="function"){this.request.onreadystatechange=this.echo(this);/*currying*/}
else{this.request.onreadystatechange=echoFunction;};
this.request.send(null);
}

/********* M E T H O D *********/
this.post=function(address, send, echoFunction, responseType){
if(!address || !this.initialize()){return false;};
this.callingMethod='POST';
this.responseType=responseType||this.responseType;
send=send||'';
//send=unescape(send);
this.request.open('POST', address, true);
this.request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
if(typeof(echoFunction)!="function"){this.request.onreadystatechange=this.echo(this);/*currying*/}
else{this.request.onreadystatechange=echoFunction;};
this.request.send(send);
}

/********* M E T H O D *********/
this.head=function(address, send, echoFunction){
if(!address || !this.initialize()){return false;};
this.callingMethod='HEAD';
send=send||'';
this.request.open('HEAD', address, true);
this.request.setRequestHeader('Content-Type', 'text/xml');
if(typeof(echoFunction)!="function"){this.request.onreadystatechange=this.echo(this);/*currying*/}
else{this.request.onreadystatechange=echoFunction;};
this.request.send((send||null));
}

/********* M E T H O D *********/
this.error=function(statusError){
if(statusError){
this.response=(this.request && this.request.status)? 'Ajax Error: '+this.request.status+': '+this.request.statusText: 'Ajax Error: Requested document may be temporarily unavailable';
alert(this.response);
return this.response;
}
else{return false;};
}

/********* M E T H O D *********/
this.echo=function(ajaxInstance){
return function(){//currying
	if(ajaxInstance.request.readyState==4 || ajaxInstance.request.readyState=='complete'){
		if(ajaxInstance.request.status==200){
		ajaxInstance.response=
		(ajaxInstance.callingMethod=='GET' || ajaxInstance.callingMethod=='POST')?ajaxInstance.request[ajaxInstance.responseType]:
		(ajaxInstance.callingMethod=='HEAD')?ajaxInstance.request.getAllResponseHeaders():false;
			if(typeof(ajaxInstance.echoFunction)=="function"){
			return ajaxInstance.echoFunction(ajaxInstance.response);
			};/*no function passed as parameter: a default behaviour:*/
		return ajaxInstance.response;/*response is STORED in the instance.response property, ready for further manipulation*/
		}
		else{return ajaxInstance.error(1);};
	}
	else{return ajaxInstance.error(0);};
}//currying over
}
/*class ends - Keep this comment to reuse freely: http://www.unitedscripters.com/ */}

var myajax=new Ajax(); //a bonus instance is ready!

