﻿//          ________
//          |/\/\/\|
//          | o o  |
//-----oOOO---(_---OOOo---------------------------------------------------------------------------------------
//
// Auteur      : Chanh T.Do [Thoransoft - 2008.04.08]
// Description : fichier contenant les fonctions globales du site de la Centrale ATR
// JScript File
//------------------------------------------------------------------------------------------------------------

/**
* @fileOverview
  globalfunc.js [JScript File]: <br>
  Scripts de fonctions globales utilisées dans le site de l'ATR <br>
  @author: Chanh T.Do [www.thoransoft.com - 2008.04.08] <br>
  Version 1.0 */

/** Fonction permettant de précharger les images en javascript.<br>
    Retourne undefined s'il ne trouve pas l'objet.
    @param {array} arguments - Vecteur contenant les images à charger
    @return vecteur des images qui ont été pré-chargées
    @type array */
function preloadImg()
{ 
  var vImg = new Array();   //Initialiser le tableau d'image
  if (document.images)      //Vérifier s'il y a des images dans le documents
  {
    //Boucler dans le array des arguments pour pré-charger les images
    for (var i=0; i<arguments.length; i++)
    {
      vImg[i] = new Image();
      vImg[i].src = arguments[i];
    }
    return vImg;    //Retourner le tableau d'image préchargée
  }
}


/* IndexOf pour array */
/* provient de: http://www.tutorialspoint.com/javascript/array_indexof.htm */
if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(elt /*, from*/) {
        var len = this.length;

        var from = Number(arguments[1]) || 0;
        from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
        if (from < 0)
            from += len;

        for (; from < len; from++) {
            if (from in this &&
          this[from] === elt)
                return from;
        }
        return -1;
    };
}


/** Fonction permettant de choisir une image au hasard.
    Prend un tableau d'image en entrée et retourne une seule image choisie au hasard. <br>
    <b>Note</b>: retourne undefined s'il ne trouve pas l'objet.
    @param {array} vImg - Vecteur contenant les ID des images dont on voudrait prendre une au hasard
    @return ID de l'images choisie au hasard
    @type string */
function getRandomImg(vImg)
{
  var borne_max = vImg.length;  //Borne maximum pour le random
  
  if (borne_max > 0)  //Générer un nombre
  {
    return vImg[Math.floor(Math.random() * borne_max)];
  }
}


/** Fonction permettant de colorer les couleur du grid lorsqu'on passe la souris dessus. <br>
    @param {object} element - Element à colorer (tablecells)
    @param {string} oGrid - ID de la grid en cours de traitement */
var oldgridSelectedColor;
function setMouseOverColor(element, oGrid)
{ 
  if (!document.getElementById(oGrid).disabled)
  {
    oldgridSelectedColor = element.style.backgroundColor;
    element.style.backgroundColor='#f6f6f8';
    element.style.cursor='pointer';
  }
}


/** Fonction permettant de décolorer les couleur du grid lorsqu'on passe la souris dessus. <br>
    @param {object} element - Element à colorer (tablecells)
    @param {string} oGrid - ID de la grid en cours de traitement */
function setMouseOutColor(element, oGrid)
{
  if (!document.getElementById(oGrid).disabled)
  {
    element.style.backgroundColor=oldgridSelectedColor;
  }
}


/** Fonction permettant de générer un nombre au hasard.
    @param {int} lbound - Valeur de la borne de début
    @param {int} ubound - Valeur de la borne de fin 
    @return Nombre au hasard (integer arrondi)
    @type int */
function getRandomNum(lbound, ubound) 
{
  return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
}


/** Fonction permettant de générer des caractères au hasard.
    @param {bool} number - Mettre à true si on veut obtenir des chiffres dans la génération (0123456789)
    @param {bool} lower - Mettre à true si on veut obtenir des caractères minuscules (abcdefghijklmnopqrstuvwxyz)
    @param {bool} upper - Mettre à true si on veut obtenir des caractères majuscule (ABCDEFGHIJKLMNOPQRSTUVWXYZ)
    @param {bool} other - Mettre à true si on veut obtenir les caractères spéciaux (`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/? )
    @param {string} extra - Contient un chaîne de caractères extra que l'utilisateur peut définir
    @return Le caractère générés au hasard
    @type char */
