// -------------------------------------------------------------------
// Bibliotheques de fonctions JAVASCRIPT
//
// Auteur : UNG Seng Por  Rajouter par daniel le 6 decembre 2007 dans extranetv3 common
// 
// Modification:
// ------------
//   Action                         Date               Auteur
// Création                      08/06/2000            UNG SP
//
// Liste de fonctions:
// -------------------
// trimString (str)           Enleve les espaces a droite et a gauche d'une chaine
// EstEntierPositif(nombre)   Test si c'est un nombre entier positif
// EstNombrePositif(nombre)   Test si c'est un nombre positif
// ModifDate(formulaire, annee, mois, jour)   modif le nombre de jour max suivant la date
// -------------------------------------------------------------------

// -------------------------------------------------------------------
// Ce script sert a détecté la version du navigateur
// si ie45 --> version IE >= 4
//    ns4  --> version NS < 5
//    ns6  --> version NS >= 5
// le DOM est géré par ie45 et ns6
var ie45,ns6,ns4,dom;
if (navigator.appName=="Microsoft Internet Explorer")
  ie45=parseInt(navigator.appVersion)>=4;
else if (navigator.appName=="Netscape"){
  ns6=parseInt(navigator.appVersion)>=5;
  ns4=parseInt(navigator.appVersion)<5;}
dom=ie45 || ns6;
// -------------------------------------------------------------------


function showhide(id) {
el = document.all ? document.all[id] : dom ? document.getElementById(id) : document.layers[id];
els = dom ? el.style : el;
if (dom){
  if (els.visibility == "hidden")
    els.visibility = "visible";
  else els.visibility = "hidden";
  }
else if (ns4){
  if (els.visibility == "show")
    els.visibility = "hide";
  else els.visibility = "show";
  }
}

function show(id) {
el = document.all ? document.all[id] : dom ? document.getElementById(id) : document.layers[id];
els = dom ? el.style : el;
if (dom){
  els.visibility = "visible";
  }
else if (ns4){
  els.visibility = "show";
  }
}

function hide(id) {
el = document.all ? document.all[id] : dom ? document.getElementById(id) : document.layers[id];
els = dom ? el.style : el;
if (dom){
  els.visibility = "hidden";
  }
else if (ns4){
  els.visibility = "hide";
  }
}

function getCellValue (cellOrId) {
  var cell = typeof cellOrId == 'string' ? 
      (document.all ? document.all[cellOrId] : document.getElementById(cellOrId)) : cellOrId;

  if (document.all)
    return cell.innerText;
  else {
    cell.normalize();
    if (cell.firstChild.nodeType == 3)
      return cell.firstChild.nodeValue;
    else 
      return '';
  }
}

function setCell (cellOrId, value) {
  var cell = typeof cellOrId == 'string' ? 
      (document.all ? document.all[cellOrId] : document.getElementById(cellOrId)) : cellOrId;

  if (document.all)
    cell.innerText = value;
  else if (document.getElementById)
    cell.replaceChild(document.createTextNode(value), cell.firstChild);
}


function getAnchorValue (indice) {
  return document.anchors[name].innerText;
}


function setAnchor (indice, value) {
  document.anchors[indice].innerText = value;
}

function openCentered (url, width, height, windowName, featureString) {

  if (!windowName)      windowName = '';
  //if (!featureString)   featureString = '';
  if (!featureString)   featureString = ',location=no,toolbar=no,directories=no,menubar=no,resizable=no,scrollbars=no,status=no';
  else                  featureString = ',' + featureString;
  var x = Math.round((screen.availWidth - width) / 2);
  var y = Math.round((screen.availHeight - height) / 2);
  featureString = 'left=' + x + ',top=' + y + ',width=' + width + ',height=' + height + featureString;
 return open (url, windowName, featureString);
}

function openCenteredModal (url, width, height, windowName, featureString) {
  if (!windowName)
    windowName = '';
  if (!featureString)
    featureString = '';
  else
    featureString = ',' + featureString;
  featureString = 'dialogWidth:' + width + 'px; dialogHeight:' + height + 'px; center:1;' + featureString;
  return showModalDialog (url, windowName, featureString);
}


