var debug=false;
var niveau='1';
var charge=0;
//alert("debut "+niveau);
var size=new Array(3);
size[0]=3; size[1]=3; size[2]=size[0]*size[1];
var solveur=0;
var nb_indice;
var la_solution;
var nb_start=0;
var grille_debut;
var fU89=0;
var gc=0;

function createGrid()
 {
 var i, j, s=' <div id="table_jeu"> <div id="ombre_portee" style="width:525px; height:435px"></div><div id="tableau" style="affiche"><table cellSpacing=0 align="center" valign="center" cellPadding=1  id="tableau" class="box-table">',
 inp='<input type="text" class="cell" maxLength=1 id="x';
 for (i=0;i<=size[2];i++)
  {
  s+='<tr>';
  for (j=0;j<=size[2];j++)
   {
   if (i==0 && j==0)
    s+='<td align="center" valign="center"  class="celler"></td>';//<a href="#" onClick="howTo();" style="font-size:10px;text-decoration:none;border:0px;">?</a>
   else if (j==0){
    s+='<td align="center" valign="center"  class="celler"><div style="position:relative"> <span id="coord'+xName[i-1]+'" class="cache">'+(size[2]<36?xName[i-1]:'&nbsp;')+'</span></div></td>';

   }
   else if (i==0) {
    s+='<td align="center" valign="center"  class="celler"><div style="position:relative"><span id="coord'+yName[j-1]+'" class="cache">'+(size[2]<36?yName[j-1]:'&nbsp;')+'</span></div></td>'; 

   }
   else
    {
    if (i%size[0]==0 && j%size[1]==0)	s+='<td class="tdBR">';
    else if (i%size[0]==0)		s+='<td class="tdB">';
    else if (j%size[1]==0)		s+='<td class="tdR">';
    else				s+='<td class="tdC">';
    s+=inp+((i-1).toString(36))+'y'+((j-1).toString(36))+'"></td>';
   }}
  s+='</tr>';
  }
 return s+'</table></div></div>';
 }

var	xName=new Array(36), yName=new Array(36), zName=new Array(36), 
	xyz=new Array(3),	// xyz[0]=rows, xyz[1]=columns, xyz[2]=regions
	xy=new Array(),		// row & column intersections = cells
	full='123456789',	//full='abcdefghi',
	//full='0123456789ab',
	//full='0123456789abcdef',
	currentSize=new Array(2),
	timerID=null, running=false, //d1=0, d2, d3,
	guess=new Array();
for (i=0;i<26;i++)
 {xName[i]=String.fromCharCode((65+i)); yName[i]=(i+1).toString();}
for (i=26;i<36;i++)
 {xName[i]='A'+String.fromCharCode((65+i-26)); yName[i]=(i+1).toString();}
/*
k, k1:	0|1	0|1|2	0|1|2|3
	-+-	-+-+-	-+-+-+-
	2|3	3|4|5	4|5|6|7
		-+-+-	-+-+-+-
		6|7|8	8|9|...
*/
function i_k(k,k1) {return Math.floor(k/size[0])*size[0]+Math.floor(k1/size[1]);}
function j_k1(k,k1) {return k%size[0]*size[1]+k1%size[1];}

function ini()	// initiates objects
 {
 document.getElementById('Grid').innerHTML=createGrid();
 //ChronoStop();
 tip=document.ti.ps;
 var i, j, k, l, m, n, g=true, o;
 for (i=0;i<size[2];i++)
  {
  xy[i]=new Array(size[2]);
  for (j=0;j<size[2];j++)
   {
   xy[i][j]=new Object();
   xy[i][j].g=eval('document.grid.x'+i.toString(36)+'y'+j.toString(36));
   xy[i][j].h=full;	//g for grid; h for hidden candidats
   xy[i][j].i=i;
   xy[i][j].j=j;
   xy[i][j].k =i_k(i,j);
   xy[i][j].k1=j_k1(i,j);
   xy[i][j].g.onkeydown=keyD;
   xy[i][j].g.onkeypress=keyP;
   xy[i][j].g.i=i;
   xy[i][j].g.j=j;
  }}
 currentSize=page();

 gridSize();
 for (n=0;n<3;n++)
  {
  xyz[n]=new Array(size[2]);
  for (i=0;i<size[2];i++)
   {
   xyz[n][i]=new Object();
   xyz[n][i].h=full;
   xyz[n][i].c=new Array(size[2]);
   for (j=0;j<size[2];j++)
    {
    if (n==0)		xyz[n][i].c[j]=xy[i][j];
    else if (n==1)	xyz[n][i].c[j]=xy[j][i];
    else xyz[n][i].c[j]=xy[i_k(i,j)][j_k1(i,j)];
  }}}
 eat('off').toGrid();

 for (k=0;k<size[2];k++)
  zName[k]='['+xName[i_k(k,0)]+','+yName[j_k1(k,0)]+']...['
	+xName[i_k(k,size[2]-1)]+','+yName[j_k1(k,size[2]-1)]+']';
start_(niveau);

}


