var mol = new Array();
var react = new Array();
var tempReact0 = new Array();
var tempReact1 = new Array();
var quizName = new Array();
var reactNum = new Array();
var volts = new Array();
var holdReact = new Array();
var potName = new Array();
var electrons = new Array();
var reactMult = new Array();
var balReact = new Array();
var voltDel;
var holdMol;
var quizWindowOpened;
var quizType;
var quizWidth = 750;
var modMol;
var textWidth;
var is_macie;
var is_winie;
var is_justie;
var is_nn4;
var helpWindowOpened;
var emailAdd = 'aearhart@femtowatt-club.com';
var dateMod = '5\/28\/02';

quizName[0] = 'Standard Cell Potentials';
quizName[1] = 'Standard Cell Potentials and Net Cell Reactions';

potName[0] = 'Cathode';
potName[1] = 'Anode';

// Molecules
//   'basic species,charge,state label'

  mol[0] = 'F2,0,g';
  mol[1] = 'F,-1,aq';
  mol[2] = 'PbO2,0,s';
  mol[3] = 'SO4,-2,aq';
  mol[4] = 'H,+1,aq';
  mol[5] = 'PbSO4,0,s';
  mol[6] = 'H2O,0,l';
  mol[7] = 'HOCl,0,aq';
  mol[8] = 'Cl2,0,g';
  mol[9] = 'MnO4,-1,aq';
 mol[10] = 'Mn,+2,aq';
 mol[11] = 'PbO2,0,s';
 mol[12] = 'Pb,+2,aq';
 mol[13] = 'BrO3,-1,aq';
 mol[14] = 'Br,-1,aq';
 mol[15] = 'Au,+3,aq';
 mol[16] = 'Au,0,s';
 mol[17] = 'Cl,-1,aq';
 mol[18] = 'O2,0,g';
 mol[19] = 'Br2,0,aq';
 mol[20] = 'NO3,-1,aq';
 mol[21] = 'NO,0,g';
 mol[22] = 'Hg,+2,aq';
 mol[23] = 'Hg,0,l';
 mol[24] = 'Ag,+1,aq';
 mol[25] = 'Ag,0,s';
 mol[26] = 'Fe,+3,aq';
 mol[27] = 'Fe,+2,aq';
 mol[28] = 'I2,0,s';
 mol[29] = 'I,-1,aq';
 mol[30] = 'NiO2,0,s';
 mol[31] = 'Ni(OH)2,0,s';
 mol[32] = 'OH,-1,aq';
 mol[33] = 'Cu,+2,aq';
 mol[34] = 'Cu,0,s';
 mol[35] = 'H2SO3,0,aq';
 mol[36] = 'H2,0,g';
 mol[37] = 'Sn,+2,aq';
 mol[38] = 'Sn,0,s';
 mol[39] = 'Ni,+2,aq';
 mol[40] = 'Ni,0,s';
 mol[41] = 'Co,+2,aq';
 mol[42] = 'Co,0,s';
 mol[43] = 'Pb,0,s';
 mol[44] = 'Cd,+2,aq';
 mol[45] = 'Cd,0,s';
 mol[46] = 'Fe,0,s';
 mol[47] = 'Cr,+3,aq';
 mol[48] = 'Cr,0,s';
 mol[49] = 'Zn,+2,aq';
 mol[50] = 'Zn,0,s';
 mol[51] = 'Al,+3,aq';
 mol[52] = 'Al,0,s';
 mol[53] = 'Mg,+2,aq';
 mol[54] = 'Mg,0,s';
 mol[55] = 'Na,+1,aq';
 mol[56] = 'Na,0,s';
 mol[57] = 'Ca,+2,aq';
 mol[58] = 'Ca,0,s';
 mol[59] = 'K,+1,aq';
 mol[60] = 'K,0,s';
 mol[61] = 'Li,+1,aq';
 mol[62] = 'Li,0,s';

// reactions' string format=
//   potential (in volts),
//   # of electrons,
//   total # reactants,
//   total # products,
//   index of component 1, # of component 1,
//   index of component 2, # of component 2,
//   etc.

