mNumQuestions = 10;
mQ = new Array(mNumQuestions);
mQA = new Array(mNumQuestions);
mQB = new Array(mNumQuestions);
mQC = new Array(mNumQuestions);
mQD = new Array(mNumQuestions);
mQE = new Array(mNumQuestions);
mA = new Array(mNumQuestions);
mR = new Array(mNumQuestions);
mC = new Array(mNumQuestions);
mT = new Array(mNumQuestions);
mE = new Array(mNumQuestions);
mG = new Array(mNumQuestions);
mGW = new Array(mNumQuestions);
mGH = new Array(mNumQuestions);
mN = new Array(mNumQuestions);
mAns = new Array(mNumQuestions);


var mGreenCheckIMG = "<IMG SRC='darkGreenCheck.gif' WIDTH=22 HEIGHT=16>";
var mRedCheckIMG = "<IMG SRC='redCheck.gif' WIDTH=22 HEIGHT=16>";
var mBlankCheckIMG = "<IMG SRC='blankCheck.gif' WIDTH=22 HEIGHT=16>";
var mTStart = "<P><TABLE border=0 cellspacing=0 cellpadding=6>\n";
var mTDBegin = "<TD VALIGN='Top' ALIGN='Left'>";
var mTDBeginR = "<TD VALIGN='Top' ALIGN='Right'>";
var mTDBeginA = "<TD VALIGN='Top' ALIGN='Left'>&nbsp;</TD><TD VALIGN='Top' ALIGN='Left'>";
var mTDBeginB = "<TD VALIGN='Top' ALIGN='Left' BGCOLOR='lightGrey' COLSPAN=2>";
var mTDBeginC = "<TD VALIGN='Top' ALIGN='Left' BGCOLOR='lightGrey'>";
var mRadBeg = "<INPUT TYPE='RADIO' NAME='";
var mRadMid = "' VALUE='";
var mRadEnd = "'>";
var mTABeg = "<TEXTAREA NAME='";
var mTAEnd = "' ROWS='4' COLS='60' WRAP='Virtual'></TEXTAREA>";
var mCopy = "<P>&nbsp;</P><DIV align='right'>&copy; 2002 - 2007 by Mark A. Lane, Ph.D.</DIV>";

var mBlankRow = "<TR><TD>&nbsp;</TD></TR>\n";

var isNav = false;
var isIE = false;
var isNav6 = false;
var mVersion = parseInt(navigator.appVersion);

if (mVersion >= 4) {
	if (navigator.appName == "Netscape") {
		if (mVersion >= 5) {
			isNav6 = true;
		} else {
			isNav = true;
		}
	} else if (navigator.appName == "Microsoft Internet Explorer") {
		isIE = true;
	}
}

function drawLayer() {
	var theLayerStr = "";
	if (isNav) {
		theLayerStr = "<layer name='theQuiz' left=150 top=110><form>"
		theLayerStr += drawMCQuestions();
		theLayerStr += "</form></layer>";
	} else if (isIE || isNav6) {
		theLayerStr = "<div id='theQuiz'><form>";
		theLayerStr += drawMCQuestions();
		theLayerStr += "</form></div>";
	} else {
		theLayerStr = "" + navigator.appName + " " + navigator.appVersion + " " + isNav + isIE + "Netscape Communicator 4.0 or higher or Internet Explorer 4.0 or higher is required for the quiz.";
	}
	return theLayerStr;
}


