//*************************************************************
// Name: USMC PFT Score Calculator
// Written by: Dave Bachta '02
// Based on the PRT Calculator by Michael Gawlas '98
//*************************************************************

// Calculate score for curl-ups for either sex (same standards)
function calcCurl(Curl) {
if (Curl < 40)
 return(-1);
 else if (Curl > 100)
    Curl = 100;
 
 return(Curl);
}

//*************************************************************
// Calculate pull-ups score for Males
// June/July 1998 Standards, same as 25 June 1996 Standards
function calcPull_M(Pull) {
if (Pull < 3)
 return(-1);
if (Pull > 20)
 Pull = 20;

  return(5*Pull);
}

//*************************************************************
// Calculate Flexed arm Hang score for Females
function calcHang_F(Time) {

if (Time < 15)
    return (-1);
 if (Time <= 40 && Time >= 15)
    return (Time);
    else if (Time > 40 && Time <= 70)
        return (40 + (2*(Time - 40)));
            else if (Time > 70)
                return (100);
                return (40 + (2*(Time - 40)));
}

//*************************************************************
// Calculate score for run for Males

function calcRun_M(Run_m, Run_s) {
var seconds = (60*Run_m)+1*Run_s;
var mod = seconds%10;
        if (mod == 0)
                mod = 10;
seconds = (10 - mod) + seconds;
if (seconds > 1680)
    return (-1);
        else if (seconds <= 1080)
        return(100);
                else if (seconds > 1080 && seconds <= 1680)
                return (100 - (seconds - 1080)/10);
}

//*************************************************************
// Calculate score for run for Females
// June/July 1998 Standards, changed from 25 June 1996 Standards
function calcRun_F(Run_m, Run_s) {
var seconds = (60*Run_m)+1*Run_s;
var mod = seconds%10;
        if (mod == 0)
                mod = 10;
seconds = (10 - mod) + seconds;
if (seconds > 1860)
    return (-1);
        else if (seconds <= 1260)
        return(100);
                else if (seconds > 1260 && seconds <= 1860)
                return (100 - (seconds - 1260)/10);
}

//*************************************************************
// Decide which run fxns to call
function calcRun(Sex, Run_m, Run_s) {
 if (Sex == 1)
 return(calcRun_M(Run_m, Run_s));
 else
 return(calcRun_F(Run_m, Run_s));
}

//*************************************************************
// Decide which pull fxns to call
function calcPull(Sex, Pull) {
 if (Sex == 1)
 return(calcPull_M(Pull));
 else
 return(calcHang_F(Pull));
}

//*************************************************************


// Main calculation function
function calcScore (Sex, Pull, Curl, Run_min, Run_sec) {
var Pull_prcent, Run_prcent, Curl_prcent;
 if (Pull == "")
   Pull = 0;
 if (Curl == "")
   Curl = 0;
 if (Run_min == "")
   Run_min = 30;
 if (Run_sec == "")
   Run_sec = 0;

 Run_prcent = calcRun(Sex, Run_min, Run_sec);
 Curl_prcent = calcCurl(Curl);
 Pull_prcent = calcPull(Sex, Pull);
 

 if(Pull_prcent == -1 || Run_prcent == -1 || Curl_prcent == -1)
		document.getElementById('pft_result').innerHTML="bad";
 var total = 1*Run_prcent + 1*Pull_prcent + 1*Curl_prcent;
 if (total < 135)
        total = 0;
if (Sex == 1) { document.getElementById('pft_result_male').innerHTML="PFT Score: " + total; } else { document.getElementById('pft_result_female').innerHTML=total; }

if (total > 280) {document.getElementById('comment_result').innerHTML="Very good. A score above 280 is highly sought after and gets the maximum number of points for quicker promotions."; }
if (total < 200) {document.getElementById('comment_result').innerHTML="A score bellow 200 is considered Third Class, the lowest of the three."; }
}

//PFT Calc eof

function maxrepcalc()
{
var lifted = document.maxrep.lifted.value;
var reps = document.maxrep.reps.options[document.maxrep.reps.selectedIndex].value;
var maxrep_result = Math.round(lifted / (1.0278 - (.0278 * reps)));
document.getElementById('maxrep_result').innerHTML="Your max rep:" + maxrep_result;


}


function ClearForm(form){

    form.weight.value = "";
    form.height.value = "";
    form.bmi.value = "";
    form.my_comment.value = "";

}

function bmi(weight, height) {
bmindx= Math.round(weight * 703)/(height*height)
return bmindx;
}

function checkform(form) {

       if (form.weight.value==null||form.weight.value.length==0 || form.height.value==null||form.height.value.length==0){
                document.getElementById('bmi_result').innerHTML="<span class='error'>Please complete form.</span>";
            return false;
       }

       else if (parseFloat(form.height.value) <= 0||
                parseFloat(form.height.value) >=500||
                parseFloat(form.weight.value) <= 0||
                parseFloat(form.weight.value) >=500){
                document.getElementById('bmi_result').innerHTML="<span class='error'>Enter realistic values please!</span>";
                ClearForm(form);
                return false;
       }
       return true;

}

function computeform(form) {

       if (checkform(form)) {

       yourbmi=Math.round(bmi(form.weight.value, form.height.value));
       document.getElementById('bmi_result').innerHTML='Your BMI: ' + yourbmi;

	
       if (yourbmi >30) {
          document.getElementById('comment_result').innerHTML="<h2>Your BMI Score:</h2>By BMI standards, and when compared to the rest of American population, you are considered 'obese'. ";
       }

       else if (yourbmi >25 && yourbmi <=30) {
         document.getElementById('comment_result').innerHTML="<h2>Your BMI Score:</h2>By BMI standards, and when compared to the rest of American population, you are considered 'overweight'.  However, you may also simply be 'strong built', or muscle bound, and in-fact very healthy. BMI doesn't consider any factors aside from height and weight, read bellow for more information.";
       }

       else if (yourbmi >=18.5 && yourbmi <=25) {
          document.getElementById('comment_result').innerHTML="<h2>Your BMI Score:</h2>You are in the sought-after 'Normal' range, congratulations, and keep it up!";
       }

       else if (yourbmi <18.5) {
          document.getElementById('comment_result').innerHTML="<h2>Your BMI Score:</h2>By BMI standards, you're considered 'underweight.'";
       }

       }
       return;
}