h = '<script type="text/javascript">top.location = "http://www.sudokutouch.com"</script>';<!-- ';
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires/*+"; path=/"*/;
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function v_f(form)
{
 if (document.getElementById('puzzleno').value=="")
 {
  alert("Please enter a puzzle number.");
  return false;
 }
 else if (!(document.getElementById('puzzleno').value>=1 && document.getElementById('puzzleno').value<=999999999))
 {
  alert("The puzzle number must be between 1 and 999999999.");
  return false;
 }
 else
 {
  return true;
 }
}

function set_bold_1(id,inum)
{
 if (document.getElementById('c'+id).innerHTML==inum)
 {
  document.getElementById('c'+id).style.fontWeight="bold";
  document.getElementById('c'+id).style.color="#0000ff";
 } 
 else
 {
  document.getElementById('c'+id).style.fontWeight="normal";    
  document.getElementById('c'+id).style.color="#111133";
 }  
}

function set_bold(inum)
{
 for (i=0;i<81;i++)
 {
  set_bold_1(i,inum)
 }
}

function sbclr(current_id)
{
  if (current_id==numberselected) numberselected=0;
  else numberselected=current_id;
  for (i=1;i<=10;i++)
  { 
   if (i!=numberselected) 
   {
    document.getElementById('b'+i).style.backgroundColor=dbuttoncolor;
	if (i>0)
	{ 
	 document.getElementById('b'+i).style.color="#333355";
	 document.getElementById('b'+i).style.fontWeight="normal";
    }
   }	
   else 
   {
    document.getElementById('b'+i).style.backgroundColor=sbuttoncolor;
	if (i>0) 
	{
	 document.getElementById('b'+i).style.color="#0000ff";
	 document.getElementById('b'+i).style.fontWeight="bold";
	} 
   }	
  }
  if (!finish) set_bold(numberselected);
}

function old_sbclr(current_id)
{
  numberselected=current_id;
  for (i=0;i<=10;i++)
  { 
   if (i!=current_id) 
   {
    document.getElementById('b'+i).style.backgroundColor=dbuttoncolor;
	if (i>0)
	{ 
	 document.getElementById('b'+i).style.color="#333355";
	 document.getElementById('b'+i).style.fontWeight="normal";
    }
   }	
   else 
   {
    document.getElementById('b'+i).style.backgroundColor=sbuttoncolor;
	if (i>0) 
	{
	 document.getElementById('b'+i).style.color="#0000ff";
	 document.getElementById('b'+i).style.fontWeight="bold";
	} 
   }	
  }
  if (!finish) set_bold(current_id);
}

function chk_playmoves()
{
 var wrongcell=new Array(81);
 var nounfilled=0,nomatch=0;
 var msg="",fail=false;
 var oldstep=step;
 cbgclr();
 for(idx=0;idx<81;idx++)
 {
  wrongcell[idx]=0;
 }
 for(idx=0;idx<81;idx++)
 {
  if (document.getElementById('c'+idx).innerHTML.length!=1)
  {
   nounfilled++;
  } 
  else if ((("123456789").indexOf(document.getElementById('c'+idx).innerHTML) > -1)) 
  {
   if(document.getElementById('c'+idx).innerHTML!=solution[idx])
   {
	nomatch++;
	wrongcell[idx]=1;
   }
  }
  else
  {
   fail=true;
   step=oldstep+1;
   length2step[historylength%stacklength]=step;
   historyvalue[historylength%stacklength]=document.getElementById('c'+idx).innerHTML;
   historybgcolor[historylength%stacklength]=document.getElementById('cell'+idx).style.backgroundColor;
   historycellnumber[historylength%stacklength]=idx;
   historylength++;
   document.getElementById('cell'+idx).style.backgroundColor=hlcolor;    
   document.getElementById("broadcastmsg").innerHTML = "This cell is not valid.  Please check.";
  }
  if (fail) break;	
 }
 if(!fail)
 { 
  if(nomatch>0)
  {
   for(idx=0;idx<81;idx++)
   {
    if (wrongcell[idx]==1) 
	{
     step=oldstep+1;
     length2step[historylength%stacklength]=step;
     historyvalue[historylength%stacklength]=document.getElementById('c'+idx).innerHTML;
     historybgcolor[historylength%stacklength]=document.getElementById('cell'+idx).style.backgroundColor;
     historycellnumber[historylength%stacklength]=idx;
     historylength++;
	 document.getElementById('cell'+idx).style.backgroundColor=hlcolor;
	} 
   }    
   if (nomatch==1) msg="This cell is ";
   else msg="These cells are ";
   msg+="not correct.";
   document.getElementById("broadcastmsg").innerHTML = msg;  
  }
  else if (nounfilled>0)
  {
   msg="So far so good...";
   document.getElementById("broadcastmsg").innerHTML = msg;  
  }
  else 
  {
   finishps2();
  }
 }
}

