/***
* AJAX Class
***/
function AJAX() {

    this.mode = ":";
    this.url = "";
    this.queryString = "";
    this.arr_get = new Object();
    this.threadSafe = false;
    this.requestDone = false;
    this.setURL("/RequestProcessor/main.php");
    this.alertOnBrokenConnection = true;
    this.brokenConnection = false;

    this.postrequest_function = ""; //postrequest_function;
    this.callback = 'response_callback';
    this.frm = '';
    this.debug = false;
}

AJAX.prototype.setCallBack = function(f) {
    this.callback = f;
}

AJAX.prototype.setURL = function(url) {
    this.url = url
}

AJAX.prototype.setQueryString = function(str) {
    this.queryString = str;
}

AJAX.prototype.threadSafe = function(bool) {
    this.threadSafe = bool;
}

AJAX.prototype.response_callback = function(response) {
    response = response.documentElement;

    this.XMLParser = new FormHandlerXMLParser;
    this.XMLParser.parse(response);
}

AJAX.prototype.send = function() {
  if (this.url == "")
    return alert("no url specified");

  function ajaxBindCallback(){
    if (ajaxRequest.readyState == 4) {
      if (ajaxRequest.status == 200) {
        if (obj.debug)
          alert(ajaxRequest.responseText);
        if (ajaxCallback){
          eval("obj." + ajaxCallback + '(ajaxRequest.responseXML)');
          eval(obj.postrequest_function + "(obj)");
          return true;
        }
        else {
          alert('no callback defined');
          return false;
        }
      }
      else {
        if (this.alertOnBrokenConnection)
          alert("There was a problem retrieving the xml data (Error # " + ajaxRequest.status + ")");

        this.brokenConnection = true;
        return false;
      }
    }
  }

  var url = this.url;
  var now = new Date();
  var obj = this;
  var ajaxCallback = this.callback;

  if (this.url.indexOf('?') == -1)
    url += '?time=' + now.getTime();
  else
    url += '&time=' + now.getTime();

  //Mozzilla
  if (window.XMLHttpRequest) {

    var ajaxRequest = new XMLHttpRequest();

    if (ajaxRequest.overrideMimeType)
      ajaxRequest.overrideMimeType('text/xml');
    //else
      //alert('Your browser does not support this function');

    ajaxRequest.onreadystatechange = ajaxBindCallback;

    if (this.mode == "post") {
      ajaxRequest.open('POST', url, true);
      ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      ajaxRequest.setRequestHeader("Content-length", this.queryString.length);
      ajaxRequest.setRequestHeader("Connection", "close");
      ajaxRequest.send(this.queryString);
    }
    else {
      ajaxRequest.open("GET", url + "&" + this.queryString, true);
      ajaxRequest.send(null);
    }
  }

  //IE
  else if (window.ActiveXObject) {
    var ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");

    if (ajaxRequest)
      ajaxRequest.onreadystatechange = ajaxBindCallback;
    //else
     // alert('Your browser does not support this function');

    if (this.mode == "post") {
      ajaxRequest.open('POST', url, true);
      ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      ajaxRequest.setRequestHeader("Content-length", this.queryString.length);
      ajaxRequest.setRequestHeader("Connection", "close");
      ajaxRequest.send(this.queryString);
    }
    else {
      ajaxRequest.open("GET", url + "&" + this.queryString, true);
      ajaxRequest.send();
    }
  }

  else {
    //return alert('Your browser does not support this function');
  }
}

AJAX.prototype.setget = function(index, value) {
  this.arr_get[index] = this.URLEncode(value);
}

AJAX.prototype.get = function() {
  var str = "";

  for (var i in this.arr_get)
  {
    str += i + "=" + this.arr_get[i] + "&";
  }

  this.mode = "get";
  this.setQueryString(str);

  this.send();
}

AJAX.prototype.post = function(form) {
   this.frm = form;
   this.mode = "post";

   var getstr = '';

   for (i=0; i < form.elements.length; i++) {
      el = form.elements[i];
      if (el.name != '') {
        if (el.type == "SELECT")
          getstr += el.name + "=" + this.URLEncode(el.options[el.selectedIndex].value) + "&";
        else if (el.type == "checkbox")
        {
          if (el.checked)
            getstr += el.name + "=" + this.URLEncode(el.value) + "&";
        }
        else
          getstr += el.name + "=" + this.URLEncode(el.value) + "&";
      }
   }

   for (var i in this.arr_get)
   {
     getstr += i + "=" + this.arr_get[i] + "&";
   }

   this.setQueryString(getstr);
   this.send();
}

AJAX.prototype.URLDecode = function(url)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef";
   var encoded = url;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
           if (ch == "+") {
               plaintext += " ";
                   i++;
           } else if (ch == "%") {
                        if (i < (encoded.length-2)
                                        && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1
                                        && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
                                plaintext += unescape( encoded.substr(i,3) );
                                i += 3;
                        } else {
                                alert( 'Bad escape combination near ...' + encoded.substr(i) );
                                plaintext += "%[ERROR]";
                                i++;
                        }
                } else {
                   plaintext += ch;
                   i++;
                }
        } // while
   return plaintext;
}

AJAX.prototype.URLEncode = function(url)
{
        // The Javascript escape and unescape functions do not correspond
        // with what browsers actually do...
        var SAFECHARS = "0123456789" +                                        // Numeric
                                        "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +        // Alphabetic
                                        "abcdefghijklmnopqrstuvwxyz" +
                                        "-_.!~*'()";                                        // RFC2396 Mark characters
        var HEX = "0123456789ABCDEF";

        if (!url || url == '')
          return '';

        var plaintext = url;
        var encoded = "";

        if (plaintext > 0)
          return plaintext;

        for (var i = 0; i < plaintext.length; i++ ) {
                var ch = plaintext.charAt(i);

            if (ch == " ") {
                    encoded += "+";                                // x-www-urlencoded, rather than %20
                } else if (SAFECHARS.indexOf(ch) != -1) {
                    encoded += ch;
                } else {
                    var charCode = ch.charCodeAt(0);
                        if (charCode > 255) {
                            alert( "Unicode Character '"
                        + ch
                        + "' cannot be encoded using standard URL encoding.\n" +
                                          "(URL encoding only supports 8-bit characters.)\n" +
                                                  "A space (+) will be substituted." );
                                encoded += "+";
                        } else {
                                encoded += "%";
                                encoded += HEX.charAt((charCode >> 4) & 0xF);
                                encoded += HEX.charAt(charCode & 0xF);
                        }
                }
        } // for

        return encoded;
}