/** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
 ** Code licensed under Creative Commons Attribution-ShareAlike License      **
 ** http://creativecommons.org/licenses/by-sa/2.0/                           **/
/**
 * Modified at 2007-05-18 By woodless
 * Email: woodlessr@hotmail.com
 */
function XHConn() {
  this.xmlhttp = false;
  this.isComplete = false;
  try { this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { this.xmlhttp = new XMLHttpRequest(); }
  catch (e) { this.xmlhttp = false; }}}
}
XHConn.Parameter = tom.Class.create();
tom.extend(XHConn.Parameter.prototype, tom.util.Map.prototype, {
  initialize: function(m) {
  	m = m ? m : {};
  	tom.util.Map.prototype.initialize.apply(this, []);
  	tom.extend(this, m);
  },
  append: function(k, v) {
  	this.put(k, v);
  	return this;
  },
  stringValue: function() {
  	var m = this._m;
	var s = "";
  	var i = 0;
  	var v = null;
  	for (var k in m) {
  	  if (i++ > 0) {
  	  	s += "&";
  	  }
  	  v = m[k];
  	  k = k.substring(1, k.length);
  	  s += k + "=" + encodeURIComponent(v);
  	}
  	return s;
  }
});
XHConn.parseResult = function(xhq, options) {
  var opt = {};
  tom.extend(
    opt,
    { text: true, xml: false, json: false},
    options || {}
  );
  var o = {};
  if (opt.text) {
    o["text"] = xhq.responseText;
  }
  if (opt.xml) {
    o["xml"] = xhq.responseXML;
  }
  if (opt.json) {
    o["json"] = eval("(" + xhq.responseText + ")");
  }
  return o;
}
XHConn.prototype = {
  connectAsync: function(url, method, vars, callback) {
    if (!this.xmlhttp) return false;
    this.isComplete = false;
    try {
      if (method.toUpperCase() == "GET") {
        this.xmlhttp.open(method, url + "?" + vars, true);
        vars = "";
      } else {
        this.xmlhttp.open(method, url, true);
        this.xmlhttp.setRequestHeader("Method", "POST " + url + " HTTP/1.1");
        this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      }
      var _this = this;
      this.xmlhttp.onreadystatechange = function() {
        if (_this.xmlhttp.readyState == 4 && !_this.isComplete) {
          _this.isComplete = true;
          callback(_this.xmlhttp);
        }
      };
      this.xmlhttp.send(vars);
    } catch(z) {
    	alert(z.message);
    	return false;
    }
    return true;
  },
  connectSync: function(url, method, vars, options) {
    if (!this.xmlhttp) return false;
    options = options ? options : {text: true, xml: false, json: false};
    this.isComplete = false;
    method = method.toUpperCase();
    try {
      if (method == "GET") {
        this.xmlhttp.open(method, url + "?" + vars, false);
        vars = "";
      } else {
        this.xmlhttp.open(method, url, false);
        this.xmlhttp.setRequestHeader("Method", "POST " + url + " HTTP/1.1");
        this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      }
      this.xmlhttp.send(vars);
    } catch(e) {
    	alert(e.message);
    	return false;
    }
    var o = {};
    if (options.text) {
      o["text"] = this.xmlhttp.responseText;
    }
    if (options.xml) {
      o["xml"] = this.xmlhttp.responseXML;
    }
     if (options.json) {
      o["json"] = eval("(" + this.xmlhttp.responseText + ")");
    }
    return o;
  }
}
XHConn.quickFillGet = function(fillId, url, vars) {
  var xhc = new XHConn();
  xhc.connectAsync(
    url, 
    "get", 
    vars ? vars : "", 
    function(xhq) {
      document.getElementById(fillId).innerHTML = xhq.responseText;
    });
}

