/*
url-loading object and a request queue built on top of it
*/

/* Constructor */
var debug=false;
function netObject(debug) {
	//alert("Constructing net Object");
	if (debug == null || debug == undefined) debug = false;
	this.debug = debug;
	//alert("debug = "+debug);
}

/* namespacing object */
var net=new netObject();	//netObject(true) if you want to debug

net.READY_STATE_UNINITIALIZED=0;
net.READY_STATE_LOADING=1;
net.READY_STATE_LOADED=2;
net.READY_STATE_INTERACTIVE=3;
net.READY_STATE_COMPLETE=4;

var ignoreList=['getMessage', 'keepAlive', 'view record&component=PlatonAction'];

/*--- content loader object for cross-browser requests ---*/

net.ContentLoader=function(url,onload,onerror,method,params,contentType){
  if(ignoreReq(params)){
	makeBusy();
  }
  this.req=null;
  this.onload=onload;
  this.onerror=(onerror) ? onerror : this.defaultError;
  contentType='application/x-www-form-urlencoded;charset=UTF-8';
  this.loadXMLDoc(url,method,params,contentType);
}
net.send=function(url,onload,onerror,method,params,contentType){
  if(ignoreReq(params)){
	makeBusy();
  }
  this.req=null;
  this.onload=onload;
  this.onerror=(onerror) ? onerror : this.defaultError;
  contentType='text/xml;charset=UTF-8';
  this.loadXMLDoc(url,method,params,contentType);
}

net.ContentLoader.prototype.loadXMLDoc=function(url,method,params,contentType){
  if (!method){
    method="GET";
  }
  if (!contentType && method=="POST"){
    contentType='application/x-www-form-urlencoded;charset=UTF-8';
  }
  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(){
        net.ContentLoader.onReadyState.call(loader);
      }
      this.req.open(method,url,true);
      if (contentType){
        this.req.setRequestHeader('Content-Type', contentType);
      }
      //alert(contentType);
      this.req.send(params);
      //setTimeout(killGetMessage,300000);
    }catch (err){
		makeLazy();
      this.onerror.call(this);
    }
  }
}

net.ContentLoader.onReadyState=function(){
  var req=this.req;
  var ready=req.readyState;
  if (ready==net.READY_STATE_COMPLETE){
    var httpStatus=req.status;
    if (httpStatus==200 || httpStatus==0){
		makeLazy();
      this.onload.call(this);
    }else{
		makeLazy();
      this.onerror.call(this);
    }
  }
}

net.ContentLoader.prototype.defaultError=function(){
  if (debug == true) {
	  alert("error fetching data!"
		+"\n\nreadyState:"+this.req.readyState
		+"\nstatus: "+this.req.status
		+"\nheaders: "+this.req.getAllResponseHeaders());
  }
  makeLazy();
}

net.send.prototype.loadXMLDoc=function(url,method,params,contentType){
  if (!method){
    method="GET";
  }
  if (!contentType && method=="POST"){
    contentType='application/x-www-form-urlencoded;charset=UTF-8';
  }
  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(){
        net.ContentLoader.onReadyState.call(loader);
      }
      this.req.open(method,url,true);
      if (contentType){
        this.req.setRequestHeader('Content-Type', contentType);
      }
      this.req.send(params);
      //setTimeout(killGetMessage,300000);
    }catch (err){
		makeLazy();
      this.onerror.call(this);
    }
  }
}

net.send.onReadyState=function(){
  var req=this.req;
  var ready=req.readyState;
  if (ready==net.READY_STATE_COMPLETE){
    var httpStatus=req.status;
    if (httpStatus==200 || httpStatus==0){
		makeLazy();
	  this.onload.call(this);
    }else{
		makeLazy();
      this.onerror.call(this);
    }
  }
}

net.send.prototype.defaultError=function(){
  if (debug == true) {
	  alert("error fetching data!"
		+"\n\nreadyState:"+this.req.readyState
		+"\nstatus: "+this.req.status
		+"\nheaders: "+this.req.getAllResponseHeaders());
	}
	makeLazy();
}

