// JavaScript Document
//globals 
var g_bg_color1="white";
var g_bg_color2="#DDD";
var g_colorb="#28b";
var g_width=35;
var g_height=35;

var undo_tab=new Array();
var undo_ptr=0;
/*var timer_ticks=false;
var check_sem=false;*/
var key_down=false;
var active;
var solution;
//var old_grid; //needed when user grid

var sav_givens;
var sav_user;
var sav_undotab;
var sav_undoptr;
var sav_solution;


function undo_push( id, val ){
  //alert( "Val="+val );
  if( !val )
    val="";
  l=undo_tab.length;
  if( undo_ptr < l ){
    undo_tab.length=undo_ptr;
    }
  undo_tab.push( id+"-"+val );
  undo_ptr++;
  UndoRedoButton();
  } 
  
function undo_clear(){  
  undo_tab.length=0;
  undo_ptr=0;
  UndoRedoButton();
  }
  
function fUndo(){
  if( undo_ptr>0 ){
    undo_ptr--;
    udata=undo_tab[undo_ptr];
    uid=udata.substring( 0,3 );
    uval=udata.substring( 4, 5 );
    ufld=document.getElementById( uid );
    undo_tab[undo_ptr]=uid+"-"+ufld.value; //zeby redo
    ufld.value=uval;
    }
   UndoRedoButton();
   ufld.focus();
  }
  
function fRedo(){
  l=undo_tab.length;
  if( undo_ptr<l ){
    udata=undo_tab[undo_ptr];
    uid=udata.substring( 0,3);
    uval=udata.substring( 4,5);
    ufld=document.getElementById( uid );
    undo_tab[undo_ptr]=uid+"-"+ufld.value; //zeby undo
    ufld.value=uval;
    undo_ptr++;
    }
  UndoRedoButton();
  ufld.focus();
  }
  
function fUser(){
  saveGame();
  solution="";
  setGrid("");
  bkg=document.getElementById("userbkg");
  frg=document.getElementById("userfrg");
  
  bkg.style.display="block";
  frg.style.display="block"; 
  sda=document.getElementById("sday");
  sda.setAttribute( "disabled", true ); 
  
  }
  
function getGivens(){
  ans="";
  for( i=0; i<9; i++ ){
      for( j=0; j<9; j++ ){
        ix="i"+j+i;
        fld=document.getElementById( ix );
        if( (!fld.value || fld.value==" ") || !fld.given )
          val="0";
        else
          val=fld.value;
        ans+=val;
        }
      }
    return ans;
    }
    
function getUser(){
  ans="";
  for( i=0; i<9; i++ ){
      for( j=0; j<9; j++ ){
        ix="i"+j+i;
        fld=document.getElementById( ix );
        if( (!fld.value || fld.value==" ") || fld.given )
          val="0";
        else
          val=fld.value;
        ans+=val;
        }
      }
    return ans;
    }
  
function saveGame(){
  sav_givens=getGivens();
  sav_user=getUser();
  sav_undotab=undo_tab;
  sav_undoptr=undo_ptr;
  sav_solution=solution;
  }

function restoreGame(){
  setGrid( sav_givens, sav_user );
  undo_ptr=sav_undoptr;
  undo_tab=sav_undotab;
  solution=sav_solution;
  UndoRedoButton();
  }
  
function fPrint(){
  bkg=document.getElementById("printbkg");
  frg=document.getElementById("printfrg");
  
  bkg.style.display="block";
  frg.style.display="block"; 
  sda=document.getElementById("sday");
  sda.setAttribute( "disabled", true ); 
  }
  
function fClPrint(){
  bkg=document.getElementById("printbkg");
  frg=document.getElementById("printfrg");
  bkg.style.display="none";
  frg.style.display="none"; 
  sda=document.getElementById("sday");
  sda.removeAttribute( "disabled" );
  }
  
