var ns4 = (document.layers);
var ie4 = (document.all && !document.getElementById);
var ie5 = (document.all && document.getElementById);
var ns6 = (!document.all && document.getElementById);
var firefox= (window.XMLHttpRequest);


//Cette fonction permet de récupérer un Calque grace à son ID
function getIt(id){
	if (!isObject(id))
	{
		if(ns4) return document.layers[id];
  		if(ie4 || ie5) return document.all[id];
  		if(ns6)	return document.getElementById(id);
	}
	else
	{
		return id;
	}
}

function addOption(oListe,strTexte,value){
	var oOption = document.createElement("OPTION");
	oOption.text = text;
	oOption.value = value;
	oListe.options.add(oOption);
}


/*
Cette fonction sert à vider le contenu d'une listbox
 */
function viderListeBox(oListe){
var intTaille=0;
	//Au cas où je récupère uniquement le nom de la liste, au lien de l'objet lui-même
	if(!isObject(oListe)) {oListe=getIt(oListe)}

	intTaille=oListe.length
	for (i=0;i<intTaille;i++)
	{
	   oListe.remove(0);
	}
}

/*
Cette fonction sert à positionner les valeurs d'une liste défilante
[strListe]=chaine de couple valeur:libellé;valeur:libellé....
*/
function remplirListeBox(oListe,strListe){
	remplirListeBox(oListe,strListe,true)
}

function remplirListeBox(oListe,strListe,boolViderListe){
	var tabCouples = strListe.split(";");
	var tabOption;
	var oNewOption;

	//Au cas où je récupère uniquement le nom de la liste, au lien de l'objet lui-même
	if(!isObject(oListe)) {oListe=getIt(oListe);}

	if (boolViderListe) {viderListeBox(oListe);}

	for (i=0;i<tabCouples.length;i++)
	{
       tabOption=tabCouples[i].split(":");
	   oNewOption = new Option(tabOption[1],tabOption[0],true,true);
	   oListe.options[i] = oNewOption;
	}
}

function transfererElementsListeBox(oListeSource, oListeDestination,boolViderListeSource){
	var i;
	var strListe="";
	
	//Au cas où je récupère uniquement le nom de la liste, au lien de l'objet lui-même
	if(!isObject(oListeSource)) {oListeSource=getIt(oListeSource);}
	if(!isObject(oListeDestination)) {oListeDestination=getIt(oListeDestination);}
	
	for(i=0;i<oListeSource.length;i++)
	{
		if(oListeSource.options[i].selected)
		{
			var oNewOption = document.createElement("option"); 
			oNewOption.text=oListeSource.options[i].text;
			oNewOption.value=oListeSource.options[i].value;
			oListeDestination.add(oNewOption);
			if (boolViderListeSource)
			{
				oListeSource.remove(i);
				i=i-1;
			}
		}
	}
}

function string_remplace( text, stringToFind, stringRemplacement)
{
	var text = text.toString() ;
	var maReg = new RegExp( stringToFind, "gi") ;
	var resultat = text.replace( maReg, stringRemplacement ) ;
 
	return resultat ;
}

function bascule_image(id,strIMG1,strIMG2){

	if(getIt(id).src.indexOf(strIMG1)>0)
	{
		getIt(id).src=strIMG2;
	}
	else
	{
		getIt(id).src=strIMG1;
	}
}

function isObject(o) {return (o && "object" == typeof o) || isFunction(o);}

function isFunction(o) {return "function" == typeof o;}

function addListener(element, event, listener, bubble){
	if(element.addEventListener)
	{
		if(typeof(bubble) == "undefined") bubble = false;
		element.addEventListener(event, listener, bubble);
	}
	else if(this.attachEvent)
	{
		element.attachEvent("on" + event, listener);
	}
}



//FIREFOX ne gère pas le innerHTML.
function remplaceInnerHTML(elementid,content){
	if (document.getElementById && !document.all)
	{
		rng = document.createRange();
		el = document.getElementById(elementid);
		rng.setStartBefore(el);
		htmlFrag = rng.createContextualFragment(content);
		while (el.hasChildNodes()) {el.removeChild(el.lastChild);}
		el.appendChild(htmlFrag);
		//alert(new XMLSerializer().serializeToString(el));
	}
	else
	{
		document.getElementById(elementid).innerHTML=content;
	}
}