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(ns4) return document.layers[id];
  	if(ie4 || ie5) return document.all[id];
  	if(ns6)	return document.getElementById(id);
}

//Cette fonction permet d'afficher le calque [ID]
function show(id){
	if(ns4)
	{
		document.layers[id].visibility = "show";
	}
	else
	{
		getIt(id).style.visibility = "visible";
      	getIt(id).style.display = "block";
	}
}

//Cette fonction permet de cacher le calque [ID]
function hide(id){
	if(ns4){document.layers[id].visibility = "hide";}
	else
	{
		getIt(id).style.visibility = "hidden";
    	getIt(id).style.display = "none";
	}
}

function bascule_visibilite(id){
	if(ns4)
	{
		if(document.layers[id].visibility == "hide")
			{show(id);}
		else
			{hide(id);}
	}
	else
	{
	 	if (getIt(id).style.visibility == "hidden")
			{show(id);}
		else
			{hide(id);}
	}
}

//Accèpte en entrée aussi bien l'objet TR lui même, que son id.
function setBackgroundColor(theRow, thePointerColor)
{
	if (isString(theRow)) {theRow=getIt(theRow);}
    if (typeof(theRow.style) == 'undefined' || typeof(theRow.cells) == 'undefined')
    {
        return false;
    }
    var row_cells_cnt           = theRow.cells.length;
    for (var c = 0; c < row_cells_cnt; c++)
    {
        theRow.cells[c].bgColor = thePointerColor;
    }
    return true;
}

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)
		{
			oListeDestination.add(oListeSource.options[i]);
			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;}

/*
exemple de contenu pour le champ [features]: 
	'toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=200,height=200'
*/
function popup(theURL,winName,features){ //v2.0
	window.open(theURL,winName,features);
}
