// XmlHttp2 component

/**
 * @author Andrey Gubar
 */

// --------------------------------------

function paramDesc(name, value) {
  this.name = name;
  this.value = value;
}

function xmlhttp2() {
  this.xmlHttp = null;
  this.responseText = null;
  this.responseXML = null;
  this.error = null;
  this.callback = null;
  this.externalData = null;
}

xmlhttp2.prototype = {

  request : function(url, method, data, async, callback, externalData, contentType) {
    if(url == null) return;
    if(method == null) method = "GET";
    if(async == null) async = true;
    if(contentType == null) contentType = "application/x-www-form-urlencoded; charset=utf-8";    
    
    this.externalData = externalData;
    this.callback = callback;

    if(this.xmlHttp != null && this.xmlHttp.readyState != 4) return;
    this.xmlHttp = null;
    if (window.XMLHttpRequest) { // IE7 or Non-IE browsers
      this.xmlHttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE5.x or IE6
      this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if(this.xmlHttp != null) {
      var _xmlHttp2 = this;
      this.xmlHttp.onreadystatechange = function() {
        if (_xmlHttp2.xmlHttp.readyState == 4) {
          // Request complete
          if (_xmlHttp2.xmlHttp.status == 200 || _xmlHttp2.xmlHttp.status == 304 ||
              (window.location.protocol == 'file:' && !_xmlHttp2.xmlHttp.status)) {
            // OK or found, but determined unchanged and loaded from cache
            _xmlHttp2.responseText = _xmlHttp2.xmlHttp.responseText;
            _xmlHttp2.responseXML = _xmlHttp2.xmlHttp.responseXML;
            _xmlHttp2.error = null;
          } else {
		        _xmlHttp2.error = _xmlHttp2.xmlHttp.statusText;
          }
          if (typeof(_xmlHttp2.callback) == 'function') {
            _xmlHttp2.callback(_xmlHttp2);
          }
        }
      };
    }
    try {
      this.responseText = null;
      this.responseXML = null;
      this.error = null;
      this.xmlHttp.open(method, url, async);
      this.xmlHttp.setRequestHeader("Content-Type", contentType);
      this.xmlHttp.setRequestHeader("Pragma", "no-cache");
      this.xmlHttp.setRequestHeader("Referer", document.URL);
      this.xmlHttp.setRequestHeader("User-Agent", window.navigator.userAgent);
      this.xmlHttp.send(data);
    } catch(e) {
    }
  },
  
  makeBoundary : function() {
		var dt = new Date();
		return "--------------------------------------------------------" + escape(dt.toUTCString());
  },

  makeContentType : function(boundary) {
    return "multipart/form-data; charset=utf-8; boundary=" + boundary + "";
  },
  
  makeGetParams : function(params) {
	  if(params == null) return;
	  var formData = "";
	  for(var i = 0, n = params.length; i < n; i++) {
		  formData += (i > 0 ? "&" : "") + params[i].name + "=" + params[i].value;
	  }
	  return formData;
  },
    
  makePostParams : function(params, boundary) {
	  if(params == null) return;
	  var formData = "";
	  for(var i = 0, n = params.length; i < n; i++) {
		  formData += "--" + boundary + "\r\n";
		  formData += "Content-Disposition: form-data; name=\"" + params[i].name + "\"\r\n";
		  formData += "\r\n";
		  formData += "" + params[i].value + "\r\n";
	  }
	  formData += "--" + boundary + "--\r\n";
	  return formData;
  }  
}