// The reactants are listed first followed by the products

 react[0] = '+2.87,2,1,1,0,1,1,2';
 react[1] = '+1.69,2,3,2,2,1,3,1,4,4,5,1,6,2';
 react[2] = '+1.63,2,2,2,7,2,4,2,8,1,6,2';
 react[3] = '+1.51,5,2,2,9,1,4,8,10,1,6,4';
 react[4] = '+1.46,2,2,2,11,1,4,4,12,1,6,2';
 react[5] = '+1.44,6,2,2,13,1,4,6,14,1,6,3';
 react[6] = '+1.42,3,1,1,15,1,16,1';
 react[7] = '+1.36,2,1,1,8,1,17,2';
 react[8] = '+1.23,4,2,1,18,1,4,4,6,2';
 react[9] = '+1.07,2,1,1,19,1,14,2';
react[10] = '+0.96,3,2,2,20,1,4,4,21,1,6,2';
react[11] = '+0.85,2,1,1,22,1,23,1';
react[12] = '+0.80,1,1,1,24,1,25,1';
react[13] = '+0.77,1,1,1,26,1,27,1';
react[14] = '+0.54,2,1,1,28,1,29,2';
react[15] = '+0.49,2,2,2,20,1,6,2,31,1,32,1';
react[16] = '+0.34,2,1,1,33,1,34,1';
react[17] = '+0.17,2,2,2,3,1,4,4,35,1,6,1';
react[18] = '0.00,2,1,1,4,2,36,1';
react[19] = '-0.14,2,1,1,37,1,38,1';
react[20] = '-0.25,2,1,1,39,1,40,1';
react[21] = '-0.28,2,1,1,41,1,42,1';
react[22] = '-0.36,2,1,2,5,1,43,1,3,1';
react[23] = '-0.40,2,1,1,44,1,45,1';
react[24] = '-0.44,2,1,1,27,1,46,1';
react[25] = '-0.74,3,1,1,47,1,48,1';
react[26] = '-0.76,2,1,1,49,1,50,1';
react[27] = '-0.83,2,1,2,6,2,36,1,32,2';
react[28] = '-1.66,3,1,1,51,1,52,1';
react[29] = '-2.37,2,1,1,53,1,54,1';
react[30] = '-2.71,1,1,1,55,1,56,1';
react[31] = '-2.76,2,1,1,57,1,58,1';
react[32] = '-2.92,1,1,1,59,1,60,1';
react[33] = '-3.05,1,1,1,61,1,62,1';

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 closeChildWindows() {
  if (quizWindowOpened) {
    if (!quizWindow.closed) {
      quizWindow.close();
    }
  }
  if (helpWindowOpened) {
    if (!helpWindow.closed) {
      helpWindow.close();
    }
  }
}

function initValues(isAdmin) {
  var isAdmin;
  
  if (isAdmin == 1) {
    window.document.adminelectro.password.value = '';
  }
  quizWindowOpened = 0;
  helpWindowOpened = 0;
  checkBrowser();
  quizHeight = 325;
  if (is_justie) {
    textWidth = 10;
    quizHeight = 375;
  }
  else {
    textWidth = 9;
  }
}

function popUpElectroQuiz(tempValue) {
  var tempValue;

  quizType = tempValue;
//  if ((is_nn4) && (quizType == 1)) {
//    alert('Your browser appears to be Netscape Navigator v4 and this '+
//      'quiz will not work in this browser.');
//  }
//  else {
    electroQuiz();
//  }
}

function chooseRandomReactions() {
  var uniqueReact;

  reactNum[0] = Math.round((react.length - 1)*Math.random());
  uniqueReact = 0;
  while (!uniqueReact) {
    reactNum[1] = Math.round((react.length - 1)*Math.random());
    if (reactNum[0] != reactNum[1]) {
      uniqueReact = 1;
    }
  }
}

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

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

function myVoid() { }

function doFlipEquation() {
  var tempHold = new Array();
  var currCount;
  var workReact;
  
  tempHold = holdReact[1].split(',');
  tempHold[0] = (-1)*(tempHold[0] - 0);
  workReact = tempHold[0] + ',' + tempHold[1] + ',' + 
    tempHold[3] + ',' + tempHold[2] + ',';

  for (currCount = 0; currCount < tempHold[3]; currCount++) {
   workReact += tempHold[4 + (tempHold[2]*2) + (currCount*2)] + ',';
   workReact += tempHold[5 + (tempHold[2]*2) + (currCount*2)] + ',';
  }

  for (currCount = 0; currCount < tempHold[2]; currCount++) {
    workReact += tempHold[4 + (currCount*2)] + ',';
    workReact += tempHold[5 + (currCount*2)];
    if (currCount != (tempHold[2] - 1)) {
      workReact += ',';
    }
  }

  holdReact[1] = workReact;
}

