<!--
var uString = ["oz", "fl&nbsp;oz", "lb"]
var f
var iList = new Array()
var sList = ["cl_sp","cl_sm","alk_sp","alk_sm","ca_sp","ca_sm","stab_sp","stab_sm","ph_sp","ph_sm"]
var amtList = ["cl_amt","ph_amt","alk_amt","ca_amt","stab_amt"]
var imgList = new Array()
var dimList = ["w","l","w2","ds","dd"]
var pV

function keyChk(e) {
  if (!e) var e = window.event;
  if (e.keyCode == 13) return false;
}

function f_init() {
  textfields = document.getElementsByTagName('input');
  for (i=0, count=textfields.length; i<count; i++) {
    if (textfields[i].type == 'text') {
      textfields[i].onkeypress = keyChk
    }
  }

  f = document.forms[0]
  for (i=0;i<dimList.length;i++) {
    obj = eval("f."+dimList[i])
    obj.value = ''
  }
  pV = cookieGet("pVol")
  if (pV != "") f.p_vol.value=pV  
  if (pV > 0) upd_all();
  else {
    iChk(f.cl); iChk(f.ph); iChk(f.alk); iChk(f.ca); iChk(f.st)
  }
  imgList = document.getElementsByName("shape");
  pv_hide( rVal(f.shp))
  pInd = cookieGet("pIndices")
  if (pInd != "") pList = pInd.split(":")
  for (i=0;i<sList.length;i++) {
    ref = "f."+sList[i]
    eval(ref).selectedIndex = (pInd)?pList[i]:0
  }
  iList.push(f.ca,f.cl,f.st,f.alk,f.ph)
}

function cookieGet(str) {
  allCookies = document.cookie
  p = allCookies.indexOf(str+"=")
  if (p !=-1) {
    p = p+str.length+1
    e = allCookies.indexOf(";",p); if (e == -1) e = allCookies.length
    return allCookies.substring(p,e);
  } else return ""
}

function clrCookies() {
  document.cookie = "pVol="
  f.p_vol.value = ''
  document.cookie = "pIndices="
  for (i=0;i<sList.length;i++) {
    ref = eval("f."+sList[i])
    ref.selectedIndex = -1
    hide(ref)
  }
  for (i=0;i<iList.length;i++) iList[i].value=""
}

function pv_upd() {
  if (iChk(f.p_vol)>1) upd_all();
  else {
    for (i=0;i<sList.length;i++) {
      eval("f."+sList[i]).className = "selhid"
    }
    for (i=0;i<amtList.length;i++) dObj(amtList[i]).innerHTML="";
  }
}

function upd_all () {
  if (f.p_vol.value =='') ErrorFlag(f.p_vol,false);
  upd_Cl(); upd_pH(); upd_alk(); upd_ca(); upd_stab()
  if (f.p_vol.value !=  pV)
    document.cookie = "pVol="+f.p_vol.value+"; max-age="+(3600*24*60)
}

function upd_Cl() {
  aObj = dObj("cl_amt")
  if (iChk(f.cl)==2) {
    delta = 5.0-f.cl.value
    selObj = selInit(f.cl.value, delta, f.cl_sm,f.cl_sp)
    if (iChk(f.p_vol,true)) {
      dose = [1.3,2,1.75,10.7,3.8,1.5,2.4, 2.6,2.4]
      units = [0,0,0,1,0,0,0, 0,0]
      upd_line(selObj, aObj, delta, dose, units)
    }
  } else hide3(aObj,f.cl_sm,f.cl_sp)
}

function upd_pH() {
  aObj = dObj("ph_amt")
  if (iChk(f.ph)==2) {
    delta = 7.5-f.ph.value
    selObj = selInit(f.ph.value, delta, f.ph_sm,f.ph_sp)
    if (iChk(f.p_vol,true)) {
      dose = [30,27.5, 60,80,20]
      units = [0,1, 1,0,0]
      upd_line(selObj, aObj, delta, dose, units)
    }
  } else hide3(aObj,f.ph_sm,f.ph_sp)
}

function upd_alk() {
  aObj = dObj("alk_amt")
  if (iChk(f.alk)==2) {
    delta = 100-f.alk.value
    selObj = selInit(f.alk.value, delta, f.alk_sm,f.alk_sp)
    if (iChk(f.p_vol,true)) {
      dose = [2.24,1.4,2.0, 2.6,3.36]
      units = [0,0,0, 1,0]
      upd_line(selObj, aObj, delta, dose, units)
    }
  } else hide3(aObj,f.alk_sm,f.alk_sp)
}

function upd_ca() {
  aObj = dObj("ca_amt")
  if (iChk(f.ca)==2) {
    delta = 300-f.ca.value
    inStr =  (delta < 0)?'':f.ca.value
    selObj = selInit(inStr, delta, f.ca_sm,f.ca_sp)
    if (iChk(f.p_vol,true)) {
      dose = [1.44,1.92]
      units = [0,0]
      upd_line(selObj, aObj, delta, dose, units)
    }
  } else hide3(aObj,f.ca_sm,f.ca_sp)}

function upd_stab() {
  aObj = dObj("stab_amt")
  if (iChk(f.st)==2) {
    delta = 50-f.st.value
    inStr =  (delta < 0)?'':f.st.value
    selObj = selInit(inStr, delta, f.stab_sm,f.stab_sp)
    if (iChk(f.p_vol,true)) {
      dose = [1.3,2.56]
      units = [0,0]
      upd_line(selObj, aObj, delta, dose, units)
    }  
  } else hide3(aObj,f.stab_sm,f.stab_sp)
}

