﻿/*
url-loading object and a request queue built on top of it
*/

/* namespacing object */
var mpAjax=new Object();

//alert("HOHI"+mpAjax);

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


/*--- content loader object for cross-browser requests ---*/
mpAjax.ContentLoader=function(url, onload, params, params_erstellen, onerror, method, contentType){
  this.url=url;
  this.req=null;
  this.onload=onload;
  if(!params)
  	{
  		if(params_erstellen)
  			this.params = params_erstellen();
  	}
  else
  	this.params = params;
  this.onerror=(onerror) ? onerror : this.defaultError;
  this.loadXMLDoc(url,method,contentType);
}

mpAjax.ContentLoader.prototype.loadXMLDoc=function(url,method,contentType){
  if (!method){
    method="post";
  }
  if (!contentType /*&& method=="post"*/){
    contentType='application/x-www-form-urlencoded';
  }
  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(){
        mpAjax.ContentLoader.onReadyState.call(loader);
      }
      this.req.open(method,url,true);
      if (contentType){
        this.req.setRequestHeader('Content-Type', contentType);
      }
      
      //alert("params: "+this.params+" \nmethod: "+method+" \nurl: "+url+"\nContType: "+contentType);
      
      this.req.send(this.params);
    }catch (err){
      this.onerror.call(this);
    }
  }
}


mpAjax.ContentLoader.onReadyState=function(){
  var req=this.req;
  
		//alert("REQ: "+req+"      REQ ready: "+req.readyState);
		//alert("REQ: "+req+"   REQ status: "+req.status+"   REQ ready: "+req.readyState);
  
  var ready=req.readyState;
  if (ready==mpAjax.READY_STATE_COMPLETE)
  	{
  		  var httpStatus=req.status;
				{
	    if (httpStatus==200 || httpStatus==0){
	      this.onload.call(this);
	    }else{
	      this.onerror.call(this);
	    }
				}
  }
}

mpAjax.ContentLoader.prototype.defaultError=function(){
  alert("error fetching data!"
    +"\n\nreadyState:"+this.req.readyState
    +"\nstatus: "+this.req.status
    +"\nheaders: "+this.req.getAllResponseHeaders());
}




