/**
 * @author Sunil Agrawal,i4 Infotech Pvt Ltd.
 * 
 * Functionality: Provide pseudo classes and function for facilitating Ajax calls in a browser independent manner
 * 
 * Date of creation: 4/10/2009
 */


var I4Portal = new Object();

I4Portal.READY_STATE_UNINITIALIZED=0;
I4Portal.READY_STATE_LOADING=1;
I4Portal.READY_STATE_LOADED=2;
I4Portal.READY_STATE_INTERACTIVE=3;
I4Portal.READY_STATE_COMPLETE=4;

I4Portal.ContentLoader=function(url,onload,callerRef,onerror){
	this.url=url;
	this.req=null;
	this.onload=onload;
	this.onerror=(onerror) ? onerror : this.defaultError;
	this.callerRef=callerRef;
	// alert("got callerref"+ typeof this.callerRef)
	this.loadXMLDoc(url);
	
};


I4Portal.ContentLoader.prototype = {
	loadXMLDoc:function(url){
		if(window.XMLHttpRequest){
			this.req= new XMLHttpRequest();
		}else if(window.ActiveXObject){
			this.req= new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		if(this.req) {
			
			try {
				var loader=this;
				this.req.onreadystatechange=function(){
					loader.onReadyState.call(loader);
				}
				this.req.open('GET',url,true);
				this.req.send(null);
			}catch(err){
				this.onerror.call(this);
			}
			
		}
	},
	
	onReadyState:function(){
		var req=this.req;
		var ready=req.readyState;
		if(ready == I4Portal.READY_STATE_COMPLETE){
			var httpStatus=req.status;
			if(httpStatus==200 || httpStatus==0) {
				this.onload.call(this.callerRef, req);
			}else{
				this.onerror.call(this);
			}
		}
		
	},
	
	defaultError:function(){
		alert("Error fetching data!"
				+"\n\n readyState:"+this.req.readyState
				+"\n status: " + this.req.status
				+"\n headers: "+this.req.getAllResponseHeaders());
		
		
	}
	
};

