﻿
    function mainCalc(){
this.vn="histidine|isoleucine|leucine|lysine|methionine+cysteine|phenylalanine+tyrosine|threonine|tryptophan|valine";
this.vp="17|23|52|47|23|41|24|6|29";
this.vpg="1.7|2.3|5.2|4.7|2.3|4.1|2.4|0.6|2.9"; 
this.fv="hist|isoleuc|leuci|lysine|methocyste|phenyltyro|threonine|tryptophan|valine";
this.dpg="Milk - 95|Casein - 99|Whey - 99|egg white - 98|Soy - 95|Soy - 96|Soy - 97|Soy - 98|Collagen - 95|Gelatin - 95|Pea flour - 69|Kidney beans - 58|Pinto beans- 63|Whole wheat- 40";
this.mn="";this.sv="";this.rb="0";
    }
 
 cf = new mainCalc(); 
   
    function calcStart(){
 if(cf.rb!="0") cLoopT();
    }
    
    function sSz(tg){
cf.rb = tg; cLoopT();   
    }
    
        function rsb(){
cf.rb = "0";   
document.getElementById("pdcaas2").innerHTML = "&nbsp;";
document.getElementById("pdcaas").innerHTML = "&nbsp;";
document.getElementById("caps").innerHTML = "&nbsp;";
    }
    
        function calcCA(){
cv = 0; nv="";
gp = new String(document.calc.gop.value);
pd = new String(document.getElementById("pdcaas2").innerHTML);        
if((pd.length==0)||(pd=="&nbsp;")) alert("Please calculate your PDCAAS score to get Corrected Amount of Protein per Serving");
if((pd.length==0)||(pd=="&nbsp;")) cv = 1;
if((gp.length==0)&&(cv==0)) alert("Please enter a value in the Total Grams of Protein Per Serving box provided");
if((gp.length==0)&&(cv==0)) cv = 1;
if((cv==0)&&(isNaN(gp)==true)) alert("Please enter only numbers in the Total Grams of Protein Field."); 
if((cv==0)&&(isNaN(gp)==true)) document.calc.gop.value = "";
if(cv==0) nv = new Number(document.calc.gop.value)
if((cv==0)&&(pd!="100")) pd = (pd.length==1) ? ".0" + pd : "." + pd;
if((cv==0)&&(pd!="100")) nt = new Number(pd);
if((cv==0)&&(pd=="100")) nt = 1;
if(cv==0) ttv = nv * nt;
document.getElementById("caps").innerHTML = (cv==0) ? clc(ttv,"1"): "";
        }
    
    function cLoopT(){
if(lTst()=="0") cLoop();
//if(lTst()=="0") alert(lTst())
if(lTst()!="0") document.getElementById("pdcaas2").innerHTML = "&nbsp;";
if(lTst()!="0") document.getElementById("pdcaas").innerHTML = "&nbsp;";
    }
    
        function calcLoopT(){
 if(lTst()=="0") cLoop();if(lTst()!="0") alert(lTst());   
    }
    
    function lTst(){
 dft = document.calc.digestfactor.value; rs = "0"; rts="";
 if(cf.rb=="0") rts = "Please select a unit in the radio buttons provided."
 if(dft=="0") rs = rts + " Please select a Select a Digestability Factor using the drop-down menu provided."
 return rs;
    }
    
    function cLoop(){
 df = document.calc.digestfactor.value; gs = (cf.rb=="100") ? cf.vpg : cf.vp;
 dv= cf.fv.toString().split("|"); vp= gs.toString().split("|");as=dv.length;
 i=0;dl="";rdg="";
		while(i<as){
	fv = eval("document.calc." + dv[i] + ".value");
	fv = (fv.toString().length==0||isNaN(fv)==true) ? 0 : fv;
	rvs = fv / vp[i];
	//alert(fv + "  " + vp[i] + " " + rvs)
	//rv = clc(rv,"");
	rv = rvs;	edl = (as - 1);	adv= (edl==i) ? "" : "|";
	dl = dl + rv + adv;	i++;rdg = rdg + rvs + adv;
		}  
ms = new String(dl);lar = ms.split("|");
lar = lar.sort(sortNumbers);ln = new Number(lar[0]);
rtv = (ln<1)? ln : 100;
rtv = (rtv * df)
ctv =(lTst()=="0") ? clc(rtv,"") : "&nbsp;";
if(rtv==100) ctv = "100"
if(ln>1) ctv = 100; 
document.getElementById("pdcaas2").innerHTML = ctv;
document.getElementById("pdcaas").innerHTML = ctv;

    }
    
    function sortNumbers(a, b) { return a - b} 
    
       function clc(pr,tg){
 prs = new String(pr); av = prs.split(".");rv="0"; 
 mst = (av.length==2) ? av[1] : "00";
 mst = new String(mst);if(mst.toString().length==2) rv = mst;
 if(mst.toString().length==1) rv = (mst=="0") ? 0 : mst + "0";
 if(mst.toString().length>2) rv= mst.charAt(0) + mst.charAt(1) + "." + mst.charAt(2);
 if(mst.toString().length>2) rv= Math.round(rv);
 if(tg=="1") rv = av[0] + "." + mst.charAt(0); 
 if(rv=="00") rv = "0";
 
 return rv;
   }
   
   
   
