var QuizType = "names";
var help;
//if (document.myform.Type[0].checked) alert("o checked");
//QuizType = getSelectedRadioValue(myform.Type);
//alert(QuizType)

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function

function init() {
 help="off";
 Score=0;
 Qnum=0;
 HelpCounter=0;
 document.myform.score.value=0;
// ChangeInstruction();
 InitArrays();
 NextQstn();

  if ((defOpt == "on")&&(QuizType == "names")) { forceRadioButton(); }

  ForceHelpOff()

}

function ShowOrHideHelpButton () {


	
  if(document.layers) { //Test for Netscape.    
	
	if (QuizType == 'names') {
		
		document.layers.HelpButton.style.visibility="visible";
	}
	else { 
			
		document.layers.HelpButton.style.visibility="hidden";
	}
	
    }
     else {
	
    	if (QuizType == 'names') { 
		 
		document.all.HelpButton.style.visibility="visible"
	}
	else { 
			
		document.all.HelpButton.style.visibility="hidden"
	}
    }

}

function XChangeInstruction() {
// nop. if needed, overridden in test html
}

function ChangeInstruction() {	

	if (defOpt=="on") {
		if (QuizType=="names") { 
			display('visible', 'hidden');
		}
		else { display('hidden', 'visible'); }
	}

}


function NextQstn() {
  if (help=="on") {
	  document.myform.answerbox.value="Please turn help OFF before continuing";
	  return;
  }
  Qnum++
//alert("question num "+Qnum)
  Points=3
  if (Qnum>MaxQstns)
     { endGame() }
  else
     { 
	//alert("quiztype="+QuizType)
	if (QuizType=="def") {

		if (Qstn[Qnum].def == "") {
			rnd = randomNum();
			Question = "no definition available, click "+Qstn[rnd].name+ " and get free points"
			Answer = Qstn[rnd].name;
		}
		else
		{
		Question=Qstn[Qnum].def;
		Answer=Qstn[Qnum].name 
		}
	}
		else  {	Question=Qstn[Qnum].name; Answer=Qstn[Qnum].name }
	
	
        initForm() }
	
}
function endGame() {
  if (Score==MaxPts)
      gameNarr="Quiz over... Perfect Score! You used help " + HelpCounter + " times."
  else
      gameNarr="Quiz over. You scored " + Score + " out of "+ MaxPts + " and you used help " + HelpCounter + " times."
  document.myform.answerbox.value=gameNarr
  document.myform.question.value=""
  document.myform.qnum.value=""
}

function MakeArray(y) {
  for (var j = 1; j <= y; j++)
  {    this[j] = 0;   }
  return this; 
}

function InitArrays() {

	Qstn= new Array(MaxQstns)
	
	for (i=1; i<=MaxQstns; i++) {
		Qstn[i]=new Object;
	}

	FillQuestionArray();

	Guess=new Array(MaxQstns)
	for (i=1; i<=MaxQstns; i++) {
		Guess[i]=Qstn[i].name;
	}

	fisherYates(Qstn)
}

var currImg = 'unlabeled';

var HelpCounter = 0;

function ForceHelpOff() {

	document["quizImage"].src = quizImage_unlabeled.src;
	currImg = 'unlabeled';
	help="off";
	document.myform.answerbox.value=""


}

function ToggleHelp() {

if (document.images) {

	if (currImg == 'unlabeled') { 
	// show the labeled diagram
  	 	document.images["quizImage"].src= quizImage_labeled.src ;
		currImg = 'labeled';
		help="on";
		HelpCounter++;
	}
	else {
	//  show the unlabeled diagram
	
	    	document["quizImage"].src = quizImage_unlabeled.src;
		currImg = 'unlabeled';
		help="off";
		document.myform.answerbox.value=""
	}
	} 

}

function ShowHelp () {

	

	if (document.images) {
	// show the labeled diagram
  	 	document.images["quizImage"].src= quizImage_labeled.src 
	} 
	//change button label to "Hide help"

}

function HideHelp () {

	if (document.images) {

	//  show the unlabeled diagram
	
	    	document["quizImage"].src = quizImage_unlabeled.src
  	}
	//change button label to "Show help"

}


