<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.util.Date"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Epeak Fitness Activities</title>
<%
	String userid = null;
	Cookie[] cookies = request.getCookies();
	if (cookies != null) {
		for (int i = 0; i < cookies.length; i++)
			if (cookies[i].getName().equals("userid"))
				userid = cookies[i].getValue();
	}

	String year = request.getParameter("year");
	String month = request.getParameter("month");
	String day = request.getParameter("day");
	if ((year == null) || (month == null) || (day == null)) {
		Date d = new Date();
		year = new Integer(d.getYear() + 1900).toString();
		month = new Integer(d.getMonth() + 1).toString();
		day = new Integer(d.getDate()).toString();
	}
%>
<script type="text/javascript">
var activityDay = new Date()
function logout() {
	date = new Date();
	document.cookie = 'userid=-1; expires='+date.toGMTString() + ";"
	document.location.pathname = "/fitness/login.html"
}
function changeDate() {
	activityDay = new Date(document.getElementById("yearid").value, document.getElementById("monthid").value - 1,document.getElementById("dayid").value);
	if (typeof(userBirth) != "undefined") {
		userAge = activityDay.getFullYear() - userBirth.getFullYear()
		document.getElementById("ageid").value = userAge
	}
}
function afterload() {
	document.getElementById("yearid").value = "<%= year %>"
	document.getElementById("monthid").value = "<%= month %>"
	document.getElementById("dayid").value = "<%= day %>"
	selectActivityType()
	changeDate()
}
function doCalculation() {
	age = document.getElementById("ageid").value
	weight = document.getElementById("weightid").value 
	if (document.getElementById("unitsid").checked)
		weight *= 0.45359237
	vo2max = document.getElementById("vo2maxid").value
	hr = document.getElementById("heartrateid").value
	min = document.getElementById("minid") .value
	male = document.getElementById("maleid").checked 
	female = document.getElementById("femaleid").checked
	if (isNaN(min) || min==0) {
		alert("Minutes exercised is required in order to calculate the estimated calories burned")
		return false;
	}
	if ((female || male) && !isNaN(hr) && (hr!=0) && !isNaN(weight) && (weight!=0) && !isNaN(age) && (age!=0)) {  
		if (isNaN(vo2max) || vo2max == -1) {
			if (male) {
				C = (-55.0969 + 0.2017 * age + 0.1988 * weight + 0.6309 * hr)/4.184*min
			} else {
				C = (-20.4022 + 0.0740 * age + 0.1263 * weight + 0.4472 * hr)/4.184*min
			}	
		} else {
			if (male) {
				C = (-59.3954 + (-36.3781 + 0.271 * age + 0.394 * weight + 0.404 * vo2max + 0.634 * hr))/4.184 * min
			} else {
				C = (-59.3954 + (0.274 * age + 0.103 * weight + 0.380 * vo2max + 0.450 * hr))/4.184 * min
			}	
		}
	} else {
		actType = document.getElementById("activityType").value;
		act = document.getElementById("activity").value
		burnRate = activities[actType][act][1]
	 	if (!isNaN(weight))
	 		weight = 160
	 	C = weight * burnRate * min / 60
		
	}
	document.getElementById("caloriesID").value = Math.round(C)
	return false
}
</script>
</head>
<body style="font-family: arial, sans-serif;" alink="blue" text="#000000" bgcolor="#ffffff" link="blue" vlink="blue"  onload="afterload()">
<script type="text/javascript">
activities = new Array()
<% 
int userWeight = 0;
int userVO2max = -1;
String userBirth = null;
char userGender = ' ';
Connection conn = null;
Statement stmt = null;
Statement actStmt = null;
ResultSet rs = null;
ResultSet actrs = null;
String[] ActivityTypes = {"aerobic", "misc", "running",	"sports",
			"cycling", "rowing", "swimming", "rowing", "martialArts",
			"rowing", "work", "horse", "skiing", "dance", "weightTraining", 
			"skating", "hunting"};
