var httpRequestId = new Array();

function getBrowserId() {
  var nVer = navigator.appVersion;
  var nAgt = navigator.userAgent;
  var browserName  = navigator.appName;
  var fullVersion  = ''+parseFloat(navigator.appVersion); 
  var majorVersion = parseInt(navigator.appVersion,10);
  var nameOffset,verOffset,ix;
  var b;
  
//In MSIE, the true version is after "MSIE" in userAgent
 if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
  browserName = "IE";
  fullVersion = nAgt.substring(verOffset+5);
}
// In Opera, the true version is after "Opera" 
else if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
 browserName = "OP";
 fullVersion = nAgt.substring(verOffset+6);
}
// In Chrome, the true version is after "Chrome" 
else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {
 browserName = "CH";
 fullVersion = nAgt.substring(verOffset+7);
}
// In Safari, the true version is after "Safari" 
else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
 browserName = "SF";
 fullVersion = nAgt.substring(verOffset+7);
}
// In Firefox, the true version is after "Firefox" 
else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
 browserName = "FF";
 fullVersion = nAgt.substring(verOffset+8);
}
// In most other browsers, "name/version" is at the end of userAgent 
else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) < (verOffset=nAgt.lastIndexOf('/')) ) 
{
 browserName = nAgt.substring(nameOffset,verOffset);
 fullVersion = nAgt.substring(verOffset+1);
 if (browserName.toLowerCase()==browserName.toUpperCase()) {
  browserName = navigator.appName;
 }
}
// trim the fullVersion string at semicolon/space if present
if ((ix=fullVersion.indexOf(";"))!=-1) fullVersion=fullVersion.substring(0,ix);
if ((ix=fullVersion.indexOf(" "))!=-1) fullVersion=fullVersion.substring(0,ix);
majorVersion = parseInt(''+fullVersion,10);
if (isNaN(majorVersion)) {
 fullVersion  = ''+parseFloat(navigator.appVersion); 
 majorVersion = parseInt(navigator.appVersion,10);
}
  browserName = browserName.substring(0,2); 
  b=new BrowserId(browserName,fullVersion,majorVersion);
  return b;
}


function BrowserId (name,full,major) {
 this.name=name;
 this.full=full;
 this.major=major;
}

function URLencode(string) {
 if(encodeURIComponent()) string=encodeURIComponent(string);
 else string=escape(string);
 
 return string;
}

function buildRequest() {
  var request;
  var httpType = new Array();
  var bid=getBrowserId();
  
  if (bid.name=='IE') { 
   if (bid.major<7) {
     httpType = new Array('Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP','Microsoft.XMLHTTP');
   }
  }  
  for (var i = 0; i < httpType.length; i++) {
    try {
		request = new ActiveXObject(httpType[i]);
    }
    catch (e) {
	   request = null;
    }
  } //end for
  if(!request && typeof XMLHttpRequest != "undefined") request = new XMLHttpRequest();
  return request;
 }

function xmlRequest(callBack,type,HandShake) {
  this.routine = callBack;
  this.busy = false;
  this.requestType = type;
  this.handShake = HandShake;
  this.data = '';
  this.Async =false;
}

function clientRequest(url,callBack,Requesttype,HandShake) {
  if(!Requesttype) Requesttype='TEXT';
  if(! HandShake) HandShake = 'GET';
  
  this.request = new xmlRequest(callBack,Requesttype,HandShake);
  this.param = new Array();
  this.invoke = invokePriam;
  this.url = url;
}

function invokePriam(Async) {
  var url = this.url;
  var first = true;
  var param = '';
  var last=false;
  var value;
  var si;
  if(!Async) Async=false; 
 
  for (var i in this.param ) {
    value=this.param[i];
    if(typeof value=='undefined') continue; //FL23343
    if(! value.split) continue;
    si=i;
    if((! si.split)) continue;
    
    var chr = (first) ? '?' : '&';

    if(i=='end' && this.param[i]) last=true;

    if(!last) {
    	if(this.request.handShake == 'POST' && first) chr='';
   	 param += chr + i + '=' + URLencode(this.param[i]);
    	first=false;
   }
  }
 
 var found = false;
 var callBack = this.request.routine;
 var busy= false;
  
 if ( httpRequestId[callBack] ) {
    found = true;
    if(httpRequestId[callBack].busy) busy = true;
 }
  
if(! busy) {
  var http = buildRequest();
  this.request.xmlRequestObject = http;
  httpRequestId[callBack] =  this.request;

  this.request.busy = true;
  if(this.request.handShake == 'POST') {
   http.open('POST', url,Async);
  }
  else {
    url += param;
    http.open('get', url,Async);
  }
 
  this.request.Async= Async;
  if(Async) http.onreadystatechange =  function() { handleResponse(callBack); };
   
  if(this.request.handShake == 'POST') {
    http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    http.send(param);
  }
  else{
   http.send(null);
  }
  
 if(!Async) {
    handleResponse(callBack);
  }
  
 }
}

function handleResponse(handle) {
  var http = httpRequestId[handle];
  var DoRequest = false;
  if (http.xmlRequestObject.readyState ==4 && http.xmlRequestObject.status == 200) DoRequest = true;
  if (! http.Async) DoRequest = true;
  
  if(DoRequest ) {
    	  if(http.requestType == 'XML')  http.data=  http.xmlRequestObject.responseXML;
   	  else http.data=  http.xmlRequestObject.responseText;
    	  var funct = new Function ("handle",http.routine + '(handle)');
         httpRequestId[handle].busy =false;
          http.xmlRequestObject = undefined;
          funct(handle);
    	  httpRequestId[handle] = undefined;
  }
     
}