function dObj(idStr) {
 return document.getElementById(idStr)
}

function selInit(inp, delta, selM, selP) {
  if (inp == '' || delta == 0 || !iChk(f.p_vol,true)) {
    hide(selP); hide(selM)
    return null
  } else if (delta<0) {
    hide(selP); unhide(selM)
    return selM
  } else {
    hide(selM); unhide(selP)
    return selP
  }
}

function upd_line(selObj, aObj, delta, dose, units) {
  if (delta == 0) {
    aObj.innerHTML = "0.0"
  } else {
    if (selChk(selObj)) {
      ix = selObj.value
      amtCalc(aObj, dose[ix], f.p_vol.value, delta, units[ix])
      cookie_upd()
    } else {
      aObj.innerHTML = "<i>Cannot decrease</i>"
    };
  }
}

function iChk(iObj) {
  emptyOK = !(arguments.length>1 && arguments[1])
  iObj.value = iObj.value.replace(/^\s+|\s+$/g,'')
  ival = parseFloat(iObj.value)
  istr = iObj.value.replace(/\.0*$/,"") // if integer, lose .000
  istr = istr.replace(/(\.[1-9]*)0*$/,"$1") // else lose trailing zeroes
  if (iObj.value=='' && emptyOK) {
    ErrorFlag(iObj,false); return 1;
  }
  if (String(ival)==istr && ival > 0) {
    ErrorFlag(iObj,false); return 2;
  } else {
    ErrorFlag(iObj,true); return 0;
  }
}

function selChk(sObj) {
  if (sObj && sObj.length > 0) {
    return (sObj.selectedIndex >= 0)
  } else return false;
}

function rVal(rColl) {
  sel = -1
  for (i=0; i<rColl.length;i++) {
    if (rColl[i].checked == true) sel = rColl[i].value
  }
  return sel
}

function dimChk() {
  dimsOK = (iChk(f.w)==2)
  dimsOK &= (iChk(f.ds)==2)
  dimsOK &= (iChk(f.dd)==2)
  shape = rVal(f.shp) 
  if (!(shape==1 || shape==3)) dimsOK &= (iChk(f.l)==2)
  if (shape==5) dimsOK &= (iChk(f.w2)==2)
  if (dimsOK) {
    area = -1, w = Number(f.w.value), l = Number(f.l.value)
    switch(Number(shape)) {
     case 1:
      area = w*w
      break;
     case 2:
      area = w*f.l.value
      break;
     case 3:
      area = Math.PI*w*w/4
      break;
     case 4:
      area = Math.PI*w*l/4
      break;
     case 5:
      area = (w+Number(f.w2.value))*l*0.45
    }
    davg = (Number(f.ds.value) + Number(f.dd.value))/2
    f.p_vol.value = Math.round(area * davg * 7.5)
    ErrorFlag(f.p_vol,false)
    upd_all()
  }
}

function amtCalc(amtObj, dose, pVol, delta, unit) {
  amt = dose * (pVol/10000) * Math.abs(delta)
  amt = Math.round(amt*100)/100
  switch (unit) {
   case 0:
    amt1 = Math.floor((amt+0.1)/16); amt2 = Math.max(amt-amt1*16,0)
    u1 = "lb"; u2 = "oz"
    break;
   case 1:
    amt1 = Math.floor((amt+.1)/128); amt2 = Math.max(amt-amt1*128,0)
    u1 = "gal"; u2 = "fl oz"
    break;
   default:
    amt1 = 0; amt2=0
  }
  amt2 = Math.round(amt2*10)/10
  amtStr = (amt1>0)? (amt1+"\xa0"+u1+"\xa0"):""
  if (amt2!=0) amtStr += amt2+"\xa0"+u2
  amtObj.innerHTML = amtStr
}

function cookie_upd() {
  str = '';
  for (i=0;i<sList.length;i++) {
    ref = "f."+sList[i]+".selectedIndex"
    str += ((str.length>0)?":":"") + eval(ref)
  }
  document.cookie = "pIndices="+str+"; max-age="+(3600*24*60)
}

function ErrorFlag(eObj,err) {
  if (eObj) {
    if (err) {
      if( eObj.className.lastIndexOf('Err')==-1) eObj.className += 'Err'
    }
    else eObj.className = eObj.className.replace(/Err/,"")
  }
}

function hide(sObj) {
  sObj.className = "selhid"
}

function unhide(sObj) {
  if (sObj.length>0) {
    sObj.className = "sel"
  } else sObj.className = "selhid";
}

function hide3(aObj,sel1Obj,sel2Obj) {
  aObj.innerHTML = '';
  hide(sel1Obj); hide(sel2Obj)
}

function pv_rad(rObj) {
  pv_hide(rObj.value)
  dimChk()
}

function pv_hide(objVal) {
  for (i=0;i<imgList.length;i++) imgList[i].className = (objVal==i+1)?"iVis":"iHid";
  dObj("len").className = (objVal==1 || objVal==3)? "opt_hid":"opt"
  if (objVal==1||objVal==3) f.l.value=''; f.w2.value=''
  dObj("w2").className = (objVal==5)? "opt":"opt_hid"
  if (!(objVal==5)) f.w2.value=''
}

function ozToLb() {
  dObj("ozlb").innerHTML = (iChk(f.cOz)==2)?((Math.round(10*f.cOz.value/16)/10)+" lb"):""
}

function ozToGal() {
  dObj("floz").innerHTML = (iChk(f.cFl)==2)?((Math.round(10*f.cFl.value/128)/10)+" gal"):""
}

//-->
