var isloggedon = 0;

var inputlettersol = "black";
var countup = 0;
function placevalues() {

var allInputs = document.getElementsByTagName("input");
for (var i =0; i < 9; i++) {
	for (var j =0; j <9; j++) {
if (allInputs[countup].type=="text") {
if (spos[i][j]) {
allInputs[countup].value = spos[i][j];
allInputs[countup].readOnly = true;
}
countup = countup + 1;
}
}
}

countup = 0; // reset value incase resetgrid called
}
function isNumber(sendno,row,col) {
var str = sendno;
if (!str || str == 0) {
document.getElementById("cell"+row+col).value = '';
alert ("Only numbers 1-9 allowed");
return false;
}
var re = /\d/;
if (!str.match(re)) {
document.getElementById("cell"+row+col).value = '';
alert ("Only numbers 1-9 allowed");
return false;
}
}
function resetgrid() {
var allInputs = document.getElementsByTagName("input");
for (var i =0; i < allInputs.length; i++) {
if (allInputs[i].type=="text") {
allInputs[i].value = "";
}
}
placevalues();
}

function showsolution() {
var allInputs = document.getElementsByTagName("input");
for (var i =0; i < 9; i++) {
	for (var j =0; j < 9; j++) {
if (allInputs[countup].type=="text") {
allInputs[countup].style.color = inputlettersol;
allInputs[countup].value = cells[i][j];
allInputs[countup].style.fontSize = "14pt";
}
countup = countup + 1;
}
}
countup = 0;
}
var wronganswer = 0;
var thiscell = 0;
function checksolution() {

var allInputs = document.getElementsByTagName("input");
for (var i =0; i < 9; i++) {
	for (var j = 0; j < 9; j++) {
if (allInputs[thiscell].type=="text") {
if (allInputs[thiscell].value == cells[i][j]) {
allInputs[thiscell].style.color = "black";
}
else {
wronganswer += 1;
}
}
thiscell = thiscell + 1;
}
}
if (wronganswer !=0) {
alert ("Oops, you didn't get it completely right.  " + wronganswer + "  cells do not contain the correct value - if you wish to find out your mistakes, click \'show wrong moves\'. \n\n");
wronganswer = 0;
}
else {
alert ("Congratulations, you got the answer right!");
}
thiscell = 0;
}

var font, fcolor, setcheckval;
function showmistakes(thisrow,thiscol) {
if (thisrow == "9") {
}

else {
var thisref = document.getElementById("cell"+thisrow+thiscol);
//alert (thisref.value.length);
if (thisref.value.length > 1)  {
// update font size to smaller
thisref.style.fontSize = "10pt";
} // greater than 1
else {
thisref.style.fontSize = "14pt";
}
}
var allInputs = document.getElementsByTagName("input");
if (document.sudokucheck.iswrong.checked) {
font = 'italic';
setcheckval = 1;

}
else {
font = 'normal';
setcheckval = 0;
}
var usehere = -1;
for (var i =0; i < 9; i++) {
for (var j = 0; j < 9; j++) {
usehere = usehere + 1;
if (allInputs[usehere].type=="text") {
if (allInputs[usehere].value != "") {
if (allInputs[usehere].value == cells[i][j]) {
allInputs[usehere].style.fontStyle = "normal";
allInputs[usehere].style.color = "black";
}
else {
allInputs[usehere].style.fontStyle = font;
if (setcheckval) {
allInputs[usehere].style.color = "red";
}
else {
allInputs[usehere].style.fontStyle = "normal";
allInputs[usehere].style.color = "black";
						}
					}
				}

			}

		}
	}
usehere = 0;
}

function undomoves() {
var lastmove = StoreNumbers.splice((StoreNumbers.length-1), 1);
if (lastmove.length) {
var lastmovestr = lastmove.join("");
var lastvalArray = lastmovestr.split("");
if (lastvalArray.length == 5) {
var findfirst = lastvalArray[2];
var findsecond = lastvalArray[4];
var checkre = "/" + (findfirst) + ","+(findsecond) + "$/";
checkre = eval(checkre);
for (var q = StoreNumbers.length - 1; q>=0; q--) {
if (StoreNumbers[q].match(checkre)) {
var foundmatch = 1;
var workoutstr = StoreNumbers[q];
var replacewiththis = workoutstr.substring(0, 1);
if (replacewiththis == ",") {
var updatethiscell = document.getElementById("cell"+findfirst+findsecond);
updatethiscell.value = '';
}
else {
var updatethiscell = document.getElementById("cell"+findfirst+findsecond);
updatethiscell.value = replacewiththis;
}
break
}
}
if (!foundmatch) {
var updatethiscell = document.getElementById("cell"+findfirst+findsecond);
updatethiscell.value = '';
}
}
else {
var findfirst = lastvalArray[1];
var findsecond = lastvalArray[3];
var checkre = "/" + (findfirst) + ","+(findsecond) + "$/";
checkre = eval(checkre);
for (var q = StoreNumbers.length - 1; q>=0; q--) {
if (StoreNumbers[q].match(checkre)) {
var foundmatch = 1;
var workoutstr = StoreNumbers[q];
var replacewiththis = workoutstr.substring(0, 1);
if (replacewiththis == ",") {
var updatethiscell = document.getElementById("cell"+findfirst+findsecond);
updatethiscell.value = '';
}
else {
var updatethiscell = document.getElementById("cell"+findfirst+findsecond);
updatethiscell.value = replacewiththis;
}
break
}
}
if (!foundmatch) {
var updatethiscell = document.getElementById("cell"+findfirst+findsecond);
updatethiscell.value = '';
}
}
}
}

