// AllSudoku.com JavaScript - All Right Reserved - Copyright 06 Oct 2005
//------------print game-----------------------
function printpopup(){
	//document.f.ss.value=buildHTMLprint(creategamestring(0),1)
	var xy=window.open('','prwin','width=570,height=570,toolbar=no,menubar=yes')
	//alert(creategamestring(1))
	xy.document.write('<html><head><link href="http://www.allsudoku.com/allsudokua.css" type="text/css" rel="stylesheet"></head><body>\n')
	xy.document.write(buildHTMLprint(creategamestring(0),1))
	xy.document.write('<form><input type="button" value="Print" onClick="window.print()"></form></body></html>') 
	xy.document.close()
	xy.focus()
}
function buildHTMLprint(x,xx){//given game string and hints false/true return HTML table
	var ss=[ ['ptl','ptc','ptr'], ['pcl','pcc','pcr'], ['pbl','pbc','pbr']];
	var a=x.split(delimiter)
	var y='<table border="1" class="pp"><tr><tr><td colspan="9" >AllSudoku.com: Sudoku Games and Tools for Beginner or Obsessed</td></tr>'
	var row=0; var col=0;
	for (var i=5; i<5+(a[1]*a[2]); i++){
		//get class
		var ssindex=row % 3
		var sssindex= col % 3
		var clss=ss[ssindex][sssindex]
		//document.f.elements[i].className='input'
		if (((a[i] == parseInt(a[i]))) && (a[i]<0)){
			y=y+'<td class="'+clss+'"><b>'+Math.abs(a[i])+'</b></td>'//preset items
		}else{
			var z=a[i]
			if ((xx==false) && (z.length>1)){z=""}
			if (z.length==0){z="&nbsp;&nbsp;"}
			y=y+'<td class="'+clss+'">'+z+'</td>'//hints or plays
		}
		col=col+1
		if (col >=a[1]){
			col=0;row=row+1
			y=y+'</tr>\n<tr>'
		}
	}
	y=y+'</tr></table>';
	return y;
}
//-----------count numbers ---------------------
function countNumbers(){
	var p="||| ||| |||"
	var a=new Array(0,0,0,0,0,0,0,0,0,0)
	for (var i=0; i<89; i++){
		if ((parseInt(document.f.elements[i].value)) && (document.f.elements[i].value.length<2))
			a[document.f.elements[i].value]++
	}
	//have the count, so now fill in form
	for (var i=1; i<10; i++){
		if (a[i]==9){
			document.d.elements[i-1].className='rcc';//alert(document.d.elements[i-1].className)
		}else{
			document.d.elements[i-1].className='count';//alert(document.d.elements[i-1].className)
		}
		if (a[i] > 6){a[i]++}
		if (a[i] > 3){a[i]++}
		document.d.elements[i-1].value=p.substring(0,a[i])
	}
}
function highlightNumberOnly(x){
	restoreclass()
	for (var i=0; i<89; i++){
		if (document.f.elements[i].value==x){
			document.f.elements[i].className=document.f.elements[i].className+', '+'rccc'
			//highlightrcc(i,'rcc','rccc')// need to modify with different css, does not restore board, and works with readonly
		}
	}
	current=-1
}
function highlightNumber(x){
	restoreclass()
	for (var i=0; i<89; i++){
		if (document.f.elements[i].value==x){
			highlightrcc(i,'rcc','rccc')// need to modify with different css, does not restore board, and works with readonly
		}
	}
	current=-1
}