//////	adaptation to the window's sizes

function page()
 {
 var d=new Array(2), s;
 if (window.innerWidth)
  {d[0]=window.innerWidth; d[1]=window.innerHeight;}
 else
  {
  if (document.documentElement)	s=document.documentElement;
  else if (document.body)	s=document.body;
  if (s.clientWidth)		{d[0]=s.clientWidth; d[1]=s.clientHeight;}
  else if (s.offsetWidth)	{d[0]=s.offsetWidth; d[1]=s.offsetHeight;}
  }
 return d;
 }

function gridSize()
 {
 var i, j, n, d=page();
 if ((d[0]<550 || d[1]<250) && parseInt(navigator.appVersion)>3)
  {
  d[0]=600; d[1]=430;
  if (navigator.appName=="Netscape") {top.outerWidth=d[0]; top.outerHeight=d[1];}
  else top.resizeTo(d[0],d[1]);
  d=page();
  }
 if (Math.abs(d[0]+d[1]-currentSize[0]-currentSize[1])<20)
  return;
 currentSize=page();
 //if (navigator.appName=="Netscape")
  top.location.href=window.location.href;
 /*n=Math.max(16,Math.floor((d[1]-45-30)/size[0]/size[1]-4));
 for (i=0;i<size[2];i++) for (j=0;j<size[2];j++)
  {with (xy[i][j].g.style)
   {width=n; height=n; lineHeight=n-2; fontSize=n-4;
  }}
 var m=n*size[2]+26;
 tip.style.height=m;
 m=d[0]-m-n-135;
 tip.style.width=m;*/
 }

//////	cookies

function cook(name,txt,day)
 {
	
 var d=new Date();
 d.setTime(d.getTime()+day*864E5);
 document.cookie=name+'='+txt.toString()+'; expires='+d.toGMTString()+';';
 return (document.cookie!=null);
 
 
 }

function eat(name)
 {

 if (!document.cookie)
  return '';
 var j, s='', i=document.cookie.indexOf(name);
 if (-1<i)
  {
  j=document.cookie.indexOf(';',i);
  s=document.cookie.substring(i+name.length+1,(j<0)?document.cookie.length:j);
  }
 return s;
 }




//////	grid data exchange

function fromGrid()	//'|' comme séparateur de champs complexe
 {
 var i, j, s, ex='';
 for (i=0;i<size[2];i++)
  for (j=0;j<size[2];j++)
   {
   s=xy[i][j].g.value;
   ex+=s!=''?s:'-';
   }
 return ex;
 }

String.prototype.toGrid=function()
 {
 var i=size[2], j, s, txt='';
 if (this==null || this=='' || this.length==size[2]*size[2])
  txt=this;
 else if (this.length==size[2]*(size[2]+1)-1)
  {
  s=this.charAt(size[2]);
  while (i<this.length && s==this.charAt(i))
   i+=10;
  if (i<this.length-10)
   txt=null;
  else
   for (i=0;i<this.length;i++)
    if (this.charAt(i)!=s)
     txt+=this.charAt(i);
  }
 for (i=0;i<size[2];i++)
  for (j=0;j<size[2];j++)
   {
   s=(txt==null || txt=='' || full.indexOf(txt.charAt(size[2]*i+j))<0)?'':txt.charAt(size[2]*i+j);
   xy[i][j].g.disabled = false;
   xy[i][j].g.value=s;

	   if(s==1||s==2||s==3||s==4||s==5||s==6||s==7||s==8||s==9){
		   if(gc==0){
		   	xy[i][j].g.readOnly = true;
		   }
		   
	   }
	   else{
		   xy[i][j].g.readOnly = false;
		  
	   }
  

   xy[i][j].h=(s=='')?full:'';
   xy[i][j].g.className='cell';
   xy[i][j].g.style.color='gray';
   }
 if (!debug) { tip.value='';  }
 xyzH();
 xy[Math.floor(size[2]/2)][Math.floor(size[2]/2)].g.focus();

 }

