/*******************************************************************

CH-47D -10 CHAPTERS 5 AND 9 QUIZ SCRIPT

JAVASCRIPT AND HTML BY RYLI DUNLAP
ryli@transvec.com

NOTES:
- THIS IS THE SAME SCRIPT USED IN THE TH-67 5 AND 9 QUIZZER EXCEPT FOR A DIFFERENT .JS INCLUDE FILE AND SOME MINOR CHANGES TO THE TEXT AND A DIFFERENT PHOTO
- ALL YOU WEB DESIGN GURUS: FEEL FREE TO MODIFY/TWEAK THIS IF YOU WANT IF YOU THINK YOU CAN MAKE IT BETTER. LET ME KNOW IF YOU HAVE ANY IDEAS TO IMPROVE IT 

RYLI

*******************************************************************/

//*******************Indicates last update of this script logic affecting functionality
Script_Last_Updated="28NOV09";
//*******************

//*******************Indicates last update to index.asp affecting only layout/formatting
Formatting_Last_Updated="24DEC09";
//*******************

function resumequiz() { //called when the "Return to Quiz" button is clicked after clicking on "Show All"
	document.getElementById('updateable').innerHTML = "<center>Use the form above to begin a new quiz</center>";
	//document.selectform.disabled = 0;
	disableform(0);
	
}
function highlight(highlightthis) { //strips carats and places span tag around values that will be answers to blanks in quiz to highlight them
	answerhighlighted = "";
	answersplit = highlightthis.split("^");
	for (j = 0; j < answersplit.length; j++) {
		if ((j + 1) % 2 == 0) { //highlighted values will be every other value in answersplit array starting with array position 1
			answersplit[j] = "<span class=\"spanhighlight\">" + answersplit[j] + "</span>";
		}
		answerhighlighted += answersplit[j];
	}
	return answerhighlighted;
}
function showall() { //runs through the 5 and 9 arrays systematically, filters out the carats (^), highlights the values, and formats them in a table and displays them. 
	document.getElementById('progressdiv').innerHTML = "";
	//document.selectform.disabled = 1;
	disableform(1);
	buildshowallstring = "<center><input type=button value=\"Return to Quiz\" onClick=\"resumequiz()\"></center><p><table><tr class=\"tblhdr\"><td align=center>Chapter</td><td align=center>#</td><td align=center>Question</td><td align=center>Answer</td></tr>";
	for (i = 0; i < fivequestion.length; i++) {
		if (i == 0) {
			buildshowallstring += "<tr><td rowspan=" + fivequestion.length + " align=center valign=center style=\"font-size:50px;background-color:#8cbbdd;\">F<br>I<br>V<br>E</td><td>" + eval(i + 1) + "</td><td>" + fivequestion[i] + "</td><td>" + highlight(fiveanswer[i]) + "</td></tr>";
		} else {
			buildshowallstring += "<tr><td>" + eval(i + 1) + "</td><td>" + fivequestion[i] + "</td><td>" + highlight(fiveanswer[i]) + "</td></tr>";
		}
	}
	for (i = 0; i < ninequestion.length; i++) {
		if (i == 0) {
			buildshowallstring += "<tr><td rowspan=" + ninequestion.length + " align=center valign=center style=\"font-size:50px;background-color:#d6293a;\">N<br>I<br>N<br>E</td><td>" + eval(i + 1) + "</td><td>" + ninequestion[i] + "</td><td>" + highlight(nineanswer[i]) + "</td></tr>";
		} else {
			buildshowallstring += "<tr><td>" + eval(i + 1) + "</td><td>" + ninequestion[i] + "</td><td>" + highlight(nineanswer[i]) + "</td></tr>";
		}
	}
	buildshowallstring += "</table><p><center><input type=button value=\"Return to Quiz\" onClick=\"resumequiz()\"></center>";
	document.getElementById('updateable').innerHTML = buildshowallstring;
}
function validate() { //form validation for selectform. called when "Begin" is clicked
	errormsg = "";
	fiveallorsel = -1;
	nineallorsel = -1;
	function thrownumerror(checkthis) { //validates numbers. encapsulated within validate() because only validate() uses this function
		if (isNaN(checkthis)) {
			errormsg += "* One of the values you entered is not a number.<br>";
		}
		if (checkthis.indexOf('.') != -1) {
			errormsg += "* One of the values you entered contains a decimal.<br>";
		}
		if (checkthis < 1) {
			errormsg += "* One of the values you entered is less than 1 or nothing at all.<br>";
		}
	}
	if (document.selectform.fivecheck.checked == 1) {
		if (document.selectform.fiveallorsel[0].checked == 0 && document.selectform.fiveallorsel[1].checked == 0) {
			errormsg += "* Please indicate whether you would like to review all of chapter 5 or a selection of chapter 5.<br>";
		} else if (document.selectform.fiveallorsel[0].checked == 1) {
			fiveallorsel = 1;
		} else {
			fiveallorsel = 0;
		}
		if (fiveallorsel == 0) {
			thrownumerror(document.selectform.fivemin.value);
			thrownumerror(document.selectform.fivemax.value);
		}
	}
	if (document.selectform.ninecheck.checked == 1) {
		if (document.selectform.nineallorsel[0].checked == 0 && document.selectform.nineallorsel[1].checked == 0) {
			errormsg += "* Please indicate whether you would like to review all of chapter 9 or a selection of chapter 9.<br>";
		} else if (document.selectform.nineallorsel[0].checked == 1) {
			nineallorsel = 1;
		} else {
			nineallorsel = 0;
		}
		if (nineallorsel == 0) {
			thrownumerror(document.selectform.ninemin.value);
			thrownumerror(document.selectform.ninemax.value);
		}
	}
	if (document.selectform.fivecheck.checked == 0 && document.selectform.ninecheck.checked == 0) {
		errormsg += "* Please select whether you would like to review chapter 5, chapter 9, or both chapters.<br>";
	}
	if (document.selectform.inorder[0].checked == 0 && document.selectform.inorder[1].checked == 0) {
		errormsg += "* Please select a sequence.<br>";
	} else if (document.selectform.inorder[0].checked == 1) {
		inorder = 1;
	} else {
		inorder = 0;
	}
	if (errormsg == "") {
		if (document.selectform.fivecheck.checked == 1 && fiveallorsel == 0) {
			if (eval(document.selectform.fivemax.value) > fivequestion.length) {
				errormsg += "* The value you entered for chapter 5 is too high - there aren't that many questions.<br>";
			}
			if (eval(document.selectform.fivemax.value) < eval(document.selectform.fivemin.value)) {
				errormsg += "* The range you entered for chapter 5 doesn't make any sense.<br>";
			}
		}
		if (document.selectform.ninecheck.checked == 1 && nineallorsel == 0) {
			if (eval(document.selectform.ninemax.value) > ninequestion.length) {
				errormsg += "* The value you entered for chapter 9 is too high - there aren't that many questions.<br>";
			}
			if (eval(document.selectform.ninemax.value) < eval(document.selectform.ninemin.value)) {
				errormsg += "* The range you entered for chapter 9 doesn't make any sense.<br>";
			}
		}
	}
	if (errormsg == "") {
		if (fiveallorsel == 0) {
			fivemin = document.selectform.fivemin.value;
			fivemax = document.selectform.fivemax.value;
		} else {
			fivemin = 1;
			fivemax = fivequestion.length;
		}
		if (nineallorsel == 0) {
			ninemin = document.selectform.ninemin.value;
			ninemax = document.selectform.ninemax.value;
		} else {
			ninemin = 1;
			ninemax = ninequestion.length;
		}
		if (document.selectform.fivecheck.checked == 0) {
			fivemin = 0;
			fivemax = 0;
		}
		if (document.selectform.ninecheck.checked == 0) {
			ninemin = 0;
			ninemax = 0;
		}
		for (i = 0; i < document.selectform.inorder.length; i++) {
			if (document.selectform.inorder[i].checked == 1) {
				inorder = document.selectform.inorder[i].value;
			}
		}
		compilemasterorder(fivemin, fivemax, ninemin, ninemax, inorder, "q");
	} else {
		document.getElementById('progressdiv').innerHTML = ""; //clear the progress bar before printing out error messages for next quiz
		document.getElementById('updateable').innerHTML = "Oops! There's a few things that need to be fixed before the review can begin:<p>" + errormsg;
	}
}
function randnum(min, max) { //generates a random number for the range specified in min, max
	return Math.floor(Math.random() * (max - min + 1) + min);
}