function fDoPrint1(){ 
  //alert( 'print' );
  ti=document.getElementById( "ptips" );
  p1=document.getElementById( "print1" );
  hre="sprint.php?sdata="+getGivens();
  if( ti.checked )
    hre+="&ptips=1";
  //alert(hre);
  p1.setAttribute( "href", hre );
  fClPrint();
  window.blur();
  }
  
function fDoPrint2(){
  ti=document.getElementById( "ptips" );
  p2=document.getElementById( "print2" );
  hre="sprint.php?sdata="+getGivens()+"&udata="+getUser();
  if( ti.checked )
    hre+="&ptips=1";
  //alert(hre);
  p2.setAttribute( "href", hre );
  fClPrint();
  }
  
function getCount(){
  c=0;
  for( i=0; i<9; i++ ){
      for( j=0; j<9; j++ ){
        ix="i"+j+i;
        fld=document.getElementById( ix );
        if( fld.value && fld.value!=" " )
          c++;
        }
      }
  return c;
  }
  
function fDone(){
  if( getCount()<16 ){
    alert( jsltab[2]);
    return;
    }
  bkg=document.getElementById("userbkg");
  frg=document.getElementById("userfrg");
  bkg.style.display="none";
  frg.style.display="none";
  sda=document.getElementById( "sday" );
  sda.removeAttribute( "disabled" ); 
  sdr=document.getElementById( "sd_r" );
  if( sdr ){
    sda.removeChild( sdr );
    }
  opt=document.getElementById( "sd_c" );
  if( !opt ){
    opt=document.createElement( "<option>" );
    opt.setAttribute( "selected", true );
    opt.setAttribute( "id", "sd_c" );
    opt.innerText="Custom";
    opt.textContent="Custom";
    sda.appendChild( opt );
    }
  lockGrid();  
  }
  
function fClUser(){
  restoreGame();
  bkg=document.getElementById("userbkg");
  frg=document.getElementById("userfrg");
  bkg.style.display="none";
  frg.style.display="none";
  sda=document.getElementById( "sday" );
  sda.removeAttribute( "disabled" ); 
  }
  
  
function fIbox( v ){
  }
  
function UndoRedoButton(){
  ub=document.getElementById( "undob" );
  rb=document.getElementById( "redob" );
  sb=document.getElementById( "startb" );
  if( undo_ptr==0 )
    {
    ub.setAttribute( "disabled", true );
    sb.setAttribute( "disabled", true );
    }
  else{
    ub.removeAttribute( "disabled", 0 );
    sb.removeAttribute( "disabled", 0 );
    }
  if( undo_ptr<undo_tab.length ){
    rb.removeAttribute( "disabled" );
    }
  else{
    rb.setAttribute( "disabled", true );
    }   
  }
  
function buildGrid( grid ){
    c=0;
    for( i=0; i<9; i++ ){
      for( j=0; j<9; j++ ){
        createField( j,i, grid.substr(c,1) );
        c++;
        }
      }
    setHandlers();
    //buildNPad();
    }
  
function setGrid( givens, user ){
  if( !user )
    user="";
  c=0;
    for( i=0; i<9; i++ ){
      for( j=0; j<9; j++ ){
        ix="i"+j+i;
        fld=document.getElementById( ix );
        g=givens.substr(c,1);
        u=user.substr(c,1);
        if( g && g!=0)
          fld.value=g; 
        if( u && u!=0)
          fld.value=u;
        if( u==0 && g==0 )
          fld.value="";
       if( g && g!=0 )
          setStyle( fld, true );
        else
          setStyle( fld, false );
        c++;
        }
      }
  UndoRedoButton();
  }
  
function setStyle( fld, given ){
  s=fld.style;
  if( given ){
    fld.given=true;
    s.fontWeight="bold";
    s.fontSize="20px";
    s.color="black";
    }
  else {
    fld.given=false;
    s.fontWeight="normal";
    s.fontSize="18px";
    s.color="#28b";
    }
  }
  
function restartGrid(){
    for( i=0; i<9; i++ ){
      for( j=0; j<9; j++ ){
        ix="i"+j+i;
        fld=document.getElementById( ix );
        if( !fld.given )
          fld.value="";
        }
      }
    undo_clear();
  }
  
