var mol = new Array();
var react = new Array();
var tempReact = new Array();
var reactType = new Array();
var reactTypeIndex = new Array();
var atomMass = new Array();
var quizName = new Array();
var convEqn = new Array();
var prnEqn = new Array();
var prnNum = new Array();
var prnUnit = new Array();
var formulaMol;
var formulaWeight;
var quizWindowOpened;
var quizType;
var possReacts;
var reactNum;
var quizWidth = 650;
var quizHeight = 450;
var reactLength;
var holdMol;
var modMol;
var textWidth;
var is_macie;
var is_justie;
var is_winie;
var is_citizen;
var modReactType;
var helpWindowOpened;
var doChoose;
var randomNum;
var answer;
var prnAnswer;
var answerSciNot;
var answerExp;
var numSigFigs;
var isAmbiguous;
var revealConv;
var convCorrect;
var revealedAnswer;
var formulaWeightDecimal;
var emailAdd = 'chemistry@chemistry.alanearhart.org';
var dateMod = '8\/07\/06';

//quizName[0] = 'Balancing Reactions';
//quizName[1] = 'Identifying Reactions';
quizName[0] = 'Identifying Reactions';
quizName[1] = 'Balancing Reactions';
quizName[2] = 'Calculating Formula Weights';
quizName[3] = 'Converting Between Grams and Moles';

reactType[0] = 'Combination';
reactTypeIndex[0] = 9;
reactType[1] = 'Decomposition';
reactTypeIndex[1] = 22;
reactType[2] = 'Combustion';
reactTypeIndex[2] = 30;
reactType[3] = 'Combination/Combustion';
reactTypeIndex[3] = 38;
reactType[4] = 'Single Replacement';
reactTypeIndex[4] = 50;
reactType[5] = 'Double Replacement (Neutralization)';
reactTypeIndex[5] = 60;
reactType[6] = 'Double Replacement (Precipitation)';
reactTypeIndex[6] = 70;
reactType[7] = 'Double Replacement (Gas)';
reactTypeIndex[7] = 78;
reactType[8] = 'Redox';
reactTypeIndex[8] = 80;

mol[0] = 'H2O(l)';
mol[1] = 'H2O(g)';
mol[2] = 'H2O2(aq)';

// acetate

mol[3] = 'KC2H3O2(aq)';
mol[4] = 'NaC2H3O2(aq)';

// acid- weak

mol[5] = 'HC2H3O2(aq)';
mol[6] = 'HNO2(aq)';
mol[7] = 'H2CO3(aq)';
mol[8] = 'H2C2O4(aq)';
mol[9] = 'H2SO3(aq)';
mol[10] = 'H3PO4(aq)';

// acid- strong

mol[11] = 'HBr(aq)';
mol[12] = 'HBr(g)';
mol[13] = 'HCl(aq)';
mol[14] = 'HCl(g)';
mol[15] = 'HI(aq)';
mol[16] = 'HNO3(aq)';
mol[17] = 'HClO4(aq)';
mol[18] = 'H2SO4(aq)';
mol[19] = 'H2SO4(l)';

// base

mol[20] = 'NH3(aq)';
mol[21] = 'NH3(g)';

// carbon

mol[22] = 'CH4(g)';
mol[23] = 'CO(g)';
mol[24] = 'CO2(g)';
mol[25] = 'CH3OH(l)';
mol[26] = 'C2H5OH(l)';
mol[27] = 'C2H5SH(l)';
mol[28] = 'C3H6(g)';
mol[29] = 'C3H8(g)';
mol[30] = 'C4H10(g)';
mol[31] = 'C6H6(l)';
mol[173] = 'C6H6(g)';
mol[32] = 'C6H12(l)';
mol[33] = 'C12H22O11(s)';

// carbonate

mol[34] = 'BaCO3(s)';
mol[35] = 'CaCO3(s)';
mol[36] = 'LiHCO3(s)';
mol[37] = 'Li2CO3(s)';
mol[38] = 'KHCO3(aq)';
mol[39] = 'MgCO3(s)';
mol[40] = 'NaHCO3(aq)';
mol[41] = 'NaHCO3(s)';
mol[42] = 'Na2CO3(s)';

// chlorate

mol[43] = 'KClO3(s)';

// diatomic

mol[44] = 'Br2(aq)';
mol[45] = 'Br2(l)';
mol[46] = 'Cl2(g)';
mol[47] = 'F2(g)';
mol[48] = 'H2(g)';
mol[49] = 'I2(aq)';
mol[50] = 'I2(g)';
mol[51] = 'N2(g)';
mol[52] = 'O2(g)';

// dichromate

mol[53] = '(NH4)2Cr2O7(s)';

// elemental (non-diatomic)

mol[54] = 'Ag(s)';
mol[157] = 'Al(s)';
mol[55] = 'Au(s)';
mol[56] = 'C(s)';
mol[158] = 'Ca(s)';
mol[57] = 'Cd(s)';
mol[58] = 'Cr(s)';
mol[59] = 'Cu(s)';
mol[60] = 'Fe(s)';
mol[61] = 'Fe(l)';
mol[62] = 'Hg(l)';
mol[63] = 'K(s)';
mol[64] = 'Li(s)';
mol[65] = 'Mg(s)';
mol[66] = 'Mn(s)';
mol[67] = 'Na(s)';
mol[68] = 'Ni(s)';
mol[69] = 'P(s)';
mol[70] = 'P4(s)';
mol[71] = 'S8(s)';
mol[72] = 'Si(s)';
mol[73] = 'Zn(s)';

// halide (bromide, chloride, iodide)

mol[74] = 'AgCl(s)';
mol[159] = 'AgI(s)';
mol[160] = 'AlBr3(s)';
mol[75] = 'AlCl3(aq)';
mol[161] = 'AlCl3(s)';
mol[76] = 'BaCl2(aq)';
mol[174] = 'C3H6Br2(l)';
mol[77] = 'CaCl2(aq)';
mol[78] = 'CdCl2(aq)';
mol[79] = 'FeCl3(aq)';
mol[80] = 'KBr(s)';
mol[81] = 'KCl(aq)';
mol[162] = 'KCl(s)';
mol[82] = 'KI(aq)';
mol[83] = 'LiBr(s)';
mol[84] = 'MgBr2(aq)';
mol[85] = 'MgCl2(aq)';
mol[86] = 'NH4Br(s)';
mol[87] = 'NH4Cl(aq)';
mol[88] = 'NaBr(aq)';
mol[89] = 'NaCl(aq)';
mol[90] = 'NaI(aq)';
mol[91] = 'NiCl2(aq)';
mol[92] = 'PCl3(l)';
mol[93] = 'PCl5(s)';
mol[163] = 'PbCl2(s)';
mol[94] = 'PbI2(s)';
mol[95] = 'ZnCl2(aq)';

// hydroxide

mol[96] = 'Al(OH)3(s)';
mol[97] = 'Ba(OH)2(aq)';
//mol[97] = 'Ba43(O19H)25(aq)';
mol[98] = 'Ca(OH)2(aq)';
mol[99] = 'KOH(aq)';
mol[100] = 'LiOH(s)';
mol[101] = 'Mg(OH)2(s)';
mol[102] = 'NaOH(aq)';
mol[103] = 'Ni(OH)2(s)';

// nitrate

mol[104] = 'AgNO3(aq)';
mol[105] = 'Al(NO3)3(aq)';
mol[106] = 'Ba(NO3)2(aq)';
mol[107] = 'Cd(NO3)2(aq)';
mol[108] = 'Cu(NO3)2(aq)';
mol[109] = 'KNO3(aq)';
mol[110] = 'Mn(NO3)2(aq)';
mol[111] = 'NaNO3(aq)';
mol[112] = 'Fe(NO3)3(aq)';
mol[113] = 'KNO3(aq)';
mol[114] = 'NH4NO3(aq)';
mol[115] = 'Pb(NO3)2(aq)';

// nitrite

mol[164] = 'KNO2(aq)';

// nitrogen

mol[116] = 'NO(g)';
mol[117] = 'NO2(g)';
mol[165] = 'N2O(g)';