function xyzH()	//creates xyz[n][].h
 {
 var i, j, n, s;
 for (n=0;n<3;n++)
  for (i=0;i<size[2];i++)
   {
   xyz[n][i].h=full;
   for (j=0;j<size[2];j++)
    {
    s=xyz[n][i].c[j].g.value;
    if (xyz[n][i].h.indexOf(s)<0) {
     tip.value+=str[8]+str[5+n]+' '+(n==0?xName[i]:n==1?yName[i]:zName[i])+'.\n';
	 show(tip.value,"#fff"); 
    }else{
     xyz[n][i].h=xyz[n][i].h.del(s);
	 }
 } }}


//////	string manipulation

String.prototype.del=function(s)
 {
 if (this=='' || s=='')	//(this==null || s==null || this=='' || s=='')
  return this;
 var n=this.indexOf(s);
 if (n<0)
  return this;
 return this.substr(0,n)+this.substr(n+1);
 }

String.prototype.delAll=function(s)
 {
 if (this=='' || s=='')
  return this;
 var s1='', n;
 for (n=0;n<this.length;n++)
  if (s.indexOf(this.charAt(n))<0)
   s1+=this.charAt(n);
 return s1;
 }

String.prototype.cross=function(s)	// collections's intersection
 {
 if (this=='' || s=='')
  return '';
 var a, n;
 for (n=s.length-1;-1<n;n--)
  if (this.indexOf(s.charAt(n))<0)
   s=s.substr(0,n)+s.substr(n+1);
 return s;
 }

String.prototype.add=function(s)	// collections's union
 {
 if (this==full || s==full)
  return full;
 var s1='', n;
 for (n=0;n<size[2];n++)
  if (this.indexOf(full.charAt(n))>-1 || s.indexOf(full.charAt(n))>-1)
   s1+=full.charAt(n);
 return s1;
 }

String.prototype.shake=function()
 {
 var i, l=new Array(), m=this;
 for (i=m.length-1;0<=i;i--)
  {
  l[i]=m.charAt(Math.floor((i+.99)*Math.random()));
  m=m.del(l[i]);
  }
 return l;
 }

function shakeRegion(x)
 {
 var i, j, k=new Array(size[2]), m, n=new Array(x), s='';
 for (i=0;i<x;i++)
  s+=i.toString();
 m=s.shake()
 for (i=0;i<x;i++)
  {
  n=s.shake();
  for (j=0;j<size[2]/x;j++)
   k[i*x+j]=x*m[i]+n[j]*1;
  }
 return k;
 }


//////	sudoku solve methods, used by clue, find & solve

function su()
 {
 //dépiler
 }

function su1()	//calculates initial values of xy[][].h
 {if (debug){ tip.value+='1'; show(tip.value,"#fff"); }
 var i, j, k, x;
 for (i=0;i<size[2];i++)
  for (j=0;j<size[2];j++)
   {
   xy[i][j].h=xy[i][j].g.value!=''?'':xyz[0][i].h.cross(xyz[1][j].h).cross(xyz[2][xy[i][j].k].h);
   //if (xy[i][j].h.length==1) piler
 } }

var su2Running=false, timerSu2=null;
//function su2(w) {setTimeout('su2_(w)', 0);}
function su2(w)	//calculates xy[][].h && xyz[][].h modified by the last entry
 {
 /*if (!su2Running)
  {
  timerSu2=setTimeout('su2_(w)', 0);
  su2Running=true;
  return;
  }*/
 su2Running=false;
 var n, m, o=new Array(3), ww=w.g.value;
 w.h='';
 if (w.g.style.color!='red')
  w.g.style.color='green';
 o[0]=xyz[0][w.i]; o[1]=xyz[1][w.j]; o[2]=xyz[2][w.k];
 for (n=0;n<3;n++)
  {
  o[n].h=o[n].h.del(ww);
  for (m=0;m<size[2];m++)
   if (n==2 || w.k!=o[n].c[m].k)
    {
    o[n].c[m].h=o[n].c[m].h.del(ww);
    //if (o[n].c[m].h.length==1) piler
 }} }

