$(document).ready(function(){
  //setFormEvents();
});

var loader = "<div id=\"loading\"><img src=\"/images/ajax-loader.gif\" /><\/div>";

function getTopic(f){
  var theDiv = document.getElementById("submit-answers");
  //theDiv.innerHTML = "hello";
  
  var ajax = new ajaxRequest();
  theDiv.innerHTML = loader;  
  
  ajax.onreadystatechange=function()
  {
    if(ajax.readyState==4){
      theDiv.innerHTML = ajax.responseText;
      setFormEvents();
      if(f == 1){
        document.getElementById("answer").focus();
      }
    }
  }
  
  ajax.open("GET","/ajax/getGameboardTopic.php?ie="+Math.random(),true);

  ajax.send(null);  
  
}

function setFormEvents(){

  $(".gameboard-answer #answer").keydown(function(e){
    if(e.keyCode == 13){
      submitAnswer();
    }
  });
  
  $(".gameboard-button button").click(function(){
    submitAnswer();
  });  
  
  

}

function submitAnswer(){

  var topic = $(".gameboard-topic").text();
  var answer = $(".gameboard-answer #answer").attr("value");
  $("#submit-answers").html(loader);
  
  $.post("/ajax/postGameboardSuggestion.php", {
     topic: topic,
     answer: answer
  }, function(data, textStatus){
        if(textStatus == "success"){
          $("#submit-answers").html(data);
          setFormEvents();
          document.getElementById("answer").focus();
        } else {
          alert("Error submitting");
        }
      });
}