//-----------redo code-------------
function pushundo(){ //push gamestring onto stack
	undoa[undoptr]=creategamestring(1)
	if (undoptr< undomax){undoptr++}
	undotop=undoptr
	//alert('doing pushundo='+creategamestring())
	//alert('stack='+undoptr)
}
function popundo(){
	//alert('undoprt='+undoptr +' undoa[undoptr]='+undoa[undoptr]) 
	if (undoptr>0){undoptr--;setgame2(undoa[undoptr]);}
}
function redo(){
	var x=undoptr+1
	if (x < undotop){
		undoptr++
		setgame2(undoa[undoptr])
	}
}
function resetundo(){
	undoptr=0;undotop=0;
}
//------------- email code ----------------------
function buildHTMLtable(x,xx){//given game string and hints false/true return HTML table
	var a=x.split(delimiter)
	var y='<table border="1"><tr>'
	var row=0; var col=0
	for (var i=5; i<5+(a[1]*a[2]); i++){
		var flag=1
		var color='#CCCCCC'
		if ((col >2) && (col < 6)){
			flag=-1
		}
		if ((row >2) && (row < 6)){
			flag=-flag
		}
		if (flag==-1){color='#FFFFFF'}
		//document.f.elements[i].className='input'
		if (((a[i] == parseInt(a[i]))) && (a[i]<0)){
			y=y+'<td align="center" bgcolor="'+color+'">&nbsp;<b>'+Math.abs(a[i])+'</b>&nbsp;</td>'
		}else{
			var z=a[i]
			if ((xx==false) && (z.length>1)){z=""}
			if (z.length==0){z="&nbsp;&nbsp;"}
			y=y+'<td align="center" bgcolor="'+color+'">&nbsp;'+z+'&nbsp;</td>'
		}
		col=col+1
		if (col >=a[1]){
			col=0;row=row+1
			y=y+'</tr>\n<tr>'
		}
	}
	y=y+'</tr></table>';
	return y;
}
//------------------query and string functions
function link2game(){
	location.search="g="+creategamestring(1)
}
function querygame(){//if query sting of g, get value
	var a=location.search.substring(1,9999).split('&')
	var flag=0
	for (var i=0;i<a.length;i++){
		if (a[i].substring(0,2)=='g='){
			flag=1 //found query
			var x=a[i].substring(2,9999)
			//alert('x='+x)
			y=validategamestring(x)
			if (y>83){
				resetundo()//undo reset pointer to 0
				setgame2(x)
			}else{
				alert('Invalid Game String')
				newgameon()
			}
		}
	}
	return flag
}
function validategamestring(x){//make sure number of commas = 84 or more
	//for right now just display number of commas
	var pattern=/|/g;
	var y=x.match(pattern)//return only commas so length = number of commas
	//alert ('commas='+y.length)
	//alert ('match='+x.match(pattern))
	return y.length
}

function setgame2(x){//given x populate matrix
	//first two items are row and column, next tow are number of row boxes and column boxes
	newgameoff();emailfriendoff();
	var a=x.split(delimiter)
	var row=0; var col=0;
	for (var i=5; i<5+(a[1]*a[2]); i++){
		//alert('row='+row+' col='+col+ ' both='+(row*10+col)+ ' value='+a[i]) 
		if (((a[i] == parseInt(a[i]))) && (a[i]<0)){//preset squares
			document.f.elements[row*10+col].value=Math.abs(a[i])
			document.f.elements[row*10+col].readOnly=true
			document.f.elements[row*10+col].className='readonly'	
		}else{//all other squares
			document.f.elements[row*10+col].value=a[i]
			document.f.elements[row*10+col].readOnly=false
			document.f.elements[row*10+col].className='notice'	
		}
		col=col+1
		if (col >=a[1]){col=0;row=row+1}
	}
	document.r.restart.value=x//save for later restore game
	remaining(0)
	resetclock() //start clock
}
function setreadOnly(x){
	document.f.elements[x].readOnly=true
	document.f.elements[x].className='readonly'	
	//set style
}
function creategamestring(xx){//=0 single only, x=1 all
	//var delimiter=','
	var x='1|9|9|3|3'
	for (var i=0; i<9; i++){
		for (var j=0; j<9; j++){
			x=x+delimiter
			if (document.f.elements[i*10+j].readOnly==true){x=x+'-'}
			if ((xx==1) || (document.f.elements[i*10+j].value.length<2)){
				x=x+document.f.elements[i*10+j].value
			}
		}
	}
	return x
}

