/*
HTML Code, Javascript, CSS, Images, and Text Copyright (c) 2006-2008 KakuroConquest.com, All Rights Reserved. Copyright infringers will be prosecuted.
*/

var isSafari = navigator.userAgent.toLowerCase().indexOf('safari')!=-1;

function ku(t) {
  var newClassName = '';
  if (t.value.length <= 1)
    newClassName = 'cellNumberInput1';
  else if (t.value.length <= 2)
    newClassName = 'cellNumberInput2';
  else if (t.value.length <= 3)
    newClassName = 'cellNumberInput3';
  else if (t.value.length <= 5)
    newClassName = 'cellNumberInput4';
  else if (t.value.length <= 6)
    newClassName = 'cellNumberInput5';
  else if (t.value.length <= 8)
    newClassName = 'cellNumberInput6';
  else
    newClassName = 'cellNumberInput7';
  if (t.className != newClassName) {
    t.className = newClassName;
    if (isSafari) {
      var cp = gCP(t);
      sCP(t,0,0);
      t.blur();
      lastT = t;
      setTimeout('lastT.focus();sCP(lastT,'+cp.start+','+cp.end+')',0);
    }
  }
  return true;
}

var ignoreNextKey = false;
var allowBackSpace = false;

function dontIgnoreNextKey() {
  ignoreNextKey = false;
}

function dontAllowBackSpace() {
  allowBackSpace = false;
}

function kd(t,evt) {
  if (ignoreNextKey) { return true; }
  var kc = document.layers ? evt.which : document.all ? event.keyCode : document.getElementById ? evt.keyCode : 0;
  if (kc == 8) {
    allowBackSpace = true;
    setTimeout('dontAllowBackSpace()',10);
  }
  else if (kc == 39) {
    var cp = gCP(t);
    if (cp.end != t.value.length)
      return true;
    var el = t.parentNode.nextSibling;
    if (!el) el = t.parentNode.parentNode.firstChild;
    while (el) {
      if (el.tagName == 'TD') {
        var el2 = el.firstChild;
        if (el2 && el2.tagName == 'INPUT') {
          el2.focus();
          if (isSafari) { ignoreNextKey = true; setTimeout('dontIgnoreNextKey()',10); }
          return false;
        }
      }
      el = el.nextSibling;
      if (!el) el = t.parentNode.parentNode.firstChild;
    }
  }
  else if (kc == 40) {
    var elT = t.parentNode;
    var elA = elT.parentNode.firstChild;
    var cp = 0;
    while (elA && elA != elT) {
      if (elA.tagName == 'TD') {
        cp++;
      }
      elA = elA.nextSibling;
    }
    var elR = elT.parentNode.nextSibling;
    if (!elR) { elR = elT.parentNode.parentNode.firstChild; }
    while (elR) {
      if (elR.tagName == 'TR') {
        var elC = elR.firstChild;
				while (elC.tagName != 'TD') {
          elC = elC.nextSibling;
        }
        var i;
        for (i = 0; i < cp; i++) {
          elC = elC.nextSibling;
	  			while (elC.tagName != 'TD') {
            elC = elC.nextSibling;
          }
        }
        var el2 = elC.firstChild;
        if (el2 && el2.tagName == 'INPUT') {
          el2.focus();
          if (isSafari) { ignoreNextKey = true; setTimeout('dontIgnoreNextKey()',10); }
          return false;
        }
      }
      elR = elR.nextSibling;
      if (!elR) elR = elT.parentNode.parentNode.firstChild;
    }
  }
  else if (kc == 38) {
    var elT = t.parentNode;
    var elA = elT.parentNode.lastChild;
    var cp = 0;
    while (elA && elA != elT) {
      if (elA.tagName == 'TD') {
        cp++;
      }
      elA = elA.previousSibling;
    }
    var elR = elT.parentNode.previousSibling;
    if (!elR) { elR = elT.parentNode.parentNode.lastChild; }
    while (elR) {
      if (elR.tagName == 'TR') {
        var elC = elR.lastChild;
				while (elC.tagName != 'TD') {
          elC = elC.previousSibling;
        }
        var i;
        for (i = 0; i < cp; i++) {
          elC = elC.previousSibling;
	  			while (elC.tagName != 'TD') {
            elC = elC.previousSibling;
          }
        }
        var el2 = elC.lastChild;
        if (el2 && el2.tagName == 'INPUT') {
          el2.focus();
          if (isSafari) { ignoreNextKey = true; setTimeout('dontIgnoreNextKey()',10); }
          return false;
        }
      }
      elR = elR.previousSibling;
      if (!elR) elR = elT.parentNode.parentNode.lastChild;
    }
  }
  else if (kc == 37) {
    var cp = gCP(t);
    if (cp.start != 0)
      return true;
    var el = t.parentNode.previousSibling;
    if (!el) el = t.parentNode.parentNode.lastChild;
    while (el) {
      if (el.tagName == 'TD') {
        var el2 = el.firstChild;
        if (el2 && el2.tagName == 'INPUT') {
          el2.focus();
          if (isSafari) { ignoreNextKey = true; setTimeout('dontIgnoreNextKey()',10); }
          return false;
        }
      }
      el = el.previousSibling;
      if (!el) el = t.parentNode.parentNode.lastChild;
    }
  }
  return true;
}

