/*
url-loading object and a request queue built on top of it
*/

/* namespacing object */
var net=new Object();

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;

net.NUMBER_OF_RETRIES = 1;

/*--- content loader object for cross-browser requests ---*/
net.ContentLoader=function(url,onload,onerror,method,params,contentType){
  this.attemptNumber = 0;
  this.retryObj = {"url":url,"method":method,"params":params,"contentType":contentType}  
  this.req=null;
  this.onload=onload;
  this.onerror=(onerror) ? onerror : this.defaultError;
  this.abort = function(){this.req.abort();}
  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';
  }
  if (window.XMLHttpRequest){
    this.req=new XMLHttpRequest();
  } else if (window.ActiveXObject){
    try{
      this.req=new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch(e){
      this.req = null;
      document.location.href = "/Web/ConfigError.aspx";
      return;
    }
    try{  //test for js version
      var b = true;
      if(Function.call)b=true;
      else this.req = null;
    }
    catch(e){
      this.req = null;
    }
  }
  if (this.req){
    try{
      var loader=this;
      this.attemptNumber++;
      // bug 4021
      if(url.toString().indexOf("Ajax/DynamicQuestionsHandler.ashx") > -1)
      {
		// split params
		var arrayParams = params.split("&");
		// tack on location.href
		url = url + ((url.indexOf("?") > -1) ? "&" : "?") + "loc=" + escape(document.location.href);
		// check if sanctionedPage is in params list
		var foundKey = false;
		for(var i=0;i<arrayParams.length;i++)
		{
			if(arrayParams[i] != null && arrayParams[i].split("=")[0].toLowerCase() == "sanctionedpage")
			{
				url = url + "&hasSp=true&sancPage=" + arrayParams[i].split("=")[1];
				foundKey = true;
				break;
			}
		}
		if(!foundKey)
		{
			url = url + "&hasSp=false";
		}
		// check if workExperienceId var exists in the page
		if(typeof(workExperienceId) != "undefined")
		{
			url = url + "&hasweId=true&newWeId=" + workExperienceId;
		}
		else
		{
			url = url + "&hasweId=false";
		}
      }
      // end bug 4021 code
      this.req.open(method,url,true);
      if (contentType){
        this.req.setRequestHeader('Content-Type', contentType);
      }      
      this.req.onreadystatechange=function(){
        net.ContentLoader.onReadyState.call(loader);
      }
      this.req.send(params);
    }catch (err){
      this.onerror.call(this);
    }
  }
  else{
    document.location.href = "../AjaxSupportError.aspx";
  }
}


net.ContentLoader.onReadyState=function(){
  var req=this.req;
  var ready=req.readyState;
  if (ready==net.READY_STATE_COMPLETE){
    var httpStatus=null;
    try{httpStatus = req.status;}catch(eUnknownStatusCode){}
    if (httpStatus==200 || httpStatus==0){
      this.onload.call(this);
    }else{
      if(this.isRetryErrorCode())this.loadXMLDoc(this.retryObj.url,this.retryObj.method,this.retryObj.params,this.retryObj.contentType);
      else this.onerror.call(this);
    }
  }
}

net.ContentLoader.prototype.defaultError=function(){
  var status = "NA";
  var xreadyState = "NA";
  var headers = "NA";

  try{status=this.req.status;}catch(e){}
  try{xreadyState=this.req.readyState;}catch(e){}
  try{headers=this.req.getAllResponseHeaders();}catch(e){}

  alert("error fetching data!"
    +"\n\nreadyState:" + xreadyState
    +"\nstatus: " + status
    +"\nheaders: " + headers);
}

net.ContentLoader.prototype.isRetryErrorCode=function(){
  
  if(this.attemptNumber > net.NUMBER_OF_RETRIES)return false;
  
  var statusCd = "-1";
  try{statusCd = this.req.status;}catch(e){}
  
  switch(statusCd){
    case 404:
    case 12002: 
    case 12029:
    case 12030:
    case 12031:
    case 12152:
    case 13030:
      return true;
    default:
      return false;
  }  
}


if (typeof(Sys) != 'undefined')
{
    Sys.Application.notifyScriptLoaded();
}
