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

/**
* @fileOverview
  global_func.js [JScript File]: <br>
  Scripts de fonctions globales utilisées dans le site <br>
  @author: Chanh T.Do [www.thoransoft.com - 2008.05.14] <br>
  Version 1.0 */

/** 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)
  {
    panel.style.display = 'block';
    
    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;
}


/** Fonction pour afficher une image dans un light window */
function showLightWindow()
{
  var l = document.getElementById(arguments[0]);
  if (l != undefined) {l.style.display = 'block'}
  
  //Boucler dans le vecteurs des arguments pour cacher les autres élément
  for (var i=1; i<arguments.length; i++)
  {
    var other_l = document.getElementById(arguments[i]);
    if (other_l != undefined) { other_l.style.display = 'none'; }
  }
}


/** Fonction pour reset des containers parce que leur largeur sont à 100% 
    @param {string} divID - ID du DIV (h_container_id et b_container_id) */
function resetContainer(divID)
{
  var e = document.getElementById(divID);
  if (e != undefined) 
  { 
    if (e.style.width != screen.availWidth + 'px') { e.style.width = screen.availWidth + 'px'; }
  }
}


/** Fonction retournant la position du scroll (Top et Left) 
    return @Array [x,y] */
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 setter la couleur du lien lorsque l'on clique dessus
    @param {string} lnkID - ID du lien à modifier les couleurs
    @param {array} vLinkID - Vecteur contenant les ID de tous les menus */
function setLink(lnkID, vLinkID)
{
  var mnuOver;
  //Reset de tous les menus 
  for (var i=0; i<vLinkID.length; i++)
  {
    mnuOver = document.getElementById(vLinkID[i]);
    if (mnuOver != undefined) { mnuOver.style.color = ''; }
  }
  
  //Définir le lien envoyé en paramètre
  mnuOver = document.getElementById(lnkID);
  if (mnuOver != undefined) { mnuOver.style.color = '#ec2127'; }
}


/** Fonction permettant de valider le formulaire de message
    @param {string} name_id - ID du champ nom
    @param {string} email_id - ID du champ email
    @param {string} msg_id - ID du champ message */
function validateMsg(name_id, email_id, msg_id)
{
  var msgName, msgEmail, msgComments;
  if (window.document.URL.indexOf("fr") == -1)
  {
    msgName = "The field name is required.\n";
    msgEmail = "The field email is invalid.\n"
    msgComments = "Please leave your comments or messages.";
  }
  else
  {
    msgName = "Le champ nom est requis.\n";
    msgEmail = "Le champ courriel est invalide.\n";
    msgComments = "Veuillez inscrire votre message SVP.";
  }

  var msg = "";
  msg = (document.getElementById(name_id).value.length > 0) ? "" : msgName;
  msg += (document.getElementById(email_id).value.length > 0 && echeck(document.getElementById(email_id).value)) ? "" : msgEmail;
  msg += (document.getElementById(msg_id).value.length > 0) ? "" : msgComments;

  if (msg != "") { alert(msg); }
  else
  {
    var params = "f_name=" + document.getElementById(name_id).value + 
                 "&f_email=" + document.getElementById(email_id).value + 
                 "&f_comments=" + document.getElementById(msg_id).value ;
                 
    requestSrv_POST('includes/thoransm.php', r_thoransm, 'TEXT', params);
  }
}
