var dnpb=3;
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_eltern=getCookie("sudoku_eltern");
if(sudoku_eltern==null||sudoku_eltern=="")sudoku_eltern="";

function diffic(xx) {
	dnpb=eval(xx);
	init();
	}

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 show(data,color){
	$$$("display_area").innerHTML=data+"<br />\n";
	$$$("display_area").style.color=color;
	}

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;
			$$$(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_eltern",str);
	alert("Ihre Zahlen wurden gespeichert.");
	}

function load(){
	clearer();
	var str=getCookie("sudoku_eltern");
	if(str==""||str==null){
		alert("Keine gespeicherten Zahlen gefunden!\nVielleicht haben Sie Cookies deaktiviert?")
		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;
	show("&Uuml;berpr&uuml;fung...","#A5ACB2");
	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){
				show("Leere/ung&uuml;ltige Felder gefunden!","#A5ACB2");
				found=1;
				$$$(id).style.backgroundColor="#A5ACB2";
				setTimeout("discolorCells(id,id)",2000);
				break loop;
				}
			else if(value){
				if(!checkForUnique(a,b,value)){
					$$$(id).style.backgroundColor="#A5ACB2";
					$$$(repeated_at).style.backgroundColor="#A5ACB2";
					setTimeout("discolorCells(repeated_at,id)",2000);
					found++;
					break loop;
					}
				}
			else{
				show("Es fehlen noch Zahlen,<br />bitte zuerst das Sudoku vervollst&auml;ndigen.","#A5ACB2");
				$$$(id).style.backgroundColor="#A5ACB2";
				setTimeout("discolorCells(id,id)",2000);
				found=1;
				break loop;
				}
			}
		}
	if(!found){
		if(compleated)alert("...aber nur mit Hilfe geloest!");
		else alert("Herzlichen Glueckwunsch!\nSie haben das Sudoku geloest.");
		show("<b>Super, Sie haben das Eltern.de-Sudoku gel&ouml;st!</b>","#A5ACB2");
		}
	}

function reloadGame(){
	str2pos(orginal_game);
	}

function solve(){
	show("L&ouml;sung suchen...");
	if(confirm("Wollen Sie die Loesung wirklich sehen?\n")){
		str2pos(this_puzzle);
		compleated=1;
		show("Super, Sie haben das Eltern.de-Sudoku gel&ouml;st!","#A5ACB2");
		}
	}

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);
			}
		}
	orginal_game=pos2str(2);
	show("","#A5ACB2");
	document.f.Auswahl.options.selectedIndex=dnpb-1;
	}

function DatumBer(y, m, d) {
	if (m < 3)
		var f = -1;
	else
		var f = 0;
	return Math.floor((1461*(f+4800+y))/4)
		+ Math.floor(((m-2-(f*12))*367)/12)
		- Math.floor(3*Math.floor((y+4900+f)/100)/4)
		+ d
		- 32075;
	}

function TdJ(y,m,d) {
	return DatumBer(y,m,d)-DatumBer(y,1,1)+1;
	}

function TdJanz(t) {
	return TdJ(t.getYear(), t.getMonth()+1, t.getDate());
	}

var now = new Date();
var heute = TdJanz(now);
if (heute==1){
	var gestern = 366;
	}
else {
	var gestern = (eval(TdJanz(now))-1);
	}
if (heute==2){
	var vorgestern = 366;
	}
else {
	if (heute==1){
		var vorgestern = 365;
		}
	else{
		var vorgestern = (eval(TdJanz(now))-2);
		}
	}