function getRandomChar(number, lower, upper, other, extra) 
{
  var numberChars = "0123456789";
  var lowerChars = "abcdefghijklmnopqrstuvwxyz";
  var upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var otherChars = "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/? ";
  var charSet = extra;

  if (number) {charSet += numberChars;}
  if (lower) {charSet += lowerChars;}
  if (upper) {charSet += upperChars;}
  if (other) {charSet += otherChars;}
  return charSet.charAt(getRandomNum(0, charSet.length));
}


//Fonction permettant de générer une chaîne de caractère aux hasard (peut être utilisée pour un password, ID etc...)
function getRandomString(length, extraChars, firstNumber, firstLower, firstUpper, firstOther,
                         latterNumber, latterLower, latterUpper, latterOther) 
{
  var rc = "";
  if (length > 0) { rc = rc + getRandomChar(firstNumber, firstLower, firstUpper, firstOther, extraChars); }
  
  for (var idx = 1; idx < length; ++idx) 
      { rc = rc + getRandomChar(latterNumber, latterLower, latterUpper, latterOther, extraChars); }
  
  return rc;
}


//Fonction pour enlever les CRLF d'une chaîne de caractère
function rmCRLF(strHTML)
{
  var newHTML = "";
  
  //Replace méthode ne fonctionne pas. Il faut le faire manuellement.
  for (var i=0; i<strHTML.length; i++)
  {
    carac = strHTML.substr(i,1);
    if ((carac != "\n") && (carac != "\r")) { newHTML += carac; }
  }

  return newHTML;
}


//Fonction pour protéger un caractère avant l'enregistrement à la BD
//Retourne la string protégée.
function protectString(carToProtect, strIn)
{
  var strOut = "";
  
  for (var i=0; i<strIn.length; i++)
  {
    if (strIn.substr(i,1) == carToProtect) { strOut += "\\" + carToProtect; }
    else { strOut += strIn.substr(i,1); }
  }

  return strOut
}

//+--------------------------------------------------------------------
//| S e t _ C o o k i e 
//|
//| Cette fonction fera la creation d'un cookie.
//|
//+--------------------------------------------------------------------
function Set_Cookie( name, value, expires, path, domain, secure ) 
{

//Expiration du cookie
var today = new Date();
today.setTime( today.getTime() );

if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
( ( path ) ? ";path=" + path : "" ) + 
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}


//+--------------------------------------------------------------------
//| G e t _ C o o k i e 
//|
//| Cette fonction fera la lecture de la valeur d'un cookie.
//| (Prise du web http://techpatterns.com/downloads/javascript_cookies.php)
//+--------------------------------------------------------------------
function Get_Cookie( check_name ) {

	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

//+--------------------------------------------------------------------
//| D e l e t e _ C o o k i e 
//|
//| Cette fonction fera la suppression d'un cookie.
//| (Prise du web http://techpatterns.com/downloads/javascript_cookies.php)
//+--------------------------------------------------------------------
function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}


/** Fonction permettant de cacher un élément HTML par son ID
    @param {string} id - ID de l'élement à cacher */
function hideElementById(id)
{ 
  var e = document.getElementById(id);
  if (e != undefined) { e.style.display = 'none'; }
}


/** Fonction permettant d'afficher un élément HTML par son ID
    @param {string} id - ID de l'élement à afficher */
function unhideElementById(id)
{ 
  var e = document.getElementById(id);
  if (e != undefined) { e.style.display = 'block'; }
}


/** Fonction permettant de swapper le style d'affichage (block devient none et vice-versa) <br>
    @param {string} id - ID de l'élement à swapper */
function swapDisplay(id)
{ 
  var e = document.getElementById(id);
  if (e != undefined) 
  { 
    if (e.style.display == 'block') { e.style.display = 'none'; }
    else { e.style.display = 'block'; }
  }
}


/** Fonction pour rediriger vers une page html
    @param {string} strURL - Contient l'adresse URL */
function gotoURL(strURL)
{
  window.status=('Connect to ' + strURL);
  var location=(strURL);
  this.location.href = location;
}


/** Fonction permettant de retourner l'URL de la page
    @return {string} Contient l'URL de la page courante */
function getCurrentPageURL() { return document.URL; }


/** Fonction permettant de colorer le lien du menu de la page 
    @param {string} menuID - ID du conteneur du menu */
