/** ---------------------------------------------------------------------------------
 * XMLHttp Lib
 */
var boutikhttp = false

function initBoutikHTTP() {
	boutikhttp = false	
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
		try {
	 		boutikhttp	= new ActiveXObject("Msxml2.XMLHTTP");
	 	} catch (e) {
	 		try {
	 			boutikhttp = new ActiveXObject("Microsoft.XMLHTTP");
	 		} catch (E) {
	 			boutikhttp 		= false;
	 		}
	 	}
	@end @*/

	if (!boutikhttp && typeof XMLHttpRequest != 'undefined') {
	  boutikhttp = new XMLHttpRequest();
	}

	boutikhttp.onreadystatechange = function () { oPile.set_cState(boutikhttp.readyState) }
}



function URLEncode(plaintext)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for
	return encoded;
}

function URLDecode(encoded)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef";
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
}

// trigge de changement d'état pour le connecteur''
var e = ''


/** ---------------------------------------------------------------------------------
 * Objet de gestion de pile de connection
 */
function oPileObject(){

	// tableau des fonctions contenues dans la pile
	this.aPile = new Array();

	// élément courant
	this.cItem = null
	
	// élément dépilé
	this.lItem = null

	// état courant du connecteur
	this.cState = 0;

	// ajoute un élément dans la pile
	this.addItem = function(el) {
		this.aPile.push(el)
		//e+='oPile.addItem = '+this.aPile.length+"\n"
		this.execItem()
	}

	// dépile le premier élément
	this.getItem = function() {
		//e+='oPile.getItem'+"\n"
		//this.cItem = this.aPile.shift()
		this.cItem = this.aPile[0]
	}

	// dépile le premier élément
	this.delItem = function() {
		this.lItem = this.cItem
		this.aPile.shift()
	}

	// redéfini l'état du connecteur
	this.set_cState = function(v) {
		//e+='oPile.set_cState('+v+')'+"\n"
		this.cState = v
		this.execItem()
	}

	// exécute l'élément courant
	this.execItem = function() {

		if(this.cState == 0){
			this.getItem()


			if(this.cItem!=null&&typeof(this.cItem)=='object'){

				if (this.cItem.rMethod == 'GET') {
					this.cItem.rUrl=(''!=this.cItem.rData)?this.cItem.rUrl+'?'+this.cItem.rData:this.cItem.rUrl
					boutikhttp.open("GET", this.cItem.rUrl, true)
					boutikhttp.send(this.cItem.rData)

				} else if (this.cItem.rMethod == 'POST') {
					boutikhttp.open("POST", this.cItem.rUrl, true)
					boutikhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
					boutikhttp.send(this.cItem.rData)
				}
			}
		} else if(this.cState == 4){
			this.cItem.responseText		= boutikhttp.responseText
			this.cItem.responseXML		= boutikhttp.responseXML
			this.cItem.responseHeaders	= boutikhttp.getAllResponseHeaders()
			initBoutikHTTP()
			this.delItem()
			this.set_cState(0)
			eval(this.lItem.fnToExec)
			
		}
	}
}



function oPileElement(rMethod, rUrl, rData, fnToExec) {
	this.responseXML	= ''
	this.responseText	= ''
	this.responseHeaders= ''
	this.rMethod 		= rMethod
	this.rUrl 			= rUrl
	this.rData 			= rData
	this.fnToExec 		= fnToExec
}

oPile = new oPileObject()
initBoutikHTTP()
// instance du gestionnaire de pile