function dupCheck() {
  var currReactNum;
  var currProdNum;

  for (currReactNum=0; currReactNum<balReact[0]; currReactNum++) {
    for (currProdNum=0; currProdNum<balReact[1]; currProdNum++) {
      if (balReact[2+(currReactNum*2)] == balReact[2+(currProdNum*2)+(balReact[0]*2)]) {
        if (balReact[3+(currReactNum*2)] < balReact[3+(currProdNum*2)+(balReact[0]*2)]) {
          balReact[3+(currProdNum*2)+(balReact[0]*2)] -= balReact[3+(currReactNum*2)];
          balReact[3+(currReactNum*2)] = 0;
        }
        else {
          balReact[3+(currReactNum*2)] -= balReact[3+(currProdNum*2)+(balReact[0]*2)];
          balReact[3+(currProdNum*2)+(balReact[0]*2)] = 0;
        }
      }
    }
  }
}

function assembleBalanced() {
  var offset;

  if (electrons[0] == electrons[1]) {
    reactMult[0] = 1;
    reactMult[1] = 1;
  }
  else {
    if ((electrons[0]/electrons[1]) == Math.round(electrons[0]/electrons[1])) {
      reactMult[0] = 1;
      reactMult[1] = electrons[0]/electrons[1];
    }
    else if ((electrons[1]/electrons[0]) == Math.round(electrons[1]/electrons[0])) {
      reactMult[0] = electrons[1]/electrons[0];
      reactMult[1] = 1;
    }
    else {
      reactMult[0] = electrons[1];
      reactMult[1] = electrons[0];
    }
  }
  balReact = [];
  balReact[0] = tempReact0[2] + tempReact1[2];
  balReact[1] = tempReact0[3] + tempReact1[3];
  for (currCount = 0; currCount < tempReact0[2]; currCount++) {
    balReact[2+(currCount*2)] = tempReact0[4+(currCount*2)];
    balReact[3+(currCount*2)] = reactMult[0]*tempReact0[5+(currCount*2)];
  }
  offset = tempReact0[2]*2;
  for (currCount = 0; currCount < tempReact1[2]; currCount++) {
    balReact[2+(currCount*2)+offset] = tempReact1[4+(currCount*2)];
    balReact[3+(currCount*2)+offset] = reactMult[1]*tempReact1[5+(currCount*2)];
  }
  offset += tempReact1[2]*2;
  for (currCount = 0; currCount < tempReact0[3]; currCount++) {
    balReact[2+(currCount*2)+offset] = tempReact0[4+(currCount*2)+(tempReact0[2]*2)];
    balReact[3+(currCount*2)+offset] = reactMult[0]*tempReact0[5+(currCount*2)+(tempReact0[2]*2)];
  }
  offset += tempReact0[3]*2;
  for (currCount = 0; currCount < tempReact1[3]; currCount++) {
    balReact[2+(currCount*2)+offset] = tempReact1[4+(currCount*2)+(tempReact1[2]*2)];
    balReact[3+(currCount*2)+offset] = reactMult[1]*tempReact1[5+(currCount*2)+(tempReact1[2]*2)];
  }
  for (currCount = 0; currCount < balReact.length; currCount++) {
    balReact[currCount] = balReact[currCount] - 0;
  }
  dupCheck();
}