function colorLnkPage(menuID)
{ 
  var strURL = getCurrentPageURL();
  
  //À partir du menu1 du site, on colore le menu item correspondant à l'URL
  var eMenu = document.getElementById(menuID);
  if (eMenu != undefined)
  {
    vMenuLst = eMenu.getElementsByTagName('a');
    for (var i=0; i<vMenuLst.length; i++)
    {
      if (vMenuLst[i].href == strURL) { vMenuLst[i].style.color = '#ffb416';}
      else { vMenuLst[i].style.color = ''; }
    }
  }
}


/** Fonction permettant de colorer le lien du menu de la page 
    @param {string} menuID - ID du conteneur du menu */
function colorLnkPageBackEnd()
{ 
  var strURL = getCurrentPageURL();
  //Patch pour parametres
  //strURL = strURL.substring(strURL.lastIndexOf('/')+1, strURL.length-5);
  strURL = strURL.substring(strURL.lastIndexOf('/')+1, strURL.lastIndexOf('.aspx'));

  //Patch pour Ticket911 
  if ((strURL.toUpperCase().lastIndexOf("COMMENTS") > 0) || (strURL.toUpperCase().lastIndexOf("TICKETS") > 0))
  {
    strURL="managecases";
  }  

  //Patch pour Ticket911
  if (strURL.toUpperCase().lastIndexOf("USER") > 0)
  {
    strURL="managecustomers";
  }  

  var eSpan = document.getElementById(strURL);
  
  eSpan.innerHTML = "[" + eSpan.innerHTML.replace(/&nbsp;/g, "") + "]";
  
  var a = eSpan.getElementsByTagName('a')[0];
  a.style.color = '#fff';
  a.style.backgroundColor = '#0170a8';
}


/** Fonction pour obtenir les paramètres dans un URL
    @param {string} strParamName - Nom du paramètre 
    return {string} strReturn - Valeur du paramètre */
function getURLParam(strParamName)
{
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 )
  {
    var strQueryString = strHref.substr(strHref.indexOf("?"));
    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 unescape(strReturn);
}


/** Fonction retournant un index d'un dropdown par la valeur 
    @param {string} ddlID - Contient le ID du Dropdown
    @param {variant} value - Contient la valeur d'un élément du dropdown */
function getSelectedIndexByValue(ddlID, value)
{
  var ddl = document.getElementById(ddlID);
  if (ddl != undefined)
  {
    var optionLst = ddl.getElementsByTagName('option');
    for (var i=0; i<optionLst.length; i++) { if (optionLst[i].value == value) { return i; }}
  }
  return 0;
}


/* Fonction permettant d'afficher un signe + ou un signe - lorsqu'il y a expand ou collapse 
   @param {string} id = ID de l'élément */
function expand_or_collapse(id)
{
  var e = document.getElementById(id);
  if (e != undefined) 
  { 
    if (e.innerHTML == '+') { e.innerHTML = '&ndash;'; }
    else { e.innerHTML = '+'; }
  }
}


//+--------------------------------------------------------------------
//| f a d e
//|
//| Cette fonction fera le fadeout d'un Element passe en parametre
//| (Prise du web http://www.switchonthecode.com/tutorials/javascript-tutorial-simple-fade-animation)
//+--------------------------------------------------------------------
function fade2(eid,speedin)
{
  var element = document.getElementById(eid);
  var TimeToFade = speedin;
  if(element == null)
    return;
   
  if(element.FadeState == null)
  {
    if(element.style.opacity == null 
        || element.style.opacity == '' 
        || element.style.opacity == '1')
    {
      element.FadeState = 2;
    }
    else
    {
      element.FadeState = -2;
    }
  }
    
  if(element.FadeState == 1 || element.FadeState == -1)
  {
    element.FadeState = element.FadeState == 1 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
  }
  else
  {
    element.FadeState = element.FadeState == 2 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade;
    setTimeout("animateFade2(" + new Date().getTime() + ",'" + eid + "'," + speedin + ")", 33);
  }  
  
  
}

