var square = -204;
var dir = 0;
var cols = new Array("#FAEDD3", "#B8D3DA");
var cheat = 0;
var clues = 0;
var set = new Array("Cryptic</b>", "Straight</b>");

function check() {
  var r = 0;
  var m = ht*wd;
  for (i=0; i<m; i++) {
    if (grid[i]==guess[i]) r++;
  }
  if (r==m) {
    if (cheat==0) alert("Correct!");
    else alert("Cheat!");
  }
  else alert("Sorry, you have some letters wrong");
}

function wipe() {
  if (confirm("Clear all letters?")) {
    cheat = 0;
    var p;
    var m = ht*wd;
    for (i=0; i<m; i++) {
      if (grid[i]!=0) {
        p = "p"+i;
        document.getElementById(p).firstChild.nodeValue = " ";
        guess[i] = 0;
      }
    }
  }
}

function select(x) {
  var d;
  if (square!=-204) {
    d = "d"+square;
    document.getElementById(d).style.backgroundColor = "transparent";
  }
  if (x==square) dir = 1-dir;
  if (x==-204) x = 0;
  d = "d"+x;
  document.getElementById(d).style.backgroundColor = cols[dir];
  square = x;
}

function getKey(e) {
  var p = "p"+square;
  if (e.keyCode) keycode=e.keyCode;
  else keycode=e.which;
  if (keycode==53) select(square);
  if (keycode==56 && square>(wd-1)) move(-wd);
  if (keycode==52 && square>0) move(-1);
  if (keycode==54 && square<(ht*wd-1)) move(1);
  if (keycode==50 && square<(ht*wd-wd)) move(wd);
  if (keycode==47 && square!=-204) {
    document.getElementById(p).firstChild.nodeValue = " ";
    guess[square] = 0;
  }
  if (keycode<97) keycode+=32;
  if (keycode>96 && keycode<123 && square!=-204) {
    document.getElementById(p).firstChild.nodeValue = String.fromCharCode(keycode-32);
    guess[square] = keycode-32;
    if (dir==0 && square<(ht*wd-1)) move(1);
    if (dir==1 && square<(ht*wd-wd)) move(wd);
  }
}

function move(m) {
  if (square==-204) {
    document.getElementById("d0").style.backgroundColor = cols[dir];
    square = 0;
  }
  else {
    if (grid[square+m]!=0) {
      var d = "d"+square;
      document.getElementById(d).style.backgroundColor = "transparent";
      d = "d"+(square+m);
      document.getElementById(d).style.backgroundColor = cols[dir];
      square += m;
    }
  }
}
function save() {
  var c = "cw" + cw + "=";
  var htwd = ht*wd;
  for (i=0; i<htwd; i++) {
    if (grid[i]!=0) {
      c += (guess[i]==0) ? "#" : String.fromCharCode(guess[i]);
    }
  }
  var expires = new Date();
  expires.setTime(expires.getTime() + 30 * 24 * 60 * 60 * 1000);
  c += ";expires="+expires+";path=/";
  document.cookie = c;
  alert("Grid Saved");
}
function load2() {
  if (document.cookie != "") {
    var c = unescape(document.cookie);
    var cwn = "cw"+cw;
    if (c.indexOf(cwn) != -1) {
      var cat = c.indexOf(cwn);
      var htwd = ht*wd;
      var p;
      var j = cat+4;
      for (i=0; i<htwd; i++) {
        if (grid[i]!=0) {
          guess[i] = (c.charAt(j)=="#") ? 0 : c.charCodeAt(j);
          p = "p"+i;
          document.getElementById(p).firstChild.nodeValue = (c.charAt(j)=="#") ? " " : c.charAt(j);
          j++;
        }
      }
      alert("Grid Loaded");
    }
  }
}
function load() {
  if (document.cookie != "") {
    var c = unescape(document.cookie);
    var cArray = c.split("; ");
    var cwn = "cw"+cw+"=";
    var cpos = -1;
    for (i=0; i<cArray.length; i++) {
      if (cArray[i].indexOf(cwn)==0) cpos = i;
    }
    if (cpos != -1) {
      c = cArray[cpos].split("=")[1];
      var htwd = ht*wd;
      var p;
      var j = 0;
      for (i=0; i<htwd; i++) {
        if (grid[i]!=0) {
          guess[i] = (c.charAt(j)=="#") ? 0 : c.charCodeAt(j);
          p = "p"+i;
          document.getElementById(p).firstChild.nodeValue = (c.charAt(j)=="#") ? " " : c.charAt(j);
          j++;
        }
      }
      alert("Grid Loaded");
    }
  }
}
function answers() {
  cheat = 1;
  document.location = document.location.protocol + "//" + document.location.hostname + document.location.pathname + "#answers";
  var htwd = ht*wd;
  var p;
  for (i=0; i<htwd; i++) {
    if (grid[i]!=0) {
      p = "p"+i;
      document.getElementById(p).firstChild.nodeValue = String.fromCharCode(grid[i]);
      guess[i] = grid[i];
    }
  }
}

function swap() {
  var c = "ac"+clues;
  document.getElementById(c).style.display = "none";
  c = "dc"+clues;
  document.getElementById(c).style.display = "none";
  clues = 1-clues;
  document.getElementById("ac").innerHTML = "<b>ACROSS- " + set[clues];
  document.getElementById("dc").innerHTML = "<b>DOWN- " + set[clues];
  c = "ac"+clues;
  document.getElementById(c).style.display = "block";
  c = "dc"+clues;
  document.getElementById(c).style.display = "block";
}