//use textarea so ok in firefox ?
//none found - error in keying?
//try hardest one possible  - fiendish

//=========================================================================================================
//global
var cell = new Array(81);
var cellgot = new Array(81);
var c=0;
var showhide = 'H';
var poss = new Array(9);
var posszero = new Array(9);
var posszerocell = new Array(9);
var p=0;
var f=0;
var fp=0;
var h=0;
var v=0;
var submittype='';
var suggested=0;
var popd=0;
var found=0;
var totalfound=0;
//var block = new Array(81);
var block=new Array('0','0','0','1','1','1','2','2','2','0','0','0','1','1','1','2','2','2','0','0','0','1','1','1','2','2','2','3','3','3','4','4','4','5','5','5','3','3','3','4','4','4','5','5','5','3','3','3','4','4','4','5','5','5','6','6','6','7','7','7','8','8','8','6','6','6','7','7','7','8','8','8','6','6','6','7','7','7','8','8','8')
var b=0;
var pval0 = new Array(9);
var pval1 = new Array(9);
var pval2 = new Array(9);
var pval3 = new Array(9);
var pval4 = new Array(9);
var pval5 = new Array(9);
var pval6 = new Array(9);
var pval7 = new Array(9);
var pval8 = new Array(9);
var pval9 = new Array(9);
var pval10 = new Array(9);
var pval11 = new Array(9);
var pval12 = new Array(9);
var pval13 = new Array(9);
var pval14 = new Array(9);
var pval15 = new Array(9);
var pval16 = new Array(9);
var pval17 = new Array(9);
var pval18 = new Array(9);
var pval19 = new Array(9);
var pval20 = new Array(9);
var pval21 = new Array(9);
var pval22 = new Array(9);
var pval23 = new Array(9);
var pval24 = new Array(9);
var pval25 = new Array(9);
var pval26 = new Array(9);
var pval27 = new Array(9);
var pval28 = new Array(9);
var pval29 = new Array(9);
var pval30 = new Array(9);
var pval31 = new Array(9);
var pval32 = new Array(9);
var pval33 = new Array(9);
var pval34 = new Array(9);
var pval35 = new Array(9);
var pval36 = new Array(9);
var pval37 = new Array(9);
var pval38 = new Array(9);
var pval39 = new Array(9);
var pval40 = new Array(9);
var pval41 = new Array(9);
var pval42 = new Array(9);
var pval43 = new Array(9);
var pval44 = new Array(9);
var pval45 = new Array(9);
var pval46 = new Array(9);
var pval47 = new Array(9);
var pval48 = new Array(9);
var pval49 = new Array(9);
var pval50 = new Array(9);
var pval51 = new Array(9);
var pval52 = new Array(9);
var pval53 = new Array(9);
var pval54 = new Array(9);
var pval55 = new Array(9);
var pval56 = new Array(9);
var pval57 = new Array(9);
var pval58 = new Array(9);
var pval59 = new Array(9);
var pval60 = new Array(9);
var pval61 = new Array(9);
var pval62 = new Array(9);
var pval63 = new Array(9);
var pval64 = new Array(9);
var pval65 = new Array(9);
var pval66 = new Array(9);
var pval67 = new Array(9);
var pval68 = new Array(9);
var pval69 = new Array(9);
var pval70 = new Array(9);
var pval71 = new Array(9);
var pval72 = new Array(9);
var pval73 = new Array(9);
var pval74 = new Array(9);
var pval75 = new Array(9);
var pval76 = new Array(9);
var pval77 = new Array(9);
var pval78 = new Array(9);
var pval79 = new Array(9);
var pval80 = new Array(9);
var pval81 = new Array(9);
var rval0 = new Array(9);
var rval1 = new Array(9);
var rval2 = new Array(9);
var rval3 = new Array(9);
var rval4 = new Array(9);
var rval5 = new Array(9);
var rval6 = new Array(9);
var rval7 = new Array(9);
var rval8 = new Array(9);
var rval9 = new Array(9);
var rval10 = new Array(9);
var rval11 = new Array(9);
var rval12 = new Array(9);
var rval13 = new Array(9);
var rval14 = new Array(9);
var rval15 = new Array(9);
var rval16 = new Array(9);
var rval17 = new Array(9);
var rval18 = new Array(9);
var rval19 = new Array(9);
var rval20 = new Array(9);
var rval21 = new Array(9);
var rval22 = new Array(9);
var rval23 = new Array(9);
var rval24 = new Array(9);
var rval25 = new Array(9);
var rval26 = new Array(9);
var rval27 = new Array(9);
var rval28 = new Array(9);
var rval29 = new Array(9);
var rval30 = new Array(9);
var rval31 = new Array(9);
var rval32 = new Array(9);
var rval33 = new Array(9);
var rval34 = new Array(9);
var rval35 = new Array(9);
var rval36 = new Array(9);
var rval37 = new Array(9);
var rval38 = new Array(9);
var rval39 = new Array(9);
var rval40 = new Array(9);
var rval41 = new Array(9);
var rval42 = new Array(9);
var rval43 = new Array(9);
var rval44 = new Array(9);
var rval45 = new Array(9);
var rval46 = new Array(9);
var rval47 = new Array(9);
var rval48 = new Array(9);
var rval49 = new Array(9);
var rval50 = new Array(9);
var rval51 = new Array(9);
var rval52 = new Array(9);
var rval53 = new Array(9);
var rval54 = new Array(9);
var rval55 = new Array(9);
var rval56 = new Array(9);
var rval57 = new Array(9);
var rval58 = new Array(9);
var rval59 = new Array(9);
var rval60 = new Array(9);
var rval61 = new Array(9);
var rval62 = new Array(9);
var rval63 = new Array(9);
var rval64 = new Array(9);
var rval65 = new Array(9);
var rval66 = new Array(9);
var rval67 = new Array(9);
var rval68 = new Array(9);
var rval69 = new Array(9);
var rval70 = new Array(9);
var rval71 = new Array(9);
var rval72 = new Array(9);
var rval73 = new Array(9);
var rval74 = new Array(9);
var rval75 = new Array(9);
var rval76 = new Array(9);
var rval77 = new Array(9);
var rval78 = new Array(9);
var rval79 = new Array(9);
var rval80 = new Array(9);
var rval81 = new Array(9);

