
//functions for ibu

function calcibu() {
  var v1;
  var v2;
  var og;
  var c;
  var ibu1;
  var ibu2;
  var ibu3;
  var ibu4;
  var ibu5;
  var ibu6;
  var f;

  v1 = document.ibu.bv.value;
  v2 = document.ibu.rv.value;
  og = document.ibu.og.value;

  if (v1<v2) {
    og = 1+(og-1)*(v2/v1);
  }

  f=0;
  if (document.ibu.type1[0].checked) {
    f=1;
  }

  if ((v2==0) || (og==0) || (document.ibu.amt1.value==0) || (document.ibu.aa1.value==0) || (document.ibu.t1.value==0)) {
    alert ('One or more fields have been omitted');
  }
  else {
    if (og > 1.050) {
      c=round(1+((og-1.050)/.2),2);
    }
    else {
      c=1;
    }
    
    ibu1 = round (((document.ibu.amt1.value * util(document.ibu.t1.value,document.ibu.type1[0].checked) * (document.ibu.aa1.value / 100) *  7489) / ( v2 * c)),2);
    ibu2 = round (((document.ibu.amt2.value * util(document.ibu.t2.value,document.ibu.type2[0].checked) * (document.ibu.aa2.value / 100) *  7489) / ( v2 * c)),2);
    ibu3 = round (((document.ibu.amt3.value * util(document.ibu.t3.value,document.ibu.type3[0].checked) * (document.ibu.aa3.value / 100) *  7489) / ( v2 * c)),2);
    ibu4 = round (((document.ibu.amt4.value * util(document.ibu.t4.value,document.ibu.type4[0].checked) * (document.ibu.aa4.value / 100) *  7489) / ( v2 * c)),2);
    ibu5 = round (((document.ibu.amt5.value * util(document.ibu.t5.value,document.ibu.type5[0].checked) * (document.ibu.aa5.value / 100) *  7489) / ( v2 * c)),2);
    ibu6 = round (((document.ibu.amt6.value * util(document.ibu.t6.value,document.ibu.type6[0].checked) * (document.ibu.aa6.value / 100) *  7489) / ( v2 * c)),2);

    document.ibu.ibu1.value=round(ibu1,1);
    document.ibu.ibu2.value=round(ibu2,1);
    document.ibu.ibu3.value=round(ibu3,1);
    document.ibu.ibu4.value=round(ibu4,1);
    document.ibu.ibu5.value=round(ibu5,1);
    document.ibu.ibu6.value=round(ibu6,1);
    document.ibu.ibutotal.value=round(ibu1+ibu2+ibu3+ibu4+ibu5+ibu6,1); 
  }
}

function round (value,places) {
  var answer;

  if (value > 0) {
    answer = Math.round(value * Math.pow(10,places))/Math.pow(10,places);
    return answer;
  }
  else {
    return '';
  }
}

function util (value,form) {
  var answer;

  if (form == 1) {
    if (value <= 10) { answer = .05; }
    else if (value <= 20) { answer = .14; }
    else if (value <= 30) { answer = .17; }
    else if (value <= 40) { answer = .21; }
    else if (value <= 50) { answer = .23; }
    else if (value <= 60) { answer = .25; }
    else if (value <= 70) { answer = .27; }
    else if (value <= 80) { answer = .29; }
    else { answer = .31; }
  }
  else {
    if (value <= 10) { answer = .06; }
    else if (value <= 20) { answer = .15; }
    else if (value <= 30) { answer = .19; }
    else if (value <= 40) { answer = .23; }
    else if (value <= 50) { answer = .25; }
    else if (value <= 60) { answer = .28; }
    else if (value <= 70) { answer = .30; }
    else if (value <= 80) { answer = .32; }
    else { answer = .34; }
  }

  return answer;
}

