var gasEqn = new Array();
var prnEqn = new Array();
var prnNum = new Array();
var prnUnit = new Array();
var prnSymb = new Array();
var prnCombQuest = new Array();
var idealQuest = new Array();
var prnChoice = new Array();
var pUnit = new Array();
var tUnit = new Array();
var vUnit = new Array();
var gasConst = new Array();
var whichDispEqn;
var showAnswer;
var gradeAnswer;
var quizWindowOpened;
var quizWidth;
var quizHeight;
var quizType;
var randomNum;
var chooseFromAll;
var calcInit;
var whichPUnit;
var whichTUnit;
var whichVUnit;
var preHold;
var postHold;
var calcCombFinal;
var answer;
var answerUnit;
var prnAnswer;
var numSigFigs;
var userCorrect;
var prnGradeResult;
var isAmbiguous;
var prnIdealQuest;
var whichIdealVar;
var emailAdd = 'chemistry@chemistry.alanearhart.org';
var dateMod = '7\/25\/06';

 prnCombQuest[0] = 'pressure';
 prnCombQuest[1] = 'volume';
 prnCombQuest[2] = 'volume';
 prnCombQuest[3] = 'temperature';
 prnCombQuest[4] = 'pressure';
 prnCombQuest[5] = 'temperature';
 prnCombQuest[6] = 'pressure';
 prnCombQuest[7] = 'volume';
 prnCombQuest[8] = 'temperature';
 prnCombQuest[9] = 'pressure';
prnCombQuest[10] = 'volume';
prnCombQuest[11] = 'moles';
prnCombQuest[12] = 'temperature';

idealQuest[0] = 'pressure';
idealQuest[1] = 'volume';
idealQuest[2] = 'temperature';
idealQuest[3] = '# of moles';

prnSymb[0] = 'P<SUB>1<\/SUB>';
prnSymb[1] = 'P<SUB>2<\/SUB>';
prnSymb[2] = 'V<SUB>1<\/SUB>';
prnSymb[3] = 'V<SUB>2<\/SUB>';
prnSymb[4] = 'T<SUB>1<\/SUB>';
prnSymb[5] = 'T<SUB>2<\/SUB>';

pUnit[0] = 'atm';
pUnit[1] = 'torr';

tUnit[0] = '&degC';
tUnit[1] = 'K';

vUnit[0] = 'mL';
vUnit[1] = 'L';

prnChoice[0] = 'initial';
prnChoice[1] = 'final';

// Numeric value for the gas constants

gasConst[0] = '0.08206'; // Pressure unit is atm
gasConst[1] = '62.36';   // Pressure unit is torr

// Basic Gas Law Formulas

  // Boyle's Law

gasEqn[0] = '<TABLE BORDER=\"0\"><TR><TD>P<SUB>1<\/SUB>V<SUB>1<\/SUB>' +
            '<\/TD><TD>=<\/TD><TD>P<SUB>2<\/SUB>V<SUB>2<\/SUB><\/TD>' +
            '<\/TR><\/TABLE>';

  // Charles' Law

gasEqn[1] = '<TABLE BORDER=\"0\"><TR><TD ALIGN=\"center\">V<SUB>1<\/SUB>' +
            '<\/TD><TD ROWSPAN=\"3\" ALIGN=\"center\">=<\/TD>' +
            '<TD ALIGN=\"center\">V<SUB>2<\/SUB><\/TD><\/TR>' +
            '<TD ALIGN=\"center\"><HR><\/TD><TD ALIGN=\"center\"><HR><\/TD>' +
            '<TR><TD ALIGN=\"center\">T<SUB>1<\/SUB><\/TD>' +
            '<TD ALIGN=\"center\">T<SUB>2<\/SUB><\/TD><\/TR><\/TABLE>';

  // Gay-Lussac's Law

gasEqn[2] = '<TABLE BORDER=\"0\"><TR><TD ALIGN=\"center\">P<SUB>1<\/SUB>' +
            '<\/TD><TD ROWSPAN=\"3\" ALIGN=\"center\">=<\/TD>' +
            '<TD ALIGN=\"center\">P<SUB>2<\/SUB><\/TD><\/TR>' +
            '<TD ALIGN=\"center\"><HR><\/TD><TD ALIGN=\"center\"><HR><\/TD>' +
            '<TR><TD ALIGN=\"center\">T<SUB>1<\/SUB><\/TD>' +
            '<TD ALIGN=\"center\">T<SUB>2<\/SUB><\/TD><\/TR><\/TABLE>';

  // Combined Gas Law

gasEqn[3] = '<TABLE BORDER=\"0\"><TR><TD ALIGN=\"center\">P<SUB>1<\/SUB>' +
            'V<SUB>1<\/SUB><\/TD><TD ROWSPAN=\"3\" ALIGN=\"center\">=<\/TD>' +
            '<TD ALIGN=\"center\">P<SUB>2<\/SUB>V<SUB>2<\/SUB><\/TD><\/TR>' +
            '<TD ALIGN=\"center\"><HR><\/TD><TD ALIGN=\"center\"><HR><\/TD>' +
            '<TR><TD ALIGN=\"center\">T<SUB>1<\/SUB><\/TD>' +
            '<TD ALIGN=\"center\">T<SUB>2<\/SUB><\/TD><\/TR><\/TABLE>';

  // Ideal Gas Law

gasEqn[4] = '<TABLE BORDER=\"0\"><TR><TD ALIGN=\"center\">' +
            'PV&nbsp;=&nbsp;nRT<\/TD><\/TR><\/TABLE>';

// Gas law equations solved for a variable
//   Up to 6 variables, V(1-6), with their
//   corresponding units, U(1-6)

  // Boyle's, Charles', and Gay-Lussac's Law

  //   All of these gas laws have the same basic formula
  //   The difference comes in the variable substitutions

  //   V1 = V2U2 V3U3
  //        _________
  //          V4U4

gasEqn[5] = '<TABLE BORDER=\"0\"><TR><TD ROWSPAN=\"3\" ALIGN=\"center\">' +
            'V1<\/TD><TD ROWSPAN=\"3\" ALIGN=\"center\">=<\/TD>' +
            '<TD ALIGN=\"center\">V2U2V3U3<\/TD><\/TR><TR>' +
            '<TD ALIGN=\"center\"><HR><\/TD><\/TR><TR><TD ALIGN=\"center\">' +
            'V4U4<\/TD><\/TR><\/TABLE>';

  // Combined Gas Law

  //   V1 = V2U2 V3U3 V4U4
  //        ______________
  //          V5U5 V6U6  

gasEqn[6] = '<TABLE BORDER=\"0\"><TR><TD ROWSPAN=\"3\" ALIGN=\"center\">' +
            'V1<\/TD><TD ROWSPAN=\"3\" ALIGN=\"center\">=<\/TD>' +
            '<TD ALIGN=\"center\">V2U2V3U3V4U4<\/TD><\/TR>' +
            '<TD ALIGN=\"center\"><HR><\/TD><TR><TD ALIGN=\"center\">' +
            'V5U5V6U6<\/TD><\/TR><\/TABLE>';

  // Ideal Gas Law, solved for P or V

  //  V1 = V2U2 R V3U3
  //       ___________
  //          V4U4