//+--------------------------------------------------------------------
//| a n i m a t e F a d e
//|
//| Cette fonction sert a animer le fadeout d'un Element passe en parametre
//| (Prise du web http://www.switchonthecode.com/tutorials/javascript-tutorial-simple-fade-animation)
//+--------------------------------------------------------------------
function animateFade2(lastTick, eid, speedin)
{
  
  var TimeToFade = speedin 
  var curTick = new Date().getTime();
  var elapsedTicks = curTick - lastTick;
  
  var element = document.getElementById(eid);
 
  if(element.FadeTimeLeft <= elapsedTicks)
  {
    element.style.opacity = element.FadeState == 1 ? '1' : '0';
    element.style.filter = 'alpha(opacity = ' 
        + (element.FadeState == 1 ? '100' : '0') + ')';
    element.FadeState = element.FadeState == 1 ? 2 : -2;
    element.style.display = 'none' //AJOUT JL. Pour ne plus pouvoir utiliser l'element apres le fade
    
    return;
  }
 
  element.FadeTimeLeft -= elapsedTicks;
  var newOpVal = element.FadeTimeLeft/TimeToFade;
  if(element.FadeState == 1)
  {
    //newOpVal = 1 - newOpVal;
    //alert('test');
}
  element.style.opacity = newOpVal;
    //alert(newOpVal); 
  element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';
  
  //setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
  setTimeout("animateFade2(" + curTick + ",'" + eid + "'," + speedin + ")", 33);
}

//+--------------------------------------------------------------------
//| a l e r t F a d e r
//|
//| Function permettant d'ajouter un div dynamiquement pour les alerts
//| 
//| Entrants:  eIDin (Element ID sur lequel se positioner)
//|            textIn (texte du alert)
//|            OffsetIn (Differentiel Left negatif ou positif)
//+--------------------------------------------------------------------
function alertFader(eIDin, textIn, offsetIn, offsetTopIn, DelaiIn) {
  
  if (document.getElementById(eIDin) == undefined)
    { var ni = document.getElementsByTagName('body')[0] }    
  else
    { var ni = document.getElementById(eIDin); }
  
  var divIdName = eIDin+'subDiv'; //Nouveau nom du div dynamique
    
  //On valide qu'il n'existe pas deja
  var newOld = document.getElementById(eIDin+'subDiv');
  var newdiv;
  
  //Si aucun differentiel n'a ete specifié, nous mettons 0 comme valeur defaut
  if (offsetIn == undefined)
    { offsetIn = 0; }

  //Si aucun differentiel n'a ete specifié, nous mettons 0 comme valeur defaut
  if (offsetTopIn == undefined)
    { offsetTopIn = 0; }

  //Si aucun delai
  if (DelaiIn == undefined)
    { DelaiIn = 1750; }
 

  //Si l'element existait deja, nous le reprenons etant donnee 
  //qu'il est seulement cache
  if (newOld == undefined)
    { newdiv = document.createElement('div'); }
  else
    { newdiv = newOld;   }
  
  //On affecte le nom
  newdiv.setAttribute('id',divIdName);      

  //reset CSS et positionement
  newdiv.className = 'PopUpFader';
  newdiv.style.display = 'block';
  newdiv.style.opacity = 1;
  newdiv.style.filter = "alpha(opacity = '100')";  
  newdiv.FadeState == 1;
  newdiv.style.zIndex = 3000;
  
  //var strStylePopUp = "style='background:#006699; text-align: right; padding: 3px 10px 3px 10px; border-bottom : 1px solid #AAA;'"
  //newdiv.innerHTML = "<div " + strStylePopUp + "><a href=\"javascript:void(0);\" onClick=\"javascript:document.getElementById('" + divIdName + "').style.display = 'none';\">x</a> </div><p>" + textIn + "</p>";
  newdiv.innerHTML = "<p>" + textIn + "</p>";
  document.body.appendChild(newdiv);
  
  //Si sur body nous centrons (repositionnement)
  if (document.getElementById(eIDin) == undefined)
  { 
    newdiv.style.position = "fixed";
    newdiv.style.top = "50%";
    newdiv.style.left = "50%";
    newdiv.style.marginLeft = "-200px";
    
    //Calculer la hauteur du Div
    var newHeight = (parseInt(newdiv.offsetHeight, 10) + 100);
    newdiv.style.marginTop = '-' + parseInt((newHeight / 2),10) + 'px';
    newdiv.style.textAlign = 'center';
    newdiv.style.width ='300px';
    newdiv.style.padding = '50px';
  }
  else
  { 
    newdiv.style.left = parseInt(getLeft(ni) + offsetIn) + 'px';
    newdiv.style.top = parseInt(getTop(ni) + offsetTopIn) + 'px';
  } 
  setTimeout("fade2('" + divIdName + "', 500)",DelaiIn);   
}