function resetibu() {
  document.ibu.bv.value = '5';
  document.ibu.rv.value = '5';
  document.ibu.og.value = '1.050';

  document.ibu.amt1.value = '1';
  document.ibu.aa1.value = '8';
  document.ibu.t1.value = '60';
  document.ibu.ibu1.value='';
  document.ibu.type1[1].checked=true;

  document.ibu.amt2.value = '';
  document.ibu.aa2.value = '';
  document.ibu.t2.value = '';
  document.ibu.ibu2.value='';
  document.ibu.type2[1].checked=true;

  document.ibu.amt3.value = '';
  document.ibu.aa3.value = '';
  document.ibu.t3.value = '';
  document.ibu.ibu3.value='';
  document.ibu.type3[1].checked=true;

  document.ibu.amt4.value = '';
  document.ibu.aa4.value = '';
  document.ibu.t4.value = '';
  document.ibu.ibu4.value='';
  document.ibu.type4[1].checked=true;

  document.ibu.amt5.value = '';
  document.ibu.aa5.value = '';
  document.ibu.t5.value = '';
  document.ibu.ibu5.value='';
  document.ibu.type5[1].checked=true;

  document.ibu.amt6.value = '';
  document.ibu.aa6.value = '';
  document.ibu.t6.value = '';
  document.ibu.ibu6.value='';
  document.ibu.type6[1].checked=true;

  document.ibu.ibutotal.value='';
}

function clearibu() {
  resetibu();
  document.ibu.bv.value = '';
  document.ibu.rv.value = '';
  document.ibu.og.value = '';
  document.ibu.amt1.value = '';
  document.ibu.aa1.value = '';
  document.ibu.t1.value = '';
}

//functions for infusion
function calcinfusion1()
{
  var Mm = document.infusion1.grain.value;   //mass of malt
  var Mw = document.infusion1.water.value * 2.08;  //Mass of strike water in lbs
  var Tf = document.infusion1.targettemp.value;    //target temperature
  var Tm = document.infusion1.mashtemp.value;      //starting temperature
  var Tw = document.infusion1.infusiontemp.value;  //Temperature of infusion water

  if ((Mm==0) || (Mw==0) || (Tf==0) || (Tm==0)) {
    alert ('Must enter all values');
  }
  else {
    document.infusion1.infusionmass.value = Math.round( (.39*Mm+Mw)*(Tf-Tm)/(Tw-Tf) *10)/10;
  }

}

//functions for strike
function calcstrike()
{
  var Ma = document.strike.water.value * 2.08;  //Mass of strike water in lbs
  var Mm = document.strike.grain.value;   //Mass of malt
      var Tm = document.strike.graintemp.value;     //Temp of malt
  var Tf = document.strike.targettemp.value;    //Target temperature
  var TMt = document.strike.tunfactor.value;    //Mash Tun heat factor
  var Tw = document.strike.tunmass.value;       //Mash Tun mass
  

  if ((Ma*Mm*Tm*Tf) == 0)
  {
     alert("Water, grain, and temp fields are required.");
  }
  else
  {
     document.strike.watertemp.value = Math.round((Ma*Tf+(.39*Mm + TMt * Tw)*(Tf-Tm))/Ma);
  }

//  adding .3 as the thermal mass in Promash gives us a strike temp of 149
//  How to add this to the formula? we need to make the numerator larger by an amount of 312.
//  so, the thermal mass * the temp difference (132-80) only gives us about 15.6 - this should
//  be * 20 (weight of tun?) to get 


// the strike water temp is calculated by taking
//    the water mass in pounds
//      times the target temp
//      minus the quantity of
//       (.39 times the malt mass) times the temp difference (grain temp - target temp)
//    all divided by the water mass.
//
// to figure in the mash tun thermal mass, we need to allow for the amount of heat necessary to
// increase the temp of the mash tun to the target temp
}

//functions for ac