function createQuestions() {
	var thePV;
	var theFV;
	var theN;
	var theAns = 0;
	var theType = 0;
	var theR;
	var theA;
	var theCF = new Array(8);
	var theNumCF;
	var	theTemp;
	var ii;
	for (i=0; i< mNumQuestions; i++) {
		theType = Math.round(Math.random()*4); // 0 -> PV 1 -> PVCF 2-> FV 3 -> PVA 4 -> FVA
		if (theType == 0) {
			theFV = Math.round(Math.random()*10 + 1)*100;
			theN = Math.round(Math.random()*14 + 1);
			theR = Math.round(Math.random()*20 + 1);
			thePV = Math.round(theFV*Math.pow(1 + theR/100,-theN)*100)/100;
			mQ[i] = "Find the present value of $" + theFV + " to be recieved " + theN + " years from today if the interest rate is " + theR + " per cent.";
			mE[i] = "To solve using the TI BAII Plus calculator:<BR><TABLE>";
			mE[i] += "<TR><TD ALIGN='CENTER'>" + theFV + "</TD><TD ALIGN='CENTER'>[ FV ]</TD></TR>";
			mE[i] += "<TR><TD ALIGN='CENTER'>" + theR  + "</TD><TD ALIGN='CENTER'>[ I/Y ]</TD></TR>";
			mE[i] += "<TR><TD ALIGN='CENTER'>" + theN + "</TD><TD ALIGN='CENTER'>[ N ]</TD></TR>";
			mE[i] += "<TR><TD ALIGN='CENTER'>[ CPT ]</TD><TD ALIGN='CENTER'>[ PV ]</TD></TR></TABLE>";
			theAns = thePV;
		} else if (theType == 1) {
			theR = Math.round(Math.random()*20 + 1);
			thePV = 0;
			mQ[i] = "<P>Find the present value of the following cash flow stream if the interest rate is " + theR + " per cent.</P>";
			theNumCF = Math.round(Math.random()*4 + 4);
			for (j = 0; j < theNumCF; j++) {
				if (j > 0) {
					theTemp = Math.random();
					if (theTemp < 0.70) {
						theCF[j] = Math.round(Math.random()*5)*100;
					} else {
						theCF[j] = theCF[j-1];
					}
					if ((j == theNumCF) && (theCF[j] == 0)) {
						theCF[j] = 100;
					}
					thePV += theCF[j]*Math.pow(1 + theR/100,-(j+1));
				} else {
					theCF[j] = Math.round(Math.random()*5)*100;
					thePV += theCF[j]*Math.pow(1 + theR/100,-(j+1));
				}
			}
			mQ[i] += "<TABLE BORDER='0' CELLSPACING='0' CELLPADDING='0'><TR>";
			for (j = 0; j <= theNumCF; j++) {
				mQ[i] += "<TD ALIGN='CENTER'>" + j + "</TD>";
			}
			mQ[i] += "</TR><TR>";
			mQ[i] += "<TD ALIGN='CENTER'><IMG SRC='TimeLineStart.gif'></TD>";
			for (j = 1; j < theNumCF; j++) {
				mQ[i] += "<TD ALIGN='CENTER'><IMG SRC='TimeLine.gif'></TD>";
			}
			mQ[i] += "<TD ALIGN='CENTER'><IMG SRC='TimeLineEnd.gif'></TD>";
			mQ[i] += "</TR><TR><TD ALIGN='CENTER'>&nbsp;</TD>";
			for (j = 0; j < theNumCF; j++) {
				mQ[i] += "<TD ALIGN='CENTER'>$" + theCF[j] + "</TD>";
			}
			mQ[i] += "</TR></TABLE>";
			mE[i] = "To solve using the TI BAII Plus calculator:<BR><TABLE>";
			mE[i] += "<TR><TD ALIGN='CENTER'>[ CF ]</TD><TD ALIGN='CENTER'>[ 2nd ][CLR Work]</TD></TR>";
			mE[i] += "<TR><TD ALIGN='CENTER'>0</TD><TD ALIGN='CENTER'>[ENTER][DOWN ARROW]</TD></TR>";
			for (j = 0; j < theNumCF; j++) {
				ii = 1;
				mE[i] += "<TR><TD ALIGN='CENTER'>" + theCF[j] + "</TD><TD ALIGN='CENTER'>[ENTER][DOWN ARROW]</TD></TR>";
				while ((j < theNumCF) && (theCF[j] == theCF[j+1])) {
					ii++;
					j++;	
				}
				mE[i] += "<TR><TD ALIGN='CENTER'>" + ii + "</TD><TD ALIGN='CENTER'>[ENTER][DOWN ARROW]</TD></TR>";
			}
			mE[i] += "<TR><TD ALIGN='CENTER'>[ NPV ]</TD><TD ALIGN='CENTER'>&nbsp;</TD></TR>";
			mE[i] += "<TR><TD ALIGN='CENTER'>" + theR + "</TD><TD ALIGN='CENTER'>[ENTER][DOWN ARROW]</TD></TR>";
			mE[i] += "<TR><TD ALIGN='CENTER'>[ CPT ]</TD><TD ALIGN='CENTER'>&nbsp;</TD></TR></TABLE>";
	
			thePV = Math.round(thePV*100)/100;
			theAns = thePV;
		} else if (theType == 2) {
			thePV = Math.round(Math.random()*10 + 1)*100;
			theN = Math.round(Math.random()*14 + 1);
			theR = Math.round(Math.random()*20 + 1);
			theFV = Math.round(thePV*Math.pow(1 + theR/100,theN)*100)/100;
			mQ[i] = "Find the future value at the end of " + theN + " years of $" + thePV + " invested today at an interest rate of " + theR + " per cent.";
			mE[i] = "To solve using the TI BAII Plus calculator:<BR><TABLE>";
			mE[i] += "<TR><TD ALIGN='CENTER'>" + thePV + "</TD><TD ALIGN='CENTER'>[ PV ]</TD></TR>";
			mE[i] += "<TR><TD ALIGN='CENTER'>" + theR  + "</TD><TD ALIGN='CENTER'>[ I/Y ]</TD></TR>";
			mE[i] += "<TR><TD ALIGN='CENTER'>" + theN + "</TD><TD ALIGN='CENTER'>[ N ]</TD></TR>";
			mE[i] += "<TR><TD ALIGN='CENTER'>[ CPT ]</TD><TD ALIGN='CENTER'>[ FV ]</TD></TR></TABLE>";
			theAns = theFV;
		} else if (theType == 3) {
			thePMT = Math.round(Math.random()*10 + 1)*100;
			theN = Math.round(Math.random()*14 + 1);
			theR = Math.round(Math.random()*20 + 1);
			thePV = Math.round(thePMT*((1-Math.pow(1 + theR/100,-theN))/(theR/100))*100)/100;
			mQ[i] = "Find the present value of an annuity of $" + thePMT + " per year for " + theN + " years if the interest rate is " + theR + " per cent.";
			mE[i] = "To solve using the TI BAII Plus calculator:<BR><TABLE>";
			mE[i] += "<TR><TD ALIGN='CENTER'>" + thePMT + "</TD><TD ALIGN='CENTER'>[ PMT ]</TD></TR>";
			mE[i] += "<TR><TD ALIGN='CENTER'>" + theR  + "</TD><TD ALIGN='CENTER'>[ I/Y ]</TD></TR>";
			mE[i] += "<TR><TD ALIGN='CENTER'>" + theN + "</TD><TD ALIGN='CENTER'>[ N ]</TD></TR>";
			mE[i] += "<TR><TD ALIGN='CENTER'>[ CPT ]</TD><TD ALIGN='CENTER'>[ PV ]</TD></TR></TABLE>";
			theAns = thePV;
		} else if (theType == 4) {
			thePMT = Math.round(Math.random()*10 + 1)*100;
			theN = Math.round(Math.random()*14 + 1);
			theR = Math.round(Math.random()*20 + 1);
			theFV = Math.round(thePMT*((Math.pow(1 + theR/100,theN)-1)/(theR/100))*100)/100;
			mQ[i] = "Find the future value of an annuity of $" + thePMT + " per year for " + theN + " years if the interest rate is " + theR + " per cent.";
			mE[i] = "To solve using the TI BAII Plus calculator:<BR><TABLE>";
			mE[i] += "<TR><TD ALIGN='CENTER'>" + thePMT + "</TD><TD ALIGN='CENTER'>[ PMT ]</TD></TR>";
			mE[i] += "<TR><TD ALIGN='CENTER'>" + theR  + "</TD><TD ALIGN='CENTER'>[ I/Y ]</TD></TR>";
			mE[i] += "<TR><TD ALIGN='CENTER'>" + theN + "</TD><TD ALIGN='CENTER'>[ N ]</TD></TR>";
			mE[i] += "<TR><TD ALIGN='CENTER'>[ CPT ]</TD><TD ALIGN='CENTER'>[ FV ]</TD></TR></TABLE>";
			theAns = theFV;
		}
		
		mAns[i] = theAns;
		mN[i] = 4;
		mT[i] = "MC";
		mG[i] = "";
		mGW[i] = "";
		mGH[i] = "";
		
		theA = Math.round(Math.random()*3 + 1);
		if (theA == 1) {
			mA[i] = "a";
			mQA[i] = "$" + theAns;
			mQB[i] = "$" + Math.round((theAns + 1 + Math.random()*9)*100)/100;
			mQC[i] = "$" + Math.round((theAns + 11 + Math.random()*9)*100)/100;
			mQD[i] = "$" + Math.round((theAns + 21 + Math.random()*9)*100)/100;
			mQE[i] = "";
		} else if (theA == 2) {
			mA[i] = "b";
			mQB[i] = "$" + theAns;
			mQC[i] = "$" + Math.round((theAns + 1 + Math.random()*9)*100)/100;
			mQD[i] = "$" + Math.round((theAns + 11 + Math.random()*9)*100)/100;
			mQA[i] = "$" + Math.round((theAns - 1 - Math.random()*9)*100)/100;
			mQE[i] = "";
		} else if (theA == 3) {
			mA[i] = "c";
			mQC[i] = "$" + theAns;
			mQD[i] = "$" + Math.round((theAns + 1 + Math.random()*9)*100)/100;
			mQA[i] = "$" + Math.round((theAns - 11 - Math.random()*9)*100)/100;
			mQB[i] = "$" + Math.round((theAns - 1 - Math.random()*9)*100)/100;
			mQE[i] = "";
		} else if (theA == 4) {
			mA[i] = "d";
			mQD[i] = "$" + theAns;
			mQA[i] = "$" + Math.round((theAns - 21 - Math.random()*9)*100)/100;
			mQB[i] = "$" + Math.round((theAns - 11 - Math.random()*9)*100)/100;
			mQC[i] = "$" + Math.round((theAns - 1 - Math.random()*9)*100)/100;
			mQE[i] = "";
		}
	}
}

