/*
HTML Code, Javascript, CSS, Images, and Text Copyright (c) 2006-2008 SudokuConquest.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;
    }
  }
  //else
  //  alert('keyCode = ' + kc);
  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;