// oxalate

mol[118] = 'CaC2O4(s)';

// oxide

mol[171] = 'As2O3(l)';
mol[119] = 'Al2O3(s)';
mol[120] = 'CaO(s)';
mol[121] = 'Cr2O3(s)';
mol[122] = 'CuO(s)';
mol[123] = 'FeO(s)';
mol[124] = 'Fe2O3(s)';
mol[125] = 'HgO(s)';
mol[175] = 'Li2O(s)';
mol[126] = 'MgO(s)';
mol[127] = 'N2O5(g)';
mol[128] = 'PbO(s)';
mol[129] = 'PbO2(s)';
mol[130] = 'SiO2(s)';

// perchlorate

mol[166] = 'NH4ClO4(s)';

// phosphate

mol[167] = 'AlPO4(s)';
mol[131] = 'Ca3(PO4)2(s)';
mol[132] = '(NH4)3PO4(aq)';

// sulfate

mol[133] = 'Ag2SO4(s)';
mol[134] = 'Al2(SO4)3(aq)';
mol[135] = 'BaSO4(s)';
mol[136] = 'CuSO4(aq)';
mol[137] = 'FeSO4(aq)';
mol[138] = 'Fe2(SO4)3(aq)';
mol[139] = 'K2SO4(aq)';
mol[140] = 'MgSO4(aq)';
mol[141] = '(NH4)2SO4(aq)';
mol[142] = 'Na2SO4(aq)';
mol[143] = 'NiSO4(aq)';
mol[144] = 'PbSO4(s)';
mol[168] = 'ZnSO4(aq)';

// sulfide

mol[145] = 'Au2S3(s)';
mol[169] = 'As2S3(s)';
mol[146] = 'CS2(l)';
mol[147] = 'CdS(s)';
mol[148] = 'Cr2S3(s)';
mol[176] = 'Fe2S3(s)';
mol[149] = 'H2S(g)';
mol[150] = 'MnS(s)';
mol[151] = 'Na2S(aq)';
mol[152] = 'ZnS(s)';


// sulfite

mol[170] = 'CaSO3(aq)';
mol[172] = 'CaSO3(s)';
mol[153] = 'K2SO3(aq)';
mol[154] = 'Na2SO3(aq)';

// sulfur

mol[155] = 'SO2(g)';
mol[156] = 'SO3(g)';

// reactions' string format=
//   reaction type index,
//   total # reactants,
//   total # products,
//   index of component 1, # of component 1,
//   index of component 2, # of component 2,
//   etc.

// The reactants are listed first followed by the products

// 0 Combination Reactions

react[0] = '0,2,1,100,1,24,1,36,1';
react[1] = '0,2,1,51,1,48,3,21,2';
react[2] = '0,2,1,67,2,46,1,89,2';
react[3] = '0,2,1,157,2,45,3,160,2';
react[4] = '0,2,1,120,1,155,1,172,1';
react[5] = '0,2,1,64,2,45,1,83,2';
react[6] = '0,2,1,69,2,46,5,93,2';
react[7] = '0,2,1,23,2,52,1,24,2';
react[8] = '0,2,1,28,1,45,1,174,1';
react[9] = '0,2,1,70,1,46,6,92,4';

// 1 Decomposition Reactions

react[10] = '1,1,2,35,1,120,1,24,1';
react[11] = '1,1,2,43,2,162,2,52,3';
react[12] = '1,1,2,125,2,62,2,52,1';
react[13] = '1,1,2,2,2,0,2,52,1';
react[14] = '1,1,2,114,1,165,1,1,2';
react[15] = '1,1,2,127,2,117,4,52,1';
react[16] = '1,1,2,74,2,54,2,46,1';
react[17] = '1,1,2,33,1,56,12,1,11';
react[18] = '1,1,2,109,2,164,2,52,1';
react[19] = '1,1,2,86,1,21,1,12,1';
react[20] = '1,1,2,39,1,126,1,24,1';
react[21] = '1,1,2,117,2,116,2,52,1';
react[22] = '1,1,3,41,2,42,1,24,1,1,1';

// 2 Combustion Reactions

react[23] = '2,2,2,22,1,52,2,24,1,1,2';
react[24] = '2,2,2,30,2,52,13,24,8,1,10';
react[25] = '2,2,2,29,1,52,5,24,3,1,4';
react[26] = '2,2,2,146,1,52,3,24,1,155,2';
react[27] = '2,2,3,27,2,52,9,24,4,1,6,155,2';
react[28] = '2,2,2,26,1,52,3,24,2,1,3';
react[29] = '2,2,2,32,1,52,9,24,6,1,6';
react[30] = '2,2,2,21,4,52,5,116,4,1,6';

// 3 Combination/Combustion Reactions

react[31] = '3,2,1,60,4,52,3,124,2';
react[32] = '3,2,1,155,2,52,1,156,2';
react[33] = '3,2,1,48,2,52,1,0,2';
react[34] = '3,2,1,51,1,52,1,116,2';
react[35] = '3,2,1,65,2,52,1,126,2';
react[36] = '3,2,1,56,1,52,1,24,1';
react[37] = '3,2,1,48,2,52,1,1,2';
react[38] = '3,2,1,64,4,52,1,175,2';

// 4 Single-Replacement Reactions

react[39] = '4,2,2,158,1,0,2,98,1,48,1';
react[40] = '4,2,2,65,1,11,2,84,1,48,1';
react[41] = '4,2,2,73,1,13,2,95,1,48,1';
react[42] = '4,2,2,67,2,0,2,102,2,48,1';
react[43] = '4,2,2,59,1,104,2,108,1,54,2';
react[44] = '4,2,2,157,2,124,1,119,1,61,2';
react[45] = '4,2,2,90,2,46,1,89,2,49,1';
react[46] = '4,2,2,46,1,88,2,89,2,44,1';
react[47] = '4,2,2,57,1,104,2,107,1,54,2';
react[48] = '4,2,2,65,3,138,1,140,3,60,2';
react[49] = '4,2,2,73,1,136,1,168,1,59,1';
react[50] = '4,2,2,157,2,10,2,167,2,48,3';

// 5 Double-Replacement (Neutralization) Reactions

react[51] = '5,2,2,5,1,102,1,4,1,0,1';
react[52] = '5,2,2,96,1,16,3,105,1,0,3';
react[53] = '5,2,2,18,1,99,2,139,1,0,2';
react[54] = '5,2,2,102,1,15,1,90,1,0,1';
react[55] = '5,2,2,13,1,102,1,89,1,0,1';
react[56] = '5,2,2,101,1,13,2,85,1,0,2';
react[57] = '5,2,2,96,1,13,3,75,1,0,3';
react[58] = '5,2,2,16,1,102,1,111,1,0,1';
react[59] = '5,2,2,97,1,13,2,76,1,0,2';
react[60] = '5,2,2,98,3,10,2,131,1,0,6';

// 6 Double-Replacement (Precipitation) Reactions

react[61] = '6,2,2,79,2,151,3,176,1,89,6';
react[62] = '6,2,2,77,1,42,1,35,1,89,2';
react[63] = '6,2,2,82,2,115,1,109,2,94,1';
react[64] = '6,2,2,77,1,7,1,35,1,13,2';
react[65] = '6,2,2,8,1,77,1,118,1,13,2';
react[66] = '6,2,2,106,1,139,1,135,1,109,2';
react[67] = '6,2,2,104,1,90,1,159,1,111,1';
react[68] = '6,2,2,115,1,87,2,163,1,114,2';
react[69] = '6,2,2,140,1,102,2,101,1,142,1';
react[70] = '6,2,2,104,2,139,1,133,1,109,2';

// 7 Double-Replacement (Gas) Reactions

react[71] = '7,2,3,5,1,40,1,4,1,24,1,0,1';
react[72] = '7,2,3,153,1,13,2,81,2,155,1,0,1';
react[73] = '7,2,3,42,1,13,2,89,2,0,1,24,1';
react[74] = '7,2,3,154,1,13,2,89,2,0,1,155,1';
react[75] = '7,2,2,151,1,13,2,89,2,149,1';
react[76] = '7,2,2,152,1,13,2,95,1,149,1';
react[77] = '7,2,3,35,1,13,2,77,1,24,1,0,1';
react[78] = '7,2,3,41,2,18,1,142,1,0,2,24,2';