function drawMCQuestions() {
	createQuestions();
	theOut = "<h1>Time Value of Money Quiz</h1><hr>";
	allEssay = true;
	theNumQuestions = mNumQuestions;
	theOut += "<form name='quiz'>";
	theOut += mTStart + "\n";
	for (i = 1; i <= theNumQuestions; i++) {
		if (mG[i-1] != "") {
			theOut += mBlankRow;
			theOut += "<TR>\n" + mTDBegin + "&nbsp;</TD>\n";
			theOut += mTDBegin + "&nbsp;</TD>\n";
			if ((mGH[i-1] != "") && (mGW[i-1] != "")) {
				theOut += mTDBegin + "<IMG SRC='" + mG[i-1] + "' WIDTH='" + mGW[i-1] + "' HEIGHT='" + mGH[i-1] + "'></TD></TR>\n";
			} else {
				theOut += mTDBegin + "<IMG SRC='" + mG[i-1] + "'></TD></TR>\n";
			}
		}
			theOut += mBlankRow;
		theOut += "<TR>\n" + mTDBegin + "&nbsp;</TD>\n";
		theOut += mTDBegin  +  i  + ".</TD>\n";
		theOut += mTDBegin + mQ[i-1] + "</TD>\n</TR>\n";
		if (mN[i-1] >= 1) {
			theOut += "<TR>\n" + mTDBeginA + mRadBeg + "q" + i + mRadMid + "q" + i + "a" + mRadEnd + "</TD>";
			theOut += mTDBegin + "a. " + mQA[i-1] + "</TD>\n</TR>\n";
		}
		if (mN[i-1] >= 2) {
			theOut += "<TR>\n" + mTDBeginA + mRadBeg + "q" + i + mRadMid + "q" + i + "b" + mRadEnd + "</TD>";
			theOut += mTDBegin + "b. " + mQB[i-1] + "</TD>\n</TR>\n";
		}
		if (mN[i-1] >= 3) {
			theOut += "<TR>\n" + mTDBeginA + mRadBeg + "q" + i + mRadMid + "q" + i + "c" + mRadEnd + "</TD>";
			theOut += mTDBegin + "c. " + mQC[i-1] + "</TD>\n</TR>\n";
		}
		if (mN[i-1] >= 4) {
			theOut += "<TR>\n" + mTDBeginA + mRadBeg + "q" + i + mRadMid + "q" + i + "d" + mRadEnd + "</TD>";
			theOut += mTDBegin + "d. " + mQD[i-1] + "</TD>\n</TR>\n";
		}
		if (mN[i-1] >= 5) {
			theOut += "<TR>\n" + mTDBeginA + mRadBeg + "q" + i + mRadMid + "q" + i + "e" + mRadEnd + "</TD>";
			theOut += mTDBegin + "e. " + mQE[i-1] + "</TD>\n</TR>\n";
		}
		if (mT[i-1] == "Essay") {
			theOut += "<TR>\n" + mTDBeginA + "&nbsp;</TD>";
			theOut += mTDBegin +  mTABeg + "q" + i + mTAEnd  + "</TD>\n</TR>\n";
		}
		if (mT[i-1] != "Essay") {
			allEssay = false;
		}
	}
		theOut += "</TABLE></P>\n";
	theOut += "<P><INPUT TYPE='button' Name='checkit' Value='How did I do?' onClick='checkMCAnswers(this.form)'></P>";
	theOut += "</form>";
	theOut += mCopy;
	if (isNav) {
		theOut += "<P><img src='space.gif' width='1' height='6000'></P>";
	}
	return theOut;
}

