function CArchive(p_sScriptFile,
					p_sMethod,
					p_sEncoding,
					p_sTarget,
					p_sName,
					p_nLength,
					p_sOnReceive,
					p_arParam){
	var m_sMethod;
	var m_sAction;
	var m_sEncoding;
	var m_sTarget;
	var m_sName;
	var m_Length;
	var m_sOnReceive;
	var m_arParam;
	var m_objHTTPConnection;

	this.m_sAction		= p_sScriptFile;
	this.m_sMethod		= p_sMethod;
	this.m_sEncoding	= p_sEncoding;
	this.m_sTarget		= p_sTarget;
	this.m_sName		= p_sName;
	this.m_Length		= p_nLength;
	this.m_sOnReceive	= p_sOnReceive;
	
	this.m_arParam		= p_arParam;

	if (typeof CArchive_prototype == 'undefined'){
		CArchive_prototype = true;

		CArchive.prototype.close	= CArchive_close;
		CArchive.prototype.query	= CArchive_query;
		CArchive.prototype.load		= CArchive_load;
		CArchive.prototype.store	= CArchive_store;
	}
}

function CArchive_close(){
	this.m_objHTTPConnection.close();
	this.m_objHTTPConnection = null;
}

function CArchive_query(){
	this.m_objHTTPConnection = new HTTPConnection();

	if(this.m_sMethod == "GET"){
		this.m_sAction += "?OnReceive="+this.m_sOnReceive;
		for (var i = 0; i < this.m_arParam.length; i++)
			this.m_sAction += "&" + this.m_arParam[i]["param"] + "=" + QuoteString(this.m_arParam[i]["value"]);
	}else if (this.m_sMethod == "POST"){
		this.m_objHTTPConnection.setOnReceive(this.m_sOnReceive);
	}

	//alert( "this.m_sAction = " + this.m_sAction );
	//alert( "this.m_arParam = " + this.m_arParam );

	this.m_objHTTPConnection.setMethod(this.m_sMethod);
	this.m_objHTTPConnection.setAction(this.m_sAction);
	this.m_objHTTPConnection.setEncoding(this.m_sEncoding);
	this.m_objHTTPConnection.setTarget(this.m_sTarget);
	this.m_objHTTPConnection.setName(this.m_sName);	
	
	this.m_objHTTPConnection.send(this.m_arParam)
}

function CArchive_load(){
}

function CArchive_store(){
}

/**********************************************************************
 *
 **********************************************************************/
function HTTPConnection(){
	var m_sAction;
	var m_sMethod;
	var m_sEncoding;
	var m_sTarget;
	var m_sName;
	var m_sOnReceive;
	var m_eQueryFrame;

	this.m_eQueryFrame = null;
	if (typeof HTTPConnection_prototype == 'undefined'){
		HTTPConnection_prototype = true;

		HTTPConnection.prototype.send				= HTTPConnection_send;
		HTTPConnection.prototype.setMethod			= HTTPConnection_setMethod;
		HTTPConnection.prototype.setAction			= HTTPConnection_setAction;
		HTTPConnection.prototype.close				= HTTPConnection_close;
		HTTPConnection.prototype.createHiddenFrame	= HTTPConnection_createHiddenFrame;
		HTTPConnection.prototype.createHiddenForm	= HTTPConnection_createHiddenForm;

		HTTPConnection.prototype.setEncoding		= HTTPConnection_setEncoding;
		HTTPConnection.prototype.setTarget			= HTTPConnection_setTarget;
		HTTPConnection.prototype.setName			= HTTPConnection_setName;
		HTTPConnection.prototype.setLength			= HTTPConnection_setLength;
		HTTPConnection.prototype.setOnReceive		= HTTPConnection_setOnReceive;
	}
}

function HTTPConnection_send(p_arParam){
	/*
	do{
		this.m_eQueryFrame = null;
		var nRandom = Math.round(Math.random() * 1000);
		var sReqFrame = "reqframe" + nRandom;
		this.m_eQueryFrame = document.getElementById(sReqFrame);
	}while(this.m_eQueryFrame != null)
	*/

	if (this.m_sMethod == "GET"){
		eQueryFrame = HTTPConnection_createHiddenFrame ("reqframe");
		eQueryFrame.src = this.m_sAction;
	}else if (this.m_sMethod == "POST"){
		eQueryForm = this.createHiddenForm(p_arParam);
		eQueryForm.submit();
	}
}

function HTTPConnection_setMethod(p_sMethod){
	this.m_sMethod = p_sMethod;
}

