/* ***************************** */
/* calculates input1/input2/Vol/Mol	*/
/* ***************************** */

// This script is intended to work in conjunction with labcalculator.js which creates the LabCalc global object

LabCalc.calculate = function () {
  // Get input 
  var F=parseFloat(document.calculator.input1.value);
  var W=parseFloat(document.calculator.input2.value);
  var V=parseFloat(document.calculator.input3.value);
  var M=parseFloat(document.calculator.input4.value);
  var mode=document.calculator.Mode.value;

// apply unit conversions

  // convert weight to g if necessary
  switch(document.calculator.units_weight.value) {
  case "mg" : {W=W/1000; break;}
  case "ug" : {W=W/1000000; break;}
  case "kg" : {W=W*1000; break;}
  }
  
  // convert volume to L if necessary
  switch(document.calculator.units_volume.value) {
  case "ml" : {V=V/1000; break;}
  case "ul" : {V=V/1000000; break;}
  }
  
  // convert molarity to M if necessary
  switch(document.calculator.units_molarity.value) {
  case "mM" : {M=M/1000; break;}
  case "uM" : {M=M/1000000; break;}
  }
  
  if(isNaN(M) || (mode=="molarity")) {
    // calculate molarity
    M=(W/F)/V;
	document.calculator.units_molarity.selectedIndex=0;
	document.calculator.input4.value=M.toPrecision(4);
	LabCalc.updateTicker("Molarity = (" + W + "g/" + F + "[g/mol])/" + V + " L", document.calculator.input4.value + "\n");
  }
  
  else if(isNaN(F) || (mode=="formula")) {
    // calculate formula weight
	F=W/M/V;
	document.calculator.input1.value=F.toPrecision(4);
	LabCalc.updateTicker("Formula input2 = " + W + "g/" + M + "[mol/L]/" + V + "L", document.calculator.input1.value + "\n");
  }
  
  else if(isNaN(V) ||(mode=="volume")) {
    // calculate volume
	V=W/F/M;
	document.calculator.units_volume.selectedIndex=0;
	document.calculator.input3.value=V.toPrecision(4);
	LabCalc.updateTicker("Volume (L) = " + W + "g/" + F + "[g/mol]/" + M + "[mol/L]",document.calculator.input3.value + "\n");
  }
  
  else if(isNaN(W) || (mode=="weight")) {
    // calculate weight of chemical
	W=F*M*V;
	document.calculator.units_weight.selectedIndex=0;
	document.calculator.input2.value=W.toPrecision(4);
	LabCalc.updateTicker("Dry weight (g) = " + F + "[g/mol] x " + M + "[mol/L] x " + V + "L ",document.calculator.input2.value + "\n");
  }
  else window.alert("Which field should be recalculated?  Please clear one entry or choose a mode.");

}

/********************************************************************/
/* This function fills in field input1 given the chemical formula in field input5 */
/* This function REQUIRES chemcalc.js */

LabCalc.calculateFW = function () {
	// Get input 
	var x = document.calculator.input5.value;
	// Calculate result
	var y = ChemCalc.FW.weightOf(x);
	// display result(s) in output1 element
	if(y) document.calculator.input1.value=y; else document.calculator.input1.value="error";
	// Update ticker
	if(y) LabCalc.updateTicker(x,y);
}