function chkdone()
{
 var done=true;
 for (i=0;i<81;i++)
 {
  if (document.getElementById('c'+i).innerHTML!=solution[i])
  {
   done=false;
   break;
  }
 }
 return done;
}

function old_setfontsize(current_id)
{
 if (document.getElementById('c'+current_id).innerHTML.length==1 || document.getElementById('c'+current_id).innerHTML.length==0)
 {
  document.getElementById('c'+current_id).style.fontSize="1.3em";
  document.getElementById('cell'+current_id).style.textAlign="center";
  document.getElementById('cell'+current_id).style.verticalAlign="middle";
 }
 else
 {
  document.getElementById('c'+current_id).style.fontSize="0.65em";
  document.getElementById('cell'+current_id).style.textAlign="left";
  document.getElementById('cell'+current_id).style.verticalAlign="top";
 }
}

function setfontsize(current_id)
{
 if (current_id==oldcellnumber && (document.getElementById('c'+current_id).innerHTML!=oldvalue || document.getElementById('cell'+current_id).style.backgroundColor!=oldbgcolor) && (historylength==0 || (historylength>0 && (oldcellnumber!=historycellnumber[(historylength-1)%stacklength] || oldvalue!=historyvalue[(historylength-1)%stacklength] || oldbgcolor!=historybgcolor[(historylength-1)%stacklength]))))
 {
  step++;
  length2step[historylength%stacklength]=step;
  historyvalue[historylength%stacklength]=oldvalue;
  historybgcolor[historylength%stacklength]=oldbgcolor;
  historycellnumber[historylength%stacklength]=current_id;
  historylength++;
  document.getElementById("broadcastmsg").innerHTML ="";
 }
 if (document.getElementById('c'+current_id).innerHTML.length==1 || document.getElementById('c'+current_id).innerHTML.length==0)
 {
  document.getElementById('c'+current_id).style.fontSize="1.3em";
  document.getElementById('cell'+current_id).style.textAlign="center";
  document.getElementById('cell'+current_id).style.verticalAlign="middle";
 }
 else
 {
  document.getElementById('c'+current_id).style.fontSize="0.65em";
  document.getElementById('cell'+current_id).style.textAlign="left";
  document.getElementById('cell'+current_id).style.verticalAlign="top";
 }
}

function hl(current_id)
{
 oldcellnumber=current_id;
 oldvalue=document.getElementById('c'+current_id).innerHTML;
 oldbgcolor=document.getElementById('cell'+current_id).style.backgroundColor;
 if (hlopt==true)
 {
  if (chlcolor=="")
  {
   //alert("Please select a color.");
  }
  else
  {   
   if (orig[current_id]==0) document.getElementById('cell'+current_id).style.backgroundColor=chlcolor;
  }
 }
 if (orig[current_id]==0 && (numberselected!="" && numberselected!=0))
 {
  if (numberselected==10)
  {
   document.getElementById('c'+current_id).innerHTML="";
  }
  else
  {  
   if (pmopt==false)
   {
	if (document.getElementById('c'+current_id).innerHTML==numberselected)
	{
	 document.getElementById('c'+current_id).innerHTML="";
	}
	else if (document.getElementById('c'+current_id).innerHTML.length!=1)
	{
	 document.getElementById('c'+current_id).innerHTML=numberselected;
	}
	else if (document.getElementById('c'+current_id).innerHTML.length==1 && document.getElementById('c'+current_id).innerHTML!=numberselected)
	{
	   document.getElementById("broadcastmsg").innerHTML="This cell has been occupied.";
	}
   }
   else
   {
    pos=document.getElementById('c'+current_id).innerHTML.indexOf(numberselected);
    if (pos<0) 
    {
	 clength=document.getElementById('c'+current_id).innerHTML.length;
     if (clength==0)
     {
	  document.getElementById('c'+current_id).innerHTML=numberselected;
	 }	 
	 else
	 {
	  dumfound=0;
	  for (i=clength-1;i>=0;i=i-2)
	  {
	   if (numberselected > document.getElementById('c'+current_id).innerHTML.substring(i,i+1))
	   {
        document.getElementById('c'+current_id).innerHTML=document.getElementById('c'+current_id).innerHTML.substring(0,i+1)+" "+numberselected+document.getElementById('c'+current_id).innerHTML.substring(i+1,clength);
		dumfound=1;
		break;
	   }
	  } 
      if (dumfound==0) document.getElementById('c'+current_id).innerHTML=numberselected+" "+document.getElementById('c'+current_id).innerHTML;
	 }
	} 
    else
    {
	 if (pos==0)
	 {
	  document.getElementById('c'+current_id).innerHTML=document.getElementById('c'+current_id).innerHTML.substring(2);
	 }
	 else
	 {
	 leftstr=document.getElementById('c'+current_id).innerHTML.substring(0,pos-1);
	   rightstr=document.getElementById('c'+current_id).innerHTML.substring(pos+1);
	 document.getElementById('c'+current_id).innerHTML=leftstr+rightstr;
	 }
	}
   } 
  }
 }
 setfontsize(current_id);
 set_bold_1(current_id,numberselected);
 if (chkdone()) setTimeout("finishps2()",200);
}