// 8 Redox Reactions

react[79] = '8,2,2,23,1,48,3,22,1,1,1';
react[80] = '8,2,4,157,3,166,3,119,1,161,1,116,3,1,6';

// Atomic masses using the Webelements 5 SF periodic table dated 3/17/03

 atomMass['H']  = '1.0079';
 atomMass['Li'] = '6.941';
 atomMass['C']  = '12.011';
 atomMass['N']  = '14.007';
 atomMass['O']  = '15.999';
 atomMass['F']  = '18.998';
 atomMass['Na'] = '22.990';
 atomMass['Mg'] = '24.305';
 atomMass['Al'] = '26.982';
 atomMass['Si'] = '28.086';
 atomMass['P']  = '30.974';
 atomMass['S']  = '32.065';
 atomMass['Cl'] = '35.453';
 atomMass['K']  = '39.098';
 atomMass['Ca'] = '40.078';
 atomMass['Cr'] = '51.996';
 atomMass['Mn'] = '54.938';
 atomMass['Fe'] = '55.845';
 atomMass['Ni'] = '58.693';
 atomMass['Cu'] = '63.546';
 atomMass['Zn'] = '65.39';
 atomMass['As'] = '74.922';
 atomMass['Br'] = '79.904';
 atomMass['Ag'] = '107.87';
 atomMass['Cd'] = '112.41';
 atomMass['Sn'] = '118.71';
 atomMass['I']  = '126.90';
 atomMass['Ba'] = '137.33';
 atomMass['Au'] = '196.97';
 atomMass['Hg'] = '200.59';
 atomMass['Pb'] = '207.2';

// Basic conversion equation for converting grams to moles
//
//   V1 g * 1 mol   V3 mol  (standard notation)
//   ____   _____ =  or
//    1     V2 g    V4 mol  (scientific notation)

convEqn[0] = '<TABLE BORDER=\"0\"><TR><TD CLASS=\"firstCell\" ALIGN=\"center\">' +
             '<TABLE BORDER=\"0\"><TR><TD ALIGN=\"center\">V1&nbsp;' +
             '<SPAN CLASS="uCan">g<\/SPAN><\/TD><\/TR><TR>' +
             '<TD ALIGN=\"center\"><HR><\/TD><\/TR><TR><TD ALIGN=\"center\">' +
             '1<\/TD><\/TR><\/TABLE><\/TD><TD CLASS=\"otherCell\" ALIGN=\"center\">' +
             '<TABLE BORDER=\"0\"><TR><TD ALIGN=\"center\">1&nbsp;mol<\/TD>' +
             '<\/TR><TR><TD ALIGN=\"center\"><HR><\/TD><\/TR><TR>' +
             '<TD ALIGN=\"center\">V2&nbsp;<SPAN CLASS=\"uCan\">g<\/SPAN>' +
             '<\/TD><\/TR><\/TABLE><\/TD><TD ALIGN=\"center\">=<\/TD>' +
             '<TD ALIGN=\"center\"><TABLE BORDER=\"0\"><TR>' +
             '<TD ALIGN=\"center\">V3&nbsp;mol<\/TD><\/TR><TR>' +
             '<TD ALIGN=\"center\">or<\/TD><\/TR><TR>' +
             '<TD ALIGN=\"center\">V4&nbsp;mol<\/TD><\/TR><\/TABLE>' +
             '<\/TD><\/TR><\/TABLE>';

// Basic conversion equation for converting moles to grams
//
//   V1 mol * V2 g    V3 g  (standard notation)
//   _____    _____ =  or
//    1       1 mol   V4 g  (scientific notation)

convEqn[1] = '<TABLE BORDER=\"0\"><TR><TD CLASS=\"firstCell\" ALIGN=\"center\">' +
             '<TABLE BORDER=\"0\"><TR><TD ALIGN=\"center\">V1&nbsp;' +
             '<SPAN CLASS=\"uCan\">mol<\/SPAN><\/TD><\/TR><TR>' +
             '<TD ALIGN=\"center\"><HR><\/TD><\/TR><TR><TD ALIGN=\"center\">' +
             '1<\/TD><\/TR><\/TABLE><\/TD><TD CLASS=\"otherCell\" ALIGN=\"center\">' +
             '<TABLE BORDER=\"0\"><TR><TD ALIGN=\"center\">V2&nbsp;g<\/TD>' +
             '<\/TR><TR><TD ALIGN=\"center\"><HR><\/TD><\/TR><TR>' +
             '<TD ALIGN=\"center\">1&nbsp;<SPAN CLASS=\"uCan\">mol<\/SPAN>' +
             '<\/TD><\/TR><\/TABLE><\/TD><TD ALIGN=\"center\">=<\/TD>' +
             '<TD ALIGN=\"center\"><TABLE BORDER=\"0\"><TR>' +
             '<TD ALIGN=\"center\">V3&nbsp;g<\/TD><\/TR><TR>' +
             '<TD ALIGN=\"center\">or<\/TD><\/TR><TR>' +
             '<TD ALIGN=\"center\">V4&nbsp;g<\/TD><\/TR><\/TABLE>' +
             '<\/TD><\/TR><\/TABLE>';

function chooseRandomNum(rangeNum) {
  var rangeNum;

  randomNum = Math.round(rangeNum*Math.random());
}

function modBasicConv() {
  var tempAnswerSci;

  if (prnUnit[0] == 'g') {
  // grams to moles
    prnEqn[0] = convEqn[0];
  }
  else {
  // mol to grams
    prnEqn[0] = convEqn[1];
  }
  prnEqn[0] = prnEqn[0].replace(/V1/, prnNum[0]);
  prnEqn[0] = prnEqn[0].replace(/V2/, formulaWeight);

  // Insert the answer in both standard and scientific notation
  prnEqn[0] = prnEqn[0].replace(/V3/, answer);
  tempAnswerSci = answerSciNot + '&nbsp;x&nbsp;10<SUP>' +
                  answerExp + '<\/SUP>';
  prnEqn[0] = prnEqn[0].replace(/V4/, tempAnswerSci);
}

function detNumSigFigs(val0,val1,val2,val3,val4,val5) {
  // Find out how many sig figs should be used in the final answer
  //   Multiplication and/or division operations, only
  //
  // val0, val1, val2, and val3 will be checked for the number of sig figs
  //   The one with the smallest number will be the final number for
  //   the answer
  //
  // Pass '' for any value that won't be used

  var val0;
  var val1;
  var val2;
  var val3;
  var val4;
  var val5;
  var valSigFigs;
  var charPos;
  var tempHoldLeft;
  var tempHoldRight;
  var allValues = new Array();

  allValues = [];
  allValues[0] = val0;
  allValues[1] = val1;
  allValues[2] = val2;
  allValues[3] = val3;
  allValues[4] = val4;
  allValues[5] = val5;

  numSigFigs = 50;
  for (var i = 0; i < 6; i++) {
    if (allValues[i]) {
      charPos = allValues[i].search(/\./);
      if (charPos != -1) {
        tempHoldLeft = allValues[i].substr(0,charPos);
        tempHoldRight = allValues[i].substr(charPos+1);
        if ((tempHoldLeft != '0') && (tempHoldLeft != '')) {
          valSigFigs = tempHoldLeft.length + tempHoldRight.length;
        }
        else {
          tempHoldRight = tempHoldRight - 0;
          tempHoldRight = tempHoldRight + '';
          valSigFigs = tempHoldRight.length;
        }
      }
      else {
        valSigFigs = allValues[i].length;
      }
      if (valSigFigs < numSigFigs) {
        numSigFigs = valSigFigs;
      }
    }
  }
}

function doSigFigsAdd() {
// This will only work with numbers that have decimal
//   places and thus have a specific number of sig figs
}

