/* detection code from http://www.dithered.com/javascript/browser_detect/index.html*/
var ua = navigator.userAgent.toLowerCase();
var isOpera     = (ua.indexOf('opera') != -1);
var is_pc_ie    = (ua.indexOf('msie') != -1 && !isOpera && (ua.indexOf('webtv') == -1) ); 
var is_dom      = (document.getElementById);
var isDOM2Event = (document.addEventListener && document.removeEventListener);

/**
*fonction qui affiche les propri?t?s d'un objet
*/
function /*void*/ traceObject(/*Object*/ obj)
{
	alert(objToString(obj));
}

/**
*fonction qui renvoie un contenant les propri?t?s d'un objet pass? en argument
*/
function /*String*/ objToString(/*Object*/ obj,/*String*/ lineBreakChar)
{
	if(!lineBreakChar)
		var lineBreakChar="\n";
	var result="";
	for ( var prop in obj) result += prop + "\t = " + obj[prop] + lineBreakChar;
	return result;
}

/**
*renvoie l'element dont l'ID est pass? en argument
*/
function getElement(id)
{
	if(document.all) 
	{
		return document.all[id];
	}
	else
	{
		return document.getElementById(id);
	}
}

/**
*cette fonction ecrit un text dans un element HTML qui possed la propriete innerHTML
*http://www.quirksmode.org/js/layerwrite.html
*/
function writeIt(/*String*/ text, /*htmlElemntWithInnerHTML*/ element)
{
	element.innerHTML = ''; /*passage oblig? car bug dans Explorer 5.1 and higher on Mac (but not in 5.0)*/
	element.innerHTML = text;
}

/**
*fonction qui ecrit un text dans un element dont l'ID est pass? en argument [un peu redondante avec getElement suivi de writeIt]
*http://www.quirksmode.org/js/layerwrite.html
*/
function writeItWithId(/*String*/ text,/*String*/ id)
{
	writeIt(text,getElement(id));
}

/**
*fonction qui permet de savoir si un tableau d'?l?ments contient un ?l?ment particulier
*/
function arrayContains(/*Array*/ array,/*String*/ element)
{
	for(var i =0; i < array.length; i++)
	{
		if(array[i] == element)
			return true;
	}
	return false;
}

/**
* AJoute un event listener a un objet
* target, l'objet surlequel ajoute l'event listener
* type, une string represantant l'event ? catche
* callback, la fonction a appell?e lorsque ce dernier evenement survient
* captures, boolean capture event
*/
function addEventListener(target,type,callback,captures) 
{
    if (target.addEventListener) 
    {
        /* EOMB*/
        target.addEventListener(type,callback,captures);
    } 
    else if (target.attachEvent) 
    {
        /* IE*/
        target.attachEvent('on'+type,callback,captures);
    } 
    else 
    {
    	/*IE 5 Mac and some others*/
	    target['on'+type] = callback;
    }
}

function removeEventListener(target,type,callback,captures) 
{
    if (target.removeEventListener) 
    {
        /* EOMB*/
        target.removeEventListener(type,callback,captures);
    } 
    else if (target.detachEvent) 
    {
        /* IE*/
        target.detachEvent('on'+type,callback,captures);
    } 
    else 
    {
    	/*IE 5 Mac and some others*/
	    target['on'+type] = undefined;
    }
}

/**
*
*/
function hasProperties(/*object*/ object , /*string*/ propertiesName)
{
	if(object.hasAttribute)
	{
		return object.hasAttribute(propertiesName);
	}
	return object[propertiesName] != undefined;
}


function openPopupWindow(/*String*/ URL)
{
	URL += '&PopupMode=true';
	return window.open(URL,'','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=800,height=600');
}

function openModalPopup(/*String*/ URL)
{
	URL += '&PopupMode=true';
	if(window.showModalDialog) /* IE fait du vrai modal */
		return window.showModalDialog(URL,'','dialogWidth:800px;dialogHeight:600px');
	else /* pseudo modal pour mozilla */
		return window.open(URL,'','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=800,height=600,modal=yes');
}

/*Renvoie la position x absolue de l'element pass?? en argument*/
function getAbsX(elt) { return (elt.x) ? elt.x : getAbsPos(elt,"Left"); }
/*Renvoie la position y absolue de l'element pass?? en argument*/
function getAbsY(elt) { return (elt.y) ? elt.y : getAbsPos(elt,"Top"); }

/*Renvoie la position (par rapport ?? which) absolue de l'element pass?? en argument*/
function getAbsPos(elt,which) 
{
	iPos = 0;
	while (elt != null) 
	{
		iPos += elt["offset" + which];
		elt = elt.offsetParent;
	}
	return iPos;
}

/*renvoie la hauteur de la zone HTML du navigateur( sans les barres etc..)*/
function getInnerHeight()
{
	if(is_pc_ie)
		return document.body.clientHeight;
	else
		return window.innerHeight;
}

/*renvoie la largeur de la zone HTML du navigateur( sans les barres etc..)*/
function getInnerWidth()
{
	if(is_pc_ie)
		return document.body.clientWidth;
	else
		return window.innerWidth;
}

function displayElement(/*String*/ elementId, /*boolean*/ display)
{
	if(display)
		getElement(elementId).style.display='';
	else
		getElement(elementId).style.display='none';	
}

function fillSelectWithIntRepresentation(/*String*/ selectId, /*IntRepresentation[]*/ optionsIntRep, /*Boolean*/ allowNull, /*int*/ initValue)
{
	var selectUI = getElement(selectId);
	selectUI.options.length = 0;
	var selectIndex = 0;
	if(allowNull)
	{
		selectUI.options[selectIndex++]= new Option('?','');
	}
	for(var i=0;i<optionsIntRep.length;i++)
	{
		var value = optionsIntRep[i].id;
		selectUI.options[selectIndex++]= new Option(optionsIntRep[i].representation, value, value==initValue, value==initValue);
	}
}

function fillSelectWithObjectPresentation(/*String*/ selectId, /*ObjectPresentation[]*/ objectPresentation, /*Boolean*/ allowNull, /*Object*/ initValue)
{
	var selectUI = getElement(selectId);
	selectUI.options.length = 0;
	var selectIndex = 0;
	if(allowNull)
	{
		selectUI.options[selectIndex++]= new Option('?','');
	}
	for(var i=0;i<objectPresentation.length;i++)
	{
		var value = objectPresentation[i].object;
		selectUI.options[selectIndex++]= new Option(objectPresentation[i].representation, value, value==initValue, value==initValue);
	}
}

function hideElement(/*String*/ elementId)
{
	var elementUI = getElement(elementId);
	elementUI.style.display='none';
}

function showElement(/*String*/ elementId)
{
	var elementUI = getElement(elementId);
	elementUI.style.display='';
}

function resetSelect(/*String*/ selectId)
{
	var selectUI = getElement(selectId);
	selectUI.options.length = 0;
	selectUI.value='';
}

/*renvoie la position courrante en scroll de la page*/
function getCurrentYPos() 
{
    if (document.body && document.body.scrollTop)
      return document.body.scrollTop;
    if (document.documentElement && document.documentElement.scrollTop)
      return document.documentElement.scrollTop;
    if (window.pageYOffset)
      return window.pageYOffset;
    return 0;
}

/*renvoie un param?tre de l'url de ce document*/
function getURLParam(strParamName){
  var strReturn = null;
  var strHref = window.location.href;
  if ( strHref.indexOf("&") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("&")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return strReturn;
}