function lockGrid(){
  c=0;
    for( i=0; i<9; i++ ){
      for( j=0; j<9; j++ ){
        ix="i"+j+i;
        fld=document.getElementById( ix );
        if( fld.value && fld.value!=" " ){
          setStyle( fld, true );
          }
        else
          fld.style.color="#28b";
        c++;
        }
      }
    undo_clear();
  }
  
function fSolve(){
  if( !confirm( jsltab[0] ) )
      return;
  if( !solution ){
    //alert( "No Solution" );
    document.getElementById('load').style.display="block";
    document.forms[0].sdata.value=getGivens();
    document.forms[0].acct.value="solve";
		document.forms[0].submit(); 
    return;
    }
  setSolution();
  }
  
function setSolution(){
  //alert( "setting solution" );
  c=0;
  for( i=0; i<9; i++ ){
      for( j=0; j<9; j++ ){
        ix="i"+j+i;
        fld=document.getElementById( ix );
        fld.value=solution.substr(c,1);
        c++;
        }
      }
   undo_clear();
   document.getElementById( "startb" ).removeAttribute( "disabled" );
  }
      
function fTip(){
  if( !solution ){
    alert( jsltab[1] );
    return;
    }
  if( !confirm( jsltab[0] ) )
      return;
  if( !active ){
    alert( jsltab[3] );
    return;
    }
  idx=active.getAttribute("id");
  n=parseInt(idx.substr(1,1))+parseInt(9*idx.substr(2,1));
  undo_push( idx, active.value );
  active.value=solution.substr(n,1);    
  }
  
function buildNPad(){
  cnt=document.getElementById("npad");
  w=g_width;
  h=g_height;
  for( i=0;i<10;i++ ){
    fld=document.createElement("button");
    if( i<9 ){
    fld.innerText=i+1;
    fld.textContent=i+1;
    }
    else{
    fld.innerText="Del";
    fld.textContent="Del";
    }
    
    s=fld.style;
    s.backgroundColor=g_colorb;
    s.color="black";
    s.display="inline";
	  s.position="absolute"
	  s.borderWidth="1px";
	 // s.borderTopWidth="0px";
	  s.borderTopColor=s.borderLeftColor="white";
	  s.borderBottomColor=s.borderRightColor="black";
		s.borderStyle="solid";
		//s.textAlign="center";
		//s.paddingTop="5px";
		//s.paddingBottom="-5px";
		s.fontSize="18px"
		s.fontFamily="tahoma";
		s.fontWeight="normal";
		s.cursor="pointer";
		//s.padding="0px";
		//s.verticalAllign="middle";
		
		t=i*(h-1);
		s.left=0;
		s.top=t;
	  s.width=w;
		s.height=h-1;
		cnt.appendChild( fld );
		}
  }  
    
function createField( x, y, v ){
    w=g_width;
    h=g_height;
    fld=document.createElement("input");
    fld.setAttribute( "id", "i"+x+y, 0 );
    if( v && v!="0" ){
      fld.value=v;
      fld.given=true;
      }
    bkg=g_bg_color1;
    if( ((x < 3 || x >5) && (y <3 || y > 5 )) )
        bkg=g_bg_color2;
    if( ( x >2 && x <6 ) && (y >2 && y <6 ) )
        bkg=g_bg_color2;
    s=fld.style;
    s.backgroundColor=bkg;
    s.display="inline";
		s.position="absolute"
		s.borderWidth="1px";
		if( x%3==0 )
		  s.borderLeftWidth="2px";
		if( x==8 )
		  s.borderRightWidth="2px";
		if( y%3==0 )
		  s.borderTopWidth="2px";
		if( y==8 )
		  s.borderBottomWidth="2px";
		s.borderColor=g_colorb;;
		s.borderStyle="solid";
		s.textAlign="center";
		s.paddingTop=5;
		s.fontSize="20px"
		s.fontFamily="tahoma";
		if( v!=0 )
		  s.fontWeight="bold";
		else{
		  s.fontSize="18px";
		  s.color="#28b";
		  }
		l=(x%9)*(w-1);
		t=(y%9)*(h-1);
		s.left=l;
		s.top=t;
	  s.width=w;
		s.height=h;
		document.getElementById("sboard").appendChild( fld );
    }
    
