/**-----------------------------------------------------------------------------*
 * verobio                                                                      *
 * @Version   0.0.8                                                             *
 * @Date      01/05/10                                                          *
 * @Author    SUET Eric mailto:eric.suet@laposte.net                            *
 * @Since     PHP 5, MYSQL 4.0.15                                               *
 * @File      commun.js                                                         *
 * Fonctions javascript communes                                                *
 * ---------------------------------------------------------------------------- */

window.onload = function() { setFooter(); }   //Initialisation du pied de page
window.onresize = function() { setFooter(); } //Repositionner le pied de page a chaque changement de dimension

/* ------------------------------------------------------------------------------------------------
 * Gestion du positionnement du pied de page
 */

//Lecture de la hauteur de la fenetre
function getWindowHeight() {
  var windowHeight = 0;
  if (typeof(window.innerHeight) == 'number') //firefox 3
    windowHeight = window.innerHeight;
  else if (document.documentElement && document.documentElement.clientHeight) //internet explorer 6
    windowHeight = document.documentElement.clientHeight;
  else if (document.body && document.body.clientHeight)
    windowHeight = document.body.clientHeight;

  return windowHeight;
}

//Positionner le pied de page
function setFooter() {

  if (document.getElementById) {
    var Hwindow = getWindowHeight()-
                  document.getElementById('entete').clientHeight-
                  document.getElementById('piedDePage').clientHeight-20;
    var navigation = document.getElementById('navigation');
    var secondaire = document.getElementById('secondaire');
    var principal  = document.getElementById('principal');
    var centre     = document.getElementById('centre').clientHeight+30;
    var ie=getInternetExplorerVersion();

    //Pas ie
    if (ie<0) {

      //Texte plus grand que la fenetre -> Maxi = texte
      if (centre>Hwindow) Hwindow=centre+25;

      //Ce qu'il reste a augmenter
      if (Hwindow > navigation.clientHeight) navigation.style.height = (Hwindow-48) + 'px';
      if (Hwindow > secondaire.clientHeight) secondaire.style.height = (Hwindow-71) + 'px';
      if (Hwindow > principal.clientHeight) principal.style.height = (Hwindow-68) + 'px';

    //Internet explorer 8
    } else if (ie==8) {

      //Texte plus grand que la fenetre -> Maxi = texte
      if (centre>Hwindow) Hwindow=centre;

      //Ce qu'il reste a augmenter
      if (Hwindow > navigation.clientHeight) navigation.style.height = (Hwindow-51) + 'px';
      if (Hwindow > secondaire.clientHeight) secondaire.style.height = (Hwindow-74) + 'px';
      if (Hwindow > principal.clientHeight) principal.style.height = (Hwindow-71) + 'px';

    //Internet explorer 8
    } else  {

      //Texte plus grand que la fenetre -> Maxi = texte
      if (centre>Hwindow) Hwindow=centre;

      //Ce qu'il reste a augmenter
      if (Hwindow > navigation.clientHeight) navigation.style.height = (Hwindow-51) + 'px';
      if (Hwindow > secondaire.clientHeight) secondaire.style.height = (Hwindow-74) + 'px';
      if (Hwindow > principal.clientHeight) principal.style.height = (Hwindow-71) + 'px';
    }
  }
}

function getInternetExplorerVersion() {
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer') {
    var ua = navigator.userAgent;
    var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat(RegExp.$1);
  }
  return rv;
}

function key(event,ev1,ev2,Suiv) {
  isErr = true;                                                              //par defaut erreur
  var isNS4 = (navigator.appName=="Netscape")?true:false;                    //detection du navigateur
  var ev = (isNS4)?ev2:ev1;                                                  //code ascii de la touche
  if((ev >= 48 && ev <= 57 ) || ev ==46 || ev==44 || ev==8){                //nombre normal
    isErr = false;
  }

  document.retour = true;                                                    //par defaut pas d'erreur
  if (isErr) {        //pas d'affichage
    document.retour = false;
    if (isNS4)
      event.returnValue = false;
    else
      return false;
  }
}

//retourne un nombre ou null
function nombre(sText) {
  if (isNumeric(sText)) {
    var pos=sText.indexOf(',');
    if (pos>0)
      sText=sText.substr(0,pos)+'.'+sText.substr(pos+1,sText.length-pos-1);
    return sText*1;
  } else
    return 0;
}

function resultat() {
  var champs=window.document.quantitePanier.champs.value;
  var nbClients=window.document.quantitePanier.nbClients.value;
  var champs=champs.split(",");
  var nombres=new Array();
  var prix=new Array();
  for(champ in champs) {
    eval("nombres["+champs[champ]+"]=nombre(window.document.quantitePanier.p"+champs[champ]+".value)");
    eval("prix["+champs[champ]+"]=window.document.quantitePanier.c"+champs[champ]+".value");
    eval("window.document.quantitePanier.t"+champs[champ]+".value=Math.round((nombres["+champs[champ]+"]*nbClients)*100)/100");
  }
  var total=0;
  for(champ in champs)
    eval("total+=nombres["+champs[champ]+"]*prix["+champs[champ]+"]");
  window.document.quantitePanier.total.value=Math.round(total*100)/100;
}

//Nombre ?
function isNumeric(sText){
  var ValidChars = "0123456789.,";
  var IsNumber=true;
  var Char;
  for (i = 0; i < sText.length && IsNumber == true; i++) {
    Char = sText.charAt(i);
    if (ValidChars.indexOf(Char) == -1) IsNumber = false;
  }
  return IsNumber;
}
