function ajax()
{
	var obj;
	if (window.XMLHttpRequest){ // Firefox et autres
		obj = new XMLHttpRequest();
	}
	else if (window.ActiveXObject){ // Internet Explorer (Merci IE...)
		var ieversions = [	'Msxml2.XMLHTTP',
							'Microsoft.XMLHTTP',
							'Msxml2.XMLHTTP.5.0',
							'Msxml2.XMLHTTP.4.0',
							'Msxml2.XMLHTTP.3.0'];

		for (var i=0; !obj && i<ieversions.length; i++){
			try {
				obj = new ActiveXObject(ieversions[i]);
			} catch (e){
				obj = null;
			}
		}
	}

	return obj;
}

function submitForm(formName)
{
	if (document.forms[formName]){
		if (document.forms[formName].onsubmit){
			if (document.forms[formName].onsubmit())
			{
				document.forms[formName].submit();
				return true;
			}
		} else {
			document.forms[formName].submit();
			return true;
		}
	}
	return false;
}

function get_radio_value(radio)
{
	for (var i=0; i<radio.length; i++){
		if (radio[i].checked)
			return radio[i].value;
	}

	return radio.value; // Si il n'y a qu'un élément
}

function selectRadio(radio, value){
	for (var i=0; i<radio.length; i++){
		if (radio[i].value == value){
			radio[i].checked = true;
			return true;
		}
	}
	return false;
}

function hideBox(id){
	if (document.getElementById(id)){
		document.getElementById(id).style.display = 'none';
	}
}

function showBox(id){
	if (document.getElementById(id)){
		document.getElementById(id).style.display = 'block';
	}
}

function hideElmt(id){
	if (document.getElementById(id)){
		document.getElementById(id).style.display = 'none';
	}
}

function showElmt(id){
	if (document.getElementById(id)){
		document.getElementById(id).style.display = 'inline';
	}
}

// Mise en forme des infos dans une liste ordonnée
function array2ol(array)
{
	var tmp = document.createElement('div');
	var ol = document.createElement('ol');
	for (var i=0; i<array.length; i++){
		var li = document.createElement('li');
		li.innerHTML = array[i];
		ol.appendChild(li);
	}
	tmp.appendChild(ol);

	return tmp.innerHTML;
}

/**
 * Définit une opacité selon le navigateur qui le lit... (Merci IE)
 *
 * @author: Roland Dufour <roland.dufour@multiprog.net>
 */
function setOpak(opak, id)
{
  	var oElemStyle = document.getElementById(id).style;

	oElemStyle.opacity = (opak / 100);
	oElemStyle.MozOpacity = (opak / 100);
	oElemStyle.KhtmlOpacity = (opak / 100);
	oElemStyle.filter = 'alpha(opacity=' + opak + ')';
}

/**
 * Equivalent de print_r de PHP
 */
function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;

	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";

	if(typeof(arr) == 'object') { //Array/Hashes/Objects
	 for(var item in arr) {
	  var value = arr[item];

	  if(typeof(value) == 'object') { //If it is an array,
	   dumped_text += level_padding + "'" + item + "' ...\n";
	   dumped_text += dump(value,level+1);
	  } else {
	   dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
	  }
	 }
	} else { //Stings/Chars/Numbers etc.
	 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

/**
 * Interverti la propriété display d'un bloc
 **/
function swapId(id){
	var elmt = document.getElementById(id);
	var visible = (elmt.style.display == 'none' ? false : true);
	
	if (arguments.length == 2){
		if (arguments[1]){
			visible = true;
		}
		else {
			visible = false;
		}
	}
	
	if (visible){
		elmt.style.display = 'none';
	} else {
		elmt.style.display = 'block';
	}
	return visible;
}


function showHelp(event, id_aide){
	var xhr = new ajax();
	if (xhr == null){ alert('Votre navigateur ne gère pas les objets Ajax.\nMerci de mettre à jour votre navigateur ou d\'autoriser cette fonctionnalité.'); return false; }
	
	windowBox.setEvent(event);
	windowBox.setTitle('Aide en ligne');
	windowBox.setContent('Chargement de l\'aide d\'identifiant : ' + id_aide + "\n" + 'Merci de patienter...');
	windowBox.setHeight(250);
	windowBox.setWidth(350);

	xhr.open ('POST', 'showHelp.php', true);
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			var texte = xhr.responseText;
			var eltTexte = document.createElement('div');
			eltTexte.innerHTML = texte;
			
			eltTexte = editClassName(eltTexte, 'windowBox_byJS', 'windowBox_');
			
			windowBox.setContent(eltTexte.innerHTML);
			windowBox.show();
		}
	}

	xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	xhr.send('id_aide='+escape(id_aide));
	windowBox.show();
	windowBox.setMouseDownEvent();
}

