function check(i, j, checkval) {
	checkval = document.getElementById('f'+i+'_'+j).value;
	if(checkval == 0 || checkval == '') {
		return true;
	}
	if(isNaN(checkval)) {
		return 2;
	}
	lx = Math.floor(i /3)*3;
	ly = Math.floor(j /3)*3;
	for(x = lx; x < lx+3; x++) {
		for(y = ly; y < ly+3; y++) {
			if(x != i || y != j) {
				if(document.getElementById('f'+x+'_'+y).value == checkval) {
					return false;
				}
			}
		}
	}
	for(x=0;x<9;x++) {
		if(x != i) {
			if(document.getElementById('f'+x+'_'+j).value == checkval) {
				return false;
			}
		}
	}
	for(y=0;y<9;y++) {
		if(y != j) {
			if (document.getElementById('f'+i+'_'+y).value == checkval) {
				return false;
			}
		}
	}
	return true;
}

var prevstate='';

function input(event) {
	a=event.target.id.split('_');
	i=a[0][1];
	j=a[1];
	if(!(r = check(i, j))) {
		event.target.style.backgroundColor="#faa";
	} else if(
		(
			(event.keyCode > 47 && event.keyCode < 58)
			|| (event.keyCode > 95 && event.keyCode < 106)
		) && !isNaN(event.target.value)
	) {
		
		event.target.style.backgroundColor=event.target.parentNode.style.backgroundColor;
//		i++;
//		if(i>8) {
//			i=0;
//			j++;
//			if(j>8) {
//				return;
//			}
//		}
//		document.getElementById(i+'_'+j).select();
	} else if (event.keyCode != 9) {
		event.target.style.backgroundColor="#faa";
	}
}
var fields= new Array(
	new Array(9), new Array(9), new Array(9), new Array(9), new Array(9),
	new Array(9), new Array(9), new Array(9), new Array(9)
);
function init() {
	for(i=0; i<9;i++) {
		for(j=0; j<9; j++) {
			fields[i][j]=document.getElementById('f'+i+'_'+j);
			fields[i][j].addEventListener("keyup", input, false);
		}
	}
	inputs = document.getElementsByTagName('input');
	for(i=0; i < inputs.length; i++) {
		if( inputs[i].className.match(/hidden/) ) {
			inputs[i].addEventListener("click", open, false);
		}
	}
}

function solveall() {
	for(i=0; i < inputs.length; i++) {
		if( inputs[i].className.match(/hidden/) ) {
			inputs[i].className='open';
		}
	}
}

function open() {
	this.className = 'open';
}


var field = new Array();
var x, y;

function write_field() {
	for(i=0; i<9; i++) {
		for(j=0; j<9; j++) {
			document.getElementById('f'+i+'_'+j).value = field[i][j];
		}
	}
}

function get_next_empty(xx, yy) {
	while(1) {
		x++;
		if(x > 8) {
			x = 0;
			y++;
			if(y > 8) {
				y = 0;
			}
		}
		if(!field[x][y]) return 0;
		if ((xx == x) && (yy == y)) return 1;
	}
}

function backtrace(tx, ty) {
	var i, xx, yy;
	xx = x = tx;
	yy = y = ty;
	if(get_next_empty(x, y)) {
		done = 1;	
	} else { 
		for (i = 1; i < 10; i++) {
			if (allowed(xx, yy, i)) {
				field[xx][yy]=i;
				backtrace(x, y);
			}
			if (done) break;
		}
	}
	if (!done) {
		field[xx][yy] = 0;
	}
	
}

function allowed(i, j, checkval) {
	lk = Math.floor(i /3)*3;
	lm = Math.floor(j /3)*3;
	for(k = lk; k < lk+3; k++) {
		for(m = lm; m < lm+3; m++) {
			if(k != i || m != j) {
				if(field[k][m] == checkval) {
					return false;
				}
			}
		}
	}
	for(k=0;k<9;k++) {
		if(k != i) {
			if(field[k][j] == checkval) {
				return false;
			}
		}
	}
	for(m=0;m<9;m++) {
		if(m != j) {
			if(field[i][m] == checkval) {
				return false;
			}
		}
	}
	return true;
}


