/*-------------------------------*/
/* GenFonction.js        */
/* Librairie de fonction commune */
/*                 */
/* CheckProperties(obj,prop)    */
/* SearchString( text, stringToFind) */
/* showProperties(obj,lstprop)     */
/* IsNumeric(VarStr)       */
/*                 */
/* Historique des maintenances   */
/*-------------------------------*/
/* 2007-08-23 Creation RayBert   */
/* 2007-12-05 Ajout de ObjArrNbr RayBert */
/* 2008-02-13 Ajout de la fonction ConcatWB */
/*-------------------------------*/

//----------------------------------------------------
// Si la propriété [prop] existe dans l'objet [obj]
// retour la valeur de la prorpiété autrement retourne
// un chaine vide.
//----------------------------------------------------
function CheckProperties(obj,prop) {    
  if (typeof(obj[prop]) == "undefined") { 
    return "";            
  } 
  else {
    return obj[prop];
  }
}

//----------------------------------------------------
// Retourne true si stringToFind est retouve dans text
//----------------------------------------------------
function SearchString( text, stringToFind) {
  var text = text.toString() ;
  var maReg = new RegExp( stringToFind, "gi") ;
  if ( text.search( maReg ) == -1 )
    return 0;
  else
    return 1;
}

//-----------------------------------------
// Affiche propriété et valeur d'un objet
//
// obj    : objet à parser
//
// lstprop  : Liste des properties à dumper
//            si vide dump l'emsemble des 
//        properties.
//
//            ex. 'banane,pomme,orange,'
//-----------------------------------------
function showProperties(obj,lstprop) {
  var props = [];
  for (var prop in obj) {
    if (SearchString(lstprop,prop) || lstprop == '')
    {       
      if (prop == 'm' || prop == 't' || prop == 'n' || prop == 'co' || prop == 'u' || prop == 'un' || prop == 'oun' )
      {
        
      } else {
        props.push('<br>'+prop+' : '+obj[prop]);
      }
    }
  }
  document.write(props.join(', '));
}

//------------------------------------
// Retourne true si [VarStr] est numeric
//------------------------------------
function IsNumeric(VarStr) {
  var RegExp = /^(\d*)$/;
  var result = VarStr.match(RegExp);
  return result;
}


//---------------------------------------
// Debug dumper objet
//
// ex.
//
// if(typeof debugDumpVar == 'function') {
//  debugDumpVar(s,'testdumpvar=on','OMNITURE',',pageName,account,channel,server,
//  srcsite,linkInternalFilters,pageType,prop1,prop2,prop3,prop4,prop5,prop6,prop7
//  ,prop22,prop23');
// }
//---------------------------------------
function debugDumpVar (obj,hidecode,lbl,filter) { 
  var url_racine_param = document.location.href.toLowerCase().split("?");
  if (url_racine_param.length > 1)
    {
      var list_param=url_racine_param[1].split('&')

      for (i = 0; i <= list_param.length; i++) {
        
        var par = list_param[i];

        if (par == hidecode)
        {
        //---------------------------------------------------
        // Affiche les propriétées de l'objet 
        //---------------------------------------------------         
        document.write('<hr><strong>Dump variable -> '+lbl+' ['+filter+']:</strong><br>');
        showProperties(obj,filter);
        document.write('<br><br>Fin...');
        }
      }
    } 
}


//----------------------------------------------
// Recupération d'un parametre d'un query string
//
// ex. http://localhost/GetParams/test.html?code=zzz
//     alert(qs('code')); -> zzz
//----------------------------------------------
function qs(TheKey) {
 var query = window.location.search.substring(1);
 var parms = query.split('&');
 for (var i=0; i<parms.length; i++) {
  var pos = parms[i].indexOf('=');
  if (pos > 0) {
  var key = parms[i].substring(0,pos);
  var val = parms[i].substring(pos+1);
  if (TheKey == key) {
    return val;
  }   
  }
 }
}

//---------------------------------------------
//- Function : ObjArrNbr(n,m,q,t)
//
//- Description : Permet de générer un array de nombre
// d'une certaine dimension et dans un certain ordre
//
//- paramêtres :  n -> Numérique, début de la séquence
//-         m -> Numérique, fin de la séquence
//-         q -> Numérique, Quantité de nombre désiré
//-         t -> String, type de tri désiré 
//-            a : Ascendant
//-            d : Descendant
//-            r : Random
//- Ex. MyArr = new ObjArray(1,10,5,'r');
//    MyArr.BuildArr();
//- 
//---------------------------------------------
function ObjArrNbr(n,m,q,t) {
  this.GlbArray = new  Array();
  this.n = n; //Séquence de départ
  this.m = m; //Séquence de fin
  this.q = q; //Quantité de nombre désiré (not used)
  this.t = t; //Type de tri désiré (a,d,r)

  //- Tri l'objet GlbArray random
  this.SortRand = function(a, b) {
    return (Math.round(Math.random()) - 0.5); 
  }

  //- Tri l'objet GlbArray Ascendant
  this.SortAsc = function (a, b){ 
    return (a-b); 
  }

  //- Tri l'objet GlbArray Descendant
  this.SortDesc = function (a, b){ 
    return (b-a); 
  }
  
  //- Popule l'objet GlbArray
  this.BuildArr = function() {
    for (x=this.n; x<=this.m; x++) {
      this.GlbArray.push(x);
    }

    switch (this.t)
    {
      case 'a':
       this.GlbArray.sort(this.SortAsc);
       break;
      case 'd':
       this.GlbArray.sort(this.SortDesc);
      break;
      case 'r':
       this.GlbArray.sort(this.SortRand);
      break;
      default:
       break;
    }
  } 
} 

//---------------------------------------------
//- Function : ConcatWB(ConcatString, delimit)
//
//- Description : Concatene avec un delimiteur 
//- 
//---------------------------------------------
function ConcatWB (ConcatString, delimit) {
    if (!ConcatString == '')
    {
      return delimit + ConcatString;
    } else {
      return '';
    } 
}