function doSigFigs() {
  var expCheck;
  var charPos;
  var beforeDec;
  var afterDec;
  var numSigFigsDiff;
  var tempLen;
  var numPlaceHolders;
  var notDone;

//  alert(answer); // Testing
  isAmbiguous = 0;

  // The current form of the answer could be 1e-7
  expCheck = answer.search(/e/i);
  if (expCheck != -1) {
  // Need to remove the 'e' and convert back to standard notation
    convertE(expCheck);
  }

  // See if there is a decimal point
  charPos = answer.search(/\./);
  if (charPos != -1) {
    beforeDec = answer.substr(0,charPos);
    afterDec = answer.substr(charPos + 1);
  }
  else {
    beforeDec = answer;
    afterDec = '';
  }

  if (answer > 1) {
  // Greater than 1
    numSigFigsDiff = numSigFigs - beforeDec.length;
    answer = Math.round((answer - 0)*Math.pow(10,numSigFigsDiff)) + '';

  // Again, the current form of the answer could be 1e7
    expCheck = answer.search(/e/i);
    if (expCheck != -1) {
    // Need to remove the 'e' and convert back to standard notation
      convertE(expCheck);
    }

    if (answer.length == beforeDec.length) {
    // Same # of sig figs as digits before the decimal point
      if (answer.substr(answer.length - 1,1) == '0') {
        answer += '.';
      }
    }
    else if (answer.length > beforeDec.length) {
    // Too many digits before the decimal place
      answer = answer.substr(0,beforeDec.length) + '.' + answer.substr(beforeDec.length);
    }
    else {
    // Not enough digits before the decimal place
      isAmbiguous = 1;
      tempLen = beforeDec.length - answer.length;
      for (var i = 0;i < tempLen;i++) {
        answer += '0';
      }
    }
  }
  else {
  // Less than 1
  // Count the number of placeholder zeroes, if any
    numPlaceHolders = 0;
    notDone = 1;
    while (notDone) {
      if (afterDec.substr(numPlaceHolders,1) == '0') {
        numPlaceHolders++;
      }
      else {
        notDone = 0;
      }
    }

    answer = Math.round((answer - 0)*Math.pow(10,numPlaceHolders+numSigFigs)) + '';
    for (var i = 0;i < numPlaceHolders;i++) {
      answer = '0' + answer;
    }
    answer = '0.' + answer;
  }
}

function convert1() {
// Converts from standard notation to scientific notation
  var charPos;
  var tempHold;
  var notDone;
  var currPos;
  var tempPos;

  charPos = answer.search(/\./);
  tempHold = answer.length;
  if ((answer - 0) > 1) {
    if (charPos == -1) {
      answerSciNot = answer.substr(0,1) + '.' + answer.substr(1);
      answerExp = tempHold - 1;
    }
    else {
      answerSciNot = answer.substr(0,1) + '.' + 
                     answer.substr(1,charPos - 1) + answer.substr(charPos + 1);
      answerExp = charPos - 1;
    }
  }
  else {
  // Need to find the position of the first non-zero number
    notDone = 1;
    currPos = 2;
    while (notDone) {
      if (answer.substr(currPos,1) != '0') {
        notDone = 0;
        tempPos = currPos;
      }
      else {
        currPos++;
      }
    }

    answerSciNot = answer.substr(tempPos,1);
    if (tempPos != (answer.length - 1)) {
      answerSciNot += '.' + answer.substr(tempPos + 1);
    }
    answerExp = '-' + ((tempPos - 1) + '');
  }

  // If this is a conversion from an answer with an ambiguous # of
  //   significant figures, need to chop off unneeded zeroes
  tempHold = answerSciNot.length;
  if (isAmbiguous) {
    for (var i=0; i<(tempHold-numSigFigs-1); i++) {
      answerSciNot = answerSciNot.substr(0,answerSciNot.length - 1)
    }
//    if (answerSciNot.substr(answerSciNot.length-1,1) == '.') {
//      answerSciNot = answerSciNot.substr(0,answerSciNot.length-1);
//    }
  }

  // Remove the last character if it's a decimal point
  if (answerSciNot.substr(answerSciNot.length-1,1) == '.') {
    answerSciNot = answerSciNot.substr(0,answerSciNot.length-1);
  }
//  alert(answerSciNot + '\n' + answerExp); // Testing
}

function convertE(ePos) {
  var ePos;
  var tempAnswer;
  var tempCount;
  var charPos;

  tempAnswer = answer.substr(0,ePos);
  tempCount = answer.substr(ePos + 1) - 0;
  charPos = tempAnswer.search(/\./);
  if (charPos != -1) {
    tempAnswer = tempAnswer.substr(0,charPos) + tempAnswer.substr(charPos + 1);
    if (tempCount < 0) {
      tempCount += charPos;
    }
    else {
      tempCount -= charPos;
    }
  }
  else {
    if (tempCount < 0) {
      tempCount++;
    }
    else {
      tempCount--;
    }
  }

  if (tempCount < 1) {
  // Negative exponent
    for (var i = tempCount; i < 0; i++) {
      tempAnswer = '0' + tempAnswer;
    }
    answer = '0.' + tempAnswer;
  }
  else {
  // Positive exponent
    for (var i = 0; i < tempCount; i++) {
      tempAnswer = tempAnswer +'0';
    }
    answer = tempAnswer;
  }
}

function chooseSigFig(digitPre,digitPost) {
  var digitPre;
  var digitPost;
  var theRange;
  var iRange;
  var tempHold;
  var firstTime;
  var notDone;
  var tempDisp;
  var preHold;
  var postHold;

  // 50:50 chance of getting a number greater than one
  chooseRandomNum(1);
//  randomNum = 1; // Testing
  if (!randomNum) {
  // The number is greater than 1
  // Determine the number of digits before the decimal point
    chooseRandomNum(digitPre);
    iRange = randomNum + 1;
    firstTime = 1;
    preHold = '';
    for (var i = 0; i < iRange; i++) {
    // % chance of the digit being a 0 but not on the first pass
      chooseRandomNum(9);
      if ((randomNum < 4) && !firstTime) {
      // Zero digit
        tempHold = '0';
      }
      else {
      // Nonzero digit
        chooseRandomNum(8);
        tempHold = randomNum + 1;
        tempHold += '';
      }
      preHold += tempHold;
      if (firstTime) {
        firstTime = 0;
      }
    }
  }
  else {
  // The number is less than 1
    preHold = '0';
  }

  notDone = 1;
  while (notDone) {
  // Determine the number of digits after the decimal point
    chooseRandomNum(digitPost);
    iRange = randomNum + 1;
    postHold = '';
    for (var i = 0; i < iRange; i++) {
    // % chance of the digit being a 0 
      chooseRandomNum(9)
      if (randomNum < 4) {
      // Zero digit
        tempHold = '0';
      }
      else {
      // Nonzero digit
        chooseRandomNum(8);
        tempHold = randomNum + 1;
        tempHold += '';
      }
      postHold += tempHold;
    }

  // Make sure the entire number is not zero
    if (preHold == '0') {
      if ((postHold - 0) != 0) {
        notDone = 0;
      }
    }
    else {
      notDone = 0;
    }
  }
//  alert(preHold + '.' + postHold); // Testing

  if (quizType > 1) {
    answer = preHold + '.' + postHold;
  }
  else if (quizType == 1) {
  // Possibly modify a number to move the decimal place
    answer = preHold + '.';
    if (preHold == '0') {
      chooseRandomNum(4);
      for (var i = 0; i < randomNum; i++) {
        answer += '0';
      }
    }
    answer += postHold;
  }

//  answer = '0.4'; // Testing
}

function closeChildWindows() {
  if (quizWindowOpened) {
    if (!quizWindow.closed) {
      quizWindow.close();
    }
  }
  if (helpWindowOpened) {
    if (!helpWindow.closed) {
      helpWindow.close();
    }
  }
}

function initValues(isAdmin) {
  var isAdmin;
  
  if (isAdmin == 1) {
    window.document.adminstoich.password.value = '';
  }
  quizWindowOpened = 0;
  helpWindowOpened = 0;
  checkBrowser();
  if (is_justie) {
    textWidth = 5;
  }
  else {
    textWidth = 4;
  }
}

