
function IsInt( chaine )
{
  
	ref = "0123456789";
	result = true;
	for (var i = 0; i < chaine.length ; i++) 
	{
		$car = chaine.charAt(i);
		cart = false;
		for (var j = 0; j < ref.length ; j++) 
		{
			if ($car==ref.charAt(j)) 
			{
				cart = true;
			} 
		}
		if (cart==false) 
		{
			result = false;
		}
	}
	return result;
}

function FVerifImc() {

  if ( ( document.datas.taif.value.length == 0 ) && ( document.datas.pdsf.value.length==0 ) ) {
    return false;
  }

  if ( document.datas.taif.value.length < 3 ) {
    alert("La taille de la femme n'est pas correcte");
    return false;
  }

  if ( ! IsInt(document.datas.taif.value) ) {
    alert("La taille de la femme n'est pas un entier");
    return false;
  }

  if ( document.datas.pdsf.value.length < 2 ) {
    alert("Le poids de la femme n'est pas correct");
    return false;
  }
    
  if ( ! IsInt(document.datas.pdsf.value) ) {
    alert("Le poids de la femme n'est pas un entier");
    return false;
  }

  return true;

}

function HVerifImc() {

  if ( ( document.datas.taih.value.length == 0 ) && ( document.datas.pdsh.value.length==0 ) ) {
    return false;
  }

  if ( document.datas.taih.value.length < 3 ) {
    alert("La taille de l'homme n'est pas correcte");
    return false;
  }

  if ( ! IsInt(document.datas.taih.value) ) {
    alert("La taille de l'homme n'est pas un entier");
    return false;
  }

  if ( document.datas.pdsh.value.length < 2 ) {
    alert("Le poids de l'homme n'est pas correct");
    return false;
  }
    
  if ( ! IsInt(document.datas.pdsh.value) ) {
    alert("Le poids de l'homme n'est pas un entier");
    return false;
  }

  return true;

}

function FVerifMb() {
  if ( !FVerifImc() ) { return false; }

  if ( document.datas.agef.value.length == 0 )  {
    return false;
  }

  if ( document.datas.agef.value.length < 2 ) {
    alert("L'age de la femme n'est pas correct");
    return false;
  }

  if ( ! IsInt(document.datas.agef.value) ) {
    alert("L'age de la femme n'est pas un entier");
    return false;
  }

  return true;

}

function HVerifMb() {
  if ( !HVerifImc() ) { return false; }

  if ( document.datas.ageh.value.length == 0 )  {
    return false;
  }

  if ( document.datas.ageh.value.length < 2 ) {
    alert("L'age de l'homme n'est pas correct");
    return false;
  }

  if ( ! IsInt(document.datas.ageh.value) ) {
    alert("L'age de l'homme n'est pas un entier");
    return false;
  }

  return true;

}

function CalcImc() {
  var hdata;
  var fdata;
  hdata = false;
  fdata = false;

  fdata = FVerifImc();
  hdata = HVerifImc();

  document.datas.imcf.value = "";
  document.datas.imch.value = "";

  if ( fdata ) {
    var resultat;
    var correc;
    resultat = parseInt( document.datas.pdsf.value / Math.pow( ( document.datas.taif.value / 100 ) , 2 ) ) ;
    correc = 1 + ( ( 22 - resultat ) / 100 );
    correc = Math.round( correc * 100 ) / 100;
    if ( !isNaN( resultat ) ) {
      document.datas.imcf.value = resultat;
      document.datas.corimcf.value = correc;
    }
  }

  if ( hdata ) {
    var resultat;
    var correc;
    resultat = parseInt( document.datas.pdsh.value / Math.pow( ( document.datas.taih.value / 100 ) , 2 ) ) ;
    correc = 1 + ( ( 22 - resultat ) / 100 );
    correc = Math.round( correc * 100 ) / 100;
    if ( !isNaN( resultat ) ) {
      document.datas.imch.value = resultat;
      document.datas.corimch.value = correc;
    }
  }

  return true;
}

function CalcMb() {
  var hdata;
  var fdata;
  var calceffec;
  
  hdata = false;
  fdata = false;
  calceffec = false;

  fdata = FVerifMb();
  hdata = HVerifMb();

  //alert( fdata + " // " + hdata );
  
  document.datas.mbf.value = "";
  document.datas.mbh.value = "";

  if ( fdata ) {
    var resultat;
    var vcalc;
    vcalc = 0.963 * Math.pow( document.datas.pdsf.value , 0.48 ) *
            Math.pow( ( document.datas.taif.value / 100 ) , 0.5 ) *
            Math.pow( document.datas.agef.value , -0.13 );
    vcalc = Math.round( vcalc * 100 ) / 100;
    resultat = parseFloat( vcalc ) ;
    if ( !isNaN( resultat ) ) {
      document.datas.mbf.value = resultat;
      calceffec = true;
    }
  }

  if ( hdata ) {
    var resultat;
    var vcalc;
    vcalc = 1.083 * Math.pow( document.datas.pdsh.value , 0.48 ) *
            Math.pow( ( document.datas.taih.value / 100 ) , 0.5 ) *
            Math.pow( document.datas.ageh.value , -0.13 );
    vcalc = Math.round( vcalc * 100 ) / 100;
    resultat = parseFloat( vcalc ) ;
    if ( !isNaN( resultat ) ) {
      document.datas.mbh.value = vcalc;
      calceffec = true;
    }
  }

  return calceffec;

}

function CalcNap() {
  var resf;
  var resh;

  resf = document.datas.vnapf.value * document.datas.mbf.value * 239 * document.datas.corimcf.value;
  resh = document.datas.vnaph.value * document.datas.mbh.value * 239 * document.datas.corimch.value;
  document.datas.dejf.value = parseInt( resf ) ;
  document.datas.dejh.value = parseInt( resh ) ;
  
}

function CalcTot() {
  CalcImc();
  if ( CalcMb() ) {
    CalcNap();
  }
}
