
var Class = {
	create: function() {
		return function() { this.initialize.apply(this, arguments); }
	}
};

//

function $(id) {  
	if (id == undefined) return false;
	return (document.getElementById(id.toString())) ? document.getElementById(id.toString()) : false; 
}


var Ajax = Class.create();

Ajax.prototype = {

	// Constructeur

	initialize: function(sUrl, scope) {

		if (scope == undefined) { 
			this.scope = window; 
		} else {
			this.scope = scope; 
		} 

		this.sUrl = sUrl;
		this.oXmlHttp = false;
		this.sVariables = "";
		this.aArguments = [];

		/*@cc_on
		@if(@_jscript_version >= 5)
			try {
				this.oXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					this.oXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (E) {
					this.oXmlHttp = false;
				}
			}
		@else
			this.oXmlHttp = false;
		@end @*/
		if(!this.oXmlHttp && typeof XMLHttpRequest != 'undefined') {
			try {
				this.oXmlHttp = new XMLHttpRequest();
			} catch (e) {
				this.oXmlHttp = false;
			}
		}
		if (!this.oXmlHttp) {
			// XMLHttpRequest n'est pas supporte pas le client
		}
	},
	// Envoi
	envoie: function(fonctionRetour) {

		this.fonctionRetour = fonctionRetour;
		this.oXmlHttp.abort();
		this.oXmlHttp.open('POST', this.sUrl, true);
		this.oXmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

		for (var i = 0; i < this.aArguments.length; i++) {
			this.sVariables += "&"+this.aArguments[i].nom+"="+this.aArguments[i].valeur;
		}

		this.oXmlHttp.send(this.sVariables);
		var _this = this;

		this.oXmlHttp.onreadystatechange = function () {
			try {
				if (_this.oXmlHttp.status == 200 && _this.oXmlHttp.readyState == 4) {
					_this.fonctionRetour.apply(_this.scope, [_this.oXmlHttp.responseText]);
				}
			} catch (e) {
				// debug seulement
			}
		}
	},

	// Ajoute
	ajoute: function(nom, valeur) {
		var bExiste = false;
		for (var i = 0; i < this.aArguments.length; i++) {
			if (this.aArguments[i].nom == nom) {
				bExiste = true;
				this.aArguments[i].valeur = valeur;
				break;
			}
		}
		if (!bExiste) {
			this.aArguments.push( {nom: nom, valeur: valeur} );
		}
	}

};

// setInnerHTML Sécurisé
function setInnerHTML(divContent, HTML) {
	
	divContent.innerHTML = HTML;
	try {
		  var All=divContent.getElementsByTagName("*");
		  for (var i=0; i<All.length; i++) {
			All[i].id=All[i].getAttribute("id")
			All[i].name=All[i].getAttribute("name")
			//All[i].className=All[i].getAttribute("class")
		  }
	} catch (ex) {}
		try {
		  var AllScripts=HTML.extractTags("script");
		  AllScripts.forEach(function (v) {
			eval(v);
		  })
		} catch (ex) {}
		try {
		  var AllStyles=HTML.extractTags("style");
		  AllStyles.forEach(function (v) {
			var s=document.createStyleSheet()
			s.cssText=v;
			s.enabled=true;
		  }, true)
		} catch (ex) {}
	}

	String.prototype.extractTags=function(tag) {
		var matchAll = new RegExp('(?:<'+tag+'.*?>)((\n|\r|.)*?)(?:<\/'+tag+'>)', 'img');
		var matchOne = new RegExp('(?:<'+tag+'.*?>)((\n|\r|.)*?)(?:<\/'+tag+'>)', 'im');
		return (this.match(matchAll) || []).map(function(scriptTag) {
		  return (scriptTag.match(matchOne) || ['', ''])[1];
		});
	  }

	//Detect IE5.5+
	version=0;
	if(navigator.appVersion.indexOf("MSIE")!=-1)
	{
	temp=navigator.appVersion.split("MSIE");
	version=parseFloat(temp[1]);
	}
	// NON IE browser will return 0
	if(version>=5.5)
	{
	Object.prototype.forEach=function(delegate, ownpropertiesonly) {
		if (typeof(delegate)=="function") {
			if (this instanceof Array && typeof(ownpropertiesonly)=="undefined") {
				ownpropertiesonly=true;
			}
			for (key in this) {
				var ok = (!ownpropertiesonly);
				if (!ok) {
					try {
						ok=this.hasOwnProperty(key)
					} catch (ex) {}
				}
				if (ok) {
					try { delegate(this[key], key, this) } catch(e) {
						// ...
					}
				}
			}
		}
		return false;
	}

	Object.prototype.map=function(iterator) {
		var results = [];
		this.forEach(function(value, index) {
		  results.push(iterator(value, index));
		});
		return results;
	}
}