function showTexts(event, ref, id){
	var xhr = new ajax();
	if (xhr == null){ alert('Votre navigateur ne gère pas les objets Ajax.\nMerci de mettre à jour votre navigateur ou d\'autoriser cette fonctionnalité.'); return false; }
	
	windowBox.setEvent(event);
	windowBox.setTitle('Exemple de textes');
	windowBox.setContent('Chargement du texte pour la ref : ' + ref + "\n" + 'Merci de patienter...');
	windowBox.setHeight(300);
	windowBox.setWidth(500);
	
	xhr.open ('POST', 'showTexts.php', true);
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			windowBox.setContent(xhr.responseText);
			windowBox.show();
		}
	}

	xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	xhr.send('ref='+escape(ref)+'&txt_id='+escape(id));
	windowBox.show();
	windowBox.setMouseDownEvent();
}

function showImg(event, imgSrc, title, alignX, alignY){
	
	var xhr = new ajax();
	if (xhr == null){ alert('Votre navigateur ne gère pas les objets Ajax.\nMerci de mettre à jour votre navigateur ou d\'autoriser cette fonctionnalité.'); return false; }
	
	if (imgSrc.indexOf("?") > 0){
		imgSrc = imgSrc.substring(0, imgSrc.indexOf("?"));
	}
	
	windowBox.setEvent(event);
	windowBox.setTitle(title);
	
	if (typeof(arguments[7]) != "undefined"){
		windowBox.setContent('<img src="'+escape(imgSrc)+'" alt="" title="" style="width: '+arguments[7]+'px; height : '+arguments[8]+'px;" />'); // Pas de class="windowBox_img" pour fermeture de la box sur clic sur l'img
	}
	else{
		windowBox.setContent('<img src="'+escape(imgSrc)+'" alt="" title="" />'); // Pas de class="windowBox_img" pour fermeture de la box sur clic sur l'img
	}
	
	windowBox.setWidth(600);
	windowBox.setHeight(600); 
	
	windowBox.setAlignX(alignX);
	windowBox.setAlignY(alignY);
		
	xhr.open ('POST', 'showImg.php', true);
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			var infos = eval(xhr.responseText);
			if (infos[0] == 1){
				windowBox.setWidth(parseInt(infos[1]) + 12);
				windowBox.setHeight(parseInt(infos[2]) + 36);
				windowBox.show();
			}
		}
	}
	
	data = 'imgsrc='+escape(imgSrc);
	
		if (typeof(arguments[7]) != "undefined"){
			data += '&maxwidth='+escape(parseInt(arguments[7]));
			data += '&maxheight='+escape(parseInt(arguments[8]));
		}
		else{
			if (arguments.length >= 6){
				data += '&maxwidth='+escape(parseInt(arguments[5]) - 12);
			}
			if (arguments.length >= 7){
				data += '&maxheight='+escape(parseInt(arguments[6]) - 36);
			}
		}
		
	xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	xhr.send(data);
	windowBox.show();
	windowBox.setMouseDownEvent();
}



function editClassName(elt, maClasse, nonValide){
	for (var i=0; i < elt.childNodes.length; i++){
		if (elt.childNodes[i].nodeName == '#text'){
			continue;
		}
		else {
			var obj = editClassName(elt.childNodes[i], maClasse, nonValide); 
			
			if (obj.className.substr(0, nonValide.length) != nonValide){
				if (obj.className != ''){
					obj.className = maClasse + ' ' + obj.className;
				} else {
					obj.className = maClasse;
				}
			} 
		}
	}
	return elt;
}