function printBalReaction() {
  var currCount;
  var endCount;
  var offset;
  var firstHit;

  assembleBalanced();
  quizWindow.document.write('<DIV ID=\"balanced\" CLASS=\"hideIt\" ' +
    'ALIGN=\"center\">\n');
  quizWindow.document.write('<TABLE ALIGN=\"center\" BORDER=\"0\" STYLE=\"color: red\">\n' +
    '<TR>\n<TD ALIGN=\"center\" STYLE=\"color: red\" NOWRAP>\n<B>Cell Rxn.<\/B>:<\/TD>' +
    '<TD ALIGN=\"left\">\n');
  quizWindow.document.write('<TABLE BORDER=\"0\" STYLE=\"color: red\">\n<TR>\n');
  firstHit = 0;
  for (currCount = 0; currCount < balReact[0]; currCount++) {
    holdMol = mol[balReact[2 + (currCount*2)]];
    modifyMolecule();
    if (balReact[3 + (currCount*2)]) {
      if (firstHit) {
        quizWindow.document.write('<TD ALIGN=\"center\" VALIGN=\"middle\" ' +
          'STYLE=\"font-size: larger;color: red\">+<\/TD> \n');
      }
      if (balReact[3 + (currCount*2)] != 1) {
        quizWindow.document.write('<TD ALIGN=\"right\" STYLE=\"color: red\" VALIGN=\"middle\">\n' +
          balReact[3 + (currCount*2)] + '<\/TD>\n');
      }
      quizWindow.document.write('<TD ALIGN=\"left\" STYLE=\"color: red\" VALIGN=\"middle\">\n' + 
        modMol + '\n<\/TD>\n');
      if (!firstHit) {
        firstHit = 1;
      }
    }
  }
  quizWindow.document.write('<TD ALIGN=\"center\" VALIGN=\"middle\">\n' +
    '<IMG SRC=\"Images\/arrow-balance.gif\" WIDTH=\"26\" HEIGHT=\"19\" ALT=\"arrow\">\n</TD>\n');
  firstHit = 0;
  for (currCount = 0; currCount < balReact[1]; currCount++) {
    holdMol = mol[balReact[2+(currCount*2)+(balReact[0]*2)]];
    modifyMolecule();
    if (balReact[3+(currCount*2)+(balReact[0]*2)]) {
      if (firstHit) {
        quizWindow.document.write('<TD ALIGN=\"center\" VALIGN=\"middle\" ' +
          'STYLE=\"font-size: larger;color: red\">+<\/TD> \n');
      }
      if (balReact[3+(currCount*2)+(balReact[0]*2)] != 1) {
        quizWindow.document.write('<TD ALIGN=\"right\" STYLE=\"color: red\" VALIGN=\"middle\">\n' +
          balReact[3+(currCount*2)+(balReact[0]*2)] + '<\/TD>\n');
      }
      quizWindow.document.write('<TD ALIGN=\"left\" STYLE=\"color: red\" VALIGN=\"middle\">\n' + 
        modMol + '\n<\/TD>\n');
      if (!firstHit) {
        firstHit = 1;
      }
    }
  }
  quizWindow.document.write('<\/TR>\n<\/TABLE>\n<\/TD>\n<\/TR>\n<\/TABLE>');
  quizWindow.document.write('\n<\/DIV>\n');
}

