function check_input_solver(cell_no)
{
	var cell_name='cell'+cell_no;
	var cell= document.getElementById(cell_name);
	var v= cell.value;
	if (v=='') return true;
	
	//debugger;
	game = get_CurrentGame();
	
	if (v.length > 1) 
	{
		cell.style.fontSize='8pt';
		if (cell.style.color != 'black') cell.style.color = 'black';
		return true;
	}
	
	cell.style.fontSize='12pt';
	game = game.substr(0, cell_no) + v + game.substr(cell_no+1, 81);
	//undo_stack.push(cell_no);
	
	digits=/[123456789]/;
	if( (v.length > 0) && (!digits.test(v)))
	{
		cell.style.color = 'red';
		cell.style.fontSize ='12pt';
		document.getElementById('TopMessage').innerHTML="invalid entry";
		cell.value = "";
		return false;	
	}
	
	var row = row_num(cell_no);
	for (var i = row*9; i < (row+1)*9; i++)
	{
		if (( i != cell_no ) && (game.charAt(i) == v))
		{ 
			// found a duplicate in a row
			cell.style.color = 'red';
			cell.style.fontSize ='12pt';
			document.getElementById('TopMessage').innerHTML = "duped entry in row";
			return false;
		} 
	}

	var col = col_num(cell_no);
	for (var i = 0; i < 9; i++)
	{
		if (((i*9+col) != cell_no) && (game.charAt(i*9+col) == v))
		{
			cell.style.color = 'red';
			cell.style.fontSize ='12pt';
			document.getElementById('TopMessage').innerHTML="duped entry in column";
			return false;
		}
	} 

//	debugger;
	var startBlock = start_block_num(cell_no);

	for (var i = startBlock; i < (startBlock + 3 + 2*9); i += 9) 
	{
		for (var j = 0; j < 3; j++)
		{
			if (((i+j) != cell_no) && (v == game.charAt(i+j)))
			{
				cell.style.color = 'red';
				cell.style.fontSize ='12pt';
				document.getElementById('TopMessage').innerHTML = "duped entry in a block";
				return false;
			}
		}
	}
		
	if(cell.style.color != 'black') cell.style.color = 'black';
	document.getElementById('TopMessage').innerHTML="" ;
	cell.style.fontSize ='12pt';		 	
	return true;
}

function row_num(c)
{
   return (c - (c % 9)) / 9;
}

function col_num(c)
{   
	return c % 9;
}

function start_block_num(c)
{
	//debugger;
	return ((Math.floor(c / 27) * 27) + (Math.floor(col_num(c) / 3) * 3));
}

function f_start_over()
{
	document.getElementById('TopMessage').innerHTML="";
	document.frm.reset();
}

function f_print()
{
    window.open('print.php?game_id='+game_id.toString()+'&level='+document.getElementById('level').value.toString(), 'Print');
}

function f_solve_puzzle()
{
	//debugger;
	btSolve = document.getElementById('btSolve');

	if	(document.getElementById('btSolve').value == "Click to Solve")
	{
		btSolve.value = "Click to Pause";

		initial_game = get_CurrentGame();
		cell_counter = 0;
		
		game_arr = new Array(81);
		
		for (i = 0; i < 81; i++)
		{
			var v = document.getElementById('cell'+i).value;
			if (v.length == 1) 
				game_arr[i] =  v;
			else
				game_arr[i] =  0;
		}
		
		direction = 1;

		f_solve();	
	}
	else 
	if	(document.getElementById('btSolve').value == "Click to Pause")
	{
		btSolve.value = "Click to Resume";
	} 
	else
	{
		btSolve.value = "Click to Pause";
	} 
}

function f_solve()
{
	if	(document.getElementById('btSolve').value == "Click to Solve")
		return;
		
	if	(document.getElementById('btSolve').value == "Click to Resume")
	{
		 window.setTimeout(f_solve, 1);
		 return;
	}
		
	while ((cell_counter >= 0) && (cell_counter <81) && (initial_game.charAt(cell_counter) != '0') )
	{
		cell_counter += direction;
	}
	
	if (cell_counter < 0) 
	{
		alert('This Sudoku puzzle cannot be solved');
		document.getElementById('btSolve').value = "Click to Solve";
		return false;
	}
	else if (cell_counter >= 81) 
	{
		alert('This Sudoku puzzle has been successfully solved');
		document.getElementById('btSolve').value = "Click to Solve";
		return true;
	}

	while (game_arr[cell_counter] < 10)
	{
		game_arr[cell_counter] += 1;
		if (game_arr[cell_counter] == 10)
		{
			game_arr[cell_counter] = 0;
			document.getElementById('cell' + cell_counter).value='';
			direction = -1; // backtracking
			break;
		}

		document.getElementById('cell' + cell_counter).value = game_arr[cell_counter];//shown puzzle
		
		//check_valid sudoku
		isValid = true;
		
		var row = row_num(cell_counter);
		for (i = row*9; i < (row+1)*9; i++)
		{
			if ((i != cell_counter ) && (game_arr[cell_counter] == game_arr[i]))
			{ 
				// found a duplicate in a row
				isValid = false;
				break;
			} 
			
		}
		
		if (!isValid)
			continue;
			
		var col = col_num(cell_counter);
		for (var i = 0; i < 9; i++)
		{
			if (((i*9 + col) != cell_counter) && (game_arr[cell_counter] == game_arr[i*9+col])) 
			{
				// found a duplicate in a col
				isValid = false;
				break;
			}
		} 
	
		if (!isValid)
			continue;
			
		var startBlock = start_block_num(cell_counter);
	
		for (var i = startBlock; (i < (startBlock + 3 + 2*9)) && isValid; i += 9) 
		{
			for (var j = 0; j < 3; j++)
			{
				if (((i+j) != cell_counter) && (game_arr[cell_counter] == game_arr[i+j]))
				{
					// found a duplicate in a block
					isValid = false;
					break;
				}
			}
		}
		
		if (isValid)
		{
			direction = 1;
			break;
		}
	}
	
	cell_counter += direction;
	if ((cell_counter >= 0) || (cell_counter < 81)) window.setTimeout(f_solve, 1);
}

function f_new_game()
{
	document.frm.submit();
}

function get_CurrentGame()
{
	lgame = "";
	for (i = 0; i < 81; i++)
	{
		var v = document.getElementById('cell'+i).value;
		if (v.length == 1) 
			lgame = lgame + v;
		else
			lgame = lgame + "0";
	}
	
	return lgame;
}

function f_blank_puzzle()
{
	document.getElementById('TopMessage').innerHTML="";
	document.getElementById('btSolve').value = "Click to Solve";
	
	for (i = 0; i < 81; i++)
	{
		document.getElementById('cell'+i).value = '';
	}
}