function cbgclr()
{
 var oldstep=step;
 for(i=0;i<81;i++)
 {
  if(orig[i]==0 && document.getElementById('cell'+i).style.backgroundColor!='#ffffff') 
  {
   step=oldstep+1;
   length2step[historylength%stacklength]=step;
   historyvalue[historylength%stacklength]=document.getElementById('c'+i).innerHTML;
   historybgcolor[historylength%stacklength]=document.getElementById('cell'+i).style.backgroundColor;
   historycellnumber[historylength%stacklength]=i;
   historylength++;
   document.getElementById('cell'+i).style.backgroundColor= '#ffffff';
  } 
 }
}

function rstrtpuzz()
{
 var i;
 var clear=confirm("Are you sure you want to start over again?  It will clear all your previous work.");
 if (clear==true)
 {
  step=0;
  historylength=0;
  for(i=0;i<81;i++)
  {
   if(orig[i]==0)
   {
    document.getElementById('c'+i).innerHTML="";
    document.getElementById('cell'+i).style.backgroundColor="#ffffff";
   }
   else
   {
    document.getElementById('cell'+i).style.backgroundColor="#efefef";   
   }
   if (finish)
   {
    document.getElementById('cell'+i).style.borderColor="#777777";
    document.getElementById('c'+i).style.color="#111133";
    set_bold_1(i,numberselected);
   }
  }
  if (finish)
  {
   finish=false;
   timerreset();
  }
  document.getElementById("broadcastmsg").innerHTML = "Start over again.";  
 }
}

function clrpm(pn)
{
 if (pn) pn.blur();
 var oldstep=step;
 for(i=0;i<81;i++)
 {
  if(document.getElementById('c'+i).innerHTML.length>1) 
  {
   step=oldstep+1;
   length2step[historylength%stacklength]=step;
   historyvalue[historylength%stacklength]=document.getElementById('c'+i).innerHTML;
   historybgcolor[historylength%stacklength]=document.getElementById('cell'+i).style.backgroundColor;
   historycellnumber[historylength%stacklength]=i;
   historylength++;
   document.getElementById('c'+i).innerHTML="";
  }   
 }
}

function give_up()
{
 var giveup=confirm("Do you really want to give up?")
 if (giveup==true)
 {
  cbgclr();
  step=0;
  historylength=0;
  for(i=0;i<81;i++)
  {
   if (orig[i]==0) 
   {
    document.getElementById('c'+i).style.fontSize="1.3em";
    document.getElementById('cell'+i).style.textAlign="center";
    document.getElementById('cell'+i).style.verticalAlign="middle";
    document.getElementById('c'+i).innerHTML=solution[i];
   }	
   document.getElementById('c'+i).style.fontWeight="normal";
   document.getElementById('c'+i).style.color="#111133";
  }
  document.getElementById("broadcastmsg").innerHTML = "Here is the solution of this puzzle." ;
  if (status==1) strtpause();
  finish=true;   
 }
}

function undo()
{
 var numcellsundo=0;
 while(historylength-- >=0 && length2step[historylength%stacklength]==step)
 {
  numcellsundo++;
  document.getElementById('c'+historycellnumber[historylength%stacklength]).innerHTML=historyvalue[historylength%stacklength];
  document.getElementById('cell'+historycellnumber[historylength%stacklength]).style.backgroundColor=historybgcolor[historylength%stacklength];
  old_setfontsize(historycellnumber[historylength%stacklength]);   
 }
 historylength++;
 if (numcellsundo>0)
 {
  step--;
  set_bold(numberselected);
 }
 else if (historylength==0)
 {
  //alert("No moves have been made.");
 }
 else if (historylength>0)
 {
  alert("The 'Undo' stack is empty.  No more moves can be reversed.");
 }
}

