//-----------------------------------------------------------------
// Class HTTPRequest1
//
//-----------------------------------------------------------------		

function HTTPRequest1(sURL, bAsync, sMethod) {
	this.construct(sURL, bAsync, sMethod);
} 
		
//-----------------------------------------------------------------
// Method HTTPRequest1.construct()
//-----------------------------------------------------------------
HTTPRequest1.prototype.construct = function (sURL, bAsync, sMethod) {
	this.url = sURL;
	this.async = (bAsync != null) ? bAsync : false;
	this.requestMethod = (sMethod != null) ? sMethod : 'GET';
	this.onload = null;
	this.http = null;
	this.namespaces = '';
	this.username = null;
	this.password = null;
	this.parameter = new Array();
	this.parameters = '';
	this.header = new Array();
	this.headers = '';
	this.requestData = '';
	this.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

}  // constructor

//-----------------------------------------------------------------
// Method HTTPRequest1.setOnLoad()
//-----------------------------------------------------------------
HTTPRequest1.prototype.setOnLoad = function (fnFunction) {
	this.onload = fnFunction;		
}				

//-----------------------------------------------------------------
// Method HTTPRequest1.setNameSpace()
//-----------------------------------------------------------------
HTTPRequest1.prototype.setNameSpace = function (sName, sValue) {
	this.namespaces += ' xmlns:' + sName + '="' + sValue + '"';		
}		

//-----------------------------------------------------------------
// Method HTTPRequest1.setParameter()
//-----------------------------------------------------------------
HTTPRequest1.prototype.setParameter = function(sName, sValue) {
	this.parameter[sName] = sValue;
	if (!this.parameters.match(sName)) {this.parameters += " " + sName}
}


//-----------------------------------------------------------------
// Method HTTPRequest1.setRequestHeader()
//-----------------------------------------------------------------
HTTPRequest1.prototype.setRequestHeader = function(sName, sValue) {
	this.header[sName] = sValue;
	if (!this.headers.match(sName)) {this.headers += ' ' + sName}
}

//-----------------------------------------------------------------
// Method HTTPRequest1.getResponseHeader()
//-----------------------------------------------------------------
HTTPRequest1.prototype.getResponseHeader = function(sName) {
	return this.http ? this.http.getResponseHeader(sName) : '';
}

//-----------------------------------------------------------------
// Method HTTPRequest1.responseText()
//-----------------------------------------------------------------
HTTPRequest1.prototype.responseText = function() {
	return this.http ? this.http.responseText : '';
}

//-----------------------------------------------------------------
// Method HTTPRequest1.responseXML()
//-----------------------------------------------------------------
HTTPRequest1.prototype.responseXML = function() {
	return this.http ? this.http.responseXML : '';
}

//-----------------------------------------------------------------
// Method HTTPRequest1.createHttpObject()
//-----------------------------------------------------------------
HTTPRequest1.prototype.createHttpObject = function() {
	if ( window.ActiveXObject ) {
		var _MSXMLSSIGNATURES_ = [	"MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0",
                          			"MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP.2.0",
                          			"MSXML2.XMLHTTP"];
	   var bFound = false;
	   for ( var i = 0; i < _MSXMLSSIGNATURES_.length; i++ ) {
	      try {
	         var oHttp = new ActiveXObject(_MSXMLSSIGNATURES_[i]);
	         return oHttp;
	      } 
	      catch (e) {
	      	// ignore
	      } // end try
	   } // end for
	   return false;
	} else {
		return new XMLHttpRequest;
	}
}

//-----------------------------------------------------------------
// Method HTTPRequest1.request()
//-----------------------------------------------------------------		
HTTPRequest1.prototype.request = function() {	
	if ( this.async && arguments.length > 1 ) {
		this.readyObject = arguments[0];
		this.readyMethod = arguments[1];
	} else {
		this.readyObject = null;
		this.readyMethod = null;
	}
	var self = this;
	this.ready = false;
	var i, name, value, data = '', params = this.parameters.split(' ');
	for ( i=1; i < params.length; i++ ){
		name = params[i];
		value = this.parameter[name];
		if (typeof value == 'function') { value = value(); }
		data += name + '=' + encodeURIComponent(value) + '&';
	}

	var URL = this.url;

	if ((this.requestMethod != 'POST') && data) {
		URL += '?' + data;
		data = null;
	}

	this.http = this.createHttpObject();
	if ( this.onload )
		this.http.onload = this.onload;
	
	this.http.open( this.requestMethod, URL, this.async, this.username, this.password );

	var headers = this.headers.split(' ');
	for ( i=1; i < headers.length; i++ ){
		name = headers[i];
		value = this.header[name];
		if (typeof value == 'function') { value = value(); }
		this.http.setRequestHeader( name, value );
	}

	this.http.send( data );

	if ( this.async ) {
		this.http.onreadystatechange = changestate;
	}

	function changestate(){
		if ( self.http.readyState == 4 ) {
			self.ready = true;
			self.callReadyFunction();
		}
		return;
	}
}

//-----------------------------------------------------------------
// Method HTTPRequest1.callReadyFunction()
//-----------------------------------------------------------------		
HTTPRequest1.prototype.callReadyFunction = function() {
	if ( this.readyObject && this.readyMethod )
		this.readyObject[this.readyMethod].apply(this.readyObject, [this]);
}

//-----------------------------------------------------------------
// Method HTTPRequest1.getResponse()
//-----------------------------------------------------------------		
HTTPRequest1.prototype.getResponse = function() {
	if ( this.http.responseXML && this.http.responseXML.hasChildNodes() ) {
		return this.http.responseXML;
	}
	else {
		return this.http.responseText;
	}
	
}

//-----------------------------------------------------------------
// Method HTTPRequest1.getResponseStr()
//-----------------------------------------------------------------		
HTTPRequest1.prototype.getResponseStr = function() {
	var str;
	if ( this.http.responseXML && this.http.responseXML.hasChildNodes() ) {
		try {
			var s = new XMLSerializer();
			var d = this.http.responseXML;
			str = s.serializeToString(d);
		} 
		catch (e) {
			str = this.http.responseXML.xml;
		}
		return str;
	}
	else {
		return this.http.responseText;
	}
}

//-----------------------------------------------------------------
// Method HTTPRequest1.isReady()
//-----------------------------------------------------------------		
HTTPRequest1.prototype.isReady = function() {
	return this.ready;	
}