function checkMCAnswers(form) {
	theNumCorrect = 0;
	theNumToGrade = mNumQuestions;
	theOutStr = "";
	k = -1;
	for (i = 0; i < mNumQuestions; i++) {
		if (mT[i] == "Essay") {
			k++;
			mR[i] = "" + form.elements[k].value;
		} else {
			mR[i] = "";
			for (j = 0; j < mN[i]; j++) {
				k++;
				if (form.elements[k].checked) {
					if (j == 0) {
						mR[i] = "a";
					}
					if (j == 1) {
						mR[i] = "b";
					}
					if (j == 2) {
						mR[i] = "c";
					}
					if (j == 3) {
						mR[i] = "d";
					}
					if (j == 4) {
						mR[i] = "e";
					}
				}
			}
		}
		if ((mT[i] != "Essay") && (mA[i].toLowerCase() == mR[i])) {
			theNumCorrect++;
			mC[i] = 1;
		} else {
			mC[i] = 0;
		}
		if (mT[i] == "Essay") {
			theNumToGrade--;
		}
	}
	if (theNumToGrade > 0) {
		theOutStr += " \nYou correctly answered " + theNumCorrect + " of " + theNumToGrade + " questions.\n \n";
		theOutStr += "Correct answers are indicated with a green checkmark. ";
		theOutStr += "Incorrect answers are indicated with a red X.";
		alert(theOutStr);
	}
	drawExplanation();
	scroll(0,0);
}

