HeinzBMI = function() {
	var isInitialized;
	var measurementType;
	var sexType;
	var weight ;
	var feet;
	var inches;
	var meters;
	var error;
	var results;
	var bmiVariable;

	this.isInitialized = true;
	this.measurementType = "standard";
	this.bmiVariable = 703;	
	this.textType = "thin";
	}
	HeinzBMI.prototype={
		initializeHeinzBMI:function() {
			if (!(this.isInitialized)){ this.initializeHeinzBMI(); }
			this.isInitialized = false;
			this.weight = document.getElementById("weightField");
			this.feet = document.getElementById("feetField");
			this.inches = document.getElementById("inchesField");
			this.error = document.getElementById("calcErrorBox");
			this.meters = document.getElementById("cmField");
			this.results = document.getElementById("result");
			this.resultBar = document.getElementById("resultsBar");
                        $('#result').css('color' , '#666666');
		},

		calculateBMIFormula:function() {
			var bmi;
			var inputValidationSuccess = this.validateInputs();
			if (inputValidationSuccess) return false;
			
			bmi = this.calculateBMI();
			this.results.innerHTML = bmi;
			this.results.className = 'result';
			this.growCell();
			this.animateResults();
			this.setWeight();
		},
		calculateBMI:function() {
			var bmi = 0;
			if (this.measurementType == "standard") {
				var totalHeightInInches = (Math.abs(this.feet.value) * 12) + Math.abs(this.inches.value);
				bmi = (Math.abs(this.weight.value) /  (Math.abs(totalHeightInInches) * Math.abs(totalHeightInInches))) * this.bmiVariable;
			} else {
				bmi = Math.abs(this.weight.value) /  (Math.abs(this.meters.value) * Math.abs(this.meters.value)) * this.bmiVariable;;
			};
			return this.roundNumber(bmi,1);
			
		},
		roundNumber:function(num,dec) {

			var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
			return result;
		},
		growCell:function() {
			var setHeight = 5 * this.calculateBMI();
			document.getElementById("resultsBar").style.height = setHeight + 'px';
			
			var getMargin = 250 - this.calculateBMI() * 5;
			document.getElementById("resultsBar").style.marginTop = getMargin + 'px';
		},
		setWeight:function() {
				var thinMin = 0;
				var thinMax = 18.5;
				var normMin = 18.6;
				var normMax = 27.9;
				var obeseMin = 28;
				var obeseMax = 100;
				var weight = 1 * this.calculateBMI();
					if (normMin < weight && normMax > weight)
					{
						this.normalFunc();
						return false;
					}
					else if (weight == normMax || weight == normMin)
					{
						this.normalFunc();
						return false;	
					}
					else if (thinMin < weight && thinMax > weight)
					{
						this.thinFunc();
						return false;
					}
					else if (weight == thinMax || weight == thinMin)
					{
						this.thinFunc();
						return false;
					}
					else (obeseMin < weight && obeseMax > weight)
					{
						this.obeseFunc();
						return false;
					}
		},
		//animate function:grow cell
		animateResults:function() {
			$('#resultsBar').css('visibility','visible').fadeIn(250);
			//$('#resultsBar').SlideInDown(250);
			$('#result').fadeIn('slow');
		},
		validateInputs:function() {		
//has a weight been entered
			if (this.weight.value.isEmpty())
			{
				this.displayErrorBox(errorMsgWeight)
				this.weight.focus();
				$('#calcErrorBox').css('visibility' , 'visible').css('margin', '220px 0 0 160px').fadeIn(200);
				return true;
			}
			else {
				$('#calcErrorBox').css('visibility' , 'hidden');
			}
//is weight a valid #
			if (isNaN(parseFloat(this.weight.value)))
			{
				this.displayErrorBox(errorMsgNaN)
				this.weight.value='';
				this.weight.focus();
				$('#calcErrorBox').css('visibility' , 'visible').css('margin', '220px 0 0 160px').fadeIn(200);
				return true;
			}
			else {
				$('#calcErrorBox').css('visibility' , 'hidden');
			}


			if (this.measurementType == "standard") {
//Has Feet Been Entered?
				if (this.feet.value.isEmpty())
				{
					this.displayErrorBox(errorMsgFeet)
					this.feet.focus();
					$('#calcErrorBox').css('visibility' , 'visible').css('margin', '290px 0 0 160px').fadeIn(200);
					return true;
				}
				else {
					clearText(this.error);
				}
//Is Feet A Valid #
				if (isNaN(this.feet.value))
				{
					this.displayErrorBox(errorMsgNaN)
					this.feet.value='';
					this.feet.focus();
					$('#calcErrorBox').css('visibility' , 'visible').css('margin', '290px 0 0 160px').fadeIn(200);
					return true;
				}
				else {
					$('#calcErrorBox').css('visibility' , 'hidden');
				}
//Has inches been entered?
				if (this.inches.value.isEmpty())
				{
					this.displayErrorBox(errorMsgInches)
					this.inches.focus();
					$('#calcErrorBox').css('visibility' , 'visible').css('margin', '290px 0 0 160px').fadeIn(200);
					return true;
				}
				else {
					$('#calcErrorBox').css('visibility' , 'hidden');
				}
//is inches a valid #
				if (isNaN(this.inches.value))
				{
					this.displayErrorBox(errorMsgNaN)
					this.inches.value='';
					this.inches.focus();
					$('#calcErrorBox').css('visibility' , 'visible').css('margin', '290px 0 0 160px').fadeIn(200);
					return true;
				}
				else {
					$('#calcErrorBox').css('visibility' , 'hidden');
				}
//did someone enter more than 11 inches?
				if (this.inches.value > 11)
				{
					this.displayErrorBox(errorInchesMax)
					this.inches.focus();
					$('#calcErrorBox').css('visibility' , 'visible').css('margin', '290px 0 0 160px').fadeIn(200);
					return true;
				}
				else {
					$('#calcErrorBox').css('visibility' , 'hidden');
				}
			}
			else {
				
//Did they enter a cm input
				if (this.meters.value.isEmpty())
				{
					this.displayErrorBox(errorMsgCM)
					this.meters.focus();
					$('#calcErrorBox').css('visibility' , 'visible').css('margin', '290px 0 0 140px').fadeIn(200);
					return true;
				}
				else {
					$('#calcErrorBox').css('visibility' , 'hidden');
				}

//was the cm input a valid #
				if (isNaN(this.meters.value))
				{
					this.displayErrorBox(errorMsgNaN)
					this.meters.value='';
					this.meters.focus();
					$('#calcErrorBox').css('visibility' , 'visible').css('margin', '290px 0 0 140px').fadeIn(200);
					return true;
				}
				else {
					$('#calcErrorBox').css('visibility' , 'hidden');
				}
			}
		},//end validation
		displayErrorBox:function(message) {
			this.error.innerHTML=message;
		},
		performToggle:function() {
			if (this.measurementType == "standard") {
				
				document.getElementById("feetInput").className = 'bmiHeightValues';
				document.getElementById("heightInput").className = 'bmiHeightValues';
				document.getElementById("cmInput").className = 'dNone';
				$('#feetInput').fadeIn(300);
				$('#heightInput').fadeIn(300);
				
			}
			else {
				document.getElementById("feetInput").className = 'dNone';
				document.getElementById("heightInput").className = 'dNone';
				document.getElementById("cmInput").className = 'bmiHeightValuesCM';
				$('#cmInput').fadeIn(300);
			}
		},
		thinFunc:function() {
			$('#obese').css('color' , '#CCCCCC');
			$('#normal').css('color' , '#CCCCCC');
			$('#thin').css('color' , '#ffff00');
			$('#result').css('color' , '#ffff00');
		},
		normalFunc:function() {
			$('#obese').css('color' , '#CCCCCC');
			$('#thin').css('color' , '#CCCCCC');
			$('#normal').css('color' , '#99ff00');
			$('#result').css('color' , '#99ff00');
		},
		obeseFunc:function() {
			$('#thin').css('color' , '#CCCCCC');
			$('#normal').css('color' , '#CCCCCC');
			$('#obese').css('color' , '#ff6600');
			$('#result').css('color' , '#ff6600');
		},
		setmeasurementType:function(val) {
			this.measurementType = val;
			if (this.measurementType == "standard")
			{
				this.bmiVariable= 703;
			}
			else  {
				this.bmiVariable = 10000;
			}	
			this.performToggle();
		}
	} //end
	String.prototype.isEmpty = function() {
		if (this == '')	{
			return true
		}
	}
	function clearText(obj) {
		obj.innerHTML='';
	}
// Errors
var errorMsgInches = 'Enter Inches';
var errorMsgFeet = 'Enter Feet';
var errorMsgWeight = 'Enter weight';
var errorMsgCM = 'Enter Height';
var errorMsgEnterValue = 'Please enter a value';
var errorMsgNaN = 'Enter a Number';
var errorInchesMax = 'Inches must be < 12"';