function CP() {
  var start = null;
  var end = null;
}

function gCP(oField) {
  var oCaretPos = new CP();

  if (document.selection) {
    oField.focus();
    var oSel = document.selection.createRange();
    var selectionLength = oSel.text.length;
    oSel.moveStart ('character', -oField.value.length);
    oCaretPos.start = oSel.text.length - selectionLength;
    oCaretPos.end = oSel.text.length;
  }
  else if (oField.selectionStart || oField.selectionStart == '0') {
    oCaretPos.start = oField.selectionStart;
    oCaretPos.end = oField.selectionEnd;
  }

  return (oCaretPos);
}

function sCP(oField, iCaretStart, iCaretEnd) {
  if (document.selection) {
    oField.focus();
    var oSel = document.selection.createRange();
    oSel.moveStart ('character', -oField.value.length);
    oSel.moveEnd ('character', -oField.value.length);
    if (iCaretEnd != null)
      oSel.moveEnd ('character', iCaretEnd);
    else
      oSel.moveEnd ('character', iCaretStart);
    oSel.moveStart ('character', iCaretStart);
    oSel.select();
  }
  else if (oField.selectionStart || oField.selectionStart == '0') {
    oField.selectionStart = iCaretStart;
    if (iCaretEnd != null)
      oField.selectionEnd = iCaretEnd;
    else
      oField.selectionEnd = iCaretStart;
    oField.focus();
  }
}

window.status = '';

function dKD(evt) {
  if (window.event && window.event.keyCode == 8) {
    if (allowBackSpace) {
      allowBackSpace = false;
      return true;
    }
    if (window.event.srcElement && window.event.srcElement.type == 'input')
      return true;
    if (window.event.target && window.event.target.type == 'input')
      return true;
    window.event.cancelBubble = true;
    window.event.returnValue = false;
    return false;
  }
  return true;
}

document.onkeydown = dKD;

var domTT_styleClass = 'domTTOverlib';
var domTT_maxWidth = false;

var existingPopups = [];
var existingPopupsMO = [];

