//© dowedo.co.uk 2007
var i = 0;var direct=0;var vis;var posy = 1;var b = "b";var newPop;var theHit; var editWorkingNums = false;var highlightColour = "#DADADA";var backColour="#FFFFFF";
var workData = "";var thisWeek;var popInstruct = new Array();var solution = new Array(); var xlayout; var NS = document.layers;

function getObject(obj) {var theObj;
if (document.all) {if (typeof obj == "string") {return document.all(obj)} else {return obj.style}}
if (document.getElementById) {if (typeof obj == "string") {return document.getElementById(obj)} else {return obj.style}}return null}

function init(){
		okSign = getObject("ok").style;
		displaySign = getObject("checkDisplay").style;
		cont = getObject("control").style;

thisWeek = document.sudokudata.thssudokuid.value;
popInstruct = (document.sudokudata.xpopInstruct.value).split(",");
solution = (document.sudokudata.complete.value).split(",");
xlayout = parseInt(document.sudokudata.xtype.value);
posy = parseInt(document.sudokudata.cursorposition.value);
highlightBox(posy);
show(cont);
}

function mov(evt){
hide (displaySign);
evt = (evt) ? evt : event
if (navigator.appName == "Netscape"){
numCode = evt.which;
if (numCode > 57)numCode -= 48;
inputChar = String.fromCharCode(numCode)
if (checkNumerical(inputChar)){
fillBox(inputChar);
}
}
if (navigator.appName == "Microsoft Internet Explorer"){
numCode = event.keyCode;
if (numCode > 57)numCode -= 48;
inputChar = String.fromCharCode(numCode);

if (checkNumerical(inputChar)){
fillBox(inputChar);
}
}

if (navigator.appName == "Netscape"){
  if (evt.which == 32 || evt.which == 80) editWorking(true);
  if (evt.which == 38) chkLastImg(1);
   if (evt.which == 40) chkLastImg(2);
    if (evt.which == 37) chkLastImg(3);
     if (evt.which == 39) chkLastImg(4);
 	  if (evt.which == 8 ) fillBox(0);
	    if (evt.which == 46) fillBox(0);
	if (evt.which == 8) {
        evt.preventDefault();
    } 
  }

if (navigator.appName == "Microsoft Internet Explorer"){
  if (event.keyCode == 32 || event.keyCode == 80) editWorking(true);
  if (event.keyCode == 38) chkLastImg(1); 
   if (event.keyCode == 40) chkLastImg(2);
    if (event.keyCode == 37) chkLastImg(3);
     if (event.keyCode == 39) chkLastImg(4); 
	 if (event.keyCode == 8 ) fillBox(0);
	 if (event.keyCode == 46) fillBox(0); 
  if (event.keyCode == 8) {
        
        window.event.cancelBubble = true;
        window.event.returnValue = false;
        return false;
    }
}

}

function gridPosition(newPosition){
if (newPosition != posy){
clearBox(posy);
posy = newPosition;
highlightBox(posy);
}else{
editWorking(false);
}  	
}

function highlightBox(gridId){
	
	getObject("tbl"+(gridId)).style.background = highlightColour;
	getObject("wrk"+(gridId)).style.background = highlightColour;
	getObject("cur"+(gridId)).innerHTML = "<a href='javascript:editWorking(true)'><img src='http://www.dowedo.co.uk/sudoku/dosudoku/sudoku_data_files/images/pencil_cursor.gif' width='8' height='12' alt='Pencil in possibles for this box.' border='0'></a>";
			 
}
function clearBox(gridId){
	getObject("tbl"+(gridId)).style.background = backColour;
	getObject("wrk"+(gridId)).style.background = backColour;
	getObject("cur"+(gridId)).innerHTML = "";
			 
}
function checkBlack(thePos){
	if (popInstruct[thePos] == "b") return true;else return false;
}
  

function chkLastImg(direct){
	editWorking(false);
	clearBox(posy);
	udlr(direct);
	highlightBox(posy);
}

function udlr(direct){	   
switch (direct){
 case 1:
 	  posy -= xlayout;
	  if (posy < 0 ) udlr(2);
 break;
 case 2:
 	  posy += xlayout;
	  if (posy > ((xlayout * xlayout)-1)) udlr(1);
 break;
 case 3:
 	  posy -= 1;
	  if (posy < 0 ) udlr(4);
 break;
 case 4:
 	  posy += 1;
	  if (posy > ((xlayout * xlayout)-1)) udlr(3);
 break;	  
}


if (checkBlack(posy)) udlr(direct);
}