function su3(f)	//search for an unicity in xy[][]
 {if (debug) { tip.value+='3'; show(tip.value,"#fff"); }
 var g=false;
 for (i=0;i<size[2];i++)
  for (j=0;j<size[2];j++)
   if (xy[i][j].g.value=='' && xy[i][j].h.length<2)
    {
    if (xy[i][j].h=='')
     {
     if (f<2) {
      tip.value+=str[size[2]]+xName[i]+','+yName[j]+str[10];
	  show(tip.value,"#fff");
		coord(xName[i]);
		coord(yName[j]);
	  }
     return 'error';
     }
    if (f>0)
     {
     xy[i][j].g.value=xy[i][j].h;
     su2(xy[i][j]);
     g=true;
     }
    if (f<2)
     {
     tip.value+=str[11]+xName[i]+','+yName[j]+'].';
	 show(tip.value,"#fff");
	 coord(xName[i]);
	 coord(yName[j]);

     return true;
    }}
 return g;
 }

function su4(f)	//search for a number's unicity in xyz[]
 {if (debug){ tip.value+='4'; show(tip.value,"#fff");}
 var i, j, g=false, l, j1, m, n, s;
 for (n=0;n<3;n++)
  for (i=0;i<size[2];i++)
   for (m=xyz[n][i].h.length-1;-1<m;m--)
    {
    s=xyz[n][i].h.charAt(m);
    l=0;
    for (j=0;j<size[2];j++)
     if (xyz[n][i].c[j].g.value=='' && xyz[n][i].c[j].h.indexOf(s)>=0)
      {l++; j1=j;}
    if (l==1)
     {
     if (f>0)
      {
      xyz[n][i].c[j1].g.value=s;
      su2(xyz[n][i].c[j1]);
      g=true;
      }
     if (f<2)
      {
      tip.value+=str[12]+s+str[13]+str[5+n]+(n==0?xName[i]:n==1?yName[i]:zName[i])+'.';
	  if (n==0) {coord(xName[i]);}
	  if (n==1) {coord(yName[i]);}
	
	  show(tip.value,"#fff");
      return true;
    }}}
 return g;
 }

function su5(f)	//search for mandatory intersections
 {if (debug){ tip.value+='5'; show(tip.value,"#fff");}
 var g=false, k, m, n, o1, o2, s, x, xR, xC, xK;
 for (k=0;k<size[2];k++)
  {
  o1=xyz[2][k];
  for (m=0;m<o1.h.length;m++)
   {
   s=o1.h.charAt(m);
   xR=-2;
   xC=-2;
   for (k1=0;k1<size[2] && (xR!=-1 || xC!=-1);k1++)
    if (-1<o1.c[k1].h.indexOf(s))
     {
     x=o1.c[k1].i;
     xR=(xR==-2||xR==x)?x:-1;
     x=o1.c[k1].j;
     xC=(xC==-2||xC==x)?x:-1;
     }  
   if (-1<xR)
    for (j=0;j<size[2];j++)
     {
     o2=xyz[0][xR].c[j];
     if (o2.k!=k)
      {
      x=o2.h.length;
      o2.h=o2.h.del(s);
      if (o2.h.length<x)
       g=true;
     }}
   if (-1<xC)
    for (i=0;i<size[2];i++)
     {
     o2=xyz[1][xC].c[i];
     if (o2.k!=k)
      {
      x=o2.h.length;
      o2.h=o2.h.del(s);
      if (o2.h.length<x)
       g=true;
  }} }}
 for (n=0;n<2;n++)
  for (i=0;i<size[2];i++)
   {
   o1=xyz[n][i];
   for (m=0;m<o1.h.length;m++)
    {
    s=o1.h.charAt(m);
    xK=-2;
    for (j=0;j<size[2] && xK!=-1;j++)
     if (-1<o1.c[j].h.indexOf(s))
      {
      x=o1.c[j].k;
      xK=(xK==-2||xK==x)?x:-1;
      }  
    if (-1<xK)
     for (k1=0;k1<size[2];k1++)
      {
      o2=xyz[2][xK].c[k1];
      if ((n==0 && o2.i!=i) || (n==1 && o2.j!=i))
      {
      x=o2.h.length;
      o2.h=o2.h.del(s);
      if (o2.h.length<x)
       g=true;
   }}}}
 if (g && f<2){
  tip.value+=str[14];
  show(tip.value,"#fff");
  }
 return g;
 }

