/**
   Twelve Horses AJAX Object Class
   Copyright 2006 Twelve Horses
   http://www.twelvehorses.com
*/

  function thajax() {
    this.ajax = null;
    this.method = "GET";
    this.async = true;
    this.error="";
    this.postdata ="";

    this.onError    = function() {};  // Called if there is an error.
    this.onFinished = function() {};  // Called when ready state = 4
    this.onLoad     = function() {};  // Called when ready state = 1
    this.onLoaded   = function() {};  // Called when ready state = 2
    this.createRequestor = function () {
      this.ajax=null;
      if(window.XMLHttpRequest) {  // Native XMLHttpRequest (Mozilla,Safari,Firefox,IE7)
        try {
          this.ajax = new XMLHttpRequest();
        } catch(e) {
          this.ajax=null;
        }
      } else if(window.ActiveXObject) { // IE5 & IE6 
        try {
          this.ajax = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
          try {
            this.ajax = new ActiveXObject("Microsoft.XMLHTTP");
          } catch(e) {
            this.ajax = false;
          }
        }
      }
      return !(this.ajax == null);
    }

    this.setMethod = function (method, async, postdata) {
      this.async  = !async?false:true;
      this.method = method=="POST"?"POST":"GET";
      this.postdata = postdata;
    }

    this.request = function (action, callback, method, async) {
      var self = this;
      if (!this.createRequestor()) return false;
      this.onFinished = callback;

      if (method && async) {
        this.setMethod(method, async);
      }

      this.ajax.onreadystatechange = function () {
        switch (self.ajax.readyState) {
          case 1: // Loading
            self.onLoad();
          break;
          case 2: // Loaded
            self.onLoaded();
          break;
          case 4: // Done
            var error = false;
            if (self.ajax.responseXML) {
              if (self.ajax.responseXML.getElementsByTagName("error").length>=1) {
                self.error = getNodeText(self.ajax.responseXML.getElementsByTagName("error")[0]);
                self.onError();
              }
              if (typeof(self.onFinished)=="function") {
                self.onFinished(self.ajax, self.error);
              }
              else {
                self.error = "Invalid Callback";
                self.onError();
              }
            }
            else {
              self.error = "XML Was Not Returned By AJAX Function";
              self.onError();
            }
          break;
        }
      }

      this.ajax.open(this.method, action, this.async);
      if (this.method=="POST") {
        this.ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
      }
      this.ajax.send(this.postdata);
      this.postdata ="";
    }

    if (!this.createRequestor()) { // Create a requestor object to make sure AJAX is available
      this.error = "Cannot create XMLHttpRequest object.";
      this.onError();
    }
  }

  function getNodeAttribute(node, attribute) {
    return node.getAttribute(attribute);
  }

  function getNodeText(node) {
    if (node.childNodes.length>1) {
      return node.childNodes[1].nodeValue;
    }
    else {
      if (node.firstChild) {
        return node.firstChild.nodeValue;
      }
      else {
        return "";
      }
    }
  }
