var BMI = Class.create();
BMI.prototype = {

    initialize : function BMI() {

        this.bmiArray = [0,18.5,25,30,35,40];

        this.i_height = $('bmi_height');
        this.i_weight = $('bmi_weight');
        this.result   = $('bmi_result');
        this.clear();

        Event.observe($('bmi_button'), 'click', this.calculate.bindAsEventListener(this));
    },

    getIndex : function(value) {
        for(var i = this.bmiArray.length; i>=0; i--){
            if(value >= this.bmiArray[i-1]) return i-1;
        }
        return 0;
    },

    getBMI : function(height, weight) {
        var h = height.value.replace(',','.');
        var w = weight.value.replace(',','.');

        if(isNaN(h)) return '-';
        if(isNaN(w)) return '-';

        h = h / 100;

        var bmi = w / (h*h);
        return (Math.round(bmi*100))/100
    },

    calculate : function(e) {

        this.clear();
        var bmi = this.getBMI(this.i_height, this.i_weight);

        if(!isNaN(bmi)) {
            var index = this.getIndex(bmi);
            $('bmi_res'+index).style.backgroundColor = '#FFCC66';
            $('bmi_res'+index).parentNode.style.color = '#FFCC66';
            $('bmi_res'+index).parentNode.style.fontWeight = 'bold';
            this.result.innerHTML = bmi;
        }
    },

    clear : function() {
        this.result.innerHTML = '';
        for(var i = 0; i<this.bmiArray.length; i++){
            $('bmi_res'+i).style.backgroundColor = '#000';
            $('bmi_res'+i).parentNode.style.color = 'white';
            $('bmi_res'+i).parentNode.style.fontWeight = 'normal';
        }
    }
}