function setHandlers(){
				 for( i=0;i<9;i++)
				 			for(j=0;j<9;j++){
									idi="i"+i+j;
									obj=document.getElementById(idi);
									if( obj ){	
									   obj.onfocus=fFocus;
									   obj.onkeyup=fOnKeyUp;
									   obj.onkeydown=fOnKeyDown;
									   if (obj.captureEvents) obj.captureEvents(Event.ONFOCUS|Event.ONKEYUP|Event.ONKEYDOWN);
									}
								}
						}
function fFocus(e){
  active=(!e ? window.event.srcElement : e.target );
  if( active.given ){
    np_enall( false, 4 );
    }
  else
    np_enall( true, 4 );
  }
function fOnKeyDown(e){
  if( key_down )
    return false;
  fld=(!e ? window.event.srcElement : e.target );
  if( !e )e=window.event;
  k=e.keyCode;
  key_down=k;
  return keyDown(fld,k);
  //alert(k);
/* if( (k>=37 && k<=40)){ //arrows
  return true;
  }
 if( k==8 || k==32 ){
  fld.p_value=fld.value;
  if( fld.given )
    return false;
  return true;
  }
 if( k==9 ){
    return true;
    }
if( (k<=48 || k>57) && k!=32 && (k<97 || k>105)){ //keys
    return false;
    }
  if( fld.given )
    return false;
  fld.p_value=fld.value;
  fld.value="";
  return true;*/
  }
  
function keyDown(fld,k){
  key_down=k;
  if( (k>=37 && k<=40)){ //arrows
  return true;
  }
 if( k==8 || k==32 ){
  fld.p_value=fld.value;
  if( fld.given )
    return false;
  return true;
  }
 if( k==9 ){
    return true;
    }
if( (k<=48 || k>57) && k!=32 && (k<97 || k>105)){ //keys
    return false;
    }
  if( fld.given )
    return false;
  fld.p_value=fld.value;
  fld.value="";
  return true;
  }
  
function np_enall( enable ){
  for( i=0; i<=9; i++ ){
    np_enable( enable, i );
    }
  }
  
function np_enable( enable, what ){
  np=document.getElementById( "np"+what );
  if( !np )
    return;
  if( enable ){
    np.removeAttribute( "disabled" );
    np.style.color="#ee0";
    np.style.cursor="pointer";
    }  
  else{
    np.setAttribute( "disabled", true );
    np.style.color="#DDD";
    np.style.cursor="default";
    }
  }
  
function fOnKeyUp(e){
  q_fld=(!e ? window.event.srcElement : e.target );
  if( !e )e=window.event;
  k=e.keyCode;
  if( key_down!=k )
    return false;
  keyUp(q_fld,k);
 /* id=q_fld.getAttribute("id");
  x=id.substring( 1,2);
  y=id.substring( 2,3);
  
  //new_id="i11";
  if( k==9  ) //tab
    {
    key_down=false;
    return;
    }
  if( k==37 )
    x--;
  if( k==38 )
    y--;
  if( k==39 )
    x++;
  if( k==40 )
    y++;
  if( x<0 )x=8;
  if( y<0 )y=8;
  if( x>8 )x=0;
  if( y>8 )y=0;
  if( k==32 && !q_fld.given ){
    q_fld.value="";
    //alert( "spacja="+q_fld.p_value);
    if( q_fld.p_value )
      undo_push( id, q_fld.p_value );
    }
  if( ((k>48 && k<=57) || (k>=97 && k<=105)) && !fld.given){
    if( k>=97 )key=k-96; else key=k-48;
    if( !checkField( q_fld, key ) ){
      flashFields();
      }
    else
      undo_push( id, q_fld.p_value );
    }
  new_id="i"+x+""+y;
  document.getElementById( new_id ).focus();
  key_down=false;*/
  }
