Conjoin.POST='POST';
Conjoin.GET='GET';
function Conjoin(){
  this.parms = new Array();
  function getPipe(){
    if(typeof XMLHttpRequest == 'object' || typeof XMLHttpRequest == 'function') return new XMLHttpRequest();
    else if(typeof ActiveXObject == 'object' || typeof ActiveXObject == 'function') return new ActiveXObject("Msxml2.XMLHTTP");
    else {
       alert("xmlHTTPRequest: "+typeof XMLHttpRequest);
       alert("ActiveXObject: "+typeof ActiveXObject);
       alert("cannot get a pipe");
    }
  }

  function get(url,callback,extraData){
    url = this.prepareUrl(url);
    // GET asynchronous
    if(callback)
      return this.sendAsync(Conjoin.GET,url,callback,null,extraData);
    else if(url)  
      return this.sendSync(Conjoin.GET,url,null);
    else return null;  
  }
  function post(url,callback,extraData){
    var query = this.prepareQuery();
    if(callback)
      return this.sendAsync(Conjoin.POST,url,callback,query,extraData);
    else if(url)  
      return this.sendSync(Conjoin.POST,url,query);
    else return null;  
  }
  function sendSync(type,url,query){
    var pipe = getPipe();
    pipe.open(type,url,false);
    this.submit(pipe,type,query);
    var response = new String(pipe.responseText);
    response.status = pipe.status;
    return response;
  }  
  function sendAsync(type,url,callback,query,extraData){
    var pipe = getPipe();
    var func = function(){
      if(pipe.readyState == 4){
        var response = new String(pipe.responseText);
	response.status = pipe.status;
        callback(response,extraData,pipe);
      }
    }
    pipe.open(type,url,true);
    pipe.onreadystatechange = func;
    this.submit(pipe,type,query);
    return ;
  }
  function submit(pipe,type,query){
    if(type == Conjoin.POST){
      pipe.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    }
    pipe.send(query);
    this.reset();
  }
  // Resets the parameter array used by post.
  function reset(){
    this.parms.length = 0;
  }
  function add(key,value){
    var parms = this.parms;
    parms[parms.length] = {k:key,v:value};
  }
  function prepareQuery(){
    var query = "";
    var parms = this.parms;
    var len = parms.length;
    for(var i=0;i<len;i++){
      query += parms[i].k+"="+parms[i].v+"&"; 
    }
    return query+"Conjoin="+(new Date().getTime());
  }
  // Adds required parameters on input URL.
  function prepareUrl(url){
    if(url.indexOf('?') == -1) url += '?';
    else url += '&';
    return url+this.prepareQuery();
  }
  this.get = get;
  this.post = post;
  this.add = add;
  this.sendAsync = sendAsync;
  this.sendSync = sendSync;
  this.reset = reset;
  this.prepareUrl = prepareUrl;
  this.prepareQuery = prepareQuery;
  this.submit = submit;
}