function su6(f)	//Chain finder
 {if (debug){ tip.value+='6'; show(tip.value,"#fff"); }
 var i, j, g=false;
 for (n=0;n<3;n++)
  for (i=0;i<size[2];i++)
   if (3<xyz[n][i].h.length)
    g=xyz[n][i].c.chain()?true:g;
 if (g && f<2){
  tip.value+=str[19];
  show(tip.value,"#fff");
  }
 return g;
 }

Array.prototype.chain=function()
 {
 var cL=this.length-1, cC='', fI=0, fr='', g=false, i=0, j=0, s='', sum=new Array(8);
 for (i=0;i<this.length;i++)
  if (0<this[i].h.length)
   {
   fr+=i.toString(36);
   if (this[i].h.length<cL)
    cL=this[i].h.length;
   }
 if (fr.length-2<cL)
  return false;
 do
  {
  cC='';
  fI=0;
  while (true)
   {
   if (this[parseInt(fr.charAt(fI),36)].h.length<=cL)
    {
    sum[cC.length]=this[parseInt(fr.charAt(fI),36)].h;
    if (cC!='')
     sum[cC.length]=sum[cC.length-1].add(sum[cC.length]);
    if (sum[cC.length].length<=cL)
     cC+=fr.charAt(fI);
    if (cC.length==cL && sum[cC.length-1].length==cL)
     break;
    }
   if (fI<fr.length-1)
    fI++;
   else if (cC=='' || fr.lastIndexOf(cC)==fr.length-cC.length)
    {
    cL++;
    cC='';
    fI=0;
    if (cL>fr.length-1)
     break;
    }
   else
    {
    while (-1<cC.indexOf(fr.charAt(fI)))
     fI--;
    while (-1<fI && cC.indexOf(fr.charAt(fI))<0)
     fI--;
    cC=cC.substr(0,cC.indexOf(fr.charAt(fI))-1);
    fI++;
   }}
   if (cC!='')
    {
    fr=fr.delAll(cC);
    s=sum[cC.length-1];
    cL=this.length-1;
    for (i=0;i<this.length;i++)
     if (-1<fr.indexOf(parseInt(i,36)))
      {
      j=this[i].h.length;
      this[i].h=this[i].h.delAll(s);
      g=this[i].h.length<j?true:g;
      if (this[i].h.length<cL)
       cL=this[i].h.length;
  } } }
 while (cC!='' && 1<fr.length)
 return g;
 }


//////	sudoku guess methods, used only by solve

function guess1()
 {
 var i=0, j=0, n=0, o;
 while (xy[i][j].h=='')
  {
  j=(j+1)%size[2];
  i+=j==0?1:0;
  //if (size[2]<=i) return 'end';
  }
 o=xy[i][j];
 while (guess[n]!=null)
  n++;
 guess[n]=new Object();
 guess[n].i=i;
 guess[n].j=j;
 guess[n].h=o.h;
 guess[n].choice=0;
 guess[n].g=fromGrid();
 o.g.value=o.h.charAt(0);
 o.g.style.color='red';
 if (debug){ tip.value+='\ng'+n+'||  o.h'+o.h; show(tip.value,"#fff");}
 su2(o);
 return true;
 }