function disableform(enordis){ //custom function to enable/disable selectform. can't use document.selectform.disabled = 1; anymore because xhtml formatting causes abort button to become disabled if this is called. So, we manually disable everything EXCEPT the abort button. 0 psssed as argument reeanbles controls.
  formcontrolnames=new Array("fiveallorsel[0]","fiveallorsel[1]","fivecheck","fivemin","fivemax","ninecheck","ninemin","ninemax","inorder[0]","inorder[1]","beginbut","showallbut","nineallorsel[0]","nineallorsel[1]");
  
  
  if(enordis==0){    //do not reenable the five/nineallorsel radio buttons if the five/ninecheck box is not checked 
  
    if(document.selectform.fivecheck.checked == 0){
    
     formcontrolnames.splice(0,2) //remove the first 2 members of the array - this prevents them from getting reenabled
    }
    if(document.selectform.ninecheck.checked == 0){
    
    formcontrolnames.splice(formcontrolnames.length-2,2) ////remove the last 2 members of the array - this prevents them from getting reenabled
    }
    //alert(formcontrolnames)
  }
  
             //alert(formcontrolnames)
  //if(enordis){
  for(disableloop=0;disableloop<formcontrolnames.length;disableloop++){
        disablethis=eval("document.selectform."+formcontrolnames[disableloop])
    //alert(disablethis)
    disablethis.disabled=enordis;
      //alert(formcontrolnames[disableloop])
  }
  
  
   //}
   // else{
           //       for(disableloop=0;disableloop<formcontrolnames.length;disableloop++){
       // disablethis=eval("document.selectform."+formcontrolnames[disableloop])
    //disablethis.disabled=enordis;
      //alert(formcontrolnames[disableloop])
  //}
    
    //}
  
  /*document.selectform.fivecheck.disabled=1;
  document.selectform.fiveallorsel[0].disabled=1;
  document.selectform.fiveallorsel[1].disabled=1;
  document.selectform.fivemin.disabled=1;
  document.selectform.fivemax.disabled=1;
  
  document.selectform.ninecheck.disabled=1;
  document.selectform.nineallorsel[0].disabled=1;
  document.selectform.nineallorsel[1].disabled=1;
  document.selectform.ninemin.disabled=1;
  document.selectform.ninemax.disabled=1;
  
  document.selectform.inorder[0].disabled=1;
  document.selectform.inorder[1].disabled=1;
  
  document.selectform.beginbut.disabled=1;
  document.selectform.showallbut.disabled=1; */
}

