var ajax = {

loadDoc : function(url, data, onload, method, cache, async, onerror) {
  this.xhr = this.getXMLHttpRequest();
  this.onload = (onload) ? onload : function() {};
  this.method = (method) ? method : 'POST';
  this.cache = (cache != null) ? cache : true;
  this.async = (async != null) ? async : true;
  this.onerror = (onerror) ? onerror : this.defaultOnError;
  this.load(url, data);
},
// XHR API
loadText : function(url, data, method, cache) {
  try {
    var output = null;
    var onload = function(obj) {
      output = obj.xhr.responseText;
    }
    new this.loadDoc(url, data, onload, method, ((cache) ? cache : null), false, null);
	return output;
  } catch(e) {
    return null;
  }
},

loadXML : function(url, data, method, cache) {
  try {
    var output = null;
    var onload = function(obj) {
      output = obj.xhr.responseXML;
    }
    new this.loadDoc(url, data, onload, method, ((cache) ? cache : null), false, null);
    return output;
  } catch(e) {
    return null;
  }
}
}
ajax.loadDoc.prototype = {
  load : function(url, data) {
    var loader = this;
/*	this.xhr.onreadystatechange = function() {
	  if(loader.xhr.readyState == 4) {
        if(loader.xhr.status == 200)
          loader.onload(loader);
        else
          loader.onerror(3);
      }
    }
*/


    if(!data) this.method = "GET";
    switch(this.method.toUpperCase()) {
      case "GET" :
        if(data) url += ((url.indexOf("?") + 1) ? "&" : "?") + data;
        if(this.cache) url += ((url.indexOf("?") + 1) ? "&" : "?") + "killcache=" + new Date().getTime();
        this.xhr.open("GET", url, this.async);
        this.xhr.send(null);
	  if(loader.xhr.readyState == 4) {
        if(loader.xhr.status == 200)
          loader.onload(loader);
        else
          loader.onerror(3);
      }
        break;
      case "POST" :
        this.xhr.open("POST", url + "?killcache=" + new Date().getTime(), this.async);
        this.xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        this.xhr.setRequestHeader("Content-length", data.length);
        this.xhr.setRequestHeader("Connection", "close");
		this.xhr.send(data);
		  if(loader.xhr.readyState == 4) {
			if(loader.xhr.status == 200)
			  loader.onload(loader);
			else
			  loader.onerror(3);
		  }
        break;
      default :
        this.onerror(2);
        break;
    }
  },
  getXMLHttpRequest : function() {
    var req;

    if(window.XMLHttpRequest) {
      try {
        req = new XMLHttpRequest();
      } catch(e) {
        req = null;
      }
    }
    if(window.ActiveXObject && !req) {
      try {
        req = new ActiveXObject("Msxml2.XMLHTTP");
      } catch(e) {
        try {
          req = new ActiveXObject("Microsoft.XMLHTTP");
        } catch(e) {
          req = null;
        }
      }
    }

    if(!req)
      this.onerror(1);
    else
      return req;
  },
  defaultOnError : function(errno) {
    var errmsgs = {
      1 : "Unable to initialize XMLHttpRequest",
      2 : "Unknow method (known are POST and GET)",
      3 : "Data load error"
    }
    throw errmsgs[errno];
  }
}




var ajaxLibLoaded = true;
