var countup = 0;
function placevalues() {
var allInputs = document.getElementsByTagName("input");
for (var i =0; i < allInputs.length; i++) {
if (allInputs[i].type=="text") {
if (spos[i]) {
allInputs[i].value = spos[i];
// COLOURS DO
if (spos[i] == 1) {
var cellfill = "#ff6";

} // if 1
if (spos[i] == 2) {
var cellfill = "#9f3";
} // if 2
if (spos[i] == 3) {
var cellfill = "#fcc";
} // if 3
if (spos[i] == 4) {
var cellfill = "#f93";

} // if 4
if (spos[i] == 5) {
var cellfill = "#9ff";
} // if 5
if (spos[i] == 6) {
var cellfill = "#ff0";
} // if 6
if (spos[i] == 7) {
var cellfill = "#c9f";
} // if 7
if (spos[i] == 8) {
var cellfill = "#ccf";
} // if 8
if (spos[i] == 9) {
var cellfill = "#3f9";
} // if 9


if (cellfill) {
allInputs[i].style.background = cellfill;
var cellbg = document.getElementById("ct"+i);
cellbg.style.background = cellfill;
cellfill = "";
}
} // if spos
//fontSize = "14pt";
}

}
countup = 0; //
}
//

function showsolution() {
resetgrid();
var allInputs = document.getElementsByTagName("input");
for (var i =0; i < allInputs.length; i++) {
if (allInputs[i].type=="text") {
if (sol[i]) {
allInputs[i].value = sol[i];
}

if (sol[i] == 1) {
var cellfill = "#ff6";

} // if 1
if (sol[i] == 2) {
var cellfill = "#9f3";
} // if 2
if (sol[i] == 3) {
var cellfill = "#fcc";
} // if 3
if (sol[i] == 4) {
var cellfill = "#f93";

} // if 4
if (sol[i] == 5) {
var cellfill = "#9ff";
} // if 5
if (sol[i] == 6) {
var cellfill = "#ff0";
} // if 6
if (sol[i] == 7) {
var cellfill = "#c9f";
} // if 7
if (sol[i] == 8) {
var cellfill = "#ccf";
} // if 8
if (sol[i] == 9) {
var cellfill = "#3f9";
} // if 9


if (cellfill) {
allInputs[i].style.background = cellfill;
var cellbg = document.getElementById("ct"+i);
cellbg.style.background = cellfill;
cellfill = "";
}

} // if sol i

}
}
//
function capturekey(evt) {
evt = (evt) ? evt : ((window.event) ? event : null);
if (evt) {
var elem = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if (elem) {

var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
//alert (charCode);

focussedelem = elem.id;
justcell = focussedelem.substring(4,6);
if (charCode == 9) {

}
else {
if (charCode == 49 || charCode == 97) {
var cellfill = "#ff6";
}
else if (charCode == 50 || charCode == 98) {
var cellfill = "#9f3";
}

else if (charCode == 51 || charCode == 99) {
var cellfill = "#fcc";
}

else if (charCode == 52 || charCode == 100) {
var cellfill = "#f93";
}

else if (charCode == 53 || charCode == 101) {
var cellfill = "#9ff";
}

else if (charCode == 54 || charCode == 102) {
var cellfill = "#ff0";
}

else if (charCode == 55 || charCode == 103) {
var cellfill = "#c9f";
}

else if (charCode == 56 || charCode == 104) {
var cellfill = "#ccf";
}

else if (charCode == 57 || charCode == 105) {
var cellfill = "#3f9";
}

else {
var cellfill = "#ffe";
}
}
if (cellfill) {
document.getElementById(elem.id).style.background = cellfill;
document.getElementById(elem.id).style.color = "#000";
var cellbg = document.getElementById("ct"+justcell);
cellbg.style.background = cellfill;
cellfill = "";
}
// apply check
var cval = document.getElementById(elem.id).value;
if (cval.length > 1) {
document.getElementById(elem.id).style.background = "#ffe";
document.getElementById(elem.id).style.color = "#000";
var cellbg = document.getElementById("ct"+justcell);
cellbg.style.background = "#ffe";
} // check

} // if elem
}
}


function resetgrid() {
var allcells = document.getElementsByTagName("td");
for (var i =0; i < allcells.length; i++) {
if (allcells[i].id.indexOf("ct") == 0) {
allcells[i].style.background = "#ffe";
}
}
var allInputs = document.getElementsByTagName("input");
for (var i =0; i < allInputs.length; i++) {
if (allInputs[i].type=="text") {
allInputs[i].style.background = "#ffe";
allInputs[i].value = "";
placevalues();
}
}
}

var wronganswer = 0;
function checksolution() {

var allInputs = document.getElementsByTagName("input");
for (var i =0; i < allInputs.length; i++) {
if (allInputs[i].type=="text") {
if (sol[i]) {
if (allInputs[i].value == sol[i]) {
allInputs[i].style.color = "#000";
}
else {
if (allInputs[i].value) {
allInputs[i].style.color = "#f00";
}
wronganswer += "mistake";
}
}
}
}
if (wronganswer !=0) {
alert ("Your solution is not correct. This may be because you have not yet finished the puzzle. Any mistakes you have made have been shown in red on the grid.");
wronganswer = 0;
}
else {
alert ("Congratulations, you got the answer right!");
}
}