/**
 * @author mpinir01
 * date:19 Decemeber 2007
 */
 BMICalculator = function()
 {
 	this.addEvent();
 	this.dom = new newsi.HTML.DOM();
 	this._metric();
 	this._imperial();
 	//var cont = document.getElementById("showContainer");
 	this.dom.setClass( {el:document.getElementById("showContainer"), className:"bmi_container"} );
 
 }
 	
	//method to listen to onclick for radio group
 	BMICalculator.prototype._metric = function()
 	{
 		var met = new newsi.Event();
 		met.addListener("onclick", "_met", this, "_setMetric", "false");
 	}
 	
 	BMICalculator.prototype._imperial = function()
 	{
 		var met = new newsi.Event();
 		met.addListener("onclick", "_imp", this, "_setImperial", "false");
 	}
	
 	BMICalculator.prototype._setImperial = function()
 	{
 		this.myUnit = "imp"
 		this._initOnChange();
 	}
 	
 	BMICalculator.prototype._setMetric = function()
 	{
 		this.myUnit = "met";
 		this._initOnChange();
 	}
 	
 	BMICalculator.prototype._initOnChange = function()
 	{
 		
 		this.printToPage();
 	}
 	
	BMICalculator.prototype.addEvent = function()
	{
		var calc = new newsi.Event();
		calc.addListener( "onclick", "executeCalculation", this, "result", "false" )
	}
	
	//check radio button selected
	BMICalculator.prototype.isImperial = function()
	{
		//test which button is clicked
		if(this.myUnit == "imp")
		{
 			return true;
 		}
 			return false;
		}
	
	//produce selected inputs to user
	BMICalculator.prototype.printToPage = function()
	{
		this.rewriteTextElements();
	}
	
	
	/**
	 * Add / Remove the weight boxes based on metric or imperial settings
	 */
	BMICalculator.prototype.rewriteTextElements = function(){
		
		var tUnitRowOld = document.getElementById('theUnit');
		
		var tUnitRowNew = document.createElement('tr');
		tUnitRowNew.setAttribute("id","theUnit");
		
		var tUnitFirstCell = document.createElement('td');
		var tUnitSecondCell = document.createElement('td');
		
		tUnitFirstCell.setAttribute("class","td_left");
		tUnitSecondCell.setAttribute("class","td_right");
		
		tUnitRowOld.parentNode.replaceChild(tUnitRowNew,tUnitRowOld)

		tUnitRowNew.appendChild(tUnitFirstCell);
		tUnitRowNew.appendChild(tUnitSecondCell);
		
		if( this.myUnit === "imp" ){
			tUnitFirstCell.innerHTML = "<input type='text' name='f1' class='f1' value='0'/>&#39;<input type='text' name='f2' class='f2' value='0'/>&#34;";
			tUnitSecondCell.innerHTML = "<input type='text' name='f3' class='f3' value='0'/>st<input type='text' name='f4' class='f2' value='0'/>lb"
		}
		else{
			tUnitFirstCell.innerHTML = "<input type='text' name='m1' class='m1' value='0.00'/>m";
			tUnitSecondCell.innerHTML = "<input type='text' name='m2' class='m2' value='0.00'/>kg";
		}

	}

	
	//metric calculation
	BMICalculator.prototype._metricCalculation = function()
	{
		//perform the calculation
		var h = document.bmicalculator.m1.value;
		var w = document.bmicalculator.m2.value;
		this.bmi = (w / ( h * h) );
	}
	
	//imperial calculation
	BMICalculator.prototype._imperialCalculation = function()
	{
		//perform the calculation
		var feet = document.bmicalculator.f1.value;
		var inches = document.bmicalculator.f2.value;
		var stones = document.bmicalculator.f3.value;
		var pounds = document.bmicalculator.f4.value;
		var h, w;
		h = ( parseFloat( ( 12 * feet ) ) + parseFloat( inches ));
		w = (parseFloat( (14*stones) ) + parseFloat( pounds ));
		this.bmi = ( (w / ( h * h)) * 703);
	}
	
	//perform calculation
	BMICalculator.prototype._calculate = function()
	{		
		if( this.isImperial() )
		{
			this._imperialCalculation();
		}else
		{
			this._metricCalculation();
		}
		
	}
	
	BMICalculator.prototype.result = function()
	{
		this._calculate();
		var myclass = document.getElementById("result_color")
		var total = this.bmi;
		//underweight
		document.getElementById("_value").innerHTML = this.bmi.toFixed(2);
		if(total <= 20)
		{
			this.dom.setClass( {el:myclass, className:"bmi_underweight"} );
			myclass.innerHTML = "Underweight";
		}else
		{
			//ideal
			if( ( total > 20 ) && ( total <= 25 ) )
			{
				this.dom.setClass( {el:myclass, className:"bmi_ideal"} );
				myclass.innerHTML = "Ideal";
			}else
			{
				//overweight
				if( ( total > 25 ) && ( total <= 30 ) )
				{
					this.dom.setClass( {el:myclass, className:"bmi_overweight"} );
					myclass.innerHTML = "Overweight";
				}else
				{
					//obese
					if( ( total > 30 ) && ( total <= 40 ) )
					{
						this.dom.setClass( {el:myclass, className:"bmi_obese"} );
						myclass.innerHTML = "Obese";
					}else
					{
						//very obese
						if( total > 40 )
						{
							this.dom.setClass( {el:myclass, className:"bmi_veryobese"} );
							myclass.innerHTML = "Very obese";
						}
					}
				}
			}
		}
		return;
	}
	
	