var densityEqn = new Array();
var convEqn = new Array();
var prnEqn = new Array();
var prnNum = new Array();
var prnUnit = new Array();
var prnChoice = new Array();
var theBase = new Array();
var thePrefix = new Array();
var valPrefix = new Array();
var disPrefix = new Array();
var prnSciNum = new Array();
var whichPrefix = new Array();
var whichBase;
var showAnswer;
var gradeAnswer;
var quizWindowOpened;
var quizWidth;
var quizHeight;
var quizType;
var randomNum;
var preHold;
var postHold;
var answer;
var answerPre;
var answerExp;
var userDecPoint;
var answerSciNot;
var answerExp;
var answerUnit;
var prnAnswer;
var prnAnswerSciNot;
var numSigFigs;
var userCorrect;
var prnGradeResult;
var isAmbiguous;
var doSciNotConvert;
var prevDoSciNotConvert;
var revealedAnswer;
var emailAdd = 'chemistry@chemistry.alanearhart.org';
var dateMod = '7\/29\/06';

theBase[0] = 'g';
theBase[1] = 'L';
theBase[2] = 'm';
theBase[3] = 'J';
theBase[4] = 's';

thePrefix[0] = 'M';
valPrefix[0] = '1000000';
disPrefix[0] = '1&nbsp;x&nbsp;10<SUP>6<\/SUP>';
thePrefix[1] = 'k';
valPrefix[1] = '1000';
disPrefix[1] = '1&nbsp;x&nbsp;10<SUP>3<\/SUP>';
thePrefix[2] = 'd';
valPrefix[2] = '0.1';
disPrefix[2] = '1&nbsp;x&nbsp;10<SUP>-1<\/SUP>';
thePrefix[3] = 'c';
valPrefix[3] = '0.01';
disPrefix[3] = '1&nbsp;x&nbsp;10<SUP>-2<\/SUP>';
thePrefix[4] = 'm';
valPrefix[4] = '0.001';
disPrefix[4] = '1&nbsp;x&nbsp;10<SUP>-3<\/SUP>';
thePrefix[5] = '&micro;';
valPrefix[5] = '0.000001';
disPrefix[5] = '1&nbsp;x&nbsp;10<SUP>-6<\/SUP>';

// Basic conversion equation for converting a base unit into a prefixed unit
//
//   V1U1 * 1 U2   V3U2  (standard notation)
//   ____   ____ =  or
//    1     V2U1   V4U2  (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">U1<\/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;U2<\/TD>' +
             '<\/TR><TR><TD ALIGN=\"center\"><HR><\/TD><\/TR><TR>' +
             '<TD ALIGN=\"center\">V2&nbsp;<SPAN CLASS=\"uCan\">U1<\/SPAN>' +
             '<\/TD><\/TR><\/TABLE><\/TD><TD ALIGN=\"center\">=<\/TD>' +
             '<TD ALIGN=\"center\"><TABLE BORDER=\"0\"><TR>' +
             '<TD ALIGN=\"center\">V3&nbsp;U2<\/TD><\/TR><TR>' +
             '<TD ALIGN=\"center\">or<\/TD><\/TR><TR>' +
             '<TD ALIGN=\"center\">V4&nbsp;U2<\/TD><\/TR><\/TABLE>' +
             '<\/TD><\/TR><\/TABLE>';

// Basic conversion equation for converting a prefixed unit into a base unit
//
//   V1U1 * V2U2   V3U2  (standard notation)
//   ____   ____ =  or
//    1     1 U1   V4U2  (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\">U1<\/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;U2<\/TD>' +
             '<\/TR><TR><TD ALIGN=\"center\"><HR><\/TD><\/TR><TR>' +
             '<TD ALIGN=\"center\">1&nbsp;<SPAN CLASS=\"uCan\">U1<\/SPAN>' +
             '<\/TD><\/TR><\/TABLE><\/TD><TD ALIGN=\"center\">=<\/TD>' +
             '<TD ALIGN=\"center\"><TABLE BORDER=\"0\"><TR>' +
             '<TD ALIGN=\"center\">V3&nbsp;U2<\/TD><\/TR><TR>' +
             '<TD ALIGN=\"center\">or<\/TD><\/TR><TR>' +
             '<TD ALIGN=\"center\">V4&nbsp;U2<\/TD><\/TR><\/TABLE>' +
             '<\/TD><\/TR><\/TABLE>';

// Complex conversion equation for converting a prefixed unit into a different prefixed unit
//
//   V1U1 * V2U2 * 1 U3
//   ____   ____   ____ = V4U3
//    1     1 U1   V3U2

convEqn[2] = '<TABLE BORDER=\"0\"><TR><TD CLASS=\"firstCell\" ALIGN=\"center\">' +
             '<TABLE BORDER=\"0\"><TR><TD ALIGN=\"center\">V1&nbsp;' +
             '<SPAN CLASS=\"uCan\">U1<\/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;' +
             '<SPAN CLASS=\"uCan\">U2<\/SPAN><\/TD><\/TR><TR><TD ALIGN=\"center\">' +
             '<HR><\/TD><\/TR><TR><TD ALIGN=\"center\">1&nbsp;' +
             '<SPAN CLASS=\"uCan\">U1<\/SPAN><\/TD><\/TR><\/TABLE><\/TD>' +
             '<TD CLASS=\"otherCell\" ALIGN=\"center\"><TABLE BORDER=\"0\">' +
             '<TR><TD ALIGN=\"center\">1&nbsp;U3<\/TD><\/TR><TR>' +
             '<TD ALIGN=\"center\"><HR><\/TD><\/TR><TR><TD ALIGN=\"center\">' +
             'V3&nbsp;<SPAN CLASS=\"uCan\">U2<\/SPAN><\/TD><\/TR><\/TABLE>' +
             '<\/TD><TD ALIGN=\"center\">=<\/TD><TD ALIGN=\"center\">' +
             '<TABLE BORDER=\"0\"><TR><TD ALIGN=\"center\">V4&nbsp;U3<\/TD>' +
             '<\/TR><TR><TD ALIGN=\"center\">or<\/TD><\/TR><TR>' +
             '<TD ALIGN=\"center\">V5&nbsp;U3<\/TD><\/TR><\/TABLE>' +
             '<\/TD><\/TR><\/TABLE>';

