numAnswers = new Array();
answers = new Array();
var acc = 0;
var loop;
var numRight = 0;

/* ============================== */

function checkForm()
{
if ((document.test.realname.value == '') || (document.test.email.value == '') || (document.test.phone.value == ''))
{
  alert('Please provide values for all required fields');
  return false;
}

else
{
  return true;
}

}

/* ============================== */

function buildRedirect(score)
{
var sPath = location.href;
var sRedirect = sPath.substring(0, sPath.lastIndexOf('/') + 1) + 'testcomplete.html?score=' + score;
return sRedirect;
}

/* ================================ */

function scoreTest(skip)
/* skip is the number of form elements before the first radio button */
{
var popup = false;

if (!checkForm())
{
  return false;
}

if (document.test.showpopup.value == 'yes')
  popup = true;
else
  popup = false;


for (loop = 0; loop < answers.length; loop++)
{
   if (document.test.elements[answers[loop] + acc + skip].checked)
      numRight++;
 
   acc = acc + numAnswers[loop];
}

acc = 0;

/*
** fill in some values on the form to be included in email
*/
document.test.testscore.value = Math.round((numRight / answers.length) * 100) + '%';
document.test.studentname.value = document.test.realname.value;
document.test.studentemail.value = document.test.email.value;
document.test.subject.value = document.test.subject.value + ' -- ' + document.test.studentname.value;
document.test.testversion.value = document.test.version.value;
/*
** build URL to send student to after scoring test
*/
document.test.redirect.value = buildRedirect(Math.round((numRight / answers.length) * 100));



if (popup)
{
tstWindow=window.open('','tstWindow','scrollbars=yes,resizable=no,height=350,width=550');
tstWindow.document.writeln('<html><head><title>Results</title></head><body bgcolor=#ffffff>');
tstWindow.document.writeln('<P><CENTER><FONT face=arial size=3><B>Test Results</B></FONT></CENTER><BR>');
tstWindow.document.writeln('<P><FONT face=arial>You answered <B>', Math.round((numRight / answers.length) * 100),'%</B> of the questions correctly.</FONT><BR>');

if (numRight != answers.length)
{
   tstWindow.document.writeln('<P><FONT face=arial>You missed the following:</FONT><BR>');
   for (loop = 0; loop < answers.length; loop++)
   {
      if (!document.test.elements[answers[loop] + acc + skip].checked)
         tstWindow.document.writeln('<FONT face=arial><B>#', loop+1, '</B></FONT><BR>');
      acc = acc + numAnswers[loop];
   }
   tstWindow.document.writeln('<P><FONT face=arial>Feel free to look over the questions you missed.</FONT><BR>');
}
else
   tstWindow.document.writeln('<P><CENTER><FONT face=arial><B>Congratulations on your perfect score!</B></FONT></CENTER><BR>');
tstWindow.document.writeln('</body></html>');
tstWindow.document.close();
}

acc = 0;
numRight = 0;

return true;
}

/******************************************/

function checkBrowser()
{
if (navigator.userAgent.indexOf('AOL') != -1) /* if AOL */
{
  document.writeln('<p><div class="jswarning">It appears that you are using an AOL browser. If you cannot play the MP3 audio clip, please check your settings for AOL, or use a different browser. To change your media player, do the followning:<ol><li>Go to Keyword (Ctrl+K)<li>Write <i>aol preferences</i><li>Click on <i>aol preferences</i><li>Click on <i>Categories</i><li>Click on <i>Multimedia</i><li>Uncheck <i>use AOL media player for supported file types</i></ol></div>');
}

} /* checkBrowser */
