var ContentLoader = function(url, id, params, options ){
	this.url = url;
	this.id = id;
    if(this.id == null) {
        this.key = this.url;
    }else{
        this.key = this.id + '|' + this.url;
    }
	this.params = params;
	this.init = false;
	this.xmlHttp = false;
	this.xml = false;
	this.text = '';
	this.done = false;
	this.browser = '';
	this.responseType = 'text'; //'text' or 'xml'
	this.callback1 = function(){};
	this.callback2 = function(){};
	this.callback3 = function(){};
	this.callback4 = function(){};
	this.callbackError = function(){};
	if (typeof options != 'undefined' ){
		if (typeof options.responseType != 'undefined' ){
			this.responseType = options.responseType;
		}
		if (typeof options.callback1 != 'undefined' ){
			this.callback1 = options.callback1;
		}
		if (typeof options.callback2 != 'undefined' ){
			this.callback2 = options.callback2;
		}
		if (typeof options.callback3 != 'undefined' ){
			this.callback3 = options.callback3;
		}
		if (typeof options.callback4 != 'undefined' ){
			this.callback4 = options.callback4;
		}
		if (typeof options.callbackError != 'undefined' ){
			this.callbackError = options.callbackError;
		}
	}
	this.setup();
};
ContentLoader.prototype.setup = function(){
	if (typeof XMLHttpRequest!='undefined')
	{
		this.xmlHttp = new XMLHttpRequest();
		if(document.all){
			this.browser = 'ie'+parseFloat(navigator.appVersion.split('MSIE')[1]);
		}else{
			this.browser = 'mozilla';
		}
	}
	if (!this.xmlHttp && window.ActiveXObject){
		this.browser = 'ie'+parseFloat(navigator.appVersion.split('MSIE')[1]);
		try
		{
			this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try
			{
				this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) { this.xmlHttp = false; }
		}
	}
	if (this.xmlHttp) this.init = true;
};
ContentLoader.handler = function( obj ){
	//Execute callbacks if we are the last in the list
	var list = ContentLoader.contentLoaders[obj.key];
	
	var wIndexOfObject = -1;
	for( var wI=0; wI<list.length; wI++ ){
        if(list[wI]==obj){
            wIndexOfObject = wI;
        }
    }
	if( ( list.length == 0 ) || ( wIndexOfObject!=list.length-1 ) ){  
		return;
	}
	      
	if (obj.xmlHttp.readyState == 1){
		obj.callback1();
	}else if (obj.xmlHttp.readyState == 2){
		obj.callback2();
	}else if (obj.xmlHttp.readyState == 3){
		obj.callback3();
	}else if (obj.xmlHttp.readyState == 4){
   		if (obj.xmlHttp.getResponseHeader('SessionAccessDenied') != 'true' && (obj.xmlHttp.status == 200 || obj.xmlHttp.status == 0 ) ){   			
   			if( obj.responseType == 'text' ){
   				obj.text = obj.xmlHttp.responseText;
   				if(obj.id){
   					document.getElementById(obj.id).innerHTML = obj.text;
   				}
   			}else if( obj.responseType == 'xml' ){
   				obj.xml = obj.xmlHttp.responseXML;
   			}
   			obj.done = true;
   			obj.callback4();
           }else if ( obj.xmlHttp.getResponseHeader('SessionAccessDenied') == 'true' ){
           	document.location.href = '/cgi/en/session.redirect';
               obj.done = true;
   		}else{
   			obj.callbackError();
   			obj.text = obj.xmlHttp.status; 
   		}
		
		// Empty the list
        list = [];
	}
};
ContentLoader.prototype.start = function(){
	if (!this.init) return false;
	
    //Initialise the list + list of list
	if(typeof ContentLoader.contentLoaders == 'undefined'){
		ContentLoader.contentLoaders=[];
	}
    if(typeof ContentLoader.contentLoaders[this.key] == 'undefined'){
        ContentLoader.contentLoaders[this.key]=[];
    }
    //Add me to the list of my key
	ContentLoader.contentLoaders[this.key][(ContentLoader.contentLoaders[this.key].length)]=this;
	
	var me = this;
	try{
		this.xmlHttp.onreadystatechange = function() { ContentLoader.handler(me); }
		this.xmlHttp.open( 'POST', this.url, true );

		//Send the proper header information along with the request
		this.xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.xmlHttp.setRequestHeader("Connection", "close");
		if(this.params){
			this.xmlHttp.setRequestHeader("Content-length", this.params.length);
		}
		this.xmlHttp.send(this.params);
	}catch(e){
		this.init = false;
	}
};