//====================================================================
function init() {
clearall();
}
//====================================================================
function calcone() {
//get all input
get();

//try and get one square
tryone('one');

//check any still empty
//if totalfound=0 & none empty show alert - all finished
if (found==0){
document.inp.tx.value='None Found To Calculate';
}

//set all form values
set();

}

//====================================================================
function suggone() {
//get all input
get();

//try and get one square
tryone('sug');

//check any still empty
//if totalfound=0 & none empty show alert - all finished
if (found==0){
document.inp.tx.value='None Found To Suggest';
}

//set all form values
set();

}

//====================================================================
function calcall() {
//get all input
get();

totalfound=0;

var totfound=1;
while (totfound>0) {
totfound=0;
//try and get one square
poppval();
found=1;
while (found>0) {
found=0;
usepval('one');
if (found==0) {
checkgroup('one');
}
totfound=totfound+found;
totalfound=totalfound+found;
}
}

document.inp.tx.value='';
//check any still empty
//if totalfound=0 & none empty show alert - all finished
if (totalfound==0){
document.inp.tx.value='None Found To Calculate';
}

//set all form values
set();

}
//====================================================================
function tryone(submittype) {

poppval();
usepval(submittype);
poppval();

if (found==0) {
checkgroup(submittype);
}

}

//====================================================================
function get() {
//change cursor to timer
//document.body.style.cursor = "wait";
//document.inp.style.cursor = "wait";
for (c = 0; c < 81; c++){
cell[c]=document.inp.ce[c].value;
}
suggested=99;
//alert('get '+cell);
}

//====================================================================
function set() {
//alert(showhide);
for (c = 0; c < 81; c++){
if (cell[c]!=''){
document.inp.ce[c].value=cell[c];
document.inp.ce[c].className="ce1";
}
if (cell[c]==''){
document.inp.ce[c].value='';
var posstot=0;
for (p = 0; p < 9; p++){
if (showhide=='S'){
if (eval('pval'+c)[p]=='1'){
//document.inp.ce[0].value='1234\r5678';
posstot=posstot+1;
if (posstot==6){
document.inp.ce[c].value=document.inp.ce[c].value+'\r';
}
document.inp.ce[c].value=document.inp.ce[c].value+(p+1);
document.inp.ce[c].className="ce3";
//document.inp.ce[c].style.background="#FFEECC";
}
}
}
}
}
//alert('set '+cell);
//change cursor back to auto
//document.body.style.cursor = "auto";
//document.inp.style.cursor = "auto";
}

//====================================================================
function chkclr(el) {
//alert(el.value.length);
if (el.value.length>1){
el.value='';
}
}

//====================================================================
function chg(el) {
document.inp.ce[el].style.background="#eeffcc";
if (document.inp.ce[el].value=='' || document.inp.ce[el].value==' '){
document.inp.ce[el].style.background="#ffffff";
}
//for (c = 0; c < 81; c++){
//checkcell('all');
//}
get();
poppval();
set();
}

