function changerImage(img, src, maxWidth, maxHeight)
{
   var image = new Image();
 
   image.onerror = function()
   {
	  alert("Erreur lors du chargement de l'image");
   }
 
   image.onabort = function()
   {
	  alert("Chargement interrompu");
   }
 
   // une fois l'image chargée :
   image.onload = function()
   {
	  // si l'image est désignée par son id
	  if(typeof img == "string")
		 img = document.getElementById(img);
 
	  // si l'image doit être redimensionnée
	  var reduction = 1;
	  if(maxWidth && maxHeight)
		 if(image.width > maxWidth || image.height > maxHeight)
			reduction = Math.max(image.width/maxWidth, image.height/maxHeight);
 
	  // on affiche l'image
	  img.src = image.src;
	  img.width = Math.round(image.width / reduction);
	  img.height = Math.round(image.height / reduction);
   }
 
   image.src = src;
}

function getRadioValue(radio)
{
	val=0;

	if(radio.length)
	{
		for(i=0;i < radio.length;i++)
		{
			if(radio[i].checked == true)
			{
				val=radio[i].value;

				break;
			}
		}
	}
	else
	{
		val=radio.value;
	}

	return val;
}

function isMSIE()
{
	if(navigator.appVersion.indexOf('MSIE') == -1)
	{
		return false;
	}

	return true;
}

function addListener(element,baseName,handler)
{
	if(element.addEventListener)
	{
		element.addEventListener(baseName, handler, false);
	}
	else if(element.attachEvent)
	{
		element.attachEvent('on'+baseName, handler);
	}
}

function fenetreCent(url,nom,largeur,hauteur,options)
{
	var haut=(screen.height-hauteur)/2;
	var Gauche=(screen.width-largeur)/2;
	fencent=window.open(url,nom,"top="+haut+",left="+Gauche+",width="+largeur+",height="+hauteur+","+options);
	fencent.focus();
}

function openWindow(url,largeur,hauteur,scrollbars,resizable)
{
	if(largeur == 0 || hauteur == 0)
	{
		win=window.open(url,'_blank');
	}
	else
	{
		win=window.open(url,'popup_'+largeur+'x'+hauteur+'_'+scrollbars+'_'+resizable,'width='+largeur+',height='+hauteur+',top='+((screen.height-hauteur)/2)+',left='+((screen.width-largeur)/2)+',scrollbars='+scrollbars+',resizable='+resizable);

		if(win != null)
		{
			win.focus();
		}
	}

	if(win == null)
	{
		alert('La fenêtre pop-up n\'a pas pu s\'ouvrir.\n\nVeuillez désactiver votre anti-popup et réessayer !');
	}

	return win;
}

function checkAll(form)
{
	for(i=0;i < form.elements.length;i++)
	{
		if(form.elements[i].type == 'checkbox' && form.elements[i].disabled == false)
		{
			form.elements[i].checked=true;
		}
	}
}

function uncheckAll(form)
{
	for(i=0;i < form.elements.length;i++)
	{
		if(form.elements[i].type == 'checkbox' && form.elements[i].disabled == false)
		{
			form.elements[i].checked=false;
		}
	}
}

function insertIntoTextArea(textarea,ajout)
{
	if(document.selection)
	{
		textarea.focus();

		sel=document.selection.createRange();

		if(sel.text != '')
		{
			ajout=ajout.replace(/Ecrivez ici/g,sel.text);
		}

		sel.text=ajout;
	}
	else if(textarea.selectionStart || textarea.selectionStart == '0')
	{
		var startPos=textarea.selectionStart;
		var endPos=textarea.selectionEnd;
		var texte=textarea.value;
		var sel_text=texte.substring(startPos,endPos);

		if(sel_text != '')
		{
			ajout=ajout.replace(/Ecrivez ici/g,sel_text);
		}

		textarea.value=texte.substring(0,startPos)+ajout+texte.substring(endPos,texte.length);
	}
	else
	{
		textarea.value+=ajout;
	}

	textarea.focus();
}

function evalEntryLength(curField,countField,maxLimit,discardXtra)
{
	charCount=curField.value.length;

	if(charCount > maxLimit && discardXtra)
	{
		//curField.value=curField.value.substr(0,maxLimit);
	}

	document.getElementById(countField).innerHTML=((maxLimit - charCount) > 0)?(maxLimit - charCount):0;
}

function newXHRObject()
{
	var xhr=null;

	if(window.XMLHttpRequest)
	{
		xhr=new XMLHttpRequest();

		if(xhr.overrideMimeType)
		{
			xhr.overrideMimeType('text/xml');
		}
	}
	else if(window.ActiveXObject)
	{
		try
		{
			xhr=new ActiveXObject('Msxml2.XMLHTTP');
		}
		catch(e)
		{
			try
			{
				xhr=new ActiveXObject('Microsoft.XMLHTTP');
			}
			catch(e)
			{
				xhr=null;
			}
		}
	}

	return xhr;
}

function switchPopUp(popup,e,offsetX,offsetY)
{
	popupNode=document.getElementById(popup);

	if(document.getElementById && popupNode)
	{
		if(popupNode.style.visibility == 'visible')
		{
			popupNode.style.visibility='hidden';
		}
		else
		{
			if(navigator.appName.substring(0,3) == 'Net')
			{
				document.captureEvents(Event.MOUSEMOVE);
			}

			posX=getPosX(e);
			posY=getPosY(e);

			posX+=offsetX;
			posY+=offsetY;

			popupNode.style.left=posX+'px';
			popupNode.style.top=posY+'px';
			popupNode.style.zIndex='1';
			popupNode.style.visibility='visible';

			body=document.getElementsByTagName('BODY')[0];

			body.insertBefore(popupNode,body.firstChild);
		}

		return true;
	}

	return false;
}

function getPosX(e)
{
	posX = e.pageX ? e.pageX : e.clientX;

	if(isMSIE())
	{
		posX += document.documentElement.scrollLeft;
	}

	return posX;
}

function getPosY(e)
{
	posY = e.pageY ? e.pageY : e.clientY;

	if(isMSIE())
	{
		posY += document.documentElement.scrollTop;
	}

	return posY;
}