function checkCheckbox() {
  is_citizen = 0;
  if (window.document.stoichForm.citizen.checked) {
    is_citizen = 1;
  }
}

function popUpChemEqnQuiz(tempValue) {
  var tempValue;

  checkCheckbox();
//  quizType = tempValue.substr(-1,1) - 0;
  quizType = tempValue.substr(tempValue.length - 1,1) - 0;
//  alert('got here and quizType=x' + quizType + 'x');
//  revealConv = 0;
  chemEqnQuiz();
}

function anotherChemEqnQuiz(tempValue) {
  var tempValue;

  checkCheckbox();
  quizType = tempValue;
//  revealConv = 0;
  chemEqnQuiz();
}

function chooseRandomReaction() {
  var offset;

  // All reactions

  offset = 0;
  possReacts = 79;

  reactNum = Math.round((possReacts - 1)*Math.random());
  reactNum += offset;
  // reactNum = 35;
}

function modifyMolecule() {
  var result;
  var currCount;
  var currChar;
  var subOn;
  var digitCheck = /\d/;

  subOn = 0;
  modMol = '';
  for (currCount = 0; currCount < holdMol.length; currCount++) {
    currChar = holdMol.charAt(currCount);
    result = currChar.match(digitCheck);
    if (result != null) {
      if (!subOn) {
        modMol += '<SUB>';
        subOn = 1;
      }
      modMol += currChar;
    }
    else {
      if (subOn) {
        modMol += '<\/SUB>';
        subOn = 0;
      }
      modMol += currChar;
    }
  }
  if (modMol.substr((modMol.length - 1),1).match(digitCheck)) {
    modMol += '<\/SUB>';
  }
}

function printReaction() {
  var currCount;
  var currIndex;

  quizWindow.document.write('<FORM NAME=\"reaction\" ' +
    'onSubmit=\"actionReaction(button); return false\">\n' +
    '<TABLE ALIGN=\"center\" BORDER=\"0\">\n<TR>\n');
  for (currCount = 1; currCount < (tempReact[1] + 1); currCount++) {
    currIndex = 3 + (currCount - 1)*2;
    holdMol = mol[tempReact[currIndex]];
    modifyMolecule();
    quizWindow.document.write('<TD ALIGN=\"center\" VALIGN=\"top\">\n');
    if (quizType == 1) {
      quizWindow.document.write('<INPUT NAME=\"react' + currCount + '\" ' +
        'TYPE=\"text\" VALUE=\"\" SIZE=\"' + textWidth + '\" MAXLENGTH=\"4\">\n');
    }
    else {
      quizWindow.document.write(tempReact[4 + ((currCount - 1)*2)]);
    }
    quizWindow.document.write('<\/TD>\n<TD ALIGN=\"center\" VALIGN=\"top\">\n' + 
      modMol + '\n<\/TD>\n');
    if (currCount != (tempReact[1])) {
      quizWindow.document.write('<TD ALIGN=\"center\" VALIGN=\"top\" ' +
        'STYLE=\"font-size: larger\">+<\/TD> \n');
    }
  }
  quizWindow.document.write('<TD ALIGN=\"center\" VALIGN=\"top\">\n' +
    '<IMG SRC=\"Images\/arrow.gif\" WIDTH=\"26\" HEIGHT=\"19\" ' +
    'ALT=\"arrow\">\n</TD>\n');
  for (currCount = 1; currCount < (tempReact[2] + 1); currCount++) {
    currIndex = 3 + (2*tempReact[1]) + (currCount - 1)*2;
    holdMol = mol[tempReact[currIndex]];
    modifyMolecule();
    quizWindow.document.write('<TD ALIGN=\"center\" VALIGN=\"top\">\n');
    if (quizType == 1) {
      quizWindow.document.write('<INPUT NAME=\"prod' + currCount + '\" ' +
        'TYPE=\"text\" VALUE=\"\" SIZE=\"' + textWidth + '\" MAXLENGTH=\"4\">\n');
    }
    else {
      quizWindow.document.write(tempReact[4+(2*tempReact[1])+((currCount-1)*2)]);
    }
    quizWindow.document.write('<\/TD>\n' + 
      '<TD ALIGN=\"center\" VALIGN=\"top\">\n' + modMol + '\n<\/TD>\n');
    if (currCount != (tempReact[2])) {
      quizWindow.document.write('<TD ALIGN=\"center\" VALIGN=\"top\" ' +
        'STYLE=\"font-size: larger\">+<\/TD> \n');
    }
  }
  quizWindow.document.write('<\/TR>\n<\/TABLE>\n');
  if (!quizType) {
    printReactionType();
  }
  printGradeButtons();
  quizWindow.document.write('<\/FORM>');
}

function printGradeButtons() {
  var elementCount;

  elementCount = 4;
//  if (is_winie) {
//    elementCount--;
//  }
  quizWindow.document.write('<BR>\n<DIV ID=\"quizButtons\" ');
//  if (!(is_nn && !is_geck)) {
    quizWindow.document.write('CLASS=\"showIt\" ');
//  }
  quizWindow.document.write('ALIGN=\"center\">\n');
//  if (!((quizType == 3) && revealConv)) {
  quizWindow.document.write('<INPUT TYPE=\"submit\" onClick=\"button=this.value\" onMouseOver=\"mOver(0,document.forms[0].elements.length - ' + elementCount + ')\" onMouseOut=\"mOut(0,document.forms[0].elements.length - ' + elementCount + ')\" VALUE=\"Grade It!\">\n' +
      '<INPUT TYPE=\"submit\" onClick=\"button=this.value\" onMouseOver=\"mOver(0,document.forms[0].elements.length - ' + (elementCount - 1) + ')\" onMouseOut=\"mOut(0,document.forms[0].elements.length - ' + (elementCount - 1) + ')\" VALUE=\"Reveal!\">\n');
  quizWindow.document.write('<INPUT TYPE=\"reset\" onMouseOver=\"mOver(0,document.forms[0].elements.length - ' + (elementCount - 2) + ')\" onMouseOut=\"mOut(0,document.forms[0].elements.length - ' + (elementCount - 2) + ')\" VALUE=\"Reset Value(s)\">\n' +
    '<\/DIV>\n');
//  if (!is_winie) {
    quizWindow.document.write('<BR><DIV ALIGN=\"center\">\n' +
      '<INPUT TYPE=\"submit\" onClick=\"button=this.value\" onMouseOver=\"mOver(0,document.forms[0].elements.length - 1)\" onMouseOut=\"mOut(0,document.forms[0].elements.length - 1)\" VALUE=\"Gimme\' Another One\">\n<\/DIV>\n');
//  }
}

function assignReactionType() {
  modReactType = tempReact[0];
  if (modReactType < 3) {
    modReactType += 1;
  }
}

function printReactionType() {
  quizWindow.document.write('<BR>');
  quizWindow.document.write('<DIV STYLE=\"text-align: center\">\n' +
    'Choose the type of reaction: <SELECT NAME=\"reacttype\" SIZE=\"1\">\n' +
    '<OPTION VALUE=\"0\">Choose one\n' +
    '<OPTION VALUE=\"1\">Combination\n' +
    '<OPTION VALUE=\"2\">Decomposition\n' +
    '<OPTION VALUE=\"3\">Combustion\n' +
    '<OPTION VALUE=\"4\">Single Replacement\n' +
    '<OPTION VALUE=\"5\">Double Replacement (Neutralization)\n' +
    '<OPTION VALUE=\"6\">Double Replacement (Precipitation)\n' +
    '<OPTION VALUE=\"7\">Double Replacement (Gas Formation)\n' +
    '<\/SELECT>\n<\/DIV>');
}

function openHelpWindow(whichHelp) {
  var whichHelp;

  helpWindowOpened = 1;
  if (whichHelp == 1) {
    helpWindow = window.open('stoichiometry-help.html','helpWindowName','resizable=1,scrollbars=1,status=1,width=650,height=425');
  }
  helpWindow.focus();
}