//========================DEBUT CONFIRM BOX============================================================================
//+--------------------------------------------------------------------
//| C o n f i r m   B o x
//|
//| Function permettant d'ajouter un div dynamiquement pour les confirmations
//| avec optionnellement un checkbox. Il sera accrocher sur l'element passe
//| en parametre ou sinon centré si aucun element est passe (nullstring)
//| Entrants:  eIDin (Element ID sur lequel se positioner)
//|            textIn (texte du alert)
//|            OffsetIn (Differentiel Left negatif ou positif)
//+--------------------------------------------------------------------
function confirmBox(textIn,strButtonOkText, strButtonCancelText, function2Run, blnCheckbox, strChkBoxText) {

    //Appel de la function retournant les points (position) du centre  
    var point = window.center();
    var newdiv = document.createElement("div");
    newdiv.setAttribute('id', 'confirmBoxID');

    var strText = textIn + "<br /><br />";

    if (blnCheckbox == true) {

        if (strChkBoxText != undefined) 
        {           
            strText += "&nbsp;&nbsp;<form name='f1_confirm' style='display:inline;'><input id='chkConfirmBox' type='checkbox' name='cb1_confirm'/>";
            strText += "<span onClick='document.f1_confirm.cb1_confirm.checked=(! document.f1_confirm.cb1_confirm.checked);' style='cursor:default;'>";            
            strText += strChkBoxText + "</span></form>";   
        }            
        else
        { strText += "&nbsp;&nbsp;<input id='chkConfirmBox' type='checkbox' />"; }

        strText += "<br /><br />";
        strText += "<div style='text-align:center'>";
        strText += "<input type='button' value='" + strButtonOkText + "' onclick='if (document.getElementById(\"chkConfirmBox\").checked) {" + function2Run.replace(/`/gi, "\"") + "; confirmBoxClose(); }' />";
    }
    else {
      strText += "<div style='text-align:center'>";
      strText += "<input type='button' value='" + strButtonOkText + "' onclick='" + function2Run.replace(/`/gi, "\"") + "; confirmBoxClose(); ' />";
    }
    strText += "<input type='button' value='" + strButtonCancelText + "' onclick='confirmBoxClose();' />";
    strText += "</div>";
   
    newdiv.style.position = "fixed";
    newdiv.style.top = "50%";
    newdiv.style.left = "50%";
    newdiv.style.marginLeft = "-200px";
    newdiv.style.marginTop = "-150px";
    newdiv.style.width = "300px";
    newdiv.style.height = "auto";
    newdiv.style.backgroundColor = "#F7F7F7";
    newdiv.style.padding = "50px";
    newdiv.style.textAlign = "center";
    newdiv.style.border = "1px solid #aaa";
    newdiv.className = 'ombrage';
    newdiv.innerHTML = strText;
    newdiv.style.display = 'none';
    document.body.appendChild(newdiv);
    showModalBeforeElementId('confirmBoxID', '0', true);
    newdiv.style.display = 'block';
}

function confirmBoxClose() {
    var objConfirm = document.getElementById('confirmBoxID');
    removeModal('confirmBoxID');
    document.body.removeChild(objConfirm);
}
//========================FIN CONFIRM BOX============================================================================





//+--------------------------------------------------------------------
//| getTop
//|
//| Function permettant de retourner la position relative TOP d'un element
//| (prise du web http://bytes.com/topic/javascript/answers/90547-how-get-absolute-position-element)
//+--------------------------------------------------------------------
function getTop( oElement )
{
var iReturnValue = 0;
while( oElement != null ) {
iReturnValue += oElement.offsetTop;
oElement = oElement.offsetParent;
}
return iReturnValue;
}

//+--------------------------------------------------------------------
//| getLeft
//|
//| Function permettant de retourner la position relative LEFT d'un element
//|  (prise du web http://bytes.com/topic/javascript/answers/90547-how-get-absolute-position-element)
//+--------------------------------------------------------------------
function getLeft( oElement )
{
var iReturnValue = 0;
while( oElement != null ) {
//alert(oElement.offsetLeft);
iReturnValue += oElement.offsetLeft;
oElement = oElement.offsetParent;
//oElement.style.border = '1px solid red';
}
return iReturnValue;
}