function guess2()
 {
 var n=0, o;
 while (guess[n]!=null)	//do not use guess[n].length!
  n++;
 n--;
 while (n>=0 && guess[n].choice>=guess[n].h.length-1)
  {
  xy[guess[n].i][guess[n].j].g.style.color='green';
  guess[n]=null;	//guess[n].delete();
  n--;
  }
 if (n<0 || guess[n]==null)
  return false;
 for (i=0;i<size[2];i++)
  for (j=0;j<size[2];j++)
   {
   s=guess[n].g.charAt(size[2]*i+j);
   s=(s=='-')?'':s;
   //if (xy[i][j].g.value!=s) //for Firefox
    xy[i][j].g.value=s;
   }
 guess[n].choice=guess[n].choice+1;
 o=xy[guess[n].i][guess[n].j];
 o.g.value=guess[n].h.charAt(guess[n].choice);
 xyzH();
 su1();
 if (debug) { tip.value+='g'+n+'|'+guess[n].choice; show(tip.value,"#fff"); }
 return true;
 }


//////	sudoku high level methods

function last(f)
 {
 var i, n, s=str[15];

 switch (f) {
  case 1: s+=str[16]; break;
  case 2: s+=str[17]; break;
  default: s+=str[18];if(nb_indice>0){s+="\n Vous avez utilisé "+nb_indice+" indices.";} break;}
 for (n=0;n<2;n++)	//n<3 not needed
  for (i=0;i<size[2];i++)
   if (xyz[n][i].h!='')
    break; 
 if (n*i<2*size[2])
  return false;  
 tip.value+=s; show(tip.value,"#fff"); if(la_solution==0&fU89==0){ ChronoStop(); verifier_score();}  if(fU89==1){ ChronoStop(); wor();}
//ChronoStop();
 return true; 
 }

function clue(f)	//à réduire de quelques lignes...

 {
	 if(fU89==0){
		  nb_indice=nb_indice+1;	 
		  // cacheDisplay();
		 tip.value=''; 
		 if (last(1))
		  return;
		 su1();
		 var g=su4(f) || su3(f) || (su6(f) && (su4(f) || su3(f)));
		 if (!g)
		  {
		  su1(); g=su5(f) && (su4(f) || su3(f));
		  }
		 if (!g)
		  {
		  tip.value=''; 
		  g=su6(f) && (su4(f) || su3(f));
		  }
		 if (!g)
		  {
		  su1(); g=su6(f) && su5(f);
		  tip.value=str[24];
		  show(str[24],"#fff");
		  g=g && (su4(f) || su3(f));
		  if (!g);
		   tip.value=='';
		   
		  }
		 if (g==false){
		  tip.value=str[20]+str[21];
		   show(str[20]+str[21],"#fff");
		   }
		 last(3);
		 // setTimeout("cacheDisplay()",6000); 
	 }
 }

function test()
 {
 su1();
 su7(2);
 last(3);

 }

function solve()
 {
la_solution=1;

ChronoStop();
if (solveur==0){
 starter_(grille_debut);
}
 tip.value=''; 
 var i=0, j, s='', date=new Date();
 var d4=date.getTime(), d5=0;
 if (last(1))
  return;
 tip.value='';

 // chronoOff(); alert(chronoOff);
 
 window.status='Working: ';
 su1();
 var g=su3(2);
 while (g || su4(2) || su5(2) || su6(2) || su4(2))
  {if (debug){ tip.value+='\n'; show(tip.value,"#fff"); }
  i++;
  if (i%6==0)
   window.status+='\u258C';
  g=su3(2);
  if (g=='error')
   {
   tip.value+=str[22]; show(tip.value,"#fff");
   
   window.status='';
   return;
   }
  if (!g && last(2))
   {
   g=true;
   break;
  }}
 //date=new Date();
 //d4=date.getTime()-d4;
 if (g==false && confirm(str[20]))
  {
  //date=new Date();
  //d5=date.getTime();
  while (g || su4(2) || su5(2) || su6(2) || guess1())
   {if (debug){ tip.value+='\n'; show(tip.value,"#fff"); }
   i++;
   if (i%6==0)
    window.status+='\u258C'; if (debug && window.status.length>50) break;
   g=su3(2);
   if (g=='error' && !guess2())	//guess2()=='error')
    {
    tip.value+=str[22]; show(tip.value,"#fff");
    break;
    }
   if (!g && last(2))
    break;
  }}
  


 }


//////	keyboard interface