function ttP(src, event, total, length, leftMiddleRight, mo, row, col, dir) {
  var direction = 'north';
  if (leftMiddleRight < 0) direction = 'northwest';
  if (leftMiddleRight >= 0) direction = 'northeast';

  var digits = [];
  if (row || col) {
    if (dir == 1) { // below
      for (var i = 1; i <= length; i++) {
        var cellVal = eval('document.puzzle.cell_' + (row + i) + '_' + col + '.value');
        if (cellVal.length == 1 && cellVal >= '0' && cellVal <= '9')
          digits[digits.length] = cellVal;
      }
    }
    else { // right
      for (var i = 1; i <= length; i++) {
        var cellVal = eval('document.puzzle.cell_' + row + '_' + (col + i) + '.value');
        if (cellVal.length == 1 && cellVal >= '0' && cellVal <= '9')
          digits[digits.length] = cellVal;
      }
    }
  }

  var tooltipCaption = total + ' in ' + length;
  var tooltipContent = '<' + 'div align="center">';
  var splitAt = 0;
  for (i=0; i<pm[total][length].length; i++) {
    if (i != 0) tooltipContent += '; ';
    if (i > 0 && ((splitAt == 0 && ((i * length) % 12) < length) || (splitAt != 0 && (i % splitAt) == 0))) {
      if (splitAt == 0) splitAt = i;
      tooltipContent += '<' + 'br>';
    }
    var gray = 0;
    if (digits.length > 0) {
      for (var j=0; j<digits.length; j++) {
        if (pm[total][length][i].indexOf(digits[j]) == -1) {
          gray = 1;
          break;
        }
      }
    }
    if (gray)
      tooltipContent += '<' + 'span style="color:#aaa">(' + pm[total][length][i] + ')<'+'/span>';
    else
      tooltipContent += '(' + pm[total][length][i] + ')';
  }
  tooltipContent += '<' + '/div>';

  if (mo) {
    if (existingPopups[row + '_' + col + '_' + dir] && document.getElementById(existingPopups[row + '_' + col + '_' + dir]) != null) {
      if (document.getElementById(existingPopups[row + '_' + col + '_' + dir]).style.visibility == 'hidden') {
        domTT_close(existingPopups[row + '_' + col + '_' + dir]);
        var el = document.getElementById(existingPopups[row + '_' + col + '_' + dir]);
        if (el) {
          var tooltip = domTT_tooltips.get(existingPopups[row + '_' + col + '_' + dir]);
          if (tooltip) {
    				domTT_tooltips.remove(tooltip.get('owner').id);
    				domTT_tooltips.remove(tooltip.get('id'));
          }
          el.parentNode.removeChild(el);
        }
      }
    }

    if (!existingPopups[row + '_' + col + '_' + dir] || document.getElementById(existingPopups[row + '_' + col + '_' + dir]) == null) {
      var id = 'domTTmo_' + row + '_' + col + '_' + dir + '_' + Math.floor(Math.random()*100000);
      existingPopups[row + '_' + col + '_' + dir] = id;
      existingPopupsMO[row + '_' + col + '_' + dir] = true;
      domTT_activate(src, event, 'caption', tooltipCaption, 'content', tooltipContent, 'direction', direction, 'closeAction', 'destroy', 'id', id);
      setTimeout('clsTO("' + id + '")', 3500);
      return false;
    }
  }
  else {
    if (existingPopups[row + '_' + col + '_' + dir] && document.getElementById(existingPopups[row + '_' + col + '_' + dir]) != null) {
      if (existingPopupsMO[row + '_' + col + '_' + dir]) {
        domTT_close(existingPopups[row + '_' + col + '_' + dir]);
        var el = document.getElementById(existingPopups[row + '_' + col + '_' + dir]);
        if (el) {
          var tooltip = domTT_tooltips.get(existingPopups[row + '_' + col + '_' + dir]);
          if (tooltip) {
    				domTT_tooltips.remove(tooltip.get('owner').id);
    				domTT_tooltips.remove(tooltip.get('id'));
          }
          el.parentNode.removeChild(el);
        }
      }
    }

    if (!existingPopups[row + '_' + col + '_' + dir] || document.getElementById(existingPopups[row + '_' + col + '_' + dir]) == null) {
      var id = 'domTTmo_' + row + '_' + col + '_' + dir + '_' + Math.floor(Math.random()*100000);
      existingPopups[row + '_' + col + '_' + dir] = id;
      existingPopupsMO[row + '_' + col + '_' + dir] = false;
      return makeFalse(domTT_activate(src, event, 'caption', tooltipCaption, 'content', tooltipContent, 'type', 'sticky', 'closeLink', '[x]', 'draggable', true, 'direction', direction, 'closeAction', 'destroy', 'id', id));
    }
  }
}

function clsTO(id) {
  var el = document.getElementById(id);
  if (el)
    domTT_close(id);
}

/*
function clsTO(id) {
  var el = document.getElementById(id);
  if (el)
    el.parentNode.removeChild(el);
  clsTO(id);
}
*/

