var currentQuestion = 0;
var attemptAtQuestion = 0;

function readCookie()
{
  var cookie=document.cookie;
  currentQuestion=getNumberValue(cookie,"currentQuestion");
  attemptAtQuestion=getNumberValue(cookie,"attemptAtQuestion");
}

function getNumberValue(s,n)
{
  var s=removeBlanks(s);
  var pairs=s.split(";");
  for(var i=0; i<pairs.length; ++i)
  {
    var pairSplit=pairs[i].split("=");
    if(pairSplit[0]==n)
    {
      if(pairSplit.length>1) 
      {
        return parseInt(pairSplit[1]);
      }
      else
      {
        return 0;
      }
    }
  }
  return 0;
}

function removeBlanks(s)
{
  var temp="";
  for(var i=0; i<s.length; ++i)
  {
    var c=s.charAt(i);
    if(c!=" ")
    {
      temp+=c;
    }
  }
  return temp;
}

function displayVerses()
{
  if(attemptAtQuestion==0)
  {
    for(var currentVerse=0; currentVerse<verses[currentQuestion].length; ++currentVerse)
    {
    document.writeln('<P><I>'+verses[currentQuestion][currentVerse]+'<BR></I></P>');
    }
  }
  else
  {
    for(var currentVerse=0; currentVerse<underscoreVerses[currentQuestion].length; ++currentVerse)
    {
    document.writeln('<P><I>'+underscoreVerses[currentQuestion][currentVerse]+'<BR></I></P>');
    }
  }
}

function askNextQuestion()
{  
  if(attemptAtQuestion==0)
  {
    if(currentQuestion==2)
    {
      document.writeln('<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
      document.writeln('<I><FONT SIZE="3" COLOR="#000000"><B>Question '+(currentQuestion+1));
      document.writeln(' of '+questions.length+'</B></FONT>');
      document.writeln('<FONT SIZE="3" COLOR="#000000"><B>&nbsp;&nbsp;&nbsp;You\'re doing great. Keep it up.</B></FONT>');
      document.writeln('<BR></I></P>');
    }  
    else if(currentQuestion==5)
    {
      document.writeln('<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
      document.writeln('<I><FONT SIZE="3" COLOR="#000000"><B>Question '+(currentQuestion+1));
      document.writeln(' of '+questions.length+'</B></FONT>');
      document.writeln('<FONT SIZE="3" COLOR="#000000"><B>&nbsp;&nbsp;&nbsp;Great work.  You\'re almost done.</B></FONT>');
      document.writeln('<BR></I></P>');
    }
    else  
    {
      document.writeln('<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
      document.writeln('<I><FONT SIZE="3" COLOR="#000000"><B>Question '+(currentQuestion+1)+' of '+questions.length);
      document.writeln('<BR></B></FONT></I></P>');
    }
  }
  else
  {
    document.writeln('<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
    document.writeln('<I><FONT SIZE="3" COLOR="#000000"><B>Question '+(currentQuestion+1));
    document.writeln(' of '+questions.length+'</B></FONT>');
    document.writeln('<FONT SIZE="3" COLOR="#CC9900"><B>&nbsp;&nbsp;&nbsp;Incorrect! Please try again.</B></FONT>');
    document.writeln('<BR></I></P>');
  }

  document.writeln("<P ALIGN='LEFT'><FONT SIZE='3'><B>");
  document.writeln('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')
  document.writeln(questions[currentQuestion][0]+"</B></FONT></P>");

  displayAnswers();
}

function displayAnswers()
{
  document.writeln('<FORM NAME="answerForm">');
  for(var currentAnswer=1; currentAnswer<questions[currentQuestion].length; ++currentAnswer)
  {
    document.writeln('<P ALIGN="LEFT"><FONT SIZE="3"><B>');
    document.writeln('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')
    document.writeln('<INPUT TYPE="RADIO" NAME="answer">')
    document.writeln(questions[currentQuestion][currentAnswer]);
  }

  document.writeln('<BR><BR><DIV ALIGN="CENTER"><INPUT TYPE="BUTTON" NAME="continue" VALUE="Continue" onClick="checkAnswers()"></DIV>');

  document.writeln('</B></FONT></P>')
  document.writeln('</FORM>')
}

function checkAnswers()
{
  for(var answerChoice=0; answerChoice<questions[currentQuestion].length-1; ++answerChoice)
  {
    if(document.answerForm.elements[answerChoice].checked)
    {
      if(answerChoice==answers[currentQuestion])
      {
        correct();
        break;
      }
      else
      {
        incorrect();
        break;
      }
    }
    if(answerChoice==questions[currentQuestion].length-1)
    {
      incorrect();
      break;
    }
  }
}

function correct()
{
  ++currentQuestion;
  //reset attempts for new question
  attemptAtQuestion=0; 
  updateCookie();
  location.reload(true);
}

function incorrect()
{
  ++attemptAtQuestion;
  updateCookie();
  location.reload(true);
}

function updateCookie()
{
  document.cookie="currentQuestion="+currentQuestion;
  document.cookie="attemptAtQuestion="+attemptAtQuestion;
}

function endQuiz()
{
  document.cookie="currentQuestion=0";
  document.cookie="attemptAtQuestion=0";
  document.writeln('<FORM NAME="finishedForm">');

  document.write("<H4 ALIGN='CENTER'>");
  document.write("We trust that as you read the Scripture verses, that you have repented of sin and believed, you then have received the free gift of eternal life through Jesus Christ and have become a new creature.");
  document.writeln("</H4>");

  document.writeln('<CENTER>');
  document.writeln('<BLOCKQUOTE><FONT SIZE="2"><I>');
  document.write("Therefore if any man is in Christ, he is a new creature; the old things passed away; behold, new things have come. <BR>2 Corinthians 5:17<BR><BR>");
  document.write("He is able to keep you from stumbling, and to make you stand in the presence of His glory blameless with great joy.  Jude 24<BR><BR>");
  document.write("If you confess with your mouth that Jesus is Lord and believe in your heart that God raised him from the dead, you will be saved. Romans 10:9<BR><BR>");
  document.writeln('</I></FONT>');
  document.writeln('</BLOCKQUOTE>');

  document.writeln('<INPUT TYPE="BUTTON" NAME="restart" VALUE="Restart" ');
  document.writeln(' onClick="restartQuiz()">');
  document.writeln('</CENTER>');
  document.writeln('</FORM>');
}

function restartQuiz()
{
  location.reload(true);
}
