var ok=false, pcbusy=false, userturn=true, butdown=false, tst=true, stage=1, cnt=1, pcclicks=new Array(). userclicks=new Array(), pos;

var btn1=new Image();
btn1.src="btn1.gif";
var btn2=new Image();
btn2.src="btn2.gif";

function updown(isdn){
document.images['pl'+pos].src=(isdn)? 'btn2.gif' : 'btn1.gif';
}

function dopc(){
document.f.st.value=stage;
if(cnt<=stage){
pcbusy=true;
userturn=false;
document.f.s.value="Simon's  Turn";
setTimeout("pos=Math.floor((Math.random()*8)+1) ; updown(true)",500);
setTimeout("updown(false) ; pcclicks[cnt]=pos ; cnt++ ; dopc()",1200);
}else{
userclicks=new Array();
cnt=1;
document.f.s.value="Player's  Turn";
pcbusy=false;
userturn=true;
}}

function testclicks(){
tst=true;
for(i=1;cnt>i;i++)tst=(pcclicks[i]==userclicks[i])? tst && true : tst && false;
(tst)? setTimeout("nextlevel()",300) : setTimeout("startover()",300);
cnt=1;
}

function nextlevel(){
document.f.st.value=++stage;
alert('You did it! Click "Ok" to go on to the next level.');
dopc();
}

function startover(){
stage=1;
cnt=1;
pcclicks=new Array();
document.f.st.value=stage;
alert('Sorry, You missed.\n\nClick "Ok" to start over');
dopc();
}

function testclk(downflag,pos){
if(userturn&&(!pcbusy)){
document.images['pl'+pos].src=(downflag)? 'btn2.gif' : 'btn1.gif';
if(ok&&!downflag){
userclicks[cnt]=pos;
cnt++;
if(pcclicks.length==userclicks.length)testclicks();
}}}

window.onload=function(){
var t='PLAY INSTRUCTIONS:\n\n- Simon starts the game. Remember which order he moves!.\n- When Simon is done, match his moves in the exact order.\n- An alert box will pop up telling you if you matched correctly or not.\n- The text box tells you whose turn it is.\n- Losing restarts the game at level 1, otherwise the level advances.\n- Don\'t try to cheat! Your clicks only count when it is your turn.\n\nNote:  Be sure to press AND release the mouse button while over\neach button. If you don\'t, the button will not change color the way\nit\'s supposed to.\n\nClick "Ok" to start the game...';
alert(t);
ok=true;
dopc();
}