/**
 * This class represent an http REquest.
 * Compatible : NA
 * @package DHL::NET
 * @author Inoveo technologie inc.
 */
if (DHL == undefined) var DHL = {};
if (DHL.NET == undefined) DHL.NET = {};
DHL.NET.HTTPREQUEST = function () {
	//Init the objXmlHttp object
	
	//Private Member {{{
	this.objXmlHttp = false;
	//}}}
	
	//Public member {{{
	this.jsClass = DHL.NET.HTTPREQUEST;
	//}}}
	
	//Initialisation {{{
	try {
		this.objXmlHttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object…
	} catch (e) {
		try {
			this.objXmlHttp = new ActiveXObject('Microsoft.XMLHTTP'); // try the second kind of active x object
		}
		catch (E) {
			this.objXmlHttp = false;
		}
	}
	if (!this.objXmlHttp && typeof XMLHttpRequest!='undefined') 
		this.objXmlHttp = new XMLHttpRequest(); // if we were able to get a working active x object, start an XMLHttpRequest
	//}}}
}

var PUBLIC = DHL.NET.HTTPREQUEST.prototype;
var PRIVATE = DHL.NET.HTTPREQUEST.prototype;
var STATIC = DHL.NET.HTTPREQUEST; 
var SELF = DHL.NET.HTTPREQUEST; 

//Public Method {{{
	
/**
  * Abort the current operation
  * @param void
  * @return void
  */
PUBLIC.abort = function() {
	if(this.objXmlHttp)
		return this.objXmlHttp.abort();
	return false;
}

/**
 * Set request header field
 * @param string
 * @param string
 * @return 
 */
PUBLIC.setRequestHeader = function(strName, strValue) {
	if(this.objXmlHttp) {
		this.objXmlHttp.setRequestHeader(strName, strValue);
	}
}

/**
 * Return the header of the request
 * @param void
 * @return string
 */
PUBLIC.getAllResponseHeaders = function() {
	var strReturn = "";
	if(this.objXmlHttp)
		strReturn = this.objXmlHttp.getAllResponseHeaders();
	return strReturn;
}

/**
 * Return the header of the request
 * @param string strHeader
 * @return string
 */
PUBLIC.getResponseHeader = function(strHeader) {
	var strReturn = "";
	if(this.objXmlHttp)
		strReturn = this.objXmlHttp.getResponseHeaders(strHeader);
	return strReturn;
}

/**
 * Open a connection with to the specified url
 * @param string strMethod (POST, GET)
 * @param object objUrl DHL.NET.URL
 * @param boolean blnAsync
 * @param string strUsername
 * @param string strPassword
 * @return boolean
 */
PUBLIC.open = function(strMethod, objUrl, blnAsync, strUserName, strPassword) {
	if(this.objXmlHttp)
		return this.objXmlHttp.open(strMethod, objUrl.toString(), blnAsync, strUserName, strPassword);
	return false;
}

/**
 * Work like send but data must be an array.
 * @param Array aData
 * @return
 */
PUBLIC.post = function(aData) {
	var sData = '';
	for(i in aData) {
		var sType = typeof(aData[i]);
		if (
			sType == 'boolean' || 
			sType == 'string' || 
			sType == 'number' || 
			aData[i] == null
		) {
			sData += (sData ? '&' : '') + escape(i) + '=' + escape(aData[i]);
		}
		
	}
	this.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=ISO-8859-1');
	this.setRequestHeader('Content-Length', sData.size);
	return this.send(sData);
}

/**
 * Transamit the request, optionaly the strData (string / DOM object data)
 * @param string strData
 * @return boolean
 */
PUBLIC.send = function(strData) {
	if(this.objXmlHttp) {
		this.objXmlHttp.send(strData);
	}
}

/**
 * Method fired when the state of the xmlHttp is change
 * @param string strFunction
 * @return void
 */
PUBLIC.onreadystatechange = function (strFunction) {
	if(this.objXmlHttp)
		this.objXmlHttp.onreadystatechange = strFunction;
}

/**
 * Return the ready state of the http request 
 * 0 = The request is not initialized
 * 1 = The request has been set up
 * 2 = The request has been sent
 * 3 = The request is in process
 * 4 = The request is completed
 * @param void
 * @return int
 */
PUBLIC.getReadyState = function () {
	var intReturn = 0;
	if(this.objXmlHttp)
		intReturn = this.objXmlHttp.readyState;
	return intReturn;
}

/**
 * Return the response text of the object
 * @param void
 * @return string
 */
PUBLIC.getResponseText = function () {
	var responseText = "";
	if(this.objXmlHttp)
		responseText = this.objXmlHttp.responseText;
	return responseText;
}

/**
 * Return the DOM version of the server response
 * @param void
 * @return object (DOM-compatible)
 */
PUBLIC.getResponseXMl = function() {
	var strDomResponse = null;
	if(this.objXmlHttp)
		strDomResponse = this.objXmlHttp.responseXML;
	return strDomResponse;
}

/**
 * return the numeric status of the request
 * @param void
 * @return int
 */
PUBLIC.getStatus = function() {
	var intStatus = 0;
	if(this.objXmlHttp)
		intStatus = this.objXmlHttp.status;
	return intStatus;
}

/**
 * Return the Text version of the status
 * @param void
 * @return string
 */
PUBLIC.getStatusText = function() {
	var strStatus = 0;
	if(this.objXmlHttp) {
		strStatus = this.objXmlHttp.statusText;
	}
	return strStatus;
}

if (DHL.NET.HTTPREQUEST.className == undefined) {
	DHL.NET.HTTPREQUEST.className = 'DHL.NET.HTTPREQUEST';
	
	DHL.NET.HTTPREQUEST.statusNotInitialized = 0;
	DHL.NET.HTTPREQUEST.statusSetUp = 1;
	DHL.NET.HTTPREQUEST.statusSent = 2;
	DHL.NET.HTTPREQUEST.statusInProcess = 3;
	DHL.NET.HTTPREQUEST.statusReceived = 4;
}
//}}}


