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
//Cette fonction permet de récupérer un Calque grace à son ID
function getIt(id){
	if (!isObject(id))
	{
		if(document.getElementById(id)==null)
		{
			if(document.getElementsByName(id).length>0)
			{
				return document.getElementsByName(id)[0];
			}
		}
		else
		{
			return document.getElementById(id);
		}
	}
	else
	{
		return 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;
}


/*
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){
	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)}

	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 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;}

/*ex: addListener(window, 'load', myFunction);*/
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);
	}
}

//Permet d'appeler un click sur un object, avec prise en charge de FF.
function callClick(id){
	if(getIt(id).dispatchEvent)
	{//Sous FF, getIt("ctl00_cphContenu_btnCantonsSelectionne").click ne marche pas
		var clickEvent = document.createEvent("MouseEvents");
		clickEvent.initEvent("click", true, true);
		document.getElementById(id).dispatchEvent(clickEvent);
	}
	else
	{
		getIt(id).click();
	}

}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}