/*** ouvre une nouvelle fenêtre avec largeur l, hauteur h, source src ***/
/*** l par défaut : 500; h par défaut: 350; ***/
/*** tous les contrôles sont bloqués ***/
function dialogBox(src,l,h,nom) {
	var larg = (l==null)?500:l;
	var haut = (h==null)?350:h;
	var win=window.open(src,nom,'toolbar=no,scrollbars=no,width='+larg+',height='+haut);
}

/*** ouvre une nouvelle fenêtre avec largeur l, hauteur h, source src ***/
/*** l par défaut : 650; h par défaut: 500; ***/
/*** tous les contrôles sont activés ***/
function newWindow(src,l,h,nom) {
	var larg = (l==null)?650:l;
	var haut = (h==null)?500:h;
	var win=window.open(src,nom,'toolbar=yes,scrollbars=yes,resizable=yes,menubar=yes,width='+larg+',height='+haut);
}



/*** retourne un élément au hasard dans un tableau passé en paramètre ***/
function auHasard(a) {
	var index = Math.floor(Math.random(new Date())*a.length);
	return a[index];
}

/*** rajoute la page spécifiée aux favoris de l'explorateur ***/
/*** si aucune page n'est spécifiée, rajoute la page courante ***/
function addToFavorites(URL,title) {
	if (document.all) { 
		if ((!URL)&&(!title)) {
			window.external.AddFavorite(location.href, document.title); 
		} else {
			window.external.AddFavorite(URL, title);
		}
	}	else {
		if ((!URL)&&(!title)) {
			alert('Utilisez "CTRL + D" pour ajouter cette page dans vos signets/favoris.'); 
		} else {
			alert('Cette fonction n\'est pas disponible avec votre browser.');
		}
	} 
}

/** retourne l'élément html avec l'id spécifié **/
function getHtmlObj(id) {	return (document.getElementById)?document.getElementById(id):(document.all)?document.all[id]:null; }

/** execute l'action spécifiée si la touche pressée est Enter **/
/** a mettre dans le onkeypress de l'élément **/
/** onkeypress="pressKey(event,'getHtmlObj(\'searchRelatedForm\').submit();')" **/
function pressKey(e,sAction) {
	var ch = e.keyCode;
	if (ch == 13) {	eval(sAction); }
}

/** retourne un objet coordonnées ayant pour propriétés :
			- x : l'abscice de l'élément demandé
			- y : l'ordonnée de l'élément demandé
 **/
function getCoord(el) {
	var coordinates = new Object();
	coordinates.x = _getX(el);
	coordinates.y = _getY(el);

	return coordinates;
}

function _getX (el) {
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
}

function _getY (el) {
	var ot=el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
}

function hideOrShow(sLayerId) {
	hLayer = getHtmlObj(sLayerId);
	if(hLayer != null) {
		if(hLayer.style.display == 'none') {
			hLayer.style.display = '';
		} else {
			hLayer.style.display = 'none';
		}
	}
}

//global timer ID for the safeOnChange function.
var onChangeTimer = null;
//executes an onchange function after 500ms (or specified delay)
function safeOnChange( code, delay ) {
  delay = delay || 500;
  window.clearTimeout( onChangeTimer );
  onChangeTimer = window.setTimeout( code, delay );
} 


/**
 * var lst= getElementsBySelector('div.h');
 * //Renvoie la liste des div avec la classe 'h'
 * var lst= getElementsBySelector('span h1');
 * //Renvoie la liste des H1 contenu par des span
 *
 * //Ensuite, traitez lst comme vous le feriez avec getElementByTagName
 * @param selector
 * @return
 */