gasEqn[7] = '<TABLE BORDER=\"0\"><TR><TD COLSPAN=\"2\" ALIGN=\"center\">' +
            '&nbsp;<\/TD><TD ALIGN=\"center\">V2U2<\/TD>' +
            '<TD ALIGN=\"center\">RGASCONSTANT<\/TD><TD ALIGN=\"center\">' +
            'V3U3<\/TD><\/TR><TR><TD ALIGN=\"center\">V1<\/TD>' +
            '<TD ALIGN=\"center\">=<\/TD>' +
            '<TD COLSPAN=\"3\" ALIGN=\"center\"><HR><\/TD><\/TR><TR>' +
            '<TD COLSPAN=\"2\" ALIGN=\"center\">&nbsp;<\/TD>' +
            '<TD COLSPAN=\"3\" ALIGN=\"center\">V4U4<\/TD><\/TR>' +
            '<\/TABLE>';

  // Ideal Gas Law, solved for n or T

  //  V1 = V2U2 V3U3
  //       _________
  //        V4U4 R

gasEqn[8] = '<TABLE BORDER=\"0\"><TR><TD COLSPAN=\"2\" ALIGN=\"center\">' +
            '&nbsp;<\/TD><TD COLSPAN=\"2\" ALIGN=\"center\">V2U2V3U3' +
            '<\/TD><\/TR><TR><TD ALIGN=\"center\">V1<\/TD>' +
            '<TD ALIGN=\"center\">=<\/TD><TD COLSPAN=\"2\" ALIGN=\"center\">' +
            '<HR><\/TD><\/TR><TR><TD COLSPAN=\"2\" ALIGN=\"center\">&nbsp;' +
            '<\/TD><TD ALIGN=\"center\">V4U4<\/TD><TD ALIGN=\"center\">' +
            'RGASCONSTANT<\/TD><\/TR><\/TABLE>';

function popUpGasQuiz(whichQuiz) {
  var whichQuiz;
  var tempHold;
  var tempQuiz;

  if (whichQuiz == 'Reveal!') {
    showAnswer = 1;
    gradeAnswer = 0;
  }
  else if (whichQuiz == 'Grade Me!') {
    showAnswer = 0;
    gradeAnswer = 1;
  }
  else {
    initValues();
    showAnswer = 0;
    gradeAnswer = 0;
    if (whichQuiz.substr(0,4) == 'Quiz') {
      chooseFromAll = 0;
      tempHold = whichQuiz.length;
      tempQuiz = whichQuiz.substr(tempHold - 2,2) - 0;
      if (tempQuiz == 1) {
        chooseRandomNum(8);
        quizType = randomNum;
        chooseRandomNum(1);
        calcInit = randomNum;
      }
      else if (tempQuiz == 2) {
        chooseRandomNum(3);
        quizType = randomNum + 9;
      }
      else if (tempQuiz == 3) {
        chooseFromAll = 1;
      // 60% chance of the Combined gas law
        chooseRandomNum(9);
        if (randomNum < 6) {
          chooseRandomNum(8)
          quizType = randomNum;
          chooseRandomNum(1);
          calcInit = randomNum;
        }
        else {
          chooseRandomNum(3);
          quizType = randomNum + 9;
        }
      }
//      quizType = 0;  // Testing
    }
    else {
      if (chooseFromAll) {
        chooseRandomNum(12);
        quizType = randomNum;
      }
      else {
        if (quizType < 9) {
          chooseRandomNum(8);
          quizType = randomNum;
//          quizType = 0;  // Testing
        }
        else {
          chooseRandomNum(3);
          quizType = randomNum + 9;
        }
      }
    }
  }

  printGasQuiz();
}

function initValues() {
  quizWindowOpened = 0;
  quizHeight = 475;
  quizWidth = 575;
}