function HTTPConnection_setAction(p_sAction){
	this.m_sAction = p_sAction;
}

function HTTPConnection_close(){
	document.removeChild(this.m_eQueryFrame);
}

function HTTPConnection_setEncoding(p_sEncoding){
	this.m_sEncoding = p_sEncoding;
}
function HTTPConnection_setTarget(p_sTarget){
	this.m_sTarget = p_sTarget;
}
function HTTPConnection_setName(p_sName){
	this.m_sName = p_sName;
}
function HTTPConnection_setLength(p_Length){
	//this.m_Length = p_Length;
}
function HTTPConnection_setOnReceive(p_sOnReceive){
	this.m_sOnReceive = p_sOnReceive;
}


function HTTPConnection_createHiddenFrame (sFrameId) {
	// Crée un DIV invisible pour contenir l'IFRAME qui permet d'envoyer les requètes au serveur
	eDivHidden = document.createElement ("div");
	eDivHidden.style.visibility = "hidden";
	eDivHidden.style.position = "absolute";
	eDivHidden.style.left = "-32000";
	eDivHidden.style.top  = "-32000";
	eDivHidden.width = 1;
	eDivHidden.height = 1;

	// Crée l'IFRAME
	eFrameHidden = document.createElement ("iframe");
	eFrameHidden.id = sFrameId;
	eFrameHidden.name = sFrameId;
	eFrameHidden.width = 2;
	eFrameHidden.height = 2;
	eFrameHidden.scrolling = "no";
	eDivHidden.appendChild (eFrameHidden);

	document.body.appendChild (eDivHidden);

	return eFrameHidden;
}


function HTTPConnection_createHiddenForm(p_arParam){
	eDivHidden = document.createElement ("div");
	eDivHidden.style.visibility = "hidden";
	eDivHidden.style.position = "absolute";
	eDivHidden.style.left = "-32000";
	eDivHidden.style.top  = "-32000";
	eDivHidden.width = 1;
	eDivHidden.height = 1;

	// Ajouter le div au document.
	//document.body.appendChild (eDivHidden);

	// Création de la form.
	var eForm = document.createElement ("form");
	document.body.appendChild (eForm);

	// Ajouter la form au div créé précédement.
	//eDivHidden.appendChild(eForm);

	// Initialisation des propriétés du formulaire.
	eForm.method = this.m_sMethod;
	eForm.action = this.m_sAction;
	eForm.enctype = "multipart/form-data";

	if (this.m_sEncoding) eForm.encoding= this.m_sEncoding;
	if (this.m_sTarget) eForm.target	= this.m_sTarget;
	if (this.m_sName) eForm.name		= this.m_sName;

	// Création des élément du formulaire.
	var sType = "";
	var sValue = "";
	var sName = "";
	for (var i = 0; i < p_arParam.length; i++){
		sName = p_arParam[i]["name"];
		sType = p_arParam[i]["type"];
		sValue = p_arParam[i]["value"];
		switch(sType) {
			case "text":
			case "radio":
			case "checkbox":
			case "textarea":
				var eInput = document.createElement("INPUT");
				eInput.type = "hidden";
				eInput.value = sValue;
				eInput.name = sName
				eForm.appendChild(eInput);
				break;
			case "file":
				eFile = document.createElement("input");
				eFile.name = sName;
				eFile.value = sValue;
				eForm.appendChild(eFile);
				eFile.type="file";
				break;
		}
	}


	eInput = document.createElement("INPUT");
	eInput.type = "hidden";
	eInput.value = this.m_sOnReceive;
	eInput.name = "OnReceive";
	eForm.appendChild(eInput);

	// Soumettre le formulaire.
	return eForm;
}

function QuoteString (sEntry) {
	var sQuotedEntry = "";

	if ((sEntry == undefined) || (sEntry == null))
		return "";

	for (var i = 0; i < sEntry.length ; i++) {
		var sChar = sEntry.charCodeAt(i);

		if (sChar == 32)
			sQuotedEntry = sQuotedEntry + "+";
		else {
			if ((sChar <= 47) || ((sChar >= 58) && (sChar <= 64)) || ((sChar >= 93) && (sChar <= 96)) || (sChar >= 123))
				sQuotedEntry = sQuotedEntry + "%" + CharToHex (sChar);
			else
				sQuotedEntry = sQuotedEntry + sEntry.charAt(i);
		}
	}
	return sQuotedEntry;
}

function CharToHex (nValue) {
	var sConvTable = "0123456789abcdef";
	var sHex;
	return sConvTable.charAt((nValue / 16) % 16) + sConvTable.charAt(nValue % 16);
}

