$(document).ready(function() {
  $('#quiz-score').hide();
  if($('input#btnSubmit')){
    $('input#btnSubmit').bind("click", function(e){
      var isFullyAnswered = 1;

      $.each($('input[@type=radio]'), function() {
          var tmpVal = $('input[@type=radio][@name=' + $(this)[0].name + ']').getValue();
          if(tmpVal == '')
          {
            alert('Tidak semua pertanyaan diisi');
            isFullyAnswered = 0;
            return false;
          }
        }
      );
  
      if(!isFullyAnswered)
        return false;
      else
        $('#btnSubmit').remove();

      var postVar = '';
      jQuery.each($("div.question"),function(){
        var postAns = '';
        jQuery.each($(this).find("input"),function(){
          if($(this)[0].checked){
            postVar += $(this)[0].name + ',' + $(this)[0].value + ';';
          }
        });
      });
      
      postVar += '';
      
      var totalQuestion = 0;
      var correctAnswer = 0;
      
      $.ajax({
        type: "POST",
        url: "/learn/useful-tools/quiz-analyzer.aspx",
        data: "ansList="+postVar,
        success: function(xml){
          var data = "";
          jQuery.each($("comment", xml),function(){
            $('p#'+$(this).attr("id")).html($(this).text());
            $('span#quiz-answer_'+$(this).attr("answerID")).text($(this).attr("answer") == 1 ? 'Benar!' : 'Salah!').show();
            
            totalQuestion++;
            if($(this).attr("answer") == 1)
              correctAnswer++;
          });
          
          $('#quiz-summary').text('Anda berhasil menjawab ' + correctAnswer + ' dari ' + totalQuestion + ' pertanyaan dengan benar');
          $('#quiz-score span').text(Math.ceil(eval(correctAnswer / totalQuestion * 100)));
          $('#quiz-score').show();
        }
      });
    });
  }
});
