var qc=-1,rc=-1;var sc;function custom_change_focus(r,c){if(rc>=0){var tc=document.getElementById('celltbl'+qc+rc);tc.style.borderColor="";}
var uc=document.getElementById('celltbl'+r+c);uc.style.borderColor=oc(C_highlight);qc=r;rc=c;}
function vc(e){var bc;if(!e)e=window.event;if(e.keyCode)bc=e.keyCode;else if(e.which)bc=e.which;if(e.keyCode==K_backspace&&e.srcElement.tagName!="INPUT"&&e.srcElement.tagName!="TEXTAREA"){event.cancelBubble=true;event.returnValue=false;}
var k=ac(bc,false);if(k=="left"&&rc>0)custom_change_focus(qc,rc-1);else if(k=="right"&&rc<8)custom_change_focus(qc,rc+1);else if(k=="up"&&qc>0)custom_change_focus(qc-1,rc);else if(k=="down"&&qc<8)custom_change_focus(qc+1,rc);else if(k=="space"||k=="0"||k=="del"||k=="backspace"){wc(qc,rc,0,true);sc[qc][rc]=0;custom_change_focus(qc,rc);}
else if(k.charAt(0)>='1'&&k.charAt(0)<='9'){var kc=parseInt(k);var f=xc(qc,rc,kc);if(f==null){wc(qc,rc,kc,true);sc[qc][rc]=kc;custom_change_focus(qc,rc);}
else{var uc=document.getElementById('celltbl'+f[0]+f[1]);yc(1,uc,["","#cc4444"]);}
}
}
function xc(r,c,n){for(var i=0;i<DIM;i++){if(sc[i][c]==n&&i!=r)return[i,c];if(sc[r][i]==n&&i!=c)return[r,i];}
var zc=State.BOXES[r]*3;var $c=State.BOXES[c]*3;for(var j=zc;j<zc+BOX;j++)for(var k=$c;k<$c+BOX;k++)if(sc[j][k]==n&&(j!=r||k!=c))return[j,k];return null;}
function _c(){pause(true);qc=-1;rc=-1;sc=State.create_grid();ad=true;bd.content=g("enter-puzzle-box");bd.set_position_obj(AUTO,g("celltbl08"),RIGHT,TOP);bd.manifest();g("right-column").style.visibility="hidden";g("tab-box").style.visibility="hidden";g("time").style.visibility="hidden";cd("puzzle",true);custom_change_focus(0,0);rb();if(document.addEventListener){document.addEventListener('keydown',vc,false);}
else{document.onkeydown=vc;}
}
function dd(){g("right-column").style.visibility="visible";g("tab-box").style.visibility="visible";g("time").style.visibility="visible";ad=false;if(document.addEventListener){document.removeEventListener('keydown',vc,false);}
else{document.onkeydown=null;}
qb();}
function ed(fd,gd,hd,id,jd,kd){if(!fd){alert("Unable to contact server.");ld();return;}
if(!gd){if(!confirm("This puzzle has no solutions. Sorry! Click 'OK' to edit this puzzle, or 'Cancel' to abort.")){ld();}
else{md.hide();bd.manifest();}
return;}
if(!hd){if(!confirm("This puzzle is so difficult that only limited hint information will be available. Alternatively, this puzzle may have multiple solutions, in which case hints and undos may not work correctly. Click 'OK' to play the puzzle or 'Cancel' to continue editing.")){md.hide();bd.manifest();return;}
}
md.hide();dd();nd("This puzzle is "+kd);od=kd;var pd=qd(rd);if(pd==null)pd=ADVANCED;sd=pd;td(id,jd,"CUSTOM");}
function ld(){bd.hide();md.hide();dd();ud();pause();}
function vd(e){md.set_position_obj(AUTO,g("enter-puzzle-box"),LEFT,TOP);md.manifest();bd.hide();var id="";for(var i=0;i<DIM;i++){id+=sc[i].join("");}
id=id.replace(/0/g,' ');wd(id,ed);}