function keyD(ev)	//check for arrow keys
 {
// cacheDisplay();	 
 var key=document.all?window.event.keyCode:ev.which;
 var w=this;
 var i=w.i, j=w.j;
 switch (key) {
  case 35: i=size[2]-1; j=size[2]-1; break;
  case 36: i=0; j=0; break;
  case 37: j=(0<j)?(j-1):0; break;
  case 38: i=(0<i)?(i-1):0; break;
  case 39: j=(j<size[2]-1)?(j+1):size[2]-1; break;
  case 40: i=(i<size[2]-1)?(i+1):size[2]-1; break;
  case 8:  ;
  case 32: ;
  case 45: ;
  case 46: w.value=''; afterKey(this);
  default: break;
  }
 xy[i][j].g.focus();
 if (i==w.i && j==w.j)
  w.className='cell';
 tip.value='';
 // setTimeout("cacheDisplay()",7000);  alert("cache display 4");
 return true;
 }

function keyP(ev)	//check for the last manual entry's validity
 {
 var key=document.all?window.event.keyCode:ev.which;
 if (full.indexOf(String.fromCharCode(key))<0)
  return false; 
 this.select();
 if (this.style.color=='gray')
  this.style.color='#1276C0';
 this.value=String.fromCharCode(key);

 afterKey(this);
 last(3); 
 return true; 
 }

function afterKey(w)
 {
 var i=w.i, j=w.j, k, m, n, o=new Array(3), s;
 k=xy[i][j].k;
 o[0]=xyz[0][i]; o[1]=xyz[1][j]; o[2]=xyz[2][k];
 if(fU89==1){
 ct=w.value;
 getUpEnd(i,j,ct);
 
	 
 }
 for (n=0;n<3;n++)
  {
  o[n].h=full;
  for (m=0;m<size[2];m++)
   {
   s=o[n].c[m].g.value;
   
   if (-1<o[n].h.indexOf(s))
    o[n].h=o[n].h.del(s);
   else if (s==w.value)
    {
    tip.value+=str[8]+str[5+n]+' '+(n==0?xName[i]:n==1?yName[j]:zName[k])+'.\n'; 
	if(n==0){coord(xName[i]);}
	if(n==1){coord(yName[j]);}
	if (fU89==0){
	show(tip.value,"#fff");
    w.className='cell_erreur';
	}
	



 }}}}


//////	buttons

function howTo()
 {alert(str[0]+str[1]); alert(str[2]);}


 function starter_(grille)
 {

nb_start=1;
 //setTimeout("cacheDisplay()",200); 
// alert("niveau dans start"+niveau);
 var i, j, s=grille, l=shakeRegion(size[0]), m=shakeRegion(size[1]);
 var n=full.shake(), o, p=(Math.round(Math.random())==0), q='';

 if (s==null || s=='') return;
 ''.toGrid();
 if (s==0) eat('store').toGrid();
 if (s.length>1) s.toGrid();
// alert("start"+s);
 switch (s) {
  case '1': o=easy;	break;
  case '2': o=medium;	break;
  case '3': o=hard;	break;
  case '4': o=diabolic;	break;
  default:return;}
 o=o[Math.floor(Math.random()*(o.length-.001))];
 for (i=0;i<size[2];i++)
  for (j=0;j<size[2];j++)
   {
   s=o.charAt(size[2]*l[(p?i:j)]+m[(p?j:i)]);
   q+=(s=='-')?'-':n[s-1];
   }
 q.toGrid();
 //d1=0;
 store();
 }


function start_()
 {
charge=0;
fermerTousMeilleursTemps();
solveur=0;
la_solution=0;
nb_indice=0;

if(nb_start>0&fU89==0){
ouvrirPub();
}
nb_start=1;
 //setTimeout("cacheDisplay()",200); 
// alert("niveau dans start"+niveau);
 var i, j, s=niveau, l=shakeRegion(size[0]), m=shakeRegion(size[1]);
 var n=full.shake(), o, p=(Math.round(Math.random())==0), q='';

 
 if (s==null || s=='') return;
 ''.toGrid();
 if (s==0) eat('store').toGrid();
 if (s.length>1) s.toGrid();
// alert("start"+s);
 switch (s) {
  case '1': o=easy;	break;
  case '2': o=medium;	break;
  case '3': o=hard;	break;
  case '4': o=diabolic;	break;
  default:return;}
 o=o[Math.floor(Math.random()*(o.length-.001))];
 for (i=0;i<size[2];i++)
  for (j=0;j<size[2];j++)
   {
   s=o.charAt(size[2]*l[(p?i:j)]+m[(p?j:i)]);
   q+=(s=='-')?'-':n[s-1];
   }
 q.toGrid();
 //d1=0;
 store();
  
  
	  grille_debut=fromGrid();
	

  
 }

