var questionNbr = 0;
var answerChars = new Array('1','X','2');

function printQuestion(questionNbr){
  var question = '';
  question = questions[questionNbr];
  questionHTML = '';
  questionHTML += '<table cellpadding=4 border=0 cellspacing=0 width=512>';

  height = 46;
  width = 512;
  src = '/images/QuestionHeadline.swf';
  flashvars = 'time=&content='+question['body']+'&nbr='+(questionNbr+1);
  id = 'questionHeadline';

  questionHTML += '	<tr><td colspan=4 style="padding:opx;padding-bottom:4px;">';
  questionHTML += getFlashHTML(src,height,width,flashvars,id);
  questionHTML += '</td>';
  questionHTML += '</tr>';

  questionHTML += '<tr>';
  questionHTML += '<td colspan="2" style="padding:0px;">';
  questionHTML += '<img src="'+question['image']+'">';
  questionHTML += '</td>';
  questionHTML += '</tr>';

  answers = question['answers'];
  for(var i in answers){
    height = 32;
    width = 512;
    src = '/images/QuestionAlternative.swf';
    flashvars = 'time=&content='+answers[i]['answer']+'&letter='+answerChars[i]+'&theUrl=javascript:printCorrectQuestion(questionNbr,'+i+');';
    id = 'alternative';

    questionHTML += '	<tr><td colspan=4 style="padding:opx;padding-bottom:1px;">';
    questionHTML += getFlashHTML(src,height,width,flashvars,id);
    questionHTML += '</td>';
    questionHTML += '</tr>';
  }
  questionHTML += '<tr>';
  questionHTML += '<td>';

  questionHTML += '</table>';

  document.getElementById('question').innerHTML = questionHTML;
}

function printCorrectQuestion(questionNbr,answer){
  var question = '';
  question = questions[questionNbr];

  for(var i in answers){
    if(answers[i]['correct'] == '1'){
      question['correctAnswer'] = i;
      questions[questionNbr]['correctAnswer'] = i;
    }
  }
  questions[questionNbr]['givenAnswer'] = answer;

  questionHTML = '';
  questionHTML += '<table cellpadding=4 border=0 cellspacing=0 width=512>';

  height = 46;
  width = 512;
  if(questions[questionNbr]['givenAnswer'] == questions[questionNbr]['correctAnswer']){
    src = '/images/QuestionHeadline_right.swf';
  }
  else{
    src = '/images/QuestionHeadline_wrong.swf';
  }
  flashvars = 'time=&content='+question['body']+'&nbr='+(questionNbr+1);
  id = 'questionHeadline';

  questionHTML += '	<tr><td colspan=4 style="padding:opx;padding-bottom:4px;">';
  questionHTML += getFlashHTML(src,height,width,flashvars,id);
  questionHTML += '</td>';
  questionHTML += '</tr>';

  questionHTML += '<tr>';
  questionHTML += '<td colspan="2" style="padding:0px;">';
  questionHTML += '<img src="'+question['image']+'">';
  questionHTML += '</td>';
  questionHTML += '</tr>';




  width = 512;
  if(questions[questionNbr]['givenAnswer'] == questions[questionNbr]['correctAnswer']){
    src = '/images/QuestionCorrectAnswer.swf';
    height = 28;
  }
  else{
    src = '/images/QuestionWrongAnswer.swf';
    height = 60;
  }
  flashvars = 'time=&content='+question['body']+'&correct='+answers[question['correctAnswer']]['answer']+'&wrong='+answers[answer]['answer'];
  id = 'questionHeadline';

  questionHTML += '	<tr><td colspan=4 style="padding:opx;padding-bottom:4px;">';
  questionHTML += getFlashHTML(src,height,width,flashvars,id);
  questionHTML += '</td>';
  questionHTML += '</tr>';

  if(question['answer'].length != 0) {
    height = 46;
    width = 512;
    src = '/images/QuestionAnswer.swf';
    flashvars = 'time=&content='+encodeURI(question['answer'])+'';
    id = 'questionHeadline';

    questionHTML += '	<tr><td colspan=4 style="padding:opx;padding-bottom:1px;">';
    questionHTML += getFlashHTML(src,height,width,flashvars,id);
    questionHTML += '</td>';
    questionHTML += '</tr>';
  }

  height = 32;
  width = 512;
  src = '/images/QuestionNext.swf';
  flashvars = 'time=&theUrl=javascript:nextQuestion();';
  id = 'alternative';

  questionHTML += '	<tr><td colspan=4 style="padding:opx;padding-bottom:1px;">';
  questionHTML += getFlashHTML(src,height,width,flashvars,id);
  questionHTML += '</td>';
  questionHTML += '</tr>';

  questionHTML += '</table>';

  document.getElementById('question').innerHTML = questionHTML;
}


function printResult(){
  nbrOfQuestions = questions.length;
  nbrOfCorrectAnswers = 0;

  questionHTML = '';

  for(var i in questions){
    if(questions[i]['correctAnswer'] == questions[i]['givenAnswer']){
      nbrOfCorrectAnswers++;
    }
    //questionHTML += questions[i]['correctAnswer']+' '+questions[i]['givenAnswer']+'<br>';
  }

  document.location = '/'+currentPage+'?nbrOfCorrectAnswers='+nbrOfCorrectAnswers+'&nbrOfQuestions='+nbrOfQuestions;

  questionHTML += '<table cellpadding=4 border=0 cellspacing=0 width=512>';
  questionHTML += '<tr>';
  questionHTML += '<td colspan="2" style="padding:0px;">';
  questionHTML += '<img src="/pageImages/default1.jpg">';
  questionHTML += '</td>';
  questionHTML += '</tr>';
  questionHTML += '<tr>';
  questionHTML += '<td colspan="2" style="padding-left:0px;">';
  questionHTML += '<h4><font color=red>Resultat '+nbrOfCorrectAnswers+' av '+nbrOfQuestions+'</font></h4>';
  questionHTML += '</td>';
  questionHTML += '</tr>';
  questionHTML += '<tr>';
  questionHTML += '<td colspan=2 align=right>';
  questionHTML += '<a class="button" id="red" href="/'+currentPage+'"><span>Gör ett till kunskapstest</span></a>';
  questionHTML += '</td>';
  questionHTML += '</tr>';
  questionHTML += '</table>';
  questionHTML += '<iframe src="/elevsidan/registrera_testresultat/?section='+currentPage+'&score='+nbrOfCorrectAnswers+'&nbrOfQuestions='+nbrOfQuestions+'" style="height:1px;width:1px;border:0px;"></iframe>';

  //document.getElementById('question').innerHTML = questionHTML;
}

function nextQuestion(){
  if(questionNbr < 9){
    questionNbr++;
    printQuestion(questionNbr,questions);
  }
  else{
    printResult();
  }
}
