/*
Online Sudoku By Binny V A (http://www.geocities.com/binnyva)
This code is Copyright (c) 2005 Binny V Abraham
License is granted to user to reuse this code on other Web site if, and only if, this entire copyright notice is included. The Web Site containing this script must be a not-for-profit(non-commercial) web site unless I gave permission for the use of the script.
End copyright - This must be retained and posted as is to use this script along with a link to the original location.

This code is compressed to save bandwidth - if you wish to see the code, use this url
http://www.geocities.com/binnyva/code/javascript/sudoku/sudoku.js
<script>*/
var hlw=350;var vlh=370;var dnpb=4;var repeated_at="";var compleated=0;var xy=new Array(9);
for(i=0;i<9;i++){xy[i]=new Array(9);}
var puzzles=new Array(
"abc.gdefihdeif.hagcbfgh.cibadebf.iehdcg.ae.acbf.gid.hghdiace.bficgdabhefh.b.f.cg.eaidde.ahfibcg",
"a.bfhiec.g.ddcg.fbai.heiehdg.cabfdegi.hc.fabai.cb.df.eghhf.be.agcidbfi.edhgcac.adgf.iheb.g.hebcaf.di",
"bfi.edhgcac.adgf.iheb.g.hebcaf.dia.b.fhiec.g.ddcg.fbai.heiehdg.cabfdegi.hc.fabai.cb.df.eghhf.be.agcid",
"ib.ad.c.g.efhe.hc.fbiagd.fg.daehcibbhcgd.faiegf.ei.ab.dchdai.hcebfgfgdcai.he.bbeahd.gc.if ih.c.e.bf.gda",
"aeh.dfi.cgbcf.b.e.hgiad.gidc.abfeh.fc.g.b.aeihdb.eif dha.gchda.ig.cebfg.bfhd.aeic.dcegi.f.hbaah.ibce.dfg",
"dah.iegfcbbigfd.cehae.fchab.gi.dbh.fe.gacidcedi.bhgaf.ag.i.d.cfbh.egb.ch.dea.f.ihf.e.aci.d.gbid.a.fbgceh",
"cf.b.ag.eidhhd.eif.cbaga.g.ibdh.fceg.acb.if he.d.dehgcafb.i.if be.hdg.acdc.iebg.f.haegf.ahd.cibhba.ci.f.deg",
"chfgeai.d.be.gdib.hcf.ab.iacf.dh.gefa.d.hcg.ebigic.bd.eahfeb.hfa.i.gdcd.gh.bica.fe.fei.hagd.cb.a.cbdefihg",
"adbfheig.c.icfba.gdeh.he.gcidfa.bcb.ide.ah.fgag.dh.fbei.cefhigcbd.abi.he.cd.gafg.de.fbach.i.acf.g.hid.b.e",
"ied.a.bfghchcfgeia.b.dagbh.cdi.efb.ied.ca.f.ghcga.ef.hdib.fd.hbig.cae.hdie.fg.cabb.a.e.ihcf.dggfcd.b.aehi",
"h.g.bc.ea.idfa.ci.bf.dgehfe.dih.gcbab.fi.ehg.acdh.dcfabi.geag.edc.ib.fhdah.gbe.ficebf.ci.ad.hggi.ch.df.e.ab",
".e.cadbhfgi.fbhegiad.cg.di.afch.bei.h.fba.g.cd.e.badhceif.g.ec.g.dif.b.hag.ibhe.da.fc.chagif de.bfedcabi.g.h",
"ge.c.h.i.fdabaif.bg.d.echdh.bae.cf.gic.he.fbdig.a.dbageif h.c.if gca.hb.deb.dh.efg.acicf.e.ia.bhdggia.h.c.d.ebf",
"deb.cg.a.ihff.ah.dbi.egccgi.h.fe.bda.gcdf.bihae.a.fb.hd.ec.i.geihg.acfb.dbi.ca.d.hefgge.dbc.fi.haah.f.ie.gdcb"
);
var chosen=0;
var this_puzzle="";
var orginal_game="";
var bites=document.cookie.split(";");
var sudoku_fingerprint=getCookie("sudoku_fingerprint");
if(sudoku_fingerprint==null||sudoku_fingerprint=="")sudoku_fingerprint="";
function getCookie(cookie){
for(i=0; i<bites.length; i++){
nextbite=bites[i].split("=");
if(nextbite[0]==cookie)return unescape(nextbite[1]);
}
return null;
}
var today=new Date();
var expiry=new Date(today.getTime()+28*24*60*60*1000);
function setCookie(name,value){
if((value!=null)&&(value!=""))document.cookie=name+"="+escape(value)+"; expires="+expiry.toGMTString();
bites=document.cookie.split(";");
}
function $(id){return document.getElementById(id);}
function getIndex(x,y){
var index=(x*3)+y-3;
return index;
}
function getXY(index){
var x=1,y=1;
switch(index){
case 1:x=1; y=1; break;
case 2:x=1; y=2; break;
case 3:x=1; y=3; break;
case 4:x=2; y=1; break;
case 5:x=2; y=2; break;
case 6:x=2; y=3; break;
case 7:x=3; y=1; break;
case 8:x=3; y=2; break;
case 9:x=3; y=3; break;
}
var xy_coods=new Array(x,y);
return xy_coods;
}
function rand(){
var number=Math.round(Math.random()*10);
while(number<1||number>9){
number=Math.round(Math.random()*10);
}
return number; 
}
function uniqueRand(list){
var number=rand();
for(var a=0;a<list.length;a++){
if(list[a]==number){
number=rand();
a=-1;
}
}
return number;
}
function sh(data,color){
$("display_area").innerHTML=data+"<br />\n";
$("display_area").style.color=color;
}
function helpLineV(line){
var item=$("lv-"+line).style;
if(item.position=="absolute"){
item.position="relative";
item.height="10px";
}else{
item.position="absolute";
item.height=vlh+"px";
}
}
function helpLineH(line){
var item=$("lh-"+line).style;
if(item.position=="absolute"){
item.position="relative";
item.width="5px";
}else{
item.position="absolute";
item.width=hlw+"px";
}
}
function checkForUnique(box,cell,number){
if(box>1){
var b2c=new Array(); 
switch(box){
case 2:b2c.push(1);break;
case 3:b2c.push(1,2);break;
case 4:b2c.push(1);break;
case 5:b2c.push(2,4);break;
case 6:b2c.push(3,4,5);break;
case 7:b2c.push(1,4);break;
case 8:b2c.push(2,5,7);break;
case 9:b2c.push(3,6,7,8);break;
}
var id="",value="";
for(i=0;i<b2c.length;i++){
xy_coods=getXY(cell);
for(j=1; j<=3; j++){
if(box==2||box==3||box==5||box==6||box==8||box==9){
if(box==5&&b2c[i]==2);
else if(box==6&&b2c[i]==3);
else if(box==8&&(b2c[i]==2||b2c[i]==5));
else if(box==9&&(b2c[i]==3||b2c[i]==6));
else{
cell_to_check=getIndex(xy_coods[0],j);
id="c"+b2c[i]+cell_to_check;
value=$(id).value;
if(value==number){
repeated_at=id;
return false;
}
}
}
if(box>=4){
if(box==5&&(b2c[i]==4));
else if(box==6&&(b2c[i]==4||b2c[i]==5));
else if(box==8&&b2c[i]==7);
else if(box==9&&(b2c[i]==7||b2c[i]==8));
else{
cell_to_check=getIndex(j,xy_coods[1]);
id="c"+b2c[i]+cell_to_check;
value=$(id).value;
if(value==number){
repeated_at=id;
return false;
}
}
}
}
}
}
for(k=1;k<=9;k++){
id="c"+box+k;
if(number==$(id).value&&k !=cell){
repeated_at=id;
return false;
}
}
return true;
}
function insert(id,value){
$(id).value=value
if(value)$(id).disabled=true;
else $(id).disabled=false;
}
function clearer(){
var id=""
document.f.reset();
for(var i=1; i<=9; i++){
for(var j=1; j<=9; j++){
id="c"+i+j;
if($(id)) $(id).disabled=false;
}
}
}
function discolorCells(cell1,cell2){
$(cell1).style.background="#fff";
$(cell2).style.background="#fff";
}
function recordPosition(){
var value=0;
for(a=0;a<9;a++){
for(b=0;b<9;b++){
value=$("c"+(a+1)+(b+1)).value;
if(!isNaN(value)&&value.length==1){
xy[a][b]=value;
}else{
xy[a][b]=0;
}
}
}
}
function pos2str(action){
var str="";
var zero_count=0;
var alpha=" abcdefghijklmnopqrstuvwxyz";
if(action){
recordPosition();
}
for(a=0;a<9;a++){
for(b=0;b<9;b++){
if(xy[a][b]){
if(zero_count){
str+=alpha.charAt(zero_count);
zero_count=0;
}
str+=xy[a][b];
}else{
zero_count++;
}
}
}
return str;
}
function str2pos(str){
var zero_flag=0;
var alpha="abcdefghijklmnopqrstuvwxyz";
var pos=0;
var a=0;
var b=0;
str=str.replace(/\./g,"");
while(a<9){
ch=str.charAt(pos);
id="c"+(a+1)+(b+1);
if(!isNaN(ch)&&!zero_flag){
insert(id,ch);
}else if(zero_flag){
insert(id,"");
zero_flag--;
}else{
insert(id,"");
zero_flag=alpha.indexOf(ch);
}
if(!zero_flag){
pos++;
}
b++;
if(b>=9){
b=0;
a++;
}
}
}
function makeCookieString(){
var str="";
var puzzle_cells=new Array(0);
for(a=0;a<9;a++){
for(b=0;b<9;b++){
value=$("c"+(a+1)+(b+1)).value;
if($("c"+(a+1)+(b+1)).disabled){
value="."+value;
}
puzzle_cells.push(value);
}
}
str=puzzle_cells.join(';');
alert(str);
return str;
}
function save(){
var str=makeCookieString();
setCookie("sudoku_fingerprint",str);
alert("Game saved.");
}
function load(){
clearer();
var str=getCookie("sudoku_fingerprint");
if(str==""||str==null){
alert("No saved games found!")
return false;
}
var index=0;
var puzzle_cells=str.split(";");
for(a=0;a<9;a++){
for(b=0;b<9;b++){
id="c"+(a+1)+(b+1);
number=puzzle_cells[index];
if(number.charAt(0)=="."){
number=number.charAt(1);
$(id).disabled=true;
}
if(number){
}
$(id).value=number;
index++;
}
}
}
function checker(){
var found=0;
sh("Checking game...","#a84efa");
loop:
for(a=1;a<=9;a++){
for(b=1;b<=9;b++){
id="c"+a+b;
value=$(id).value
if(isNaN(value)||value>9||value<1){
if(value == "") {
sh("Empty cells found.","#d78601");
} else {
sh("Invalid entries found.","#d78601");
}
found=1;
$(id).style.background="red";
setTimeout("discolorCells(id,id)",2000);
break loop;
}
else if(value){
if(!checkForUnique(a,b,value)){
$(id).style.background="red";
$(repeated_at).style.background="red";
setTimeout("discolorCells(repeated_at,id)",2000);
found++;
break loop;
}
}else{
sh("Empty cells were found. Please complete the puzzle.","#d78601");
$(id).style.background="red";
setTimeout("discolorCells(id,id)",2000);
found=1;
break loop;
}
}
}
if(!found){
if(compleated)alert("Sorry-you can't win after giving up. But it is solved.");
else alert("Congratulations-You have completed the puzzle.");
sh("Game Over","#000000"); 
}
}
function reloadGame(){
str2pos(orginal_game);
}
function solve(){
sh("Finding the solution to the game...");
if(confirm("This will automaticaly solve the puzzle for you.\nAre you sure you want to do this?")){
str2pos(this_puzzle);
compleated=1;
sh("Solved the puzzle.");
}
}
function makeNewOrder(str){
var alpha=" abcdefghijklmnopqrstuvwxyz";
var new_order="";
numbers=new Array("0");
for(j=0;j<9;j++){
new_numbers=uniqueRand(numbers);
numbers.push(new_numbers);
}
for(i=0;i<str.length;i++){
if(str.charAt(i)=="."||str.charAt(i)=="*"||str.charAt(i)=="x"||
str.charAt(i)=="_"||str.charAt(i)=="-"||str.charAt(i)=="+"){
new_order+="."
}else{
new_order+=numbers[alpha.indexOf(str.charAt(i))]
}
}
return new_order;
}
function init(){
chosen=Math.floor((Math.random()*10)/(10/puzzles.length));
chosen = 2;
this_puzzle=makeNewOrder(puzzles[chosen]);
compleated=0;
clearer();
var lbea=0;
var extra_number_count=0;
for(var i=0;i<9;i++){
var b=0,lofn=0;
var arr_b=new Array();
var limit=9;
var this_box="";
var dot_count=0;
for(var j=lbea; j<lbea+limit; j++){
if(this_puzzle.charAt(j)=="."){
limit++;
dot_count++;
}
this_box=this_box+this_puzzle.charAt(j);
}
lbea=lbea+limit;
nonos=dnpb-extra_number_count;
extra_number_count=0;
if(dot_count>nonos){
if(rand()>5){
extra_number_count=dot_count-nonos;
}
nonos=dot_count;
}
arr_b=new Array();
for(b=0;b<nonos;b++){
lofn=this_box.indexOf(".");
if(lofn+1){
this_box=this_box.substring(0,lofn)+
this_box.substring(lofn+1,this_box.length);
}else{
lofn=uniqueRand(arr_b);
lofn--;
}
arr_b.push(lofn+1);
insertion_number=this_box.charAt(lofn);
lofn++;
id="c"+(i+1)+lofn
insert(id,insertion_number);
}
}
original_game=pos2str(2);
}

