// Tobias Wetzel 

var BMI = ({
		   
	init: function(){
		
		BMI.sli1 = new Fx.Slide('imperial').hide();
		BMI.sli2 = new Fx.Slide('metric').hide();
		BMI.sli3 = new Fx.Slide('imperialPwl').hide();
		BMI.sli4 = new Fx.Slide('metricPwl').hide();
		
		$('showImp').addEvent('click', function(){
			BMI.sli1.slideIn();
			BMI.sli2.slideOut();
		});
		$('showMet').addEvent('click', function(){
			BMI.sli1.slideOut();
			BMI.sli2.slideIn();
		});
		$('showImpPwl').addEvent('click', function(){
			BMI.sli3.slideIn();
			BMI.sli4.slideOut();
		});
		$('showMetPwl').addEvent('click', function(){
			BMI.sli3.slideOut();
			BMI.sli4.slideIn();
		});
		
		$('calcImp').addEvent('click', function(){
			if(!$chk($('feet').get('value')) || !$chk($('stones').get('value'))) {
				alert('Feet and stones are compulsory');
				return;
			}
			var hei = $('feet').get('value').toFloat() * 12 + $('inches').get('value').toFloat();
			var wei = $('stones').get('value').toFloat() * 14 + $('pounds').get('value').toFloat();
			
			writeBmi( (wei / (hei * hei)) * 703 );
		});
		
		$('calcMet').addEvent('click', function(){
			if(!$chk($('centimeter').get('value')) || !$chk($('kilos').get('value'))) {
				alert('Centimeters and kilos are compulsory');
				return;
			}
			var kil = $('kilos').get('value').toFloat();
			var hei = $('centimeter').get('value').toFloat() / 100;
			writeBmi(( kil / (hei * hei)));			
		});
		
		$('calcImpPwl').addEvent('click', function(){
			if(!$chk($('prevStones').get('value')) || !$chk($('curStones').get('value'))) {
				alert('Previous stones and current stones are compulsory');
				return;
			}
			var prevWei = $('prevStones').get('value').toFloat() * 14 + $('prevPounds').get('value');
			var curWei = $('curStones').get('value').toFloat() * 14 + $('curPounds').get('value');
			writePwl(Math.round(100 * (prevWei - curWei)) / prevWei);	
		});
		
		$('calcMetPwl').addEvent('click', function(){
			if(!$chk($('prevKilos').get('value')) || !$chk($('curKilos').get('value'))) {
				alert('Previous kilos and current kilos are compulsory');
				return;
			}
		
			var prevWei = $('prevKilos').get('value').toFloat();
			var curWei = $('curKilos').get('value').toFloat();
			writePwl(Math.round(100 * (prevWei - curWei)) / prevWei);
		});
		
		$$('input.floatnum').addEvent('keydown', function(e){
			if(e.code >= 48 && e.code <= 57 || e.code >= 96 && e.code <= 105 || e.code == 8) return;
			else e.preventDefault(); e.stopPropagation();
		});
		
		$('print').addEvent('click', function(){
			window.print();
			window.document.location.href = 'http://www.nutritionincancer.co.uk/en/special-tools/healthcare-professional-thank-you.html';
		});
	},
	
	setBMI: function(value){
		$('bmi').set('value', value.replace(".",","));
	}
		   
});

window.addEvent('domready', BMI.init);

    
    function writeBmi(index) {
        index = index.round(1);
        $('bmi').set('value', index);
        var result = '';
        var color = '';
        
        if (index <= 18.5) {
            result = 'BMI < 18,5 : Underweight';
            color = 'red';
        }
        
        if (index >= 18.5 && index < 25) {
            result = 'BMI 18,5 - 25 : Normal';
            color = 'green';
        }
        
        if (index >= 25 &&  index < 30) {
            result = 'BMI 25 - 30 : Overweight';
            color = 'orange';
        }
        
        if (index >= 30 &&  index < 40) {
            result = 'BMI 30 - 40 : Obese';
            color = 'red';
        }    

        if (index > 40) {
            result = 'BMI > 40 : Seriously Obese';
            color = 'red';
        }                                 
        
        $('ltlBmi').set('html', result);
        $('ltlBmi').setStyles({'color':color,'fontWeight':'bold'});
        
        $('spanBmi').set('html', 'Your BMI is: ' + index + ' (' + result + ')');
		$('spanBmi').setStyles({'fontWeight':'bold'});
    }



function writePwl(index) {
    index = index.round(1);
	var sPwl = $('spanPwl');
	var pwl = $('pwl');
    if (index > 5) {
        sPwl.set('html', 'Your Percentage Weight Loss is ' + index + '%');
        pwl.set('html', 'Your Percentage Weight Loss is ' + index + '%').setStyles({'color':'red'});
    }
    else if (index > 0 && index <= 5) {
        sPwl.set('html', 'Your Percentage Weight Loss is ' + index + '%');    
        pwl.set('html','Your Percentage Weight Loss is ' + index + '%').setStyles({'color':'orange'});        
    }
    else if (index <= 0) {
        sPwl.set('html', 'Your Percentage Weight Loss is +' + (index*-1) + '%');
        pwl.set('html', 'Your Percentage Weight Loss is +' + (index*-1) + '%').setStyles({'color':'green'});
    }
    
    sPwl.setStyles({'fontWeight':'bold'});
    pwl.setStyles({'fontWeight':'bold','display':'block'});
}