/* navigation hover effect for IE */
sfHover = function() {
	var sfEls = document.getElementById("navigation").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" fthover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" fthover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

/* studio page trainer bios */
function showTrainer(trainer)
{
	document.getElementById(selected_trainerID).className = "bio";
	document.getElementById(trainer).className = "bio selected";
	selected_trainerID = trainer;
}

/* sidebar success story swapping */
window.setTimeout('fadeOutSuccessStory()', 10000);

/* fade out the success story */
function fadeOutSuccessStory()
{
	// fade the testimonial out
	Effect.Fade(document.getElementById('ss_title'), {to: 0.001, duration:1});
	Effect.Fade(document.getElementById('ss_img'), {to: 0.001, duration:1});
	Effect.Fade(document.getElementById('ss_quote'), {to: 0.001, duration:1});
	
	// make the ajax call
	window.setTimeout('updateSuccessStory()', 1000);

	// do it all over again
	window.setTimeout('fadeOutSuccessStory()', 10000);
}

/* success story ajax call */
function updateSuccessStory()
{
	var ajaxRequest = new Ajax.Request('/successstory.php?op=1' + (Math.random()*11), {method: 'get', onComplete: fadeInSuccessStory});
}

/* fade in success story */
function fadeInSuccessStory(request)
{
	// update the success story
	$('sidebar_successstory').innerHTML = request.responseText;

	// fade the testimonial in
	Effect.Appear(document.getElementById('ss_title'), {duration:1});
	Effect.Appear(document.getElementById('ss_img'), {duration:1});
	Effect.Appear(document.getElementById('ss_quote'), {duration:1});
}

/* calculate max heart rate */
function calcMaxHR()
{
	age = document.maxHRForm.age.value;

	if (age == "")
	{
		document.maxHRForm.age.style.backgroundColor = "pink";
		$('max_hr_error_msg').innerHTML = "Age is required";		
		$('max_hr_table').style.display = "none";
		Effect.Appear(document.getElementById('max_hr_error'), {duration:1});
		return false;
	}
	else
	{
		$('max_hr_error').style.display = "none";
		document.maxHRForm.age.style.backgroundColor = "white";
	}

	hr = 205.8 - (.685 * age);
	hr = Math.round(hr*100)/100;

	$('max_hr_result').innerHTML = hr;
	Effect.Appear(document.getElementById('max_hr_table'), {duration: 1});

	return false;
}

/* calculate target heart rate */
function calcTargetHR()
{
	age = document.targetHRForm.age.value;
	resting_hr = document.targetHRForm.resting_hr.value;

	if (age == "" || resting_hr == "")
	{
		document.targetHRForm.age.style.backgroundColor = "pink";
		document.targetHRForm.resting_hr.style.backgroundColor = "pink";
		$('target_hr_error_msg').innerHTML = "Age and Resting Heart Rate are required";		
		$('target_hr_table').style.display = "none";
		Effect.Appear(document.getElementById('target_hr_error'), {duration:1});
		return false;
	}
	else
	{
		$('target_hr_error').style.display = "none";
		document.targetHRForm.age.style.backgroundColor = "white";
		document.targetHRForm.resting_hr.style.backgroundColor = "white";
	}

	max_hr = 205.8 - (.685 * age);

	max50 = ((max_hr - resting_hr) * .50) + resting_hr;
	max50 = Math.round(max50*100)/100;
	
	max60 = ((max_hr - resting_hr) * .60) + resting_hr;
	max60 = Math.round(max60*100)/100;

	max70 = ((max_hr - resting_hr) * .70) + resting_hr;
	max70 = Math.round(max70*100)/100;

	max80 = ((max_hr - resting_hr) * .80) + resting_hr;
	max80 = Math.round(max80*100)/100;

	max90 = ((max_hr - resting_hr) * .90) + resting_hr;
	max90 = Math.round(max90*100)/100;

	$('target_hr_result').innerHTML = 	"50% of Max = " + max50 + "<br/>" + 
										"60% of Max = " + max60 + "<br/>" +
										"70% of Max = " + max70 + "<br/>" +
										"80% of Max = " + max80 + "<br/>" + 
										"90% of Max = " + max90 + "<br/>"; 

	Effect.Appear(document.getElementById('target_hr_table'), {duration: 1});

	return false;
}

/* calculate body mass index */
function calcBMI()
{
	weight = document.bmiForm.weight.value;
	height = document.bmiForm.height.value;

	if (weight == "" || height == "")
	{
		document.bmiForm.weight.style.backgroundColor = "pink";
		document.bmiForm.height.style.backgroundColor = "pink";
		$('bmi_error_msg').innerHTML = "Weight and Height are required";		
		$('bmi_table').style.display = "none";
		Effect.Appear(document.getElementById('bmi_error'), {duration:1});
		return false;
	}
	else
	{
		$('bmi_error').style.display = "none";
		document.bmiForm.weight.style.backgroundColor = "white";
		document.bmiForm.height.style.backgroundColor = "white";
	}

	bmi = ( weight / ( height * height )) * 703;
	bmi = Math.round(bmi*100)/100;

	$('bmi_result').innerHTML = bmi;
	Effect.Appear(document.getElementById('bmi_table'), {duration: 1});

	return false;
}

/* quiz */
function displayQuizResult(element, answer_msg, explanation, correct)
{
	html_result = '';

	if (correct)
		html_result += '<p class="answer_msg">Correct!</p>';
	else
		html_result += '<p class="answer_msg">Correct answer: ' + answer_msg + '</p>';

	html_result += '<p>' + explanation + '</p>';

	document.getElementById(element).innerHTML = html_result;
}

function ltoi(letter)
{
	if (letter == "a") return 0;
	if (letter == "b") return 1;
	if (letter == "c") return 2;
	if (letter == "d") return 3;
	if (letter == "e") return 4;
}

function displayQuizResults(number_correct)
{	
	html_result = "<p class=\"answer_msg\">You answered " + number_correct + " out of 10 questions correctly!</p>";
	html_result += "<p>Even if you answered all 10 questions correctly the real questions are:";
	html_result += "<ol><li>Are you doing what you need to be doing on a daily basis to be fit, healthy, and happy?</li>";
	html_result += "<li>Have you reached all of your health and fitness goals?</li>";
	html_result += "<li>Do you need to make a change in your life?</li></ol>";
	html_result += "The staff at Fitness Together pride themselves in providing the absolute best in personal training.  With a staff to client ratio of only 1:9 we are committed to the personal service and accountability necessary to ensure that you reach your goals.  No more excuses, no more procrastination – It’s time.</p>";
 
	$('quiz_result').innerHTML = html_result;
	var str = new String(document.location);
	if (str.indexOf("#quiz") > -1)
		document.location = str;		
	else
		document.location = str += "#quiz";
		
}

function scoreQuiz()
{
	q1 = document.quiz.q1[ltoi("e")].checked;
	q2 = document.quiz.q2[ltoi("b")].checked;
	q3 = document.quiz.q3[ltoi("d")].checked;
	q4 = document.quiz.q4[ltoi("b")].checked;
	q5 = document.quiz.q5[ltoi("a")].checked;
	q6 = document.quiz.q6[ltoi("b")].checked;
	q7 = document.quiz.q7[ltoi("d")].checked;
	q8 = document.quiz.q8[ltoi("d")].checked;
	q9 = document.quiz.q9[ltoi("d")].checked;
	q10 = document.quiz.q10[ltoi("a")].checked;
	
	displayQuizResult("q1_answer", 'All of the above', 'Resistance training, cardiovascular exercise, and good nutritional habits are all essential components of a good fitness program.  In conjunction, with regular stretching, they all contribute to a healthy, fit and lean body.  They also play a major role in joint maintenance and injury/disease prevention. ', q1);
	displayQuizResult("q2_answer", 'False', 'Regular physical activity helps children, teens and young adults become physically fit by building muscle mass, strengthening bones and decreasing body fat. This is especially important as the rate of obesity among teens and younger children is increasing at an alarming rate.<br/><br/>Make physical activity part of a healthy lifestyle for you and your family. For example, plan family activities such as hiking, bicycling, playing a backyard game of basketball or swimming at your local community pool. Instead of automatically handing over the car keys to your teen, see if he or she can walk or ride a bike instead.', q2);
	displayQuizResult("q3_answer", 'All of the above', 'Getting in shape requires consistency and adherence to a properly structured program.  As beneficial as resistance training and cardiovascular exercise can be, doing it wrong can not only sabotage your results but can put you at risk for injury.  Be sure to consult with your doctor and/or find a personal trainer you trust to hold you accountable and help you get results.', q3);
	displayQuizResult("q4_answer", '30 to 60 minutes', 'Thirty minutes of moderately intense physical activity on most days of the week provides many health benefits, according to the Centers for Disease Control and Prevention and the American College of Sports Medicine. The Institute of Medicine advocates 60 minutes a day.<br/><br/>Some of the health benefits of regular exercise include lower blood pressure, stronger bones and muscles, and a more positive outlook on life. The benefits grow as you increase your exercise intensity or duration. To avoid weight gain or lose weight, you will need more than 30 minutes of activity every day. In that case, make your goal 60 minutes most days of the week, and remember that intensity has a very high correlation with results.  Work hard, but work smart.', q4);
	displayQuizResult("q5_answer", 'True', "Take these into account in your exercise program, and you'll be on your way to living a long, healthy life.<ul><li>Aerobic fitness is your body's ability to take in and use energy.</li><li>Muscular fitness refers to the strength and endurance of your muscles.</li><li>Flexibility is your ability to bend joints and stretch muscles through their full range of motion.</li><li>Balance is the ability to control your center of gravity over your base of support. Balance training with core exercises improves stability in all of your activities and helps reduce the risk of falls as you grow older.</li></ul>", q5);
	displayQuizResult("q6_answer", 'False', "To maintain a healthy weight, you have to watch what you eat — even if you're exercising regularly. Regular exercise means you're burning more calories on average than you would if you were sedentary. That might seem like a green light to indulge, but be careful. You need to strike a balance between the number of calories you take in and the number of calories you burn through your activities each day.<br/><br/>Pay attention to the kinds of foods you're eating and your portion sizes. Choose a wide variety of foods — including lots of fruits and vegetables — and keep your portion sizes in check to help manage your weight.", q6);
	displayQuizResult("q7_answer", 'All of the above', "Among the many benefits of physical activity, exercise helps prevent:<ul><li>Coronary artery disease</li><li>High blood pressure</li><li>Stroke</li><li>Diabetes</li><li>Depression</li><li>Osteoporosis</li></ul>Regular physical activity may also help prevent some cancers, such as colon cancer.<br/><br/>It may sound simplistic, but 30 minutes of activity a day can do you a world of good. And the sooner you start, the less you'll need to worry about later.", q7);
	displayQuizResult("q8_answer", 'None of the above', "You're never too old to exercise. Studies indicate that even frail adults in their 80s and 90s benefit from strength training. A strength training program makes your muscles stronger, which protects your joints and makes it easier to do everyday things — such as getting in and out of chairs and walking to the mailbox.<br/><br/>In addition, regular physical activity:<ul><li>Improves your breathing capacity</li><li>Keeps you flexible</li><li>Builds your bone mass</li><li>Reduces your risk of falls</li></ul>In fact, you can't afford not to be active. The older you get, the more your lean muscle mass decreases. The ability of your heart, lungs and blood vessels to deliver adequate oxygen to your muscles (aerobic capacity) also decreases, as does the elasticity of your joints and ligaments. Staying active as you age increases the likelihood that you'll continue to live independently.", q8);
	displayQuizResult("q9_answer", 'All of the above', "If you're not physically fit, prepare now to start a fitness program. Although it's true you can exercise at any age, talk with your doctor before starting a fitness program if you have a chronic health condition — asthma, high blood pressure or heart disease, for example. And ask your doctor if medications you're taking may affect your exercise plan.", q9);
	displayQuizResult("q10_answer", 'True', "Ready for some good news? You can make up for the loss in health benefits by restarting your regular exercise program. But remember to start slowly. Use extra caution if your exercise program has been interrupted by illness or hospitalization. Make sure it's OK with your doctor before you start.", q10);
	
	var count = 0;

	if (q1) count++;
	if (q2) count++;
	if (q3) count++;
	if (q4) count++;
	if (q5) count++;
	if (q6) count++;
	if (q7) count++;
	if (q8) count++;
	if (q9) count++;
	if (q10) count++;

	displayQuizResults(count);

	return false;
}