function keyUp(q_fld,k){
  id=q_fld.getAttribute("id");
  x=id.substring( 1,2);
  y=id.substring( 2,3);
  //new_id="i11";
  if( k==9  ) //tab
    {
    key_down=false;
    return;
    }
  if( k==37 )
    x--;
  if( k==38 )
    y--;
  if( k==39 )
    x++;
  if( k==40 )
    y++;
  if( x<0 )x=8;
  if( y<0 )y=8;
  if( x>8 )x=0;
  if( y>8 )y=0;
  if( k==32 && !q_fld.given ){
    q_fld.value="";
    //alert( "spacja="+q_fld.p_value);
    if( q_fld.p_value )
      undo_push( id, q_fld.p_value );
    }
  if( ((k>48 && k<=57) || (k>=97 && k<=105)) && !q_fld.given){
    if( k>=97 )key=k-96; else key=k-48;
    if( !checkField( q_fld, key ) ){
      flashFields();
      }
    else
      undo_push( id, q_fld.p_value );
    }
  new_id="i"+x+""+y;
  document.getElementById( new_id ).focus();
  key_down=false;
  }

var check_tab=new Array();
function checkField( fld, v ){
  id=fld.getAttribute("id");
  x=id.substring( 1,2);
  y=id.substring( 2,3);
  for( i=0; i<9; i++ ){
    if( i==x )
      continue;
    idc="i"+i+""+y;
    fld2=document.getElementById( idc );
    if( v==fld2.value ){
     check_tab.push( fld2 );
      }
      }
    for( i=0; i<9; i++ ){
    if( i==y )
      continue;
    idc="i"+x+""+i;
    fld2=document.getElementById( idc );
    if( v==fld2.value ){
     check_tab.push( fld2 );
      }
    }
    sx=3;
		if( x<3 )
			sx=0;
		if( x>5 )
			sx=6;
		sy=3;
		if( y<3 )
			sy=0;
		if( y>5 )
			sy=6;
		for( i=sx;i<sx+3;i++ )
			 for( j=sy;j<sy+3;j++ ){
			 	  if( i!=x && j!=y ){
				  	  idc="i"+i+""+j;
              fld2=document.getElementById( idc );
              if( v==fld2.value )
                check_tab.push( fld2 );
                
					}
			}
    if( check_tab.length!=0 ){
      check_tab.push( fld ); //add checked
      return false;
      }
    return true;
  }
  

function flashFields(){
  for( i=0; i<check_tab.length; i++ ){
    fld=check_tab[i];
    fld.old_bkg=fld.style.backgroundColor;
    fld.style.backgroundColor="red";
    }
    timer_ticks=true;
    setTimeout( "endFlash()", 500 );
  }
  
function endFlash(){
  for( i=0; i<check_tab.length; i++ ){
    fld=check_tab[i];
    fld.style.backgroundColor=fld.old_bkg;
    }
  //last one
  if( i>0 ){
    fld=check_tab[i-1];
    if( fld.p_value || fld.p_value=="" )
      fld.value=fld.p_value;
    }
  check_tab=new Array();
  timer_ticks=false;
  }
  
function gwchange(){
		 document.forms[0].acct.value="gwchange";
		 document.forms[0].submit(); 
		 }
function rando(){
		 document.forms[0].acct.value="rando";
		 document.forms[0].submit(); 
		 }
function np_click(n){
  if( !active ){
    alert( jsltab[3] );
    return;
    }
  //active.innerText=n;
  //x=n+'';
  if( n==0 )
    {
    if( !keyDown(active,32) ){
      key_down=false;
      return;
      }
    keyUp(active,32);
    active.value="";
    }
  else
    {
    if( !keyDown(active,n+48) ){
      key_down=false;
      return;
      }
    keyUp(active,n+48);
    active.value=n;
    }
  //active.focus();
    
  }		 