function printReactions() {
  var currCount;
  var currIndex;
  var endCount;
  var tempValue;

  quizWindow.document.write('<FORM NAME=\"reaction\" ' +
    'onSubmit=\"actionReaction(button); return false\">\n');
  quizWindow.document.write('<TABLE ALIGN=\"center\" BORDER=\"0\">\n' +
    '<TR><TD COLSPAN=\"2\">&nbsp;<\/TD>\n<TD ALIGN=\"center\">\n' +
    '<B>E<SUP>o</SUP> (volts)<\/B>\n<\/TD>\n<\/TR>\n');
  for (var currReact = 0; currReact < 2; currReact++) {
    quizWindow.document.write('<TR>\n<TD ALIGN=\"right\"><B>' +
      potName[currReact] + '<\/B>:<\/TD>\n');
    quizWindow.document.write('<TD ALIGN=\"center\">\n' +
      '<TABLE ALIGN=\"center\" BORDER=\"0\">\n<TR>\n');
    if (currReact) {
      endCount = tempReact1[2];
    }
    else {
      endCount = tempReact0[2];
    }
    for (currCount = 0; currCount < endCount; currCount++) {
      currIndex = 4 + (currCount*2);
      if (currReact) {
        holdMol = mol[tempReact1[currIndex]];
      }
      else {
        holdMol = mol[tempReact0[currIndex]];
      }
      modifyMolecule();
      if (currReact) {
        tempValue = tempReact1[5 + (currCount*2)];
      }
      else {
        tempValue = tempReact0[5 + (currCount*2)];
      }
      if (tempValue != 1) {
        quizWindow.document.write('<TD ALIGN=\"right\" VALIGN=\"middle\">\n' +
          tempValue + '<\/TD>\n');
      }
      quizWindow.document.write('<TD ALIGN=\"left\" VALIGN=\"middle\">\n' + 
        modMol + '\n<\/TD>\n');
      if (currCount != (endCount - 1)) {
        quizWindow.document.write('<TD ALIGN=\"center\" VALIGN=\"middle\" ' +
          'STYLE=\"font-size: larger\">+<\/TD> \n');
      }
    }
    if (!currReact) {
      quizWindow.document.write('<TD ALIGN=\"center\" VALIGN=\"middle\" ' +
        'STYLE=\"font-size: larger\">+<\/TD> \n'+
        '<TD ALIGN=\"right\" VALIGN=\"middle\">\n');
      if (currReact) {
        tempValue = tempReact1[1];
      }
      else {
        tempValue = tempReact0[1];
      }
      quizWindow.document.write(tempValue);
      quizWindow.document.write('<\/TD>\n<TD ALIGN=\"left\" VALIGN=\"middle\">\ne<SUP>-<\/SUP>\n<\/TD>\n');
    }
    quizWindow.document.write('<TD ALIGN=\"center\" VALIGN=\"middle\">\n' +
      '<IMG SRC=\"Images\/arrow.gif\" WIDTH=\"26\" HEIGHT=\"19\" ' +
      'ALT=\"arrow\">\n</TD>\n');
    if (currReact) {
      endCount = tempReact1[3];
    }
    else {
      endCount = tempReact0[3];
    }
    for (currCount = 0; currCount < endCount; currCount++) {
      if (currReact) {
        tempValue = tempReact1[2];
      }
      else {
        tempValue = tempReact0[2];
      }
      currIndex = 4 + (2*tempValue) + (currCount*2);
      if (currReact) {
        holdMol = mol[tempReact1[currIndex]];
      }
      else {
        holdMol = mol[tempReact0[currIndex]];
      }
      modifyMolecule();
      if (currReact) {
        tempValue = tempReact1[5 + (2*tempReact1[2]) + (currCount*2)];
      }
      else {
        tempValue = tempReact0[5 + (2*tempReact0[2]) + (currCount*2)];
      }
      if (tempValue != 1) {
        quizWindow.document.write('<TD ALIGN=\"right\" VALIGN=\"middle\">\n' +
          tempValue + '<\/TD>\n');
      }
      quizWindow.document.write('<TD ALIGN=\"left\" VALIGN=\"middle\">\n' + 
        modMol + '\n<\/TD>\n');
      if (currCount != (endCount - 1)) {
        quizWindow.document.write('<TD ALIGN=\"center\" VALIGN=\"middle\" ' +
          'STYLE=\"font-size: larger\">+<\/TD> \n');
      }
    }
    if (currReact) {
      quizWindow.document.write('<TD ALIGN=\"center\" VALIGN=\"middle\" ' +
        'STYLE=\"font-size: larger\">+<\/TD> \n'+
        '<TD ALIGN=\"right\" VALIGN=\"middle\">\n');
      if (currReact) {
        tempValue = tempReact1[1];
      }
      else {
        tempValue = tempReact0[1];
      }
      quizWindow.document.write(tempValue);
      quizWindow.document.write('<\/TD>\n<TD ALIGN=\"left\" VALIGN=\"middle\">\ne<SUP>-<\/SUP>\n<\/TD>\n');
    }
    quizWindow.document.write('<\/TR>\n<\/TABLE>\n<\/TD>\n' +
      '<TD ALIGN=\"center\" VALIGN=\"middle\">\n' + volts[currReact] + '<\/TD>\n');
    quizWindow.document.write('<\/TR>\n');
  }
  quizWindow.document.write('<\/TABLE>\n<HR WIDTH=\"75%\">\n')
  if (quizType == 1) {
    printBalReaction();
    if (is_nn4) {
      quizWindow.document.write('\n\n<\/DIV>\n\n');
    }
  }
  quizWindow.document.write('<DIV ALIGN=\"center\">\n');
  quizWindow.document.write('<IMG ALIGN=\"middle\" SRC=\"Images\/dele-unit.gif\" WIDTH=\"137\" HEIGHT="32" ALT=\"Delta E\">\n' +
    '&nbsp;<INPUT NAME=\"userdele\" TYPE=\"text\" SIZE=\"' + textWidth + '\">\n');
  if (quizType == 1) {
    quizWindow.document.write('<BR><BR>\n(Pressing \"Reveal!\" will also ' +
      'show the net cell reaction)\n<BR>\n');
  }
  else if (!quizType) {
    quizWindow.document.write('<BR><BR>\n');
  }
  quizWindow.document.write('\n' +
    '<INPUT TYPE=\"submit\" onMouseOver=\"mOver(0,document.forms[0].elements.length - 4)\" onMouseOut=\"mOut(0,document.forms[0].elements.length - 4)\" onClick=\"button=this.value\" VALUE=\"Grade It!\">\n' +
    '<INPUT TYPE=\"submit\" onMouseOver=\"mOver(0,document.forms[0].elements.length - 3)\" onMouseOut=\"mOut(0,document.forms[0].elements.length - 3)\" onClick=\"button=this.value\" VALUE=\"Reveal!\">\n');
//  if (!is_winie) {
    quizWindow.document.write('<INPUT TYPE=\"submit\" onMouseOver=\"mOver(0,document.forms[0].elements.length - 2)\" onMouseOut=\"mOut(0,document.forms[0].elements.length - 2)\" onClick=\"button=this.value\" VALUE=\"Gimme\' Another One\">\n');
//  }
  quizWindow.document.write('<BR><BR><INPUT TYPE=\"submit\" ' +
    'onClick=\"button=this.value\" onMouseOver=\"mOver(0,document.forms[0].elements.length - 1)\" onMouseOut=\"mOut(0,document.forms[0].elements.length - 1)\" VALUE=\"Reset Values\">\n' +
    '<\/DIV>\n\n');
  quizWindow.document.write('<\/FORM>\n');
}

