var score = 0;

var total_questions = 26;

function begin() {

  if ($('score')) {
    new Ajax.Updater('question','/bb/quizzes/science.php?score='+$('score').value + '&random=' + random_int());
    $('science').style.height='auto';
    $('morestuff').style.display='block';
  } else {
    new Ajax.Updater('question','/bb/quizzes/science.php?question=0&random='+random_int());
  } 

}


function answer(question_id,answer) {
  
  new Ajax.Request('/bb/quizzes/science.php',
    {
      method:'get',
      parameters: { question: question_id, answer: answer, random: random_int() },
      onSuccess: function(transport){
        var response = transport.responseText || "no response text";
        
        if (response.match(/POINTS:(\d+)/)) {
        
          score += parseFloat(RegExp.$1);
          
          percentage = Math.floor((score / total_questions) * 100);
          
          $('progress').style.width = (percentage * 3) + 'px';
          
        } 
        
        question_id++;
        
        if (question_id >= total_questions) {
        
          dest_url = escape('www.oneplusyou.com/bb/science?score=' + percentage);
          window.location = "http://www.oneplusyou.com/q/v/m2_interstitial?dest_url=" + dest_url + '&view_key=science';
          
        //  new Ajax.Updater('question','/bb/quizzes/science.php?score='+percentage+'&random='+random_int());
        //  $('science').style.height='auto';
        //  $('morestuff').style.display='block';
          
        } else {
          new Ajax.Updater('question','/bb/quizzes/science.php?question='+question_id+'&random='+random_int());
        }
        
      },
      onFailure: function(){ alert('Something went wrong...') }
    });
}


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