var grid;
var gridx;
var gridy;
var down = 0;
var wcards;
var scrfrm;
var gn;
var res;

function solve() {
    document.getElementById('nosolve').value = 1;
    document.forms[0].submit();
}

function m_over(obj,cell,v,wc) {
  obj.style.background='#ddddfc';
  var el=top.document.getElementById(cell);
  el.value=v;
  el.className='inv';
  el.style.border='2px solid #444';
}

function m_out(obj,cell,v) {
  obj.style.background='#ffffdc';
  var el=top.document.getElementById(cell);
  el.className='cell';
  el.value='';
  el.style.border='2px solid white';
}

function isWild(el) {
    var ltr = el.value.toUpperCase();
    if (ltr < 'A' || ltr > 'Z') return;
    if (el.style.color == 'red') {
	el.style.color = 'black';
	wcards.value = wcards.value.replace(el.id, '');
    } else {
	el.style.color = 'red';
        wcards.value = (wcards.value + '|' + el.id);
    }
}

function nextltr(e,row,col) {
  var srow = ((row < 10) ? '0' : '') + row;
  var scol = ((col < 10) ? '0' : '') + col;
  var el = document.getElementById('m' + srow + '' + scol);
  if ((e.keyCode >=37 && e.keyCode <=40) || (e.keyCode >=65 && e.keyCode <=90)) {
    if (e.keyCode == 38) row--;
    if (e.keyCode == 40) row++;
    if (el.value.length > 0 || e.keyCode == 39) {
      if (e.keyCode != 37 && e.keyCode != 38 && e.keyCode != 40) {
        if (e.keyCode >= 65 && e.keyCode <= 90) {
          el.value = String.fromCharCode(e.keyCode);
	} else {
          if (e.keyCode != 39) el.value = '';
        }
        row += e.shiftKey ? 1 : 0;
        col += e.shiftKey ? 0 : 1;
      }
    }
    if (col < 15 && row < 15) {
      srow = ((row < 10) ? '0' : '') + row;
      scol = ((col < 10) ? '0' : '') + col;
      var next = document.getElementById('m' + srow + scol);
      next.focus();
    }
  }
}

function backspace(e,row,col) {
  var srow = ((row < 10) ? '0' : '') + row;
  var scol = ((col < 10) ? '0' : '') + col;
  var el = document.getElementById('m' + srow + '' + scol);

  if (e.keyCode == 56) {
      isWild(el);
      return;
  }

  if (e.keyCode == 8 || e.keyCode == 37)  {
     if (el.value.length == 0 || e.keyCode == 37)  {
        row -= e.shiftKey ? 1 : 0;
	col -= e.shiftKey ? 0 : 1;
        if (col >= 0 && row >= 0) {
	  srow = ((row < 10) ? '0' : '') + row;
	  scol = ((col < 10) ? '0' : '') + col;
          var prev = document.getElementById('m' + srow + scol);
	  prev.focus();
	  if (e.keyCode == 8) prev.value = '';
        }
     } else {
        el.value = '';
     }
     return false;
  }
  return false;
}

function noenter(e) {
    return (e.keyCode != 13);
}

function nosubmit(e,obj) {    
    if (e.keyCode == 13) {
	var go = document.getElementById('go');
        go.focus();
        scrfrm.submit();
  }
}

function showdlg(op) {
  document.getElementById('savedlg').style.display = 'none';
  document.getElementById('opendlg').style.display = 'none';
  document.getElementById('deldlg' ).style.display = 'none';
  document.getElementById('msg'    ).style.display = 'none';
  document.getElementById(op).style.display = '';
  if (op == 'msg') {
    document.getElementById('rackdiv').style.display = '';
  } else {
    document.getElementById('rackdiv').style.display = 'none';
  }
  return false;
}


function showmenu(e) {
   alert(e.clientX + ' ' + e.clientY);
}

var defs  = new Array();
var words = new Array();

function cleardefs() {
    for (var i=1;i<7;i++) {
	document.getElementById('def'+i).innerHTML = '';
    }
}

function lookup(word, elem, num) {
  var xmlHttp;

  var target = document.getElementById('def' + elem);
  target.innerHTML = '';

  if (word == '') return;

  var i = 0;
  for (i=1;i<elem;i++) {
      if (words[i] == word) {
	  return;
      }
  }
  words[elem] = word;

  if (defs[word]) {
      target.innerHTML = defs[word];
      return;
  }

  try {
    xmlHttp=new XMLHttpRequest();
  }

  catch (e) {
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
	alert("Upgrade your browser");
	return false;
      }
    }
  }

  var url = '/fcgi-bin/dict.pl?ax=1&word=' + word + '&num=' + num;

  xmlHttp.onreadystatechange=function() {
      defs[word] = 'Searching for definition...';
      if(target && xmlHttp.readyState==4) {      
          defs[word] = xmlHttp.responseText;
	  target.innerHTML = xmlHttp.responseText;
      }
  }

   xmlHttp.open("GET", url,true);
   xmlHttp.send(null);
}

function answers() {
  var xmlHttp;

  try {
    xmlHttp=new XMLHttpRequest();
  }

  catch (e) {
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
	alert("Upgrade your browser");
	return false;
      }
    }
  }

  var url = '/fcgi-bin/scrabble_help.pl?gn=' + gn.value;

  xmlHttp.onreadystatechange=function() {
      res.innerHTML = xmlHttp.responseText;
      if(res && xmlHttp.readyState==4) {      
	  res.innerHTML = xmlHttp.responseText;
      }
  }

   xmlHttp.open("GET", url,true);
   xmlHttp.send(null);
}
