var score = 0;
var quiz = 2;
var total_questions = 20;
var img_width = 13;
var max_score = 100;
var q = 1;

function begin() {
  
  new Ajax.Updater('questionbox','../../q/brain.aspx?quiz='+quiz+'&question=0&random='+random_int());
  $('qcount').innerHTML='Question ' + q++ + ' of ' + total_questions;
}


function answer(question_id,answer) {
  
  $('questionbox').innerHTML = 'Loading...';
  
  new Ajax.Request('../../q/brain.aspx',
    {
      method:'get',
      parameters: { question: question_id, answer: answer, random: random_int() },
      onSuccess: function(transport){
        
        var response = transport.responseText || "no response text";
        
        
        if (q > total_questions)
        {
			$('score_graph').style.display = 'none';
		}
		else
		{
			$('qcount').innerHTML='Question ' + q++ + ' of ' + total_questions;
        }
        
		if (response.match(/PTS:(\d+)/)) {
        
			score += parseFloat(RegExp.$1);
			percentage = (score / (max_score)) * 100;
			//$('progress').style.width = Math.floor((percentage * img_width/5)) + 'px';
			percentage = Math.floor(percentage);
			if (parseFloat(RegExp.$1)>0)
			{

				$('ans').innerHTML='<IMG SRC="../../q/70s/images/right.gif" />';
				$('ans').style.color='green';
			}
			else
			{
				$('ans').innerHTML='<IMG SRC="../../q/70s/images/wrong.gif" />';
				$('ans').style.color='red';
			}
        }


        
        
        question_id++;
        
        if (question_id >= total_questions) {
          new Ajax.Updater('questionbox','../../q/brain.aspx?quiz='+quiz+'&score='+percentage+'&random='+random_int());
        } else {
          new Ajax.Updater('questionbox','../../q/brain.aspx?quiz='+quiz+'&question='+question_id+'&random='+random_int());
        }
        
      },
      onFailure: function(){ alert('Sorry, there was an error.') }
    });
}


function random_int() {
 return Math.floor(Math.random()*100000);
}
