
//==========================================================================
function isDigit(ch) {
   if (ch >= '0' && ch <= '9' || ch=='.')
      return true;
   return false;
}

//fonction qui teste les valeurs d'un formulaire devant etre numérique
function checkNumerique(form,champ) {
 var obj = eval('document.'+form+'.'+champ);
 value = obj.value;
 if (value=="") return true;
 for (n = 0; n < value.length; n++)
   if (! isDigit(value.charAt(n))) {
    alert("Le champ "+obj.name+" doit être numérique!");
    obj.value="";
    obj.focus();
    return false;
   }
 return true;
}

//==========================================================================
function validate(obj) {
   value = obj.value;
   if (value=="") return true;
   for (n = 0; n < value.length; n++)
      if ( ! isDigit(value.charAt(n))) {
         obj.value="";
         return false;
      }
        return true;
}


//==========================================================================
function startChargement() {
 debut = new Date;
 debut = debut.getTime();
}

//==========================================================================
function tempschargement()
{
   fin = new Date;
   fin = fin.getTime();
   secondes = (fin-debut)/1000;
   if (secondes>1)
   {
    window.status='Page chargée en ' + secondes + ' secondes.';
   }
   else
   {
    window.status='Page chargée en ' + secondes + ' seconde.';
   }

}

//==========================================================================
 /** Lance la fenêtre de selection des colonnes d'une table
     entite : nom de la classe (exemple : 'societe')
     defaut : colonnes par défaut, séparés par ';' (exemple : 'nom;annuaire;email;ville')
     configuration : numéro de config (en général 0)*/
   function selectColonnes(entite,defaut,configuration) {
          url = "../divers/selectionColonnes.php4?entite=" + entite + "&defaut=" + defaut + "&configuration=" + configuration;
          param = "width=700,height=500,resizable=yes,scrollbars=yes";
          window.open(url,"",param);
    }


   function selectColonnesRetrait(entite,defaut,configuration,httpIntranetRoot) {
          var url;
          url = httpIntranetRoot + "/divers/selectionColonnes.php4?entite=" + entite + "&defaut=" + defaut + "&configuration=" + configuration;
          param = "width=700,height=500,resizable=yes,scrollbars=yes";
          window.open(url,"",param);
    }


    function showSousMenu (sousMenu) {
        NS4 = (document.layers) ? 1 : 0;
        IE4 = (document.all) ? 1 : 0;
        W3C = (document.getElementById) ? 1 : 0;

        if (W3C) {
           sousMenu.style.visibility = "visible";
        } else if (NS4) {
          sousMenu.visibility = "show";
        } else {
          sousMenu.style.visibility = "visible";
        }
    }


    function hideSousMenu (sousMenu) {
        NS4 = (document.layers) ? 1 : 0;
        IE4 = (document.all) ? 1 : 0;
        W3C = (document.getElementById) ? 1 : 0;

        if (W3C) {
           sousMenu.style.visibility = "hidden";
        } else if (NS4) {
          sousMenu.visibility = "hide";
        } else {
          sousMenu.style.visibility = "hidden";
        }
    }


    /** Modifie le status des cases à cocher d'un formulaire
        type : all => coche tout
               none => décoche tout
               inverse => inverse l'état*/
    function modifierCheckStatus(formul,type) {
        if (type=="all") for (i = 0; i < formul.length; i++) formul[i].checked = true ;
        else if (type=="none") for (i = 0; i < formul.length; i++) formul[i].checked = false ;
        else if (type=="inverse") for (i = 0; i < formul.length; i++) {
             if (formul[i].checked) formul[i].checked = false ;
             else formul[i].checked = true ;
        }
    }


    /** Valide un formulaire, en modifiant la valeur 'action_form'*/
    function validerFormulaire(formul,action_form_value) {
        valider = true;
        if (action_form_value=='supprimer') {
           valider = confirm('Etes-vous sûr de vouloir supprimer ?');
        }
        valider = ((valider) && (action_form_value!=""));
        if (valider) {
            formul.action_form.value=action_form_value;
            formul.submit();
        }
    }


   function newFenetreSize(url,w,h) {
          param = "width=" + w +",height=" + h +",resizable=yes,scrollbars=yes";
          window.open(url,"",param);
    }

    function newFenetreFull(url) {
          param = "width=800,height=600,resizable=yes,scrollbars=yes";
          //param = "fullsize=yes,resizable=yes,scrollbars=yes";
          window.open(url,"",param);
    }

function gettext(str) {
    var s;
    if ((typeof(LocalizedStrings)=="object") && (s=LocalizedStrings[str])) return s;
    return str;
}