net.ContentLoaderThis=function(url,onload,onerror,obj_reference,obj_reference2,method,params,contentType){
  if(ignoreReq(params)){
	makeBusy();
  }
  this.obj_reference=obj_reference;
  this.obj_reference2=obj_reference2;
  this.req=null;
  this.onload=onload;
  this.onerror=(onerror) ? onerror : this.defaultError;
  contentType='application/x-www-form-urlencoded;charset=UTF-8';
  this.loadXMLDoc(url,method,params,contentType);
}
net.sendThis=function(url,onload,onerror,obj_reference,obj_reference2,method,params,contentType){
  if(ignoreReq(params)){
	makeBusy();
  }
  this.obj_reference=obj_reference;
  this.obj_reference2=obj_reference2;
  this.req=null;
  this.onload=onload;
  this.onerror=(onerror) ? onerror : this.defaultError;
  contentType='text/xml;charset=UTF-8';
  this.loadXMLDoc(url,method,params,contentType);
}

net.ContentLoaderThis.prototype.loadXMLDoc=function(url,method,params,contentType){
  if (!method){
    method="GET";
  }
  if (!contentType && method=="POST"){
    contentType='application/x-www-form-urlencoded;charset=UTF-8';
  }
  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(){
        net.ContentLoaderThis.onReadyState.call(loader);
      }
      this.req.open(method,url,true);
      if (contentType){
        this.req.setRequestHeader('Content-Type', contentType);
      }
      //alert(contentType);
      this.req.send(params);
      //setTimeout(killGetMessage,300000);
    }catch (err){
		makeLazy();
      this.onerror.call(this);
    }
  }
}

net.ContentLoaderThis.onReadyState=function(){
  var req=this.req;
  var ready=req.readyState;
  if (ready==net.READY_STATE_COMPLETE){
    var httpStatus=req.status;
    if (httpStatus==200 || httpStatus==0){
		makeLazy();
      this.onload.call(this);
    }else{
		makeLazy();
      this.onerror.call(this);
    }
  }
}

net.ContentLoaderThis.prototype.defaultError=function(){
  if (debug == true) {
	  alert("error fetching data!"
		+"\n\nreadyState:"+this.req.readyState
		+"\nstatus: "+this.req.status
		+"\nheaders: "+this.req.getAllResponseHeaders());
  }
  makeLazy();
}

net.sendThis.prototype.loadXMLDoc=function(url,method,params,contentType){
  if (!method){
    method="GET";
  }
  if (!contentType && method=="POST"){
    contentType='application/x-www-form-urlencoded;charset=UTF-8';
  }
  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(){
        net.ContentLoaderThis.onReadyState.call(loader);
      }
      this.req.open(method,url,true);
      if (contentType){
        this.req.setRequestHeader('Content-Type', contentType);
      }
      this.req.send(params);
      //setTimeout(killGetMessage,300000);
    }catch (err){
		makeLazy();
      this.onerror.call(this);
    }
  }
}

net.sendThis.onReadyState=function(){
  var req=this.req;
  var ready=req.readyState;
  if (ready==net.READY_STATE_COMPLETE){
    var httpStatus=req.status;
    if (httpStatus==200 || httpStatus==0){
		makeLazy();
	  this.onload.call(this);
    }else{
		makeLazy();
      this.onerror.call(this);
    }
  }
}

net.sendThis.prototype.defaultError=function(){
  if (debug == true) {
	  alert("error fetching data!"
		+"\n\nreadyState:"+this.req.readyState
		+"\nstatus: "+this.req.status
		+"\nheaders: "+this.req.getAllResponseHeaders());
  }
  makeLazy();
}

function makeBusy(){
	if(jQuery('#noclickLayer').length==0){
		jQuery('body').append("<div id='noclickLayer' style='filter: alpha(opacity=0); zoom:1; opacity: 0; -moz-opacity: 0; display:block; cursor:wait; position:absolute; top:"+jQuery(document).scrollTop()+"px; left:0; width:100%; height:100%; z-index:100000;'></div>");
		jQuery('#noclickLayer').css('background-color', '#FFF');
		jQuery(document).bind('scroll', function(){
			jQuery('#noclickLayer').css('top', jQuery(document).scrollTop());
		});
	}
}

function makeLazy(){
	jQuery('#noclickLayer').remove();
}

function ignoreReq(params){
	var l=ignoreList.length;
	for(var i=0; i<l; i++){
		if(params.indexOf(ignoreList[i])>=0){
			return false;
		}
	}
	return true;
}