function compilemasterorder(fivemin, fivemax, ninemin, ninemax, inorder, qora) { //assembles a list of questref strings in an array that determines the order of questions.
	fiveseq = new Array();
	if (fivemin != 0 && inorder == 0) {
		for (i = fivemin - 1; i < fivemax; i++) {
			sameflag = 0;
			newrandnum = randnum(fivemin - 1, fivemax - 1);
			for (j = 0; j < fiveseq.length; j++) {
				if (fiveseq[j] == newrandnum) {
					sameflag++;
					break;
				}
			}
			if (sameflag == 0) {
				fiveseq[fiveseq.length] = newrandnum;
			} else {
				i--;
			}
		}
	}
	if (fivemin != 0 && inorder == 1) {
		for (i = fivemin - 1; i < fivemax; i++) {
			fiveseq[fiveseq.length] = i;
		}
	}
	nineseq = new Array();
	if (ninemin != 0 && inorder == 0) {
		for (i = ninemin - 1; i < ninemax; i++) {
			sameflag = 0;
			newrandnum = randnum(ninemin - 1, ninemax - 1);
			for (j = 0; j < nineseq.length; j++) {
				if (nineseq[j] == newrandnum) {
					sameflag++;
					break;
				}
			}
			if (sameflag == 0) {
				nineseq[nineseq.length] = newrandnum
			} else {
				i--;
			}
		}
	}
	if (ninemin != 0 && inorder == 1) {
		for (i = ninemin - 1; i < ninemax; i++) {
			nineseq[nineseq.length] = i;
		}
	}
	masterorder = new Array();
	if (inorder == 0) {
		while (fiveseq.length > 0 || nineseq.length > 0) {
			if (randnum(0, 1) == 0) {
				if (fiveseq.length > 0) {
					masterorder[masterorder.length] = "f" + fiveseq.shift();
				}
				if (nineseq.length > 0) {
					masterorder[masterorder.length] = "n" + nineseq.shift();
				}
			} else {
				if (nineseq.length > 0) {
					masterorder[masterorder.length] = "n" + nineseq.shift();
				}
				if (fiveseq.length > 0) {
					masterorder[masterorder.length] = "f" + fiveseq.shift();
				}
			}
		}
	} else {
		while (fiveseq.length > 0) {
			masterorder[masterorder.length] = "f" + fiveseq.shift();
		}
		while (nineseq.length > 0) {
			masterorder[masterorder.length] = "n" + nineseq.shift();
		}
	}
	if (qora == "q") { //always going to be q now that quiz is fill in the blank
		for (i = 0; i < masterorder.length; i++) {
			masterorder[i] += "q";
		}
	} else if (qora == "a") { //no longer used for fitb
		for (i = 0; i < masterorder.length; i++) {
			masterorder[i] += "a";
		}
	} else { //no longer used for fitb
		for (i = 0; i < masterorder.length; i++) {
			if (randnum(0, 1) == 0) {
				masterorder[i] += "q";
			} else {
				masterorder[i] += "a";
			}
		}
	}
	//document.selectform.disabled = 1; //done here because if it was done any earlier, it would just have to be enabled again if form validation failed
	//for(disableloop=0;disableloop<=15;disableloop++){
  //document.selectform[disableloop].disabled=1;
  //}
  //document.getElementById('abortbutton').disabled=0;
  /*document.selectform.fivecheck.disabled=1;
  document.selectform.fiveallorsel[0].disabled=1;
  document.selectform.fiveallorsel[1].disabled=1;
  document.selectform.fivemin.disabled=1;
  document.selectform.fivemax.disabled=1;
  
  document.selectform.ninecheck.disabled=1;
  document.selectform.nineallorsel[0].disabled=1;
  document.selectform.nineallorsel[1].disabled=1;
  document.selectform.ninemin.disabled=1;
  document.selectform.ninemax.disabled=1;
  
  document.selectform.inorder[0].disabled=1;
  document.selectform.inorder[1].disabled=1;
  
  document.selectform.beginbut.disabled=1;
  document.selectform.showallbut.disabled=1;*/
  disableform(1);
  document.getElementById('abortbutton').style.visibility = "visible"; //unhide the abort button
	tallyright = new Array(); //tracks right answers
	tallywrong = new Array(); //tracks wrong answers
	initialmasterorderlength = masterorder.length; //used to determine the number of question the user is on
	fullquizloop();
}
function fullquizloop(abort) { //determines the next question to be presented to the user or initiates displayresults() if no more questions remaining. 1 is passed to abort 
	if (masterorder.length > 0 && abort != 1) {
		questref = masterorder.shift();
		questrefint = questref.substring(1, questref.length - 1);
		buildprogressbar();
		showquestion(questref.charAt(0), questrefint, questref.charAt(questref.length - 1)); //sends questref characters as arguments so next question can be built
/*new*/ gotimer();	
	} else {
/*new*/ stoptimer();		
		//document.selectform.disabled = 0; //quiz done, let user use selectform again
		disableform(0);
		document.getElementById('inputdiv').style.visibility = "hidden"; //this will continue to show the last question even after grading. Will confuse user - hide it
		document.getElementById('abortbutton').style.visibility = "hidden"; //hide the abort button
		if (abort != 1) {
			buildprogressbar(1); //show progress bar with summary even though quiz is complete - argument of 1 indicates quiz is complete
			displayresults();
		} else {
			buildprogressbar(2); //show abort message on progress bar
			document.getElementById('gotitwrong').innerHTML = ""; //clear any error messages
			document.getElementById('updateable').innerHTML = "<center>Quiz aborted.<p>Use the form above to begin a new quiz.</center>";
		}
	}
}
function displayresults() { //grades the quiz and presents user with a summary of the results by reading tallywrong and tallyright arrays
//totalquestions=0;//The total number of questions asked during the quiz. Used to calculate percentages
skippedwrong=0;//How many the user skipped
multipleattempts=0;//How many the user got right after more than 1 attempt
for(i=0;i<tallywrong.length;i++){//Loops through tallywrong array to determine how many questions were skipped and how many were answered correctly after multiple attempts
if(tallywrong[i][2]==1){
skippedwrong++
}
else{
multipleattempts++
}
}	
scorepercent = Math.floor((tallyright.length / eval(tallywrong.length + tallyright.length)) * 100);//Overall score based on questions answered correctly on the 1st attempt.
scorepercentmulti = Math.floor((multipleattempts / eval(tallywrong.length + tallyright.length)) * 100);//Percentage of questions correct after multiple attempts.
scorepercentskipped = Math.floor((skippedwrong / eval(tallywrong.length + tallyright.length)) * 100);//Percentage of questions skipped.

function plural(number,word){ //Adds an 's' to a word passed as an argument if the associated number isn't 1
	if(number!=1){
	return String(number+" "+word+"s");	
	}
	else{
		return String(number+" "+word);
	}
}

(scorepercent >= 90) ? scorepercentcolor = "#1eb049": scorepercentcolor = "#d6293a";
	//document.getElementById('updateable').innerHTML = "<center><br>You got " + tallyright.length + " out of " + eval(tallywrong.length + tallyright.length) + " questions right on the initial attempt.<p><span style=\"font-size:20;font-weight:bold;color:" + scorepercentcolor + ";\">" + scorepercent + "%</span></center>";
	//document.getElementById('updateable').innerHTML += "<center><p>Here is a summary of all questions presented:</center><p>";
	statsstring="<center>Your stats out of a total of "+plural(eval(tallywrong.length + tallyright.length),"question")+":<br>"+plural(tallyright.length,"question")+" right on the initial attempt. The overall score below was determined from this number";
	if(multipleattempts!=0){
	statsstring+="<br>"+plural(multipleattempts,"question")+" right after attempting more than once ("+scorepercentmulti+"%)";
	}
	if(skippedwrong!=0){
	statsstring+="<br>"+plural(skippedwrong,"question")+" skipped ("+scorepercentskipped+"%)";
}
statsstring+="<br>Time taken to complete the quiz: "+timeroutput+"<p><span style=\"font-size:20;font-weight:bold;color:" + scorepercentcolor + ";\">" + scorepercent + "%</span></center>";
	document.getElementById('updateable').innerHTML =statsstring+ "<center><p>Here is a summary of all questions presented (Red=Skipped, Yellow=Correct After Multiple Attempts, Green=Correct On 1st Attempt):</center><p>";

	
	summarystring = "<center><table><tr class=\"tblhdr\"><td align=center>Chapter</td><td align=center>#</td><td align=center>Question</td><td align=center>Correct Answer</td><td align=center>Your Response</td></tr>"; //<table><tr class=\"tblhdr\"><td align=center>Reference</td><td align=center>Question</td><td align=center>Correct Answer</td><td align=center>Your Response</td></tr>
	for (i = 0; i < tallywrong.length; i++) {
		questref = tallywrong[i][0];
		questrefint = questref.substring(1, questref.length - 1);
		fiveornine = questref.charAt(0);
		buildcommand = "";
		(fiveornine == "f") ? buildcommand += "five": buildcommand += "nine";
		redoryellow = 0; //red 0 yellow 1
		redoryellowstring = ""; //construct this string and then apend it after summarystring
		if (tallywrong[i][2] == 1) {
			if (tallywrong[i][1] > 0) {
				redoryellow = 0;
				redoryellowstring += "<td>Attempted " + tallywrong[i][1] + " time";
				(tallywrong[i][1] > 1) ? redoryellowstring += "s": redoryellowstring += "";
				redoryellowstring += ". Skipped.</td></tr>";
			} else {
				redoryellow = 0;
				redoryellowstring += "<td>Skipped.</td></tr>";
			}
		} else {
			redoryellow = 1;
			redoryellowstring += "<td>Correct on attempt " + eval(tallywrong[i][1] + 1) + ".</td></tr>";
		} (redoryellow == 0) ? summarystring += "<tr style=\"background-color:#d6293a;\"><td>": summarystring += "<tr style=\"background-color:yellow;\"><td>";
		(fiveornine == "f") ? summarystring += "5</td>": summarystring += "9</td>";
		summarystring += "<td>" + (eval(questrefint) + 1) + "</td><td>" + eval(buildcommand + "question[" + questrefint + "]") + "</td><td>" + highlight(eval(buildcommand + "answer[" + questrefint + "]")) + "</td>" + redoryellowstring;
	}
	for (i = 0; i < tallyright.length; i++) {
		questref = tallyright[i];
		questrefint = questref.substring(1, questref.length - 1);
		fiveornine = questref.charAt(0);
		buildcommand = "";
		(fiveornine == "f") ? buildcommand += "five": buildcommand += "nine";
		summarystring += "<tr style=\"background-color:#1eb049;\"><td>";
		(fiveornine == "f") ? summarystring += "5</td>": summarystring += "9</td>";
		summarystring += "<td>" + (eval(questrefint) + 1) + "</td><td>" + eval(buildcommand + "question[" + questrefint + "]") + "</td><td>" + highlight(eval(buildcommand + "answer[" + questrefint + "]")) + "</td><td>Correct on initial attempt.</td></tr>";
	}
	document.getElementById('updateable').innerHTML += summarystring + "</table></center><center><p>Use the form above to begin another quiz</center>";
}
function buildprogressbar(quizcomplete) { //builds progress bar. arguments: 1 flags quiz is complete, 2 is abort
	if (quizcomplete != 2) { //only update these if user isn't aborting
		numofrightsofar = tallyright.length;
		numofretriedsofar = 0;
		numofskipssofar = 0;
		for (i = 0; i < tallywrong.length; i++) {
			if (tallywrong[i][2] == 1) {
				numofskipssofar++;
				continue;
			}
			numofretriedsofar++;
		}
	}
	incrementsof = Math.ceil(500 / initialmasterorderlength);
	overallproglength = Math.ceil(500 / initialmasterorderlength) * initialmasterorderlength;
	ninetypercent = Math.floor(overallproglength * .9);
	proggreenwidth = incrementsof * numofrightsofar;
	progyellowwidth = incrementsof * numofretriedsofar;
	progredwidth = incrementsof * numofskipssofar;
	progwhitewidth = overallproglength - (proggreenwidth + progyellowwidth + progredwidth);
	if ((quizcomplete != 1) && (quizcomplete != 2)) {
		stats = "Question " + eval(initialmasterorderlength - masterorder.length) + "/" + initialmasterorderlength + " [" + Math.floor(eval((((initialmasterorderlength - masterorder.length) - 1) / initialmasterorderlength) * 100)) + "% Complete]";
		activetrackerstring = "So far you've missed " + tallywrong.length + " and are standing at " + Math.floor(100 - eval((tallywrong.length / initialmasterorderlength) * 100)) + "%.";
	} else { (quizcomplete == 1) ? stats = "[Complete]": stats = "[Aborted]";
		activetrackerstring = "&nbsp;";
	}
	progressbarstring = "<center><table cellpadding=0 width=" + eval(overallproglength) + "><tr><td><table height=24 width=" + eval(overallproglength + 2) + " class=\"progtable\" cellpadding=0 cellspacing=0>" + "<tr><td width=" + ninetypercent + "></td><td width=" + eval(overallproglength - ninetypercent) + " height=2 class=\"progtablecellbgninety\"></td><td rowspan=3 width=2 class=\"progtablecellbgninety\"></td>" + "</tr><tr><td colspan=2><table class=\"progtablemid\" width=" + overallproglength + " class=\"progtable\" cellpadding=0 cellspacing=0><tr><td width=" + proggreenwidth + " class=\"progtablecellgrn\"></td><td width=" + progyellowwidth + " class=\"progtablecellyel\"></td><td width=" + progredwidth + " class=\"progtablecellred\"></td><td width=" + progwhitewidth + " class=\"progtablecellwht\"></td></table>" + "</td></tr><tr><td></td><td height=2 class=\"progtablecellbgninety\"></td></tr></table></td></tr><!--<td class=\"progtext\" width=10>&#60;-</td>--><td class=\"progtext\" valign=top align=center width=540>" + activetrackerstring + "<div class=\"progtext\" style=\"position:relative;top:-36px;width:" + eval(overallproglength + 2) + ";color:#000000;\"><center>" + stats + "</center></div></td></table></center>";
	document.getElementById('progressdiv').innerHTML = progressbarstring;
}
function showquestion(fiveornine, questionnum, qora) { //retrieves the correct value from fivequestion or ninequestion and displays it. calls buildfitbhtml()
	buildcommand = "";
	buildfitb = "";
	(fiveornine == "f") ? buildcommand += "five": buildcommand += "nine";
	(fiveornine == "f") ? buildfitb += "five": buildfitb += "nine";
	(qora == "q") ? buildcommand += "question": buildcommand += "answer";
	document.getElementById('updateable').innerHTML = "<center><b>" + eval(buildcommand + "[" + questionnum + "]") + "</b></center><br>";
	document.getElementById('inputdiv').style.visibility = "visible";
	document.getElementById('inputdiv').innerHTML = buildfitbhtml(eval(buildfitb + "answer[" + questionnum + "]"));
	if (document.answerform.length == 3) { //disable "Skip" and "Give Up" buttons for questions with no blanks
		document.answerform.skipbutton.disabled = 1;
		document.answerform.giveupbutton.disabled = 1;
	}
	self.setTimeout('setfocus()', 200); //delay necessary in order for innerHTML of inputdiv to load up first before setting focus to first text box
}
function setfocus() { //sets focus to first text box or "Continue" button if there are no blanks
	if (document.answerform.length > 3) { //length of form is already 3 due to the "Continue" and "Skip" buttons
		document.answerform.fitb0.focus();
	} else {
		document.answerform.elements[0].focus();
	}
}
function detectkeys() { //handling for spacebar and enter
	if (event.keyCode == 32) { //spacebar will act as tab and shift focus to the next textbox
		event.keyCode = 9;
	}
	if (event.keyCode == 13) { //enter will do the same as clicking "Continue" - call fitbrefresh();
		fitbrefresh();
		return false; //prevents form from submitting on enter if there is only 1 input box
	}
}
function buildfitbhtml(questionref) { //retrieves value from fiveanswer or nineanswer, inserts blanks, and displays the question	
	splitit = questionref.split("^");
	blanks = new Array();
	for (i = 0; i < Math.floor(splitit.length / 2); i++) {
		blanks[i] = splitit[(i * 2) + 1]; //blanks will be every other string in splitit array starting with position 1. load the answers to the blanks in blanks array
		splitit[(i * 2) + 1] = "<input style=\"border-style:solid;border-width:1px;\" onKeyDown=\"return detectkeys()\" size=10 type=text name=\"fitb" + i + "\">"; //substitute text box for the answer
	}
	fitbhtml = "<center><form name=\"answerform\"><table><tr><td style=\"text-align:left;line-height:25px;\">";
	for (i = 0; i < splitit.length; i++) {
		fitbhtml += splitit[i]; //puts array back together in one string now with text boxes instead of answers
	}
	fitbhtml += "</td></tr></table><p><input name=\"contbutton\" type=button value=\"Continue\" onClick=\"if(!document.answerform.contbutton.disabled){fitbrefresh()}\"><input name=\"giveupbutton\" type=button value=\"Give Up\" onClick=\"if(!document.answerform.giveupbutton.disabled){giveup()}\"><input name=\"skipbutton\" type=button value=\"Skip\" onClick=\"if(!document.answerform.skipbutton.disabled){fitbrefresh(1)}\"></form></center>";
	retryflag = 0; //reset each time a new question is presented. Set to 1 if user makes more than one attempt.
	giveupflag = 0; //reset each time a new question is presented. Set to 1 if user clicks "Give Up"
	return fitbhtml;
}
function giveup() { //called when user clicks "Give Up". Fills all the input boxes with the corect answers.
	if (document.answerform.length == 3) { //"Give Up" should already be disabled if question has no blanks. Ignore just in case
		return;
	}
	for (i = 0; i < blanks.length; i++) {
		eval("document.answerform.fitb" + i).value = blanks[i];
		eval("document.answerform.fitb" + i).style.backgroundColor = "#ffffff"; //highlight boxes white in case they're still red
	}
	giveupflag = 1;
	document.answerform.contbutton.disabled = 1; //disable "Continue" button
	document.getElementById('gotitwrong').innerHTML = "<center>These are the correct answers. Since you gave up, you cannot attempt this question again. Click \"Skip\" to continue.</center>"; //inform user these are the corect answers
}
function getordinal(ordinalthis){ //returns 1st 2nd 3rd 4th etc. . . Currently Only fitbrefresh() uses this but it might be useful again in a later update so it got made into its own function.  
ordinalthis=String(ordinalthis);
//teens=0;
//(ordinalthis.length>1) ? buildcommand += "five": buildcommand += "nine";
if(ordinalthis.length>1){
	(ordinalthis.charAt(ordinalthis.length-2)==1) ? teens=1: teens=0;
}
else{
teens=0;	
}

if(!teens){
switch(ordinalthis.charAt(ordinalthis.length-1))
{
case "1":
  apendwith="st";
  break;
case "2":
  apendwith="nd";
  break;
case "3":
  apendwith="rd";
  break;
default:
  apendwith="th";
}
}
else{
	apendwith="th";
}
return ordinalthis+apendwith;
}
function fitbrefresh(skip) { //called when "Continue" is clicked. Determines if question is right or wrong, inputs data into tallyright and tallywrong accordingly. 1 is passed as argument from "Skip" button.
	function stripcommas(stripthis) { //removes commas and .0 from userinput and correct answer from blanks array so they are not marked wrong if user doesn't enter them 
		stripthis = stripthis.replace(/,/g, "");
/*new*/	stripthis = stripthis.replace(".00", "");
		stripthis = stripthis.replace(".0", "");
		return stripthis;
	}
	if (giveupflag) { //if user has clicked "Give Up", treat it like a skip regardless of "Continue" or "Skip" being pressed. "Continue" should be disabled if user gives up, but this is just to make sure the form is not checked again.
		skip = 1;
	}
	flagerror = 0;
	if (skip != 1) { //only check if user isn't skipping or giving up
		interchangeables = new Array("0","zero","1","one", "2","two", "3","three", "4","four","5", "five","6", "six","7", "seven", "8","eight", "9","nine", "10","ten","eng","engine","emer","emergency","cond","condition","disch","discharge","mtr","motor","alt","altitude","inc","increase","dec","decrease","xmsn","transmission","gnd","ground","press","pressure","aux","auxiliary","xfeed","crossfeed","hyd","hydraulic","pwr","power","xfer","transfer","sys","system","util","utility","sel","select"); //alert(hashnums[]);
/*new*/	//	interchangeables = new Array("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten","eng","engine","emer","emergency");
		for (i = 0; i < blanks.length; i++) {
			blankuserinput = stripcommas(eval("document.answerform.fitb" + i + ".value"));
			blankshouldbe = stripcommas(blanks[i]);
			checkhashnums = 0; //alert(blankuserinput+"\n"+blankuserinput.indexOf('.'));
			/*if ((blankuserinput <= 10) && (blankuserinput >= 0) && (blankuserinput != "") && (blankuserinput.indexOf('.') == -1)) { //alert(blankuserinput)
				correctRegExp = new RegExp(hashnums[blankuserinput], "i");
				if ((blankshouldbe.search(correctRegExp) != -1) && (blankshouldbe.length == hashnums[blankuserinput].length)) {
					checkhashnums++;
				}
			} //alert(blankshouldbe+"\n"+blankshouldbe.indexOf('.'));
			if ((blankshouldbe <= 10) && (blankshouldbe >= 0) && (blankshouldbe != "") && (blankshouldbe.indexOf('.') == -1)) {
				correctRegExp = new RegExp(hashnums[blankshouldbe], "i");
				if ((blankuserinput.search(correctRegExp) != -1) && (blankuserinput.length == hashnums[blankshouldbe].length)) {
					checkhashnums++;
				}
			}*/
			correctRegExp = new RegExp(blankuserinput, "i");
			
		/*new*/correctRegExpsb=new RegExp(blankshouldbe,"i");

		/*new*/			for(j=0;j<interchangeables.length;j++){
					if( (interchangeables[j].search(correctRegExp)!=-1) && (blankuserinput.length==interchangeables[j].length) ){
						//alert();
					if((j%2==0)&&(interchangeables[j+1].search(correctRegExpsb)!=-1)&&(interchangeables[j+1].length==blankshouldbe.length)){
						checkhashnums++;
						break;
					}
					
					if((j%2!=0)&&(interchangeables[j-1].search(correctRegExpsb)!=-1)&&(interchangeables[j-1].length==blankshouldbe.length)){
						checkhashnums++;
						break;
					}
						
						
						
						
						}
						//alert("checkhashnums");
				}		
			
			
			
			
			

			if (((blankshouldbe.search(correctRegExp) == -1) || (blankshouldbe.length != blankuserinput.length)) && checkhashnums == 0) {
				flagerror = 1;
				eval("document.answerform.fitb" + i).style.backgroundColor = "#e26b77"; //highlight incorrect text boxes red
			} else {
				eval("document.answerform.fitb" + i).style.backgroundColor = "#ffffff"; //hightlight correct text boxes white
			}
		}
	} else {
		flagerror = 1; //if user skips, it counts as getting the question wrong
	}
	if (blanks.length == 0) { //"Skip" should already be disabled if question has no blanks. Don't penalize for clicking skip on questions with no blanks just in case
		flagerror = 0;
	}
	if (flagerror == 1) {
		if (retryflag == 0) { //only declare the new array entry once
			tallywrong[tallywrong.length] = new Array();
			tallywrong[tallywrong.length - 1][0] = questref; //reference to the question user got wrong
			tallywrong[tallywrong.length - 1][1] = 1; //counts number of retries
			tallywrong[tallywrong.length - 1][2] = 0; //tracks if user skipped question
			retryflag = 1;
		} else {
			tallywrong[tallywrong.length - 1][1]++; //increment the retry counter
		}
		if (skip != 1) {
/*new*/		document.getElementById('gotitwrong').innerHTML = "<center>"+getordinal(tallywrong[tallywrong.length - 1][1])+" attempt failed. Incorrect answers are highlighted. Check your spelling. Try again, give up, or skip.</center>"; //writes error message to gotitwrong div
		} else {
			tallywrong[tallywrong.length - 1][2] = 1; //since user skipped, indicate by setting this array value to 1
			tallywrong[tallywrong.length - 1][1]--; //a skip doesn't count as a try - set retry counter back 1
			fullquizloop(); //continue with the next question
			document.getElementById('gotitwrong').innerHTML = ""; //reset error message
		}
	} else {
		if (retryflag == 0) {
			tallyright[tallyright.length] = questref;
		}
		fullquizloop(); //continue with the next question
		document.getElementById('gotitwrong').innerHTML = ""; //reset error message
	}
}