//+--------------------------------------------------------------------
//| getScrollXY
//|
//| Function permettant de retourner la position relative LEFT du scroll
//|  (prise du web http://www.howtocreate.co.uk/tutorials/javascript/browserwindow)
//+--------------------------------------------------------------------
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}


/* Fonction permettant de mettre un panel modal par-dessus le navigateur 
   @param {string} id - Contient le ID de l'élément dont on veut insérer le divModal avant 
   @param {string} valeur en decimal de l'opacity (optionnel) 
   @param {bool} valeur indiquant si il y a plusieurs modal (meme id) ou un seul unique */
function showModalBeforeElementId(id, intOpacity, unique)
{
  var divModal = document.createElement('div');
  var screenLenght = window.size();

  var strOpacity = "0.6";
  var strAlpha = "60";
  
  if (intOpacity != undefined) 
  {
      if (intOpacity == "1" || intOpacity == "1.0")
        { strOpacity = "1.0"; strAlpha = "100"; }
      else
      { strOpacity = intOpacity; strAlpha = intOpacity.replace("0.", ""); }
  }

  divModal.setAttribute('style', 'position:fixed; top:0px; left:0px; background-color:#fff; opacity:' + strOpacity + '; -moz-opacity:' + strOpacity + '; -ms-filter:"alpha(opacity=' + strAlpha + ')"; filter:alpha(opacity=' + strAlpha + ');');
  divModal.style.position = 'fixed';
  divModal.style.opacity = strOpacity;
  divModal.style.filter = "alpha(opacity = '" + strAlpha + "')";    
  divModal.style.top = '0px';
  divModal.style.left = '0px';
  divModal.style.backgroundColor = (intOpacity == 0) ? '' : '#000';
  divModal.style.width = "100%"; /* screenLenght.width + 'px';*/
  divModal.style.height = "100%"; /* screenLenght.height + 'px';*/
  divModal.style.zIndex = "9000";

  //Si nous voulons un modal avec un ID unique nous combinerons les divModal et le ID en param.
  if (unique != undefined)
  { divModal.setAttribute('id', 'divModal' + id); }
  else
  { divModal.setAttribute('id', 'divModal'); }

  var element = document.getElementById(id);
  var parentElement = element.parentNode;
  parentElement.insertBefore(divModal, element);
}


/* Fonction permettant d'enlever le DIV modal 
@param {string} id - optionnel le nom du div */
function removeModal(id) {

  //Si aucun parametre id nous supprimons l'element divModal
  if (id == undefined)
  { var divModal = document.getElementById('divModal'); }
  else
  { var divModal = document.getElementById('divModal' + id); }
    
  if (divModal != undefined)
  {
    var parentElement = divModal.parentNode;
    parentElement.removeChild(divModal);
  }
}


//Resize du divModal lors d'un resize du window
function resizeDivModal ()
{
  var e = document.getElementById('divModal');
  if (e != undefined)
  {
    //Reset lorsqu'on réduit le windows (pour pas que les scrollbars restent s'ils ne sont pas nécessaires)
    e.style.width = "100%";   
    e.style.height = "100%";
    
    //Mets la bonne valeur du height lorsqu'il y a des scrollbars
    var screenLenght = window.size();
    e.style.width = screenLenght.width + 'px';
    e.style.height = screenLenght.height + 'px';
  }
}


/* Permet d'obtenir le size de l'écran du navigateur
   provient de: http://www.geekdaily.net/2007/07/04/javascript-cross-browser-window-size-and-centering/
   + addition du scrollHeight/Width pour obtenir le Height/Width du document avec scrollbar */
