  var daystoexpire = 90;      // Days until cookie expires
 
  //New cookie for NMR domain
  var nmrcookie = new Cookie(document, "nmr", daystoexpire*24); 

  //Load Cookies and refresh form
  if (nmrcookie.load()) FormLoad(document.samsched_table, nmrcookie);

  // Submit Button Event Handler - Store cookie before subitting.
  document.samsched_table.onsubmit = Function("FormStore(document.samsched_table, nmrcookie);");

  // Store Cookie Button Event Handler
  document.samsched_table.StoreCookie.onclick = Function("FormStore(document.samsched_table, nmrcookie); alert('Cookie Stored');");

  // Load Cookie Button Event Handler
  document.samsched_table.LoadCookie.onclick = Function("FormLoad(document.samsched_table, nmrcookie);");

  // Event handlers for change in dimension radio control: store cookie and reload document with new dimension as query
  document.samsched_table.dimension[0].onclick = Function("FormStore(document.samsched_table, nmrcookie); document.location.replace('default.asp?dimension=2')");
  document.samsched_table.dimension[1].onclick = Function("FormStore(document.samsched_table, nmrcookie); document.location.replace('default.asp?dimension=3')");
  //document.samsched_table.dimension[2].onclick = Function("FormStore(document.samsched_table, nmrcookie); document.location.replace('default.asp?dimension=4')");


  // Event Handlers for Pre-defined Macro Parameter Sets
     
  // on change of solvent flag, recalculate all R2, LB and maxpoint values
  document.samsched_table.solvent[0].onclick = Function("RecalcParms();");
  document.samsched_table.solvent[1].onclick = Function("RecalcParms();");
  
  // on change MW, recalculate all R2, LB, maxpoint values, as well as set all SF & Decay-factor to defaults
  document.samsched_table.molecular_weight.onchange = Function("RecalcParms(); RecalcFactors()");
  
  // on change of SW, recalculate appropriate R2, LB and maxpoint
  document.samsched_table.sw_t0.onchange = Function("RecalcT0();");
  document.samsched_table.sw_t1.onchange = Function("RecalcT1();");
  document.samsched_table.sw_t2.onchange = Function("RecalcT2();");
  
  // on change of Constant time checkbox -> if checked, df, lb = 0; if unchecked, recalculate appropriate R2, LB & maxpoint
  document.samsched_table.ct_t1.onclick = Function("if (document.samsched_table.ct_t1.checked == true) {document.samsched_table.decay_factor_t1.value = 0; document.samsched_table.lb_t1.value = 0;} else RecalcT1();");
  document.samsched_table.ct_t2.onclick = Function("if (document.samsched_table.ct_t2.checked == true) {document.samsched_table.decay_factor_t2.value = 0; document.samsched_table.lb_t2.value = 0;} else RecalcT2();");
  
  // on change of nucleus, recalculate appropriate R2, LB and maxpoint
  document.samsched_table.nucleus_t0.onchange = Function("RecalcT0();");
  document.samsched_table.nucleus_t1.onchange = Function("RecalcT1();");
  document.samsched_table.nucleus_t2.onchange = Function("RecalcT2();");
  
  // on change of sf, recalculate appropriate maxpoint and decay_factor = 2/sf, and since decay_factor changes, appropriate lb
  document.samsched_table.sf_t1.onchange = Function("document.samsched_table.maxpoint_t1.value = CalcMaxIncr(CalcR2(CalcTM(), document.samsched_table.nucleus_t1.value),document.samsched_table.sw_t1.value,document.samsched_table.sf_t1.value); document.samsched_table.decay_factor_t1.value = 2/document.samsched_table.sf_t1.value; document.samsched_table.lb_t1.value = Math.round(100 * document.samsched_table.r2_t1.value * document.samsched_table.decay_factor_t1.value)/100;");
  document.samsched_table.sf_t2.onchange = Function("document.samsched_table.maxpoint_t2.value = CalcMaxIncr(CalcR2(CalcTM(), document.samsched_table.nucleus_t2.value),document.samsched_table.sw_t2.value,document.samsched_table.sf_t2.value); document.samsched_table.decay_factor_t2.value = 2/document.samsched_table.sf_t2.value; document.samsched_table.lb_t2.value = Math.round(100 * document.samsched_table.r2_t2.value * document.samsched_table.decay_factor_t2.value)/100;");
  
  // on change of maxpoint, recalculate appropriate sf and decay_factor = 2/sf, and since decay_factor changes, appropriate lb
  document.samsched_table.maxpoint_t1.onchange = Function("document.samsched_table.sf_t1.value = CalcSF(document.samsched_table.r2_t1.value,document.samsched_table.sw_t1.value,document.samsched_table.maxpoint_t1.value); document.samsched_table.decay_factor_t1.value = 2/document.samsched_table.sf_t1.value; document.samsched_table.lb_t1.value = Math.round(100 * document.samsched_table.r2_t1.value * document.samsched_table.decay_factor_t1.value)/100;");
  document.samsched_table.maxpoint_t2.onchange = Function("document.samsched_table.sf_t2.value = CalcSF(document.samsched_table.r2_t2.value,document.samsched_table.sw_t2.value,document.samsched_table.maxpoint_t2.value); document.samsched_table.decay_factor_t2.value = 2/document.samsched_table.sf_t2.value; document.samsched_table.lb_t2.value = Math.round(100 * document.samsched_table.r2_t2.value * document.samsched_table.decay_factor_t2.value)/100;");
  
  // for default, lb = r2 * decay factor
  document.samsched_table.lb_t1.value = document.samsched_table.r2_t1.value * document.samsched_table.decay_factor_t1.value;
  document.samsched_table.lb_t2.value = document.samsched_table.r2_t2.value * document.samsched_table.decay_factor_t2.value;
  
  // on change of decay factor, recalculate appropriate lb
  document.samsched_table.decay_factor_t0.onchange = Function("document.samsched_table.lb_t0.value = Math.round(100 * document.samsched_table.r2_t0.value * document.samsched_table.decay_factor_t0.value)/100;");
  document.samsched_table.decay_factor_t1.onchange = Function("document.samsched_table.lb_t1.value = Math.round(100 * document.samsched_table.r2_t1.value * document.samsched_table.decay_factor_t1.value)/100;");
  document.samsched_table.decay_factor_t2.onchange = Function("document.samsched_table.lb_t2.value = Math.round(100 * document.samsched_table.r2_t2.value * document.samsched_table.decay_factor_t2.value)/100;");

  // on change of r2, recalculate appropriate MaxIncr and lb
  document.samsched_table.r2_t0.onchange = Function("document.samsched_table.maxpoint_t0.value = CalcMaxIncr(document.samsched_table.r2_t0.value, document.samsched_table.sw_t0.value, document.samsched_table.sf_t0.value); document.samsched_table.lb_t0.value = document.samsched_table.r2_t0.value * document.samsched_table.decay_factor_t0.value;");
  document.samsched_table.r2_t1.onchange = Function("document.samsched_table.maxpoint_t1.value = CalcMaxIncr(document.samsched_table.r2_t1.value, document.samsched_table.sw_t1.value, document.samsched_table.sf_t1.value); document.samsched_table.lb_t1.value = document.samsched_table.r2_t1.value * document.samsched_table.decay_factor_t1.value;");
  document.samsched_table.r2_t2.onchange = Function("document.samsched_table.maxpoint_t2.value = CalcMaxIncr(document.samsched_table.r2_t2.value, document.samsched_table.sw_t2.value, document.samsched_table.sf_t2.value); document.samsched_table.lb_t2.value = document.samsched_table.r2_t2.value * document.samsched_table.decay_factor_t2.value;");
  
  // on change of lb, recalculate appropriate decay factor  
  document.samsched_table.lb_t0.onchange = Function("document.samsched_table.decay_factor_t0.value = document.samsched_table.lb_t0.value / document.samsched_table.r2_t0.value;");
  document.samsched_table.lb_t1.onchange = Function("document.samsched_table.decay_factor_t1.value = document.samsched_table.lb_t1.value / document.samsched_table.r2_t1.value;");
  document.samsched_table.lb_t2.onchange = Function("document.samsched_table.decay_factor_t2.value = document.samsched_table.lb_t2.value / document.samsched_table.r2_t2.value;");