var pm = [[],["",["1"]],["",["2"]],["",["3"],["1,2"]],["",["4"],["1,3"]],["",["5"],["1,4","2,3"]],["",["6"],["1,5","2,4"],["1,2,3"]],["",["7"],["1,6","2,5","3,4"],["1,2,4"]],["",["8"],["1,7","2,6","3,5"],["1,2,5","1,3,4"]],["",["9"],["1,8","2,7","3,6","4,5"],["1,2,6","1,3,5","2,3,4"]],["","",["1,9","2,8","3,7","4,6"],["1,2,7","1,3,6","1,4,5","2,3,5"],["1,2,3,4"]],["","",["2,9","3,8","4,7","5,6"],["1,2,8","1,3,7","1,4,6","2,3,6","2,4,5"],["1,2,3,5"]],["","",["3,9","4,8","5,7"],["1,2,9","1,3,8","1,4,7","1,5,6","2,3,7","2,4,6","3,4,5"],["1,2,3,6","1,2,4,5"]],["","",["4,9","5,8","6,7"],["1,3,9","1,4,8","1,5,7","2,3,8","2,4,7","2,5,6","3,4,6"],["1,2,3,7","1,2,4,6","1,3,4,5"]],["","",["5,9","6,8"],["1,4,9","1,5,8","1,6,7","2,3,9","2,4,8","2,5,7","3,4,7","3,5,6"],["1,2,3,8","1,2,4,7","1,2,5,6","1,3,4,6","2,3,4,5"]],["","",["6,9","7,8"],["1,5,9","1,6,8","2,4,9","2,5,8","2,6,7","3,4,8","3,5,7","4,5,6"],["1,2,3,9","1,2,4,8","1,2,5,7","1,3,4,7","1,3,5,6","2,3,4,6"],["1,2,3,4,5"]],["","",["7,9"],["1,6,9","1,7,8","2,5,9","2,6,8","3,4,9","3,5,8","3,6,7","4,5,7"],["1,2,4,9","1,2,5,8","1,2,6,7","1,3,4,8","1,3,5,7","1,4,5,6","2,3,4,7","2,3,5,6"],["1,2,3,4,6"]],["","",["8,9"],["1,7,9","2,6,9","2,7,8","3,5,9","3,6,8","4,5,8","4,6,7"],["1,2,5,9","1,2,6,8","1,3,4,9","1,3,5,8","1,3,6,7","1,4,5,7","2,3,4,8","2,3,5,7","2,4,5,6"],["1,2,3,4,7","1,2,3,5,6"]],["","","",["1,8,9","2,7,9","3,6,9","3,7,8","4,5,9","4,6,8","5,6,7"],["1,2,6,9","1,2,7,8","1,3,5,9","1,3,6,8","1,4,5,8","1,4,6,7","2,3,4,9","2,3,5,8","2,3,6,7","2,4,5,7","3,4,5,6"],["1,2,3,4,8","1,2,3,5,7","1,2,4,5,6"]],["","","",["2,8,9","3,7,9","4,6,9","4,7,8","5,6,8"],["1,2,7,9","1,3,6,9","1,3,7,8","1,4,5,9","1,4,6,8","1,5,6,7","2,3,5,9","2,3,6,8","2,4,5,8","2,4,6,7","3,4,5,7"],["1,2,3,4,9","1,2,3,5,8","1,2,3,6,7","1,2,4,5,7","1,3,4,5,6"]],["","","",["3,8,9","4,7,9","5,6,9","5,7,8"],["1,2,8,9","1,3,7,9","1,4,6,9","1,4,7,8","1,5,6,8","2,3,6,9","2,3,7,8","2,4,5,9","2,4,6,8","2,5,6,7","3,4,5,8","3,4,6,7"],["1,2,3,5,9","1,2,3,6,8","1,2,4,5,8","1,2,4,6,7","1,3,4,5,7","2,3,4,5,6"]],["","","",["4,8,9","5,7,9","6,7,8"],["1,3,8,9","1,4,7,9","1,5,6,9","1,5,7,8","2,3,7,9","2,4,6,9","2,4,7,8","2,5,6,8","3,4,5,9","3,4,6,8","3,5,6,7"],["1,2,3,6,9","1,2,3,7,8","1,2,4,5,9","1,2,4,6,8","1,2,5,6,7","1,3,4,5,8","1,3,4,6,7","2,3,4,5,7"],["1,2,3,4,5,6"]],["","","",["5,8,9","6,7,9"],["1,4,8,9","1,5,7,9","1,6,7,8","2,3,8,9","2,4,7,9","2,5,6,9","2,5,7,8","3,4,6,9","3,4,7,8","3,5,6,8","4,5,6,7"],["1,2,3,7,9","1,2,4,6,9","1,2,4,7,8","1,2,5,6,8","1,3,4,5,9","1,3,4,6,8","1,3,5,6,7","2,3,4,5,8","2,3,4,6,7"],["1,2,3,4,5,7"]],["","","",["6,8,9"],["1,5,8,9","1,6,7,9","2,4,8,9","2,5,7,9","2,6,7,8","3,4,7,9","3,5,6,9","3,5,7,8","4,5,6,8"],["1,2,3,8,9","1,2,4,7,9","1,2,5,6,9","1,2,5,7,8","1,3,4,6,9","1,3,4,7,8","1,3,5,6,8","1,4,5,6,7","2,3,4,5,9","2,3,4,6,8","2,3,5,6,7"],["1,2,3,4,5,8","1,2,3,4,6,7"]],["","","",["7,8,9"],["1,6,8,9","2,5,8,9","2,6,7,9","3,4,8,9","3,5,7,9","3,6,7,8","4,5,6,9","4,5,7,8"],["1,2,4,8,9","1,2,5,7,9","1,2,6,7,8","1,3,4,7,9","1,3,5,6,9","1,3,5,7,8","1,4,5,6,8","2,3,4,6,9","2,3,4,7,8","2,3,5,6,8","2,4,5,6,7"],["1,2,3,4,5,9","1,2,3,4,6,8","1,2,3,5,6,7"]],["","","","",["1,7,8,9","2,6,8,9","3,5,8,9","3,6,7,9","4,5,7,9","4,6,7,8"],["1,2,5,8,9","1,2,6,7,9","1,3,4,8,9","1,3,5,7,9","1,3,6,7,8","1,4,5,6,9","1,4,5,7,8","2,3,4,7,9","2,3,5,6,9","2,3,5,7,8","2,4,5,6,8","3,4,5,6,7"],["1,2,3,4,6,9","1,2,3,4,7,8","1,2,3,5,6,8","1,2,4,5,6,7"]],["","","","",["2,7,8,9","3,6,8,9","4,5,8,9","4,6,7,9","5,6,7,8"],["1,2,6,8,9","1,3,5,8,9","1,3,6,7,9","1,4,5,7,9","1,4,6,7,8","2,3,4,8,9","2,3,5,7,9","2,3,6,7,8","2,4,5,6,9","2,4,5,7,8","3,4,5,6,8"],["1,2,3,4,7,9","1,2,3,5,6,9","1,2,3,5,7,8","1,2,4,5,6,8","1,3,4,5,6,7"]],["","","","",["3,7,8,9","4,6,8,9","5,6,7,9"],["1,2,7,8,9","1,3,6,8,9","1,4,5,8,9","1,4,6,7,9","1,5,6,7,8","2,3,5,8,9","2,3,6,7,9","2,4,5,7,9","2,4,6,7,8","3,4,5,6,9","3,4,5,7,8"],["1,2,3,4,8,9","1,2,3,5,7,9","1,2,3,6,7,8","1,2,4,5,6,9","1,2,4,5,7,8","1,3,4,5,6,8","2,3,4,5,6,7"]],["","","","",["4,7,8,9","5,6,8,9"],["1,3,7,8,9","1,4,6,8,9","1,5,6,7,9","2,3,6,8,9","2,4,5,8,9","2,4,6,7,9","2,5,6,7,8","3,4,5,7,9","3,4,6,7,8"],["1,2,3,5,8,9","1,2,3,6,7,9","1,2,4,5,7,9","1,2,4,6,7,8","1,3,4,5,6,9","1,3,4,5,7,8","2,3,4,5,6,8"],["1,2,3,4,5,6,7"]],["","","","",["5,7,8,9"],["1,4,7,8,9","1,5,6,8,9","2,3,7,8,9","2,4,6,8,9","2,5,6,7,9","3,4,5,8,9","3,4,6,7,9","3,5,6,7,8"],["1,2,3,6,8,9","1,2,4,5,8,9","1,2,4,6,7,9","1,2,5,6,7,8","1,3,4,5,7,9","1,3,4,6,7,8","2,3,4,5,6,9","2,3,4,5,7,8"],["1,2,3,4,5,6,8"]],["","","","",["6,7,8,9"],["1,5,7,8,9","2,4,7,8,9","2,5,6,8,9","3,4,6,8,9","3,5,6,7,9","4,5,6,7,8"],["1,2,3,7,8,9","1,2,4,6,8,9","1,2,5,6,7,9","1,3,4,5,8,9","1,3,4,6,7,9","1,3,5,6,7,8","2,3,4,5,7,9","2,3,4,6,7,8"],["1,2,3,4,5,6,9","1,2,3,4,5,7,8"]],["","","","","",["1,6,7,8,9","2,5,7,8,9","3,4,7,8,9","3,5,6,8,9","4,5,6,7,9"],["1,2,4,7,8,9","1,2,5,6,8,9","1,3,4,6,8,9","1,3,5,6,7,9","1,4,5,6,7,8","2,3,4,5,8,9","2,3,4,6,7,9","2,3,5,6,7,8"],["1,2,3,4,5,7,9","1,2,3,4,6,7,8"]],["","","","","",["2,6,7,8,9","3,5,7,8,9","4,5,6,8,9"],["1,2,5,7,8,9","1,3,4,7,8,9","1,3,5,6,8,9","1,4,5,6,7,9","2,3,4,6,8,9","2,3,5,6,7,9","2,4,5,6,7,8"],["1,2,3,4,5,8,9","1,2,3,4,6,7,9","1,2,3,5,6,7,8"]],["","","","","",["3,6,7,8,9","4,5,7,8,9"],["1,2,6,7,8,9","1,3,5,7,8,9","1,4,5,6,8,9","2,3,4,7,8,9","2,3,5,6,8,9","2,4,5,6,7,9","3,4,5,6,7,8"],["1,2,3,4,6,8,9","1,2,3,5,6,7,9","1,2,4,5,6,7,8"]],["","","","","",["4,6,7,8,9"],["1,3,6,7,8,9","1,4,5,7,8,9","2,3,5,7,8,9","2,4,5,6,8,9","3,4,5,6,7,9"],["1,2,3,4,7,8,9","1,2,3,5,6,8,9","1,2,4,5,6,7,9","1,3,4,5,6,7,8"]],["","","","","",["5,6,7,8,9"],["1,4,6,7,8,9","2,3,6,7,8,9","2,4,5,7,8,9","3,4,5,6,8,9"],["1,2,3,5,7,8,9","1,2,4,5,6,8,9","1,3,4,5,6,7,9","2,3,4,5,6,7,8"]],["","","","","","",["1,5,6,7,8,9","2,4,6,7,8,9","3,4,5,7,8,9"],["1,2,3,6,7,8,9","1,2,4,5,7,8,9","1,3,4,5,6,8,9","2,3,4,5,6,7,9"],["1,2,3,4,5,6,7,8"]],["","","","","","",["2,5,6,7,8,9","3,4,6,7,8,9"],["1,2,4,6,7,8,9","1,3,4,5,7,8,9","2,3,4,5,6,8,9"],["1,2,3,4,5,6,7,9"]],["","","","","","",["3,5,6,7,8,9"],["1,2,5,6,7,8,9","1,3,4,6,7,8,9","2,3,4,5,7,8,9"],["1,2,3,4,5,6,8,9"]],["","","","","","",["4,5,6,7,8,9"],["1,3,5,6,7,8,9","2,3,4,6,7,8,9"],["1,2,3,4,5,7,8,9"]],["","","","","","","",["1,4,5,6,7,8,9","2,3,5,6,7,8,9"],["1,2,3,4,6,7,8,9"]],["","","","","","","",["2,4,5,6,7,8,9"],["1,2,3,5,6,7,8,9"]],["","","","","","","",["3,4,5,6,7,8,9"],["1,2,4,5,6,7,8,9"]],["","","","","","","","",["1,3,4,5,6,7,8,9"]],["","","","","","","","",["2,3,4,5,6,7,8,9"]],["","","","","","","","","",["1,2,3,4,5,6,7,8,9"]]];
