function InitSudoku()
{
    gameFinished = false;
    
	if(document.all)
	{
		document.body.onkeydown = insertNumber;
	}
	else
	{
		document.documentElement.onkeydown = insertNumber;
	}         
}

function insertNumber(e)
{
    if(document.all)e = event;
    
    if(!higlightedCell)return;
    if(gameFinished)return;
    
    if (e.keyCode) code = e.keyCode; else if (e.which) code = e.which;
    var span = higlightedCell.getElementsByTagName('SPAN')[0];
    
    var numbers = higlightedCell.id.split('_');

    var row = numbers[1]/1;
    var col = numbers[2]/1;
    var nextObject = false;
        
    if(code==39)
    {
	    if(col<8)
	    {
		    nextObject = document.getElementById('square_' + row + '_' + (col/1+1));
		    if(nextObject.style.backgroundColor=='#ddd')
		    {
			    while(col<8 && nextObject.style.backgroundColor=='#ddd')
			    {
				    col = col+1;
				    nextObject = document.getElementById('square_' + row + '_' + col);
			    }
		    }				
	    }
    }

    if(code==37)
    { 
	    if(col>0)
	    {
		    nextObject = document.getElementById('square_' + row + '_' + (col/1-1));
		    if(nextObject.style.backgroundColor=='#ddd')
		    {
			    while(col>0 && nextObject.style.backgroundColor=='#ddd')
			    {
				    col = col-1;
				    nextObject = document.getElementById('square_' + row + '_' + col);
			    }
		    }
		    if(nextObject.style.backgroundColor=='#ddd')nextObject = false;
	    }
    }
    if(code==38)
    {
	    if(row>0)
	    {
		    nextObject = document.getElementById('square_' + (row-1) + '_' + col);
		    if(nextObject.style.backgroundColor=='#ddd')
		    {
			    while(row>0 && nextObject.style.backgroundColor=='#ddd')
			    {
				    row = row-1;
				    nextObject = document.getElementById('square_' + row + '_' + col);
			    }
		    }				
	    }
    }		
    if(code==40)
    {
	    if(row<8)
	    {
		    nextObject = document.getElementById('square_' + (row+1) + '_' + col);
		    if(nextObject.style.backgroundColor=='#ddd')
		    {
			    while(row<8 && nextObject.style.backgroundColor=='#ddd')
			    {
				    row = row+1;
				    nextObject = document.getElementById('square_' + row + '_' + col);
			    }
		    }	
	    }
    }
    
    if(nextObject)
    {
	    highlightSquare(nextObject);
    }
    
    if(code==46 || code==8)
    {
		if(span.innerHTML.toUpperCase() != '<BR>' && span.innerHTML.length>1) span.innerHTML = span.innerHTML.substr(0, span.innerHTML.length-2);
		else span.innerHTML = '<br>';
		
		if(span.innerHTML == '<BR>' || span.innerHTML.length==1) span.style.fontSize = "30px";
		
	    if(code==8) return false;
    }
    
    if(code>96 && code<=105)code-=48;
    
    if(code>48 && code<=57)
    {				
	    var theChar = String.fromCharCode(code);
	    
	    if(span.innerHTML.toUpperCase()!='<BR>') span.innerHTML = span.innerHTML +  " " + theChar;
	    else span.innerHTML = theChar;
	    
	    if(span.innerHTML.length > 1) span.style.fontSize = "12px";
	    else span.style.fontSize = "30px";
    }		
}

var higlightedCell;

function highlightSquare(inputObj)
{	        
	if(!inputObj)
	{
	    inputObj = this;	
	}
	    
	if(inputObj.style.backgroundColor=='#ddd')
	{
	    return;
	}
	
	inputObj.style.backgroundColor='#e2ebed';
	
	if(higlightedCell && higlightedCell!=inputObj) higlightedCell.style.backgroundColor='White';

	higlightedCell = inputObj;
	
	if(document.all)
	{
	    inputObj.focus();
	}
}  

function btnHint_Click()
{
    for(var no=0;no<9;no++)
    {		
		for(var no2=0;no2<9;no2++)
		{				
			var obj = document.getElementById('square_' + no + '_' + no2);
			var spanObjects = obj.getElementsByTagName('SPAN');
	        
			var span = spanObjects[1];		
	        
	        if(spanObjects[0].innerHTML.toUpperCase()=='<BR>')
	        {   		
				spanObjects[0].innerHTML = span.innerHTML;
				return;
			}
		}
    }
}

function btnRestart_Click()
{  
	InitSudoku(puzzle, full);
}

function btnDone_Click()
{ 
	var i = 0;
    for(var no=0;no<9;no++)
    {
        for(var no2=0;no2<9;no2++)
        {				
	        var obj = document.getElementById('square_' + no + '_' + no2);
	        var spanObjects = obj.getElementsByTagName('SPAN');
        	
	        var span = spanObjects[0];		
        	
        	if(spanObjects[1].innerHTML != span.innerHTML)
        	{
        		i++;
        	}
        }			
    } 
    
    if(i==0)
		alert("Congratulations!");
	else
		alert("Sorry, you have " + i + " location(s) wrong");
}