//====================================================================
function clearall() {
for (c = 0; c < 81; c++){
document.inp.ce[c].style.background="#ffffff";
document.inp.ce[c].value='';
document.inp.ce[c].className="ce1";
}
document.inp.tx.value='';
}

//====================================================================
function clearcalc() {
for (c = 0; c < 81; c++){
//alert(document.inp.ce[c].style.background);
if (document.inp.ce[c].style.background=="#ffeecc") {
document.inp.ce[c].value='';
document.inp.ce[c].style.background="#ffffff";
}
}
}

//====================================================================
function genpuz() {
alert('Feature Not Available Yet - Coming Soon');
}

//====================================================================
function showposs() {
if (showhide=='H'){
showhide = 'S';
//document.showposs.value='Hide Possible Values';
}
else {
showhide = 'H';
//document.showposs.value='Show Possible Values';
}
get();
poppval();
set();
}

//====================================================================
function poppval() {

popd=0;

//clear previous pvals
for (c = 0; c < 81; c++){
//if (document.inp.ce[c].style.background=="#ffffff") {
if (document.inp.ce[c].className=="ce3" || cell[c].length>1) {
cell[c]='';
if (c!=suggested) {
document.inp.ce[c].style.background="#ffffff";
}
}
}

//check each cell
for (c = 0; c < 81; c++){

if (cell[c]!=''){
popd=popd+1;
}

//clear poss values 1-9 [0-8]
for (p = 0; p < 9; p++){
poss[p]=0;
eval('pval'+c)[p]='0';
}

//if blank attempt to fill
if (cell[c]==''){
//alert(c);

//read vertical values higher
v=c+9;
while (v<81) {
//alert(v);
if (cell[v]!=''){
poss[cell[v]-1]=1;
}
v=v+9;
}
//read vertical values lower
v=c-9;
while (v>=0) {
//alert(v);
if (cell[v]!=''){
poss[cell[v]-1]=1;
}
v=v-9;
}

//read horizontal values
//hs=Integer(c/9);
hs=parseInt(c/9);
//alert('hs1 '+hs+' c '+c);
hs=hs*9;
//alert('hs2 '+hs+' c '+c);
for (h = hs; h < hs+9; h++){
//alert(h);
if (h!=c){
if (cell[h]!=''){
poss[cell[h]-1]=1;
}
}
}

//check block of 9
thisblock=block[c];
for (b = 0; b < 81; b++){
if (b!=c){
if (block[b]==thisblock){
if (cell[b]!=''){
poss[cell[b]-1]=1;
}
}
}
}

//set pval
for (p = 0; p < 9; p++){
eval('pval'+c)[p]='0';
if (poss[p]==0){
eval('pval'+c)[p]='1';
}
}

} //if cell blank

} //c=1to81

}

//====================================================================
function usepval(submittype) {

found=0;

for (c = 0; c < 81; c++){
if (cell[c]==''){
if (found==0){

//check if narrowed down to one number
//alert(poss);
f=0;
for (p = 0; p < 9; p++){
//if (poss[p]==0){
if (eval('pval'+c)[p]=='1'){
f=f+1;
fp=p;
}
}

//use the only 0 poss
if (f==1){
//alert('c '+c+' fp+1 '+fp+1);
if (submittype!='sug'){
cell[c]=fp+1;
document.inp.ce[c].className="ce1";
}
else{
suggested=c;
}
document.inp.ce[c].style.background="#FFEECC";
found=found+1;
document.inp.tx.value='1. Only number not used in this row, column and 3*3 block.';
}

}
}
}
//alert('usepval '+submittype+' '+suggested);
}

