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;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("What is your response? Would you like to receive Jesus Christ now?");
  document.writeln("</H4>");

  document.write("<H4 ALIGN='LEFT'>");
  document.write("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A] Acknowledge your sin and be willing to turn from it.<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;B] Believe Christ died for your sins and rose from the grave.<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C] Commit you life to Christ; receive Him as Savior and Lord.");
  document.writeln("</H4>");

  document.write("<H4 ALIGN='CENTER'>");
  document.write("We would like to talk with you or send you more information.<BR><a HREF='mailto:pcadmin@verizon.net'>Contact us.</a>");
  document.writeln("</H4>");

  document.writeln('<CENTER>');
  document.writeln('<BLOCKQUOTE><FONT SIZE="2"><I>');
  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");
  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);
}