
function theTimer() {
	if (document.getElementById("timer")) {
		m=Math.floor(oT/60);
		s=oT%60;
		document.getElementById("timer").innerHTML='<B>'+m+':'+((s<10) ? '0' : '')+s+'</B>';
		oT++;
		theT = setTimeout("theTimer()", 1000);
	}
}

function pauseTimer() {
	if (document.getElementById("plateau").style.visibility=="") {
		clearTimeout(theT);
		document.getElementById("plateau").style.visibility="hidden";
		document.getElementById("plateau2").style.visibility="hidden";
		document.getElementById("tmr").innerHTML="Reprendre";
	} else {
		document.getElementById("plateau").style.visibility="";
		document.getElementById("plateau2").style.visibility="";
		document.getElementById("tmr").innerHTML="Pause";
		theT = setTimeout("theTimer()", 1000);
	}
}

function sZ(a) {
	o = document.getElementById(a);
	s = o.value.length;
	if (s>3) o.style.fontSize='8pt';
	else if (s>1) o.style.fontSize='10pt';
	else o.style.fontSize='18pt';
}

function resetJeu() {
	clearHints(true);
}

function clearHints(clearValue) {
	for (i=0;i<9;i++) {
		for (j=0;j<9;j++) {
    	if (eval(document.getElementById("a"+i+""+j))) {
      	document.getElementById("a"+i+""+j).style.backgroundColor="#FFFFFF";
				if (clearValue) document.getElementById("a"+i+""+j).value="";
			}
    }
  }
}

function result() {
	for (i=0;i<9;i++) {
  	for (j=0;j<9;j++) {
    	if (eval(document.getElementById("a"+i+""+j))) {
      	document.getElementById("a"+i+""+j).value=sol.charAt(i*9+j);;
        document.getElementById("a"+i+""+j).style.fontSize='18pt';
			}
    }
  }
}

function hintme(level) {
	// 1 : vérifier les doublons sur les données saisies et les cellules fixes uniquement (pas de comparaison avec la solution)
	// 2 : indiquer qu'une ou plusieurs cases est fausse sans préciser où (comparé avec la solution)
	// 3 : Mettre en valeur les cases fausses (comparé avec la solution)

	clearHints();
	if (level==1) {

	dbl=0;
	allFilled=true;
	// Doublons sur les lignes
	for (i=0;i<9;i++) {
		for (j=0;j<8;j++) {
			var a=0;
			var b=0;
			if (eval(document.getElementById("a"+i+""+j))) {
				a=document.getElementById("a"+i+""+j).value;
				if ((a.length>1) || (a<='0') || (a>'9')){
					a="";
					allFilled=false;
				}
			} else a=sol.charAt(i*9+j);
			if (a=='.') a='';
			for (k=j+1;k<9;k++) {
				if (eval(document.getElementById("a"+i+""+k))) {
					b=document.getElementById("a"+i+""+k).value;
					if ((b.length>1) || (b<='0') || (b>'9')){
						b="";
						allFilled=false;
					}
				} else b=sol.charAt(i*9+k);
				if (b=='.') b='';
				if ((a!='') && (b!='') && (a==b)) {dbl++;}
			}
		}
	}
	if (dbl==0) {
	for (j=0;j<9;j++) {
		for (i=0;i<8;i++) {
			var a=0;
			var b=0;
			if (eval(document.getElementById("a"+i+""+j))) {
				a=document.getElementById("a"+i+""+j).value;
				if (a.length>1) a="";
			} else a=sol.charAt(i*9+j);
			if (a=='.') a='';
			for (k=i+1;k<9;k++) {
				if (eval(document.getElementById("a"+k+""+j))) {
					b=document.getElementById("a"+k+""+j).value;
					if (b.length>1) b="";
				} else b=sol.charAt(k*9+j);
				if (b=='.') b='';
				if ((a!='') && (b!='') && (a==b)) {dbl++;}
			}
		}
	}
	}
	if (dbl==0) {
		for (i=0;i<9;i++) {
			tab = new Array(0,0,0,0,0,0,0,0,0,0);
			for (j=0;j<3;j++) {
				for (k=0;k<3;k++) {
					if (eval(document.getElementById("a"+(Math.floor(i/3)*3+j)+""+(i%3*3+k)))) {
		                                a=document.getElementById("a"+(Math.floor(i/3)*3+j)+""+(i%3*3+k)).value;
                		                if (a.length>1) a="";

                        		} else a=sol.charAt((Math.floor(i/3)*3+j)*9+(i%3*3+k));
					if ((a!='') && (a>='1') && (a<='9')) {
						if (tab[a]!=0) dbl++;
						tab[a]='1';
					}
				}
			}
		}
	}
	if (dbl>0) alert("Il y a plusieurs fois le même nombre dans une même colonne et/ou ligne et/ou région !");
	else if (!allFilled) alert("Jusque là, tout *semble* aller bien !");
	else alert("Bravo !");
	} else {
	dbl=0;
        for (i=0;i<9;i++) {
                for (j=0;j<9;j++) {
                        var b=sol.charAt(i*9+j);
                        var a;
                        if (eval(document.getElementById("a"+i+""+j)))
			  if (document.getElementById("a"+i+""+j).value.length==1)
                               a=document.getElementById("a"+i+""+j).value;
				else a=0;
                        else a=-1;
                        if (a!=-1) {
				if ((a!=0) && (a!=b)) {
					dbl++;
					if (level==2) {
						alert("Au moins un chiffre est mal placé !");
						return;
					} else document.getElementById("a"+i+""+j).style.backgroundColor="#FF8080";
				}
                        }
                }
        }
	if (dbl==0) alert("Rien à signaler !");
	}


}

function depClavier(e) {
	if (!e) e=window.event;
	t=e.target ? e.target : e.srcElement;
	if (t) {
		k=e.keyCode;
		i=t.id;
		if ((i.charAt(0)=='a')&&(k>=37)&&(k<=40)) {
			g=true;
			if (k%2) {
				dx=k-38;
				l=t.value.length;
				if (l>0) {
					if (t.selectionStart || t.selectionStart=='0') {
						sp=t.selectionStart;
						ep=t.selectionEnd;
					} else if (document.selection && document.selection.createRange) {
						r=document.selection.createRange();
						sl=r.text.length;
						r.moveStart('character', -l);
					ep=r.text.length;
						sp=ep-sl;
					} else
						g=false;

					if ((dx<0)&&(ep>0))
						g=false;

					if ((dx>0)&&(sp<l))
						g=false;
				}
			}

			if (g) {
				y=parseInt(i.charAt(1));
				x=parseInt(i.charAt(2));
				for (r=1; r<9; r++) {
					if (k%2) x=(9+x+k-38)%9;
					else y=(9+y+k-39)%9;
					n=document.getElementById('a'+y+x);
					if (n) {
						n.select();
						if (window.event) window.event.cancelBubble=true;
						if (e.stopPropagation) e.stopPropagation();
						break;
					}
				}
			}
		}
	}
}

