//          ________
//          |/\/\/\|
//          | o o  |
//-----oOOO---(_---OOOo---------------------------------------------------------------------------------------
//
// Auteur      : Chanh T.Do [Thoransoft - 2008.05.14]
// Description : fichier contenant les fonctions globales du site www.comtechllc.us
// JScript File
//------------------------------------------------------------------------------------------------------------

/**
* @fileOverview
  global_func.js [JScript File]: <br>
  Scripts de fonctions globales utilisÃ©es dans le site de comtechllc <br>
  @author: Chanh T.Do [www.thoransoft.com - 2008.05.14] <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 (SRC)
    @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
  }
}


/** 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 d'afficher et de cacher les tabs.<br>
    <b>Note</b>: le premier Ã©lÃ©ment du array est le paneau que l'on veut afficher. Les autres sont cachÃ©s par dÃ©faut.
    On ajoute le prÃ©fix 'p_' devant chaque argument. On passe le ID du tab Ã  afficher et la fonction se charge d'inscrire
    le ID du panel Ã  cacher ou Ã  afficher (p_IdDuPanel)
    @param {array} arguments - Vecteur contenant les ID des paneau qu'on veut afficher ou cacher */
function swapPanel()
{
  //Afficher le premier paneau (arguments[0])
  var panel = document.getElementById('p_' + arguments[0]);
  if (panel != undefined)
  {
    //changeOpac(0, 'o_' + arguments[0]);        //Mettre l'opacité à 0 avant d'afficher en block l'élément [requiert le script blendtrans.js]
    panel.style.display = 'block';             //Affichage du panel en block avant d'effectuer l'animation d'opacité.
    //opacity('o_' + arguments[0],0,100,1500);   //Afficher lentement avec opacité l'onglet [requiert le script blendtrans.js]
    
    var tab = document.getElementById(arguments[0]);        //ID du tab
    if (tab != undefined) { tab.className = 'current'; }    //Tab courrant
    
    //Boucler dans le vecteurs des arguments pour cacher les autres panels
    for (var i=1; i<arguments.length; i++)
    {
      var other_panel = document.getElementById('p_' + arguments[i]);
      if (other_panel != undefined) 
      {
        other_panel.style.display = 'none'; 
        var other_tab = document.getElementById(arguments[i]);
        if (other_tab != undefined) {other_tab.className = ''; }
      }
    }   //Fin boucle for i
  }     //Fin if panel
}       //Fin fonction


/** 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)
    @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 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...).
    @param {int} length - Longueur de la chaÃ®ne de caractÃ¨re Ã  gÃ©nÃ©rer
    @param {string} extraChars - CaractÃ¨res supplÃ©mentaires Ã  considÃ©rer dans la gÃ©nÃ©ration
    @param {bool} firstNumber - Mettre Ã  true si on veut obtenir les premiers caractÃ¨res en chiffres dans la gÃ©nÃ©ration (0123456789)
    @param {bool} firstLower - Mettre Ã  true si on veut obtenir les premiers caractÃ¨res en minuscules (abcdefghijklmnopqrstuvwxyz)
    @param {bool} firstUpper - Mettre Ã  true si on veut obtenir les premiers caractÃ¨res en majuscule (ABCDEFGHIJKLMNOPQRSTUVWXYZ)
    @param {bool} firstOther - Mettre Ã  true si on veut obtenir les premiers caractÃ¨res en caractÃ¨res spÃ©ciaux (`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/? )
    @param {bool} latterNumber - Mettre Ã  true si on veut obtenir des chiffres dans la gÃ©nÃ©ration (0123456789)
    @param {bool} latterLower - Mettre Ã  true si on veut obtenir des caractÃ¨res minuscules (abcdefghijklmnopqrstuvwxyz)
    @param {bool} latterUpper - Mettre Ã  true si on veut obtenir des caractÃ¨res majuscule (ABCDEFGHIJKLMNOPQRSTUVWXYZ)
    @param {bool} latterOther - Mettre Ã  true si on veut obtenir les caractÃ¨res spÃ©ciaux (`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/? )
    @return ChaÃ®ne de caractÃ¨re
    @type string */
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
    @param {string} strHTML - ChaÃ®ne de caractÃ¨re HTML
    @return ChaÃ®ne de caractÃ¨re sans les cariages return (CRLF)
    @type string */
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 valider une adresse e-mail
    @param {string} str - Contient l'adresse e-mail
    @return true si l'adresse est valide
    @type bool */
function echeck(str) 
{
  var at="@"
  var dot="."
  var lat=str.indexOf(at)
  var lstr=str.length
  var ldot=str.indexOf(dot)
  
  if (str.indexOf(at)==-1) { return false; }
  if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) { return false; }
  if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) { return false; }
  if (str.indexOf(at,(lat+1))!=-1) { return false; }
  if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) { return false; }
  if (str.indexOf(dot,(lat+2))==-1) { return false; }
  if (str.indexOf(" ")!=-1) { return false; }
  
  return true;
}


/** 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;
}

