	var player,yourname,emptystr="",gameover=true;
	var Grid3 = new Array(); for(loop=0;loop<9;loop++) Grid3[loop]=null;
	var tileNames = new Array("top1","top2","top3","mid1","mid2","mid3","bot1","bot2","bot3");
	var dummy,recentTokens = new Array(null,null,null,null,null,null);
	
	function winningLines(a,b,c) {
		this.a = function() {return Grid3[a]};
		this.b = function() {return Grid3[b]};
		this.c = function() {return Grid3[c]};
		this.tiles =[a,b,c];
		this.result = function() {return (this.a()!=null && this.a()==this.b() && this.a()==this.c())};
		this.nt = nearlytrue;  
		this.nf = nearlyfalse;
		this.ttg = twotogo;  
		} 
		
	function nearlytrue() {
		if(this.a()==null && this.b()==true && this.c()==true) return this.tiles[0];
		else if(this.b()==null && this.a()==true && this.c()==true) return this.tiles[1]; 
		else if(this.c()==null && this.b()==true && this.a()==true)  return this.tiles[2];
		else return -1;
		}	

	function nearlyfalse() {
		if(this.a()==null && this.b()==false && this.c()==false) return this.tiles[0];
		else if(this.b()==null && this.a()==false && this.c()==false) return this.tiles[1]; 
		else if(this.c()==null && this.b()==false && this.a()==false)  return this.tiles[2];
		else return -1;
		}	

	function twotogo() {
		var choice;
		if(this.a()==false && this.b()==null && this.c()==null) {choice=rndInRange(1,2); return this.tiles[choice];}
		else if(this.b()==false && this.a()==null && this.c()==null) {choice=rndInRange(0,1)*2; return this.tiles[choice];} 
		else if(this.c()==false && this.b()==null && this.a()==null) {choice=rndInRange(0,1); return this.tiles[choice];}
		else return -1;
		}	

	var wl=new Array(new winningLines(0,1,2),
					new winningLines(3,4,5),
					new winningLines(6,7,8),
					new winningLines(0,3,6),
					new winningLines(1,4,7),
					new winningLines(2,5,8),
					new winningLines(0,4,8),
					new winningLines(2,4,6));
					
	function checklines() {
		var count=0,result="Nobody", done=false, tf;
		while(count<wl.length) {
			done=wl[count].result();
			if(done) {tf=wl[count].a(); count=wl.length}
			count++;
			}
		if(done) if(tf) { you.score++; you.won=true; me.won=false; result=you.name; }
		else { me.score++; me.won=true; you.won=false; result=me.name;}
		return result;
	}

	function Player() {
		this.name = "Simon";
		this.won = false;
		this.score=0;
		this.startedlastgame=true;
		this.token=null;
		}
		
	function token(n) {
		this.neutral=function() {return "Site/tictactoe/"+n+".jpg";}
		this.win=function() { return "Site/tictactoe/"+n+"_win.jpg";}
		this.lose=function() { return "Site/tictactoe/"+n+"_lose.jpg";}
		this.draw=function() { return "Site/tictactoe/"+n+"_draw.jpg";}
		this.pic1=new Image();
		this.pic1.src="Site/tictactoe/"+n+".jpg";
		this.pic2=new Image();
		this.pic2.src="Site/tictactoe/"+n+"_win.jpg";
		this.pic3=new Image();
		this.pic3.src="Site/tictactoe/"+n+"_lose.jpg";
		this.pic4=new Image();
		this.pic4.src="Site/tictactoe/"+n+"_draw.jpg";
		}

	var tokenSet = new Array(new token("bill"), 
							new token("elizabeth"), 
							new token("emma"), 
							new token("gillian"), 
							new token("hugo"), 
							new token("lawrence"), 
							new token("lois"), 
							new token("louise"), 
							new token("maurice"), 
							new token("megan"), 
							new token("natalie"), 
							new token("paul"), 
							new token("robin"), 
							new token("paul"), 
							new token("sally"), 
							new token("simon"), 
							new token("stephen"), 
							new token("sylvia"), 
							new token("veronica"), 
							new token("vinnie") 
							);
							

							
	var you = new Player(); you.startedlastgame=false; you.name="YOU"; 
	var me = new Player(); me.name="I"; 
	player="you";
	
	function rndInRange(l,u) {	return (l+Math.floor(Math.random()*(++u-l))); }
					
	function setTile(tile,clicked) {
		var go,loop,done=true, delay, n,nt;
		if(document.layers)  nt="document.layers['tictac'].document.images";
		else nt="document.tictac_tile";
		n=eval(nt+"["+tile+"]");
		if(you.token==null) {gameover=true; recentTokens=setTokens();gameover=false;}
		if(!gameover) {
			switch(player) {
				case "you":
					if(Grid3[tile]==null) {
						Grid3[tile]=true;
						n.src=you.token.neutral();
					} else done=false;
					break;
				case "me":
					if(clicked) { updatemsg("OI! MY TURN");done=false;}
					else {Grid3[tile]=false; n.src=me.token.neutral();}
				}
			if(done) {
				go=true; 
				winner=checklines();
				if(winner=="Nobody") {
					for(loop=0;loop<Grid3.length;loop++) if(Grid3[loop]==null) go=false;
					if(!go) if(player=="me") {player="you"; updatemsg("YOUR TURN");} 
						else {player="me";  updatemsg("MY TURN"); delay=setTimeout('myTurn()',1000);}
					else {gameover=true; 
						for(loop=0;loop<Grid3.length;loop++) {
							n=eval(nt+"["+loop+"]");
							if(Grid3[loop]==false) n.src=me.token.draw();
							else if(Grid3[loop]==true) n.src=you.token.draw();
					}
						updatemsg("DRAW");}
				} else {gameover=true; 
				for(loop=0;loop<Grid3.length;loop++) {
					n=eval(nt+"["+loop+"]");
					if(Grid3[loop]==false) if(me.won) n.src=me.token.win(); else n.src=me.token.lose();
					else if(Grid3[loop]==true) if(you.won) n.src=you.token.win(); else n.src=you.token.lose();
					}
				updatemsg(winner+" WIN"); }
				
			}}
			else resetGame();
			}
		
	function myTurn() {
		var repeat=true,loop,count=0,t,n;
		var a1 = new Array(), a2 = new Array();
		for(loop=0;loop<Grid3.length;loop++) if(Grid3[loop]==null) {a1[count]=loop; count++;}
		if(a1.length==0) {gameover=true; updatemsg("DRAW");}
		else {
			count=0;
			for(loop=0;loop<wl.length;loop++) {         //check for winners
				n=wl[loop].nf();
				if(n!=-1) {a2[count]=n; count++;}
				}
			if(a2.length==0) {             //if no winners, check for opponent's winners
				for(loop=0;loop<wl.length;loop++) {        
					n=wl[loop].nt();
					if(n!=-1) {a2[count]=n; count++;}
					}}
			if(a2.length==0) {             //now look for lines started by computer player
				for(loop=0;loop<wl.length;loop++) {        
					n=wl[loop].ttg();
					if(n!=-1) {a2[count]=n; count++;}
					}}
			if(a2.length==0) { n=rndInRange(0,a1.length-1); t=a1[n];} else { n=rndInRange(0,a2.length-1); t=a2[n];}
			setTile(t,false);
		}
		}
		
	function setTokens() {
		var l=tokenSet.length-1,r=recentTokens;
		var t1,t2,fail;
		do {
			t2=t1=rndInRange(0,l);
			while(t2==t1) t2=rndInRange(0,l);
			fail=false;
			for(var loop=0;loop<r.length-1;loop++) {
				if(t1==r[loop] || t2==r[loop]) fail=true;
				}
			} while(fail);
		r[5]=r[3];r[4]=r[2]; r[3]=r[1]; r[2]=r[0];r[1]=t1;r[0]=t2;
		you.token=tokenSet[t1];
		me.token=tokenSet[t2];
		return r;
		}


	function resetGame() {
		var d;
		if(document.layers) d=document.layers["tictac"].document.images;
		else d=document.tictac_tile;
		clearTimeout(hideit);
		for(var loop=0;loop<d.length;loop++) d[loop].src="Site/tictactoe/blank.gif";
		for(loop=0;loop<Grid3.length;loop++) Grid3[loop]=null;
		gameover=false;
		if(me.startedlastgame) {me.startedlastgame=false; you.startedlastgame=true; player="you";  updatemsg("YOUR TURN");}
		else {me.startedlastgame=true; you.startedlastgame=false; player="me";  updatemsg("MY TURN"); myTurn();}
		}
	
	function report(str) { 
		var r=str;
		if(!document.layers) r='<span class="tic">'+str+'<\/span>';
		return r
		}
		
	function updatemsg(str){
		var u;
		if(document.all) document.all.tags("td")[10].innerHTML=report(str);
		else if(document.layers) {
			switch(str) {
				case "MY TURN": u="javascript:report('MY TURN')"; break;
				case "YOUR TURN": u="javascript:report('YOUR TURN')"; break;
				case "YOU WIN": u="javascript:report('YOU WIN')"; break;
				case "I WIN": u="javascript:report('I WIN')"; break;
				case "DRAW": u="javascript:report('DRAW')"; 
			}
			document.layers['tictac'].layers['msg1'].src=u;
			}
		}



	function putTic() {
		var x,y,max=getWinDims();
		x=(max[0]-200)/2; y=(max[1]-200)/2;
		if(document.layers) {
			document.layers["tictac"].moveToAbsolute(x,y);
		} else {
			document.all["tictac"].style.left=x+"px";
			document.all["tictac"].style.top=y+"px";
		}}


	function playtic() { 
		if(t_show) showhide("tictac",false);
		else {gameover=true; recentTokens=setTokens();  resetGame(); showhide("tictac",true)} 
		}
	
	