var windowBox = {
	instance: null,
	evenement: null,
	content: null,
	title: null,
	contenu: '',
	titre: '',
	height: 200,
	width: 300,
	pos: null,
	alignX: null,
	alignY: null,
	
	setMouseDownEvent: function(){
		if (document.addEventListener){
			document.addEventListener("mousedown", windowBox.mouseDown, false );
		} else if (document.attachEvent){		
			document.attachEvent("onmousedown", function () { windowBox.mouseDown(window.event); } );
		}
	},
	
	mouseDown: function(event){
		if (windowBox.instance != null){
			var el = event.target ? event.target : event.srcElement;
		    while (el.nodeType != 1) el = el.parentNode;
			if (el.className.substr(0, 10) != 'windowBox_'){
				document.body.removeChild(windowBox.instance);
				windowBox.instance = null;
			}
		}
	},
	
	close: function(){
		if (this.instance != null){
			document.body.removeChild(this.instance);
			this.instance = null;
		}
	},
	
	setEvent: function(evenement){
		if (null == evenement){
			evenement = event;
		}	
		this.evenement = evenement;
		this.pos = new Array(evenement.clientX, evenement.clientY);
	},
	
	setContent: function(contenu){
		this.contenu = contenu;
	},
	
	setTitle: function(titre){
		this.titre = titre;
	},
	
	setAlignX: function(alignX){
		this.alignX = alignX;
	},
	
	setAlignY: function(alignY){
		this.alignY = alignY;
	},
	
	setHeight: function(height){
		this.height = height;
	},
	
	setWidth: function(width){
		this.width = width;
	},
	
	show: function(){
		this.close();
	
		heightBox = this.height;
		widthBox = this.width;
	
		this.instance = document.createElement('div');
		
		var btn = document.createElement('div');
		btn.className = 'windowBox_btn';
		with (btn.style){
			position = 'absolute';
			width = '60px';
			height = '20px';
			top = 0;
			right = 0;
			lineHeight = '20px';
			textAlign = 'center';
		}
		
		var abtn = document.createElement('a');
		abtn.className = 'windowBox_abtn';
		with (abtn.style){
			color = '#444';
		}
		abtn.innerHTML = 'Fermer';
		abtn.href = '#';
		abtn.onclick = function(){
			windowBox.close();
			return false;
		};
		btn.appendChild(abtn);
		this.instance.appendChild(btn);
		
		this.title = document.createElement('h4');
		this.title.className = 'windowBox_titre';
		with (this.title.style){
			height = '20px';
			lineHeight = '20px';
			paddingLeft = '10px';
			margin = '0';
			background = '#ffeeee';
			borderBottom = '1px dotted #222';
		}
		this.title.innerHTML = this.titre;
		this.instance.appendChild(this.title);
		
		this.content = document.createElement('div');
		this.content.className = 'windowBox_contenu';
		hauteur = parseInt(heightBox - 36);
		largeur = parseInt(widthBox - 12);
		with (this.content.style){
			height = hauteur+'px';
			overflow = 'auto';
			padding = '5px';
			margin = '0';
			width = largeur +'px';
		}
		this.content.innerHTML = this.contenu;
		this.instance.appendChild(this.content);
		this.instance.className = 'windowBox_box';

		with (this.instance.style){
			position = 'absolute';
			width = widthBox +'px';
			height = heightBox +'px';
			background = '#fff';
			border = '1px solid #666';
		}
		document.body.appendChild(this.instance);
		this.moveBox();
		
		return this.instance;
	},
		
	moveBox: function (){
		var pos = this.pos;
		var screenSize = this.getScreenSize();
		var screenScroll = this.getScreenScroll();
					
		with (this.instance){
			// Alignement horizontal
			switch (this.alignX){
				case 'center':					
					style.left = (screenSize[2] - parseInt(style.width)) / 2 + screenScroll[0] + 'px';
					if (parseInt(style.left) <= 5){
						style.left = 5+'px';
					}
					break;
					
				case 'left':
					style.left = '5px';
					break;
					
				case 'right':
					style.left = parseInt(screenScroll[0] + screenSize[2]) - parseInt(parseInt(style.width) + 5) + 'px';
					break;
				
				default:
					if (parseInt(parseInt(style.width) + pos[0] + 10) > screenSize[2]){
						// Affichage à gauche
						if ((pos[0] - parseInt(style.width)) + screenScroll[0] > 5){
							style.left = (pos[0] - parseInt(style.width)) + screenScroll[0] + 'px';
						} else {
							style.left = '5px';
						}
					} else {
						// Affichage à droite
						style.left = pos[0] + screenScroll[0] + 'px';
					}	
			}

			// Alignement vertical
			switch (this.alignY){
				case 'middle':
					style.top = (screenSize[3] - parseInt(style.height)) / 2 + screenScroll[1] + 'px';
					if (parseInt(style.top) <= 5){
						style.top = 5+'px';
					}
					break;
					
				case 'top':
					style.top = parseInt(screenScroll[1] + 5) + 'px';
					break;
					
				case 'ptop':
					style.top = '5px';
					break;
					
				case 'bottom': 
					style.top = parseInt(screenScroll[1] + screenSize[3]) - parseInt(parseInt(style.height) + 5) + 'px';
					break;
				
				default:	
					if (parseInt(pos[1] - parseInt(style.height)) < 0){
						// Affichage en bas
						style.top = parseInt(pos[1] + screenScroll[1]) + 'px';
					} else {	
						// Affichage en haut
						if ((pos[1] - parseInt(style.height) + screenScroll[1]) > 5){
							style.top = (pos[1] - parseInt(style.height) + screenScroll[1]) + 'px';
						} else {
							style.top = '5px';
						}
					}
			}
		}
		return pos;
	},
	
	// Obtient la taille de la page
	getScreenSize: function(){
		var h, w, x, y;

		// Inspiré du projet Lightbox

		if (window.innerHeight && window.scrollMaxY) {
			x = document.body.scrollWidth;
			y = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			x = document.body.scrollWidth;
			y = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			x = document.body.offsetWidth;
			y = document.body.offsetHeight;
		}

		if (self.innerHeight) {	// all except Explorer
			w = self.innerWidth;
			h = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight){
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		} else {
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}

		if (y < h)			y = h;
		if (x < w)			x = w;

		var screenSize = new Array(x, y, w, h);
		return screenSize;
	},

	// Obtient la position des scrollbars du navigateur
	getScreenScroll: function(){
		var h, w;

		// Inspiré du projet Lightbox

		if (self.pageYOffset){
			w = self.pageXOffset;
			h = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			w = document.documentElement.scrollLeft;
			h = document.documentElement.scrollTop;
		} else {
			w = document.body.scrollLeft;
			h = document.body.scrollTop;
		}

		var screenScroll = new Array(w, h);
		return screenScroll;
	}
}