function printBalanceHeader() {
  var currCount;

  quizWindow.document.write('<HEAD>\n<TITLE>');
  quizWindow.document.write('Chemical Equations\' Quiz');
  quizWindow.document.write('<\/TITLE>\n<SCR' + 'IPT LANGUAGE=\"javascript\">\n');
  quizWindow.document.write('var trueCoeff = new Array();\n' +
    'var userCoeff = new Array();\nvar coeffOK;\nvar isMultiple;\n' +
    'var isBlank;\nvar trueReactType = ' + modReactType + ';\n' +
    'var userReactType;\n');
  if (tempReact[0] == 3) {
    quizWindow.document.write('var isComCom = 1;\n\n');
  }
  else {
    quizWindow.document.write('var isComCom = 0;\n\n');
  }
  for (currCount = 0; currCount < (tempReact[1] + tempReact[2]); currCount++) {
    quizWindow.document.write('trueCoeff[' + currCount + '] = ' + 
      tempReact[4 + (currCount*2)] + ';\n');
  }

  // actionReaction

  quizWindow.document.write('\nfunction actionReaction(whichAction) {\n' +
    'var tempValue;\nvar whichAction;\n' +
    'if (whichAction == \'Reveal!\') {\n' +
    'revealResults();\n}\nelse if (whichAction == \'Grade It!\'){\n' +
    'userCoeffFill();\nverifyAnswer();\n}\nelse {\n' +
    'opener.anotherChemEqnQuiz(1);\n}\n}\n\n');

  // userCoeffFill

  quizWindow.document.write('function userCoeffFill() {\nisBlank = 0;\n');
  for (currCount = 0; currCount < (tempReact[1] + tempReact[2]); currCount++) {
    if (currCount < tempReact[1]) {
      tempValue = currCount + 1;
      tempValue = 'react' + tempValue;
    }
    else {
      tempValue = currCount - (tempReact[1] - 1);
      tempValue = 'prod' + tempValue;
    }
    quizWindow.document.write('if (!window.document.reaction.' + tempValue +
      '.value) {\nisBlank = 1;\n}\nelse {\nuserCoeff[' + currCount + '] = ' +
      'window.document.reaction.' + tempValue + '.value;\n}\n');
  }
  quizWindow.document.write('}\n\n');

  // revealResults

  quizWindow.document.write('function revealResults() {\n');
  for (currCount = 0; currCount < (tempReact[1] + tempReact[2]); currCount++) {
    quizWindow.document.write('window.document.reaction.');
    if (currCount < tempReact[1]) {
      tempValue = currCount + 1;
      quizWindow.document.write('react' + tempValue);
    }
    else {
      tempValue = currCount - (tempReact[1] - 1);
      quizWindow.document.write('prod' + tempValue);
    }
    quizWindow.document.write('.value = trueCoeff[' + currCount + '];\n');
  }
  quizWindow.document.write('}\n\n');

  // checkMultiple

  quizWindow.document.write('function checkMultiple() {\n' +
    'var multipleFactor;\nvar currCount;\ncoeffOK = 1;\n\n');
  quizWindow.document.write('for (currCount = 0; currCount < (trueCoeff.length); currCount++) {\n' +
    'if (userCoeff[currCount] == 0) {\ncoeffOK = 0;\n}\n}\nif (coeffOK) {\n');
  quizWindow.document.write('multipleFactor = userCoeff[0]/trueCoeff[0];\n' +
    'for (currCount = 1; currCount < (trueCoeff.length); currCount++) {\n' +
    'if ((userCoeff[currCount]\/trueCoeff[currCount]) != multipleFactor) {\n' +
    'coeffOK = 0;\n}\n}\nif (coeffOK) {\nisMultiple = 1;\n}\n}\n}\n\n');

  // verifyAnswer

  quizWindow.document.write('function verifyAnswer() {\n\n' +
    'var currCount;\ncoeffOK = 1;\nisMultiple = 0;\n\n' +
    'if (isBlank) {\nalertString = \'You have left one or more of the coefficients ' +
    'blank.\';\n}\n\n');
  quizWindow.document.write('else {\nif (');
  for (currCount = 0; currCount < (tempReact[1] + tempReact[2]); currCount++) {
    quizWindow.document.write('(userCoeff[' + currCount + '] != ' +
      'trueCoeff[' + currCount + '])');
    if (currCount < (tempReact[1] + tempReact[2] -1)) {
      quizWindow.document.write(' || ');
    }
  }
  quizWindow.document.write(') {\ncoeffOK = 0;\n}\n');
  quizWindow.document.write('if (!coeffOK) {\ncheckMultiple();\n}\n');
  quizWindow.document.write('if (coeffOK) {\nif (!isMultiple) {\n' +
    'alertString = \'You have balanced the equation.\';\n}\n' +
    'else {\nalertString = \'While you have balanced the equation, ' +
    'you have not used the smallest whole-number coefficients.\';\n}\n}\n' +
    'else {\nalertString = \'You have not balanced the equation correctly.\';\n' +
    '}\n}\nalert(alertString);\n}\n\n');
  printButtonChanges();
  quizWindow.document.write('<\/SCR' + 'IPT>\n\n<\/HEAD>\n');
}

function printTypeHeader() {
  var currCount;

  quizWindow.document.write('<HEAD>\n<TITLE>');
  quizWindow.document.write('Chemical Equations\' Quiz');
  quizWindow.document.write('<\/TITLE>\n<SCR' + 'IPT LANGUAGE=\"javascript\">\n');
  quizWindow.document.write('var trueReactType = ' + modReactType + ';\n' +
    'var userReactType;\nvar alertString;');
  if (tempReact[0] == 3) {
    quizWindow.document.write('var isComCom = 1;\n\n');
  }
  else {
    quizWindow.document.write('var isComCom = 0;\n\n');
  }

  // actionReaction

  quizWindow.document.write('\nfunction actionReaction(whichAction) {\n' +
    'var tempValue;\nvar whichAction;\n' +
    'if (whichAction == \'Reveal!\') {\n' +
    'revealResults();\n}\nelse if (whichAction == \'Grade It!\'){\n' +
    'verifyAnswer();\n}\nelse {\n' +
    'opener.anotherChemEqnQuiz(0);\n}\n}\n\n');

  // revealResults

  quizWindow.document.write('function revealResults() {\n');
  quizWindow.document.write('window.document.reaction.reacttype[trueReactType].selected = true;\n');
  quizWindow.document.write('}\n\n');

  // verifyAnswer

  quizWindow.document.write('function verifyAnswer() {\n\n' +
    'var currCount;\n\n' +
    'for (currCount = 0; currCount < 8; currCount++) {\n' +
    'if (window.document.reaction.reacttype[currCount].selected == true) {\n' +
    'userReactType = currCount;\n}\n}\n\n' +
    'if (!userReactType) {\nalertString = \'You need to choose a type of reaction.\';\n}\n' +
    'else {\n');
  quizWindow.document.write('if (userReactType == trueReactType) {\nalertString = \'' +
    '\\nCorrect!\';\n}\nelse {\n' +
    'if ((isComCom) && ((userReactType == 1) || (userReactType == 3))) {\n' +
    'alertString = \'\\nCorrect!\';\n}\n' +
    'else {\nalertString = \'\\nIncorrect!\';\n}\n}\n}\n');
  quizWindow.document.write('alert(alertString);\n}\n\n');
  printButtonChanges();
  quizWindow.document.write('<\/SCR' + 'IPT>\n\n<\/HEAD>\n');
}

function printConvHeader() {
//  alert('got here');
  quizWindow.document.write('<HEAD>\n<TITLE>');
  quizWindow.document.write('Chemical Equations\' Quiz');
  quizWindow.document.write('<\/TITLE>\n<SCR' + 'IPT LANGUAGE=\"javascript\">\n');
  quizWindow.document.write('function actionReaction(whichAction,whichQuiz) {\n' +
    'var whichAction;\nvar whichQuiz;\n\nif (whichAction == \'Grade It!\') {\n' +
    'opener.convGrade();\n' +
    '}\nelse if (whichAction == \'Reveal!\') {\n' +
    'opener.convReveal();\n' +
    '}\nelse {\nopener.anotherChemEqnQuiz(' + quizType + ');\n' +
    '}\n}\n');
  printButtonChanges();
  quizWindow.document.write('<\/SCR' + 'IPT>\n');
  printStyleSheet();
  quizWindow.document.write('\n<\/HEAD>\n');
}

