// JavaScript Document
$(function() {	
	$('form[name=imc]').submit(function() {			
		var u_alt = parseInt($('select[name=medida_altura] option:selected').attr('value'));
		var u_pes = parseInt($('select[name=medida_peso] option:selected').attr('value'));
			
		if(u_alt == 0 || u_pes == 0) {
			alert('Favor de elegir una unidad de medida');
			return false;
		}
						
		var alt = $('input[name=altura]').attr('value');
		var pes = $('input[name=peso]').attr('value');
			
		if(alt <= 0 || alt == '' || pes <= 0 || pes == '') {
			alert('Llene los campos de textos con sus datos requeridos');
			return false;
		}
		
		switch(u_alt) {
			case 1:
				alt = alt / 100;
				break;
			case 2:
				alt = alt;
				break;
			case 3:
				alt = alt * 0.025;
				break;
			case 4:
				alt = alt * 0.25;
				break;
		}
		
		switch(u_pes) {
			case 1:
				pes = pes;
				break;
			case 2:
				pes = pes * 0.323;
				break;
		}
		//alert('Peso => '+pes+' Altura => '+alt);				
		var resp = pes/(alt * alt);							
		resp = parseFloat(resp);
		resp = resp.toFixed(2);
		$('label.imc').html(resp);
					
		if(resp < 18.5) {
			dif = 21.7 - resp;				
			dif = Math.round(dif);				
			p = (alt * alt) * dif;				
			if(u_pes == 1) {					
				p = Math.round(p)+' KGS';
			} else {									
				p = Math.round(p * 2.20)+' LBS';
			}  											
			$('.resultado span').html('INFERIOR AL NORMAL');
			$('.recomend').html('LE RECOMENDAMOS QUE AUMENTE '+p);
		} else if(resp >= 18.5 && resp <= 24.9) {
			$('.resultado span').html('PESO ADECUADO');
			$('.recomend').html('SU PESO ES EL RECOMENDADO PARA SU ESTATURA');
		} else if(resp >= 25.0 && resp <= 29.9) {
			dif = resp - 21.7;				
			dif = Math.round(dif);				
			p = (alt * alt) * dif;				
			if(u_pes == 1) {					
				p = Math.round(p)+' KGS';
			} else {									
				p = Math.round(p * 2.20)+' LBS';
			}  		
			$('.resultado span').html('SUPERIOR AL NORMAL');
			$('.recomend').html('LE RECOMENDAMOS QUE DISMINUYA '+p);
		}else if(resp > 30.0) {				
			dif = resp - 21.7;				
			dif = Math.round(dif);				
			p = (alt * alt) * dif;				
			if(u_pes == 1) {					
				p = Math.round(p)+' KGS';
			} else {									
				p = Math.round(p * 2.20)+' LBS';
			}
			$('.resultado span').html('ESTAS EN SOBREPESO');
			$('.recomend').html('LE RECOMENDAMOS QUE DISMINUYA '+p);  							
		}
		$('input[name=altura]').attr('value', '');
		$('input[name=peso]').attr('value', '');
		return false;			
	});		
});