function openHelpWindow() {
  helpWindowOpened = 1;
  helpWindow = window.open('electro-help.html','helpWindowName','resizable=1,scrollbars=1,status=1,width=400,height=400');
  helpWindow.focus();
}

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

function printStandardHeader() {
  var currCount;

  quizWindow.document.write('<HEAD>\n<TITLE>');
  quizWindow.document.write('Electrochemistry Quiz');
  quizWindow.document.write('<\/TITLE>\n');
  quizWindow.document.write('<STYLE TYPE=\"text/css\">\n<!--' +
    '.hideIt {position: relative; visibility: hidden;}\n' +
    '.showIt {position: relative; visibility: visible;}\n\/\/-->\n' +
    '<\/STYLE>\n\n');

  quizWindow.document.write('<SCR' + 'IPT LANGUAGE=\"javascript\">\n');

  // actionReaction

  quizWindow.document.write('\nfunction actionReaction(whichAction) {\n' +
    'var tempValue;\nvar whichAction;\n' +
    'if (whichAction == \'Reveal!\') {\n' +
    'opener.revealValues();\n}\nelse if (whichAction == \'Grade It!\'){ \n' +
    'opener.gradeValues();\n}\nelse if (whichAction == \'Reset Values\'){\n' +
    'opener.resetValues();\n}\nelse {\n' +
    'opener.popUpElectroQuiz(' + quizType + ');\n}\n}\n\n');

  printButtonChanges();
  quizWindow.document.write('<\/SCR' + 'IPT>\n\n<\/HEAD>\n');
}

function calcTrueValues() {
  var tempValue;

  volts[0] = tempReact0[0] - 0;
  volts[1] = tempReact1[0] - 0;
  electrons[0] = tempReact0[1] - 0;
  electrons[1] = tempReact1[1] - 0;
  voltsDel = volts[0] + volts[1];
  voltsDel = (Math.round(voltsDel*100))/100;
  if (voltsDel > 0) {
    voltsDel = voltsDel + '';
    voltsDel = '+' + voltsDel;
  }
  else if (!voltsDel) {
    voltsDel = '0.00';
  }
  else {
    voltsDel = voltsDel + '';
  }
  if (voltsDel != '0.00') {
    while (voltsDel.length < 5) {
      if (voltsDel.charAt(1) == '.') {
        voltsDel = voltsDel.charAt(0) + '0' + voltsDel.substr(1);
      }
      else if (voltsDel.length == 2) {
        voltsDel += '.00';
      }
      else if (voltsDel.length == 4) {
        voltsDel += '0';
      }
    }
  }
  for (var currCount = 0; currCount < 2; currCount++) {
    if (volts[currCount]) {
      if (volts[currCount] > 0) {
        volts[currCount] = volts[currCount] + '';
        volts[currCount] = '+' + volts[currCount];
      }
      else {
        volts[currCount] = volts[currCount] + '';
      }
      while (volts[currCount].length < 5) {
        if (volts[currCount].charAt(1) == '.') {
          volts[currCount] = volts[currCount].charAt(0) + '0' + 
            volts[currCount].substr(1);
        }
        else if (volts[currCount].length == 2) {
          volts[currCount] += '.00';
        }
        else if (volts[currCount].length == 4) {
          volts[currCount] += '0';
        }
      }
    }
    else {
      volts[currCount] = '0.00';
    }
  }
}