function aex (spg){
  var answer;

  answer = round(2.58333 * (spg-1) * Math.pow(10,2) 
           - 2.14657 * Math.pow(spg-1,2) * Math.pow(10,2)
           - 1.96640 * Math.pow(spg-1,3)
           + 1.31634 * Math.pow(spg-1,4) * Math.pow(10,3)
           - 5.08755 * Math.pow(spg-1,5) * Math.pow(10,3)
           + 1.02848 * Math.pow(spg-1,6) * Math.pow(10,4)
           - 1.09709 * Math.pow(spg-1,7) * Math.pow(10,4)
           + 4.82488 * Math.pow(spg-1,8) * Math.pow(10,3), 2);
  return answer;
}

function spgae (re, al, t) {
  var answer;
  var spgre = round((1 + 0.387217 * re / Math.pow(10,2)
              + 0.120700 * Math.pow(re,2) / Math.pow(10,4)
              + 0.120485 * Math.pow(re,3) / Math.pow(10,6)
              - 0.237056 * Math.pow(re,4) / Math.pow(10,8)
              + 0.445367 * Math.pow(re,5) / Math.pow(10,10)
              - 0.515428 * Math.pow(re,6) / Math.pow(10,12)
              + 0.310578 * Math.pow(re,7) / Math.pow(10,14)
              - 0.766564 * Math.pow(re,8) / Math.pow(10,17)), 5);

  var spgal = round((1 - 0.191706 * al / Math.pow(10,2)
              + 0.269886 * Math.pow(al,2) / Math.pow(10,4)
              + 0.768683 * Math.pow(al,3) / Math.pow(10,6)
              - 0.880478 * Math.pow(al,4) / Math.pow(10,7)
              + 0.234733 * Math.pow(al,5) / Math.pow(10,8)
              - 0.294948 * Math.pow(al,6) / Math.pow(10,10)
              + 0.183199 * Math.pow(al,7) / Math.pow(10,12)
              - 0.454977 * Math.pow(al,8) / Math.pow(10,15)), 5);

  var spg = round(spgre + spgal - 1, 5);
  var ae = aex(spg);

  if (t == 1) { answer = spg; }
  if (t == 2) { answer = ae; }

  return answer;
}

function rdfx(og, ae) {
  var answer;
//estimate real extract
  var re = 0.19 * og + 0.81 * ae;
  var al = 100 * (re - og) / (1.0665 * og - 206.65);
//correct real extract
  re = re + 0.81 * (ae - spgae(re, al, 2));
  al = 100 * (re - og) / (1.0665 * og - 206.65);
//calculate RDF
  answer = round(206.65 / (2.0665 + re / al), 1);
  return answer;
}


function CalcAC () {
  var OG=document.ac.og.value;
  var FG=document.ac.fg.value;
  var OGP='0';
  var FGP='0';
  var RDF='0';
  var RE='0';
  var ALW='0';
  var ALV='0';
  var CAL='0';

  if (OG=='' || FG=='') {
    alert ('You must enter OG & FG');
    return;
  }

  OGP = aex(OG);
  FGP = aex(FG);
  RDF = rdfx(OGP,FGP);
  RE  = round(OGP*(100-RDF)/(100-0.005161*OGP*RDF),2);
  ALW = round(0.4839*OGP*RDF/(100-0.005161*OGP*RDF),2);
  ALV = round(ALW*FG/0.7907,2);
  CAL = round((6.9*ALW+4*(RE-0.1))*FG*3.55,0);

  document.ac.ogp.value = OGP;
  document.ac.fgp.value = FGP;
  document.ac.alw.value = ALW;
  document.ac.alv.value = ALV;
  document.ac.cal.value = CAL;
}

function ResetAC() {
  document.ac.og.value = '';
  document.ac.fg.value = '';
  document.ac.ogp.value = '';
  document.ac.fgp.value = '';
  document.ac.alw.value = '';
  document.ac.alv.value = '';
  document.ac.cal.value = '';
}


//functions for forcecarb
function calcFC() {
  var t = document.frm.temp.value;
  var v = document.frm.vol.value;
  var p = -16.6999 - 0.0101059 * t + 0.00116512 * t * t + 0.173354 * t * v + 4.24267 * v - 0.0684226 * v * v;

  if ((t==0) || (v==0)) {
    alert ('You must enter temperature and volumes CO2');
  }
  else {
    document.frm.pressure.value=round(p,5);
  }
}