//************ EXPERIMENTAL AREA BELOW THIS LINE


running=0;
//timer=0;

function stoptimer(){
clearInterval(timerloop);
document.getElementById('timerdiv').innerHTML="";
running=0;
}

function gotimer(){


if(running==1){return;}
running=1;
//alert(running)

timer=0;
clock();
timerloop=setInterval("clock()",1000);
}


function clock(){
//return "bkah"
//clock();
//running=1;
//timer++;	
secs=0;
mins=0;
hours=0;
days=0;
reducer=timer;
timeroutput="";

if(reducer/86400>=1){
	days=Math.floor(reducer/86400);	
}
reducer=reducer-(days*86400)

if(reducer/3600>=1){
	//reducer=reducer/3600;
	hours=Math.floor(reducer/3600);
}
reducer=reducer-(hours*3600);
if(reducer>=1){
mins=Math.floor(reducer/60);	
}
secs=reducer-(mins*60);
//secs=reducer
//}
function placezero(value){
	//alert(value.length)
	if(String(value).length==1){
		return String("0"+value);
	}
	else{
		return value
	}
}
if(days==1){
timeroutput=days+" Day ";	
}
if(days>1){
timeroutput=days+" Days ";	
}
//else{
timeroutput+=placezero(hours)+":"+placezero(mins)+":"+placezero(secs);
//}
document.getElementById('timerdiv').innerHTML=timeroutput;
//return timeroutput;
timer++
//timerloop=setTimeout("clock()",1000);
}
//return timeroutput
//}

