/*
 * Copyright by Frieder Kesch
 * http://sudokucheater.ikesch.de
 * 
 * License:   GNU General Public License 2 (GPL 2) or later
 * 
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied
 * warrenty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.
 * See the GNU General Public License for more details.
 *
 */
var numRows = 8;
var numCols = 8;

function checkField(row, col,resore_color) {
	
	
	
	var value = document.getElementById('field_' + row + '_' + col).value;
	
	if (!isInt(value) || value.length > 1 || value < 1) {
		// reset field
		document.getElementById('field_' + row + '_' + col).value = '';
	}
	else {
		// select next field
		var val = document.getElementById('field_' + row + '_' + col).value;
		//setGem(val,row,col);
		nextRow = parseInt(row);
		nextCol = parseInt(col)+1;
		
		if (nextCol > numCols) {
			nextRow = parseInt(row)+1;
			nextCol = 0;
			if (nextRow > numRows) nextRow = 0;
		}
		//document.getElementById('field_' + nextRow + '_' + nextCol).focus();
	}
	document.getElementById('field_' + row + '_' + col).focus();
	
	value = document.getElementById('field_' + row + '_' + col).value;
	setGem(value,row,col,resore_color,true);
}

function isInt(x) {
	var y = parseInt(x);
	if (isNaN(y)) return false;
	return ((x == y) && (x.toString() == y.toString()));
}

function setGem(val,row,col,resore_color,f) {
	
	if (f == true) {
		document.getElementById('field_' + row + '_' + col).select();
	}
	
	var el_name = "image_cell_" + row + "_" + col;
	//alert(el_name);
	var el = document.getElementById(el_name);
	el.style.zIndex=100;
	
	if (val == "" || val == "0" || val == " ") {
		val = document.getElementById('field_' + row + '_' + col).value;
		
	}
	
	

	if (val == "" || val == "0" || val == " ") {
		el.style.background = resore_color;
		
	}
	
	if (val == 1 || val == "1") {
		el.style.background = resore_color + ' url(images/gem_1.png) no-repeat center ';
		
	}
	if (val == 2 || val == "2") {
		el.style.background = resore_color + ' url(images/gem_2.png) no-repeat center ';
		
	}
	if (val == 3 || val == "3") {
		el.style.background = resore_color + ' url(images/gem_3.png) no-repeat center ';
		
	}
	if (val == 4 || val == "4") {
		el.style.background = resore_color + ' url(images/gem_4.png) no-repeat center '  ;
		
	}
	if (val == 5 || val == "5") {
		el.style.background = resore_color + ' url(images/gem_5.png) no-repeat center ' ;
		
	}
	if (val == 6 || val == "6") {
		el.style.background = resore_color + ' url(images/gem_6.png) no-repeat center '  ;
		
	}
	if (val == 7 || val == "7") {
		el.style.background = resore_color + ' url(images/gem_7.png) no-repeat center ';
		
	}
	if (val == 8 || val == "8") {
		el.style.background = resore_color + ' url(images/gem_8.png) no-repeat center ';
		
	}
	if (val == 9 || val == "9") {
		el.style.background = resore_color + ' url(images/gem_9.png) no-repeat center ';
		
	}
	
}