var celln=new Array();
function initarray()
{
var sRet;
var iC;
var iX;
var iY;

InitBoard();

do {
	CheckPoss();
	sRet=MoveSoleCandidate();

	if (sRet=='')
		sRet=MoveUniqueCandidateRow();

	if (sRet=='')
		sRet=MoveUniqueCandidateCol();

	if (sRet=='')
		sRet=MoveUniqueCandidateBlock();

	} while (sRet!='')

for (iC=0;iC<81;iC++)
	{
	iX = iC % 9;
	iY = Math.floor(iC/9);
	if (SolverBoard[iX][iY][0] !=0 )
		celln[iC]=parseInt(SolverBoard[iX][iY][0]);
	else
		celln[iC]=0;
	}

}

function showarray()
{
  for (i=0; i<81; i++) {
    document.forms["puzzle"]["n"+i].value=celln[i];
  }
}
// check if num can be entered at pos legally, 1=yes (no conflict), 0=no (conflict)
function check(num,pos)
{
  row=Math.floor(pos/9);
  for (i=0; i<9; i++) {
    if (num==celln[row*9+i]) {
      return(0);
    }
  }
  row=Math.floor(row/3)*3;
  col=pos%9;
  col=Math.floor(col/3)*3;
  for (r=0; r<3; r++) {
    for (c=0; c<3; c++) {
      if (num==celln[(row+r)*9+col+c]) {
        return(0);
      }
    }
  }
  col=pos%9;
  for (i=0; i<9; i++) {
    if (num==celln[col+i*9]) {
      return(0)
    }
  }
  return(1);
}
function solve(pos)
{
  var num;
  if (celln[pos]==0) {
    for (num=1; num<10; num++) {
      if (check(num,pos)) {
        celln[pos]=num;
        if (pos==80) {
          return(1);
        }
        if (solve(pos+1)) {
          return(1);
        }
        celln[pos]=0;
      }
    }
    return(0);
  } else {
    if (pos==80) {
      return(1);
    }
    return(solve(pos+1));
  }
}


function LockNumbers()
{
var iC;
var iLockedNum;
var oControl;

initarray();

alert('I will now try to solve this sudoku, which could take a little while. Please be patient.');

if (solve(0))
	{
	for (iC=0;iC<iGridUnsolved.length;iC++)
		{
		iGridSolved[iC] = celln[iC];
		iLockedNum = document.getElementById('T' + iC).innerHTML.toUpperCase();
		if (iLockedNum!='' && iLockedNum!='&NBSP;')
			iGridUnsolved[iC] = parseInt(iLockedNum);
		else
			iGridUnsolved[iC] = 0;
		}

for (iC=0;iC<iGridUnsolved.length;iC++)
	{
 	oControl=document.getElementById('T' + iC);
	if (iGridUnsolved[iC]!=0)
		{ oControl.innerHTML ='<b>'+ iGridUnsolved[iC] + '</b>';
			oControl.bgColor = sBGColor; }
	}

	document.getElementById('btnLockNumbers').style.display = 'none';
	document.getElementById('btnSaveGame').style.display = 'none';
	document.getElementById('btnLoadGame').style.display = 'none';

	oCurrentPlayingSquare=undefined;

	alert("Let's play Sudoku!");
  	}
else
    	alert("I can't find a solution for this puzzle. Check that it has been entered correctly.");
}