//---------------------------------------------------------------
function displayMsg(x){
	document.getElementById("msg").innerHTML=x;
}
function entergame(){
	//make all readOnly false
	newgameoff();
	for (var i=0;i<90;i++){
		document.f.elements[i].readOnly=false
		document.f.elements[i].className='input'
		document.f.elements[i].value="";
	}
	resetundo()//undo reset pointer to 0
	remaining(0);//show count remaining
	storeclass();clearclock();
	mode=1
	setSudokuon()//change buttons 
}
function entergamestring(){
	var x=prompt('Paste in Sudoku Game String')
	if (x !==null){setgame2(x);}
}
function validate(){
	restoreclass()
	storeclass();
	//check for non-single digits in entire board
	var flag=0
	for (var j=0; j<81; j=j+10){
		for (var i=0; i<9; i++){
			var k=j+i
			if(document.f.elements[k].value.length != 1){
				flag=1;
				document.f.elements[k].className="problem";
			}
		}
	}
	//check each row, col and box
	var j=0
	for (var i=0; i<27; i++){
		build9(i)
		j=j+verify9()
		//alert('j='+j)
	}									
		displayMsg('<b>Validate</b><br>This tool examines each square to see<br>if the game is complete. Red highlights show<br>incomplete squares. Useful to see which<br>squares still need to be solved.')
	if (j==0){//if all done
		displayMsg('<b>You won! You won! You won!</b>')
		stopclock()
	}
}
function verify9(){//verify global variables p9, a9 contain only one digit
	var aa=new Array(0,0,0,0,0,0,0,0,0,0)
	var flag=9
	for (var i=0;i<9;i++){
		if ((a9[i].length == 1) && (a9[i]==parseInt(a9[i]))){//make sure single digit
			aa[a9[i]]++
			flag--
		}
	}//now we know if we have dups
	//if (flag != 0){//if dup
		for (var i=0; i<aa.length; i++){
			if (aa[i]>1){//found a dup value which is variable i
				for (var j=0; j<10; j++){//look for it in the a9
					if (a9[j]==i){//found position
						if (document.f.elements[p9[j]].readOnly != true){
							document.f.elements[p9[j]].className="problem2";
						}
					}
				}
			}
		}
	//}
	return flag
}
function build9(x){//given an "array" number *0-8=rows, 9-17=cols, 18-26=boxes return array and position
	if (x<9){//rows
		for (var i=0; i<9; i++){
			p9[i]=(x*10)+i
			a9[i]=document.f.elements[p9[i]].value
		}
	} else {
		if (x<18){//columns
			for (var i=0; i<9; i++){
				p9[i]=(i*10)+x-9
				a9[i]=document.f.elements[p9[i]].value
			}			
		}else{//boxes
			var start=Math.floor((x-18)/3)*30+(((x-18) % 3)*3)
			var count2=0
			for (var i=0;i<3;i++){//rows
				for (var j=0;j<3;j++){//cols
					p9[count2]=start+i+(j*10)
					a9[count2]=document.f.elements[p9[count2]].value
					count2=count2+1
				}
			}			
		}
	}
}



//menu stuff
activateMenu = function(nav) {
	/* currentStyle restricts the Javascript to IE only */
	if (document.all && document.getElementById(nav).currentStyle) {  
		var navroot = document.getElementById(nav);
		/* Get all the list items within the menu */
		var lis=navroot.getElementsByTagName("LI");  
		for (var i=0; i<lis.length; i++) {
		   /* If the LI has another menu level */
			if(lis[i].lastChild.tagName=="UL"){
				/* assign the function to the LI */
				lis[i].onmouseover=function() {	
				   /* display the inner menu */
				   this.lastChild.style.display="block";
				}
				lis[i].onmouseout=function() {                       
				   this.lastChild.style.display="none";
				}
			}
		}
	}
}

//-------------------------------------------------------------------------------------------------
function buildArray(id){
	//returns Array of row, column and cube element Array positions - used all the time
	var a=new Array()
	var count2=0
	//cube - need to modify to not take row and column
	var startcol=(id % 10)
	var col=startcol % 3
	var startrow=(id-startcol)/10
	var row=startrow %3;
	var start=(startrow-row)*10+(startcol-col)
	for (var i=0; i<3; i++){//rows
		if (i != row){
			for (var j=0; j<3; j++){//columns
				if (j != col){
					a [count2]=start+j+(i*10);
					count2=count2+1;
				}
			}
		}
	}
	//row
	start=id-( id % 10)
	for (var i=start; i<start+9; i++){
			a [count2]=i;
			count2=count2+1;
	}
	//column
	start=id % 10
	for (var i=start; i<start+81; i=i+10){
		if (i != id){
			a[count2]=i;
			count2=count2+1;
		}
	}
	return a;
}
function setSudokuon(){
	var node=document.getElementById('set');
	node.style.display='block'
	var node=document.getElementById('buttons');
	node.style.display='none'
	displayMsg("<b>Type In Game:</b><br>To enter your own game or one from another<br>source click on a square and type in a number.<br>When you've entered the whole game then<br>click on the <i>Set Sudoku</i> button.")	
}
function setSudokuoff(){
	var node=document.getElementById('buttons');
	node.style.display='block'
	var node=document.getElementById('set');
	node.style.display='none'	
	displayMsg("<b>Ready to Play:</b><br>You're now ready to play Sudoku.<br>Select a square. Then type in a number<br>or use the keypad on the left.")	
}

