<!--
// Quiz Logic for Guessing Game
// by Matt Gravelle (matt@pineorchard.com)

function typSolution(iQuizNum){
	var w = new Array()
	var ImgData = new Array()
	var Hints = new Array()
	GetQuiz(iQuizNum,w,ImgData,Hints)

	var a = new Array()
	for(i=0;i<w.length;i++){
		a[i]=w[i].word
	}

	this.words = w
	this.hints = Hints
	this.sentence = a.join(" ")+"."
	this.guess = guess
	this.seen = seen
	this.revealwords = revealwords
	//this.solved = false
	this.message = null // Init
	this.guessedsome = false // Init

	this.src=ImgData[0]
	this.width=ImgData[1]
	this.height=ImgData[2]

	this.quizindex=iQuizNum
	this.guesscount=0 // Reset

}

var oSolutio=new typSolution()
var oSolution
var QuizIndex=0


//===================================================

function DocItem(name){
	var o
	o=document.getElementById(name)
	return o
}


function window_onload() {
	InitPageChoices()
	InitSolution(0)
}

function InitPageChoices() {
	var e
	var t=""
	var i
	var p
	for(i=0;i<QuizCount;i++){
		p=i+1
		if(i!=QuizIndex){
			t+=" <a href='javascript:btnPgClick("+p+")'>"+p+"</a> "
		}else{
			t+=" <b><span style='color:#000000'>"+p+"</b></span> "
		}
	}
	e=DocItem("PageChoices")
	e.innerHTML=t
}

function InitSolution(iQuizNum){
	var o = new typSolution(iQuizNum)
	oSolution=o
	var e
	e=DocItem("imgQuiz")
	e.src="pv/"+o.src
	e.width=o.width
	e.height=o.height
	DisplaySolution("")
	EnableAnswerBox(true)
	EnableHints(true)
	ClearMyGuess()
	Abling()
	InitPageChoices()
}

function ClearMyGuess(){
	var eMyGuess = DocItem("MyGuess")
	eMyGuess.value="" //Clear
	InputFocus()
}
function InputFocus(){
	var eMyGuess = DocItem("MyGuess")
	try{eMyGuess.focus()}
	catch(dum){}
}



function btnOKClick(){
	var eMyGuess = DocItem("MyGuess")
	var MyGuess=eMyGuess.value
	oSolution.guess(MyGuess)
	ClearMyGuess()
}

function EnableHints(bln){
	var eHint
	eHint=DocItem("btnGiveHint")
	eHint.disabled=(bln!=true)
	eHint=DocItem("btnGiveWord")
	eHint.disabled=(bln!=true)
}

function EnableAnswerBox(bln){
	var e1
	var e2
	e1=DocItem("AnswerBox")
	e2=DocItem("btnStartNext")
	if(bln){
		e1.style.display=""
		e2.style.display="none"
	}else{
		e1.style.display="none"
		if(QuizIndex+1<QuizCount){
			e2.style.display=""
		}
	}
}


function btnNextClick(){
	if(QuizIndex+1<QuizCount){
		QuizIndex+=1
	}
	InitSolution(QuizIndex)
}

function btnBackClick(){
	if(QuizIndex>0){
		QuizIndex-=1
	}
	InitSolution(QuizIndex)
}


function btnPgClick(p){
	QuizIndex=p-1
	InitSolution(QuizIndex)
}

function btnFirstClick(){
	QuizIndex=0
	InitSolution(QuizIndex)
}
function btnLastClick(){
	QuizIndex=QuizCount-1
	InitSolution(QuizIndex)
}



function Abling(){
	var e
	e=DocItem("btnNext")
	if(QuizIndex+1>=QuizCount){
		e.innerHTML="<span style='color:#808080'>[Next]</span>"
	}else{
		e.innerHTML="[<A href='javascript:btnNextClick()'>Next</a>]"
	}
	e=DocItem("btnBack")
	if(QuizIndex<=0){
		e.innerHTML="<span style='color:#808080'>[Previous]</span>"
	}else{
		e.innerHTML="[<A href='javascript:btnBackClick()'>Previous</a>]"
	}
}


function btnGiveMeClick(what){
	if(what=="word"){
		GiveMeWord()
	}
	if(what=="hint"){
		GiveMeHint()
	}
	InputFocus()
}
function GiveMeWord(){
	var words = oSolution.words
	var i
	for(i=0;i<words.length;i++){
		if(words[i].revealed==false){
			words[i].revealed=true
			//oSolution.guessedsome=false //reset
			DisplaySolution(oSolution.seen())
			return null
		}

	}
}
function GiveMeHint(){
	var hints = oSolution.hints
	var i
	var hint
	if(hints==null){
		hints=new Array(0)
	}
	if(hints.length<1){
		alert("Sorry, no hints for this one. You'll have to guess.")
		return null
	}

	var canhelp=false
	var h
	var iWord
	for(i=0;i<hints.length;i++){
		// Find a hint that will help for a word
		// that is not yet revealed.
		for(h=0;h<hints[i].willhelp.length;h++){
			iWord=hints[i].willhelp[h]
			if(oSolution.words[iWord].revealed==false){
				canhelp=true
				if(hints[i].revealed==false){
					hint=hints[i].hint
					oSolution.message=hint
					hints[i].revealed=true
					DisplaySolution(oSolution.seen())
					return null
				}
			}
		}
	}
	if(canhelp==true){
		// Since all hints were displayed, then start over...
		// alert("Sorry, no more hints.")
		for(i=0;i<hints.length;i++){
			hints[i].revealed=false;
		}
		for(i=0;i<hints.length;i++){
			for(h=0;h<hints[i].willhelp.length;h++){
				iWord=hints[i].willhelp[h]
				if(oSolution.words[iWord].revealed==false){
					hint=hints[i].hint
					oSolution.message=hint
					hints[i].revealed=true
					DisplaySolution(oSolution.seen())
					return null
				}
			}
		}
	}else{
		//alert("Sorry, there are no more hints that would help.")
		oSolution.message="Sorry, there are no more hints that would help."
		DisplaySolution(oSolution.seen())
	}
}