//====================================================================
function checkgroup(submittype) {

if (found==0){
//block
//each block line
for (b2 = 0; b2 < 9; b2=b2+1){
//clear posszero values 1-9 [0-8]
for (p = 0; p < 9; p++){
posszero[p]=0;
posszerocell[p]=0;
}
//each cell in block
for (c = 0; c < 81; c++){
if (block[c]==b2) {      //check if cell is in block
//checkcell(submittype); -already have poss values in pvalX
//if cell not blank then set all poss to 1
//if (cell[c]!=''){
//for (p = 0; p < 9; p++){
//poss[p]=1;
//}
//poss[cell[c]-1]=99;
//}
//each digit
for (p = 0; p < 9; p++){
//if (poss[p]==0){
if (eval('pval'+c)[p]=='1'){
posszero[p]=posszero[p]+1;
posszerocell[p]=c;
}
}
//alert('c '+c+' b2 '+b2+' poss '+poss+' posszero '+posszero+' posszerocell '+posszerocell);
}
}

//alert('b2 '+b2+' posszero '+posszero);
//check posszero - if one poss = one only then must be that number - but where ?
for (p = 0; p < 9; p++){
if (posszero[p]==1){
if (cell[posszerocell[p]]==''){
if (submittype!='sug'){
cell[posszerocell[p]]=p+1;
document.inp.ce[posszerocell[p]].className="ce1";
}
else{
suggested=c;
}
if (submittype=='one' || submittype=='sug'){
b2=9;
}
document.inp.ce[posszerocell[p]].style.background="#FFEECC";
found=found+1;
document.inp.tx.value='2a. Only square possible for this number in 3x3 block.';
//alert('b2 '+b2+' p+1 '+(p+1)+' posszerocell-p '+posszerocell[p]);
}
}
}

} //end of b2 loop
//end of block
}

if (found==0){
//horiz-----------------------------------------------------------------------
//each horiz line
for (h2 = 0; h2 < 81; h2=h2+9){
//clear posszero values 1-9 [0-8]
for (p = 0; p < 9; p++){
posszero[p]=0;
posszerocell[p]=0;
}
//each cell on horiz line
for (c = h2; c < h2+9; c++){
//checkcell(submittype);
//if cell not blank then set all poss to 1
//if (cell[c]!=''){
//for (p = 0; p < 9; p++){
//poss[p]=1;
//}
//poss[cell[c]-1]=99;
//}
//each digit
for (p = 0; p < 9; p++){
//if (poss[p]==0){
if (eval('pval'+c)[p]=='1'){
posszero[p]=posszero[p]+1;
posszerocell[p]=c;
}
}
//alert('c '+c+' h2 '+h2+' poss '+poss+' posszero '+posszero+' posszerocell '+posszerocell);
}

//alert('h2 '+h2+' posszero '+posszero+' posszerocell '+posszerocell);
//check posszero - if one poss = one only then must be that number - but where ?
for (p = 0; p < 9; p++){
if (posszero[p]==1){
if (cell[posszerocell[p]]==''){
if (submittype!='sug'){
cell[posszerocell[p]]=p+1;
document.inp.ce[posszerocell[p]].className="ce1";
}
else{
suggested=c;
}
if (submittype=='one' || submittype=='sug'){
h2=81;
}
//alert (posszerocell[p]+' '+cell[posszerocell[p]]);
document.inp.ce[posszerocell[p]].style.background="#FFEECC";
found=found+1;
document.inp.tx.value='2b. Only square possible for this number in this row.';
//alert('h2 '+h2+' p+1 '+(p+1)+' posszerocell-p '+posszerocell[p]);
}
}
}

} //end of h2 loop
//end of horiz
}

if (found==0){
//vert--------------------------------------------------------------------------
//each vert line
for (v2 = 0; v2 < 9; v2=v2+1){
//clear posszero values 1-9 [0-8]
for (p = 0; p < 9; p++){
posszero[p]=0;
posszerocell[p]=0;
}
//each cell on vert line
for (c = v2; c < 81; c=c+9){
//checkcell(submittype);
//if cell not blank then set all poss to 1
//if (cell[c]!=''){
//for (p = 0; p < 9; p++){
//poss[p]=1;
//}
//poss[cell[c]-1]=99;
//}
//each digit
for (p = 0; p < 9; p++){
//if (poss[p]==0){
if (eval('pval'+c)[p]=='1'){
posszero[p]=posszero[p]+1;
posszerocell[p]=c;
}
}
//alert('c '+c+' v2 '+v2+' poss '+poss+' posszero '+posszero+' posszerocell '+posszerocell);
}

//alert('v2 '+v2+' posszero '+posszero);
//check posszero - if one poss = one only then must be that number - but where ?
for (p = 0; p < 9; p++){
if (posszero[p]==1){
if (cell[posszerocell[p]]==''){
if (submittype!='sug'){
cell[posszerocell[p]]=p+1;
document.inp.ce[posszerocell[p]].className="ce1";
}
else{
suggested=c;
}
if (submittype=='one' || submittype=='sug'){
v2=9;
}
document.inp.ce[posszerocell[p]].style.background="#FFEECC";
found=found+1;
document.inp.tx.value='2c. Only square possible for this number in this column.';
//alert('v2 '+v2+' p+1 '+(p+1)+' posszerocell-p '+posszerocell[p]);
}
}
}

} //end of v2 loop
//end of vert
}

} //function checkgroup

