/*
 * Ajax Class
 * Manuelx10.com
 *
 * User Defined Functions
 * obj.onDone - Function to execute when request is successful
 * obj.onFail - Function to execute when request fails
 * obj.onLoad - Function to execute when request is in progress
 *
 */


function AjaxClass(doneHandler,failHandler){
	newAjaxClass=this; //Create an instance object
	this.basedomain='http://'+window.location.hostname;
	this.filetype='txt';
	this.randomizeRequest=false; //For solving broswer cache problems
	this.onDone=doneHandler; //Assign Specified Done Handler Function
	this.onFail=failHandler; //Assign Specified Fail Handler Function
	this.transport=this.getTransport();
	this.transport.onreadystatechange=ajaxDispatch(this);
}

	AjaxClass.prototype.get=function(uri,params,fileType){
		this.filetype=fileType
		if(typeof params!='string'){ params=arrayToQueryString(params); }
		fullURI = uri + ( params ? ('?'+params) : '' );
		this.transport.open('GET',fullURI,true);
		this.transport.send(null); //or ''
	}

	AjaxClass.prototype.post=function(uri,data,fileType){
		this.filetype=fileType; //file_type either 'txt' or 'xml'
		if(typeof data!='string'){ data=arrayToQueryString(data); }
		if(this.randomizeRequest==true){ data+='&rand='+Math.floor(Math.random()*10000); }
		this.transport.open('POST',uri,true);
		this.transport.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		this.transport.setRequestHeader("Content-length", data.length);
		this.transport.setRequestHeader("Connection", "close");
		this.transport.send(data);
	}

	AjaxClass.prototype.stateDispatch=function(){
		if(this.transport.readyState==1&&this.onLoad)	this.onLoad(true);
		if(this.transport.readyState==4){
			if(this.onLoad) this.onLoad(false);
			if(this.transport.status>=200&&this.transport.status<300&&this.transport.responseText.length>0){
				if(this.onDone){
					if(this.filetype=='xml'){
						this.onDone(this,this.transport.responseXML);
					}
					else{
						this.onDone(this,this.transport.responseText);
					}
				}
			}else{
				if(this.onFail) this.onFail(this);
			}
		}
	}

	AjaxClass.prototype.getTransport=function(){
		var transport=null;
		//Mozilla
		try{
			transport=new XMLHttpRequest();
			if(transport.overrideMimeType) transport.overrideMimeType('text/xml');
		}
		catch(e){
			transport=null;
		}
			//IE
			try{
				if(!transport)
				transport=new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e){
				transport=null;
			}
				//IE
				try{
					if(!transport)
					transport=new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(e){
					transport=null;
				}
		return transport;
	}

function ajaxDispatch(selfObject){
	return function(){
		selfObject.stateDispatch();
	};
}

function arrayToQueryString(queryArray){
	var sep='';
	var query="";
	for(var key in queryArray){
		query=query+
		sep+
		encodeURIComponent(key)+'='+
		encodeURIComponent(queryArray[key]);
		sep='&';
	}
	return query;
}

