//****************
// Jacques Duclos
//****************

function httpRequest() {
	this.init();
}

httpRequest.prototype.init = function() {
	// Initialisation
}

httpRequest.prototype.send = function(url, postParam, fctDataManagement, asyncFlag) {
	var self 	= this;
	var async	= asyncFlag == undefined ? true : asyncFlag;
	
	if (window.gWait) {
		gWait.show();
	}
	self.sendMain(url, postParam, fctDataManagement, async);
}

httpRequest.prototype.sendMain = function(url, postParam, fctDataManagement, async) {
	// Firefox
	if (window.XMLHttpRequest) {
		this.http = new XMLHttpRequest();

	// Internet Explorer
	} else if(window.ActiveXObject) {
		this.http = new ActiveXObject("Microsoft.XMLHTTP");
	}

	// Mode asynchrone
	if (async) {
		var self = this;

		this.http.open("POST", url, true);
		this.http.onreadystatechange = function() {
			if (self.http.readyState == 4) {
				if (self.http.status == 200) {
					if (self.http.responseXML && self.http.responseXML.hasChildNodes()) {
						fctDataManagement(self.http.responseXML, url, postParam);
					}
					else {
						fctDataManagement(self.http.responseText, url, postParam);
					}

					if (window.gWait) {
						gWait.hide();
					}
				}
			}
		}
		this.http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.http.send(postParam);

	// Mode synchrone
	} else {
		this.http.open("POST", url, false);
		this.http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.http.send(postParam);

		if (this.http.responseXML && this.http.responseXML.hasChildNodes()) {
			fctDataManagement(this.http.responseXML, url, postParam);
		}
		else {
			fctDataManagement(this.http.responseText, url, postParam);
		}

		if (window.gWait) {
			gWait.hide();
		}
	}
}

httpRequest.prototype.getXmlValue = function(fieldName, index) {
	var nodes	= this.http.responseXML.documentElement.childNodes[index].childNodes;
	var nbNodes	= nodes.length;

	for (var i=0; i<nbNodes; i++) {
		if (nodes[i].nodeName == fieldName) {
			return nodes[i].childNodes[0].nodeValue.htmlDecode();
		}
	}
	return null;
}

httpRequest.prototype.xmlToSelect = function(textFieldName, valueFieldName, select) {
	var fils		= this.http.responseXML.documentElement.childNodes;
	var nbFils	= fils.length;

	select.length = 0;

	for (var i=0; i<nbFils; i++){
		var sousFils	= fils[i].childNodes;
		var nbSousFils	= sousFils.length;
		var text;
		var value;

		for (var j=0; j<nbSousFils; j++) {
			switch (sousFils[j].nodeName) {
			case textFieldName:
				text = sousFils[j].childNodes[0].nodeValue.htmlDecode();
				break;
			case valueFieldName:
				value = sousFils[j].childNodes[0].nodeValue.htmlDecode();
				break;
			}
		}

		select.options.add(new Option(text, value));
	}
}

httpRequest.prototype.xmlToArray = function() {
	var fils		= this.http.responseXML.documentElement.childNodes;
	var nbFils	= fils.length;
	var result	= new Array();

	for (var i = 0; i < nbFils; i++){
		var sousFils	= fils[i].childNodes;
		var nbSousFils	= sousFils.length;
		var element		= new Array();

		for (var j = 0; j < nbSousFils; j++) {
			element[j] = sousFils[j].childNodes[0].nodeValue.htmlDecode();
		}

		result[i] = element;
	}

	return result;
}