// latest update 3/31/02
// sequence board javascript
// initialize variables

// declare variables
var CurQuestion = 1;
var k = 0; // position of number in each sequence
var a = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0); // value to recalculate

var v = 0; // working variable for calculations
var x = 0; // working variable for calculations
var y = 0; // working variable for calculations
var z = 0; // working variable for calculations
var score = 0;
var Instructstring = new Array("Here is a new sequence. What's the next number?","Correct! What's the next number?","Incorrect, the answer was ","You lost! Press RESET to play again."); //instruction array
var c = 0; // need two numbers in a row correct to advance - this is number correct
var vSeq = ""; // the sequence string itself
// declare controls on the HTML form
var txtSeqNum = document.frmSequence.txtSeqNum;
var txtInstruction = document.frmSequence.txtInstruction;
var txtSequence= document.frmSequence.txtSequence;
var txtAnswer = document.frmSequence.txtAnswer;
var cmdAnswer = document.frmSequence.cmdAnswer;
var txtScore = document.frmSequence.txtScore;

// *************************************************************************
// functions
// *************************************************************************
function StartSequence() {
CheckForEnd();
	if (CurQuestion!=99) {
txtInstruction.value = Instructstring[0];
txtAnswer.value="";
// displayseq will be randomized so that the number of sequesces displayed for each round
// will be different - the user cannot cheat as easily by writing down correct answers
var displayseq = (((Math.random())*4)+5); // from 5 to 8
displayseq = Math.round(displayseq); // round the number
for (k=1; k<=displayseq; k++) {
calculate();
vSeq = vSeq + a[k] + ",";
txtSequence.value = vSeq;
}
k = (k-1); // bring one back from loop to set up for increment
txtAnswer.value="";
txtAnswer.focus(); }
}
// ***************************************************************************
function calculate() {
switch (CurQuestion)
 {
	case 1:
	a[k] = (a[k-1] + 2);
	break;
	
 	case 2:
 	if (k==1) {
	a[k]=1; }
	else {
	a[k]=(2*(a[k-1])); }
	break;
	
	case 3:
	if (k==1) {
	a[k]=1;
	x=1; }
	else {
	x=(x+1);
	a[k]=(a[k-1]+x); }
	break;
	
	case 4:
	if (k==1) {
	a[k]=2048; }
	else {
	a[k]=(a[k-1]/2); }
	break;
	
	case 5:
	if (k==1) {
	a[k]=1;
	a[0]=0; }
	else {
	a[k]=(a[k-1]+a[k-2]); }
	break;
	
	case 6:
	if (k==1) {
	x=0; }
	x=(x+1);
	a[k]=(2*x*x);
	break;
	
	case 7:
	if (k==1) {
	x=12; }
	a[k]=(x*x);
	x=(x+1);
	break;

	case 8:
	if (k==1) {
	x=2; }
	a[k]=((x*(x-1)));
	x=(x+1);
	break;
	
	case 9:
	if (k==1) {
	x=0; }
	x=(x+1);
	a[k]=Math.pow(x,3);
	break;
	
	case 10:
	if (k==1) {
	x=0;
	a[0]=1; }
	a[k]=((a[k-1]+x)*x);
	x=(x+1);
	break;
	
	case 11:
	if ((k==4) || (k==6) || (k==9) || (k==11)) {
	a[k]=30; }
	else if (k==2) {
	a[k]=28; }
	else {
	a[k]=31; }
	break;
	
	case 12:
	if (k==1) {
	x=12;
	a[0]=0; }
	a[k]=(a[k-1]+x);
	x=(x-1);
	break;
	
	case 13:
	if (k==1) {
	a[k]=1;
	x=1; }
	else {
	x=(x+1);
	a[k]=(a[k-1]*x); }
	break;
	
	case 14:
	a[0]=0;
	a[k]=((2*(a[k-1]))+1);
	break;

	case 15:
 	if (k==1) {
 	a[k]=1;
	x=1; }
	else {
	a[k]=((((a[k-1]*2)-2)*2)+x);
	x=(x+1); }
	break;
		
	case 16:
 	if (k==1) {
	x=0;}
	a[k]=Math.floor(Math.pow(x,1/2));
	x=(x+1000);
	break;

	case 17:
	if (k==1) {
	x=2;
	y=4;
	a[k]=6; }
	else {
	x=(x*2);
	y=(y*2);
	a[k]=(x+y); }
		
	break;
	
	case 18:
	if (k==1) {
	a[k]=100; }
	else {
	a[k]=(Math.floor(a[k-1]/2)+Math.floor(a[k-1]/4))  }
	
	break;
	
	case 19:
	if (k==1) {
	x=12; }
	a[k]=Math.floor((360/x)+.5);
	x=(x-1);
	break;
	
	case 20:
	if (k==1) {
	x=1; }
	v=Math.floor(Math.pow(x,2)/100);
	y=Math.floor(((Math.pow(x,2)*100)-v)/10);
	z=((((Math.pow(x,2)*100)*10)-v)-y);
	a[k]=(v+y+z);
	x=(x+1);
	break;
	
	}
	
}
// **********************************************************************
function skipclick(evnt) {
	if (CurQuestion==99) {
alert("The game is over. Press RESET to play again"); }
	else {
		playSound(4);
		c=0;
		CurQuestion = (CurQuestion + 1);
		k=1;
		vSeq="";
		txtSequence.value="";
		txtSeqNum.value=CurQuestion;
		score = score - 4;
		txtScore.value = score;
		StartSequence(); } 
}