window.size = function() {
    var w = 0;
    var h = 0;

    if (!window.innerWidth) //IE
    {
        //strict mode
        if (!(document.documentElement.clientWidth == 0)) {
            w = (document.documentElement.scrollWidth != 0) ? document.documentElement.scrollWidth : document.documentElement.clientWidth;
            h = (document.documentElement.scrollHeight != 0) ? document.documentElement.scrollHeight : document.documentElement.clientHeight;
        }
        else //quirks mode
        {
            w = (document.body.scrollWidth != 0) ? document.body.scrollWidth : document.body.clientWidth;
            h = (document.body.scrollHeight != 0) ? document.body.scrollHeight : document.body.clientHeight;
        }
    }
    else //w3c
    {
        w = (document.documentElement.scrollWidth != 0) ? document.documentElement.scrollWidth : window.innerWidth;
        h = (document.documentElement.scrollHeight != 0) ? document.documentElement.scrollHeight : window.innerHeight;

        if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {

            //Afin de corriger la problematique du height du body dans chrome, nous faisons un petit overide
            //permettant de reprendre la bonne hauteur.
            if (document.documentElement.scrollHeight != 0) {
                if (window.innerHeight > document.documentElement.scrollHeight)
                { h = window.innerHeight; }
            }
        }
    }
    return { width: w, height: h };
}


/* Permet d'obtenir le point central de la page
   provient de: http://www.geekdaily.net/2007/07/04/javascript-cross-browser-window-size-and-centering/ */
window.center = function()
{
	var hWnd = (arguments[0] != null) ? arguments[0] : {width:0,height:0};

	var _x = 0;
	var _y = 0;
	var offsetX = 0;
	var offsetY = 0;

	//IE
	if(!window.pageYOffset)
	{
		//strict mode
		if(!(document.documentElement.scrollTop == 0))
		{
			offsetY = document.documentElement.scrollTop;
			offsetX = document.documentElement.scrollLeft;
		}
		//quirks mode
		else
		{
			offsetY = document.body.scrollTop;
			offsetX = document.body.scrollLeft;
		}
	}
	//w3c
	else
	{
		offsetX = window.pageXOffset;
		offsetY = window.pageYOffset;
	}

	_x = ((this.size().width-hWnd.width)/2)+offsetX;
	_y = ((this.size().height-hWnd.height)/2)+offsetY;

	return{x:_x,y:_y};
}


/* Fonction permettant de rendre un checkbox checked lorsqu'il ne l'est pas 
   et de le rendre unchecked lorsqu'il est. 
   @param {string} id - Contient le id du checkbox */
function chkMe(id)
{
  var cBox = document.getElementById(id);
  if (cBox != undefined)
  {
    if (cBox.disabled == false)
    {
      if (cBox.checked) { cBox.checked = false; }
      else { cBox.checked = true; }
    }
  }
}



/* Fonction permettant de rendre un checkbox checked lorsqu'il ne l'est pas 
   et de le rendre unchecked lorsqu'il est. 
   @param {string} id - Contient le id du checkbox */
function radio_chkMe(id)
{
  var cBox = document.getElementById(id);
  if (cBox != undefined)
  {
    if (cBox.disabled == false) { if (!cBox.checked) { cBox.checked = true; } }
  }
}


//------------------------------------------------------------------------------
// W a i t M a c h i n e   Funcs
//------------------------------------------------------------------------------
var WaitMachineProcessList = new Array();

/* Fonction qui initialise le wait machine */
function WaitMachineStart() 
{
    setTimeout('WaitMachineCheck();',1000);
}

/* Function permettant d'ajouter un processus au waitMachine */
function WaitMachineProcessAdd(strProcessName) 
{ WaitMachineProcessList[WaitMachineProcessList.length] = strProcessName; }

/* Function permettant d'enlever un processus au waitMacine */
function WaitMachineProcessRemove(strProcessName) {
    WaitMachineProcessList.remove(WaitMachineProcessList.indexOf(strProcessName)); }

/* Function qui valide le nombre de processus encore actif et relance de facon 
   recursive le Check */
function WaitMachineCheck() 
    {
  
    if (WaitMachineProcessList.length == 0)
    { waitMachine(false); }
    else 
    {
    waitMachine(true, WaitMachineProcessList[0]);
    setTimeout('WaitMachineCheck();', 500);
    } 
    
    }

/* Function  permettant d'afficher un wait picture avec optionnellement un message
    @param {bool} blnSwitch - Permet de mettre a ON ou OFF le waiter
    @param {string} strMessage - Optionnel, message jumelé au waitMachine
    */