function store()	//save the initial grid into the cookie
 {
 cook('store',fromGrid(),60)
 guess=new Array();
 //chronoOn(); 
 }

function clr()
 {
if(fU89==0){
  if (solveur==1){
   solveur=0 ;
  } else {
   solveur=1; 
  }
  
 show("Remplissez vous même les cases du Sudoku et cliquez sur LA SOLUTION pour obtenir une réponse possible.","#fff");
 ''.toGrid();
 //d1=0;
 guess=new Array();
 //chronoStop();
}
else{
show("Le solveur n'est pas accessible en multijoueur.","#fff");	
}
 }

function save(cache)	//save the current grid into the cookie
 {
 var cache;


	cook('save_user',fromGrid(),60) 
   
  show(" Grille sauvegardée ...","#fff");
 

 }

function load(x)	//load the grid from the cookie
 {
 
 charge=1;

 show(" Grille chargée  ...","#fff");
 // setTimeout("cacheDisplay()",3000);


	
	 eat('save_user').toGrid();
	  la_solution=0;
	  nb_indice=0;
	 
grille_debut=fromGrid();
// d1=eat('d1');

 guess=new Array();
 //chronoOn();
 }


/////////////////////////////////////// GUI Functions ////////////////////////////////////
//Display the given data in the said colour 
var timer;
function show(data,color) {
	clearTimeout(timer);
	document.getElementById("display_area").className="display_area";
	document.getElementById("display_area").innerHTML = data + "<br />\n";
	document.getElementById("display_area").style.color = color;
	timer = setTimeout("cacheDisplay()",7500); 
	
}

var timer2;
function coord(span) {
	if(fU89==0){
	clearTimeout(timer2+span);
	document.getElementById("coord"+span).className="affiche";
	timer2= setTimeout("cacheCoord()",7500);
	}
}

function cacheCoord() {
	document.getElementById("coord1").className="cache";
	document.getElementById("coord2").className="cache";
	document.getElementById("coord3").className="cache";
	document.getElementById("coord4").className="cache";
	document.getElementById("coord5").className="cache";
	document.getElementById("coord6").className="cache";
	document.getElementById("coord7").className="cache";
	document.getElementById("coord8").className="cache";
	document.getElementById("coord9").className="cache";
	document.getElementById("coordA").className="cache";
	document.getElementById("coordB").className="cache";
	document.getElementById("coordC").className="cache";
	document.getElementById("coordD").className="cache";
	document.getElementById("coordE").className="cache";
	document.getElementById("coordF").className="cache";
	document.getElementById("coordG").className="cache";
	document.getElementById("coordH").className="cache";
	document.getElementById("coordI").className="cache";
	
}

//Change the background color of cells to white
function discolorCells(cell1,cell2) {
	document.getElementById(cell1).className="cell";
	document.getElementById(cell2).className="cell";
	document.getElementById("display_area").className="display_area_cache";
}

function cacheDisplay() {
	document.getElementById("display_area").className="display_area_cache";
}

function difficult() {	
 //alert ("difficult "+niveau);
	 if(niveau=='1'){
		document.getElementById("niveau").className="moyen";
		niveau='2';
		show("Difficulté : MOYEN</font>","#fff");
		
	}
	else if(niveau=='2'){
		document.getElementById("niveau").className="difficile";
		niveau='3';
		show("Difficulté : DIFFICILE","#fff");
	}
	else if(niveau=='3'){
		document.getElementById("niveau").className="diabolique";
		niveau='4';
		show("Difficulté : DIABOLIQUE","#fff");
	}
	else if(niveau=='4') {
		document.getElementById("niveau").className="facile";
		niveau='1';
		show("Difficulté : FACILE","#fff");
	}
start_();
// alert("fin difficult "+niveau);
 }