function popUpDimQuiz(whichQuiz) {
  var whichQuiz;
  var tempHold;
  var tempQuiz;
  var alertMessage; // Testing

  alertMessage = ''; // Testing

//  alert('got here');
  if (whichQuiz == 'Reveal!') {
    showAnswer = 1;
    gradeAnswer = 0;
  }
  else if (whichQuiz == 'Grade Me!') {
    showAnswer = 0;
    gradeAnswer = 1;
  }
  else {
    revealedAnswer = 0;
    initValues();
    showAnswer = 0;
    gradeAnswer = 0;
    if (whichQuiz.substr(0,4) == 'Quiz') {
      tempHold = whichQuiz.length;
      tempQuiz = whichQuiz.substr(tempHold - 2,2) - 0;
      quizType = tempQuiz - 1;
      if (quizType == 0) {
        chooseSigFig(4,5);
      }
      else if (quizType == 1) {
      // Decide if scientific notation-standard conversion or vice-versa
        chooseRandomNum(1);
//        randomNum = 0; // Testing
        doSciNotConvert = randomNum;
        if (doSciNotConvert) {
        // Scientific notation to standard notation
          chooseSciNot();
        }
        else {
        // Standard notation to scientific notation
          chooseSigFig(4,5);
        }
      }
      else if (quizType == 2) {
        alertMessage = 'Not working.';
      }
      else if (quizType == 3) {
        doBasicConversion();
      }
      else if (quizType == 4) {
        doComplexConversion();
      }
      else if (quizType == 5) {
        alertMessage = 'Not working.';
      }
    }
    else {
      // Clear the old field value(s)
//      clearForm();
      if (quizType == 0) {
        chooseSigFig(4,5);
      }
      else if (quizType == 1) {
        chooseRandomNum(1);
//        randomNum = 0; // Testing
        doSciNotConvert = randomNum;
        if (doSciNotConvert) {
        // Scientific notation to standard notation
          chooseSciNot();
        }
        else {
        // Standard notation to scientific notation
          chooseSigFig(4,5);
        }
      }
      else if (quizType == 3) {
        doBasicConversion();
      }
      else if (quizType == 4) {
        doComplexConversion();
      }
    }
  }

  if (!alertMessage) {
    printDimQuiz();
  }
  else {
    alert(alertMessage);
  }
 
}

function clearForm() {
// Called when the generate button is called within the quiz window
//   in order to clear the field values before generating the new quiz

  if (quizType == 0) {
    quizWindow.document.dimQuiz.userNumber.value = '';
  }
  else if (quizType == 1) {
  // Keep the previous quiz (standard to scientific notation or vice-versa)
    prevDoSciNotConvert = doSciNotConvert;
    if (prevDoSciNotConvert) {
      quizWindow.document.dimQuiz.userNumber.value = '';
    }
    else {
      quizWindow.document.dimQuiz.answerPre.value = '';
      quizWindow.document.dimQuiz.answerExp.value = '';
    }
  }
  else if ((quizType == 3) || (quizType == 4)) {
    quizWindow.document.dimQuiz.userNumber.value = '';
    quizWindow.document.dimQuiz.answerPre.value = '';
    quizWindow.document.dimQuiz.answerExp.value = '';
  }
}

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

