/* Javascript functions for DendroQuiz
 *
 * Trevor Fountain, 10 August 2008
 */

function register_quiz_events() {
  $('family').onchange = function () { compare('family'); };
  $('genus').onchange = function () { compare('genus'); };
  $('species').onchange = function () { compare('species'); };
  
  $('family').onkeyup = $('family').onchange;
  $('genus').onkeyup = $('genus').onchange;
  $('species').onkeyup = $('species').onchange;
  
  $('next').onclick = function() { return next(parseInt($('index').value)); }
}

function register_admin_events() {
}

function compare(field) {
  if($(field).value == $(field+".correct").value) {
    $(field+".check").src="images/correct.png";
    $(field).style.background="#bdb";
  } else {
    $(field+".check").src="images/wrong.png";
    $(field).style.background="#ccc";
  }
}

function next(index) {
  index += 1;
  new Ajax.Request('rpc/next.php?index='+index,
    {
      method:'get',
      requestHeaders: {Accept: 'application/json'},
      onSuccess: function(xml) {
        var json = xml.responseText.evalJSON();
        $('index').value=json[0];
        $('vernacular').innerHTML = json[1][3];
        $('family').value = '';
        $('genus').value = '';
        $('species').value = '';
        $('family').style.background = "#ccc";
        $('genus').style.background = "#ccc";
        $('species').style.background = "#ccc";
        $('family.correct').value = json[1][0];
        $('genus.correct').value = json[1][1];
        $('species.correct').value = json[1][2];
        
        compare('family');
        compare('genus');
        compare('species');
        
        $('family').focus();
      }
  });
  return false;
}

function toggle_c(checkbox) {
  checkbox.checked = !checkbox.checked;
}
