/*
this file is intended to contain the AJAX class and support functions that can be used globally
page specific functions are still to be saved in the xp_page_name.js files
Full reference on the construction of the Ajax class can be found at:
http://www.netregistry.com.au/news/articles/104/1/Build-Your-Own-AJAX-Web-Applications-Part-13/Page1.html
*/

//create an instance of the ajax class
//begin ajax class
function Ajax() {
this.req = null;
this.url = null;
this.method = null;
this.async = true;
this.status = null;
this.statusText = '';
this.postData = null;
this.readyState = null;
this.responseText = null;
this.responseXML = null;
this.handleResp = null;
this.responseFormat = 'text', // 'text', 'xml', or 'object';
this.mimeType = null;

  //create XMLHttpRequest Object
  this.init = function() {
    if (!this.req) {
      try {
      this.req = new XMLHttpRequest(); //try FireFox, Safari, MSIE 7
      }
      catch (e) {
        try {
        this.req = new ActiveXObject('MSXML2.XMLHTTP'); //try MSIE < 7, > 5.5
        }
        catch (e) {
          try {
          this.req = new ActiveXObject('Microsoft.XMLHTTP'); //try MSIE > 5.5
          }
          catch (e) {
            return false; //unable to create object
          }
        }
      }
    }
    return this.req;
  };

 //request method
  this.doReq = function() {
  if (!this.init()) {
    alert('Could not create XMLHttpRequest Object');
    return;
  }
  this.req.open(this.method,this.url,this.async);
  if (this.mimeType) {
    try {
      req.overrideMimeType(this.mimeType);
    }
    catch (e) {
    alert('error')
    }
  }
  var self = this;
    //handle the response
    this.req.onreadystatechange = function() {
      var resp = null;
      if (self.req.readyState == 4) {
        switch (self.responseFormat) {
          case 'text':
            resp = self.req.responseText;
            break;
          case 'xml' :
            resp = self.req.responseXML;
            break;
          case 'object' :
            resp = req;
            break;
        }
        if (self.req.status >= 200 && self.req.status <= 299) {
          self.handleResp(resp);
        } else {
          self.handleErr(resp);
        }
      }
    };
    //this.req.send(this.postData);
    switch (self.method) {
    case 'POST':
      this.req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      this.req.send(this.postData);
      break;
    case 'GET' :
      this.req.send(null);
      break;
    }

  };



  //Handle errors (this version tries to create a popup win while we want to have all messages in div so we need to replace this ASAP)
  this.handleErr = function() {
  var errorWin;
    try {
    errorWin = window.open('','errorWin');
    errorWin.document.body.innerHTML = this.responseText
    }
    catch(e) {
    alert('Couldn\'t open popupwin!!\n'
           + 'Anyway, the error was:\n'
           + 'Status Code : '+this.req.status+'\n'
           + 'Status Description : '+this.req.statusText+'\n'
           + 'Go figure');
    }
  };

    //set the mimeType
  this.setMimeType = function(mimeType) {
  this.mimeType = mimeType;
  };

  //abort function
  this.abort = function() {
    if (this.req) {
    this.req.onreadystatechange = function() {};
    this.req.abort();
    this.req = null;
    }
  };

  //GET function
  this.doGet = function(url, hand, format) {
    this.method = 'GET';
    this.url = url;
    this.handleResp = hand;
    this.responseFormat = format || 'text';
    this.doReq();
  };

  this.doPost = function(url,postData,hand,format) {
    this.method = 'POST';
    this.url = url;
    this.postData = postData;
    this.handleResp = hand;
    this.responseFormat = format || 'text';
    this.doReq();
  };


}
//end ajax class