/*
Globals
*/

// This gets modified by index.php using the size value in the database
var order = 0;
var active_cell_id = "empty";

/*
Functions
*/

function submit_puzzle()
{        
  var correct = 0;
  var incorrect = 0;
  var empty = 0;
  
  reset_hints();
  
  for (y = 0; y < order; y++)
  {
    for (x = 0; x < order; x++)
    {
      var id = "x" + x + "y" + y;
      var input = document.getElementById(id);
      var unsolved = input.value;
      var name = input.name;
      var length = name.length;
            
      var ok = "true";
           
      if (isNaN(unsolved) || unsolved < 1 || unsolved > 9)
      {
        if (unsolved == "")
        {
          ok = "false";
          empty++;
          input.className = "empty_cell";
        }
        else
        {
          alert("Only the numbers 1 through 9 are allowed.\n" +
                "Found and deleted '" + unsolved + "'.");
          input.value = "";
        }
      }
            
      if (ok == "true" && length == 6)
      {
        var solved = name.substring(5);
              
        if (isNaN(solved) || solved < 1 || solved > 9)
        {
          ok = "false";
        }
              
        if (ok == "true")
        {
          if (unsolved == solved)
          {
            correct++;
            input.className = "correct_cell";
          }
          else
          {
            incorrect++;
            input.className = "incorrect_cell";
          }
        }
      }
    }
  }

  if (incorrect == 0 && empty == 0)
  {
    alert("You got all of them correct!!!");
  }
  else
  {
    alert("You have " + correct + " correct, " + incorrect +
          " incorrect, and " + empty + " empty.");
  }
}

function set_active_cell(id)
{
  if (document.getElementById(id).disabled == false)
  {
    active_cell_id = id;
  }
  else
  {
    active_cell_id = "empty";
  }

  toggle_hint_button();
}

function reset_hints()
{
  active_cell_id = "empty";
  toggle_hint_button();
}

function reset_puzzle()
{
  reset_hints();

  for (y = 0; y < order; y++)
  {
    for (x = 0; x < order; x++)
    {
      var id = "x" + x + "y" + y;
      var input = document.getElementById(id);
      var disabled = input.disabled;
      
      if (disabled)
      {
        input.className = "correct_cell";
      }
      else
      {
        input.className = "empty_cell";
      }
    }
  }
}

function add_hint()
{
  var input = document.getElementById(active_cell_id);
  var name = input.name;
        
  if (name.length == 6)
  {
    var solved = name.substring(5);
    if (!isNaN(solved) && solved >= 1 && solved <= 9)
    {
      input.value = solved;
      input.className = "correct_cell";
    }
  }
}

function toggle_hint_button()
{
  if (active_cell_id == "empty")
  {
    document.getElementById("hint_button").disabled = true;
  }
  else
  {
    document.getElementById("hint_button").disabled = false;
  }
}

function change_difficulty()
{
  var difficulty_button = document.getElementById("difficulty_button");
  var value = difficulty_button.value;
  if (value.length == 1)
  {
    parent.location="index.php?difficulty=" + value;
  }
}

function check_keypress(e)
{
  var keynum = 0;
  var keychar;
  var numcheck;
  var input;
  
  if (window.event) // IE
  {
    keynum = e.keyCode
  }
  else if (e.which) // Netscape/Firefox/Opera
  {
    keynum = e.which
  }
  keychar = String.fromCharCode(keynum)

  if (e.target)
    input = e.target;
  else if (e.srcElement)
    input = e.srcElement;
  if (input.nodeType == 3) // defeat Safari bug
   input = input.parentNode;

  // ENTER/RETURN == 13
  // BACKSPACE == 8
  // DELETE = 46

  if ((!isNaN(keychar) && keychar >= 1 && keychar <= 9) ||
      keynum == 8 ||
      keynum == 46)
  {
    input.className = "empty_cell";
  }
  else
  {
    // input.value = "";
    // alert("Only the numbers 1 through 9 are valid choices.");
  }
}