//functions for decoct
function calcdecoct()
{
  var Mw = document.decoct.water.value * 2.08;  //Mass of strike water in lbs
  var Mm = document.decoct.grain.value;         //Mass of malt
  var Tm = document.decoct.mashtemp.value;      //Temperature of mash
  var Tf = document.decoct.targettemp.value;    //Target temperature
  var Td = document.decoct.decocttemp.value;    //Temperature of decoction
  var TMt = document.decoct.tunfactor.value;    //Mash Tun heat factor
  var Tw = document.decoct.tunmass.value;       //Mash Tun mass
  
  if ((Mw*Mm*Tm*Tf*Td) == 0)
  {
     alert("Water, grain, and temp fields are required.");
  }
  else
  {
    document.decoct.decoctmass.value = Math.round( (.4*Mm+Mw+TMt*Tw)/(1+((Tf-Td)/(Tm-Tf))) *10)/10;
  }

}

//functions for carb (priming)
function calcprime() {
        var Temp = document.CarbCalc.Temp.value;
        var DesiredLevel = document.CarbCalc.CarbLevel.value;
        var BatchSize = document.CarbCalc.Size.value;
        var Result;


        if ((Temp > 0) && (DesiredLevel > 0) && (BatchSize > 0)) {
            Result = 15.195 * BatchSize * (DesiredLevel - 3.0378 + (0.050062 * Temp) - (0.00026555 * Temp * Temp)); 
            document.CarbCalc.Rate1.value = round(Result, 0);
            document.CarbCalc.Rate2.value = round((Result / 28.375), 1);
        }
        else {
            alert('Fill in all values.');
        }

}

//functions for bac
var pctbodyweight = 0;
var weight = 0;
var amount = 0;
var percent = 0;
var time = 0;

function setSex(sex) {
  if(sex == 'm') {
    pctbodyweight = .58;
  }
  else if(sex = 'f') {
    pctbodyweight = .49;
  }
}

function CalcBAC() {
  weight = document.bac.bodyweight.value;
  amount = document.bac.amount.value;
  percent = document.bac.percent.value;
  time = document.bac.time.value;

  if (pctbodyweight==0) {
    alert ('You must select a gender');
  }
  else if(weight != 0 && amount != 0 && percent != 0 && time != 0) {
    var wk = weight / 2.2046;
    var bwml = wk * pctbodyweight * 1000;
    var aw = 23.36 / bwml;
    var ab = aw * .806 * 100;
    var baclevel = amount * (percent / 100) * ab;
    var fbaclevel = baclevel - (.012 * time);
    document.bac.bac.value = Math.round(fbaclevel*1000)/1000;
    if(fbaclevel >= .08) {
      document.bac.drunk.value = 'Yes';
    }
    else {
      document.bac.drunk.value = 'No';
    }
  }
  else {
    alert('Please fill in all Input fields');
  }
}

function convtemp () {
  var Cin = document.temp1.Cin.value;
  var Fin = document.temp1.Fin.value;
  var Cout;
  var Fout;
  
  if (Cin == '' && Fin == '') {
    alert('You must enter a value in one of the text boxes on the left');
  }
  else {
    if (Cin != '') {
      Cin = Cin * 1.8 + 32;
      if (Cin < 0) { Fout = -round(-Cin,1); }
      else { Fout = round(Cin, 1);}
      document.temp1.Fout.value = Fout;
    } 

    if (Fin != '') {
	Fin=(Fin-32)/1.8;
      if (Fin < 0) { Cout = -round(-Fin,1); }
      else { Cout = round(Fin,1); }
      document.temp1.Cout.value = Cout;
    }
  }
  
}

function cleartemp() {
  document.temp1.Cin.value='';
  document.temp1.Cout.value='';
  document.temp1.Fin.value='';
  document.temp1.Fout.value='';

}

