I am not a Javascript developer, though I was able to use some opensource code and make most of the changes that I needed for my needs. I am just having problems with adjusting some of the output.
I need to modify the low end and high end to not be a calculation, but to say "lower" for "0" and on the high end I get "NaN" but need it to be the word "greater"
Problem two is that the caclulations are based on the previous fields results for all zones and need to overide for certain zones. so for example zone 3, low end is the high end from zone 2. I need to be able to have the low end of for certain zones be based on a different percentage and not the low end of the previous zone. Again for only certain zones.
Code:
<SCRIPT language="javascript">
<!----
window.onError = null;
var justloaded=true;
var expireDate = new Date;
var dc = document.cookie
var nZones=15
if(dc!="")var aCookies=document.cookie.split(";");
function readcookie(){
var f = document.frm_Heart_Rate_Calculator;
var mhr="";
if(dc!=""){
for(var i=0; i<aCookies.length; i++){
mhr = eval(aCookies[i].split("=")[1]);
f.txt_Maxmum_HeartRate.value=mhr;
}
}
}
function writecookie(){
var f=eval(parseInt(document.frm_Heart_Rate_Calculator.txt_Maxmum_HeartRate.value));
expireDate.setYear(expireDate.getYear()+1)
if(f>0 && !isNaN(f)){
document.cookie = "mhr=" + f + "; expires=" + expireDate.toGMTString();
}
}
function deletecookie(){
if(document.cookie!=""){
if(confirm("Do you want to remove the cookies to?")){
expireDate.setYear(expireDate.getYear()-1)
document.cookie = "mhr= ; expires=" + expireDate.toGMTString();
}
}
}
function browsertype(){
var n=navigator.appName
if(n == 'Microsoft Internet Explorer') return "IE";
else if(n == 'Netscape')return "NC";
else return "unknown";
}
function browserver(v){
var iVer;
var strVer=navigator.appVersion.toLowerCase()
if(browsertype()=="IE"){
var ipos=eval(strVer.indexOf('msie '))
iVer= parseFloat( strVer.substring(ipos+5,strVer.length) )
}else{
iVer=parseFloat(strVer);
}
return (iVer>=v)?true:false;
}
function PreLoadImage(imgsrc){
if(browserver(4)){
var head1= new Image();
head1.src=imgsrc;
}
return head1;
}
function ShowButton(imagename,objectsrc){
if(browserver(4)) document.images[imagename].src=eval(objectsrc+".src");
};
function ShowQuad(loc1,loc2,loc3,loc4, objectsrc1,objectsrc2,objectsrc3,objectsrc4){
ShowButton(loc1, objectsrc1);
ShowButton(loc2, objectsrc2);
ShowButton(loc3, objectsrc3);
ShowButton(loc4, objectsrc4);
};
function clearform(){
var f=document.frm_Heart_Rate_Calculator;
f.txtage.value="";
f.txt_Maxmum_HeartRate.value="";
for(i=0;i<nZones;i++) f.txt[i].value="";
}
function makepos(n){
return (n<0)? -n: n;
}
function posnum(n){
if(isNaN(n) || n==0){
if(!justloaded) alert("Invalid entry");
justloaded=false;
return false;
}
return true;
}
var a_Heart_Rate_Factors = new Array(0,0.81,0.89,0.93,0.99,1.02,1.06)
function calculate_heart_zones(nLower,nUpper){
var sout=""
sout+="<br>" + (a_Heart_Rate_Factors[nLower]*100) + "-" + (a_Heart_Rate_Factors[nUpper]*100) + "%"
document.write(sout)
}
function calculate_heart_rate_levels(){
var s= " - ";
var b=" BPM";
var f=document.frm_Heart_Rate_Calculator;
var max=makepos(parseInt(f.txt_Maxmum_HeartRate.value));
if(posnum(max)){
f.txt_Maxmum_HeartRate.value=max;
f.txt[0].value="Low End"+parseInt(max*a_Heart_Rate_Factors[0])+b;
for(i=1;i<nZones;i++) f.txt[i].value=parseInt(max*a_Heart_Rate_Factors[i-1])+s+parseInt(max*a_Heart_Rate_Factors[i])+b;
}
else clearform();
}
// --->
</SCRIPT>
</HEAD>
<BODY>
<CENTER>
<table border=0>
<CENTER>
<img src="http://www.athleticimprovementcenter.com/aicblog/files/images/hrcalc.jpg">
</CENTER>
<br>
<br>
<CENTER>
<TABLE BORDER=0 CELLPADDING="5" width=958px>
<TR>
<TD ALIGN=CENTER>
<FORM NAME="frm_Heart_Rate_Calculator">
<TABLE BORDER=1 cellspacing=5>
<TR><TD ALIGN=RIGHT>
Lactate Threshold (HR)
</TD>
<TD width=200px>
<INPUT TYPE="text" NAME="txt_Maxmum_HeartRate" VALUE="" MAXLENGTH=3 SIZE=10 onChange="keyhandler(this,'max')" onkeyup="keyhandler(this,'max')" onFocus="this.select();this.focus();">
</TD>
<TD><INPUT TYPE="button" NAME="cmdlevels" VALUE="Calculate Training Zones for LTHR" onClick="calculate_heart_rate_levels();writecookie();">
</TD>
</TR>
<TR>
<TD valign=bottom align=right><b><u>Zone</u> </b></TD>
<TD valign=bottom align=center><b><u>Range BPM</u></b></TD>
<td valign=bottom align=left><b> <u>Purpose</u></b></td>
</TR>
<TR><TD valign=top ALIGN=RIGHT>
<font color=darkblue><b>Zone 1</b>
<script>
<!--
calculate_heart_zones(0,1)
//--->
</script>
</font>
</TD>
<TD ALIGN=left valign=top>
<INPUT TYPE="hidden" NAME="txt" VALUE="" SIZE=20><INPUT TYPE="text" NAME="txt" VALUE="Enter LTHR Above" SIZE=20>
</TD>
<TD valign=top ALIGN=left><font color=darkblue>Less than 81% of LTHR
</font>
</TD>
</TR>
<TR>
<TD valign=top ALIGN=RIGHT>
<font color=darkblue><b>Zone 2</b>
<script>
<!--
calculate_heart_zones(1,2)
//--->
</script>
</font>
</TD>
<TD ALIGN=left valign=top>
<INPUT TYPE="txt" NAME="txt" VALUE="" SIZE=20>
</TD>
<TD valign=top ALIGN=left><font color=darkblue>81% to 89% of LTHR
</font>
</TD>
</TR>
<TR>
<TD valign=top ALIGN=RIGHT>
<font color=darkgreen><b>Zone 3</b>
<script>
<!--
calculate_heart_zones(2,3)
//--->
</script>
</font>
</font>
</TD>
<TD ALIGN=left valign=top>
<INPUT TYPE="txt" NAME="txt" VALUE="" SIZE=20>
</TD>
<TD valign=top ALIGN=left><font color=darkgreen>90% to 93% of LTHR
</font>
</TD>
</TR>
<TR>
<TD valign=top ALIGN=RIGHT>
<font color=darkgreen><b>Zone 4</b>
<script>
<!--
calculate_heart_zones(3,4)
//--->
</script>
</font>
</TD>
<TD ALIGN=left valign=top>
<INPUT TYPE="txt" NAME="txt" VALUE="" SIZE=20>
</TD>
<TD ALIGN=left valign=top><font color=darkgreen>94% to 99% of LTHR</font></TD>
</TR>
<tr>
<TD valign=top ALIGN=RIGHT>
<font color=red><b>Zone 5a</b>
<script>
<!--
calculate_heart_zones(4,5)
//--->
</script>
</font>
</TD>
<TD ALIGN=left valign=top>
<INPUT TYPE="txt" NAME="txt" VALUE="" SIZE=20>
</TD>
<TD ALIGN=left valign=top><font color=red>100% to 102% of LTHR
</font>
</TD>
</tr>
<tr>
<TD valign=top ALIGN=RIGHT>
<font color=red><b>Zone 5b</b>
<script>
<!--
calculate_heart_zones(5,6)
//--->
</script>
</font>
</TD>
<TD ALIGN=left valign=top>
<INPUT TYPE="txt" NAME="txt" VALUE="" SIZE=20>
</TD>
<TD ALIGN=left valign=top><font color=red>103% to 106% of LTHR
</font>
</TD>
</tr>
<tr>
<TD valign=top ALIGN=RIGHT>
<font color=red><b>Zone 5c</b>
<script>
<!--
calculate_heart_zones(6,6)
//--->
</script>
</font>
</TD>
<TD ALIGN=left valign=top>
<INPUT TYPE="txt" NAME="txt" VALUE="" SIZE=20>
</TD>
<TD ALIGN=left valign=top><font color=red>More than 106% of LTHR
</font>
</TD>
</tr>
</TABLE>
</FORM>
</TD>
</TR>
And there is really no reason to be doing browser detection. None of the code I see there would have problems with any browser from MSIE 5 (1999) on forward.
And your cookies don't seem to work.
Oh...I see why...it's because your calculate_heart_rate() function gets an error.
But in any case, why do you need to save ANYTHING except the Lactose Threshold in the cookie? Everything else can be (and is) calculated from that.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
I really appreciate the response. As i mentioned I know very little about javascript and was trying to make something I found work for my needs.
There is possibly some code in there from things I stripped out from the front end that I was not using. The original code had a calculation based on other variables to create the zones that I did not need.
I am a bit confused since I am clueless. I added the code to specify the "greater" but doesn't seem to have changed anything.
I need two calculators... my plan was to get the first one working and then duplicate for the second. I don't want both to be available in the same window/page. I need to place one in a section for runners and the other in a seperate page for cycling.
I really appreciate you taking the time on this!
I think I found a way to trick the existing code to deal with the previous percentage used to calculate the second zone or getting close. I can put in a hidden field for before lets say zone 3. It then uses the previous zone highend, but hidden then I can calculate a unique lower end more or less for the zone. Probably not the best method, but seems to be working.
If the works the big issue or one I can't work around is change the lower end or first zone low end from "0" to some sort of text and then in the very last zone 5c how to insert text of "or greater" for the NaN default.
I was able to get the calculations to work with the hidden fields, so I am good there.
So the only remaining thing is how to change the output in the table after the calculation for bottom end and top end. Currnetly the Zone 1 gives you 0-(calculated HR) and I need to change the "0" to the text "less than"
For the highend zone, zone 5c the test NaN appears in two places left column and in the calculation (calculated HR)-NaN. I need the NaN to output text "Greater Than".