/*

Javascript methods used by Desifitness.com
(c) Desifitness.com - Unauthorized use prohibited.
Kartik Jayaraman, 04/07/07
*/

//Global variables which will be used to set values later
var heightmode;
var calories;
var bmi;
var hr;

//Displays page in drop down
function showpage()
{
	var Index = document.getElementById("Select1").selectedIndex;
	var val = document.getElementById("Select1").options[Index].value;
	//alert(val);
	if (Index==0) return;
	document.location.href=val;
}

//Gets the current date
function getTodaysDate() 
{
var today = new Date();

var monthnum = today.getMonth();
var monthname=new Array("JAN","FEB","MAR","APR","MAY",
  "JUN","JUL","AUG", "SEP","OCT","NOV","DEC");
var day=today.getDate();

//Year seems to be not working in Mozilla, omitted - Kartik Jayaraman
//var year=today.getYear();

var complete =  monthname[monthnum] + ' ' + day  //+ ' ' + year
return complete;

}

//Get the results needed
function calculateResults()
{

	//Validate data
	if (validatevalues())
	{
	
		var Index = document.getElementById("Selected_Units_Weight").selectedIndex;
		var weightunits = document.getElementById("Selected_Units_Weight").options[Index].value;
		var maleflag= document.getElementById("OPT_Male").checked; 
		var femaleflag= document.getElementById("OPT_Female").checked; 
		var heightfeet = parseInt(document.getElementById("Height_Feet").value,10);
		var heightinches= parseFloat(document.getElementById("Height_Inches").value); 
		var heightcm= parseFloat(document.getElementById("Height_CM").value);  
		var weight= parseFloat(document.getElementById("Weight").value);  		
		var age= parseInt(document.getElementById("Age").value,10);   
		
		//Convert results to English units
		if (weightunits=='kg') 
		{
			weight = parseFloat(weight) * 2.2;
		}
		
		if (heightmode=='cm') 
		{
			height = parseFloat(parseFloat(heightcm)) * 0.393700787;
		}
		else
		{
			height = parseFloat(heightfeet) * 12 + parseFloat(heightinches);
		}
		
		//Calories needed
		//Adult male: 66 + (6.3 x body weight in lbs.) + (12.9 x height in inches) - (6.8 x age in years)
		//Adult female: 655 + (4.3 x weight in lbs.) + (4.7 x height in inches) - (4.7 x age in years)
		
		if (maleflag) 
		{
			var	calories = 66 + (6.3 * parseFloat(weight)) + (12.9 * parseFloat(height)) - (6.8 * parseInt(age))
		}
		else
		{
			var	calories = 655 + (4.3 * parseFloat(weight)) + (4.7 * parseFloat(height)) - (4.7 * parseInt(age))
		}
		
		document.getElementById("CaloriesNeeded").innerHTML = "<strong><font color=#990000>" + "RESULTS: You need " + Math.floor(calories) + " calories to maintain your current weight" + "</font></strong>"
		
		//Calculate BMI
		
		var    h2 =  parseFloat(height)  * parseFloat(height)  ;
		   bmi = weight/h2 * 703
		   bmibuffer = Math.floor(bmi);
		   diff  = bmi - bmibuffer;
		   diff = diff * 10;
		   diff = Math.round(diff);
		
		   if (diff == 10)    // Need to bump up the whole thing instead
		   {
		      bmibuffer += 1;
		      diff   = 0;
		   }
		   bmi = bmibuffer + "." + diff;
		
			document.getElementById("CalculateBMI").innerHTML = "<strong><font color=#990000>" + "<BR>RESULTS: Your BMI is: " + Math.floor(bmi) + "</font></strong>"			

	
	
		//Calculate target Heart Rate
		hr = (220-age)
		var hrlow= Math.floor((50/100) * hr)
		var hrhigh = Math.floor((85/100) * hr)
		document.getElementById("TargetHR").innerHTML = "<strong><font color=#990000>" + "RESULTS: Your Heart rate should be between " + hrlow + " and " + hrhigh + " depending on your comfort level</font></strong>"			
		
	}
}

function validatevalues() 
{
	var heightfeet = parseInt(document.getElementById("Height_Feet").value,10);
	var heightinches= parseFloat(document.getElementById("Height_Inches").value); 
	var heightcm= parseFloat(document.getElementById("Height_CM").value);  
	var weight= parseFloat(document.getElementById("Weight").value);  
	var weight_units= document.getElementById("Selected_Units_Weight").value;  
	var maleflag= document.getElementById("OPT_Male").value;  
	var femaleflag= document.getElementById("OPT_Female").value;   
	var age= parseInt(document.getElementById("Age").value,10);    
	
	if (isNaN(age))
	{
		alert('Please enter age!');
		document.getElementById("Age").focus();
		return false;
	}
	
	if (isNaN(heightfeet) || isNaN(heightinches)) 
	{
		//Feet and inches wasnt entered, so check cm
		if (isNaN(parseFloat(heightcm)))
		{
			alert('Please enter height correctly!');
			return false;
		}				
		else
		{	//Check for negative value
			if (parseFloat(heightcm) <= 0) 
			{
				alert('Please enter height correctly!');
				return false;
			}
			else
			{
				heightmode='cm';
				document.getElementById("Height_Feet").value='';
				document.getElementById("Height_Inches").value='';
			}
		}
	}
	else
	{
		if (parseInt(heightfeet,10) < 0 || parseFloat(heightinches) < 0) 
		{	
			//Negative value check
			alert('Please enter height correctly!');
			return false;
		}
		else
		{
			heightmode='feet';
			document.getElementById("Height_CM").value='';
		}
	}

	//Go to next check - weight
	if (isNaN(weight)) 
	{
		alert('Please enter weight!');
		return false;
	}
	else
	{
		//negative value check
		if (parseInt(weight,10) <= 0) 
		{
			alert('Please enter weight correctly!');
			return false;
		}
	}
	return true;
}

function trimAll(sString)
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}