function setSudoku(){
	//scan puzzel for illegal entries
	var flag= -1;
	var pattern='/[1-9]/'
	for (var i=0;i<90;i++){
		var x=document.f.elements[i].value
		var xx=x.search(pattern);
		if ((x.length>1) || (xx!==-1)){
//		if ((x.length>1) || (pattern.markSquare(x)){
			flag=i
			//set class to highlight error in red, turn off onfocus
			break
		}
	}
	if (flag > -1){
		alert ('Invalid Entry @ '+flag)
	} else {
		for (var i=0;i<90;i++){
			var x=document.f.elements[i].value
			if (x.length>0){
				document.f.elements[i].readOnly=true
				document.f.elements[i].className='readonly'
				//alert(document.f.elements[i].value)
			}else{
				document.f.elements[i].readOnly=false
				document.f.elements[i].className='notice'	
			}
		}
		storeclass()
		resetundo()//undo reset pointer to 0
		remaining(1);//show count remaining
		//document.f.but.value="Test Completeness";
		//set restore
		resetclock() //start clock
		document.r.restart.value=creategamestring(1)
		mode=0;setSudokuoff()//swap buttons
		//undoptr=0;undotop=0;pushundo()
	}
}
function easySolver() {//multiple calls to markAll until no changes
	displayMsg('<b>Simple Solver:</b><br>This tool can completely solve any<br>simple game. It makes several passes<br>examining the board to see which squares<br>have only one possiblility.')
	var x=''
	for (var i=0; i<10; i++){
		x=creategamestring(1)
			markAll(1)
		if (x == creategamestring(1)){break}
		//alert(i+' \ngame='+x+'\ngame='+creategamestring(1))
	}
	pushundo()
}

function markSquare(id,x){//if x==true then ???
	displayMsg('<b>Mark Square:</b><br>This tool looks at the row, the column<br>and the box for the selected square and<br>marks the square with the possible numbers.')
	markSquare2(id,x)
}
function markSquare2(id,x){//if x==true then ???
	if (id == -1){
		alert('No square selected');
	}else{
		if (!document.f.elements[id].readOnly){
			var d=0
			var a=buildArray(id)
			for (var i=0;i<a.length;i++){
				if ((x==1) ||(document.f.elements[a[i]].readOnly==true)){
					d=bit(d,document.f.elements[a[i]].value)
				}
			}
			document.f.elements[id].value=decode(d,x)
			//return decode(d)
			//if ((document.f.elements[id].value.length == 1) && (x==0)){ //attempting to set count when changed to single digit
			//	remaining(1);//show count remaining
			//}
		}
	}
	
}

function bit(d,v){
	//v {123456789}
	if (v.length == 1){
	    //alert ( v + ' power='+ Math.pow(2,(parseInt(v))-1))
		d=d|Math.pow(2,(parseInt(v))-1)
	}
	return d
}

function decode(x){
	//given a number show bits
	var x= 511 ^ x //XOR bits
	var xx=""
	var mask=1
	for (var i=1;i<10;i++){
		if (x & mask){
			xx=xx+i
		}
		//modify mask
		mask=mask*2
	}
	return xx
}
function restoreclass(){
	for (var i=0; i<90; i++){
		document.f.elements[i].className=document.f.elements[i].old
	}
}