function chooseRandomNum(rangeNum) {
  var rangeNum;

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

function convert2() {
// Converts from scientific notation to standard notation
  var tempExp;
  var tempLen;

  isAmbiguous = 0;
  tempExp = answerExp - 0;
  if (tempExp > 0) {
  // Positive exponent
    if (answerSciNot.substr(2).length == tempExp) {
      answer = answerSciNot.substr(0,1) + answerSciNot.substr(2);
      if (answer.substr(answer.length - 1,1) == '0') {
        answer = answer + '.';
      }
    }
    else if (answerSciNot.substr(2).length < tempExp) {
      isAmbiguous = 1;
      answer = answerSciNot.substr(0,1) + answerSciNot.substr(2);
      for (var i=0;i<(tempExp-answerSciNot.substr(2).length);i++) {
        answer += '0';
      }
    }
    else {
      answer = answerSciNot.substr(0,1) +
               answerSciNot.substr(2,tempExp) +
               '.' + answerSciNot.substr(2+tempExp);
    }
  }
  else if (!tempExp) {
  // Zero exponent
    answer = answerSciNot;
  }
  else {
  // Negative exponent
    tempExp = Math.round((-1) * tempExp)
    answer = '0.';
    for (var i = 1; i < tempExp; i++) {
      answer += '0';
    }
    answer += answerSciNot.substr(0,1) + answerSciNot.substr(2);
  }
//  alert(answerSciNot + '\n' + answerExp + '\n' + answer); // Testing
}

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 doComplexConversion() {
// Choose the given value and desired prefix conversion for basic,
//   one-step conversion problems.
  var notDone;

  prnNum = [];
  prnUnit = [];
  whichPrefix = [];

  // Choose the given number
  chooseSigFig(3,3);
  prnNum[0] = answer;

  // Choose the given number's base unit
  chooseRandomNum(theBase.length - 1);
  prnUnit[0] = theBase[randomNum];
  prnUnit[1] = theBase[randomNum];
  prnUnit[2] = theBase[randomNum];

  // Choose the given number's prefix
  chooseRandomNum(thePrefix.length - 1);
  whichPrefix[0] = randomNum;
  prnUnit[0] = thePrefix[whichPrefix[0]] + prnUnit[0];

  prnNum[1] = valPrefix[whichPrefix[0]];
  prnSciNum[1] = disPrefix[whichPrefix[0]];

  // Choose the final number's prefix
  notDone = 1;
  while (notDone) {
    chooseRandomNum(theBase.length - 1);
    if (randomNum != whichPrefix[0]) {
      whichPrefix[2] = randomNum;
      notDone = 0;
    }
  }
  prnUnit[2] = thePrefix[whichPrefix[2]] + prnUnit[2];
  prnNum[2] = valPrefix[whichPrefix[2]];
  prnSciNum[2] = disPrefix[whichPrefix[2]];

  // Calculate the conversion value
  answer = ((prnNum[0]-0)*(prnNum[1]-0)/(prnNum[2]-0)) + '';
}

function doBasicConversion() {
// Choose the given value and desired prefix conversion for basic,
//   one-step conversion problems.

  prnNum = [];
  prnUnit = [];
  whichPrefix = [];

  // Choose the given number
  chooseSigFig(3,3);
  prnNum[0] = answer;

  // Choose the given number's base unit
  chooseRandomNum(theBase.length - 1);
  prnUnit[0] = theBase[randomNum];
  prnUnit[1] = theBase[randomNum];

  // Decide if the given value is a prefixed unit
  chooseRandomNum(1);
//  randomNum = 0; // Testing
  if (!randomNum) {
  // Base unit to prefixed unit conversion
    whichPrefix[0] = -1;
  }

  // Choose the conversion prefix
  chooseRandomNum(thePrefix.length - 1);
  if (whichPrefix[0] < 0) {
  // The prefixed unit is the second unit
//    randomNum = 2; // Testing
    whichPrefix[1] = randomNum;
    prnUnit[1] = thePrefix[whichPrefix[1]] + prnUnit[1];
    prnNum[1] = valPrefix[whichPrefix[1]];
    prnSciNum[1] = disPrefix[whichPrefix[1]];
  }
  else {
  // The prefixed unit is the first (given) unit
    whichPrefix[1] = -1;
    whichPrefix[0] = randomNum;
    prnUnit[0] = thePrefix[whichPrefix[0]] + prnUnit[0];
    prnSciNum[0] = disPrefix[whichPrefix[0]];
    prnNum[1] = valPrefix[whichPrefix[0]];
    prnSciNum[1] = disPrefix[whichPrefix[0]];
  }

  // Calculate the conversion value
  if (whichPrefix[0] < 0) {
  // Base unit to prefixed unit conversion
    answer = ((prnNum[0]-0)/(prnNum[1]-0)) + '';
  }
  else {
  // Prefixed unit to base unit conversion
    answer = ((prnNum[0]-0)*(prnNum[1]-0)) + '';
  }
}

function chooseSciNot() {
  var iRange;
  var tempHold;

// Determine the digit before the decimal point
  chooseRandomNum(8);
  preHold = (randomNum + 1) + '';

// Determine the number of digits after the decimal point
  chooseRandomNum(5);
  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;
  }

  answerSciNot = preHold + '.' + postHold;

// Determine the exponent
  answerExp = '';
  chooseRandomNum(5);
  answerExp = randomNum + '';
  chooseRandomNum(1);
  if (randomNum && (answerExp != '0')) {
    answerExp = '-' + answerExp;
  }
//  answerSciNot = '5.20'; // Testing
//  answerExp = '-1'; // Testing
}

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

  // 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 == 0) || (quizType == 3) || (quizType == 4)) {
    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 modComplexConv() {
  var tempAnswerSci;

  prnEqn[0] = convEqn[2];
  prnEqn[0] = prnEqn[0].replace(/V1/, prnNum[0]);
  prnEqn[0] = prnEqn[0].replace(/U1/, prnUnit[0]);
  prnEqn[0] = prnEqn[0].replace(/V2/, prnSciNum[1]);
  prnEqn[0] = prnEqn[0].replace(/U2/, prnUnit[1]);
  prnEqn[0] = prnEqn[0].replace(/U1/, prnUnit[0]);
  prnEqn[0] = prnEqn[0].replace(/U3/, prnUnit[2]);
  prnEqn[0] = prnEqn[0].replace(/V3/, prnSciNum[2]);
  prnEqn[0] = prnEqn[0].replace(/U2/, prnUnit[1]);

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

function modBasicConv() {
  var tempAnswerSci;

  if (whichPrefix[0] >= 0) {
  // Prefix unit to base unit conversion
    prnEqn[0] = convEqn[1];
    prnEqn[0] = prnEqn[0].replace(/V1/, prnNum[0]);
    prnEqn[0] = prnEqn[0].replace(/U1/, prnUnit[0]);
    prnEqn[0] = prnEqn[0].replace(/V2/, prnSciNum[1]);
    prnEqn[0] = prnEqn[0].replace(/U2/, prnUnit[1]);
    prnEqn[0] = prnEqn[0].replace(/U1/, prnUnit[0]);
  }
  else {
  // Base unit to prefix unit conversion
    prnEqn[0] = convEqn[0];
    prnEqn[0] = prnEqn[0].replace(/V1/, prnNum[0]);
    prnEqn[0] = prnEqn[0].replace(/U1/, prnUnit[0]);
    prnEqn[0] = prnEqn[0].replace(/V2/, prnSciNum[1]);
    prnEqn[0] = prnEqn[0].replace(/U1/, prnUnit[0]);
    prnEqn[0] = prnEqn[0].replace(/U2/, prnUnit[1]);
  }

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

function modSigFig() {
  var modUnder1 = '<SPAN CLASS=\"uUnder\">';
  var modUnder2 = '<\/SPAN>';
  var tempHold;

  if ((answer - 0) < 1) {
  // Number is less than 1
    tempHold = answer.length - numSigFigs;
    prnAnswer = answer.substr(0,tempHold) + modUnder1 + 
                answer.substr(tempHold) + modUnder2;
  }
  else {
  // Number is greater than 1
  // Check for decimal place
    charPos = answer.search(/\./);
    if (charPos == -1) {
    // No decimal place
      prnAnswer = modUnder1 + answer + modUnder2;
    }
    else {
    // Decimal place
      prnAnswer = modUnder1 + answer.substr(0,charPos) +
                  modUnder2 + '.' + modUnder1 + 
                  answer.substr(charPos + 1) + modUnder2;
    }
  }
}

function printPart1Quiz(whichQuizType,whichQuestion) {
  var whichQuizType;
  var whichQuestion;
  var textBoxSize = new Array();

  textBoxSize[0] = '4';
  textBoxSize[1] = '16';
  textBoxSize[3] = '16';
  textBoxSize[4] = '16';

  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: Dimensional Analysis 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\">Dimensional Analysis Quiz<\/H2>\n');
  quizWindow.document.write('<H3 STYLE=\"text-align: center\">' +
    whichQuizType + '<\/H3>\n');
  quizWindow.document.write('<HR STYLE=\"text-align: center\" WIDTH=\"75%\">\n');
  if ((quizType == 0) || ((quizType == 1) && !doSciNotConvert)) {
    quizWindow.document.write('<TABLE BORDER=\"0\">\n<TR>\n<TD>' + whichQuestion +
      '<\/TD>\n<TD><B>' + answer + '<\/B><\/TD>\n<\/TR>\n<\/TABLE>\n');
  }
  else if ((quizType == 1) && doSciNotConvert) {
    quizWindow.document.write('<TABLE BORDER=\"0\">\n<TR>\n<TD>' + whichQuestion +
      '<\/TD>\n<TD><B>' + answerSciNot + '&nbsp;x&nbsp;10<SUP>' + 
      answerExp + '<\/SUP>' + '<\/B><\/TD>\n<\/TR>\n<\/TABLE>\n');
  }
  else if (quizType == 3) {
    quizWindow.document.write('<DIV ALIGN=\"center\">\n' +
      '<TABLE BORDER=\"0\">\n<TR>\n<TD>Convert <B>' +
      prnNum[0] + '&nbsp;' + prnUnit[0] + '</B><\/TD>\n<TD>to <B>' +
      prnUnit[1] + '<\/B><\/TD>\n<\/TR>\n<\/TABLE>\n<\/DIV>\n');
  }
  else if (quizType == 4) {
    quizWindow.document.write('<DIV ALIGN=\"center\">\n' +
      '<TABLE BORDER=\"0\">\n<TR>\n<TD>Convert <B>' +
      prnNum[0] + '&nbsp;' + prnUnit[0] + '</B><\/TD>\n<TD>to <B>' +
      prnUnit[2] + '<\/B><\/TD>\n<\/TR>\n<\/TABLE>\n<\/DIV>\n');
  }
  quizWindow.document.write('<DIV ALIGN=\"center\">\n<FORM NAME=\"dimQuiz\" ' +
    'onSubmit=\"opener.popUpDimQuiz(button); return false\">\n');
  if ((quizType == 0) || (quizType == 1) && doSciNotConvert) {
    quizWindow.document.write('<P><INPUT NAME=\"userNumber\" TYPE=\"text\" ' +
    'SIZE=\"' + textBoxSize[quizType] + '\">\n');
  }
  else if (quizType == 1) {
    quizWindow.document.write('<P><TABLE BORDER=\"0\" CELLSPACING=\"5\">\n<TR>\n' +
      '<TD ROWSPAN=\"2\" ALIGN=\"center\">\n' +
      '<INPUT NAME="\answerPre\" TYPE=\"text\" SIZE=\"' +
      textBoxSize[quizType] + '\">\n<\/TD>\n' +
      '<TD ROWSPAN=\"2\" ALIGN=\"center\">x<\/TD>\n' +
      '<TD ROWSPAN=\"2\" ALIGN=\"center\">10<\/TD>\n<TD ALIGN=\"center\">' +
      '<INPUT NAME="\answerExp\" TYPE=\"text\" SIZE=\"4\">\n<\/TD>\n' +
      '<\/TR>\n<TR><TD>&nbsp;<\/TD><\/TR>\n<\/TABLE>\n');
  }
  else if (quizType == 3) {
    quizWindow.document.write('<P><TABLE BORDER=\"0\" CELLSPACING=\"5\">\n' +
      '<TR><TD COLSPAN=\"4\" ALIGN=\"right\">\n' +
      '<INPUT NAME=\"userNumber\" TYPE=\"text\" SIZE=\"' + textBoxSize[quizType] + '\">' +
      '<\/TD>\n<TD ALIGN=\"center\">' + prnUnit[1] + '<\/TD>' +
      '<TD ALIGN=\"center\">(standard notation)<\/TD>\n<\/TR>\n' +
      '<TR><TD COLSPAN=\"6\" ALIGN=\"center\"><B>or<\/B><\/TD><\/TR>' +
      '<TR>\n<TD ROWSPAN=\"2\" ALIGN=\"center\">\n' +
      '<INPUT NAME="\answerPre\" TYPE=\"text\" SIZE=\"' +
      textBoxSize[quizType] + '\">\n<\/TD>\n' +
      '<TD ROWSPAN=\"2\" ALIGN=\"center\">x<\/TD>\n' +
      '<TD ROWSPAN=\"2\" ALIGN=\"center\">10<\/TD>\n<TD ALIGN=\"center\">' +
      '<INPUT NAME="\answerExp\" TYPE=\"text\" SIZE=\"4\">\n<\/TD>\n' +
      '<TD ROWSPAN=\"2\" ALIGN=\"center\">' + prnUnit[1] + '<\/TD>\n' +
      '<TD ROWSPAN=\"2\" ALIGN=\"center\">(scientific notation)<\/TD>\n' +
      '<\/TR>\n<TR><TD>&nbsp;<\/TD><\/TR>\n' +
      '<\/TABLE>\n');
  }
  else if (quizType == 4) {
    quizWindow.document.write('<P><TABLE BORDER=\"0\" CELLSPACING=\"5\">\n' +
      '<TR><TD COLSPAN=\"4\" ALIGN=\"right\">\n' +
      '<INPUT NAME=\"userNumber\" TYPE=\"text\" SIZE=\"' + textBoxSize[quizType] + '\">' +
      '<\/TD>\n<TD ALIGN=\"center\">' + prnUnit[2] + '<\/TD>' +
      '<TD ALIGN=\"center\">(standard notation)<\/TD>\n<\/TR>\n' +
      '<TR><TD COLSPAN=\"6\" ALIGN=\"center\"><B>or<\/B><\/TD><\/TR>' +
      '<TR>\n<TD ROWSPAN=\"2\" ALIGN=\"center\">\n' +
      '<INPUT NAME="\answerPre\" TYPE=\"text\" SIZE=\"' +
      textBoxSize[quizType] + '\">\n<\/TD>\n' +
      '<TD ROWSPAN=\"2\" ALIGN=\"center\">x<\/TD>\n' +
      '<TD ROWSPAN=\"2\" ALIGN=\"center\">10<\/TD>\n<TD ALIGN=\"center\">' +
      '<INPUT NAME="\answerExp\" TYPE=\"text\" SIZE=\"4\">\n<\/TD>\n' +
      '<TD ROWSPAN=\"2\" ALIGN=\"center\">' + prnUnit[2] + '<\/TD>\n' +
      '<TD ROWSPAN=\"2\" ALIGN=\"center\">(scientific notation)<\/TD>\n' +
      '<\/TR>\n<TR><TD>&nbsp;<\/TD><\/TR>\n' +
      '<\/TABLE>\n');
  }
  quizWindow.document.write('<\/DIV>\n');
}

function printPart2Quiz() {
  quizWindow.document.write('<DIV ID=\"promptIt\" CLASS=\"showIt\" ALIGN=\"center\">\n');
  if (quizType == 0) {
    quizWindow.document.write('<P>(enter your answer in the box and hit ' +
      'the \"Grade Me!\" button)<\/P>\n');
  }
  else if (quizType == 1) {
    if (!doSciNotConvert) {
      quizWindow.document.write('<P>(enter your answers in the boxes and hit ' +
        'the \"Grade Me!\" button)<\/P>\n');
    }
    else {
      quizWindow.document.write('<P>(enter your answer in the box and hit ' +
        'the \"Grade Me!\" button)<\/P>\n');
    }
  }
  else if ((quizType == 3) || (quizType == 4)) {
    quizWindow.document.write('<P>(enter your answer in either standard <B>or<\/B> ' +
      'scientific notation and hit the \"Grade Me!\" button)<\/P>\n');
  }
  quizWindow.document.write('<\/DIV>\n\n');
}

function printSigFigResult() {
  quizWindow.document.write('<DIV ID=\"answer\" CLASS=\"hideIt\" ALIGN=\"center\">\n');
  quizWindow.document.write('<P><B>' + prnAnswer + '<\/B><\/P>\n' +
    '<P><B>(the significant figures have been underlined)<\/B><\/P>');
  quizWindow.document.write('<\/DIV>\n\n');
}

function printSciConvertResult() {
  quizWindow.document.write('<DIV ID=\"answer\" CLASS=\"hideIt\" ALIGN=\"center\">\n');
  if (!doSciNotConvert) {
    quizWindow.document.write('<P><B>' + answerSciNot + 
      '&nbsp;x&nbsp;10<SUP>' + answerExp + '<\/SUP><\/B><\/P>\n');
  }
  else {
    quizWindow.document.write('<P><B>' + answer + '<\/B><\/P>\n');
  }

  if (userDecPoint) {
    quizWindow.document.write('<P><B>(your use of a decimal place in this case was redundant)<\/B><\/P>\n');
  }

  if (isAmbiguous) {
    quizWindow.document.write('<P><B>(note that in standard notation the number of significant figures is ambiguous)<\/B><\/P>\n');
  }
  quizWindow.document.write('<\/DIV>\n\n');
}

function printBasicComplexConversion() {
  quizWindow.document.write('<DIV ID=\"answer\" CLASS=\"hideIt\" ALIGN=\"center\">\n');
  quizWindow.document.write('<P>' + prnEqn[0] + '\n');

  if (userDecPoint) {
    quizWindow.document.write('<P><B>(your use of a decimal place in this case was redundant)<\/B><\/P>\n');
  }
  quizWindow.document.write('<\/DIV>\n\n');
}

function printPart3Quiz() {
  var tempEleNum;

  if (is_winie) {
    tempEleNum = 4;
  }
  else {
    tempEleNum = 5;
  }

  quizWindow.document.write('<\/FORM>\n<DIV ID=\"quizButtons\" ');
  if (!(is_nn && !is_geck)) {
    quizWindow.document.write('CLASS=\"showIt\" ');
  }
  quizWindow.document.write('ALIGN=\"center\">\n' +
    '<FORM NAME=\"dimQuiz1\" onSubmit=\"opener.popUpDimQuiz(button); return false\">\n' +
    '<TABLE BORDER=\"0\"><TR>\n<TD>\n');
  if (!showAnswer && !gradeAnswer) {
    quizWindow.document.write('<INPUT TYPE=\"submit\"\n' +
      'onClick=\"button=this.value\"\n' +
      'onMouseOver=\"mOver(document.forms.length - 2,document.dimQuiz1.elements.length - 3);self.status=\'Grade your answer\';return true\"\n' +
      'onMouseOut=\"mOut(document.forms.length - 2,document.dimQuiz1.elements.length - 3);self.status=\'\';return true\"\n' +
      'VALUE=\"Grade Me!\">\n<\/TD>');
    quizWindow.document.write('<TD>\n<INPUT TYPE=\"submit\"\n' +
      'onClick=\"button=this.value\"\n' +
      'onMouseOver=\"mOver(document.forms.length - 2,document.dimQuiz1.elements.length - 2);self.status=\'Reveal answer and solution\';return true\"\n' +
      'onMouseOut=\"mOut(document.forms.length - 2,document.dimQuiz1.elements.length - 2);self.status=\'\';return true\"\n' +
      'VALUE=\"Reveal!\">\n<\/TD>');
    quizWindow.document.write('<TD>\n<INPUT TYPE=\"reset\"\n' +
      'onMouseOver=\"mOver(document.forms.length - 2,document.dimQuiz1.elements.length - 1);self.status=\'New Quiz\';return true\"\n' +
      'onMouseOut=\"mOut(document.forms.length - 2,document.dimQuiz1.elements.length - 1);self.status=\'\';return true\"\n' +
      'VALUE=\"Reset Value(s)\">\n');
  }
  quizWindow.document.write('<\/TD>\n<\/TR>\n<\/TABLE>\n<\/FORM>\n<\/DIV>\n');
  quizWindow.document.write('<DIV ALIGN=\"center\">\n' +
    '<FORM NAME=\"dimQuiz2\" onSubmit=\"opener.popUpDimQuiz(button); return false\">\n' +
    '<TABLE BORDER=\"0\">\n<TR>\n<TD>\n');
//  if (!is_winie) {
    quizWindow.document.write('<INPUT TYPE=\"submit\"\n' +
      'onClick=\"button=this.value\"\n' +
      'onMouseOver=\"mOver(document.forms.length - 1,document.dimQuiz2.elements.length - 3);self.status=\'New Quiz\';return true\"\n' +
      'onMouseOut=\"mOut(document.forms.length - 1,document.dimQuiz2.elements.length - 3);self.status=\'\';return true\"\n' +
      'VALUE=\"Gimme\' New One!\">\n<\/TD>\n<TD>\n');
//  }
  quizWindow.document.write('<INPUT TYPE=\"button\"\n' +
    'onClick=\"window.close()\"\n' +
    'onMouseOver=\"mOver(document.forms.length - 1,document.dimQuiz2.elements.length - 2);self.status=\'Close this window\';return true\"\n ' +
    'onMouseOut=\"mOut(document.forms.length - 1,document.dimQuiz2.elements.length - 2);self.status=\'\';return true\"\n ' +
    'VALUE=\"Close Window\">\n');
  quizWindow.document.write('<INPUT TYPE=\"button\" onClick=\"opener.focus()\"\n ' +
    'onMouseOver=\"mOver(document.forms.length - 1,document.dimQuiz2.elements.length - 1);self.status=\'Switch back to the tutorial\';return true\"\n ' +
    'onMouseOut=\"mOut(document.forms.length - 1,document.dimQuiz2.elements.length - 1);self.status=\'\';return true\"\n ' +
    'VALUE=\"Switch Windows\">\n');
  quizWindow.document.write('<\/TD><\/TR><\/TABLE>\n<\/FORM>\n<\/DIV>\n');
  printAddress();
  quizWindow.document.write('<\/BODY>\n<\/HTML>');
  quizWindow.focus();
  quizWindow.document.close();
}

function printDimQuiz() {

  userCorrect = 0;
  userDecPoint = 0;

  if (quizType == 0) {
  // Significant figures
    if (!showAnswer && !gradeAnswer) {
    // First time through
      detNumSigFigs(answer);
      modSigFig();
      printPart1Quiz('Significant Figures','Enter the number of significant figures are in the following number: ');
      printPart2Quiz();
      printCorrect();
      printSigFigResult();
      printPart3Quiz();
    }
    else if (gradeAnswer && !revealedAnswer) {
      gradeUserResult();
      if (!userCorrect) {
        quizWindow.alert(prnGradeResult);
      }
      else {
        revealedAnswer = 1;
        quizWindow.document.dimQuiz.userNumber.value = numSigFigs;
        hide('promptIt');
        if (!(is_nn && !is_geck)) {
          hide('quizButtons');
        }
        show('answerCorrect');
        show('answer');
      }
    }
    else if (showAnswer && !revealedAnswer) {
      revealedAnswer = 1;
      quizWindow.document.dimQuiz.userNumber.value = numSigFigs;
      hide('promptIt');
      if (!(is_nn && !is_geck)) {
        hide('quizButtons');
      }
      show('answer');
    }
  }
  else if (quizType == 1) {
  // Convert between scientific notation and standard notation
    if (!showAnswer && !gradeAnswer) {
    // First time through
      if (doSciNotConvert) {
        convert2();
        printPart1Quiz('Scientific Notation\/Standard Notation Conversion','Convert the following number into standard notation: ');
      }
      else {
        convert1();
        printPart1Quiz('Scientific Notation\/Standard Notation Conversion','Convert the following number into scientific notation: ');
      }
      printPart2Quiz();
      printCorrect();
      printSciConvertResult();
      printPart3Quiz();
    }
    else if (gradeAnswer && !revealedAnswer) {
      gradeUserResult();
      if (!userCorrect) {
        quizWindow.alert(prnGradeResult);
      }
      else {
        revealedAnswer = 1;
        if (doSciNotConvert) {
          quizWindow.document.dimQuiz.userNumber.value = prnAnswer;
        }
        else {
          quizWindow.document.dimQuiz.answerPre.value = prnAnswer;
          quizWindow.document.dimQuiz.answerExp.value = answerExp;
        }
        hide('promptIt');
        if (!(is_nn && !is_geck)) {
          hide('quizButtons');
        }
        show('answerCorrect');
        show('answer');
      }
    }
    else if (showAnswer && !revealedAnswer) {
      revealedAnswer = 1;
      if (doSciNotConvert) {
        quizWindow.document.dimQuiz.userNumber.value = answer;
      }
      else {
        quizWindow.document.dimQuiz.answerPre.value = answerSciNot;
        quizWindow.document.dimQuiz.answerExp.value = answerExp;
      }
      hide('promptIt');
      if (!(is_nn && !is_geck)) {
        hide('quizButtons');
      }
      show('answer');
    }
  }
  else if ((quizType == 3) || (quizType == 4)) {
    if (!showAnswer && !gradeAnswer) {
    // First time through
      detNumSigFigs(prnNum[0]);
      doSigFigs();
    // Answer is reported in both standard and scientific notation
      convert1();
      if (quizType == 3) {
        modBasicConv();
      }
      else if (quizType == 4) {
        modComplexConv();
      }
      printPart1Quiz('');
      printPart2Quiz();
      printCorrect();
      printBasicComplexConversion();
      printPart3Quiz();
    }
    else if (gradeAnswer && !revealedAnswer) {
      gradeUserResult();
      if (!userCorrect) {
        quizWindow.alert(prnGradeResult);
      }
      else {
        revealedAnswer = 1;
        quizWindow.document.dimQuiz.userNumber.value = prnAnswer;
        quizWindow.document.dimQuiz.answerPre.value = prnAnswerSciNot;
        quizWindow.document.dimQuiz.answerExp.value = answerExp;
        hide('promptIt');
        if (!(is_nn && !is_geck)) {
          hide('quizButtons');
        }
        show('answerCorrect');
        show('answer');
      }
    }
    else if (showAnswer && !revealedAnswer) {
      revealedAnswer = 1;
      quizWindow.document.dimQuiz.userNumber.value = answer;
      quizWindow.document.dimQuiz.answerPre.value = answerSciNot;
      quizWindow.document.dimQuiz.answerExp.value = answerExp;
      hide('promptIt');
      if (!(is_nn && !is_geck)) {
        hide('quizButtons');
      }
      show('answer');
    }
  }
}

function gradeUserResult() {
  var tempUserResult;
  var tempUserResult2;
  var tempUserResult3;
  var charPos;
  var tempHold;
  var tempHold2;
  var tempHold3;

//  userCorrect = 0;
  if (quizType == 0) {
    tempUserResult = quizWindow.document.dimQuiz.userNumber.value;
    if (tempUserResult == '') {
      prnGradeResult = 'You need to enter a number in order to have something to grade.';
    }
    else {
      tempUserResult -= 0;
      if (tempUserResult != numSigFigs) {
        prnGradeResult = 'Incorrect!';
      }
      else {
        userCorrect = 1;
      }
    }
  }
  else if (quizType == 1) {
    if (!doSciNotConvert) {
    // Standard notation to scientific notation
//      userDecPoint = 0;
      tempUserResult = quizWindow.document.dimQuiz.answerPre.value;
      prnAnswer = tempUserResult;
      if (tempUserResult.substr(tempUserResult.length - 1,1) == '.') {
        tempUserResult = tempUserResult.substr(0,tempUserResult.length - 1);
        userDecPoint = 1;
      }
      tempUserResult2 = quizWindow.document.dimQuiz.answerExp.value;
      if ((!tempUserResult) || (!tempUserResult2)) {
        prnGradeResult = 'You need to enter numbers in both boxes in order to have something to grade.';
      }
      else {
        if ((tempUserResult != answerSciNot) || (tempUserResult2 != answerExp)) {
          prnGradeResult = 'Incorrect!';
          if (tempUserResult != answerSciNot) {
            tempHold = tempUserResult.length;
            charPos = tempUserResult.search(/\./);
            if (charPos != -1) {
              tempHold--;
            }
            tempHold2 = answerSciNot.length;
            charPos = answerSciNot.search(/\./);
            if (charPos != -1) {
              tempHold2--;
            }
            if (tempHold != tempHold2) {
              prnGradeResult += '\nCheck your digits or your number of significant figures.';
            }
          }
          if (tempUserResult2 != answerExp) {
            prnGradeResult += '\nCheck your exponent.';
          }
        }
        else {
          userCorrect = 1;
        }
      }
    }
    else {
    // Scientific notation to standard notation
      tempUserResult = quizWindow.document.dimQuiz.userNumber.value;
      if (tempUserResult == '') {
        prnGradeResult = 'You need to enter a number in order to have something to grade.';
      }
      else {
        prnAnswer = tempUserResult;
        if (tempUserResult.substr(0,1) == '.') {
          tempUserResult = '0' + tempUserResult;
        }
        else if ((tempUserResult.substr(tempUserResult.length - 1,1) == '.') &&
                 (tempUserResult.substr(tempUserResult.length - 2,1) != '0')) {
          tempUserResult = tempUserResult.substr(0,tempUserResult.length - 1);
          userDecPoint = 1;
        }
        if (tempUserResult != answer) {
          prnGradeResult = 'Incorrect!';
          charPos = answer.search(/\./);
          tempHold3 = tempUserResult.search(/\./);
          if ((charPos != -1) && (tempHold3 == -1)) {
            prnGradeResult += '\nYou\'re missing a decimal point.';
          }
          if (((answer > 1) && (tempUserResult < 1)) ||
              ((answer < 1) && (tempUserResult > 1))) {
            prnGradeResult += '\nRecheck the exponent as originally written.';
          }
          if (((charPos != -1) && (tempHold3 != -1)) ||
              ((charPos == -1) && (tempHold3 == -1))) {
            if (answer.length != tempUserResult.length) {
              prnGradeResult += '\nCheck your digits or your number of significant figures.';
            }
          }
        }
        else {
          userCorrect = 1;
        }
      }
    }
  }
  else if ((quizType == 3) || (quizType == 4)) {
    userDecPoint = 0;
    tempUserResult = quizWindow.document.dimQuiz.userNumber.value;
    tempUserResult2 = quizWindow.document.dimQuiz.answerPre.value;
    tempUserResult3 = quizWindow.document.dimQuiz.answerExp.value;
    if ((tempUserResult) && (tempUserResult2 || tempUserResult3)) {
      prnGradeResult = 'Enter your answer using either standard notation or scientific notation but not both notations.';
    }
    else {
      if (!tempUserResult && !tempUserResult2 && !tempUserResult3) {
        prnGradeResult = 'You must enter something in order to be graded.';
      }
      else if ((tempUserResult2 || tempUserResult3) && !(tempUserResult2 && tempUserResult3)) {
        prnGradeResult = 'When using scientific notation, you must enter something in both boxes.';
      }
      else {
        if (tempUserResult) {
        // Standard notation
          prnAnswerSciNot = answerSciNot;
          if ((tempUserResult < 1) && (tempUserResult.substr(0,1) != '0')) {
            prnAnswer = tempUserResult;
            tempUserResult = '0' + tempUserResult;
          }
          else if ((tempUserResult.substr(tempUserResult.length-1,1) == '.') &&
                   (tempUserResult.substr(tempUserResult.length-2,1) != '0')) {
            userDecPoint = 1;
            prnAnswer = tempUserResult;
            tempUserResult = tempUserResult.substr(0,tempUserResult.length - 1);
          }
          else {
            prnAnswer = answer;
          }

          if (tempUserResult == answer) {
            userCorrect = 1;
          }
          else {
            prnGradeResult = 'Incorrect!';
            charPos = answer.search(/\./);
            tempHold3 = tempUserResult.search(/\./);
            if ((charPos != -1) && (tempHold3 == -1)) {
              prnGradeResult += '\nYou\'re missing a decimal point.';
            }
            if (((charPos != -1) && (tempHold3 != -1)) ||
                ((charPos == -1) && (tempHold3 == -1))) {
              if (answer.length != tempUserResult.length) {
                prnGradeResult += '\nCheck your digits or your number of significant figures.';
              }
            }
          }
        }
        else {
        // Scientific notation
          prnAnswer = answer;
          prnAnswerSciNot = tempUserResult2;
          if (tempUserResult2.substr(tempUserResult2.length - 1,1) == '.') {
            tempUserResult2 = tempUserResult2.substr(0,tempUserResult2.length - 1);
            userDecPoint = 1;
          }

//          alert(answerSciNot + '\n' + tempUserResult2); // Testing
          if ((tempUserResult2 != answerSciNot) || (tempUserResult3 != answerExp)) {
            prnGradeResult = 'Incorrect!';
            if (tempUserResult2 != answerSciNot) {
              tempHold = tempUserResult2.length;
              charPos = tempUserResult2.search(/\./);
              if (charPos != -1) {
                tempHold--;
              }
              tempHold2 = answerSciNot.length;
              charPos = answerSciNot.search(/\./);
              if (charPos != -1) {
                tempHold2--;
              }
              if (tempHold != tempHold2) {
                prnGradeResult += '\nCheck your digits or your number of significant figures.';
              }
            }
            if (tempUserResult3 != answerExp) {
              prnGradeResult += '\nCheck your exponent.';
            }
          }
          else {
            userCorrect = 1;
          }



        }
      }
    }
  }
}

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 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 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 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);
}
