function calculIMC(formulario, resultado)
{
	// Limite de l'IMC pour le peso normal
	var limitMin = new Number(18.5);
	var limitMax = new Number(24.9);

	// Valeur de conversion d'unité
	var lb2kg = new Number(0.4535924);
	var in2cm = new Number(2.54);

	// Lecture des paramètres
	var talla_plus = new Number(formulario.talla_plus.value);
	var talla = new Number(formulario.calcul_talla.value);
	var peso = new Number(formulario.calcul_peso.value);
	var unittalla = formulario.unit_talla[formulario.unit_talla.selectedIndex].value;
	var unitpeso = formulario.unit_peso[formulario.unit_peso.selectedIndex].value;

	// Vérification de valeur valide
	if ((talla > 0) || (talla_plus > 0)) {
		// Transfert entre unité pour le calcul
		if (unittalla == "in") {
			talla = (((talla_plus * 12) + talla) * in2cm) / 100;
		} else {
			talla = ((talla_plus * 100) + talla) / 100;
		}

		// Transfert entre unité pour le calcul
		if (unitpeso == "lb") {
			peso = (peso * lb2kg) * 100;
		} else {
			peso = peso * 100;
		}

		// Calcul de l'IMC
		var imc = ((Math.round(peso/Math.pow(talla, 2)/10))/10) + "";

		// Conversion des . en , (norme de la langue Française)
		if (imc.indexOf('.') > 0) {
			var imcElt = imc.split('.');
			imc = imcElt.join(',');
		}

		// Affichage de la réponse dans la page
		formulario.imc.value = imc;

		if (resultado == 1) {
			if ((imc >= limitMin) && (imc <= limitMax)) {
				// Dialogue explicatif des résultats
				alert("¡Felicitaciones!\nUsted tiene un peso ideal saludable.")
			} else {
				// Calcul de la zone normal selon la talla
				var pesoMax = limitMax * Math.pow(talla, 2);
				var pesoMin = limitMin * Math.pow(talla, 2);

				// Transfert pour concorder avec les unité données en paramètres
				var unite;
				if (unitpeso == "lb") {
					pesoMax = Math.round((pesoMax / lb2kg) * 100) / 100;
					pesoMin = Math.round((pesoMin / lb2kg) * 100) / 100;
					unite = "libras";
				} else {
					pesoMax = Math.round(pesoMax * 100) / 100;
					pesoMin = Math.round(pesoMin * 100) / 100;
					unite = "kilogramos";
				}

				if (imc > limitMax) {
					// Dialogue explicatif des résultats
					alert("¡¡Está gordito!!!\nSu peso normal se situa entre "+pesoMin+" y "+pesoMax+" "+unite+".")
				} else {
					if (imc < limitMin) {
						// Dialogue explicatif des résultats
						alert("¡¡Está flaquito!!!\nSu pero normal se situa entre "+pesoMin+" et "+pesoMax+" "+unite+".")
					}
				}
			}
		}
	} else {
		alert("Su talla no puede ser nula, por favor agregarla");
	}
}

function checkIMC(formulario, resultado, layer) {
	var displayText = 0;
	
	// Valeur entrée par l'usager
	formulario.calcul_talla.value = formulario.talla.value;
	formulario.calcul_peso.value = formulario.peso.value;

	// Ajustement des valeurs pour les calculs mathématiques
	if (formulario.calcul_talla.value.indexOf(',') > 0) {
		var tallaElt = formulario.calcul_talla.value.split(',');
		formulario.calcul_talla.value = tallaElt.join('.');
	}
	if (formulario.calcul_peso.value.indexOf(',') > 0) {
		var pesoElt = formulario.calcul_peso.value.split(',');
		formulario.calcul_peso.value = pesoElt.join('.');
	}

	// Vérification que tous les éléments sont présent
	if (((formulario.calcul_talla.value > 0) || ((formulario.calcul_talla.value == 0) && (formulario.talla_plus.value > 0))) && (formulario.calcul_peso.value > 0) && (formulario.unit_talla.selectedIndex > 0) && (formulario.unit_peso.selectedIndex > 0)) {
		calculIMC(formulario, resultado)

		displayText = 1;
	}

	if (layer) {
		if (displayText == 1) {
			document.getElementById("text_imc").style.display = 'inline';
			formulario.valid_imc.value = 1;
		} else {
			document.getElementById("text_imc").style.display = 'none';
			formulario.valid_imc.value = 0;
		}
	}
}

function checkpesoDisplay(formulario) {
	var displaypeso = 0;

	if ((formulario.datenjj.selectedIndex > 0) && (formulario.datenmm.selectedIndex > 0) && (formulario.datenaa.selectedIndex > 0)) {
		var ageMin = 18;
		var ageMax = 64;

		// Calcul de l'age
		var age = calculAge(formulario.datenjj[formulario.datenjj.selectedIndex].value, formulario.datenmm[formulario.datenmm.selectedIndex].value, formulario.datenaa[formulario.datenaa.selectedIndex].value);

		if ((age >= ageMin) && (age <= ageMax)) {
			displaypeso = 1;
		}
	}

	if (displaypeso == 1) {
		document.getElementById("quest_peso").style.display = 'inline';
		formulario.valid_date.value = 1;
	} else {
		document.getElementById("quest_peso").style.display = 'none';
		formulario.valid_date.value = 0;
	}
}

function calculAge(dd, mm, yy) {
	var days = new Date();
	var gdate = days.getDate();
	var gmonth = days.getMonth();
	var gyear = days.getYear();
	if ( gyear < 1900 ) {
		gyear = gyear + 1900;
	}

	// Calcul de l'âge
	var age = gyear - yy;

	// Ajustement, si nécessaire
	if((mm == (gmonth + 1)) && (dd <= parseInt(gdate))) {
		age = age;
	}
	else {
		if(mm <= (gmonth)) {
			age = age;
		} else {
			age = age - 1; 
		}
	}

	if(age == 0) age = age;

	return age;
}