function clearMarks(){ //clear all but setSudoku
	displayMsg('<b>Clear Marks:</b><br>In solving a Sudoku people often<br>mark a square with possible numbers.<br>This tool erases all those marks<br>leaving the board with just single digit entries.')
	restoreclass();
	current=-1;
	for (var i=0;i<90;i++){
		if (document.f.elements[i].value.length !== 1){
			document.f.elements[i].value=""
		}
	}
	pushundo()
}
function markAll(x){ //mark all but setSudoku
	displayMsg('<b>Mark All:</b><br>This tool has some limitations.<br>It looks at each open square and<br>marks the square with the possible numbers<br>but based on the opening board and<br>ignores your solved squares.')
	for (var j=0;j<89;j=j+10){
		for (var i=0;i<9;i++){
			if (document.f.elements[j+i].value.length !== 1){
				markSquare2(j+i,x);
			}
		}
	}
	remaining(1);//show count remaining
}
function remaining(x){//show count remaining
	countNumbers()
	var count2=0
	for (var i=0;i<9;i++){
		for (var j=0;j<9;j++){
			if (document.f.elements[j+(i*10)].value.length !==1){
				count2=count2+1
			}
		}
	}
	if (x==1){pushundo()}
	document.t.c.value=count2
	if (count2==0){validate()}
	//change link to game href
	var node=document.getElementById('link2');
	node.href='http://www.allsudoku.com/game/index.html?g='+creategamestring(1)
	saveit('inprogress')
	return count2;
}
function storeclass(){
	for (var i=0; i<90; i++){
		document.f.elements[i].old=document.f.elements[i].className
	}
}
function helpon(){
	var node=document.getElementById('help');
	node.style.display='block'
}
function helpoff(){	
	var node=document.getElementById('help');
	node.style.display='none'
}
//-------------------email friend ----------------
function validateemail(){//make sure two valid email addresses  ??? lots more work ???
	x=true;
	if (document.email.to.value.length>0){x=false}
	if (document.email.from.value.length>0){x=false}
	emailfriendoff()	
}
function emailfriendon(){
	var node=document.getElementById('emailfriend');
	node.className='emailfriendon'
	node.style.display='block'
	document.getElementById("emailtable").innerHTML=buildHTMLtable(document.r.restart.value,false)
	document.email.game.value=buildHTMLtable(document.r.restart.value,false)//populate hidden field with html game
	document.email.g1.value=document.r.restart.value//populate hidden field with game string
	document.email.g2.value=creategamestring(0)//populate hidden field with game string
	document.email.g3.value=creategamestring(1)//populate hidden field with game string
	document.email.inprogress[0].checked=true
	document.email.newsletter.checked=false
	document.email.comment.value="Try this game of Sudoku. I think you'll enjoy it."
}
function emailfriendoff(){
	var node=document.getElementById('emailfriend');
	node.style.display='none'
}

//----------- new game -----------------------------
function newgameon(){
	var node=document.getElementById('newgame');
	node.className='newgameon'
	node.style.display='block'
}
function newgameoff(){
	var node=document.getElementById('newgame');
	node.style.display='none'
}
function opennewgame(){
	if(document.ng.gs.value.length){setgame2(document.ng.gs.value);setSudoku()}
}

function displaygame2(gs,title){
	document.ng.gs.value=gs
	document.ng.title.value=title
	document.getElementById('nghtml').innerHTML=buildHTMLtable(gs,true)
}
function getXML (){
	var url = 'http://www.allsudoku.com/rss.xml'
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    req.onreadystatechange = processRequest;
    req.open("GET", url, true);
    req.send(null);
}

function processRequest() {
    if (req.readyState == 4) {
        if (req.status == 200) {
          parseXML();
        } else {
  alert ( "Not able to retrieve xml" );
        }
    }
}

function parseXML() {
	buildHTML4RSSList(req.responseXML.documentElement)
}
function buildHTML4RSSList(rss){
	var items=rss.getElementsByTagName('item');
	var xx=''
	for (i=0;((i<items.length) && (i<6));i++){
		var item=items[i];
		var itemTitle=item.getElementsByTagName('title')[0].firstChild.data;
		var itemCategory=item.getElementsByTagName('category')[0].firstChild.data;
		var itemLink=item.getElementsByTagName('link')[0].firstChild.data;
		var itemPubDate=item.getElementsByTagName('pubDate')[0].firstChild.data;
		var itemGameString=itemLink.substring((itemLink.indexOf('g=')+2),999999)
		//convert gamestring into HTML and add other nice stuff
		var xname=itemPubDate.substring(5,16)+" ("+itemCategory.substring(0,1)+")"
		xx=xx+'<a onDblClick="opennewgame()" href="javascript:displaygame2(\''+itemGameString+'\',\''+xname+'\')">'+xname+'</a>'
		if (i<(items.length-1)){xx=xx+'<br />'}
	}
	xx=xx+''
	document.getElementById('daily').innerHTML=xx;
}