function findkeyword(strWord,start){
	// return position if found, otherwise null
	var words = oSolution.words
	// loop through words
	var i
	for(i=start;i<words.length;i++){
		if(words[i].required==true){
			if(words[i].word.toLowerCase()==strWord.toLowerCase()){
				return i
			}
		}
	}
	return null
}

function findhidden(strWord,start){
	//
	// return position if found, otherwise null
	var words = oSolution.words
	// loop through words
	var i
	for(i=start;i<words.length;i++){
		if(words[i].revealed==false){
			if(IsWordAcceptable(strWord,words[i])==true){
				return i
			}
		}
	}
	return null
}

function IsWordAcceptable(strWord,o){
	var w1=strWord.toLowerCase()
	if(o.word.toLowerCase()==w1){
		return true
	}
	if(o.wordsame==null){
		return false
	}
	if(o.wordsame.length<1){
		return false
	}

	var i
	for(i=0;i<o.wordsame.length;i++){
		if(o.wordsame[i].toLowerCase()==w1){
			return true
		}
	}
	return false
}




function guess(strGuess){

	// compare words and reveal any.

	var g = new String()
	g=strGuess

	this.guessedsome=false // reset
	this.message=null // reset

	//replace weird chars with space.
	g = g.replace("."," ")
	try {g.toLowerCase()}
	catch(dum1) {alert("Error--This browser doesn't understand the .toLowerCase function.")}

	//strGuess is a group of words.
	var guesswords = new Array()
	guesswords= g.split(" ")

	//iterate through each guessed word.
	var guessword
	var pos=null
	var posKeyword=0
	////var posNonKeyword=0
	var i
	for(i=0;i<guesswords.length;i++){
		guessword=guesswords[i]
		//compare only with the required words.
		pos=findkeyword(guessword,posKeyword)
		if(pos==null){
			// try any word that remains hidden
			pos=findhidden(guessword,posKeyword)
			////if(pos!=null){
				////posNonKeyword=pos
			////}
		}else{
			posKeyword=pos+1
		}
		if(pos!=null){
			oSolution.revealwords(pos)
		}
	}
	oSolution.guesscount+=1
	DisplaySolution(oSolution.seen())
	//if(oSolution.guesscount>1){
	//	EnableHints(true)
	//}

}


function revealwords(iKeyword){
	var i
	var willreveal=oSolution.words[iKeyword].willreveal
	if(willreveal!=null){
		for(i=0;i<willreveal.length;i++){
			if(this.words[willreveal[i]].revealed==false){
			this.words[willreveal[i]].revealed=true
			this.guessedsome=true
			}
		}
	}
}


function seen(){
	// get all words that have been revealed so far
	// adds the ellipses
	var a = ""
	var wrote_ellipse=false // Init
	var words = this.words
	var i
	for(i=0;i<words.length;i++){
		if(words[i].revealed==true){
			a+=words[i].word
			if(i<words.length-1){	// space for next word
				a+=" "
			}
			wrote_ellipse=false // reset
		}
		else{
			if(wrote_ellipse==false){// add ellipse and next space
				a+=". . . "
				wrote_ellipse=true
			}
		}
	}
	if(a==". . . "){
		a="" // Clear
	}
	if(a.length>0){
		a+="." // Last period
	}
	return a
}


function DisplaySolution(strReveal){
	var t=oSolution.sentence
	var SolutionReveal=DocItem("SolutionReveal")
	var Prompt=DocItem("Prompt")
	if(strReveal==t){
		SolutionReveal.innerHTML=t;
		SolutionReveal.style.color="#000000";
		//SolutionHow.innerHTML="<BR>";
		Prompt.innerHTML="Correct!"
		EnableHints(false)
		EnableAnswerBox(false)
	}
	else{
		if(strReveal.length>0){
			SolutionReveal.innerHTML=strReveal;
		}
		else{
			SolutionReveal.innerHTML="&nbsp;";
		}
		//SolutionHow.innerHTML="(incomplete)";
		SolutionReveal.style.color="#800000";
		if(oSolution.message!=null){
			msg=oSolution.message
		}
		else{
			if(oSolution.guesscount==0){
				msg="Guess the saying and type in your answer."
			}
			else if(oSolution.guessedsome==false&&oSolution.guesscount>0){
				msg="Try again."
			}
			else{
				msg="Good job! Keep guessing until all words are solved."
			}
		}
		Prompt.innerHTML=msg
	}
}
//-->
