/**
 * TRAXI - Tom Reznak's AjaX Interface
 *
 * @author  Tomas Reznak (tomas.reznak@cdv.cz)
 * @date    2007-07-20
 */


/**
 * constructor
 *
 * @return void
 */
function traxi() {
  this.xmlhttp = null;
  this.method = "POST";
  this.url_var_separator = "&";
  this.action_script = "ajax.php";
  this.charset = "UTF-8";
  this.execute_response = false;
  
  // onreadystatechange functions
  this.onLoading = function() {};
  this.onLoad = function() {};
  this.onInteractive = function() {};
  this.onSuccess = function() {};
  this.onFailure = function() {};
  
  this.vars = new Array();
  
  this.initialize();
}

traxi.prototype = {
  /**
   * get object parameter value
   *
   * @param string param    parameter name
   * @return mixed          parameter value
   */
  get : function(param) {
    return this.vars[param];
  },

  /**
   * set object parameter to given value
   *
   * @param string param    parameter name
   * @param mixed value     parameter value
   * @return void;
   */
  set : function(param, value) {
    this.vars[param] = value;
  },

  /**
   * initialize XMLHttp object
   *
   * @return void;
   */
  initialize : function() {
    if (window.XMLHttpRequest) {
      this.xmlhttp = new XMLHttpRequest;
    } else {
      if (window.ActiveXObject) {
        this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } else {
        this.xmlhttp = false;
      }
    }
    
    if (this.xmlhttp) {
      var obj = this;

      this.xmlhttp.onreadystatechange = function() {
        switch (obj.xmlhttp.readyState) {
          case 1 : {
            obj.onLoading();
            
            break;
          }
          
          case 2 : {
            obj.onLoad();
            
            break;
          }
          
          case 3 : {
            obj.onInteractive();
            
            break;
          }
          
          case 4 : {
            obj.response = obj.xmlhttp.responseText;
            obj.response_xml = obj.xmlhttp.responseXML;
            obj.response_status = obj.xmlhttp.status;
            obj.response_status_text = obj.xmlhttp.statusText;

            if (obj.response_status == "200") {
              if (obj.execute_response) {
                eval(obj.response);
              }      
      
              obj.onSuccess();
            } else {
              obj.onFailure();
            }

            obj.url_string = "";
			
            break;               
          }
        }
      }    
    
      return true;
    }
    
    return false;
  },

  /**
   * process AJAX request
   *
   * @return void;
   */
  run : function() {
    if (this.xmlhttp) {
      if (this.method == "POST") {    // send data via POST protocol
        this.xmlhttp.open(this.method, this.action_script, true);
        try {
          this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=" + this.charset);
        } catch (e) { }
      } else {    // send data via GET protocol
        var url = this.action_script + this.url_query_separator + this.url_string;
        this.xmlhttp.open(this.method, url, true);
      }
      
      this.xmlhttp.send(this.createUrl());
    }
  },

  /**
   * create url from stored variables
   *
   * @return void;
   */  
  createUrl : function() {
    this.set("rnd", new Date() . getTime());    // don't cache results
    
    var url_vars = new Array();
    for (param in this.vars) {
      url_vars[url_vars.length] = param + "=" + this.vars[param];
    }
    
    var url = url_vars.join(this.url_var_separator);
    
    return url;
  }
  
}    // end of class traxi