function chooseRandomNum(rangeNum) {
  var rangeNum;

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

function printPart1Quiz(whichQuizType) {
  var whichQuizType;

  quizWindowOpened = 1;
  quizWindow = window.open('','quizWindowName','resizable=1,scrollbars=1,status=1,width=' + quizWidth + ',height=' + quizHeight);
  quizWindow.document.write('<HTML>\n<HEAD>\n' +
    '<TITLE>Tutorial: Gas Laws\' Quiz<\/TITLE>\n');
  quizWindow.document.write('<SCR' + 'IPT LANGUAGE=\"javascript\">\n');
  printButtonChanges();
  quizWindow.document.write('<\/SCR' + 'IPT>\n');
  printStyleSheet();
  quizWindow.document.write('<\/HEAD>\n<BODY STYLE=\"background-color: #FFFFFF\">');
  quizWindow.document.write('<H2 STYLE=\"text-align: center\">Gas Laws\' Quiz<\/H2>\n');
  if (!chooseFromAll) {
    quizWindow.document.write('<H3 STYLE=\"text-align: center\">' +
      whichQuizType + '<\/H3>\n');
  }
  else {
    quizWindow.document.write('<H3 STYLE=\"text-align: center\">Random Gas Law<\/H3>\n');
  }
  quizWindow.document.write('<HR STYLE=\"text-align: center\" WIDTH=\"75%\">\n' +
    '<DIV ALIGN=\"center\">\n' +
    '<FORM NAME=\"equation\" onSubmit=\"opener.popUpGasQuiz(button); return false\">\n');

  quizWindow.document.write('<TABLE BORDER=\"0\" CELLSPACING=\"10\">\n<TR>' +
    '<TD COLSPAN=\"6\" ALIGN=\"left\"><B>Given Values<\/B>:<\/TD>\n<\/TR>\n');
}

function printPart2CombQuiz() {
  quizWindow.document.write('<TR><TD COLSPAN=\"6\"><HR><\/TD><\/TR>\n' +
    '<TR>\n<TD COLSPAN=\"6" ALIGN=\"left\">\n' +
    '<TABLE BORDER=\"0\">\n<TR><TD NOWRAP>\n' +
    'The <B>' + prnChoice[calcCombFinal] + ' ' + prnCombQuest[quizType] + '<\/B> ' +
    ' is <\/TD>\n<TD ALIGN=\"center">\n' +
    '<INPUT NAME=\"userNumber\" TYPE=\"text\" SIZE=\"7\">' +
    '<\/TD>\n<TD>' + answerUnit);

  // Indicate if there are an ambiguous number of significant figures
  if ((showAnswer || gradeAnswer) && isAmbiguous) {
    quizWindow.document.write('&nbsp&nbsp&nbsp;(only <B>' + numSigFigs +
      '<\/B> significant figures)');
  }

  quizWindow.document.write('<\/TD>\n<\/TR>\n<\/TABLE>\n<\/TD>' +
    '<\/TABLE>\n<\/DIV>\n');
}

function printPart2IdealQuiz() {
  quizWindow.document.write('<TR><TD COLSPAN=\"6\"><HR><\/TD><\/TR>\n' +
    '<TR>\n<TD COLSPAN=\"6" ALIGN=\"left\">\n' +
    '<TABLE BORDER=\"0\">\n<TR><TD NOWRAP>\n' +
    'The <B>' + prnIdealQuest + '<\/B> ' +
    ' is <\/TD>\n<TD ALIGN=\"center">\n' +
    '<INPUT NAME=\"userNumber\" TYPE=\"text\" SIZE=\"7\">' +
    '<\/TD>\n<TD>' + answerUnit);

  // Indicate if there are an ambiguous number of significant figures
  if ((showAnswer || gradeAnswer) && isAmbiguous) {
    quizWindow.document.write('&nbsp&nbsp&nbsp;(only <B>' + numSigFigs +
      '<\/B> significant figures)');
  }

  quizWindow.document.write('<\/TD>\n<\/TR>\n<\/TABLE>\n<\/TD>' +
    '<\/TABLE>\n<\/DIV>\n');
}

function printPart3Quiz() {
  quizWindow.document.write('<DIV ALIGN=\"center\">\n' +
    '<TABLE BORDER=\"0\"><TR>');
  if (!showAnswer && !gradeAnswer) {
    quizWindow.document.write('<TD>\n<INPUT TYPE=\"submit\"\n' +
      'onClick=\"button=this.value\"\n' +
      'onMouseOver=\"mOver(document.forms.length - 2,document.equation.elements.length - 3);self.status=\'Grade your answer\';return true\"\n' +
      'onMouseOut=\"mOut(document.forms.length - 2,document.equation.elements.length - 3);self.status=\'\';return true\"\n' +
      'VALUE=\"Grade Me!\">\n<\/TD>');
  }
  if (!showAnswer && !gradeAnswer) {
    quizWindow.document.write('<TD>\n<INPUT TYPE=\"submit\"\n' +
      'onClick=\"button=this.value\"\n' +
      'onMouseOver=\"mOver(document.forms.length - 2,document.equation.elements.length - 2);self.status=\'Reveal answer and solution\';return true\"\n' +
      'onMouseOut=\"mOut(document.forms.length - 2,document.equation.elements.length - 2);self.status=\'\';return true\"\n' +
      'VALUE=\"Reveal!\">\n<\/TD>');
  }
  quizWindow.document.write('<TD>\n<INPUT TYPE=\"submit\"\n' +
    'onClick=\"button=this.value\"\n' +
    'onMouseOver=\"mOver(document.forms.length - 2,document.equation.elements.length - 1);self.status=\'New Quiz\';return true\"\n' +
    'onMouseOut=\"mOut(document.forms.length - 2,document.equation.elements.length - 1);self.status=\'\';return true\"\n' +
    'VALUE=\"Gimme\' New One!\">\n<\/TD><\/TR>\n<\/TABLE>\n<\/FORM>\n');
  quizWindow.document.write('<TABLE BORDER=\"0\"><TR><TD>\n<FORM>\n<INPUT TYPE=\"button\"\n' +
    '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<\/FORM>\n');
  quizWindow.document.write('<\/TD><\/TR><\/TABLE>\n<\/DIV>\n');
  printAddress();
  quizWindow.document.write('<\/BODY>\n<\/HTML>');
  quizWindow.focus();
  quizWindow.document.close();
}

function printGasQuiz() {
  var theQuestion;
  var combP;
  var combV;
  var combT;
  var idealVar = new Array();

  if (quizType < 9) {
  // Combined Gas Law and Variants

    if (!showAnswer && !gradeAnswer) {
    // First time through so we need to do everything

      combP = 0;
      combV = 0;
      combT = 0;
      if ((quizType != 2) && (quizType != 3)){
      // Boyle's, Gay-Lussac's, or Combined Gas Law
      //   Need pressure

        combP = 1;
      }
      if ((quizType != 4) && (quizType != 5)){
      // Boyle's, Charles', or Combined Gas Law
      //   Need volume

        combV = 1;
      }
      if ((quizType != 0) && (quizType != 1)){
      // Charles', Gay-Lussac's, or Combined Gas Law
      //   Need temperature

        combT = 1;
      }

      assembleCombinedEqn(combP,combV,combT);

      printPart1Quiz('Combined & Variants\' Gas Laws');
      printGivenCombined();
      printPart2CombQuiz();
      printPart3Quiz();
    }
    else if (showAnswer) {
      printPart1Quiz('Combined & Variants\' Gas Laws');
      printGivenCombined();
      printPart2CombQuiz();
      printCombinedSolution();
      printPart3Quiz();
      quizWindow.document.equation.userNumber.value = answer;
    }
    else if (gradeAnswer) {
      prnGradeResult = '';
      userCorrect = 1;
      if (!quizWindow.document.equation.userNumber.value) {
        userCorrect = 0;
        prnGradeResult = 'You need to enter your answer for the ';
        prnGradeResult += prnChoice[calcCombFinal] + ' ' + prnCombQuest[quizType];
        prnGradeResult += ' in order to have something to grade.';
      }
      else {
        gradeUserResult();
      }
      if (!userCorrect) {
        quizWindow.alert(prnGradeResult);
      }
      else {
        printPart1Quiz('Combined & Variants\' Gas Laws');
        printGivenCombined();
        printPart2CombQuiz();
        quizWindow.document.write('<P ALIGN=\"center\"><B>Correct!!<\/B><\/P>\n');
        printCombinedSolution();
        printPart3Quiz();
        quizWindow.document.equation.userNumber.value = answer;
      }
    }
  }
  else {
  // Ideal Gas Law

    if (!showAnswer && !gradeAnswer) {
    // First time through so we need to do everything

    // Determine if we need to calculate pressure, volume, temperature, or moles
      idealVar[0] = 1;
      idealVar[1] = 1;
      idealVar[2] = 1;
      idealVar[3] = 1;
    // Whichever random number that comes back will be the one we'll solve for
      chooseRandomNum(3);
      idealVar[randomNum] = 0;
//      idealVar[3] = 0; // Testing
      prnIdealQuest = idealQuest[randomNum];
      assembleIdealEqn(idealVar[0],idealVar[1],idealVar[2],idealVar[3]);

      printPart1Quiz('Ideal Gas Law');
      printGivenIdeal();
      printPart2IdealQuiz();
      printPart3Quiz();
    }
    else if (showAnswer) {
      printPart1Quiz('Ideal Gas Law');
      printGivenIdeal();
      printPart2IdealQuiz();
      printIdealSolution();
      printPart3Quiz();
      quizWindow.document.equation.userNumber.value = answer;
    }
    else if (gradeAnswer) {
      prnGradeResult = '';
      userCorrect = 1;
      if (!quizWindow.document.equation.userNumber.value) {
        userCorrect = 0;
        prnGradeResult = 'You need to enter your answer for the ';
        prnGradeResult += prnIdealQuest;
        prnGradeResult += ' in order to have something to grade.';
      }
      else {
        gradeUserResult();
      }
      if (!userCorrect) {
        quizWindow.alert(prnGradeResult);
      }
      else {
        printPart1Quiz('Ideal Gas Law');
        printGivenIdeal();
        printPart2IdealQuiz();
        quizWindow.document.write('<P ALIGN=\"center\"><B>Correct!!<\/B><\/P>\n');
        printIdealSolution();
        printPart3Quiz();
        quizWindow.document.equation.userNumber.value = answer;
      }
    }
  }
}

function gradeUserResult() {
  var tempUserResult;
  var charPos;
  var tempLength;

  tempUserResult = quizWindow.document.equation.userNumber.value;
  if (tempUserResult != answer) {
    if (answer.substr(0,1) == '0') {
      if (tempUserResult != answer.substr(1)) {
        userCorrect = 0;
        prnGradeResult = 'Incorrect!';
        tempLength = tempUserResult.length;
        charPos = tempUserResult.search(/\./);
        if (charPos != -1) {
          tempLength--;
          if (tempUserResult.substr(0,1) == '0') {
            tempLength--;
          }
        }
        if (tempLength != numSigFigs) {
          prnGradeResult += '\nYou also need to check your number of significant figures.';
        }
      }
    }
    else if ((tempUserResult - 0) == (answer - 0)) {
      userCorrect = 0;
      prnGradeResult = 'Incorrect!\nYou have the wrong number of significant figures.';
    }
    else {
      userCorrect = 0;
      prnGradeResult = 'Incorrect!';
      tempLength = tempUserResult.length;
      charPos = tempUserResult.search(/\./);
      if (charPos != -1) {
        tempLength--;
        if (tempUserResult.substr(0,1) == '0') {
          tempLength--;
        }
      }
      if (tempLength != numSigFigs) {
        prnGradeResult += '\nYou also need to check your number of significant figures.';
      }
    }
  }
}

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 assembleIdealEqn(chooseP,chooseV,chooseT,chooseN) {
  var chooseP;
  var chooseV;
  var chooseT;
  var chooseN;

  // Zero the values and the units used for the calculation
  prnNum = [];

  // Need to choose pressure unit
  chooseRandomNum(1);
  whichPUnit = randomNum;
  prnUnit[0] = pUnit[whichPUnit];
  if (chooseP) {
  // Choose pressure unit (0=atm, 1=torr)
    choosePressure();
    prnNum[0] = preHold;
    if (postHold != '') {
      prnNum[0] += '.' + postHold;
    }
//  prnNum[0] = '4.239';  // Testing
  }
  else {
    prnNum[0] = '';
    whichIdealVar = 0;
  }

  // Need to choose volume unit
  // Set it to 1 (1=L)
  //chooseRandomNum(1);
  //whichVUnit = randomNum;
  whichVUnit = 1;
  prnUnit[1] = vUnit[whichVUnit];
  if (chooseV) {
    chooseVolume();
    prnNum[1] = preHold;
    if (postHold != '') {
      prnNum[1] += '.' + postHold;
    }
//  prnNum[1] = '4.239';  // Testing
  }
  else {
    prnNum[1] = '';
    whichIdealVar = 1;
  }

  // Need to choose temperature unit
  // Set it to 1 (1=K)
  //chooseRandomNum(1);
  //whichTUnit = randomNum;
  whichTUnit = 1;
  prnUnit[2] = tUnit[whichTUnit];
  if (chooseT) {
    chooseTemperature();
    prnNum[2] = preHold;
    if (postHold != '') {
      prnNum[2] += '.' + postHold;
    }
//  prnNum[2] = '244.3';  // Testing
  }
  else {
    prnNum[2] = '';
    whichIdealVar = 2;
  }

  // Set the mole unit to... mol!
  prnUnit[3] = 'mol';
  if (chooseN) {
    chooseMole();
    prnNum[3] = preHold;
    if (postHold != '') {
      prnNum[3] += '.' + postHold;
    }
//  prnNum[3] = '4.239';  // Testing
  }
  else {
    prnNum[3] = '';
    whichIdealVar = 3;
  }

  // Assign the print equations
  //
  // prnEqn[0] is the Ideal Gas equation
  // prnEqn[1] is the equation solved for P, V, n, or T
  // prnEqn[2] is the solved equation with numbers and units

  prnEqn[0] = gasEqn[4];
  if (whichIdealVar < 2) {
    prnEqn[1] = gasEqn[7];
    prnEqn[2] = gasEqn[7];
  }
  else {
    prnEqn[1] = gasEqn[8];
    prnEqn[2] = gasEqn[8];
  }

  calcIdeal();

  detNumSigFigs(prnNum[0],prnNum[1],prnNum[2],prnNum[3]);
//  alert(numSigFigs);  // Testing
  doSigFigs();
}

function assembleCombinedEqn(chooseP,chooseV,chooseT) {
  var chooseP;
  var chooseV;
  var chooseT;
  var notDone;
  var i;
  var answerHold;

  // Zero the values and the units used for the calculation
  prnNum = [];
  prnUnit = [];

  if (chooseP) {
  // Choose pressure unit (0=atm, 1=torr)
    chooseRandomNum(1);
    whichPUnit = randomNum;
    prnUnit[0] = pUnit[whichPUnit];
    prnUnit[1] = pUnit[whichPUnit];
  // Choose pressure values
    for (i = 0; i < 2; i++) {
      choosePressure();
      prnNum[i] = preHold;
      if (postHold != '') {
        prnNum[i] += '.' + postHold;
      }
    }
//    prnNum[0] = '4.239';  // Testing
//    prnNum[1] = ''; // Testing
  }

  if (chooseV) {
  // Choose volume unit (0=mL, 1=L)
    chooseRandomNum(1);
    whichVUnit = randomNum;
    prnUnit[2] = vUnit[whichVUnit];
    prnUnit[3] = vUnit[whichVUnit];
  // Choose volume values
    for (i = 0; i < 2; i++) {
      chooseVolume();
      prnNum[i + 2] = preHold;
      if (postHold != '') {
        prnNum[i + 2] += '.' + postHold;
      }
    }
//    prnNum[2] = '12.91';  // Testing
//    prnNum[3] = '11.82';  // Testing
  }

  if (chooseT) {
  // Choose temperature unit (0=deg C, 1=K)
    chooseRandomNum(1);
    whichTUnit = randomNum;
    whichTUnit = 1;  // Set to Kelvin for the time being
    prnUnit[4] = tUnit[whichTUnit];
    prnUnit[5] = tUnit[whichTUnit];
  // Choose temperature values
    for (i = 0; i < 2; i++) {
      chooseTemperature();
      prnNum[i + 4] = preHold;
      if (postHold != '') {
        prnNum[i + 4] += '.' + postHold;
      }
    }
//    prnNum[4] = '467.26';  // Testing
//    prnNum[5] = '';  //Testing
  }

  // Assign the print equations

  //   prnEqn[0] is the basic equation (Boyle's, etc.)
  //   prnEqn[1] is the equation solved for a specific variable
  //   prnEqn[2] is the solved equation with numbers and units

  if (quizType < 2) {
  // Boyle's
    prnEqn[0] = gasEqn[0];
    whichDispEqn = 'Boyle\'s';
  }
  else if (quizType < 4) {
  // Charles'
    prnEqn[0] = gasEqn[1];
    whichDispEqn = 'Charles\'';
  }
  else if (quizType < 6) {
  // Gay-Lussac's
    prnEqn[0] = gasEqn[2];
    whichDispEqn = 'Gay-Lussac\'s';
  }
  else {
  // Combined
    prnEqn[0] = gasEqn[3];
    whichDispEqn = 'Combined Gas';
  }

  if (quizType < 6) {
    prnEqn[1] = gasEqn[5];
    prnEqn[2] = gasEqn[5];
  }
  else {
    prnEqn[1] = gasEqn[6];
    prnEqn[2] = gasEqn[6];
  }

  calcCombined();

  detNumSigFigs(prnNum[0],prnNum[1],prnNum[2],prnNum[3],prnNum[4],prnNum[5]);
//  alert(numSigFigs);  // Testing
  doSigFigs();
}

function choosePressure() {
  // whichPUnit = 0  atm
  //              1  torr

  var notDone;

  preHold = '';
  postHold = '';
  notDone = 1;
  while (notDone) {
    // Choose the number before the decimal point
    if (!whichPUnit) {
    // atm
      chooseRandomNum(4);
    }
    else {
    // torr
      chooseRandomNum(9);
      if (randomNum < 3) {
        chooseRandomNum(99);
      }
      else {
        chooseRandomNum(999);
      }
    }
    preHold = randomNum + '';

    // Choose the number after the decimal point
    if (!whichPUnit) {
    // atm
      chooseRandomNum(9);
      if (randomNum < 3) {
        chooseRandomNum(99);
      }
      else {
        chooseRandomNum(999);
      }
    }
    else {
    // torr
      chooseRandomNum(9);
      if (randomNum > 4) {
        chooseRandomNum(99);
      }
    }
    if ((randomNum == 0) && (preHold != '0')) {
    // The number after the decimal place is a '0'
    //   Give it a 50/50 shot between '0' and '' (nothing)
    //   Don't do this if the number before the decimal place is already a '0'
    //   This is important for significant figure purposes

      chooseRandomNum(1);
      if (randomNum) {
      // Make it a ''
        postHold = '';
      }
      else {
      // Keep it a '0'
        postHold = '0';
      }
    }
    else {
      postHold = randomNum + '';
    }

    if (!((preHold == '0') && (postHold == '0'))) {
    // Both are '0' and we can't have that so keep looping
      notDone = 0;
    }
  }
}

function chooseVolume() {
  // whichVUnit = 0  mL
  //            = 1  L

  var notDone;

  preHold = '';
  postHold = '';
  notDone = 1;
  while (notDone) {
    // Choose the number before the decimal point
    if (whichVUnit) {
      chooseRandomNum(19);
    }
    else {
      chooseRandomNum(999);
    }
    preHold = randomNum + '';

    // Choose the number after the decimal point
    chooseRandomNum(1);
    if (randomNum) {
      chooseRandomNum(9);
    }
    else {
      chooseRandomNum(99);
    }
    if ((randomNum == 0) && (preHold != '0')) {
    // The number after the decimal place is a '0'
    //   Give it a 50/50 shot between '0' and '' (nothing)
    //   Don't do this if the number before the decimal place is already a '0'
    //   This is important for significant figure purposes

      chooseRandomNum(1);
      if (randomNum) {
      // Make it a ''
        postHold = '';
      }
      else {
      // Keep it a '0'
        postHold = '0';
      }
    }
    else {
      postHold = randomNum + '';
    }

    if (!((preHold == '0') && (postHold == '0'))) {
    // Both are '0' and we can't have that so keep looping
      notDone = 0;
    }
  }
}

function chooseTemperature() {
  // Currently only does Kelvin units
  var notDone;

  preHold = '';
  postHold = '';
  notDone = 1;
  while (notDone) {
    // Choose the number before the decimal point
    chooseRandomNum(9);
    if (randomNum < 2) {
      chooseRandomNum(99);
    }
    else {
      chooseRandomNum(999);
    }
    preHold = randomNum + '';

    // Choose the number after the decimal point
    chooseRandomNum(1);
    if (randomNum) {
      chooseRandomNum(9);
    }
    else {
      chooseRandomNum(99);
    }
    if ((randomNum == 0) && (preHold != '0')) {
    // The number after the decimal place is a '0'
    //   Give it a 50/50 shot between '0' and '' (nothing)
    //   Don't do this if the number before the decimal place is already a '0'
    //   This is important for significant figure purposes

      chooseRandomNum(1);
      if (randomNum) {
      // Make it a ''
        postHold = '';
      }
      else {
      // Keep it a '0'
        postHold = '0';
      }
    }
    else {
      postHold = randomNum + '';
    }

    if (!((preHold == '0') && (postHold == '0'))) {
    // Both are '0' and we can't have that so keep looping
      notDone = 0;
    }
  }
}

function chooseMole() {
  var notDone;

  notDone = 1;
  while (notDone) {
    chooseRandomNum(1);
    preHold = randomNum + '';
    chooseRandomNum(4);
    if (!randomNum) {
      chooseRandomNum(9);
    }
    else if (randomNum < 3) {
      chooseRandomNum(99);
    }
    else {
      chooseRandomNum(999);
    }
    postHold = randomNum + '';

    if (!((preHold == '0') && (postHold == '0'))) {
      notDone = 0;
    }
  }
}

function calcIdeal() {
// Calculates the result for the Ideal Gas equation depending
//   on what it's solved for

  if (whichIdealVar == 0) {
  // Solved for pressure
    answer = ((prnNum[3]-0)*(gasConst[whichPUnit])*(prnNum[2]-0))/(prnNum[1]-0);
    answerUnit = pUnit[whichPUnit];
    modIdeal();
  }
  else if (whichIdealVar == 1) {
  // Solved for volume
    answer = ((prnNum[3]-0)*(gasConst[whichPUnit])*(prnNum[2]-0))/(prnNum[0]-0);
    answerUnit = vUnit[whichVUnit];
    modIdeal();
  }
  else if (whichIdealVar == 2) {
  // Solved for temperature
    answer = ((prnNum[0]-0)*(prnNum[1]-0))/((prnNum[3]-0)*gasConst[whichPUnit]);
    answerUnit = tUnit[whichTUnit];
    modIdeal();
  }
  else {
  // Solved for moles
    answer = ((prnNum[0]-0)*(prnNum[1]-0))/((prnNum[2]-0)*gasConst[whichPUnit]);
    answerUnit = 'mol';
    modIdeal();
  }

  answer = answer + '';
}

function calcCombined() {
  // Calculates the result for the Combined Gas Law or variant
  //   depending on what it's solved for

  // Determine if calculating initial or final value
  chooseRandomNum(1);
  calcCombFinal = randomNum;
//  calcCombFinal = 1;  // Testing

  if (quizType == 0) {
  // Boyle's- solved for pressure
    if (calcCombFinal) {
      prnNum[1] = '';
      answer = (prnNum[0]-0)*(prnNum[2]-0)/(prnNum[3]-0);
      modCombined(1,0,2,3);
    }
    else {
      prnNum[0] = '';
      answer = (prnNum[1]-0)*(prnNum[3]-0)/(prnNum[2]-0);
      modCombined(0,1,3,2);
    }
    answerUnit = pUnit[whichPUnit];
  }
  else if (quizType == 1) {
  // Boyle's- solved for volume
    if (calcCombFinal) {
      prnNum[3] = '';
      answer = (prnNum[0]-0)*(prnNum[2]-0)/(prnNum[1]-0);
      modCombined(3,0,2,1);
    }
    else {
      prnNum[2] = '';
      answer = (prnNum[1]-0)*(prnNum[3]-0)/(prnNum[0]-0);
      modCombined(2,1,3,0);
    }
    answerUnit = vUnit[whichVUnit];
  }
  else if (quizType == 2) {
  // Charles'- solved for volume
    if (calcCombFinal) {
      prnNum[3] = '';
      answer = (prnNum[2]-0)*(prnNum[5]-0)/(prnNum[4]-0);
      modCombined(3,2,5,4);
    }
    else {
      prnNum[2] = '';
      answer = (prnNum[3]-0)*(prnNum[4]-0)/(prnNum[5]-0);
      modCombined(2,3,4,5);
    }
    answerUnit = vUnit[whichVUnit];
  }
  else if (quizType == 3) {
  // Charles'- solved for temperature
    if (calcCombFinal) {
      prnNum[5] = '';
      answer = (prnNum[3]-0)*(prnNum[4]-0)/(prnNum[2]-0);
      modCombined(5,3,4,2);
    }
    else {
      prnNum[4] = '';
      answer = (prnNum[2]-0)*(prnNum[5]-0)/(prnNum[3]-0);
      modCombined(4,2,5,3);
    }
    answerUnit = tUnit[whichTUnit];
  }
  else if (quizType == 4) {
  // Gay-Lussac's- solved for pressure
    if (calcCombFinal) {
      prnNum[1] = '';
      answer = (prnNum[0]-0)*(prnNum[5]-0)/(prnNum[4]-0);
      modCombined(1,0,5,4);
    }
    else {
      prnNum[0] = '';
      answer = (prnNum[1]-0)*(prnNum[4]-0)/(prnNum[5]-0);
      modCombined(0,1,4,5);
    }
    answerUnit = pUnit[whichPUnit];
  }
  else if (quizType == 5) {
  // Gay-Lussac's- solved for temperature
    if (calcCombFinal) {
      prnNum[5] = '';
      answer = (prnNum[1]-0)*(prnNum[4]-0)/(prnNum[0]-0);
      modCombined(5,1,4,0);
    }
    else {
      prnNum[4] = '';
      answer = (prnNum[0]-0)*(prnNum[5]-0)/(prnNum[1]-0);
      modCombined(4,0,5,1);
    }
    answerUnit = tUnit[whichTUnit];
  }
  else if (quizType == 6) {
  // Combined- solved for pressure
    if (calcCombFinal) {
      prnNum[1] = '';
      answer = (prnNum[0]-0)*(prnNum[2]-0)*(prnNum[5]-0)/((prnNum[4]-0)*(prnNum[3]-0));
      modCombined(1,0,2,5,4,3);
    }
    else {
      prnNum[0] = '';
      answer = (prnNum[1]-0)*(prnNum[3]-0)*(prnNum[4]-0)/((prnNum[5]-0)*(prnNum[2]-0));
      modCombined(0,1,3,4,5,2);
    }
    answerUnit = pUnit[whichPUnit];
  }
  else if (quizType == 7) {
  // Combined- solved for volume
    if (calcCombFinal) {
      prnNum[3] = '';
      answer = (prnNum[0]-0)*(prnNum[2]-0)*(prnNum[5]-0)/((prnNum[4]-0)*(prnNum[1]-0));
      modCombined(3,0,2,5,4,1);
    }
    else {
      prnNum[2] = '';
      answer = (prnNum[1]-0)*(prnNum[3]-0)*(prnNum[4]-0)/((prnNum[5]-0)*(prnNum[0]-0));
      modCombined(2,1,3,4,5,0);
    }
    answerUnit = vUnit[whichVUnit];
  }
  else {
  // Combined- solved for temperature
    if (calcCombFinal) {
      prnNum[5] = '';
      answer = (prnNum[1]-0)*(prnNum[3]-0)*(prnNum[4]-0)/((prnNum[0]-0)*(prnNum[2]-0));
      modCombined(5,1,3,4,0,2);
    }
    else {
      prnNum[4] = '';
      answer = (prnNum[0]-0)*(prnNum[2]-0)*(prnNum[5]-0)/((prnNum[1]-0)*(prnNum[3]-0));
      modCombined(4,0,2,5,1,3);
    }
    answerUnit = tUnit[whichTUnit];
  }

  answer = answer + '';
//  alert(answer);  // Testing
}

function modIdeal() {
  var vSwap = new Array();
  var numSwap = new Array();
  var uSwap = new Array();
  var tempPrnNum = new Array();
  var tempPrnUnit = new Array();

  tempPrnNum = [];
  tempPrnUnit = [];

  for (var i = 0;i < 4;i++) {
  // Need to do text line-through in order to show cancellation
    if (i != whichIdealVar) {
      tempPrnUnit[i] = '<SPAN STYLE=\"text-decoration: line-through\">' +
                       prnUnit[i] + '<\/SPAN>)';
      tempPrnNum[i] = '(' + prnNum[i]
    }
  }

  if (whichIdealVar == 0) {
  // pressure
    prnEqn[1] = gasEqn[7];
    vSwap[0] = 'P';
    vSwap[1] = 'n';
    vSwap[2] = 'T';
    vSwap[3] = 'V';
    numSwap[1] = tempPrnNum[3];
    numSwap[2] = tempPrnNum[2];
    numSwap[3] = tempPrnNum[1]
    uSwap[1] = tempPrnUnit[3];
    uSwap[2] = tempPrnUnit[2];
    uSwap[3] = tempPrnUnit[1];
  }
  else if (whichIdealVar == 1) {
  // volume
    prnEqn[1] = gasEqn[7];
    vSwap[0] = 'V';
    vSwap[1] = 'n';
    vSwap[2] = 'T';
    vSwap[3] = 'P';
    numSwap[1] = tempPrnNum[3];
    numSwap[2] = tempPrnNum[2];
    numSwap[3] = tempPrnNum[0]
    uSwap[1] = tempPrnUnit[3];
    uSwap[2] = tempPrnUnit[2];
    uSwap[3] = tempPrnUnit[0];
  }
  else if (whichIdealVar == 2) {
  // temperature
    prnEqn[1] = gasEqn[8];
    vSwap[0] = 'T';
    vSwap[1] = 'P';
    vSwap[2] = 'V';
    vSwap[3] = 'n';
    numSwap[1] = tempPrnNum[0];
    numSwap[2] = tempPrnNum[1];
    numSwap[3] = tempPrnNum[3]
    uSwap[1] = tempPrnUnit[0];
    uSwap[2] = tempPrnUnit[1];
    uSwap[3] = tempPrnUnit[3];
  }
  else {
  // moles
    prnEqn[1] = gasEqn[8];
    vSwap[0] = 'n';
    vSwap[1] = 'P';
    vSwap[2] = 'V';
    vSwap[3] = 'T';
    numSwap[1] = tempPrnNum[0];
    numSwap[2] = tempPrnNum[1];
    numSwap[3] = tempPrnNum[2]
    uSwap[1] = tempPrnUnit[0];
    uSwap[2] = tempPrnUnit[1];
    uSwap[3] = tempPrnUnit[2];
  }

  // prnEqn[1] = equation symbolically solved
  // prnEqn[2] = equation with numbers and units

  // Swap in R
  prnEqn[1] = prnEqn[1].replace(/RGASCONSTANT/, 'R');

  // Swap in variables for variables, V1-4
  prnEqn[1] = prnEqn[1].replace(/V1/, vSwap[0]);
  prnEqn[1] = prnEqn[1].replace(/V2/, vSwap[1]);
  prnEqn[1] = prnEqn[1].replace(/V3/, vSwap[2]);
  prnEqn[1] = prnEqn[1].replace(/V4/, vSwap[3]);

  // Swap in null for units
  prnEqn[1] = prnEqn[1].replace(/U2/, '');
  prnEqn[1] = prnEqn[1].replace(/U3/, '');
  prnEqn[1] = prnEqn[1].replace(/U4/, '');

  // Swap in actual numbers for variables, V1-4
  prnEqn[2] = prnEqn[2].replace(/V1/, vSwap[0]);
  prnEqn[2] = prnEqn[2].replace(/V2/, numSwap[1]);
  prnEqn[2] = prnEqn[2].replace(/V3/, numSwap[2]);
  prnEqn[2] = prnEqn[2].replace(/V4/, numSwap[3]);

  // Swap in actual units for variables, U2-4
  prnEqn[2] = prnEqn[2].replace(/U2/, uSwap[1]);
  prnEqn[2] = prnEqn[2].replace(/U3/, uSwap[2]);
  prnEqn[2] = prnEqn[2].replace(/U4/, uSwap[3]);
}

function modCombined(a,b,c,d,e,f) {
  // Indices for the numbers and units in the proper order
  //   a = index for solved, symbolic variable
  //   b = index for variable 2 and unit 2
  //   etc.

  var a;
  var b;
  var c;
  var d;
  var e;
  var f;
  var tempPrnNum = new Array();
  var tempPrnUnit = new Array();

  tempPrnNum = [];
  tempPrnUnit = [];

  // Need to do text line-through in order to show cancellation

  for (var i = 0;i < 6;i++) {
    if (prnUnit[i] != prnUnit[a]) {
      tempPrnUnit[i] = '<SPAN STYLE=\"text-decoration: line-through\">' +
                       prnUnit[i] + '<\/SPAN>';
    }
    else {
      tempPrnUnit[i] = prnUnit[i];
    }
  }

  // Need parenthesis put in to differentiate between values

  tempPrnNum[b] = '(' + prnNum[b];
  tempPrnNum[c] = '(' + prnNum[c];
  tempPrnNum[d] = '(' + prnNum[d];
  tempPrnNum[e] = '(' + prnNum[e];
  tempPrnNum[f] = '(' + prnNum[f];
  tempPrnUnit[b] = tempPrnUnit[b] + ')';
  tempPrnUnit[c] = tempPrnUnit[c] + ')';
  tempPrnUnit[d] = tempPrnUnit[d] + ')';
  tempPrnUnit[e] = tempPrnUnit[e] + ')';
  tempPrnUnit[f] = tempPrnUnit[f] + ')';

  // prnEqn[1] = equation symbolically solved
  // prnEqn[2] = equation with numbers and units

  // Swap in variables for variables, V1-6
  prnEqn[1] = prnEqn[1].replace(/V1/, prnSymb[a]);
  prnEqn[1] = prnEqn[1].replace(/V2/, prnSymb[b]);
  prnEqn[1] = prnEqn[1].replace(/V3/, prnSymb[c]);
  prnEqn[1] = prnEqn[1].replace(/V4/, prnSymb[d]);
  prnEqn[1] = prnEqn[1].replace(/V5/, prnSymb[e]);
  prnEqn[1] = prnEqn[1].replace(/V6/, prnSymb[f]);

  // Swap in null for units
  prnEqn[1] = prnEqn[1].replace(/U2/, '');
  prnEqn[1] = prnEqn[1].replace(/U3/, '');
  prnEqn[1] = prnEqn[1].replace(/U4/, '');
  prnEqn[1] = prnEqn[1].replace(/U5/, '');
  prnEqn[1] = prnEqn[1].replace(/U6/, '');

  // Swap in actual numbers for variables, V1-6
  prnEqn[2] = prnEqn[2].replace(/V1/, prnSymb[a]);
  prnEqn[2] = prnEqn[2].replace(/V2/, tempPrnNum[b]);
  prnEqn[2] = prnEqn[2].replace(/V3/, tempPrnNum[c]);
  prnEqn[2] = prnEqn[2].replace(/V4/, tempPrnNum[d]);
  prnEqn[2] = prnEqn[2].replace(/V5/, tempPrnNum[e]);
  prnEqn[2] = prnEqn[2].replace(/V6/, tempPrnNum[f]);

  // Swap in actual units for variables, U2-6
  prnEqn[2] = prnEqn[2].replace(/U2/, tempPrnUnit[b]);
  prnEqn[2] = prnEqn[2].replace(/U3/, tempPrnUnit[c]);
  prnEqn[2] = prnEqn[2].replace(/U4/, tempPrnUnit[d]);
  prnEqn[2] = prnEqn[2].replace(/U5/, tempPrnUnit[e]);
  prnEqn[2] = prnEqn[2].replace(/U6/, tempPrnUnit[f]);
}

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 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 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 printGivenIdeal() {
  quizWindow.document.write('<TR><TD ALIGN=\"right\">\n' +
      'Pressure<\/TD>\n<TD>=<\/TD>\n<TD ALIGN=\"');
  if (prnNum[0] != '') {
    quizWindow.document.write('right\">' + prnNum[0] + '&nbsp;' + prnUnit[0]);
  }
  else {
    quizWindow.document.write('center\"><B>?<\/B>');
  }

  quizWindow.document.write('<\/TD>\n<TD ALIGN=\"right\">\n' +
    'Volume<\/TD>\n<TD>=<\/TD>\n<TD ALIGN=\"');
  if (prnNum[1] != '') {
    quizWindow.document.write('right\">' + prnNum[1] + '&nbsp;' + prnUnit[1]);
  }
  else {
    quizWindow.document.write('center\"><B>?<\/B>');
  }
  quizWindow.document.write('\n<\/TD>\n<\/TR>\n');

  quizWindow.document.write('<TR><TD ALIGN=\"right\">\n' +
      'Temperature<\/TD>\n<TD>=<\/TD>\n<TD ALIGN=\"');
  if (prnNum[2] != '') {
    quizWindow.document.write('right\">' + prnNum[2] + '&nbsp;' + prnUnit[2]);
  }
  else {
    quizWindow.document.write('center\"><B>?<\/B>');
  }

  quizWindow.document.write('<\/TD>\n<TD ALIGN=\"right\">\n' +
    '# of Moles<\/TD>\n<TD>=<\/TD>\n<TD ALIGN=\"');
  if (prnNum[3] != '') {
    quizWindow.document.write('right\">' + prnNum[3] + '&nbsp;' + prnUnit[3]);
  }
  else {
    quizWindow.document.write('center\"><B>?<\/B>');
  }
  quizWindow.document.write('\n<\/TD>\n<\/TR>\n');
}

function printGivenCombined() {
  if (prnNum[0] || prnNum[1]) {
  // This gas law uses pressure since both pressure values are not null

    quizWindow.document.write('<TR><TD ALIGN=\"right\">\n' +
      'Initial Pressure<\/TD>\n<TD>=<\/TD>\n<TD ALIGN=\"');
    if (prnNum[0] != '') {
      quizWindow.document.write('right\">' + prnNum[0] + '&nbsp;' + prnUnit[0]);
    }
    else {
      quizWindow.document.write('center\"><B>?<\/B>');
    }
    quizWindow.document.write('<\/TD>\n<TD ALIGN=\"right\">\n' +
      'Final Pressure<\/TD>\n<TD>=<\/TD>\n<TD ALIGN=\"');
    if (prnNum[1] != '') {
      quizWindow.document.write('right\">' + prnNum[1] + '&nbsp;' + prnUnit[1]);
    }
    else {
      quizWindow.document.write('center\"><B>?<\/B>');
    }
    quizWindow.document.write('\n<\/TD>\n<\/TR>\n');
  }

  if (prnNum[2] || prnNum[3]) {
  // This gas law uses volume since both volume values are not null

    quizWindow.document.write('<TR><TD ALIGN=\"right\">\n' +
      'Initial Volume<\/TD>\n<TD>=<\/TD>\n<TD ALIGN=\"');
    if (prnNum[2] != '') {
      quizWindow.document.write('right\">' + prnNum[2] + '&nbsp;' + prnUnit[2]);
    }
    else {
      quizWindow.document.write('center\"><B>?<\/B>');
    }
    quizWindow.document.write('<\/TD>\n<TD ALIGN=\"right\">\n' +
      'Final Volume<\/TD>\n<TD>=<\/TD>\n<TD ALIGN=\"');
    if (prnNum[3] != '') {
      quizWindow.document.write('right\">' + prnNum[3] + '&nbsp;' + prnUnit[3]);
    }
    else {
      quizWindow.document.write('center\"><B>?<\/B>');
    }
    quizWindow.document.write('\n<\/TD>\n<\/TR>\n');
  }

  if (prnNum[4] || prnNum[5]) {
  // This gas law uses temperature since both temperature values are not null

    quizWindow.document.write('<TR><TD ALIGN=\"right\">\n' +
      'Initial Temperature<\/TD>\n<TD>=<\/TD>\n<TD ALIGN=\"');
    if (prnNum[4] != '') {
      quizWindow.document.write('right\">' + prnNum[4] + '&nbsp;' + prnUnit[4]);
    }
    else {
      quizWindow.document.write('center\"><B>?<\/B>');
    }
    quizWindow.document.write('<\/TD>\n<TD ALIGN=\"right\">\n' +
      'Final Temperature<\/TD>\n<TD>=<\/TD>\n<TD ALIGN=\"');
    if (prnNum[5] != '') {
      quizWindow.document.write('right\">' + prnNum[5] + '&nbsp;' +prnUnit[5]);
    }
    else {
      quizWindow.document.write('center\"><B>?<\/B>');
    }
    quizWindow.document.write('\n<\/TD>\n<\/TR>\n');
  }
}

function printAddress() {
  quizWindow.document.write('<HR>\n<DIV>\n' +
    'Script last modified: ' + dateMod + 
    '<BR><A HREF=\"mailto:' + emailAdd + '\">' + emailAdd +
    '<\/A>\n<\/DIV>\n');
}

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

function printIdealSolution() {
  var prnEqnSplit = new Array();

  prnEqnSplit = prnEqn[2].split('RGASCONSTANT');

  quizWindow.document.write('<DIV ALIGN=\"center\">\n' +
    '<TABLE BORDER=\"0\" CELLSPACING=\"10\">\n<TR>\n' +
    '<TD ALIGN=\"center\">\n' + prnEqn[0] + '\n<\/TD>\n' +
    '<TD ALIGN=\"center\">\n' + prnEqn[1] + '\n<\/TD>\n' +
    '<TD ALIGN=\"center\">\n' + prnEqnSplit[0]);

  printGasConstant(0);

  quizWindow.document.write(prnEqnSplit[1] + '\n<\/TD>\n<\/TR>\n<TR>\n' +
    '<TD ALIGN=\"center\"><B>Ideal Gas Law</B><\/TD>\n' +
    '<TD ALIGN=\"center\">solved for <B>' + prnIdealQuest + '<\/B><\/TD>\n' +
    '<TD ALIGN=\"center\">cancellation of units<\/TD>\n' +
    '<\/TR>\n<\/TABLE>\n<\/DIV>\n');
}

function printCombinedSolution() {
  quizWindow.document.write('<DIV ALIGN=\"center\">\n' +
    '<TABLE BORDER=\"0\" CELLSPACING=\"10\">\n<TR>\n' +
    '<TD ALIGN=\"center\">\n' + prnEqn[0] + '\n<\/TD>\n' +
    '<TD ALIGN=\"center\">\n' + prnEqn[1] + '\n<\/TD>\n' +
    '<TD ALIGN=\"center\">\n' + prnEqn[2] + '\n<\/TD>\n' +
    '<\/TR>\n<TR>\n' +
    '<TD ALIGN=\"center\"><B>' + whichDispEqn + ' Law<\/B><\/TD>\n' +
    '<TD ALIGN=\"center\">solved for <B>' + prnChoice[calcCombFinal] + ' ' + prnCombQuest[quizType] + '<\/B><\/TD>\n' +
    '<TD ALIGN=\"center\">cancellation of units<\/TD>\n' +
    '<\/TR>\n<\/TABLE>\n<\/DIV>\n');
}

function printGasConstant() {
  // whichSaveUnit is the unit that is not cancelled
  //
  //   = 0 pressure
  //   = 1 volume
  //   = 2 temperature
  //   = 3 moles

  quizWindow.document.write('<TABLE BORDER=\"0\"><TR>' +
    '<TD ALIGN=\"center\" ROWSPAN=\"3\" STYLE=\"font-size: xx-large\">' +
    '(<\/TD><TD ALIGN=\"center\" ROWSPAN=\"3\">' + gasConst[whichPUnit] +    
    '<\/TD><TD ALIGN=\"center\" STYLE=\"font-size: small\">');
    
  if (whichIdealVar != 1) {
    quizWindow.document.write('<SPAN CLASS=\"uCan\">L<\/SPAN>&nbsp;&bull;&nbsp;');
  }
  else {
    quizWindow.document.write('L&nbsp;&bull;&nbsp;');
  }

  if (whichIdealVar != 0) {
    quizWindow.document.write('<SPAN CLASS=\"uCan\">' + pUnit[whichPUnit] + '<\/SPAN>');
  }
  else {
    quizWindow.document.write(pUnit[whichPUnit]);
  }

  quizWindow.document.write('<\/TD>' +
    '<TD ALIGN=\"center\" ROWSPAN=\"3\" STYLE=\"font-size: xx-large\">)' +
    '<\/TD><\/TR><TR><TD ALIGN=\"center\" STYLE=\"font-size: small\">' +
    '<HR><\/TD><\/TR><TR><TD ALIGN=\"center\" STYLE=\"font-size: small\">');

  if (whichIdealVar != 3) {
    quizWindow.document.write('<SPAN CLASS=\"uCan\">mol<\/SPAN>&nbsp;&bull;&nbsp;');
  }
  else {
    quizWindow.document.write('mol&nbsp;&bull;&nbsp;');
  }

  if (whichIdealVar != 2) {
    quizWindow.document.write('<SPAN CLASS=\"uCan\">K<\/SPAN>');
  }
  else {
    quizWindow.document.write('K');
  }

  quizWindow.document.write('<\/TD><\/TR><\/TABLE>');
}

function printStyleSheet() {
  quizWindow.document.write('<STYLE TYPE=\"text\/css\">\n<!--\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' +
    '.uCan {text-decoration: line-through}\n\n-->\n<\/STYLE>\n');
}