function RecalcParms()
{
  // RecalcParms recalculates the values for T0, T1 and T2
  RecalcT0();
  RecalcT1();
  if (document.samsched_table.dimension[0].checked != true) RecalcT2();
}

function RecalcFactors()
{
  // RecalcFactors sets SF in indirect dimensions to 0.75, SF in direct to 1, and decay_factors to 2.67 & 3 respectively
  document.samsched_table.sf_t0.value = 3;
  document.samsched_table.sf_t1.value = 0.75;
  document.samsched_table.sf_t2.value = 0.75;
  document.samsched_table.decay_factor_t0.value = 1;
  document.samsched_table.decay_factor_t1.value = 2.67;
  document.samsched_table.decay_factor_t2.value = 2.67;
}

function RecalcT0()
{
  // Recalc T0: sets the R2 value to that calculated by CalcR2; next recalculates MaxPoint, next recalculates lb.  Math.round ensures 2 decimal places
  document.samsched_table.r2_t0.value = CalcR2(CalcTM(), document.samsched_table.nucleus_t0.value);
  document.samsched_table.maxpoint_t0.value = CalcMaxIncr(CalcR2(CalcTM(), document.samsched_table.nucleus_t0.value),document.samsched_table.sw_t0.value,1);
  document.samsched_table.lb_t0.value = Math.round(100*document.samsched_table.r2_t0.value * document.samsched_table.decay_factor_t0.value)/100;
}

