/* Simple AJAX Code-Kit (SACK) v1.6.1 */
/* ©2005 Gregory Wild-Smith */
/* www.twilightuniverse.com */
/* Software licenced under a modified X11 licence,
   see documentation or authors website for more details */

//Encode une chaîne
function htmlentities(str) {
	str = str.replace(/"/g,'&quot;'); // 34 22
	str = str.replace(/&/g,'&amp;'); // 38 26
	str = str.replace(/\'/g,'&#39;'); // 39 27
	str = str.replace(/</g,'&lt;'); // 60 3C
	str = str.replace(/>/g,'&gt;'); // 62 3E
	str = str.replace(/\^/g,'&circ;'); // 94 5E
	str = str.replace(/‘/g,'&lsquo;'); // 145 91
	str = str.replace(/’/g,'&rsquo;'); // 146 92
	str = str.replace(/“/g,'&ldquo;'); // 147 93
	str = str.replace(/”/g,'&rdquo;'); // 148 94
	str = str.replace(/•/g,'&bull;'); // 149 95
	str = str.replace(/–/g,'&ndash;'); // 150 96
	str = str.replace(/—/g,'&mdash;'); // 151 97
	str = str.replace(/˜/g,'&tilde;'); // 152 98
	str = str.replace(/™/g,'&trade;'); // 153 99
	str = str.replace(/š/g,'&scaron;'); // 154 9A
	str = str.replace(/›/g,'&rsaquo;'); // 155 9B
	str = str.replace(/œ/g,'&oelig;'); // 156 9C
	str = str.replace(//g,'&#357;'); // 157 9D
	str = str.replace(/ž/g,'&#382;'); // 158 9E
	str = str.replace(/Ÿ/g,'&Yuml;'); // 159 9F
	str = str.replace(/ /g,'&nbsp;'); // 160 A0
	str = str.replace(/¡/g,'&iexcl;'); // 161 A1
	str = str.replace(/¢/g,'&cent;'); // 162 A2
	str = str.replace(/£/g,'&pound;'); // 163 A3
	str = str.replace(/ /g,'&curren;'); // 164 A4
	str = str.replace(/¥/g,'&yen;'); // 165 A5
	str = str.replace(/¦/g,'&brvbar;'); // 166 A6
	str = str.replace(/§/g,'&sect;'); // 167 A7
	str = str.replace(/¨/g,'&uml;'); // 168 A8
	str = str.replace(/©/g,'&copy;'); // 169 A9
	str = str.replace(/ª/g,'&ordf;'); // 170 AA
	str = str.replace(/«/g,'&laquo;'); // 171 AB
	str = str.replace(/¬/g,'&not;'); // 172 AC
	str = str.replace(/­/g,'&shy;'); // 173 AD
	str = str.replace(/®/g,'&reg;'); // 174 AE
	str = str.replace(/¯/g,'&macr;'); // 175 AF
	str = str.replace(/°/g,'&deg;'); // 176 B0
	str = str.replace(/±/g,'&plusmn;'); // 177 B1
	str = str.replace(/²/g,'&sup2;'); // 178 B2
	str = str.replace(/³/g,'&sup3;'); // 179 B3
	str = str.replace(/´/g,'&acute;'); // 180 B4
	str = str.replace(/µ/g,'&micro;'); // 181 B5
	str = str.replace(/¶/g,'&para'); // 182 B6
	str = str.replace(/·/g,'&middot;'); // 183 B7
	str = str.replace(/¸/g,'&cedil;'); // 184 B8
	str = str.replace(/¹/g,'&sup1;'); // 185 B9
	str = str.replace(/º/g,'&ordm;'); // 186 BA
	str = str.replace(/»/g,'&raquo;'); // 187 BB
	str = str.replace(/¼/g,'&frac14;'); // 188 BC
	str = str.replace(/½/g,'&frac12;'); // 189 BD
	str = str.replace(/¾/g,'&frac34;'); // 190 BE
	str = str.replace(/¿/g,'&iquest;'); // 191 BF
	str = str.replace(/À/g,'&Agrave;'); // 192 C0
	str = str.replace(/Á/g,'&Aacute;'); // 193 C1
	str = str.replace(/Â/g,'&Acirc;'); // 194 C2
	str = str.replace(/Ã/g,'&Atilde;'); // 195 C3
	str = str.replace(/Ä/g,'&Auml;'); // 196 C4
	str = str.replace(/Å/g,'&Aring;'); // 197 C5
	str = str.replace(/Æ/g,'&AElig;'); // 198 C6
	str = str.replace(/Ç/g,'&Ccedil;'); // 199 C7
	str = str.replace(/È/g,'&Egrave;'); // 200 C8
	str = str.replace(/É/g,'&Eacute;'); // 201 C9
	str = str.replace(/Ê/g,'&Ecirc;'); // 202 CA
	str = str.replace(/Ë/g,'&Euml;'); // 203 CB
	str = str.replace(/Ì/g,'&Igrave;'); // 204 CC
	str = str.replace(/Í/g,'&Iacute;'); // 205 CD
	str = str.replace(/Î/g,'&Icirc;'); // 206 CE
	str = str.replace(/Ï/g,'&Iuml;'); // 207 CF
	str = str.replace(/Ð/g,'&ETH;'); // 208 D0
	str = str.replace(/Ñ/g,'&Ntilde;'); // 209 D1
	str = str.replace(/Ò/g,'&Ograve;'); // 210 D2
	str = str.replace(/Ó/g,'&Oacute;'); // 211 D3
	str = str.replace(/Ô/g,'&Ocirc;'); // 212 D4
	str = str.replace(/Õ/g,'&Otilde;'); // 213 D5
	str = str.replace(/Ö/g,'&Ouml;'); // 214 D6
	str = str.replace(/×/g,'&times;'); // 215 D7
	str = str.replace(/Ø/g,'&Oslash;'); // 216 D8
	str = str.replace(/Ù/g,'&Ugrave;'); // 217 D9
	str = str.replace(/Ú/g,'&Uacute;'); // 218 DA
	str = str.replace(/Û/g,'&Ucirc;'); // 219 DB
	str = str.replace(/Ü/g,'&Uuml;'); // 220 DC
	str = str.replace(/Ý/g,'&Yacute;'); // 221 DD
	str = str.replace(/Þ/g,'&THORN;'); // 222 DE
	str = str.replace(/ß/g,'&szlig;'); // 223 DF
	str = str.replace(/à/g,'&agrave;'); // 224 E0
	str = str.replace(/á/g,'&aacute;'); // 225 E1
	str = str.replace(/â/g,'&acirc;'); // 226 E2
	str = str.replace(/ã/g,'&atilde;'); // 227 E3
	str = str.replace(/ä/g,'&auml;'); // 228 E4
	str = str.replace(/å/g,'&aring;'); // 229 E5
	str = str.replace(/æ/g,'&aelig;'); // 230 E6
	str = str.replace(/ç/g,'&ccedil;'); // 231 E7
	str = str.replace(/è/g,'&egrave;'); // 232 E8
	str = str.replace(/é/g,'&eacute;'); // 233 E9
	str = str.replace(/ê/g,'&ecirc;'); // 234 EA
	str = str.replace(/ë/g,'&euml;'); // 235 EB
	str = str.replace(/ì/g,'&igrave;'); // 236 EC
	str = str.replace(/í/g,'&iacute;'); // 237 ED
	str = str.replace(/î/g,'&icirc;'); // 238 EE
	str = str.replace(/ï/g,'&iuml;'); // 239 EF
	str = str.replace(/ð/g,'&eth;'); // 240 F0
	str = str.replace(/ñ/g,'&ntilde;'); // 241 F1
	str = str.replace(/ò/g,'&ograve;'); // 242 F2
	str = str.replace(/ó/g,'&oacute;'); // 243 F3
	str = str.replace(/ô/g,'&ocirc;'); // 244 F4
	str = str.replace(/õ/g,'&otilde;'); // 245 F5
	str = str.replace(/ö/g,'&ouml;'); // 246 F6
	str = str.replace(/÷/g,'&divide;'); // 247 F7
	str = str.replace(/ø/g,'&oslash;'); // 248 F8
	str = str.replace(/ù/g,'&ugrave;'); // 249 F9
	str = str.replace(/ú/g,'&uacute;'); // 250 FA
	str = str.replace(/û/g,'&ucirc;'); // 251 FB
	str = str.replace(/ü/g,'&uuml;'); // 252 FC
	str = str.replace(/ý/g,'&yacute;'); // 253 FD
	str = str.replace(/þ/g,'&thorn;'); // 254 FE
	str = str.replace(/ÿ/g,'&yuml;'); // 255 FF
	return str;
}
//Décode une chaîne
function html_entity_decode(str) {
	str = str.replace(/&quot;/g,'"'); // 34 22
	str = str.replace(/&amp;/g,'&'); // 38 26	
	str = str.replace(/&#39;/g,"'"); // 39 27
	str = str.replace(/&lt;/g,'<'); // 60 3C
	str = str.replace(/&gt;/g,'>'); // 62 3E
	str = str.replace(/&circ;/g,'^'); // 94 5E
	str = str.replace(/&lsquo;/g,'‘'); // 145 91
	str = str.replace(/&rsquo;/g,'’'); // 146 92
	str = str.replace(/&ldquo;/g,'“'); // 147 93
	str = str.replace(/&rdquo;/g,'”'); // 148 94
	str = str.replace(/&bull;/g,'•'); // 149 95
	str = str.replace(/&ndash;/g,'–'); // 150 96
	str = str.replace(/&mdash;/g,'—'); // 151 97
	str = str.replace(/&tilde;/g,'˜'); // 152 98
	str = str.replace(/&trade;/g,'™'); // 153 99
	str = str.replace(/&scaron;/g,'š'); // 154 9A
	str = str.replace(/&rsaquo;/g,'›'); // 155 9B
	str = str.replace(/&oelig;/g,'œ'); // 156 9C
	str = str.replace(/&#357;/g,''); // 157 9D
	str = str.replace(/&#382;/g,'ž'); // 158 9E
	str = str.replace(/&Yuml;/g,'Ÿ'); // 159 9F
	str = str.replace(/&nbsp;/g,' '); // 160 A0
	str = str.replace(/&iexcl;/g,'¡'); // 161 A1
	str = str.replace(/&cent;/g,'¢'); // 162 A2
	str = str.replace(/&pound;/g,'£'); // 163 A3
	str = str.replace(/&curren;/g,' '); // 164 A4
	str = str.replace(/&yen;/g,'¥'); // 165 A5
	str = str.replace(/&brvbar;/g,'¦'); // 166 A6
	str = str.replace(/&sect;/g,'§'); // 167 A7
	str = str.replace(/&uml;/g,'¨'); // 168 A8
	str = str.replace(/&copy;/g,'©'); // 169 A9
	str = str.replace(/&ordf;/g,'ª'); // 170 AA
	str = str.replace(/&laquo;/g,'«'); // 171 AB
	str = str.replace(/&not;/g,'¬'); // 172 AC
	str = str.replace(/&shy;/g,'­'); // 173 AD
	str = str.replace(/&reg;/g,'®'); // 174 AE
	str = str.replace(/&macr;/g,'¯'); // 175 AF
	str = str.replace(/&deg;/g,'°'); // 176 B0
	str = str.replace(/&plusmn;/g,'±'); // 177 B1
	str = str.replace(/&sup2;/g,'²'); // 178 B2
	str = str.replace(/&sup3;/g,'³'); // 179 B3
	str = str.replace(/&acute;/g,'´'); // 180 B4
	str = str.replace(/&micro;/g,'µ'); // 181 B5
	str = str.replace(/&para/g,'¶'); // 182 B6
	str = str.replace(/&middot;/g,'·'); // 183 B7
	str = str.replace(/&cedil;/g,'¸'); // 184 B8
	str = str.replace(/&sup1;/g,'¹'); // 185 B9
	str = str.replace(/&ordm;/g,'º'); // 186 BA
	str = str.replace(/&raquo;/g,'»'); // 187 BB
	str = str.replace(/&frac14;/g,'¼'); // 188 BC
	str = str.replace(/&frac12;/g,'½'); // 189 BD
	str = str.replace(/&frac34;/g,'¾'); // 190 BE
	str = str.replace(/&iquest;/g,'¿'); // 191 BF
	str = str.replace(/&Agrave;/g,'À'); // 192 C0
	str = str.replace(/&Aacute;/g,'Á'); // 193 C1
	str = str.replace(/&Acirc;/g,'Â'); // 194 C2
	str = str.replace(/&Atilde;/g,'Ã'); // 195 C3
	str = str.replace(/&Auml;/g,'Ä'); // 196 C4
	str = str.replace(/&Aring;/g,'Å'); // 197 C5
	str = str.replace(/&AElig;/g,'Æ'); // 198 C6
	str = str.replace(/&Ccedil;/g,'Ç'); // 199 C7
	str = str.replace(/&Egrave;/g,'È'); // 200 C8
	str = str.replace(/&Eacute;/g,'É'); // 201 C9
	str = str.replace(/&Ecirc;/g,'Ê'); // 202 CA
	str = str.replace(/&Euml;/g,'Ë'); // 203 CB
	str = str.replace(/&Igrave;/g,'Ì'); // 204 CC
	str = str.replace(/&Iacute;/g,'Í'); // 205 CD
	str = str.replace(/&Icirc;/g,'Î'); // 206 CE
	str = str.replace(/&Iuml;/g,'Ï'); // 207 CF
	str = str.replace(/&ETH;/g,'Ð'); // 208 D0
	str = str.replace(/&Ntilde;/g,'Ñ'); // 209 D1
	str = str.replace(/&Ograve;/g,'Ò'); // 210 D2
	str = str.replace(/&Oacute;/g,'Ó'); // 211 D3
	str = str.replace(/&Ocirc;/g,'Ô'); // 212 D4
	str = str.replace(/&Otilde;/g,'Õ'); // 213 D5
	str = str.replace(/&Ouml;/g,'Ö'); // 214 D6
	str = str.replace(/&times;/g,'×'); // 215 D7
	str = str.replace(/&Oslash;/g,'Ø'); // 216 D8
	str = str.replace(/&Ugrave;/g,'Ù'); // 217 D9
	str = str.replace(/&Uacute;/g,'Ú'); // 218 DA
	str = str.replace(/&Ucirc;/g,'Û'); // 219 DB
	str = str.replace(/&Uuml;/g,'Ü'); // 220 DC
	str = str.replace(/&Yacute;/g,'Ý'); // 221 DD
	str = str.replace(/&THORN;/g,'Þ'); // 222 DE
	str = str.replace(/&szlig;/g,'ß'); // 223 DF
	str = str.replace(/&agrave;/g,'à'); // 224 E0
	str = str.replace(/&aacute;/g,'á'); // 225 E1
	str = str.replace(/&acirc;/g,'â'); // 226 E2
	str = str.replace(/&atilde;/g,'ã'); // 227 E3
	str = str.replace(/&auml;/g,'ä'); // 228 E4
	str = str.replace(/&aring;/g,'å'); // 229 E5
	str = str.replace(/&aelig;/g,'æ'); // 230 E6
	str = str.replace(/&ccedil;/g,'ç'); // 231 E7
	str = str.replace(/&egrave;/g,'è'); // 232 E8
	str = str.replace(/&eacute;/g,'é'); // 233 E9
	str = str.replace(/&ecirc;/g,'ê'); // 234 EA
	str = str.replace(/&euml;/g,'ë'); // 235 EB
	str = str.replace(/&igrave;/g,'ì'); // 236 EC
	str = str.replace(/&iacute;/g,'í'); // 237 ED
	str = str.replace(/&icirc;/g,'î'); // 238 EE
	str = str.replace(/&iuml;/g,'ï'); // 239 EF
	str = str.replace(/&eth;/g,'ð'); // 240 F0
	str = str.replace(/&ntilde;/g,'ñ'); // 241 F1
	str = str.replace(/&ograve;/g,'ò'); // 242 F2
	str = str.replace(/&oacute;/g,'ó'); // 243 F3
	str = str.replace(/&ocirc;/g,'ô'); // 244 F4
	str = str.replace(/&otilde;/g,'õ'); // 245 F5
	str = str.replace(/&ouml;/g,'ö'); // 246 F6
	str = str.replace(/&divide;/g,'÷'); // 247 F7
	str = str.replace(/&oslash;/g,'ø'); // 248 F8
	str = str.replace(/&ugrave;/g,'ù'); // 249 F9
	str = str.replace(/&uacute;/g,'ú'); // 250 FA
	str = str.replace(/&ucirc;/g,'û'); // 251 FB
	str = str.replace(/&uuml;/g,'ü'); // 252 FC
	str = str.replace(/&yacute;/g,'ý'); // 253 FD
	str = str.replace(/&thorn;/g,'þ'); // 254 FE
	str = str.replace(/&yuml;/g,'ÿ'); // 255 FF
	return str;
}

function removespace(str){
str = str.replace(/(\n\r|\n|\r)/gm,"<1br />");
str = str.replace(/\t/g,"");


re1 = /\s+/g;
str = str.replace(re1," ");

re2 = /\<1br \/>/gi;
str = str.replace(re2, "\n");
return str ;
}

function sack(file) {
	this.xmlhttp = null;

	this.resetData = function() {
		this.method = "POST";
  		this.queryStringSeparator = "?";
		this.argumentSeparator = "&";
		this.URLString = "";
		this.encodeURIString = true;
  		this.execute = false;
  		this.element = null;
		this.elementObj = null;
		this.requestFile = file;
		this.vars = new Object();
		this.responseStatus = new Array(2);
  	};

	this.resetFunctions = function() {
  		this.onLoading = function() { };
  		this.onLoaded = function() { };
  		this.onInteractive = function() { };
  		this.onCompletion = function() { };
  		this.onError = function() { };
		this.onFail = function() { };
	};

	this.reset = function() {
		this.resetFunctions();
		this.resetData();
	};

	this.createAJAX = function() {
		try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				this.xmlhttp = null;
			}
		}

		if (! this.xmlhttp) {
			if (typeof XMLHttpRequest != "undefined") {
				this.xmlhttp = new XMLHttpRequest();
			} else {
				this.failed = true;
			}
		}
	};

	this.setVar = function(name, value){
		this.vars[name] = Array(value, false);
	};

	this.encVar = function(name, value, returnvars) {
		if (true == returnvars) {
			return Array(encodeURIComponent(name), encodeURIComponent(value));
		} else {
			this.vars[encodeURIComponent(name)] = Array(encodeURIComponent(value), true);
		}
	}

	this.processURLString = function(string, encode) {
		encoded = encodeURIComponent(this.argumentSeparator);
		regexp = new RegExp(this.argumentSeparator + "|" + encoded);
		varArray = string.split(regexp);
		for (i = 0; i < varArray.length; i++){
			urlVars = varArray[i].split("=");
			if (true == encode){
				this.encVar(urlVars[0], urlVars[1]);
			} else {
				this.setVar(urlVars[0], urlVars[1]);
			}
		}
	}

	this.createURLString = function(urlstring) {
		if (this.encodeURIString && this.URLString.length) {
			this.processURLString(this.URLString, true);
		}

		if (urlstring) {
			if (this.URLString.length) {
				this.URLString += this.argumentSeparator + urlstring;
			} else {
				this.URLString = urlstring;
			}
		}

		// prevents caching of URLString
		this.setVar("rndval", new Date().getTime());

		urlstringtemp = new Array();
		for (key in this.vars) {
			if (false == this.vars[key][1] && true == this.encodeURIString) {
				encoded = this.encVar(key, this.vars[key][0], true);
				delete this.vars[key];
				this.vars[encoded[0]] = Array(encoded[1], true);
				key = encoded[0];
			}

			urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key][0];
		}
		if (urlstring){
			this.URLString += this.argumentSeparator + urlstringtemp.join(this.argumentSeparator);
		} else {
			this.URLString += urlstringtemp.join(this.argumentSeparator);
		}
	}

	this.runResponse = function() {
		eval(this.response);
	}

	this.runAJAX = function(urlstring) {
		if (this.failed) {
			this.onFail();
		} else {
			this.createURLString(urlstring);
			if (this.element) {
				this.elementObj = document.getElementById(this.element);
			}
			if (this.xmlhttp) {
				var self = this;
				if (this.method == "GET") {
					totalurlstring = this.requestFile + this.queryStringSeparator + this.URLString;
					this.xmlhttp.open(this.method, totalurlstring, true);
				} else {
					this.xmlhttp.open(this.method, this.requestFile, true);
					try {
						this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; Charset=iso-8859-1")
					} catch (e) { }
				}

				this.xmlhttp.onreadystatechange = function() {
					switch (self.xmlhttp.readyState) {
						case 1:
							self.onLoading();
							break;
						case 2:
							self.onLoaded();
							break;

						case 3:
							self.onInteractive();
							break;
						case 4:
							//self.header("Content-Type:text/plain; charset=iso-8859-1");
							bodyContent = self.xmlhttp.responseText.replace(/\r\n/ig, "");	
							bodyContent = removespace(bodyContent) ;
							//alert(bodyContent) ;
							//bodyContent = bodyContent.match(/<body.*\/body>/);
							bodyContent = bodyContent.substring(bodyContent.indexOf('<body'), bodyContent.indexOf('</body>')) ;
							self.response = bodyContent ;//self.xmlhttp.responseText;
							self.responseXML = self.xmlhttp.responseXML;
							self.responseStatus[0] = self.xmlhttp.status;
							self.responseStatus[1] = self.xmlhttp.statusText;

							if (self.execute) {
								self.runResponse();
							}

							if (self.elementObj) {
								elemNodeName = self.elementObj.nodeName;
								elemNodeName.toLowerCase();
								if (elemNodeName == "input"
								|| elemNodeName == "select"
								|| elemNodeName == "option"
								|| elemNodeName == "textarea") {
									self.elementObj.value = self.response;
								} else {
									self.elementObj.innerHTML = self.response;
								}
							}
							if (self.responseStatus[0] == "200") {
								self.onCompletion();
							} else {
								self.onError();
							}

							self.URLString = "";
							break;
					}
				};

				this.xmlhttp.send(this.URLString);
			}
		}
	};

	this.reset();
	this.createAJAX();
}