try {
	Class.forName("com.mysql.jdbc.Driver");
	conn = DriverManager.getConnection("jdbc:mysql://localhost/calories?user=root&password=frogpond63");
	stmt = conn.createStatement();
	actStmt = conn.createStatement();
	int activity;

	for (int i = 0; i < ActivityTypes.length; i++) { 
		
		out.print("activities['"+ActivityTypes[i]+"'] = [" );
		rs = stmt.executeQuery("SELECT * FROM "+ActivityTypes[i]);
		for (int count = 0; rs.next(); count++) {
			if (count != 0)
				out.print(",");
			activity = rs.getInt("activity");
			actrs = actStmt.executeQuery("SELECT * FROM activitie where id="+activity);
			if (actrs.next())
				out.print("[\""+actrs.getString("name")+"\"," + actrs.getString("burnrate")+ "," + actrs.getString("id")+"]");	
		}
		out.println("];");
	}
	if (userid != null) {
		rs = stmt.executeQuery("SELECT * FROM user WHERE id='" + userid+"'");
		if (rs.next()) {
			userWeight = rs.getInt("weight");
			userVO2max = rs.getInt("vo2max");
			userBirth  = rs.getString("birth");
			String gender = rs.getString("gender");
			if (gender != null)
				userGender = gender.charAt(0);
			if (userBirth != null) {
				String[] birthParts = userBirth.split("-");
				out.println("userBirth = new Date("+birthParts[0]+","+(Integer.parseInt(birthParts[1])-1)+","+birthParts[2]+")");
				
			}
		}
	}
} catch (Exception e) { e.printStackTrace();} 
finally {
	if (actrs != null) 
		try { actrs.close(); } catch (Exception e) {}
	if (actStmt != null) 
		try { actStmt.close(); } catch (Exception e) {}
	if (actrs != null) 
		try { rs.close(); } catch (Exception e) {}
	if (actStmt != null) 
		try { stmt.close(); } catch (Exception e) {}
	if (conn != null) 
		try { conn.close(); } catch (Exception e) {}
}


%>
</script>
<table width="100%">
<colgroup>
<col width="271"/>
<col width="*"/>
</colgroup>
	<tr>
		<td><img src="images/epicbiking.png" /></td>
		<td valign="middle">
		<div style="padding: .5em; font-size:large; text-align: center; background-color: lightblue; border-bottom: darkblue solid thin">Epeak Activities</div>
		<a href="foodlog.jsp">Food Log</a>&nbsp;&nbsp;<a href="exerciselog.jsp">Exercise Log</a>&nbsp;&nbsp;<a href="recipes.html">Recipes</a>&nbsp;&nbsp;<a href="activities.jsp">Activities</a>
		&nbsp;&nbsp;
		<% if (userid != null) { %>
			<span onclick="logout()" style="color:blue; text-decoration: underline;cursor: pointer">Log Out</span>
		<% } else { %>
			<span onclick="logout()" style="color:blue; text-decoration: underline;cursor: pointer">Log In</span>
		<% } %>
		</td>
	</tr>