function convGrade() {
  var tempNumber;
  var alertMessage;
  var verifyAnswer;

  if (!revealedAnswer) {
    if (quizType == 2) {
      verifyAnswer = formulaWeight;
    }
    else if (quizType == 3) {
      verifyAnswer = answer;
    }

    alertMessage = 'Correct!';
    tempNumber = quizWindow.document.convQuiz.userNumber.value;
    prnAnswer = tempNumber;
    if (!tempNumber) {
      alertMessage = 'You need to enter a number in order to grade it.';
    }
    else if (tempNumber != verifyAnswer) {
      if (quizType != 3) {
        alertMessage = 'Incorrect!';
      }
      else {
        if (tempNumber.substr(0,1) == '.') {
          tempNumber = '0' + tempNumber;
          if (tempNumber != verifyAnswer) {
            alertMessage = 'Incorrect!';
          }
          else {
            convReveal(1);
          }
        }
      }
    }
    else {
      if (quizType == 3) {
        convReveal(1);
      }
    }

    if (quizType != 3) {
      quizWindow.alert(alertMessage);
    }
    else {
      if (alertMessage != 'Correct!') {
        quizWindow.alert(alertMessage);
      }
    }
  }
}

function convReveal(isCorrect) {
  var isCorrect;
  var revealAnswer;

  if (!revealedAnswer) {
    if (quizType == 2) {
      revealAnswer = formulaWeight;
    }
    else if (quizType == 3) {
      if (isCorrect != 1) {
        revealAnswer = answer;
      }
      else {
        revealAnswer = prnAnswer;
        show('answerCorrect');
      }
      show('answer');
      if (!(is_nn && !is_geck)) {
        hide('quizButtons');
      }
    }
    quizWindow.document.convQuiz.userNumber.value = revealAnswer;
//    revealedAnswer = 1;
  }
}

function printConvQuiz(isCorrect) {
  var isCorrect;

  quizWindow.document.write('<FORM NAME=\"convQuiz\" ' +
    'onSubmit=\"actionReaction(button,' + quizType + '); return false\">\n' +
    '<DIV ALIGN=\"center\">\n');
  if (quizType == 2) {
    quizWindow.document.write('<P>Calculate the formula weight for <B>' + modMol +
      '<\/B><\/P>\n');
    quizWindow.document.write('<P><INPUT NAME=\"userNumber\" TYPE=\"text\" ' +
      'SIZE=\"10\">&nbsp;g/mol\n<\/DIV>\n');
    quizWindow.document.write('<DIV ALIGN=\"center\">\n' +
      '(reminder- use atomic masses from the WebElements Periodic Table and round ' +
      'your answer to the appropriate number of decimal places)\n<\/DIV>\n');
  }
  else if (quizType == 3) {
    quizWindow.document.write('<P>Calculate the number of <B>' + prnUnit[1] +
      '<\/B> that are in <B>' + prnNum[0] + '&nbsp;' + prnUnit[0] + '<\/B> ' +
      'of <B>' + modMol + '<\/B>.<\/P>\n');
    quizWindow.document.write('<P><INPUT NAME=\"userNumber\" TYPE=\"text\" ' +
      'SIZE=\"16\">&nbsp;' + prnUnit[1] + '\n<\/DIV>\n');
    quizWindow.document.write('<DIV ALIGN=\"center\">\n' +
      '(reminder- use atomic masses from the WebElements Periodic Table and round ' +
      'your answer to the appropriate number of decimal places)\n<\/DIV>\n');
    printCorrect();
    quizWindow.document.write('<DIV ID=\"answer\" CLASS=\"hideIt\" ALIGN=\"center\">' + prnEqn[0] + '<\/DIV>\n');
  }
  printGradeButtons();
  quizWindow.document.write('<\/FORM>\n');
}

function chemConvFW() {
// Choose random substance and calculate the formula weight
  var atomSymb = new Array();
  var atomNum = new Array();
  var tempMol;
  var currAtomNum;
  var currChar;
  var upperCheck = /[A-Z]/;
  var lowerCheck = /[a-z]/;
  var numCheck = /\d/;
  var leftParenCheck = /\(/;
  var rightParenCheck = /\)/;
  var parenOn;
  var parenAtomBegin;
  var parenNum;
  var notDone;
  var subNotDone;
  var formulaWeightNum;
  var tempDecimal;

  atomType = [];
  atomNum = [];

  // Choose random substance and remove state label
  notDone = 1;
  while(notDone) {
    tempMolNum = Math.round((mol.length - 1)*Math.random());
    if (((tempMolNum > 53) && (tempMolNum < 70)) ||
        ((tempMolNum > 71) && (tempMolNum < 74)) ||
        (tempMolNum == 157) || (tempMolNum == 158)) {
      notDone = 1;
    }
    else {
      notDone = 0;
    }
  }
//  tempMolNum = 53; // Testing
  if (mol[tempMolNum].substr(mol[tempMolNum].length-3,1) == '(') {
    tempMol = mol[tempMolNum].substr(0,mol[tempMolNum].length-3);
  }
  else {
    tempMol = mol[tempMolNum].substr(0,mol[tempMolNum].length-4);
  }
  holdMol = tempMol;
  modifyMolecule();

  // Separate out the individual atoms
  currAtomNum = 0;
  currChar = 0;
  parenOn = 0;
  notDone = 1;
  while (notDone) {
    if (tempMol.substr(currChar,1).match(upperCheck)) {
    // Uppercase character
      atomSymb[currAtomNum] = tempMol.substr(currChar,1);
      if (tempMol.substr(currChar + 1,1).match(lowerCheck)) {
      // The next character is part of a 2-character symbol
        currChar++;
        atomSymb[currAtomNum] += tempMol.substr(currChar,1)
      }
      if (tempMol.substr(currChar + 1,1).match(numCheck)) {
      // The next character is part of a subscript for the current atom
        atomNum[currAtomNum] = '';
        subNotDone = 1;
        while (subNotDone) {
          currChar++;
          atomNum[currAtomNum] += tempMol.substr(currChar,1);
          if (!tempMol.substr(currChar + 1,1).match(numCheck)) {
            subNotDone = 0;
          }
        }
      }
      else {
        atomNum[currAtomNum] = '1';
      }
      currAtomNum++;
    }
    else if (tempMol.substr(currChar,1).match(leftParenCheck)) {
    // Left paren
      parenOn = 1;
      parenAtomBegin = currAtomNum;
    }
    else if (tempMol.substr(currChar,1).match(rightParenCheck)) {
    // Right paren
      parenOn = 0;
      if (tempMol.substr(currChar + 1,1).match(numCheck)) {
      // The next character is part of a subscript for the atoms in parens
        parenNum = '';
        subNotDone = 1;
        while (subNotDone) {
          currChar++;
          parenNum += tempMol.substr(currChar,1);
          if (!tempMol.substr(currChar + 1,1).match(numCheck)) {
            subNotDone = 0;
          }
        }
      }
      if (parenNum) {
      // Need to modify atom subscripts in parens to account for paren subscript
        for (var i=parenAtomBegin; i<currAtomNum; i++) {
          atomNum[i] = ((atomNum[i]-0)*(parenNum-0)) + '';
        }
      }
    }

    currChar++;
    if (currChar >= tempMol.length) {
      notDone = 0;
    }
  }

  // Calculate formula weight and determine sig figs for addition
  // For Chemistry and the Citizen, arbitrarily set the number of decimal places to 2

  formulaWeightNum = 0;

  formulaWeightDecimal = 500; // Set the initial number of decimal places to a huge value
  for (var i=0; i<atomNum.length; i++) {
    formulaWeightNum += (atomMass[atomSymb[i]]-0)*(atomNum[i]-0);
    tempDecimal = atomMass[atomSymb[i]].length - (atomMass[atomSymb[i]].search(/\./) + 1);
    if (tempDecimal < formulaWeightDecimal) {
      formulaWeightDecimal = tempDecimal;
    }
  }
  if (is_citizen) {
    formulaWeightDecimal = 2;
  }
  formulaWeightNum = Math.round(formulaWeightNum*Math.pow(10,formulaWeightDecimal)) + '';
  formulaWeight = formulaWeightNum.substr(0,formulaWeightNum.length-formulaWeightDecimal) + '.' +
                  formulaWeightNum.substr(formulaWeightNum.length-formulaWeightDecimal);

//  alert(mol[tempMolNum] + '\n' + tempMol + '\n' + atomSymb + '\n' + atomNum + '\n' + formulaWeight); // Testing
}