function gradeValues() {
  var tempValue;
  var alertString;

  alertString = '';
  tempValue = quizWindow.document.reaction.userdele.value;
  if ((tempValue.charAt(0) == '+') || (tempValue.charAt(0) == '-')) {
    if (tempValue.length > 1) {
      if (tempValue.charAt(1) == '.') {
        tempValue = tempValue.charAt(0) + '0' + tempValue.substr(1);
      }
    }
  }
  if (tempValue != voltsDel) {
    alertString = 'Incorrect!';
    if (!tempValue) {
      alertString += '\nYou need to enter something!';
    }
    else if ((tempValue.charAt(0) != '+') && (tempValue.charAt(0) != '-')) {
      alertString += '\nMake sure you explicitly include the sign.';
    }
    else if (tempValue.length < 4) {
      alertString += '\nWatch your significant figures.';
    }
    else if (tempValue.charAt(tempValue.length - 3) != '.') {
      alertString += '\nWatch your significant figures.';
    }
  }
  else {
    alertString = 'Correct!';
  }
  quizWindow.alert(alertString);
}

function revealValues() {
  quizWindow.document.reaction.userdele.value = voltsDel;
  if (quizType == 1) {
    show('balanced');
  }
}

function resetValues() {
  quizWindow.document.reaction.userdele.value = '';
  if (quizType == 1) {
    hide('balanced');
  }
}

function electroQuiz() {
  var currCount;

  chooseRandomReactions();
  holdReact[0] = react[reactNum[0]];
  holdReact[1] = react[reactNum[1]];
  doFlipEquation();
  tempReact0 = holdReact[0].split(',');
  tempReact1 = holdReact[1].split(',');
  for (currCount = 0; currCount < tempReact0.length; currCount++) {
    tempReact0[currCount] = tempReact0[currCount] - 0;
  }
  for (currCount = 0; currCount < tempReact1.length; currCount++) {
    tempReact1[currCount] = tempReact1[currCount] - 0;
  }
  calcTrueValues();
  quizWindowOpened = 1;
  quizWindow = window.open('','quizWindowName','resizable=1,scrollbars=1,status=1,width=' + quizWidth + ',height=' + quizHeight);
  quizWindow.document.write('<HTML>\n');
  printStandardHeader();
  quizWindow.document.write('<BODY STYLE=\"background-color: #FFFFFF\">\n');
  quizWindow.document.write('<H2 ALIGN=\"center\">' + quizName[quizType] + 
    ' Quiz<\/H2>\n\n');
  printReactions();
  quizWindow.document.write('<DIV ALIGN=\"center\">\n<FORM>\n' +
      '<INPUT TYPE=\"button\" onClick=\"window.close()\"\n ' +
      'onMouseOver=\"mOver(document.forms.length - 1,0);self.status=\'Close this window\';return true\"\n ' +
      'onMouseOut=\"mOut(document.forms.length - 1,0);self.status=\'\';return true\"\n ' +
      'VALUE=\"Close Window\">\n' +
      '<INPUT TYPE=\"button\" onClick=\"opener.focus()\"\n ' +
      'onMouseOver=\"mOver(document.forms.length - 1,1);self.status=\'Switch back to the tutorial\';' +
      'return true\"\n onMouseOut=\"mOut(document.forms.length - 1,1);self.status=\'\';return true\"\n ' +
      'VALUE=\"Switch Windows\">\n' +
      '<\/FORM>\n<\/DIV>\n\n');
  addressPrint();
  quizWindow.document.write('<\/BODY>\n<\/HTML>');
  quizWindow.document.close();
  quizWindow.focus();
}

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