_ouvrirPopup = function (url,w,h,wname,scroll,msg,params)
{
  var param = '' ;
  if( ( typeof(msg) == 'undefined' ) || ( msg == '' ) ) {
    msg = gettext("Les fenêtres ne peuvent pas s'ouvrir. Il est possible que vous ayez activé la fonction de blocage des fenêtres surgissantes ?") ;
  }

  // if( ( typeof(w) == 'undefined' ) || ( typeof(w) == 'undefined' ) param+= 'fullsize=yes' ;
  if ( typeof(scroll) == 'undefined' ) { scroll = true ; }
  if ( typeof(w) == 'undefined' ) { w = screen.availWidth / 2 ; }
  if ( typeof(h) == 'undefined' ) { h = screen.availHeight / 2 ; }
  var width = w > screen.availWidth ? screen.availWidth - 50 : w;
  var height = h > screen.availHeight ? screen.availHeight - 50 : h;
  if( typeof(params) == 'undefined' ) var params = { left: ((screen.availWidth - width) / 2), top: ((screen.availHeight - height) / 2) };
  var left = ( typeof(params.left) == 'undefined' ) ? (screen.availWidth - width) / 2 : params.left ;
  var top = ( typeof(params.top) == 'undefined' ) ? (screen.availHeight - height) / 2 : params.top ;
  var now = new Date();

  if ( typeof(wname) == 'undefined' ) { wname = "popup_window_" + now.getTime() ; }

  param+="width="+width+",height="+height+",left="+left+",top="+top+","+(scroll?"scrollbars=yes":"scrollbars=no")+",resizable=yes";

  wname = window.open(url,wname,param);

  if( ! wname ) {
    alert( msg ) ;
  } else {
    if( ! eval("wname.opener") ) {
      wname.opener = self ;
    }
  }
  return wname ;
}
_newFenetreSize = function (url,w,h,msg) { return _ouvrirPopup( url, w, h, '', true, msg ) ; } ;
_newFenetreScreenSize = function (url,msg) { return _ouvrirPopup( url, null, null, '', true, msg ) ; } ;
_newFenetreFull = function (url,msg) { return _ouvrirPopup( url, (screen.availWidth-130), (screen.availHeight-100), '', true, msg ) ; } ;

   //Drop down menu by http://www.dynamicdrive.com

var zindex=100
var ns4=document.layers
var ns6=document.getElementById&&!document.all
var ie4=document.all
var opr=navigator.userAgent.indexOf("Opera")

function dropit(e,whichone){
curmenuID=ns6? document.getElementById(whichone).id : eval(whichone).id
if (window.themenu&&themenu.id!=curmenuID)
themenuStyle.visibility=ns4?"hide" : "hidden"

themenu=ns6? document.getElementById(whichone): eval(whichone)
themenuStyle=(ns6||ie4)? themenu.style : themenu

themenuoffsetX=(ie4&&opr==-1)? document.body.scrollLeft : 0
themenuoffsetY=(ie4&&opr==-1)? document.body.scrollTop : 0

themenuStyle.left=ns6||ns4? e.pageX-e.layerX : themenuoffsetX+event.clientX-event.offsetX
themenuStyle.top=ns6||ns4? e.pageY-e.layerY+19 : themenuoffsetY+event.clientY-event.offsetY+18

hiddenconst=(ns6||ie4)? "hidden" : "hide"
if (themenuStyle.visibility==hiddenconst){
themenuStyle.visibility=(ns6||ie4)? "visible" : "show"
themenuStyle.zIndex=zindex++
}
else
hidemenu()
return false
}

function hidemenu(){
if ((ie4||ns6)&&window.themenu)
themenuStyle.visibility="hidden"
else if (ns4)
themenu.visibility="hide"
}

if (ie4||ns6)
document.onclick=hidemenu




//function for returning how many days there are in a month including leap years
function DaysInMonth(WhichMonth, WhichYear)
{
  var DaysInMonth = 31;
  if (WhichMonth == "04" || WhichMonth == "06" || WhichMonth == "09" || WhichMonth == "11") DaysInMonth = 30;
  if (WhichMonth == "02" && (WhichYear/4) != Math.floor(WhichYear/4))        DaysInMonth = 28;
  if (WhichMonth == "02" && (WhichYear/4) == Math.floor(WhichYear/4))        DaysInMonth = 29;
  return DaysInMonth;
}