function ConverNombre(nombre)
{
 if (nombre == "") return '';
 symbol_decimal = ',';
 reg = new RegExp(symbol_decimal, 'g');
 nombre = nombre.replace (reg,'.');
 nombre = parseFloat(nombre); // retourne NaN si ce n'est pas un nombre
// if (isNaN(nombre)) return '';
 return nombre;
}

function getRadioValue (radioButtonOrGroup) {
  var value = null;
  if (radioButtonOrGroup.length) { // group 
    for (var b = 0; b < radioButtonOrGroup.length; b++)
      if (radioButtonOrGroup[b].checked)
        value = radioButtonOrGroup[b].value;
  }
  else if (radioButtonOrGroup.checked)
    value = radioButtonOrGroup.value;
  return value;
}

function trimString (str)
{
 str = this != window? this : str;
 return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function ValNum(chaine)
{
 symbol_decimal = ',';
 reg = new RegExp(symbol_decimal, 'g');
 chaine = chaine.replace (reg,'.');
 return chaine; 
}

function trimString (str)
{
 str = this != window? this : str;
 return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function EstVide (inputStr)
{
 if (inputStr == null || inputStr == "") {
 return true
 }
 return false
}

function EstEntierPositif(nombre) {
inputStr = nombre.toString()
for (var i = 0; i < inputStr.length; i++) {
var oneChar = inputStr.charAt(i)
if (oneChar < "0" || oneChar > "9") {
return false
}
}
return true
}


function EstNombrePositif (nombre)
{
 // Test s'il y a des blancs
 reg = new RegExp(' ', 'g');
 if (reg.test (nombre)) { return false;}

 symbol_decimal = ',';
 reg = new RegExp(symbol_decimal, 'g');
 nombre = nombre.replace (reg,'.');
 // Test si c'est un nombre
 if (isNaN(Number(nombre)))
 {
  return false;
 } else
 {
   // On elimine les nombres negatifs
   if (nombre < 0)
   {
    return false;
   } else
   {
    return true;
   }
  }
}

function EstDecimal(nombre,nbdecimal)
{
 oneDecimal = false
 inputStr = nombre.toString()
 countDecimal = 0
 for (var i = 0; i < inputStr.length; i++) {
  var oneChar = inputStr.charAt(i)
  if (i == 0 && oneChar == "-") {
  continue
  }
  if (oneDecimal) {
  countDecimal += 1
  if (countDecimal > nbdecimal) return false
  }
  if (oneChar == "." && !oneDecimal) {
  oneDecimal = true
  continue
  }
  if (oneChar < "0" || oneChar > "9") {
  return false
  }
 }
 return true
}

function ModifDate(NomForm, NomChampAnnee,NomChampMois, NomChampJour)
{
 var date = new Date(NomForm[NomChampAnnee].value, NomForm[NomChampMois].value,0) ;
 var NbJourMax   = date.getUTCDate() +  1;
 SelObject = NomForm[NomChampJour];
 SelObject.options.length = 0;
 for (i=1; i<=NbJourMax;i++)	
 {
//  ObjJour = new Option(i, i, false, false);
//  NomForm[NomChampJour].options[NomForm[NomChampJour].length] = ObjJour;
    SelObject.options.length++;
	SelObject.options[SelObject.length -1].text = i;
	SelObject.options[SelObject.length -1].value = i;
 }
}

function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function isDate (day,month,year) {
// checks if date passed is valid
// will accept dates in following format:
// isDate(dd,mm,ccyy), or
// isDate(dd,mm) - which defaults to the current year, or
// isDate(dd) - which defaults to the current month and year.
// Note, if passed the month must be between 1 and 12, and the
// year in ccyy format.

    var today = new Date();
    year = ((!year) ? y2k(today.getYear()):year);
    month = ((!month) ? today.getMonth():month-1);
    if (!day) return false
    var test = new Date(year,month,day);
    if ( (y2k(test.getYear()) == year) &&
         (month == test.getMonth()) &&
         (day == test.getDate()) )
        return true;
    else
        return false
}
