﻿/** 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 by woodless 2007-07-18
 */
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.prototype = {
  connectAsync: function(url, method, vars, callback) {
    if (!this.xmlhttp) return false;
    this.isComplete = false;
    method = method.toUpperCase();
    try {
      if (method == "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) {
    	return false;
    }
    return true;
  },
  connectSync: function(url, method, vars) {
    if (!this.xmlhttp) return 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, true);
        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);
    	return false;
    }
    return {
      text: this.xmlhttp.responseText,
      xml: this.xmlhttp.responseXML};
  }
}
XHConn.quickFillGet = function(fillId, url, vars) {
  var xhc = new XHConn();
  xhc.connectAsync(
    url, 
    "get", 
    vars ? vars : "", 
    function(xhq) {
      document.getElementById(fillId).innerHTML = xhq.responseText;
    });
}// JScript 文件