/*
var showBoxObj = {
	instance: null,	
	event: null,
	
	mouseDown: function(event){
		if (showBoxObj.helpBox != null){
			var el = event.target ? event.target : event.srcElement;
		    while (el.nodeType != 1) el = el.parentNode;
			if (el.className.substr(0, 9) != 'showBox_'){
				document.body.removeChild(showBoxObj.instance);
				showBoxObj.instance = null;
			}
		}
	},

	show: function (event, titleBox, contentBox, widthBox, heightBox){
		this.event = event;
		var hb = this.getBox(titleBox, contentBox, widthBox, heightBox);
	},
	
	getBox: function (titleBox, contentBox, widthBox, heightBox){
		if (this.instance != null){
			document.body.removeChild(this.instance);
			this.instance = null;
		}
		this.createBox(titleBox, contentBox, widthBox, heightBox);
		this.moveBox();
		return this.instance;
	}, 
	
	createBox: function (titleBox, contentBox, widthBox, heightBox){
		this.instance = document.createElement('div');
		
		var btn = document.createElement('div');
		btn.className = 'showBox_btn';
		with (btn.style){
			position = 'absolute';
			width = '60px';
			height = '20px';
			top = 0;
			right = 0;
			lineHeight = '20px';
			textAlign = 'center';
		}
		
		var abtn = document.createElement('a');
		abtn.className = 'showBox_abtn';
		with (abtn.style){
			color = '#444';
		}
		abtn.innerHTML = 'Fermer';
		abtn.href = '#';
		abtn.onclick = function(){
			document.body.removeChild(showBoxObj.instance);
			showBoxObj.instance = null;
			return false;
		};
		btn.appendChild(abtn);
		this.instance.appendChild(btn);
		
		var titre = document.createElement('h4');
		titre.className = 'showBox_titre';
		with (titre.style){
			height = '20px';
			lineHeight = '20px';
			paddingLeft = '10px';
			margin = '0';
			borderBottom = '1px dotted #222';
		}
		titre.innerHTML = titleBox;
		this.instance.appendChild(titre);
		
		var contenu = document.createElement('div');
		contenu.className = 'showBox_contenu';
		hauteur = parseInt(heightBox - 36);
		largeur = parseInt(widthBox - 12);
		with (contenu.style){
			height = hauteur+'px';
			overflow = 'auto';
			padding = '5px';
			margin = '0';
			width = largeur +'px';
		}
		contenu.innerHTML = contentBox;
		this.instance.appendChild(contenu);
		this.instance.className = 'showBox_box';

		with (this.instance.style){
			position = 'absolute';
			width = widthBox +'px';
			height = heightBox +'px';
			background = 'url(img_general/aide_bg.gif) top left no-repeat';
		}
		document.body.appendChild(this.instance);
		
		return this.instance;
	},
	
	getCursorPos: function (){
		return new Array(this.event.clientX, this.event.clientY);
	},
	
	moveBox: function (){
		var pos = this.getCursorPos();
		var screenSize = this.getScreenSize();
		var screenScroll = this.getScreenScroll();
		
		// Gauche = 0, Droite = 1, Haut = 0, Bas = 2, on additionne pour obtenir la position
		var position = 0;
			
		with (this.instance){
			if (parseInt(parseInt(style.width) + pos[0] + 10) > screenSize[2]){
				style.left = (pos[0] - parseInt(style.width)) + screenScroll[0] + 'px';
				position += 0;
			} else {
				style.left = pos[0] + screenScroll[0] + 'px';
				position += 1;
			}			
			if (parseInt(pos[1] - parseInt(style.height)) < 0){
				style.top = (pos[1] + screenScroll[1]) + 'px';
				position += 2;
			} else {	
				style.top = (pos[1] - parseInt(style.height) + screenScroll[1]) + 'px';
				position += 0;
			}
		}
		
		var fleche = document.createElement('div');
		with (fleche.style){
			position = 'absolute';
			width = '16px';
			height = '11px';
		}
		switch (position){
			case 0:
				with (fleche.style){
					bottom = '0';
					right = '-10px';
					background = 'url(img_general/aide_pointe_bd.gif) left bottom no-repeat';
				}
				this.instance.style.left = parseInt(this.instance.style.left) - 10 + 'px';
			break;
			
			case 1:
				with (fleche.style){
					bottom = '0';
					left = '-10px';
					background = 'url(img_general/aide_pointe_bg.gif) left bottom no-repeat';
				}
				this.instance.style.left = parseInt(this.instance.style.left) + 10 + 'px';
			break;
			
			case 2:
				with (fleche.style){
					top = '0';
					right = '-10px';
					background = 'url(img_general/aide_pointe_hd.gif) left bottom no-repeat';
				}
				this.instance.style.left = parseInt(this.instance.style.left) - 10 + 'px';
			break;
			
			case 3:
				with (fleche.style){
					top = '0';
					left = '-10px';
					background = 'url(img_general/aide_pointe_hg.gif) left bottom no-repeat';
				}
				this.instance.style.left = parseInt(this.instance.style.left) + 10 + 'px';
			break;
		}
		this.instance.appendChild(fleche);
	},
	
	// Obtient la taille de la page
	getScreenSize: function(){
		var h, w, x, y;

		// Inspiré du projet Lightbox

		if (window.innerHeight && window.scrollMaxY) {
			x = document.body.scrollWidth;
			y = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			x = document.body.scrollWidth;
			y = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			x = document.body.offsetWidth;
			y = document.body.offsetHeight;
		}

		if (self.innerHeight) {	// all except Explorer
			w = self.innerWidth;
			h = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight){
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		} else {
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}

		if (y < h)			y = h;
		if (x < w)			x = w;

		var screenSize = new Array(x, y, w, h);
		return screenSize;
	},

	// Obtient la position des scrollbars du navigateur
	getScreenScroll: function(){
		var h, w;

		// Inspiré du projet Lightbox

		if (self.pageYOffset){
			w = self.pageXOffset;
			h = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			w = document.documentElement.scrollLeft;
			h = document.documentElement.scrollTop;
		} else {
			w = document.body.scrollLeft;
			h = document.body.scrollTop;
		}

		var screenScroll = new Array(w, h);
		return screenScroll;
	}
}

*/

