//-------------------    --------------------------
function selectSquare(id,css1,css2){
	//highlight current row, column, cube unless readonly
	restoreclass()
	if (document.f.elements[id].readOnly){
		current=-1;
	}else{
		storeclass();
		highlightrcc(id,css1,css2)
	}
}
		
function highlightrcc(id,css1,css2){	
	//alert('id='+id + ' readonly='+document.f.elements[id].readOnly)
	if (mode==0){//in play, not enter, mode
		var a=buildArray(id)
		for (var i=0; i<a.length; i++){
			document.f.elements[a[i]].className=document.f.elements[a[i]].className+', '+css1
		}
	}
	//document.f.elements[id].style.backgroundColor="#CCCCCC";
	document.f.elements[id].className=document.f.elements[id].className+', '+css2
	current=id
}
function saveit(x){ //save in cookie current
	//document.cookiee.sc.value=saveit2()
	var expiration = new Date();
	expiration.setTime(expiration.getTime() + 31536000000);  //one year 31536000000
	setCookie(x, creategamestring(1), expiration, '/')
}

function restoreit(){
	if (confirm('Are you sure you want to restore your saved game? \nThis will delete your current progress.')){
		setgame2(getCookie('current'));
	}
}
function restartit(){
	if (confirm('Are you sure you want to restart and clear the board? \nThis will delete your current progress.')){
		setgame2(document.r.restart.value);
	}
}

function paste(j){
	if (j=='x'){ //delete
		document.f.elements[current].value=''
	}else{ //insert number
		if (current > -1){
			if (document.f.elements[current].value.length <1){
				document.f.elements[current].value=j
				window.document.f.elements[current].focus()
			}else{
				document.f.elements[current].value=document.f.elements[current].value+j
				window.document.f.elements[current].focus()
			}
		}else{
			displayMsg('<b>Keypad Help</b><br>Keypad for changing selected square:<br>First click on an open square.<br>Then click on number adds mark,<br>Double click replaces with number,<br><i>c</i> clears all marks and<br><i>m</i> automatically marks square')
		}
	}
}
function insert(j){
	paste('x');paste(j);
}
//------------------clock functions
function clearclock(){
	clock=0
	clearTimeout(clock)
	clock=0
	document.t.t.value=""
}
function stopclock(){
	clearTimeout(clock)
}
function resetclock(){//call this to start clock
	//alert('at reset clock')
	clearclock()
	document.t.t.value=0
	document.t.t.second=0
	document.t.t.minute=0
	document.t.t.hour=0
	addtime();
}
function restartclock(){//never used
	addtime();
}
function addtime(){//clock routine depends on displayseconds variable
	document.t.t.second=document.t.t.second+ 1
	if (document.t.t.second > 59){
		document.t.t.second=0
		document.t.t.minute=document.t.t.minute+1
		if (document.t.t.minute > 59){
			document.t.t.minute=0;
			document.t.t.hour=document.t.t.hour+1
		}
	}
	var k="";var s="";
	if (displayseconds){
		if (document.t.t.second <10){
			s="0"
		}
		s=':'+s+document.t.t.second

	}
	if (document.t.t.hour >0){
		k=document.t.t.hour+':'
	}
	document.t.t.value=k+document.t.t.minute+s
	clock=setTimeout('addtime()',600)
}

// An adaptation of Dorcht's function for setting a cookie.
function setCookie(name, value, expires, path, domain, secure) {
  document.cookie = name + "=" + escape(value) + 
  ((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
  ((path == null) ? "" : "; path=" + path) +
  ((domain == null) ? "" : "; domain=" + domain) +
  ((secure == null) ? "" : "; secure");
}

// An adaptation of Dorcht's function for deleting a cookie.
function delCookie (name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path == null) ? "" : "; path=" + path) +
    ((domain == null) ? "" : "; domain=" + domain) +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}
// Heinle's function for retrieving a cookie.
function getCookie(name){
  var cname = name + "=";               
  var dc = document.cookie;             
  if (dc.length > 0) {              
    begin = dc.indexOf(cname);       
    if (begin != -1) {           
      begin += cname.length;       
      end = dc.indexOf(";", begin);
      if (end == -1) end = dc.length;
        return unescape(dc.substring(begin, end));
    } 
  }
  return null;
}