// **********************************************************************
function answerclick(evnt) {

if (CurQuestion==99) {
alert("The game is over. Press RESET to play again"); }
	else {
		checkanswer(); }
		
}
// **********************************************************************
function CheckForEnd() {
if (k>=12) {
	CurQuestion=99;
	loser(); }
else if ((CurQuestion>=21) && (score<1)) {
	CurQuestion=99;
	loser(); }
else if ((CurQuestion>=21)&& (score>0)) {
	CurQuestion=99;
	winner(); }

}

// *************************************************************************
function checkanswer() {
var Arraymember = 0; // cannot compare an array object to variable in Netscape
var Ansvalue = 0; // cannot compare an array object to variable in Netscape

if (txtAnswer.value != "") {
k = (k+1);
calculate();
vSeq = vSeq + a[k] + ",";
txtSequence.value = vSeq;
Arraymember = a[k];// cannot compare an array object to variable in Netscape	
Ansvalue=txtAnswer.value; // cannot compare an array object to variable in Netscape

	
	// if correct........
	if (Ansvalue==Arraymember) {
		txtInstruction.value = Instructstring[1];
		txtAnswer.value="";
		c = (c+1);
		score = score + 2;
		txtScore.value = score;
		playSound(0);
		txtAnswer.focus();
		if (c==2) {
			if (CurQuestion==20) {
			score = score + 20;
			txtScore.value = score; 
			CurQuestion = (CurQuestion + 1);
			StartSequence(); }
			else {
			c=0;
		CurQuestion = (CurQuestion + 1);
		score = score + 1; // total=3 - gets 2 from above
		txtScore.value = score;
		
		k=1;
		vSeq="";
		txtSequence.value="";
		txtSeqNum.value=CurQuestion;
		StartSequence(); } } 
	 }

	// if incorrect ........
	else {
		txtInstruction.value = Instructstring[2] + a[k] + ". Try again.";
		txtAnswer.value="";
		score = score - 2;
		txtScore.value = score;
		playSound(1);
		CheckForEnd();
		txtAnswer.focus(); 
	
	c = 0; } 

  }
	
// if no answer entered ........	
	else {
	alert("You have not entered an answer to submit!");
	txtAnswer.focus(); }
	
}
// ***********************************************************************
function winner() {

playSound(2);
alert("You have earned a score of " + score + " points. \nJoin our winner's circle!\nTo post your name in the winner's circle,\nsimply enter your name or screen name in the\nfollowing form and click the submit button!");
var WinWin = window.open("sequenceform.htm");

   


// these values are visible to the user	
var TodayBlock = WinWin.document.WINFORM.TODAY;
var ScreennameBlock = WinWin.document.WINFORM.SCREENNAME;
var FinalScore = WinWin.document.WINFORM.SCORE;
var Hscore=WinWin.document.WINFORM.hSCORE;
// these values are visible to the user	
 	TodayBlock.value = new Date();
	FinalScore.value = score;
	
// these values are invisible to the user (hidden) these will be the correct
// values, even if the user cheats and changes the original values he can see before submiting!
	Hscore.value = score;
	WinWin.focus();
	ScreennameBlock.focus();


	window.close(); 
	
}
// ************************************************************************
function loser() {
// loser!
		playSound(3);
		alert("You have bombed with a score of " + score + " points.\nTry again soon!");   
		window.close(); 

}
// ***********************************************************************
// sound
var aySound = new Array();
// Below: source for sound files to be preloaded
aySound[0] = "correct.wav";
aySound[1] = "incorrect.wav";
aySound[2] = "winner.wav";
aySound[3] = "loser.wav";
aySound[4] = "skip.wav";

// DO NOT edit below this line
document.write('<BGSOUND ID="auIEContainer">');
IE = (navigator.appVersion.indexOf("MSIE")!=-1 && document.all)? 1:0;
NS = (navigator.appName=="Netscape" && navigator.plugins["LiveAudio"])? 1:0;
ver4 = IE||NS? 1:0;
onload=auPreload;

function auPreload() {
if (!ver4) return;
if (NS) auEmb = new Layer(0,window);
else {
Str = "<DIV ID='auEmb' STYLE='position:absolute;'></DIV>";
document.body.insertAdjacentHTML("BeforeEnd",Str);
}
var Str = '';
for (i=0;i<aySound.length;i++)
Str += "<EMBED SRC='"+aySound[i]+"' AUTOSTART='FALSE' HIDDEN='TRUE'>";
if (IE) auEmb.innerHTML = Str;
else {
auEmb.document.open();
auEmb.document.write(Str);
auEmb.document.close();
}
auCon = IE? document.all.auIEContainer:auEmb;
auCon.control = auCtrl;
}
function auCtrl(whSound,play) {
if (IE) this.src = play? aySound[whSound]:'';
else eval("this.document.embeds[whSound]." + (play? "play()":"stop()"));
}
function playSound(whSound) { if (window.auCon) auCon.control(whSound,true); }
function stopSound(whSound) { if (window.auCon) auCon.control(whSound,false); }
// ******************************************************************************

// ************************************************************************
// set up initial control values and begin the first sequence
k=1;
c=0;

CurQuestion = 1;
score=0

txtSeqNum.value = CurQuestion;
score=0;
txtScore.value = score;
txtSequence.value = "getting sequence...";
txtAnswer.value = "";
// start it off
StartSequence();


