function waitMachine(blnSwitch, strMessage)
{
         
   //Selon le parametre nous affichons ou suprimons le div
   try { document.body.removeChild(document.getElementById("waitMachineID")); removeModal('waitMachineID'); }
    catch(err) {  }
    
    if (!blnSwitch) {return; }

    var newdiv = document.createElement("div");
    newdiv.setAttribute('id','waitMachineID');
    newdiv.style.position = "absolute";
    
    if (strMessage == undefined)
        { newdiv.innerHTML = "<img src='../images/wait.gif' alt='" + ___AltWait___ + "' />"; }
    else
        { newdiv.innerHTML = strMessage + "&nbsp;&nbsp;<img src='../images/wait.gif' alt='" + ___AltWait___ + "' />"; }

    newdiv.style.position = "fixed";
    newdiv.style.top = "50%";
    newdiv.style.left = "50%";
    newdiv.style.marginLeft = "-200px";
    newdiv.style.marginTop = "-150px";        
    newdiv.style.width = "300px";
    newdiv.style.height = "auto";			
    newdiv.style.backgroundColor = "#F7F7F7";			
    newdiv.style.padding = "50px";
    newdiv.style.textAlign = "center";
    newdiv.style.border = "1px dotted #0099CC";	    
    newdiv.style.fontWeight = "bold";
    document.body.appendChild(newdiv);
    showModalBeforeElementId('waitMachineID', '0', true);
}

/* fonction permettant d'effectuer une command asyncrone selon le wait timeout */
function waitMachineDelayCommand(strcommand) {
    
    if (WaitMachineProcessList.length == 0)
    { setTimeout(strcommand, 1); }
    else
    { setTimeout("waitMachineDelayCommand('" + strcommand + "');", 500); }
    

}

//---Fin WaitMachine-------------------------------------------------------------

// Remove d'un item d'un array
// prise du web http://ejohn.org/blog/javascript-array-remove/
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
    var rest = this.slice((to || from) + 1 || this.length);
    this.length = from < 0 ? this.length + from : from;
    return this.push.apply(this, rest);
};


/* Function permettant d'aller à la page d'acceuil quand la session est fermé et 
   que l'utilisateur essaie d'accéder le WEB service
   @param {object} result - Reponse du serveur lors d'un appel Web-service */
//function __ws_OnError(result) {
//    gotoURL('/login.aspx'); 
//}


/* Permet d'effectuer une recherche des éléments par nom de classe 
   @param {string} cl = nom de la classe dans l'atribut class */
document.getElementsByClassName = function(cl) 
{
  var retnode = [];
  var myclass = new RegExp('\\b'+cl+'\\b');
  var elem = this.getElementsByTagName('*');
  for (var i = 0; i < elem.length; i++) 
  {
    var classes = elem[i].className;
    if (myclass.test(classes)) retnode.push(elem[i]);
  }
  return retnode;
}; 

//Fonction qui efface le contenu du Search textfield quand celui-ci contient la valeur par défaut 
function txtSearchOnFocus(html_element, strDefault) 
{
    if (html_element.value == strDefault)
    {
       html_element.value = ""; 
       html_element.style.color = "black";
    }        
}

// Function permettant de reinsérer le 'Search ?? by ??' dans le text field si celui-ci est vide
function setTextFieldValue(html_element, strText)
{
    if (html_element.value == "")
    {   
        html_element.style.color = "#aaa";
        html_element.value = strText;
    }
}


/* Function permettant d'obtenir le size d'une fenêtre d'un navigateur 
   Fonctionne dans IE 7-8, FF3.0, Chrome 1.0. */
function getVisibleWindowSize() 
{
  var myWidth = 0, myHeight = 0;
  if (typeof(window.innerWidth) == 'number') 
  {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } 
  else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) 
  {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } 
  else if (document.body && (document.body.clientWidth || document.body.clientHeight)) 
  {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  
  return {w:myWidth, h:myHeight};
}

/* Recursive qui active ou desactive les crontroles d'un element 
@Param {object} el - Element pricinal
{bool} blnDisabled - statut a appliquer
*/
function toggleDisabled(el, blnDisabled) {
    try {
        el.disabled = blnDisabled;
    }
    catch (E) {
    }

    if (el.childNodes && el.childNodes.length > 0) {
        for (var x = 0; x < el.childNodes.length; x++) {
            toggleDisabled(el.childNodes[x], blnDisabled);
        }
    }
}