</table>
<div style="margin-left: 3em; margin-right: 3em;">
<% if (!request.getRemoteHost().equals("127.0.0.1")) {%>
	<script type="text/javascript"><!--
		google_ad_client = "pub-3494473617065528";
		//Calories banner
		google_ad_slot = "5419586998";
		google_ad_width = 728;
		google_ad_height = 90;
	//--></script>
	<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
	</script>
<% } %>
<script type="text/javascript">
function selectActivityType() {
	actType = document.getElementById("activityType").value;
	act = document.getElementById("activity")
	act.innerHTML = "";
	for (var i = 0; i < activities[actType].length; i++) {
		act.innerHTML+="<option value='"+i+"'>"+activities[actType][i][0]+"</option>"
	}
}
function addToExerciseLog() {
	actType = document.getElementById("activityType").value;
	act = document.getElementById("activity").value
//	alert(actType + " " + act ) + " "+activities[actType][act][2])
	document.getElementById("activityid").value = activities[actType][act][2]
	document.getElementById("theform").submit();
}
</script>
<div>
<form id="theform" action="exerciselog.jsp" method="post">
<input type="hidden" id="activityid" name="activityid"/>
<input type="hidden" name="task" value="addActivity"/>
<%@ include file="dateselector.jsf"%>
<table>
	<tr>
		<td>
		<table>
		<colgroup><col width="1px"/><col width="*" /> </colgroup>
		<tr><td colspan="2">
			<select id="activityType" name="activityType" onchange="selectActivityType()">
				<option value="aerobic">Aerobic</option>
				<option value="misc">Miscellaneous</option>
				<option value="running">Running/Walking/Hiking</option>
				<option value="sports">Sports</option>
				<option value="cycling">Cycling</option>
				<option value="rowing">Rowing/Boats</option>
				<option value="swimming">Swimming</option>
				<option value="rowing">Rowing/Boats</option>
				<option value="martialArts">Martial Arts</option>
				<option value="rowing">Rowing/Boats</option>
				<option value="work">Work</option>
				<option value="horse">Horse</option>
				<option value="skiing">Skiing</option>
				<option value="dance">Dance</option>
				<option value="weightTraining">Weight Training</option>
				<option value="skating">Skating</option>
				<option value="hunting">Hunting/Fishing</option>
			</select>
			<select id="activity" name="activity"/>
			</td>
			</tr> 
			<tr><td>Distance</td><td><input type="text" size="4" name="distance"/></td></tr>
			<tr><td>Minutes</td><td><input id="minid" type="text" size="4" name="duration"/></td></tr>
			<tr><td>Average&nbsp;Heart&nbsp;Rate</td><td><input id="heartrateid" type="text" size="4" name="avgHR"/></td></tr>
			<tr><td>Calories&nbsp;Burned</td><td><input type="text" size="3" id="caloriesID" name="calories"/><button onclick="return doCalculation()">Calculate</button></td></tr>
			<tr><td colspan="2"><textarea rows="5" cols="80" name="comment"></textarea></td>
			<tr><td colspan="2"> <button onclick="addToExerciseLog()">Add to Exercise Log</button></td></tr>
			</table>
		</td>
		<td>
		<div style="background-color: lightblue; padding: 2px">
		<div style="background-color: white; padding: 2px">
		<table>
			<tr><td colspan="2" align="center">Personal Information</td></tr>
			<tr>
				<td><input id="maleid" type="radio" name="gender" value="1" <%= (userGender == 'm')?"checked=\"checked\"":""%> />male</td>
				<td><input id="femaleid" type="radio" name="gender" value="0" <%= (userGender == 'f')?"checked=\"checked\"":""%> />female</td>
			</tr>
			<tr>
				<td>age</td>
				<td><input id="ageid" type="text" name="age"/></td>
			</tr>
			<tr>
				<td>weight</td>
				<td><input id="weightid" type="text" name="weight" value="<%= (userWeight == 0)?"": userWeight%>"/></td>
			</tr>
			<tr>
				<td><input id="unitsid" type="radio" name="units" value="1" checked="checked" />lbs</td>
				<td><input type="radio" name="units" value="0" />kg</td>
			</tr>
			<tr>
				<td>VO<sub>2</sub>max</td>
				<td><input id="vo2maxid" type="text" name="vo2max" value="-1" /></td>
			</tr>
		</table>
		</div></div>
		</td>
	</tr>
</table>
</form>
</div>
<br />
<div class="footer">Copyright (c) 2007 <a href="http://www.epicbiking.com">Epic Biking</a> - <a href="./tos.html">Terms of Service</a> - 
<a href="./privacy.html">Privacy Policy</a> - <a href="mailto:epicbiking@gmail.com">Comments?</a></div>
<% if (!request.getRemoteHost().equals("127.0.0.1")) {%>
	<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
	</script>
	<script type="text/javascript">
		_uacct = "UA-3122173-1";
		urchinTracker();
	</script>
<% } %>
</body>
</html>

