var BMI = function() {
	
	this.height = 0;
	this.weight = 0;
	this.system = 'imperial';
	this.value = 0;
	
	this.changeUnit = function(unit) {
		
		if(unit == "imperial") {
			$("#heightUnit").text('inches');
			$("#weightUnit").text('pounds');
			this.system = 'imperial';
		} else {
			$("#heightUnit").text('meters');
			$("#weightUnit").text('kilograms');
			this.system = 'metric';
		}
		
	};
	
	
	this.calculate = function(height, weight) {
	
		if(height == 0 || height == '' || weight == 0 || weight == '') {
			return false;
		}
		
		this.height = height;
		this.weight = weight;
		
		if(this.system == 'imperial') {			
			this.value = (this.weight * 703) / (this.height * this.height);			
		} else {
			this.value = this.weight / (this.height * this.height);
		}
		
		if(this.value < 1 || this.value > 100) {
			$("#bmiError").text('This value does not appear to be correct, are you sure you have entered the correct details?');
		} else {
			$("#bmiError").empty();
		}
		
		$("#bmiDisplay span").text(this.value);
		$("#bmiDisplay").parent().show();
	}
	
};

var bmi = new BMI();