function chemConv() {
  var notDone;

  prnNum = [];
  prnEqn = [];

  // Choose random starting number but only wat 2-4 sig figs
  chooseSigFig(2,3);
  notDone = 1;
  while (notDone) {
    detNumSigFigs(answer);
    if ((numSigFigs < 2) || (numSigFigs > 3)) {
      chooseSigFig(2,3);
    }
    else {
      prnNum[0] = answer;
      notDone = 0;
    }
  }
//  alert(answer);

  // Choose either g to mol or mol to g
  chooseRandomNum(1);
  if (randomNum) {
  // grams to moles
    prnUnit[0] = 'g';
    prnUnit[1] = 'mol';
  }
  else {
  // moles to grams
    prnUnit[0] = 'mol';
    prnUnit[1] = 'g';
  }

  chemConvFW();
  if (prnUnit[0] == 'g') {
  // grams to moles
    answer = ((prnNum[0] - 0)/(formulaWeight - 0)) + '';
  }
  else {
  // moles to grams
    answer = ((prnNum[0] - 0)*(formulaWeight - 0)) + '';
  }
  doSigFigs();
  convert1();
  modBasicConv();
}

function chemEqnQuiz(isCorrect) {
  var isCorrect;
  var currCount;

  if (quizType < 2) {
    chooseRandomReaction();
    tempReact = [];
    tempReact = react[reactNum].split(',');
    reactLength = tempReact.length;
    for (currCount = 0; currCount < reactLength; currCount++) {
       tempReact[currCount] = tempReact[currCount] - 0;
    }
    assignReactionType();
  }
  else if (quizType == 2) {
//    alert('got here');
    chemConvFW();
  }
//  else if ((quizType == 3) && !revealConv) {
  else if (quizType == 3) {
    chemConv();
  }
  quizWindowOpened = 1;
  quizWindow = window.open('','quizWindowName','resizable=1,scrollbars=1,status=1,width=' + quizWidth + ',height=' + quizHeight);
  quizWindow.document.write('<HTML>\n');
  if (quizType == 1) {
    printBalanceHeader();
  }
  else if (quizType == 0) {
    printTypeHeader();
  }
  else if (quizType > 1) {
    printConvHeader();
  }
  quizWindow.document.write('<BODY STYLE=\"background-color: #FFFFFF\">\n');
  quizWindow.document.write('<TABLE ALIGN=\"center\" BORDER=\"0\"><TR>\n' +
    '<TD>\n<H2>' + quizName[quizType] + '<\/H2>\n<\/TD>\n' +
    '<\/TR>\n<TR>\n<TD><HR><\/TD>\n<\/TR>\n</TABLE>\n');
  if (quizType < 2) {
    printReaction();
  }
  else if (quizType > 1) {
    revealedAnswer = 0;
    printConvQuiz(isCorrect);
  }
  printCloseButtons();
  printAddress();
  quizWindow.document.write('<\/BODY>\n<\/HTML>');
  quizWindow.document.close();
  quizWindow.focus();
}

function printCloseButtons() {
  quizWindow.document.write('<DIV ALIGN=\"center\">\n<FORM>\n' +
      '<INPUT TYPE=\"button\" onClick=\"window.close()\"\n ' +
      'onMouseOver=\"mOver(document.forms.length - 1,0);self.status=\'Close this window\';return true\"\n ' +
      'onMouseOut=\"mOut(document.forms.length - 1,0);self.status=\'\';return true\"\n ' +
      'VALUE=\"Close Window\">\n' +
      '<INPUT TYPE=\"button\" onClick=\"opener.focus()\"\n ' +
      'onMouseOver=\"mOver(document.forms.length - 1,1);self.status=\'Switch back to the tutorial\';' +
      'return true\"\n onMouseOut=\"mOut(document.forms.length - 1,1);self.status=\'\';return true\"\n ' +
      'VALUE=\"Switch Windows\">\n' +
      '<\/FORM>\n<\/DIV>\n\n');
}

function printAddress() {
  quizWindow.document.write('<HR>\n<DIV>\n');
  if (quizType < 2) {
    quizWindow.document.write('<A HREF=\"javascript: opener.openHelpWindow(' +
      quizType + ')\"><IMG ALT=\"help!\" ' +
      'BORDER=\"0\" ALIGN=\"right\" SRC=\"..\/..\/Images\/help-button.gif\" ' +
      'HEIGHT=\"37\" WIDTH=\"62\"><\/A>\n');
  }
  quizWindow.document.write('Script last modified: ' + dateMod + 
    '<BR><A HREF=\"mailto:' + emailAdd + '\">' + emailAdd +
    '<\/A>\n<\/DIV>\n');
}

function printButtonChanges() {
  quizWindow.document.write('function mOver(whichForm,whichElement) {\n' +
    'var whichForm;\nvar whichElement;\n\n' +
    'document.forms[whichForm].elements[whichElement].style.backgroundColor = \'#000000\';\n' +
    'document.forms[whichForm].elements[whichElement].style.color = \'#FFFFFF\';\n' +
    '}\n\n' +
    'function mOut(whichForm,whichElement) {\n' +
    'var whichForm;\nvar whichElement;\n\n' +
    'document.forms[whichForm].elements[whichElement].style.backgroundColor = \'\';\n' +
    'document.forms[whichForm].elements[whichElement].style.color = \'\';\n}\n\n');
}

function printStyleSheet() {
  quizWindow.document.write('<STYLE TYPE=\"text\/css\">\n<!--\n' +
    '.hideIt { position: relative; visibility: hidden; }\n' +
    '.showIt { position: relative; visibility: visible; }\n' +
    '.firstCell {\nborder-left-style: solid;\n' +
    'border-right-style: solid;\nborder-left-color: blue;\n' +
    'border-right-color: blue;\n}\n\n.otherCell {\n' +
    'border-right-style: solid;\nborder-right-color: blue;\n}\n\n' +
    '.uUnder {text-decoration: underline}\n' +
    '.uCan {text-decoration: line-through}\n\n-->\n<\/STYLE>\n');
}

function printCorrect() {
  quizWindow.document.write('<DIV ID=\"answerCorrect\" CLASS=\"hideIt\" ALIGN=\"center\">\n' +
    '<P><B>Correct!!<\/B><\/P>\n<\/DIV>\n\n');
}

function show(layer) {
  if (document.layers) {  // NN4 DOM
    quizWindow.document.layers[layer].visibility="visible";
  }
  else if ((document.all) && (!document.getElementById)) { // IE4 DOM
    quizWindow.document.all[layer].style.visibility="visible";
  }
  else if (document.getElementById) { // W3C standards-compliant DOM
    quizWindow.document.getElementById(layer).style.visibility="visible";
  }
}

function hide(layer) {
  if (document.layers) {  // NN4 DOM
    quizWindow.document.layers[layer].visibility="hide";
  }
  else if ((document.all) && (!document.getElementById)) { // IE4 DOM
    quizWindow.document.all[layer].style.visibility="hidden";
  }
  else if (document.getElementById) { // W3C standards-compliant DOM
    quizWindow.document.getElementById(layer).style.visibility="hidden";
  }
}

function showHide(layer1,layer2) {
  show(layer1);
  hide(layer2);
}