function drawExplanation() {
	theOutStr1 = "";
	theOutStr1 += "<P><H1>Time Value of Money Quiz: Results</H1></P><HR>";
	if (isIE) {
		document.all.theQuiz.innerHTML = theOutStr1 + drawMCQuestions2();
	}
	if (isNav) {
		document.theQuiz.document.write(theOutStr1 + drawMCQuestions2());
		document.theQuiz.document.close();
	}
	if (isNav6) {
		document.getElementById("theQuiz").innerHTML = theOutStr1 + drawMCQuestions2();
	}
}

function drawMCQuestions2() {
	theOut = "";
	allEssay = true
	theNumQuestions = mNumQuestions;
	theOut += mTStart + "\n";
	for (i = 1; i <= theNumQuestions; i++) {
		if (mG[i-1] != "") {
			theOut += mBlankRow;
			theOut += "<TR>\n" + mTDBegin + "&nbsp;</TD>\n";
			theOut += mTDBegin + "&nbsp;</TD>\n";
			if ((mGH[i-1] != "") && (mGW[i-1] != "")) {
				theOut += mTDBegin + "<IMG SRC='" + mG[i-1] + "' WIDTH='" + mGW[i-1] + "' HEIGHT='" + mGH[i-1] + "'></TD></TR>\n";
			} else {
				theOut += mTDBegin + "<IMG SRC='" + mG[i-1] + "'></TD></TR>\n";
			}
		}
		if (mT[i-1] != "Essay") {
			theOut += mBlankRow;
			if (mC[i-1] >= 1) {
				theOut += "<TR>\n" + mTDBeginR + mGreenCheckIMG + "</TD>\n";
			} else {
				theOut += "<TR>\n" + mTDBeginR + mRedCheckIMG + "</TD>\n";
			}
		} else {
			theOut += mBlankRow;
			theOut += "<TR>\n" + mTDBeginR + mBlankCheckIMG + "</TD>\n";
		}
		theOut += mTDBeginR  +  i  + ".</TD>\n";
		theOut += mTDBegin + mQ[i-1] + "</TD>\n</TR>\n";
		if (mN[i-1] >= 1) {
			theOut += "<TR>\n" + mTDBeginA  + "&nbsp;</TD>";
			theOut += mTDBegin + "a. " + mQA[i-1] + "</TD>\n</TR>\n";
		}
		if (mN[i-1] >= 2) {
			theOut += "<TR>\n" + mTDBeginA  + "&nbsp;</TD>";
			theOut += mTDBegin + "b. " + mQB[i-1] + "</TD>\n</TR>\n";
		}
		if (mN[i-1] >= 3) {
			theOut += "<TR>\n" + mTDBeginA  + "&nbsp;</TD>";
			theOut += mTDBegin + "c. " + mQC[i-1] + "</TD>\n</TR>\n";
		}
		if (mN[i-1] >= 4) {
			theOut += "<TR>\n" + mTDBeginA  + "&nbsp;</TD>";
			theOut += mTDBegin + "d. " + mQD[i-1] + "</TD>\n</TR>\n";
		}
		if (mN[i-1] >= 5) {
			theOut += "<TR>\n" + mTDBeginA  + "&nbsp;</TD>";
			theOut += mTDBegin + "e. " + mQE[i-1] + "</TD>\n</TR>\n";
		}
		theOut += "<TR>\n" + mTDBeginB + "<FONT COLOR='red'>Your Answer:</FONT></TD>";
		theOut += mTDBeginC  + "<FONT COLOR='red'>" + mR[i-1] + "</FONT>&nbsp;</TD>\n</TR>\n";
		if (mT[i-1] != "Essay") {
			theOut += "<TR>\n" + mTDBeginB + "<FONT COLOR='green'>Correct Answer:</FONT></TD>";
			theOut += mTDBeginC  + "<FONT COLOR='green'>" + mA[i-1].toLowerCase() + "</FONT>&nbsp;</TD>\n</TR>\n";
		}
		if (mE[i-1] != "") {
			if (mT[i-1] != "Essay") {
				theOut += "<TR>\n" + mTDBeginB + "<FONT COLOR='green'>Explanation:</FONT></TD>";
				theOut += mTDBeginC  + mE[i-1] + "&nbsp;</TD>\n</TR>\n";
			} else {
				theOut += "<TR>\n" + mTDBeginB + "<FONT COLOR='green'>Our Answer:</FONT></TD>";
				theOut += mTDBeginC  + mE[i-1] + "&nbsp;</TD>\n</TR>\n";
			}
		}
	}
	theOut += "</TABLE></P>\n";
	theOut += mCopy;
	return theOut;
}
