var nl;var ol;var pd;var pl;var ql;var rl=false;var sl=false;var ak,oj,bk;var tl=false;var ul=null;var ik=0;var xh=false;var _g=false;var ad=false;function vl(){ul=null;}
function wl(){ik++;return;var a=ik;if(State.has_won())return;var f=function(){if(a!=ik)return;var xl=yi('hinting_possibles');yl(xl,null,null);}
setTimeout(f,60000);}
function zl(){if(State.has_emph()){State.set_emph([],[]);$l(true);}
if(tl){_l();}
}
function change_focus(r,c){if(nl==r&&ol==c)return;var am=nl;var bm=ol;nl=r;ol=c;if(am!=-1)cm(am,bm,true);cm(nl,ol,true);zl();dm=false;}
function cm(r,c,em){var uc=document.getElementById('celltbl'+r+c);var fm=State.background_color(r,c);var gm=State.border_color(r,c);if(r==nl&&c==ol){uc.style.borderColor=oc(C_highlight);set_poss_highlight(r,c);}
else{uc.style.borderColor=oc(gm);if(em&&!State.is_number(r,c)){redraw_posses_cell(r,c);}
}
setTimeout(function(){hm(r,c);}
,600);uc.style.background=oc(fm);}
function $l(em){for(var i=0;i<DIM;i++)for(var j=0;j<DIM;j++)cm(i,j,em);}
function im(r,c){for(var i=0;i<DIM;i++){cm(r,i,true);cm(i,c,true);}
var jm=State.BOXES[r]*BOX,km=State.BOXES[c]*BOX;for(var j=0;j<BOX;j++)for(var k=0;k<BOX;k++)cm(jm+j,km+k,true);}
function redraw_posses_cell(r,c){if(State.is_number(r,c))return;for(var k=1;k<DIM+1;k++){var e=document.getElementById("poss"+r+c+k);if(State.is_visibly_possible(r,c,k)){e.style.visibility='';}
else{if(e==null)alert("at pos "+r+","+c+","+k);e.style.visibility='hidden';}
e.style.color=oc(C_foreground);if(e.className!="celltd"){e.onmouseover=null;e.onmouseout=null;e.onclick=null;e.className="celltd";}
}
}
function lm(){for(var i=0;i<DIM;i++)for(var j=0;j<DIM;j++)if(!State.is_number(i,j))redraw_posses_cell(i,j);}
function set_poss_highlight(r,c){if(State.is_number(r,c))return;var pc=oc(State.background_color(r,c));for(var k=1;k<DIM+1;k++){var df=document.getElementById("poss"+r+c+k);mm(r,c,pc,k,df);df.className="celltdhigh";df.style.visibility='';if(State.is_visibly_possible(r,c,k)){df.style.color=oc(C_foreground);}
else{df.style.color=pc;}
}
}
function nm(r,c,n){if(!State.is_explicitly_possible(r,c,n))om(r,c,n);else
pm(r,c,n);}
function om(r,c,n){var qm=document.getElementById("poss"+r+c+n);if(!State.set_explicitly_possible(r,c,n)){c=State.constraint_conflict(r,c,n);nd("There\'s already "+gc[n-1]+" in this "+c+", so it can't go here.");return;}
if(qm)qm.style.color=oc(C_foreground);cm(r,c,false);}
function pm(r,c,n){var qm=document.getElementById("poss"+r+c+n);if(!State.set_explicitly_impossible(r,c,n))return;if(qm)qm.style.color=oc(State.background_color(r,c));cm(r,c,false);}
function jl(){pl=[];if(ug["autofill"]){ql=(new Date()).getTime();rm();setTimeout(sm,300);}
}
function hm(r,c){if(State.is_number(r,c))return;var n;if(sd==ADVANCED){n=State.get_singleton_possibility(r,c);}
else{n=State.get_singleton_possibility(r,c);if(n){if(!State.is_only_empty_square_in_some_dimension(r,c))return;}
}
if(n!=0){pl.push([r,c,n]);}
}
function rm(){for(var i=0;i<DIM;i++)for(var j=0;j<DIM;j++)hm(i,j);}
function sm(){var a;if(!ug["autofill"])return;setTimeout(sm,300);if(ad||zd)return;var _f=(new Date()).getTime();if(ql+200>_f)return;ql=_f;do{if(pl.length==0)return;a=pl.pop();}
while(State.is_number(a[0],a[1]));State.disable_state_changed_callbacks=true;if(!tm(a[0],a[1],a[2])){nd("Autofill has detected an incorrect solution; stopping.");change_focus(a[0],a[1]);pl=[];}
State.disable_state_changed_callbacks=false;xf();}
function mm(r,c,pc,kc,qm){if($b){qm.onmouseover=function(e){ei(e);this.style.background="#cccccc";}
;qm.onmouseout=function(e){ei(e);this.style.background="";}
;}
else{qm.onmouseover=ei;qm.onmouseout=ei;}
qm.onclick=function(e){State.checkpoint(false);nm(r,c,kc);}
;qm.oncontextmenu=function(e){change_focus(r,c);return false;}
;qm.ondblclick=function(e){State.checkpoint(false);tm(r,c,kc);}
;}
function um(r,c){if(State.is_number(r,c)){var n=State.get_number(r,c);wc(r,c,n);}
else{bf(r,c);}
}
function vm(n,r,c){if(n<1||n>=COLORS.length)return;State.set_user_color(r,c,n);cm(r,c,false);}
function vf(n){zl();if(n<1||n>9)return;State.set_coloring(n);xf();$l(true);}
function _l(){tl=false;State.clear_user_colors();$l(true);}
function tm(r,c,n){if(State.get_number(r,c)==n)return false;if(!State.is_implicitly_possible(r,c,n)){c=State.constraint_conflict(r,c,n);nd("There\'s already "+gc[n-1]+" in this "+c+", so it can't go here.");return false;}
if(!State.set_number(r,c,n))return;wc(r,c,n);im(r,c);if(State.has_won()){vj();}
return true;}
function wm(r,c){var kc=State.get_number(r,c);State.set_number(r,c,0);State.set_explicitly_possible(r,c,kc);bf(r,c);im(r,c);}
function rb(){xm();}
function qb(){ym();}
function zm(e){if(!e)e=window.event;if(pd==M_color){pd=M_normal;}
}
function $m(e){if(sl)return;var bc;if(!e)e=window.event;if(e.keyCode)bc=e.keyCode;else if(e.which)bc=e.which;if(ge)ge.hide();if(e.keyCode==K_backspace&&e.srcElement.tagName!="INPUT"&&e.srcElement.tagName!="TEXTAREA"){event.cancelBubble=true;event.returnValue=false;}
if(e.shiftKey)pd=M_color;var k=ac(bc,pd==M_color);if(k=="left"&&ol>0)change_focus(nl,ol-1);else if(k=="right"&&ol<8)change_focus(nl,ol+1);else if(k=="up"&&nl>0)change_focus(nl-1,ol);else if(k=="down"&&nl<8)change_focus(nl+1,ol);else if(k=="dash"||k=="slash")pd=M_subtract;else if(k=="eq"||k=="plus"){pd=M_add;}
else if((k=="space"||k=="del"||k=="backspace")&&State.is_number(nl,ol)&&!State.is_constant(nl,ol)){State.checkpoint(false);wm(nl,ol);}
else if(k=="B")vm(1,nl,ol);else if(k=="G")vm(2,nl,ol);else if(k=="R")vm(3,nl,ol);else if(k=="Y")vm(4,nl,ol);else if(k=="O")vm(5,nl,ol);else if(k=="V")vm(6,nl,ol);else if(k=="S")vm(7,nl,ol);else if(k=="T")vm(8,nl,ol);else if(k=="C")_l();else if(k=="P")pause();else if(k=="U")cj();else if(k=="F"){if(State.get_coloring()>0&&!State.is_constant(nl,ol)){State.checkpoint(false);tm(nl,ol,State.get_coloring());}
}
else if(k.charAt(0)>='1'&&k.charAt(0)<='9'){var kc=parseInt(k);if(pd==M_color){pd=M_normal;vf(kc);return false;}
else{if(State.is_constant(nl,ol)){}
else if(pd==M_normal){if(dm&&(State.is_number(nl,ol)||!State.is_explicitly_possible(nl,ol,kc))){State.checkpoint(false);wm(nl,ol);om(nl,ol,kc);}
else{State.checkpoint(false);if(tm(nl,ol,kc)){if(sd==SIMPLE)dm=true;}
}
}
else if(pd==M_subtract){State.checkpoint(false);pm(nl,ol,kc);pd=M_normal;}
else if(pd==M_add){State.checkpoint(false);if(State.is_number(nl,ol))wm(nl,ol);om(nl,ol,kc);pd=M_normal;}
}
}
;return false;}
function $i(){if(!confirm("Are you sure you want to restart this puzzle?"))return;var jd=State.grid_to_string(State.solution_grid,true),pk=State.grid_to_string(State.constants_grid,true);jk(pk,jd,State.get_tag());}
function ud(){pl=[];cd("puzzle");lm();var r=nl,c=ol;nl=ol=-1;$l(false);change_focus(r,c);}
function bj(){if(!confirm("Your board will be reverted to the last time it was correct. Are you sure you want to undo your mistakes?"))return;var _m=State.undo_to_last_correct();if(_m){nd("Board reverted to last correct move.");bk++;}
else{nd("Good work:<br>Everything is correct so far.");}
}
function cj(){var _m=State.undo_last_user_move();if(_m){nd("Last move undone.");}
else{nd("No previous move to undo!");}
}
function an(t){sd=t;State.reset_possibles();lm();set_poss_highlight(nl,ol);gh(rd,sd,300);jl();tf();}
function switch_mode(t){var $e;if(t==SIMPLE)$e="simple_mode_radio";else
$e="advanced_mode_radio";var fb=document.getElementById($e);fb.selected=true;fb.blur();if(t==sd)return;if(confirm("All explicit candidates will be discarded! Continue?")){an(t);}
else{tf();}
lg();}
function set_difficulty(kd){if(ik>0&&!State.has_won()&&!confirm("Are you sure you want to start a new puzzle?"))return;bn(kd);setup_from_server();change_focus(0,0);}
function cn(f,dn){rl=true;if(screen.width<1024||screen.height<700){alert("Sudoku Slam works best with a browser that's over 1000 pixels wide and 500 pixels high. It may not display properly with your screen"+" resolution of "+screen.width+"x"+screen.height+". Sorry!");}
else{var en=(document.compatMode&&document.compatMode!="BackCompat")?document.documentElement:document.body?document.body:null;var wb=(en&&en.clientWidth)?en.clientWidth:window.innerWidth;var xb=(en&&en.clientHeight)?en.clientHeight:window.innerHeight;var w=(1010-wb);var h=(500-xb);if(w>0||h>0)window.resizeBy((w>0?w:0),(h>0?h:0));wb=(en&&en.clientWidth)?en.clientWidth:window.innerWidth;xb=(en&&en.clientHeight)?en.clientHeight:window.innerHeight;if(wb<1010||xb<500){alert("Sudoku Slam works best with a browser that's over 1000 pixels wide and 500 pixels high. You may want to maximize your browser.");}
}
kl();var fn=qd("welcome_done");if(location.search.indexOf("welcome=false")>=0){fn=true;}
else if(location.search.indexOf("welcome=true")>=0){fn=null;}
if(fn==null){if(dn){xh=confirm("Would you like to take a 60-second tour of Sudoku Slam's features?");gh("welcome_done",true,36500);f();}
else{gj(f);}
}
else{f();}
}
function td(gn,jd,hn,dn){var f=function(){jk(gn,jd,hn);}
if(!rl){cn(f,dn);}
else{f();}
}
function jk(gn,jd,hn){if(zd)pause();State.init(gn,jd,hn);ul=null;State.state_changed_callbacks.push(vl);State.state_changed_callbacks.push(wl);State.state_changed_callbacks.push(kg);State.state_changed_callbacks.push(zl);State.state_changed_callbacks.push(xf);ml();lm();mh("_s",function(v){return v+1;}
);pd=M_normal;nl=-1;ol=-1;change_focus(0,0);ak=0;oj=0;bk=0;ik=0;ym();lg();if(xh){zi();}
vh();xh=false;_g=true;if(sd==ADVANCED&&ug["autofill"]&&od==DIFF_EASY){nd("For easy difficulty puzzles, we suggest you either switch to Traditional mode or turn off autofill, as most easy puzzles are trivial with Sumo mode autofill on.");}
else if(od==DIFF_PILEDRIVER){nd("Piledriver alert: You may not be able to get Smart Hints for some steps of this puzzle. Good luck!");}
}
function xm(){if(document.addEventListener){document.removeEventListener('keydown',$m,false);document.removeEventListener('keyup',zm,false);}
else{document.onkeydown=null;document.onkeyup=null;}
}
function ym(){if(document.addEventListener){document.addEventListener('keydown',$m,false);document.addEventListener('keyup',zm,false);}
else{document.onkeydown=$m;document.onkeyup=zm;}
}
function setup_from_server(){if(/bookmark=(\d+)/.exec(location.search)!=null&&!_g){var vk=/bookmark=(\d+)/.exec(location.search)[1];nd("Loading puzzle progress...");qj("load_bookmarks",[["bookmark_ids",vk]],xk);}
else{nd("Please wait while your puzzle loads...");var kd=qd(jn);if(location.search.indexOf("difficulty")>=0&&!_g){kd=/difficulty=(\w+)/.exec(location.search)[1];}
if(kd==null||(kd!=DIFF_EASY&&kd!=DIFF_HARD&&kd!=DIFF_BODYSLAM&&kd!=DIFF_PILEDRIVER))kd=DIFF_MEDIUM;od=kd;var pd=qd(rd);if(pd==null)pd=ADVANCED;sd=pd;var kc=Math.random();qj("new_puzzle",[["uid",_j()],["seed",kc],["localdate",(new Date()).toString()],["difficulty",kd],["mode",pd]],kn);}
}
function bookmark(rj,ln){var mn=yi('is_explicitly_possible');var nn=State.grid_to_string(State.solution_grid);var hn=State.get_tag();var $k=(zd?yd:(new Date()).getTime())-xd;var on=$f($k);var pd=sd;qj("bookmark",[["board",mn],["solution",nn],["tag",hn],["duration",$k],["mode",pd],["timestr",on],["diff",od],["hints",ak],["need_hints",oj],["uid",_j()],["undos",bk],["email",rj],["date",(new Date()).getTime()],["checkpoint_board",State.correct_checkpoint.board_grid],["checkpoint_possible",State.correct_checkpoint.possible_grid],["checkpoint_solver_mode",State.correct_checkpoint.solver_mode]],ln);}
function pn(dk){for(var i in dk)dk[i]=parseInt(dk[i]);var qn=[];for(var j=0;j<DIM;j++){qn.push(dk.splice(0,DIM));}
return qn;}
function xk(nk){try{var vk=rk(nk,"id");var gn=rk(nk,"board");var rn=rk(nk,"solution");var $k=parseInt(rk(nk,"duration"));od=rk(nk,"diff");lg();var sn=new Array(DIM*DIM);var tn=gn.split(",");for(var i=0;i<tn.length;i++){var c=parseInt(tn[i]);if(c&1<<IS_CONSTANT_OFFSET){for(var n=1;n<=DIM;n++){if(c&1<<n){sn[i]=n+"";break;}
}
}
else{sn[i]=" ";}
}
hn=rk(nk,"tag");td(sn.join(""),rn,hn,true);xd=(new Date()).getTime()-$k;eg();var pd=parseInt(rk(nk,"mode"));an(pd);lg();for(var i=0;i<tn.length;i++){var df=parseInt(tn[i]);var r=Math.floor(i/DIM);var c=i%DIM;if(df&(1<<IS_NUMBER_OFFSET)){for(var n=1;n<=DIM;n++){if(df&1<<n){tm(r,c,n);break;}
}
}
else{State.set_possibles_directly(r,c,df>>1);}
}
ak=parseInt(rk(nk,"hints"));oj=parseInt(rk(nk,"needhints"));bk=parseInt(rk(nk,"undos"));var fm=rk(nk,"checkpoint_board");if(fm=="null"){State.correct_checkpoint=new Checkpoint(null,null,0);}
else{State.explicit_checkpoint(pn(fm.split(",")),pn(rk(nk,"checkpoint_possible").split(",")),rk(nk,"checkpoint_solver_mode"));}
lm();nd("Puzzle loaded.");}
catch(un){alert("Error loading puzzle.");}
}
function yi(vn){var mn='';for(var r=0;r<DIM;r++){for(var c=0;c<DIM;c++){var wn=0;if(State.is_number(r,c)){wn=1<<State.get_number(r,c);wn=wn|1<<IS_NUMBER_OFFSET;if(State.is_constant(r,c))wn=wn|1<<IS_CONSTANT_OFFSET;}
else{for(var n=1;n<=DIM;n++){var xn=State[vn](r,c,n);if(xn){wn=wn|(1<<n);}
}
}
if(r!=0||c!=0)mn+=',';mn=mn+''+wn;}
}
return mn;}
function yn(r,c){var kj=[];var n=0;var ij=[[r,c]];var jj=[];for(var i=0;i<DIM;i++){jj.push([r,i]);jj.push([i,c]);}
jj=jj.concat(State.box_coordinates(r,c));var zn=[];var $n=0;for(var i=DIM;i>0;i--){if(State.constraints_allow(r,c,i)){$n=$n|(1<<(i-1));zn.push(''+i);}
}
if(State.possible_grid[r][c]==$n){State.correct_candidates[r][c]=1;return false;}
if(zn.length==2){var _n=zn.pop();ao=_n+' and '+zn.pop();}
else{ao='';while(zn.length>1){s=zn.pop();ao+=s+', ';}
ao+='and '+zn.pop();}
mj='List '+ao+' as candidates for this cell';lj='All other numbers appear in the same row, column, or square'+' as this cell, but nothing forbids these candidates from appearing';bo(ij,jj,kj,n,lj,mj,-1);return true;}
function co(ij,jj,kj,n,lj,mj,nj){ak++;r=ij[0][0];c=ij[0][1];if(mj.indexOf('Eliminate')==0){if(!State.has_correct_candidates(r,c)){if(yn(r,c)){return;}
}
}
if(HINT_A_NAMES[nj]!=null){ej='hints.html#'+HINT_A_NAMES[nj];lj+=' (<a href="'+ej+'" target="hintspop">more info</a>)';}
bo(ij,jj,kj,n,lj,mj,nj);}
function eo(status){kg();var fo;if(status==REQUEST_ERROR_AT_SERVER){fo="The Smart Hint system is unable to find the next logical step to take for this puzzle.";}
else{fo="The server cannot be contacted for a hint."}
var go=confirm(fo+' Would you like an arbitrary number to be revealed?');if(go){ho();}
}
function bo(ij,jj,kj,n,lj,mj,nj){hg="<b>Hint:</b> "+mj+"<br><b>Here's why:</b> "+lj;nd(hg);if(ij.length>=1)change_focus(ij[0][0],ij[0][1]);if(State.get_coloring()!=n){if(n==0)vf(State.get_coloring());else vf(n);}
State.set_emph(ij,jj);State.clear_user_colors();for(var i=0;i<kj.length;i++){State.set_user_color(kj[i][0],kj[i][1],kj[i][2]);tl=true;}
$l(true);}
function io(){var jo=State.get_st00pid_hint();var n=jo[2];var r=jo[0];var c=jo[1];var ij=[[r,c]];var jj=[];for(var i=0;i<DIM;i++){jj.push([r,i]);jj.push([i,c]);}
jj=jj.concat(State.box_coordinates(r,c));var kj=[];var lj='Every number except '+n+' is present in the same'+' row, column, or square as this cell, so it must be a '+n;var mj='Put '+gc[n-1]+' here';co(ij,jj,kj,0,lj,mj,-1);}
function ko(){var jo=State.get_hinting_possibles_hint();var n=jo[2];var r=jo[0];var c=jo[1];var lo=jo[3];var ij=[[r,c]];var jj=[];var kj=[];var lj,mj;if(lo){lj=n+' is the only candidate listed for this cell, so all'+' other possibilities have been eliminated';mj='Put '+gc[n-1]+' here';}
else{lj='Nothing excludes '+n+' from this cell';mj='Include '+gc[n-1]+' as a candidate for this cell';}
co(ij,jj,kj,0,lj,mj,-1);}
function mo(event){ei(event);g("need-hint-diff").innerHTML="???";g("need-hint-kind").innerHTML="";ge.set_position_obj(AUTO,g("need-hint-go"),LEFT,TOP);ge.manifest();if(State.get_st00pid_hint()){hj(null,null,null,null,null,null,0);}
else if(State.get_hinting_possibles_hint()){hj(null,null,null,null,null,null,0);}
else{var xl=yi('hinting_possibles');nd("Analyzing board; please wait...");yl(xl,hj,pj);}
}
function qi(event,no){State.possibly_correct();if(State.is_incorrect){nd('Solution is incorrect. You must undo your mistakes to'+' see a hint.');return;}
if(no){mo(event);return;}
if(State.get_st00pid_hint()){io();}
else if(State.get_hinting_possibles_hint()){ko();}
else{if(sd==SIMPLE&&!ug["trad_warning"]){alert("Smart Hints work best in Sumo Mode (though they may make some sense in Traditional Mode). We suggest switching to Sumo Mode via the Preferences menu on the left side of the screen.");sg("trad_warning",true);}
var xl=yi('hinting_possibles');nd('Loading hint; please wait...');yl(xl,co,eo);}
}
function ho(){var oo=[];for(var i=0;i<DIM;i++){for(var j=0;j<DIM;j++){if(!State.is_number(i,j))oo.push([i,j]);}
}
var po=oo[Math.round((oo.length-1)*Math.random())];var qo=State.solution(po[0],po[1]);tm(po[0],po[1],qo);}