function RecalcT1()
{
  // Recalc T1: sets the R2 value to that calculated by CalcR2; next recalculates MaxPoint, next recalculates lb.  Math.round ensures 2 decimal places
  document.samsched_table.r2_t1.value = CalcR2(CalcTM(), document.samsched_table.nucleus_t1.value);
  document.samsched_table.maxpoint_t1.value = CalcMaxIncr(CalcR2(CalcTM(), document.samsched_table.nucleus_t1.value),document.samsched_table.sw_t1.value,document.samsched_table.sf_t1.value);
  document.samsched_table.lb_t1.value = Math.round(100*document.samsched_table.r2_t1.value * document.samsched_table.decay_factor_t1.value)/100;
}

function RecalcT2()
{
  // Recalc T2: sets the R2 value to that calculated by CalcR2; next recalculates MaxPoint, next recalculates lb.  Math.round ensures 2 decimal places
  document.samsched_table.r2_t2.value = CalcR2(CalcTM(), document.samsched_table.nucleus_t2.value);
  document.samsched_table.maxpoint_t2.value = CalcMaxIncr(CalcR2(CalcTM(), document.samsched_table.nucleus_t2.value),document.samsched_table.sw_t2.value,document.samsched_table.sf_t2.value);
  document.samsched_table.lb_t2.value = Math.round(100*document.samsched_table.r2_t2.value * document.samsched_table.decay_factor_t2.value)/100;
}

function CalcMaxIncr(R2, SW, SF)
{
  // Max Increment = SW * SF / R2
  return Math.round(SW*SF/R2);

}

function CalcSF(R2, SW, MI)
{
  // SF = MaxInc * R2 / SW. Math.round ensures 2 decimal places
  return Math.round(100*MI*R2/SW)/100;
}

function CalcR2(TM, nucleus)
{  
  // Calculates R2 from correlation time for several types of nuclei states.
  var coeff;
  var intercept;

  // Hardcoded linear equations for (0) 1H-homo, (1) 1H-13C, (2) 1H-15N, (3) 13C, (4) 15N, and (5) 15N-TROSY.  Math.round ensures 2 decimal places
  coeff = new Array(1.20870, 2.519, 1.523, 1.319, 0.369, 0.0877);
  intercept = new Array(0.085, 1.212, 0.954, 0.962, 0.662, 0.992);

  return Math.round(100*3.1416*(coeff[nucleus]*TM + intercept[nucleus]))/100;
}

function CalcTM()
{
  // Calculates correlation time TM from molecular weight
  var MolWeight;
  var NumAA;
  var AAWeight;
  var TM;

  MolWeight = document.samsched_table.molecular_weight.value;
  AAWeight = 115;                 // Estimate average molecular weight of amino acid = 115
  NumAA = MolWeight/AAWeight;     // Number of aminoa acids = total MW / aa MW
  TM = 0.075*NumAA - 0.5;         // Mark's formula for determining corrlation time (TM) from number of amino acids
                                  // In D2O, correlation time is increased by 25%
  if (document.samsched_table.solvent[1].checked == true) return 1.25*TM; else return TM;
}