//============================================================================================
function getrval(submittype) {

//set rval as current pval
for (c = 0; c < 81; c++){
for (p = 0; p < 9; p++){
//if (poss[p]==0){
eval('rval'+c)[p]=eval('pval'+c)[p];
}
}

if (found==0){
//block
//each block line
for (b2 = 0; b2 < 9; b2=b2+1){
//each cell in block
for (c = 0; c < 81; c++){
if (block[c]==b2) {      //check if cell is in block
//each digit
for (p = 0; p < 9; p++){
//if (poss[p]==0){
if (eval('pval'+c)[p]=='1'){
posszero[p]=posszero[p]+1;
posszerocell[p]=c;
}
}
//alert('c '+c+' b2 '+b2+' poss '+poss+' posszero '+posszero+' posszerocell '+posszerocell);
}
}

//alert('b2 '+b2+' posszero '+posszero);
//check posszero - if one poss = one only then must be that number - but where ?
for (p = 0; p < 9; p++){
if (posszero[p]==1){
if (cell[posszerocell[p]]==''){
if (submittype!='sug'){
cell[posszerocell[p]]=p+1;
document.inp.ce[posszerocell[p]].className="ce1";
}
else{
suggested=c;
}
if (submittype=='one' || submittype=='sug'){
b2=9;
}
document.inp.ce[posszerocell[p]].style.background="#FFEECC";
found=found+1;
document.inp.tx.value='2a. Only square possible for this number in 3x3 block.';
//alert('b2 '+b2+' p+1 '+(p+1)+' posszerocell-p '+posszerocell[p]);
}
}
}

} //end of b2 loop
//end of block
}

if (found==0){
//horiz-----------------------------------------------------------------------
//each horiz line
for (h2 = 0; h2 < 81; h2=h2+9){
//each cell on horiz line
for (c = h2; c < h2+9; c++){
//each digit
for (p = 0; p < 9; p++){
//if (poss[p]==0){
if (eval('pval'+c)[p]=='1'){
posszero[p]=posszero[p]+1;
posszerocell[p]=c;
}
}
//alert('c '+c+' h2 '+h2+' poss '+poss+' posszero '+posszero+' posszerocell '+posszerocell);
}

//alert('h2 '+h2+' posszero '+posszero+' posszerocell '+posszerocell);
//check posszero - if one poss = one only then must be that number - but where ?
for (p = 0; p < 9; p++){
if (posszero[p]==1){
if (cell[posszerocell[p]]==''){
if (submittype!='sug'){
cell[posszerocell[p]]=p+1;
document.inp.ce[posszerocell[p]].className="ce1";
}
else{
suggested=c;
}
if (submittype=='one' || submittype=='sug'){
h2=81;
}
//alert (posszerocell[p]+' '+cell[posszerocell[p]]);
document.inp.ce[posszerocell[p]].style.background="#FFEECC";
found=found+1;
document.inp.tx.value='2b. Only square possible for this number in this row.';
//alert('h2 '+h2+' p+1 '+(p+1)+' posszerocell-p '+posszerocell[p]);
}
}
}

} //end of h2 loop
//end of horiz
}

if (found==0){
//vert--------------------------------------------------------------------------
//each vert line
for (v2 = 0; v2 < 9; v2=v2+1){
//each cell on vert line
for (c = v2; c < 81; c=c+9){
//each digit
for (p = 0; p < 9; p++){
//if (poss[p]==0){
if (eval('pval'+c)[p]=='1'){
posszero[p]=posszero[p]+1;
posszerocell[p]=c;
}
}
//alert('c '+c+' v2 '+v2+' poss '+poss+' posszero '+posszero+' posszerocell '+posszerocell);
}

//alert('v2 '+v2+' posszero '+posszero);
//check posszero - if one poss = one only then must be that number - but where ?
for (p = 0; p < 9; p++){
if (posszero[p]==1){
if (cell[posszerocell[p]]==''){
if (submittype!='sug'){
cell[posszerocell[p]]=p+1;
document.inp.ce[posszerocell[p]].className="ce1";
}
else{
suggested=c;
}
if (submittype=='one' || submittype=='sug'){
v2=9;
}
document.inp.ce[posszerocell[p]].style.background="#FFEECC";
found=found+1;
document.inp.tx.value='2c. Only square possible for this number in this column.';
//alert('v2 '+v2+' p+1 '+(p+1)+' posszerocell-p '+posszerocell[p]);
}
}
}

} //end of v2 loop
//end of vert
}

} //function checktwins

//============================================================================================
function userval(submittype) {
}
//============================================================================================