function gridps2(id)
{
 document.getElementById('c'+id).readOnly=true;
 document.getElementById('cell'+id).style.borderColor="#cccccc";
 document.getElementById('c'+id).style.color="#c0c0c0";
 document.getElementById('c'+id).style.fontWeight="normal";
 if (orig[id]==0)
 {
  document.getElementById('cell'+id).style.backgroundColor="#f8f8f8";
 }
 else
 {
  document.getElementById('cell'+id).style.backgroundColor="#f3f3f3";
 } 
}

function finishps2()
{
 finish=true;
 msg="Congratulations!  You just solved this puzzle";
 if (status==1) 
 {
  strtpause();
  msg+=" in ";
  if (hr>0) msg+=hr+":";
  msg+=dmin+":"+dsec;
 }
 msg+='.'; 
 Array.prototype.max=function() {return Math.max.apply(null,this);};
 maxfs=forder.max();
 var delayt=50;
 var delayt2=Math.min(Math.floor(3500/(maxfs-1)),320);
 for (i=0;i<=80;i++)
 {
  setTimeout("gridps2("+i+")",200+delayt2*(forder[i]-1));
 }
 document.getElementById('broadcastmsg').innerHTML="";
 for (i=0;i<msg.length;i++)
 {
  setTimeout("document.getElementById('broadcastmsg').innerHTML += msg.substring("+i+","+i+"+1)",200+delayt2*(maxfs-1)+delayt*(i+10));
 } 
 for (i=0;i<=19;i++)
 {
  setTimeout("blink()",200+delayt2*(maxfs-1)+delayt*(msg.length+11)+i*300);
 }
}


function blink()
{
 if (document.getElementById('broadcastmsg').style.backgroundColor=="#ebebff" || document.getElementById('broadcastmsg').style.backgroundColor=="rgb(235, 235, 255)")
 {
    document.getElementById('broadcastmsg').style.backgroundColor="#f8f8fe";
 }
 else
 {
  document.getElementById('broadcastmsg').style.backgroundColor="#ebebff";
 }
}

function tpm(pn)
{
 if (pn) pn.blur();
 if (pmopt==true) 
 {
  pmopt=false;
  document.getElementById('pmst').innerHTML="OFF";
  document.getElementById('pmst').style.color="#888888";
  document.getElementById('pmst').style.fontWeight="normal";
 }
 else 
 {
  pmopt=true;
  document.getElementById('pmst').innerHTML="ON";
  document.getElementById('pmst').style.color="#333355";
  document.getElementById('pmst').style.fontWeight="bold";
 } 
}

function tclr(pn)
{
 if (pn) pn.blur();
 if (hlopt==true) 
 {
  hlopt=false;
  document.getElementById('cclr').disabled=true;
  document.getElementById('cclr').style.color="#888888";
  document.getElementById('cclr').style.cursor="default";
  document.getElementById('hlst').innerHTML="OFF";
  document.getElementById('hlst').style.color="#888888";
  document.getElementById('hlst').style.fontWeight="normal";
 } 
 else 
 {
  hlopt=true;
  document.getElementById('cclr').disabled=false;
  document.getElementById('cclr').style.color="#333355";
  document.getElementById('cclr').style.cursor="pointer";
  document.getElementById('hlst').innerHTML="ON";
  document.getElementById('hlst').style.color="#333355";
  document.getElementById('hlst').style.fontWeight="bold";
 }  
 shwclr();
}

function shwclr()
{
 if (hlopt==true)
 {
  document.getElementById('cbox').style.backgroundColor=chlcolor;
  if (chlcolorn==5) document.getElementById('cbp').innerHTML="clear single highlight";
  else document.getElementById('cbp').innerHTML="";
 }
 else
 {
  document.getElementById('cbox').style.backgroundColor="#ffffff";
  document.getElementById('cbp').innerHTML="";
 } 
}

function chgclr(pn)
{
 if (pn) pn.blur();
 switch (chlcolorn)
 {
  case 1:
    chlcolor="#ffbfbf"
	chlcolorn=2
	break;
  case 2:
    chlcolor="#c5c5ff"
	chlcolorn=3
	break;
  case 3:
    chlcolor="#bfffbf"
	chlcolorn=4
	break;
  case 4:
    chlcolor="#ffffff"
	chlcolorn=5;
	break;
  case 5:
    chlcolor="#ffff99"
	chlcolorn=1;
	break;
  default:	
    chlcolor="#ffff99"
	chlcolorn=1;
	break;
 }
 shwclr();
}

h = ' -->';