var StoreNumbers = new Array();
function helpcell(row, col) {
if (document.hintson.showhint.checked) {
var noscontainer = '0';
var i = row;
var j = col;
var allInputs = document.getElementsByTagName("input");
for (k=0; k<9; k++) {
var checkaval = (i*9)+k;
	if (allInputs[checkaval].value) {
	var tempy = allInputs[checkaval].value;
	if (tempy.length < 2) {
	noscontainer +=  allInputs[checkaval].value;
	}
}
	}

for (l=0; l<9; l++) {
var combined = ''
combined += l;
combined += j;
var checkinthisone = (l*9)+j;
if (allInputs[checkinthisone].value) {
noscontainer +=  allInputs[checkinthisone].value;
}
}
if (i == 0 || i == 1 || i == 2) { var startfromx = 0; }
if (i == 3 || i == 4 || i == 5) { var startfromx = 3; }
if (i == 6 || i == 7 || i == 8) { var startfromx = 6; }
if (j == 0 || j == 1 || j == 2) { var startfromy = 0; }
if (j == 3 || j == 4 || j == 5) { var startfromy = 3; }
if (j == 6 || j == 7 || j == 8) { var startfromy = 6; }
for (x = 0; x<3; x++) {
	for (y=0; y<3; y++) {
	var idcell = ((startfromx+x)*9)+(startfromy+y);
		if (allInputs[idcell].value) {
		var tempx = allInputs[idcell].value;
		if (tempx.length < 2) {
		noscontainer +=  allInputs[idcell].value;
		}
		}
	}
	
}
var updatehelp = document.getElementById("hello");
var finalist='';
for (var n = 1; n < 10; n++) {
if (noscontainer.indexOf(n) == -1) {
finalist = finalist + n + ", ";
}
}
if (finalist) {
finalist = finalist.slice(0, -2);
updatehelp.innerHTML = '<b>This cell can contain the following numbers: '+finalist + '</b>';
}
else {
updatehelp.innerHTML = 'All numbers have been assigned. Remember you can click Show Wrong Moves to check your choices!';
}
}
}
function togglehints() {
if (document.hintson.showhint.checked) {
document.getElementById('hello').style.visibility='visible';
}
else {
document.getElementById('hello').style.visibility='hidden';
}
}
var storepastmoves;
// use this function to de-bug undo
function showmoves() {
for (var i =0; i < StoreNumbers.length; i++) {
storepastmoves = storepastmoves + StoreNumbers[i];
storepastmoves += "\n";
}
alert ("Past moves" + storepastmoves);
}

var timerID = 0;
var tStart  = null;
function UpdateTimer() {
   if(timerID) {
      clearTimeout(timerID);
      clockID  = 0;
   }
   if(!tStart)
      tStart   = new Date();

   var   tDate = new Date();
   var   tDiff = tDate.getTime() - tStart.getTime();
   tDate.setTime(tDiff);
var minutespassed = tDate.getMinutes();
var secondspassed = tDate.getSeconds();

   document.optionToTime.showthisTime.value = ""
                                   + minutespassed + ":"
                                   + secondspassed;
  timerID = setTimeout("UpdateTimer()", 1000);
}

function Start() {
   tStart   = new Date();
 document.optionToTime.showthisTime.value = "00:00";
timerID  = setTimeout("UpdateTimer()", 1000);
}

function Stop() {
   if(timerID) {
      clearTimeout(timerID);
      timerID  = 0;
   }

   tStart = null;
}

function Reset() {
   tStart = null;
   document.optionToTime.showthisTime.value = "00:00";
}

var storesolution = '';
var trackcell = 0;
function savegame() {
if (!isloggedon) {
alert ("You need to be logged in to save your game - please log on or create a free account");
return false;
}
else {
// construct the argument to pass
var allInputs = document.getElementsByTagName("input");
for (var i =0; i < 9; i++) {
	for (var j = 0; j < 9; j++) {
if (allInputs[trackcell].value) {
storesolution+= allInputs[trackcell].value;
   }
   else {
   storesolution += 0;
   }

    trackcell = trackcell + 1;
   }

 }

 var g = 1; // puzzle 1
 var u = isloggedon; // user
 var s = storesolution; //
var sendthis = "savegame.php?s=" + escape(s) + "&g=" + escape(g) + "&u=" + escape(u);
var saveWindow = window.open(sendthis, "savewindow", "status,menubar,height=250,width=250");
trackcell = 0;
storesolution = '';
s = '';
}
}
function savelastmove(thisrow,thiscol) {
var sendno = document.getElementById("cell"+thisrow+thiscol).value;
if (sendno.length) {
isNumber (sendno,thisrow,thiscol);
}
if (sendno.length > 1) {

}
else {
var storethis = sendno+","+thisrow+","+thiscol;
StoreNumbers[StoreNumbers.length] = storethis;
}
}