﻿function Ajx(echoFunction, responseType){

    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||'';
    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
    }
    
}