function getElementsBySelector(selector){
	//Extracted from nifty.js
	var i,selid="",selclass="",tag=selector,f,s=[],objlist=[];
	if(selector.indexOf(" ")>0){  //descendant selector like "tag#id tag"
		s=selector.split(" ");
		var fs=s[0].split("#");
		if(fs.length==1) return(objlist);
		f=document.getElementById(fs[1]);
		if(f) return(f.getElementsByTagName(s[1]));
		return(objlist);
	}
	if(selector.indexOf("#")>0){ //id selector like "tag#id"
		s=selector.split("#");
		tag=s[0];
		selid=s[1];
	}
	if(selid!=""){
		f=document.getElementById(selid);
		if(f) objlist.push(f);
		return(objlist);
	}
	if(selector.indexOf(".")>0){  //class selector like "tag.class"
		s=selector.split(".");
		tag=s[0];
		selclass=s[1];
	}
	var v=document.getElementsByTagName(tag);  // tag selector like "tag"
	if(selclass=="")
		return(v);
	for(i=0;i<v.length;i++){
		if(v[i].className.indexOf(selclass)>=0)
			objlist.push(v[i]);
	}
	return(objlist);
}


function encode_utf8( s )
{
	return unescape( encodeURIComponent( s ) );
}

function decode_utf8( s )
{
	return decodeURIComponent( escape( s ) );
}

//----------------------------------------------------------------------------
//HasClassName

//Description : returns boolean indicating whether the object has the class name
//built with the understanding that there may be multiple classes

//Arguments:
//objElement              - element to manipulate
//strClass                - class name to add

function hasClassName(objElement, strClass)
{

//	if there is a class
	if ( objElement.className )
	{

		// the classes are just a space separated list, so first get the list
		var arrList = objElement.className.split(' ');

		// get uppercase class for comparison purposes
		var strClassUpper = strClass.toUpperCase();

		// find all instances and remove them
		for ( var i = 0; i < arrList.length; i++ )
		{

			// if class found
			if ( arrList[i].toUpperCase() == strClassUpper )
			{

				// we found it
				return true;

			}

		}

	}

//	if we got here then the class name is not there
	return false;

}

//HasClassName
//----------------------------------------------------------------------------


//----------------------------------------------------------------------------
//AddClassName

//Description : adds a class to the class attribute of a DOM element
//built with the understanding that there may be multiple classes

//Arguments:
//objElement              - element to manipulate
//strClass                - class name to add

function addClassName(objElement, strClass, blnMayAlreadyExist)
{

//	if there is a class
	if ( objElement.className )
	{

		// the classes are just a space separated list, so first get the list
		var arrList = objElement.className.split(' ');

		// if the new class name may already exist in list
		if ( blnMayAlreadyExist )
		{

			// get uppercase class for comparison purposes
			var strClassUpper = strClass.toUpperCase();

			// find all instances and remove them
			for ( var i = 0; i < arrList.length; i++ )
			{

				// if class found
				if ( arrList[i].toUpperCase() == strClassUpper )
				{

					// remove array item
					arrList.splice(i, 1);

					// decrement loop counter as we have adjusted the array's contents
					i--;

				}

			}

		}

		// add the new class to end of list
		arrList[arrList.length] = strClass;

		// add the new class to beginning of list
		//arrList.splice(0, 0, strClass);

		// assign modified class name attribute
		objElement.className = arrList.join(' ');

	}
//	if there was no class
	else
	{

		// assign modified class name attribute      
		objElement.className = strClass;

	}

}

//AddClassName
//----------------------------------------------------------------------------


//----------------------------------------------------------------------------
//RemoveClassName

//Description : removes a class from the class attribute of a DOM element
//built with the understanding that there may be multiple classes

//Arguments:
//objElement              - element to manipulate
//strClass                - class name to remove

function removeClassName(objElement, strClass)
{

//	if there is a class
	if ( objElement.className )
	{

		// the classes are just a space separated list, so first get the list
		var arrList = objElement.className.split(' ');

		// get uppercase class for comparison purposes
		var strClassUpper = strClass.toUpperCase();

		// find all instances and remove them
		for ( var i = 0; i < arrList.length; i++ )
		{

			// if class found
			if ( arrList[i].toUpperCase() == strClassUpper )
			{

				// remove array item
				arrList.splice(i, 1);

				// decrement loop counter as we have adjusted the array's contents
				i--;

			}

		}

		// assign modified class name attribute
		objElement.className = arrList.join(' ');

	}
//	if there was no class
//	there is nothing to remove

}

//RemoveClassName
//----------------------------------------------------------------------------