function fillBox(theHit){
var theLett = theHit;
var theBoxPos = posy;

	if (theHit == 0)theLett = "";
	if (editWorkingNums){
	   var workingOut = getObject("wrknums"+(theBoxPos)).innerHTML;
	    
	   if (theHit == 0){
	   	  getObject("wrknums"+(theBoxPos)).innerHTML = workingOut.substr(0,workingOut.length-1);
	   }else{
	   if (workingOut.length < 5){
	   	  getObject("wrknums"+(theBoxPos)).innerHTML += theLett;
		 
       }
	   }
	}else{
	getObject("box"+(theBoxPos)).innerHTML = theLett;
    getObject("box"+(theBoxPos)).style.color = "#7979FF";
	}
}


function populateTable(how) {
	
	var popList = how;
		
	for (i = 0; i < popList.length-1; i++){
		boxData = popList[i].split("_");
		getObject("wrknums"+(i)).innerHTML = boxData[0];
		getObject("box"+(i)).innerHTML = boxData[1];
		
		if (!checkBlack(i)){
		   getObject("box"+(i)).style.color = "#7979FF";
		}
	}		  
}

var clue;var ident;

var rd;
var isOut = 0;

function setForPrnt(){
print();
}

function rbc(buildType){
workData="";
var addComma = ".";
for (var s=0; s<=((xlayout*xlayout)-1); s++){

var fieldData = getObject("wrknums"+(s)).innerHTML + "_" + getObject("box"+(s)).innerHTML;

if (buildType == 1){
      if (s < ((xlayout*xlayout)-1)) addComma = ","; else addComma = "";
}
workData += (fieldData + addComma);
 
}

return workData;
}
function saveWork(out){
isOut = out
workdata = rbc(0);
var expiry = new Date();
expiry.setTime(expiry.getTime() + (13*24*60*60*1000));
document.cookie = thisWeek+"="+escape(workData)+";"
+"expires=" +expiry.toGMTString()+";";
if (isOut == 1)parent.window.close();
if (isOut == 0) show(okSign);var w=0;okShow();
}	

var w=0;
function okShow(){
w +=1
if (w < 5){
setTimeout("okShow()",400);
}else{
hide (okSign);
w=0
}
}
data = new Array();
function loadWork(){
var rePop="";
if (document.cookie){
var cookieData = unescape(document.cookie);
var crossData = cookieData.split(";");
for (c=0; c<crossData.length; c++){
var data = crossData[c].split("=");
if (data[0] == thisWeek || data[0] == " "+thisWeek){
rePop = data[1].split(".");

}
}
if (rePop >"") {
    populateTable(rePop);
 	
	}		
 }

}
function show(showobj) {
	showobj.visibility = "visible"
}
function hide(hideobj) {
	hideobj.visibility = "hidden"
}
function popHelp() {
if (newPop && newPop.open && !newPop.closed) newPop.close();
newPop = window.open("sudokuhelp.html","newPop","top=200,left=10,width=600,height=400,scrollbars,resizable");
}
function showAnswers() {
ask = confirm("Are you sure you want to see the complete solution?\r\n\r\n OK to continue. Cancel to abort this operation.");
	if (ask){
	  for (i = 0; i < solution.length; i++){
		getObject("box"+(i)).innerHTML = solution[i];
		if (!checkBlack(i)){
		   getObject("box"+(i)).style.color = "#7979FF";
		}
	  }		
	}
}
function showOneAnswer() {
	editWorkingNums = false;
	fillBox(solution[posy]);
}
function testAnswers(){
	var testSolution = "";
	for (var i=0; i<=((xlayout*xlayout)-1); i++){
		if (i < ((xlayout*xlayout)-1)) addComma = ","; else addComma = "";
		testSolution += getObject("box"+(i)).innerHTML + addComma;
	}
 	if  (testSolution == document.sudokudata.complete.value){
		getObject("checkDisplay").innerHTML = "<div class='check_sign_right'><p class='result_display'>Well Done!<br/><br/>You have completed the Sudoku.</p></div>";
		show (displaySign);
	}else{
		getObject("checkDisplay").innerHTML = "<div class='check_sign_wrong'><p class='result_display'>Sorry!<br/><br/>One or more of the numbers are incorrect.</p></div>";
		show (displaySign);
	
	} 
}
function rateSudoku(rateId) {
	document.sudokudata.userdata.value = rbc(1);
	document.sudokudata.action += "&rateid=" + rateId;
	document.sudokudata.submit();
}


function checkNumerical(thsField){
	var nums="123456789";
	if(nums.indexOf(thsField)==-1){
		return false;
	}
	return true;
} 
function editWorking(wchWay){
if (wchWay){
   getObject("wrk"+(posy)).style.background = backColour;
}else{
   getObject("wrk"+(posy)).style.background = highlightColour;
}   
editWorkingNums = wchWay; 
}

function removeWorking(){
	for (i = 0; i < popInstruct.length; i++){
		getObject("wrknums"+(i)).innerHTML = "";
	}		  	 
}

document.onkeydown = mov;
document.onmousedown = mov;