function ChangeType() {
	
	
	j=2;
	for (i=0; i<j; i++) {
		//alert(document.myform.Type[i].checked)
		if (document.myform.Type[i].checked) { QuizType=document.myform.Type[i].value; }
	}
	//alert(QuizType);
	

	if (document.images) {

		if (QuizType=="names") {	// ask the names, therefore show the unlabeled diagram
	
	    		document["quizImage"].src = quizImage_unlabeled.src
  		}
		else // show the labeled diagram
			{ document.images["quizImage"].src= quizImage_labeled.src }
	} 
	
	ChangeInstruction();
	ShowOrHideHelpButton();
	init();
	
}

function initForm() {
  Points=3
  document.myform.question.value=Question
  document.myform.points.value=Points
  document.myform.qnum.value=Qnum
  document.myform.max.value=MaxPts
  document.myform.answerbox.value=""
}

function chkQstn(Guess) {

   if (help=="on") {

	document.myform.answerbox.value="Please turn help OFF before answering the question"
	return;
	}

   if (Qnum>MaxQstns)
      endGame()
   else
   if (Points==0)
     { document.myform.points.value=""
       document.myform.answerbox.value="No more tries...click next question"  }
   else
   if (Answer==Guess) {
       Score = Score + Points
       document.myform.answerbox.value="That's right ...click next question"
       document.myform.score.value=Score
       Points=0
       document.myform.points.value=""
         }
   else
     { Points--

	if (QuizType== "def") {
	myAns=searchFor(Guess);
	str=Guess + ", " + myAns

	}      
	else str=Guess;

       if (Points==2) {
         document.myform.answerbox.value="No, that's "  + str + ", try again"
         document.myform.points.value=Points }
       else
       if (Points==1) {
         document.myform.answerbox.value="No, that's " + str + ", ...one try left"
         document.myform.points.value=Points }
       else
        { document.myform.points.value=""
          document.myform.answerbox.value="That's " + str + ". No more tries ...click next question" }
      } 
}

function searchFor (Guess) {

	var myAns;
	for (i=1; i<=MaxQstns; i++) {
		if (Qstn[i].name == Guess)
			{ myAns= Qstn[i].def;
			i=MaxQstns+1;
		}
	}
	return myAns;

}

function display(value1,value2){

// RedBoxesInstr says click on the red boxes, so you are doing Names, therefore show help button
  if(document.layers) { //Test for Netscape.    	
	document.layers.RedBoxesInstr.visibility = value1;
        document.layers.LabelsInstr.visibility = value2;
	//document.layers.HelpButton.visibility=value1;
    } else {
    	document.all.RedBoxesInstr.style.visibility = value1;
	document.all.LabelsInstr.style.visibility = value2;
	//document.all.HelpButton.visibility=value1;
       }
}


function displayHB(value){


  if(document.layers) { //Test for Netscape.    	
	document.layers.HelpButton.style.visibility=value;
    } else {
    	
	document.all.HelpButton.style.visibility=value1;
       }
}


function forceRadioButton() { 
 //forcing the Names radio button to be checked when page is loaded
	document.myform.Type[0].checked=true;
 }

function SeeMap(img,ht,wd) {

// add some padding to the height and width
ht=ht+70;
wd=wd+50;
page = "help.html?img="+img;   
mapwin=open(page,"DisplayWindow","toolbar=no,directories=no,scrollbars=yes,menubar=no,height="+ht+",width="+wd+",resize=no");

}


function jumpPage(form) {
         i = form.SelectMenu.selectedIndex;
         if (i == 0) return;
       parent.location.href = url[i+1];

         }



function doPopUp () {

var popUp=false;
	if (popUp) {
	MyWindow=window.open('support.html','MyWindow','toolbar=yes,location=yes,directories=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=600,height=550,left=75,top=75');
	}

}


function MakeArray2()
         {
         this.length = MakeArray2.arguments.length
         for (var i = 0; i < this.length; i++)
         this[i+1] = MakeArray2.arguments[i]
         }
         
function randomNum () {
	var i= MaxQstns;
	return  Math.ceil( Math.random() * ( i + 1 ) );
}

function fisherYates ( myArray ) {
  var i = myArray.length ;
  
  if ( i == -1 ) return false;
  while ( --i ) {
     var j = Math.ceil( Math.random() * ( i + 1 ) );
     var tempi = myArray[i];
     var tempj = myArray[j];
     myArray[i] = tempj;
     myArray[j] = tempi;
	//alert("swap" + i + " and " + j)
   }
}


