// Tobias Wetzel 

var BMI = ({
		   
	init: function(){
			
		$('calcMet').addEvent('click', function(){
			if(!$chk($('centimeter').get('value')) || !$chk($('kilos').get('value'))) {
				alert('Bitte geben Sie Ihre Größe und Ihr Gewicht ein');
				return;
			}
			var kil = $('kilos').get('value').toFloat();
			var hei = $('centimeter').get('value').toFloat() / 100;
			writeBmi(( kil / (hei * hei)));			
		});
		
		$('calcMetPwl').addEvent('click', function(){
			if(!$chk($('prevKilos').get('value')) || !$chk($('curKilos').get('value'))) {
				alert('Bitte geben Sie Ihr früheres und aktuelles Gewicht ein');
				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.krebsundernaehrung.de/tools/arztbesuch-danke.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 : Untergewicht';
            color = 'red';
        }
        
        if (index >= 18.5 && index < 25) {
            result = 'BMI 18,5 - 25 : Normalgewicht';
            color = 'green';
        }
        
        if (index >= 25 &&  index < 30) {
            result = 'BMI 25 - 30 : Übergewicht';
            color = 'orange';
        }
        
        if (index >= 30 &&  index < 40) {
            result = 'BMI 30 - 40 : Adipositas (Fettleibigkeit)';
            color = 'red';
        }    

        if (index > 40) {
            result = 'BMI > 40 : extreme Adipositas (extreme Fettleibigkeit)';
            color = 'red';
        }                                 
        
        $('ltlBmi').set('html', result);
        $('ltlBmi').setStyles({'color':color,'fontWeight':'bold'});
        
        $('spanBmi').set('html', 'Ihr BMI beträgt: ' + 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', 'Ihr prozentualer Gewichtsverlust beträgt ' + index + '%');
        pwl.set('html', 'Ihr prozentualer Gewichtsverlust beträgt ' + index + '%').setStyles({'color':'red'});
    }
    else if (index > 0 && index <= 5) {
        sPwl.set('html', 'Ihr prozentualer Gewichtsverlust beträgt ' + index + '%');    
        pwl.set('html','Ihr prozentualer Gewichtsverlust beträgt ' + index + '%').setStyles({'color':'orange'});        
    }
    else if (index <= 0) {
        sPwl.set('html', 'Ihr prozentualer Gewichtsverlust beträgt +' + (index*-1) + '%');
        pwl.set('html', 'Ihr prozentualer Gewichtsverlust beträgt +' + (index*-1) + '%').setStyles({'color':'green'});
    }
    
    sPwl.setStyles({'fontWeight':'bold'});
    pwl.setStyles({'fontWeight':'bold','display':'block'});
}