//function to change the available days in a months
function ChangeOptionDays(nomForm, Which)
{
  DaysObject = eval("document." + nomForm + "." + Which + "Jour");
  MonthObject = eval("document." + nomForm + "." + Which + "Mois");
  YearObject = eval("document." + nomForm + "." + Which + "Annee");

  Month = MonthObject[MonthObject.selectedIndex].value;
  Year = YearObject[YearObject.selectedIndex].value;

  DaysForThisSelection = DaysInMonth(Month, Year);
  CurrentDaysInSelection = DaysObject.length;
  if (CurrentDaysInSelection > DaysForThisSelection)
  {
    for (i=0; i<(CurrentDaysInSelection-DaysForThisSelection); i++)
    {
      DaysObject.options[DaysObject.options.length - 1] = null
    }
  }
  if (DaysForThisSelection > CurrentDaysInSelection)
  {
    for (i=0; i<(DaysForThisSelection-CurrentDaysInSelection); i++)
    {
      NewOption = new Option(DaysObject.options.length + 1);
      DaysObject.add(NewOption);
    }
  }
    if (DaysObject.selectedIndex < 0) DaysObject.selectedIndex == 0;
}


   function testerHeure(nomForm,nomChamp) {
      champHeure = document.forms[nomForm].elements[nomChamp + 'Heure'];
      champMinute = document.forms[nomForm].elements[nomChamp + 'Minute'];
      nbHeure = champHeure.value;
      nbMin = champMinute.value;
      if (isNaN(nbHeure)) {
          alert('Heure non valide...');
          champHeure.focus();
          return false;
      }
      if (isNaN(nbMin)) {
          alert('Minutes non valide...');
          champMinute.focus();
          return false;
      }

      intHeure = parseInt(nbHeure,10);
      intMinute =  parseInt(nbMin,10);
      if ((intHeure<0) || (intHeure>24)) {
          alert('Heure non valide...');
          champHeure.focus();
          return false;
      }

      if ((intMinute<0) || (intMinute>59)) {
          alert('Minutes non valide...');
          champMinute.focus();
          return false;
      }

      if (intHeure<10) champHeure.value = '0' + intHeure;
      if (intMinute<10) champMinute.value = '0' + intMinute;

      return true;
   }


   function selectDate(form,prefix,httpIntranetRoot) {
    url = "/divers/selectionDate.php4?form=" + form + '&prefix=' + prefix;
    if (httpIntranetRoot!=null) url = httpIntranetRoot + url;
    else url = ".." + url;
    //url = "../divers/selectionDate.php4?form=" + form + '&prefix=' + prefix;
    param = "width=340,height=310,resizable=yes,scrollbars=yes";
    window.open(url,"",param);
    }

//==========================================================================
function format(valeur,decimal,separateur) {
  // formate un chiffre avec 'decimal' chiffres après la virgule et un separateur
  var deci = Math.round( Math.pow(10, decimal) * (Math.abs(valeur) - Math.floor(Math.abs(valeur)))) ;
  var val = Math.floor(Math.abs(valeur));
  if ( (decimal == 0) || (deci == Math.pow(10, decimal)) ) {
    val = Math.floor(Math.abs(valeur));
    deci = 0;
  }
  var val_format = val + "";
  var nb = val_format.length;
  for (var i = 1 ; i < 4 ; i++) {
    if (val >= Math.pow(10, (3 * i))) {
      val_format = val_format.substring(0, nb - (3 * i)) + separateur + val_format.substring(nb - (3 * i));
    }
  }
  if (decimal > 0) {
    var decim = "";
    for (var j = 0 ; j < (decimal - deci.toString().length) ; j++) {
      decim+= "0";
    }
    deci = decim + deci.toString();
    val_format = val_format + "." + deci;
  }
  if (parseFloat(valeur) < 0) {
    val_format = "-" + val_format;
  }
  return val_format;
}

// args : textarea moncontroletexte, int nbcar, element_HTML moncontroledecompte
// return : aucun
// Affecte à certains évènements d'un textarea, le contrôle de la longueur de son contenu
function LimiterTextArea(moncontroletexte, nbcar) {
  var theform=document.getElementById(moncontroletexte);
  var limite_text='<div id="div_'+moncontroletexte+'" name="div_'+moncontroletexte+'" class="decompte"></div>';
  document.write(limite_text);
  var div = "div_"+moncontroletexte
  var thediv=document.getElementById(div);
  theform.onclick = function(){TextAreaEstRempli(theform, nbcar, thediv)};
  theform.onblur = function(){TextAreaEstRempli(theform, nbcar, thediv)};
  theform.onkeyup = function(){TextAreaEstRempli(theform, nbcar, thediv)};
  theform.onkeypress = function(){TextAreaEstRempli(theform, nbcar, thediv)};
  // Affichage du nombre de caractères restant
  thediv.innerHTML = NbCarRestant(theform, nbcar);
}
// TextAreaEstRempli
// args : textarea moncontroletexte, int nbcar, element_HTML moncontroledecompte
// return : bool
// Renvoie vrai si le nombre de caractères maximum du textarea n'est pas atteint
function TextAreaEstRempli(moncontroletexte, nbcar, moncontroledecompte) {
  if (moncontroletexte)
  {
      if (moncontroletexte.value.length <= nbcar)
      {
          // Affichage du nombre de caractères restant
          moncontroledecompte.innerHTML = NbCarRestant(moncontroletexte, nbcar);
          return true;
      }
      else
      {
          // Affichage du nombre de caractères restant
          moncontroletexte.value = moncontroletexte.value.substr(0, nbcar);
          moncontroledecompte.innerHTML = NbCarRestant(moncontroletexte, nbcar);
          return false;
      }
  }
}
// NbCarRestant
// args : textarea moncontroletexte, int nbcar
// return : int
// Renvoie le nombre de caractère à saisir
function NbCarRestant(moncontroletexte, nbcar) {
  var nb = new Number(nbcar - moncontroletexte.value.length);
  var str = nb + " caractères restant"
  return str
}


