
/*********************************
* Statistics calculation functions
**********************************/
function BA(f) {
  var OAB = 0;
  var BA = 0;
  var BAstr;
  OAB = f.appearances.value - f.walks.value - f.hitsbypitches.value - f.sacrifices.value;
  BA = f.hits.value/OAB;
  f.atbats.value = OAB;
  BAstr = BA.toString();
  f.ba.value = padr(BAstr.substring(0,5),5);
}

function ERA(f) {
  var ERA = 0;
  var ERAstr;
  ERA = (f.earnedruns.value / f.inningspitched.value) * f.numberofinnings.value;
  ERAstr = ERA.toString();
  f.era.value = padr(ERAstr.substring(0,5),5);
}

function SP(f) {
  var SP = 0;
  var TB = 0;
  var SPstr;
  TB = eval(f.singles.value) + eval(2 * f.doubles.value) + eval(3 * f.triples.value) + eval(4 * f.homeruns.value);
  SP = TB / f.officialatbats.value;
  SPstr = SP.toString();
  TBstr = TB.toString();
  f.sp.value = padr(SPstr.substring(0,5),5);
  f.tb.value = TBstr.substring(0,5);
}

function Verify(cell) {
  if (!((cell.value >= 0) && (cell.value < 100000))) {
    alert("Your entry is invalid.  Letters, symbols or large numbers are not accepted.");
    cell.value = "";
    cell.focus();
  }
  return;
}

function padr(NUMstr) {
  finallength=5;
  if (NUMstr.length < 2 && InStr(NUMstr, ".") == -1) { NUMstr += "."; }
  if (NUMstr.length > 1 && InStr(NUMstr, ".") == -1) { NUMstr += "."; finallength=6;}
  while (NUMstr.length < finallength) {
    NUMstr += "0";
  }
  return NUMstr;
}

function InStr(strSearch, charSearchFor) {
  // Returns the first location a substring (charSearchStr) was found in
  // the string strSearch.  If the character is not found, -1 is returned.
  for (i=0; i < strSearch.length; i++) {
    if (charSearchFor == strSearch.substr(i, 1)) { return i; }
  }
  return -1;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