//************ FUNCTIONS FOR DHTML INFO WINDOW


infoboxwidth=0
infoboxheight=0

function winfx(openorclose,infoboxcontents){
    repeater=infoboxcontents
//while(infoboxwidth<500){
    //alert(infoboxcontents)
if(openorclose){
    document.getElementById("infobox").style.visibility="visible"
    //sizelimit=500;
infoboxwidth+=20;
infoboxheight+=20;
}
else{
    //document.getElementById("infobox").style.overflow="hidden"
    infoboxwidth-=20;
infoboxheight-=20;
}
document.getElementById("infobox").style.width=infoboxwidth+"px"
document.getElementById("infobox").style.height=infoboxheight+"px"
document.getElementById("infobox").style.top=Math.floor((document.body.clientHeight-infoboxheight)/2)+"px"
document.getElementById("infobox").style.left=Math.floor((document.body.clientWidth-infoboxwidth)/2)+"px"
//alert(openorclose)
if(openorclose){  
  
    if(infoboxwidth<500){setTimeout("winfx(1,repeater)",1)}
    else{
        clearTimeout();
        //document.getElementById("infobox").innerHTML=infoboxcontents
        }
}
else{
    if(infoboxwidth>0){setTimeout("winfx(0)",1)}
    else{
        clearTimeout();
        //document.getElementById("infobox").innerHTML=""//infoboxcontents
        document.getElementById("infobox").style.visibility="hidden"
        }
}        
    //setTimeut("winfx()",1000)
}
//}





//************ PUT THIS HERE SO PAGE WILL VALIDATE AS XHTML
         
function showpresence(presence)
   {
if(presence.statusText!="Offline"){
msnstrng="<br /><br /><a href=\"javascript:void(0)\" onClick=\"window.open('http://settings.messenger.live.com/Conversation/IMMe.aspx?invitee=9e141be311fdfe18@apps.messenger.live.com&mkt=en-US','messengerwin','status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=0,height=300,width=300')\"><img valign=\"middle\" style=\"border-style: none;\" src=\"../imgs/online.gif\" />I'm Online - Click Here to Send IM</a>";
}
else{
msnstrng="";
}
      
   }

