// JavaScript Document

	var useCm = false; 	// Using centimetre for height, false = inch
	var useKg = false;	// Using kilos for weight, false = pounds;
	
	var graphColors = ['#ffbe85','#b6e785','#ffe985','#e685a7'];
	var graphLabels = ['Below 18.5: Underweight','18.5 - 24.9: Normal','25.0 - 29.9: Overweight','30 and above: Obese'];
	var labelsPerRow = 1;	/* Help labels above graph */
	var barHeight = 300; 	// Total height of bar
	var barWidth = 50;		// Width of bars */
	
	// Don't change anything below this point */
	
	var calculatorObj;
	var calculatorGraphObj;
	var bmiArray = [0,18.5,25,30,60];	/* BMI VALUES */
	var weightDiv = false;
		
	function calculateBMI()
	{
		var height = document.bmi_calculator.bmi_height.value;	
		var weight = document.bmi_calculator.bmi_weight.value;	
		height = height.replace(',','.');
		weight = weight.replace(',','.');
		if(!useKg)weight = weight / 2.2;
		if(!useCm)height = height * 2.54;
		
		if(isNaN(height))return;
		if(isNaN(weight))return;
		
		height = height / 100;
		var bmi = weight / (height*height);
		createWeightBar(bmi);		
	}
	
	function createWeightBar(inputValue){
		
		if(!weightDiv){
			self.status = Math.random();
			weightDiv = document.createElement('DIV');
			weightDiv.style.width = barWidth + 'px';
			weightDiv.className='wbarContainer';
			weightDiv.style.left = Math.round((calculatorGraphObj.offsetWidth/2) + ((calculatorGraphObj.offsetWidth/2) /2) - (barWidth/2)) + 'px';
			calculatorGraphObj.appendChild(weightDiv);
			var span = document.createElement('SPAN');
			weightDiv.appendChild(span);
			
			var innerSpan = document.createElement('SPAN');
			innerSpan.className='labelSpan';
			span.appendChild(innerSpan);			
		}else{
			span = weightDiv.getElementsByTagName('SPAN')[0];
			innerSpan = weightDiv.getElementsByTagName('SPAN')[1];
		}
		
		if(inputValue/1>1){
			innerSpan.innerHTML = inputValue.toFixed(2); 
			span.style.display='inline';
		}else span.style.display='none';
		var height = Math.min(Math.round(barHeight * (inputValue / bmiArray[bmiArray.length-1])),barHeight-10);
		span.style.lineHeight = Math.round(height) + 'px';
		weightDiv.style.height = height + 'px';
		
	}
	
	
	function validateField()
	{
		this.value = this.value.replace(/[^0-9,\.]/g,'');
		
	}
	
	function initBmiCalculator()
	{
		calculatorObj = document.getElementById('dhtmlgoodies_bmi_calculator');	
		calculatorGraphObj = document.getElementById('bmi_calculator_graph');	
		if(!useCm)document.getElementById('bmi_label_height').innerHTML = 'inches';
		if(!useKg)document.getElementById('bmi_label_weight').innerHTML = 'pounds';
		
		var heightInput = document.getElementById('bmi_height');
		heightInput.onblur = validateField; 
		var widthInput = document.getElementById('bmi_height');
		widthInput.onblur = validateField; 
		
		var labelDiv = document.createElement('DIV');
		labelDiv.className = 'graphLabels';
		calculatorGraphObj.appendChild(labelDiv);
		for(var no=graphLabels.length-1;no>=0;no--){
			var colorDiv = document.createElement('DIV');
			colorDiv.className='square';
			colorDiv.style.backgroundColor = graphColors[no];
			colorDiv.innerHTML = '<span></span>';
			labelDiv.appendChild(colorDiv);
			
			var labelDivTxt = document.createElement('DIV');
			labelDivTxt.innerHTML = graphLabels[no];
			labelDiv.appendChild(labelDivTxt);
			labelDivTxt.className='label';
			
			if((no+1)%labelsPerRow==0){
				var clearDiv = document.createElement('DIV');
				clearDiv.className='clear';
				labelDiv.appendChild(clearDiv);				
			}		
		}
		var clearDiv = document.createElement('DIV');
		clearDiv.className='clear';
		labelDiv.appendChild(clearDiv);	
						
		var graphDiv = document.createElement('DIV');
		graphDiv.className='barContainer';
		graphDiv.style.width = barWidth + 'px';
		graphDiv.style.left = Math.round(((calculatorGraphObj.offsetWidth/2) /2) - (barWidth/2)) + 'px';
		graphDiv.style.height = barHeight;
		calculatorGraphObj.appendChild(graphDiv);
		
		var totalHeight = 0;
		for(var no=bmiArray.length-1;no>0;no--){
			var aDiv = document.createElement('DIV');
			aDiv.style.backgroundColor = graphColors[no-1];
			aDiv.innerHTML = '<span></span>';
			var height = Math.round(barHeight * (bmiArray[no] - bmiArray[no-1]) / bmiArray[bmiArray.length-1]) - 1;
			aDiv.style.height = height + 'px';
			graphDiv.appendChild(aDiv);	
			
		}		
		
		createWeightBar(1);
	}