function AjaxCSI() {}

// HTTPリクエストオブジェクトを返します
AjaxCSI.prototype.createXMLHttpRequest = function() {
   return window.XMLHttpRequest ? 
      new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
}

// 非同期でHTTPリクエストを実行します
AjaxCSI.prototype.httpAsync = function(uri, getfunc, method, data, type) {
  if (this.request) {
    this.request.abort();
  }
  this.request = this.createXMLHttpRequest();
  var ajaxcsi = this;
  this.request.onreadystatechange = function() {
    if (ajaxcsi.request.readyState == 4) {
      var stateId = 0;
	  if (ajaxcsi.request.status == 200) {
        getfunc(ajaxcsi.request);
	  }
	}
  };
  if (!method) {
	  method = 'GET';
  }
  this.request.open(method, uri, true);
  if (!data) {
	  data = null;
  }
  else if (method == 'POST') {
	  if (!type) {
		  type = "application/x-www-form-urlencoded";
	  }
	  this.request.setRequestHeader("Content-type", type);
	  this.request.setRequestHeader("Content-length", data.length);
	  this.request.setRequestHeader("Connection", "close");
  }
  this.request.setRequestHeader("If-Modified-Since", "Thu, 01 Dec 1994 16:00:00 GMT");
  this.request.send(data);
}

// リクエストの内容をテキストとして返します
AjaxCSI.prototype.ajaxGetText = function(request) {
    var text = request.responseText;
    if ( this.browser == 'khtml' ) {
      var esc = escape( text );
      if ( esc.indexOf("%u") < 0 && esc.indexOf("%") > -1 ) {
        text = decodeURIComponent( esc );
      }
    }
    return text;
}

// 非同期でテキストを取得します
AjaxCSI.prototype.httpAsyncText = function(uri, getfunc, method, data, type) {
	var ajaxcsi = this;
	this.httpAsync(uri, function(request) {
    getfunc(ajaxcsi.ajaxGetText(request));
  }, method, data, type);
}

//非同期でXMLを取得します
AjaxCSI.prototype.httpAsyncXML = function(uri, getfunc, method, data, type) {
	this.httpAsync(uri, function(request) {
    getfunc(request.responseXML);
  }, method, data, type);
}

//非同期でテキストとXMLを取得します
AjaxCSI.prototype.httpAsyncBoth = function(uri, getfunc, method, data, type) {
	var ajaxcsi = this;
	this.httpAsync(uri, function(request) {
    getfunc(ajaxcsi.ajaxGetText(request), request.responseXML);
  }, method, data, type);
}

/**
* URIのエンコード
*/
function m_encodeURI(uri) {
	uri = encodeURI(uri);
	uri = uri.replace(/=/g, '%3D');
	uri = uri.replace(/;/g, '%3B');
	uri = uri.replace(/&/g, '%26');
	uri = uri.replace(/#/g, '%23');
	uri = uri.replace(/\n/g, '%0D');
	uri = uri.replace(/\r/g, '%0A');
	return uri;
}

/**
* パラメータ化
*/
function m_toPostParameters(props) {
	var data = '';
	var and = false;
	for(var i in props) {
		var name = m_encodeURI(i);
		var value = m_encodeURI(props[i]);
		if (and) {
			data += '&';
		}
		else {
			and = true;
		}
		data += name+'='+value;
	}
	return data;
}

