addEvent(window, "load", crossword_init);




function crossword_init() {
	if (!document.getElementsByTagName) return;

	var inputs = document.getElementsByTagName('input');
	for (i=0; i< inputs.length; i++) {
		inputs[i].onfocus = function() {
			this.select();
			
			var alldivs = document.getElementsByTagName('div');	
			for (x = 0; x< alldivs.length; x++) {
				alldivs[x].className = alldivs[x].className.replace("top", "");
			} 


			thediv = this.parentNode.parentNode.parentNode.parentNode;
			thediv.className += " top";
			var otherinputs = thediv.getElementsByTagName('input');
			for (j=0; j< otherinputs.length; j++) {
				thisinput  = otherinputs[j];
				thisinput.className += " hilite";
			}
		}

		inputs[i].onblur = function() {
			thediv = this.parentNode.parentNode.parentNode.parentNode;
			thediv.className = thediv.className.replace(" top", "");
			var otherinputs = thediv.getElementsByTagName('input');
			for (j=0; j< otherinputs.length; j++) {
				thisinput  = otherinputs[j];
				thisinput.className = thisinput.className.replace("hilite", "");
			}
		}
		
		inputs[i].onkeyup = function(e) {

			var id =this.id;
			
			if (intersections[id]) {
				var intersect= document.getElementById(intersections[id]);
				
				if (intersect) {
					intersect.value = this.value;
				}
			}

		
			var code;
			if (!e) var e = window.event;
			if (e.keyCode) code = e.keyCode;
			else if (e.which) code = e.which;
			var character = String.fromCharCode(code);
			//alert('Character was ' + code);

			if (code == 9 || code == 16) return;
			
			
			if (code == 8) {
				var prev = this.parentNode.previousSibling;
				if (prev) {
					var prevfield = prev.childNodes[0];
					if (prevfield) {
						prevfield.focus();
					}
				}
			} else {
				var next = this.parentNode.nextSibling;
				if (next) {	
					var nextfield = next.childNodes[0];
					if (nextfield) {
						nextfield.focus();
					}
				}
			}

		}
	
	}
}

function addEvent(elm, evType, fn, useCapture)
// addEvent and removeEvent
// cross-browser event handling for IE5+,  NS6 and Mozilla
// By Scott Andrew
{
  if (elm.addEventListener){
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent){
    var r = elm.attachEvent("on"+evType, fn);
    return r;
  }
} 