var KF = 0.6;
var KH = 0.7;

function IsReel(s)
{
  var pattern=/^[0-9]+\.?[0-9]*$/;
  return pattern.test(s) ;
}

function myround(original, rounding)
{
	var newnbr = Math.round(original*rounding)/rounding;
	return newnbr;
}

function GetHours(res, index)
{
	hours = String(res).substring(0, index);
	res = String(res).substring(index+1, String(res).length);
	hourtoadd = res/60;
	index = String(hourtoadd).indexOf(".");
	if (index != 0)
		hourtoadd = String(hourtoadd).substring(0, index);
	hours = parseInt(hours) + parseInt(hourtoadd);
	if (hours < 10)
		hours = "0"+hours;
	return hours;
}

function GetMin(res, index)
{
	res = String(res).substring(index+1, String(res).length);
	res = 0+"."+res
	min = res*60
	min = myround(min, 1);
	if (min < 10)
		min = "0"+min;
	return min;
}

function CalculAlcool()
{
	var f=document.all.form_alcool;
	var number, numbertotal, name, sous_total, res, div_num;
	if (f.alcool.value == '')
		alert("Le champs 'alcool pur ingéré' n'est pas renseigné");
	else if (f.temps.value == '')
		alert("Le champs 'temps' n'est pas renseigné");
	else if (f.poids.value == '')
		alert("Le champs 'poids' n'est pas renseigné");
	else if (f.sexe.checked == false)
		alert("Le champs 'sexe' n'est pas renseigné");
	else
	{
		if (!IsReel(f.alcool.value) || !IsReel(f.poids.value) || !IsReel(f.temps.value))
			alert('Il faut saisir un nombre (qui peut être à virgule ex : 100.25).');
		else
		{
			var coef = parseFloat(KH);
			if (f.sexe[1].checked == true)
				coef = parseFloat(KF);

			div_num = (parseFloat(f.poids.value) * parseFloat(coef));
			if (div_num > 0)
			{
				res = parseFloat(f.alcool.value)/parseFloat(div_num);
				f.taux.value=myround(res, 100);
				if (f.taux.value > 0)
				{
					res = (parseFloat(f.taux.value)-parseFloat(0.50))/parseFloat(0.15);
					if (res <0)
						res = Math.abs(res);
					res = myround(res, 100);
					
					res = parseFloat(res) - parseFloat(f.temps.value);
					res = myround(res, 100);
					if (res > 0)
					{
						var index = String(res).indexOf(".");
						if (index != 0)
						{
							hours = GetHours(res, index);
							min = GetMin(res, index);
						}
						else
						{
							hours = res;
							min = "00";
						}
					}
					else
					{
						hours = "00";
						min = "00";
					}
					f.retour_05_h.value=hours;
					f.retour_05_m.value=min;
					
					if (res <0)
						res = Math.abs(res);
					res = myround(res, 100)
					res =parseFloat(f.taux.value)/parseFloat(0.15);
					res = parseFloat(res) - parseFloat(f.temps.value);
					res = myround(res, 100);
					if (res > 0)
					{
						var index = String(res).indexOf(".");
						if (index != 0)
						{
							hours = GetHours(res, index);
							min = GetMin(res, index);
						}
						else
						{
							hours = res;
							min = "00";
						}
					}
					else
					{
						hours = "00";
						min = "00";
					}
					f.retour_normal_h.value=hours;
					f.retour_normal_m.value=min;
				}		
				else
				{
					f.taux.value="0";
					f.retour_05.value="0";
					f.retour_normal.value="0";
					alert('Attention : le resultat est négatif ou égal à zéro');
				}
			}
			else
				alert('Attention : division par zero');
		}
	}
}
