﻿function check() {
    var hl = "#ffff00"; //Dark highlight
    var hlff = "rgb(255, 255, 0)"; //Dark highlight: for firefox
    var hl_l = "#ffffcc"; //Lighter highlight
    var normal = "#dcdcdc"; //Normal
    var white = "#ffffff";
    var green = "#7cfc00";

    //First remove all highlighting
    for (var i = 0; i < 9; i++) {
        for (var j = 0; j < 9; j++) {
            var txt = document.getElementById("txt"+i+j);
            if (txt != null) {
                txt.style.backgroundColor = normal;
                document.getElementById("td"+i+j).style.backgroundColor = normal;
            } else {
                document.getElementById("td"+i+j).style.backgroundColor = white;
            }
        }
    }

    var fine = true;
    for(var i = 0; i < 9; i++) {
        for (var j = 0; j < 9; j++) {
            var indexincheat = (i * 9) + j;
            var cheatval = document.getElementById("ch").value.substring(indexincheat,indexincheat+1);
            var txt = document.getElementById("txt"+i+j); 
            if (txt != null) {
                if (txt.value != cheatval) {
                    fine = false;
                    if (txt.value != "") {
                        //Highlight
                        for (var h = 0; h < 9; h++) {
                            var cell = document.getElementById("td"+i+h);
                            var txtcell = document.getElementById("txt"+i+h);
                            if (cell.style.backgroundColor != hl && cell.style.backgroundColor != hlff) {
                                cell.style.backgroundColor = hl_l;
                                if (txtcell != null) txtcell.style.backgroundColor = hl_l;
                            }
                            var cell2 = document.getElementById("td"+h+j);
                            var txtcell2 = document.getElementById("txt"+h+j);
                            if (cell2.style.backgroundColor != hl && cell2.style.backgroundColor != hlff) {
                                cell2.style.backgroundColor = hl_l;
                                if (txtcell2 != null) txtcell2.style.backgroundColor = hl_l;
                            }
                        }
                        txt.style.backgroundColor = hl;
                        document.getElementById("td"+i+j).style.backgroundColor = hl;      
                    }
                }
            }
        }
    }
    if (fine) {
        document.getElementById("yes").style.visibility = "visible";
        document.getElementById("no").style.visibility = "hidden";
        document.getElementById("yes").style.display = "inline";
        document.getElementById("no").style.display = "none";
        //Highlight done
        for (var i = 0; i < 9; i++) {
            for (var j = 0; j < 9; j++) {
                var txt = document.getElementById("txt"+i+j);
                if (txt != null) {
                    txt.style.backgroundColor = green;
                    txt.readOnly = true;
                }
                document.getElementById("td"+i+j).style.backgroundColor = green;
            }
        }
    } else {
        document.getElementById("yes").style.visibility = "hidden";
        document.getElementById("no").style.visibility = "visible";
        document.getElementById("yes").style.display = "none";
        document.getElementById("no").style.display = "inline";
    }
}

