//      Wagner lyric quiz
//	written by Stephen Battey - March 1997
//	modified July 1999
//	 for DEBUGGED @ http://www.truplex.com




//		Set global variables

//		Reply text

ReplyText = new Array ("   ...TRY AGAIN...","   ...HAVE ANOTHER GO...","   ...NOPE !","   ...WRONG !","   ...GIVE IN ?")


//		Current text shown for each lyric

SongsInDecade = new Array()
SongSelected = new Array()
AnswerText = new Array()
ResponseText = new Array()
NoOfTrys = new Array()

titleSizeMax=0
answerSizeMax=0
lyricLineNoMax=0
lyricLineSizeMax=0

for (i=0; i<4; i++)
{
	SongsInDecade[i]=ArtistArray[i].length
	SongSelected[i]=0

	AnswerText[i] = new Array()
	ResponseText[i] = new Array()
	NoOfTrys[i] = new Array()
	for (j=0; j<SongsInDecade[i]; j++)
	{
		AnswerText[i][j] = ""
		ResponseText[i][j] = ""
		NoOfTrys[i][j]=0


		//	1 to allow for last line which has no \n
		lyricLineNo=1
		thisLineSize=1
		lyricLineSize=0
		for (chr=0; chr<LyricArray[i][j].length; chr++)
		{
			if (LyricArray[i][j].substring(chr,chr+1) == "\n") {
				lyricLineNo++
				if (thisLineSize > lyricLineSize)
					lyricLineSize=thisLineSize
				thisLineSize=1
				chr++
			} else
				thisLineSize++
		}

		if ( lyricLineNo>lyricLineNoMax )
			lyricLineNoMax=lyricLineNo
		if ( lyricLineSize>lyricLineSizeMax )
			lyricLineSizeMax=lyricLineSize
		if ( thisLineSize>lyricLineSizeMax )
			lyricLineSizeMax=thisLineSize


		titleSize=TitleArray[i][j].length
		artistSize=ArtistArray[i][j].length

		if ( titleSize>titleSizeMax )
			titleSizeMax=titleSize
		if ( (titleSize+artistSize)>answerSizeMax )
			answerSizeMax=(titleSize+artistSize)
	}
}

//	allow line for year
lyricLineNoMax++

yearSpacingStr=""
for (i=6; i<lyricLineSizeMax; i++)
	yearSpacingStr+=" "


decade=-1
lyricNo=0
tried=0
lyricsShown=0





function DisplayAns() {
//			Show answer when trial is correct
//			or user has 'Give in'

	if ( decade>-1 ) {
		f.reply.value = TitleArray[decade][lyricNo]+"  by "+ArtistArray[decade][lyricNo]
		f.test.focus()
	}
}



function Wrong() {
//			Procedure to handle an incorrect entry

	tried++
	if ( tried==6 )
		tried=5
	f.reply.value = ReplyText[tried-1]
	f.test.focus()
}



function CorrectAns(got) {
//			Returns true if the users guess contains the correct answer
//			Single addition or missing character should be ignored/accepted

	want=TitleArray[decade][lyricNo].toUpperCase()
	for (i=got.length; i<want.length+2; i++)
		got+=" "

	wantCur=0
	gotCur=0
	offset=0
	match=true

	while ( ( match ) & ( wantCur<want.length ) ) {
		WChar=want.substring(wantCur,wantCur+1)
		if ( WChar==got.substring(gotCur,gotCur+1) ) {
			wantCur++
			gotCur++
			wantOff=0
			offset=0
		} else {
			if ( WChar==got.substring(gotCur+1,gotCur+2) ) {
				wantCur++
				gotCur+=2
			} else {
				if ( offset==0 ) {
					wantCur++
					offset=1
				} else
					match=false
			}
		}
	}

	return match
}



function Reply() {
//			Procedure to test an entry and act on
//			the correct/incorrect answer.

	if ( ( f.test.value!="" ) & ( f.test.value.indexOf("CORRECT")==-1 ) ) {
		if ( CorrectAns(f.test.value.toUpperCase()) ) {
			DisplayAns()
			f.test.value="   CORRECT !!"
			setTimeout("f.sel.focus()",100)
		} else {
			Wrong()
			setTimeout("f.test.focus()",100)
		}
	}
	StoreDisplay()
}



function StoreDisplay() {
//			Stores the current display of text for
//			a lyric into the global arrays

	AnswerText[decade][lyricNo]=f.reply.value
	ResponseText[decade][lyricNo]=f.test.value
	NoOfTrys[decade][lyricNo]=tried
}



function RestoreDisplay() {
//			Restores the text display for a lyric
//			from the global arrays

	f.reply.value=AnswerText[decade][lyricNo]
	f.test.value=ResponseText[decade][lyricNo]
	tried=NoOfTrys[decade][lyricNo]
	SongSelected[decade]=lyricNo
	f.disp.value=yearSpacingStr+"(19"+YearArray[decade][lyricNo]+")\n"+LyricArray[decade][lyricNo]
}



function ChangeDecade() {
//			Set the decade and list of possible lyrics
//			before showing the selected lyric for this decade

	//	if not initial call
	if ( decade>-1 )
		//	store settings for this lyric
		StoreDisplay()

	//	get new decade number
	decade=f.dec.selectedIndex

	//	store number of lyrics shown for current and new decades
	shown=lyricsShown
	lyricsShown=SongsInDecade[decade]

	//	add/remove lyrics to the list
	if ( shown<lyricsShown ) {
		for (i=shown; i<lyricsShown; i++) {
			newOpt=new Option("")
			f.sel.options[i]=newOpt
			f.sel.options[i].text="LYRIC "+(i+1)
		}
	} else if ( shown>lyricsShown ) {
		for (i=shown-1; i>=lyricsShown; i-- )
			f.sel.options[i]=null
	}

	//	set lyric number last used for new decade
	lyricNo=SongSelected[decade]
	f.sel.selectedIndex=lyricNo

	RestoreDisplay()
}



function ChangeLyric() {
//			Store the display and change the lyric

	StoreDisplay()
	lyricNo=f.sel.selectedIndex

	RestoreDisplay()
}
