mNumQuestions = 10;
mQue = 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 Question() {
	this.mQ = "";
	this.mQA = "";
	this.mQB = "";
	this.mQC = "";
	this.mQD = "";
	this.mQE = "";
	this.mA = "";
	this.mR = "";
	this.mC = "";
	this.mT = "";
	this.mE = "";
	this.mG = "";
	this.mGW = "";
	this.mGH = "";
	this.mN = "";
	this.mAns = "";
}

function drawLayer() {
	var theLayerStr = "";
	if (isNav) {
		theLayerStr = "<layer name='theQuiz' left=150 top=110><form>"
		theLayerStr += drawMCQuestions();
		theLayerStr += "</form></layer>";
	} else if (isIE) {
		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); 
		if (theType == 0) {
			mQue[i] = new randomERIProblem();
		} else if (theType == 1) {
			mQue[i] = new randomERMProblem();
		} else if (theType == 2) {
			mQue[i] = new randomRFProblem();
		} else if (theType == 3) {
			mQue[i] = new randomBetaProblem();
		} else if (theType == 4) {
			mQue[i] = new randomERIProblem();
		} 	
	}
}

function drawMCQuestions() {
	createQuestions();
	theOut = "<h1>CAPM Quiz</h1><hr>";
	allEssay = true;
	theNumQuestions = mNumQuestions;
	theOut += "<form name='quiz'>";
	theOut += mTStart + "\n";
	for (i = 1; i <= theNumQuestions; i++) {
		if (mQue[i-1].mG != "") {
			theOut += mBlankRow;
			theOut += "<TR>\n" + mTDBegin + "&nbsp;</TD>\n";
			theOut += mTDBegin + "&nbsp;</TD>\n";
			if ((mQue[i-1].mGH != "") && (mQue[i-1].mGW != "")) {
				theOut += mTDBegin + "<IMG SRC='" + mQue[i-1].mG + "' WIDTH='" + mQue[i-1].mGW + "' HEIGHT='" + mQue[i-1].mGH + "'></TD></TR>\n";
			} else {
				theOut += mTDBegin + "<IMG SRC='" + mQue[i-1].mG + "'></TD></TR>\n";
			}
		}
			theOut += mBlankRow;
		theOut += "<TR>\n" + mTDBegin + "&nbsp;</TD>\n";
		theOut += mTDBegin  +  i  + ".</TD>\n";
		theOut += mTDBegin + mQue[i-1].mQ + "</TD>\n</TR>\n";
		if (mQue[i-1].mN >= 1) {
			theOut += "<TR>\n" + mTDBeginA + mRadBeg + "q" + i + mRadMid + "q" + i + "a" + mRadEnd + "</TD>";
			theOut += mTDBegin + "a. " + mQue[i-1].mQA + "</TD>\n</TR>\n";
		}
		if (mQue[i-1].mN >= 2) {
			theOut += "<TR>\n" + mTDBeginA + mRadBeg + "q" + i + mRadMid + "q" + i + "b" + mRadEnd + "</TD>";
			theOut += mTDBegin + "b. " + mQue[i-1].mQB + "</TD>\n</TR>\n";
		}
		if (mQue[i-1].mN >= 3) {
			theOut += "<TR>\n" + mTDBeginA + mRadBeg + "q" + i + mRadMid + "q" + i + "c" + mRadEnd + "</TD>";
			theOut += mTDBegin + "c. " + mQue[i-1].mQC + "</TD>\n</TR>\n";
		}
		if (mQue[i-1].mN >= 4) {
			theOut += "<TR>\n" + mTDBeginA + mRadBeg + "q" + i + mRadMid + "q" + i + "d" + mRadEnd + "</TD>";
			theOut += mTDBegin + "d. " + mQue[i-1].mQD + "</TD>\n</TR>\n";
		}
		if (mQue[i-1].mN >= 5) {
			theOut += "<TR>\n" + mTDBeginA + mRadBeg + "q" + i + mRadMid + "q" + i + "e" + mRadEnd + "</TD>";
			theOut += mTDBegin + "e. " + mQue[i-1].mQE + "</TD>\n</TR>\n";
		}
		if (mQue[i-1].mT == "Essay") {
			theOut += "<TR>\n" + mTDBeginA + "&nbsp;</TD>";
			theOut += mTDBegin +  mTABeg + "q" + i + mTAEnd  + "</TD>\n</TR>\n";
		}
		if (mQue[i-1].mT != "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='4000'></P>";
	}
	return theOut;
}

function checkMCAnswers(form) {
	theNumCorrect = 0;
	theNumToGrade = mNumQuestions;
	theOutStr = "";
	k = -1;
	for (i = 0; i < mNumQuestions; i++) {
		if (mQue[i].mT == "Essay") {
			k++;
			mQue[i].mR = "" + form.elements[k].value;
		} else {
			mQue[i].mR = "";
			for (j = 0; j < mQue[i].mN; j++) {
				k++;
				if (form.elements[k].checked) {
					if (j == 0) {
						mQue[i].mR = "a";
					}
					if (j == 1) {
						mQue[i].mR = "b";
					}
					if (j == 2) {
						mQue[i].mR = "c";
					}
					if (j == 3) {
						mQue[i].mR = "d";
					}
					if (j == 4) {
						mQue[i].mR = "e";
					}
				}
			}
		}
		if ((mQue[i].mT != "Essay") && (mQue[i].mA.toLowerCase() == mQue[i].mR)) {
			theNumCorrect++;
			mQue[i].mC = 1;
		} else {
			mQue[i].mC = 0;
		}
		if (mQue[i].mT == "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>CAPM 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 (mQue[i-1].mG != "") {
			theOut += mBlankRow;
			theOut += "<TR>\n" + mTDBegin + "&nbsp;</TD>\n";
			theOut += mTDBegin + "&nbsp;</TD>\n";
			if ((mQue[i-1].mGH != "") && (mQue[i-1].mGW != "")) {
				theOut += mTDBegin + "<IMG SRC='" + mQue[i-1].mG + "' WIDTH='" + mQue[i-1].mGW + "' HEIGHT='" + mQue[i-1].mGH + "'></TD></TR>\n";
			} else {
				theOut += mTDBegin + "<IMG SRC='" + mQue[i-1].mG + "'></TD></TR>\n";
			}
		}
		if (mQue[i-1].mT != "Essay") {
			theOut += mBlankRow;
			if (mQue[i-1].mC >= 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 + mQue[i-1].mQ + "</TD>\n</TR>\n";
		if (mQue[i-1].mN >= 1) {
			theOut += "<TR>\n" + mTDBeginA  + "&nbsp;</TD>";
			theOut += mTDBegin + "a. " + mQue[i-1].mQA + "</TD>\n</TR>\n";
		}
		if (mQue[i-1].mN>= 2) {
			theOut += "<TR>\n" + mTDBeginA  + "&nbsp;</TD>";
			theOut += mTDBegin + "b. " + mQue[i-1].mQB + "</TD>\n</TR>\n";
		}
		if (mQue[i-1].mN >= 3) {
			theOut += "<TR>\n" + mTDBeginA  + "&nbsp;</TD>";
			theOut += mTDBegin + "c. " + mQue[i-1].mQC + "</TD>\n</TR>\n";
		}
		if (mQue[i-1].mN >= 4) {
			theOut += "<TR>\n" + mTDBeginA  + "&nbsp;</TD>";
			theOut += mTDBegin + "d. " + mQue[i-1].mQD + "</TD>\n</TR>\n";
		}
		if (mQue[i-1].mN >= 5) {
			theOut += "<TR>\n" + mTDBeginA  + "&nbsp;</TD>";
			theOut += mTDBegin + "e. " + mQue[i-1].mQE + "</TD>\n</TR>\n";
		}
		theOut += "<TR>\n" + mTDBeginB + "<FONT COLOR='red'>Your Answer:</FONT></TD>";
		theOut += mTDBeginC  + "<FONT COLOR='red'>" + mQue[i-1].mR + "</FONT>&nbsp;</TD>\n</TR>\n";
		if (mQue[i-1].mT != "Essay") {
			theOut += "<TR>\n" + mTDBeginB + "<FONT COLOR='green'>Correct Answer:</FONT></TD>";
			theOut += mTDBeginC  + "<FONT COLOR='green'>" + mQue[i-1].mA.toLowerCase() + "</FONT>&nbsp;</TD>\n</TR>\n";
		}
		if (mQue[i-1].mE != "") {
			if (mQue[i-1].mT != "Essay") {
				theOut += "<TR>\n" + mTDBeginB + "<FONT COLOR='green'>Explanation:</FONT></TD>";
				theOut += mTDBeginC  + mQue[i-1].mE + "&nbsp;</TD>\n</TR>\n";
			} else {
				theOut += "<TR>\n" + mTDBeginB + "<FONT COLOR='green'>Our Answer:</FONT></TD>";
				theOut += mTDBeginC  + mQue[i-1].mE + "&nbsp;</TD>\n</TR>\n";
			}
		}
	}
	theOut += "</TABLE></P>\n";
	theOut += mCopy;
	return theOut;
}

function randomERIProblem() {
	var theQue = new Question();
  	var theRf = (Math.round(Math.random()*100 + 10))/10; 
  	var theERm = (Math.round(Math.random()*100 + theRf*10 + 10))/10; 
  	var theBeta = (Math.round(Math.random()*30 + 1))/10;
  	var theERi = theRf + (theERm - theRf)*theBeta; 
  	var theRfOut = "" + Math.round(theRf*100)/100;
 	var theERmOut = "" + Math.round(theERm*100)/100;
 	var theBetaOut = "" + Math.round(theBeta*100)/100;
 	var theERiOut = "" + Math.round(theERi*100)/100;
  	theQue.mQ = "Find the Expected Return on Stock i given that the ";
    theQue.mQ += "Expected Return on the Market Portfolio is ";
    theQue.mQ += "" + theERmOut + "\%, the Risk-Free Rate is ";
    theQue.mQ += "" + theRfOut + "\%, and the Beta for Stock i is " + theBetaOut + ".";
    theQue.mE = "<P>The <b>Expected Return on Stock i</b> is calculated as follows:</P>";
	theQue.mE += "<P>E[R<sub>i</sub>] = " + theRfOut + "\% + " + " (" + theERmOut + "\% - " + theRfOut + "\%)" + theBetaOut + " = " + theERiOut + "\%</P>";
    theQue.mAns = theERiOut;
    theQue.mN = 4;
	theQue.mT = "MC";
	theQue = buildAnswers(theQue, theERi, .4, "", "\%");
 	return theQue;
 }
 
 function randomERMProblem() {
	var theQue = new Question();
  	var theRf = (Math.round(Math.random()*100 + 10))/10; 
  	var theERm = (Math.round(Math.random()*100 + theRf*10 + 10))/10; 
  	var theBeta = (Math.round(Math.random()*30 + 1))/10;
  	var theERi = theRf + (theERm - theRf)*theBeta; 
  	var theRfOut = "" + Math.round(theRf*100)/100;
 	var theERmOut = "" + Math.round(theERm*100)/100;
 	var theBetaOut = "" + Math.round(theBeta*100)/100;
 	var theERiOut = "" + Math.round(theERi*100)/100;
  	theQue.mQ = "Find the Expected Return on the Market Portfolio given that the ";
    theQue.mQ += "Expected Return on Stock i is ";
    theQue.mQ += "" + theERiOut + "\%, the Risk-Free Rate is ";
    theQue.mQ += "" + theRfOut + "\%, and the Beta for Stock i is " + theBetaOut + ".";
    theQue.mE = "<P>The <b>Expected Return on the Market Portfolio</b> is calculated as follows:</P>";
	theQue.mE += "<P>" + theERiOut + "\% = " + theRfOut + "\% + " + " (E[R<sub>m</sub>] - " + theRfOut + "\%)" + theBetaOut + "</P>";
    theQue.mE += "<table><tr><td>Thus, E[R<sub>m</sub>] = </td><td>" + drawSimpleFraction(""+theERiOut+"\% - "+theRfOut+"\%",theBetaOut) + "</td><td> + " + theRfOut + "\% = " + theERmOut + "\%</td></tr></table>";
    theQue.mAns = theERmOut;
    theQue.mN = 4;
	theQue.mT = "MC";
	theQue = buildAnswers(theQue, theERm, .4, "", "\%");
 	return theQue;
 }
 
 function randomRFProblem() {
	var theQue = new Question();
  	var theRf = (Math.round(Math.random()*100 + 10))/10; 
  	var theERm = (Math.round(Math.random()*100 + theRf*10 + 10))/10; 
  	var theBeta = (Math.round(Math.random()*30 + 1))/10;
  	if (theBeta == 1) theBeta = 1.2;
  	var theERi = theRf + (theERm - theRf)*theBeta; 
  	var theRfOut = "" + Math.round(theRf*100)/100;
 	var theERmOut = "" + Math.round(theERm*100)/100;
 	var theBetaOut = "" + Math.round(theBeta*100)/100;
 	var theERiOut = "" + Math.round(theERi*100)/100;
  	theQue.mQ = "Find the Risk-Free Rate given that the ";
    theQue.mQ += "Expected Return on Stock i is ";
    theQue.mQ += "" + theERiOut + "\%, the Expected Return on the Market Portfoliois ";
    theQue.mQ += "" + theERmOut + "\%, and the Beta for Stock i is " + theBetaOut + ".";
    theQue.mE = "<P>The <b>Risk-Free Rate</b> is calculated as follows:</P>";
	theQue.mE += "<P>" + theERiOut + "\% = R<sub>f</sub> + " + " (" + theERmOut + "\% - R<sub>f</sub>)" + theBetaOut + "</P>";
    theQue.mE += drawFractionNoF("Thus, R<sub>f</sub>",""+theERiOut+"\% - "+theERmOut+"\%("+theBetaOut+")","1 - "+theBetaOut,""+theRfOut+"\%");
    theQue.mAns = theRfOut;
    theQue.mN = 4;
	theQue.mT = "MC";
	theQue = buildAnswers(theQue, theRf, .4, "", "\%");
 	return theQue;
 }
 
 function randomBetaProblem() {
	var theQue = new Question();
  	var theRf = (Math.round(Math.random()*100 + 10))/10; 
  	var theERm = (Math.round(Math.random()*100 + theRf*10 + 10))/10; 
  	var theBeta = (Math.round(Math.random()*30 + 1))/10;
  	var theERi = theRf + (theERm - theRf)*theBeta; 
  	var theRfOut = "" + Math.round(theRf*100)/100;
 	var theERmOut = "" + Math.round(theERm*100)/100;
 	var theBetaOut = "" + Math.round(theBeta*100)/100;
 	var theERiOut = "" + Math.round(theERi*100)/100;
  	theQue.mQ = "Find the Beta for Stock i given that the ";
    theQue.mQ += "Expected Return on Stock i is ";
    theQue.mQ += "" + theERiOut + "\%, the Expected Return on the Market Portfoliois ";
    theQue.mQ += "" + theERmOut + "\%, and the Risk-Free Rate is " + theRfOut + ".";
    theQue.mE = "<P>The <b>Beta for Stock i</b> is calculated as follows:</P>";
	theQue.mE += "<P>" + theERiOut + "\% = " + theRfOut + "\% + (" + theERmOut + "\% - " + theRfOut + "\%)<FONT FACE='symbol'>b</FONT><sub>i</sub></P>";
    theQue.mE += drawFractionNoF("Thus, <FONT FACE='symbol'>b</FONT><sub>i</sub>",""+theERiOut+"\% - "+theRfOut+"\%",""+theERmOut+"\% - "+theRfOut+"\%",""+theBetaOut+"");
    theQue.mAns = theBetaOut;
    theQue.mN = 4;
	theQue.mT = "MC";
	theQue = buildAnswers(theQue, theBeta, .4, "", "");
 	return theQue;
 }
 
 function buildAnswers(inQue, inAns, inSpr, inPreSym, inPostSym) {
 	var theA = Math.round(Math.random()*3 + 1);
 	var theAns = Math.round(inAns*100)/100;
 	var theLast = theAns;
	if (theA == 1) {
		inQue.mA = "a";
		inQue.mQA = "" + inPreSym + theAns + inPostSym;
		theLast = Math.round((theLast + inSpr + Math.random()*(inSpr*2))*100)/100;
		inQue.mQB = "" + inPreSym + theLast + inPostSym;
		theLast = Math.round((theLast + inSpr + Math.random()*(inSpr*2))*100)/100;
		inQue.mQC = "" + inPreSym + theLast + inPostSym;
		theLast = Math.round((theLast + inSpr + Math.random()*(inSpr*2))*100)/100;
		inQue.mQD = "" + inPreSym + theLast + inPostSym;
		inQue.mQE = "";
	} else if (theA == 2) {
		inQue.mA = "b";
		inQue.mQB = "" + inPreSym + theAns + inPostSym;
		theLast = Math.round((theLast + inSpr + Math.random()*(inSpr*2))*100)/100;
		inQue.mQC = "" + inPreSym + theLast + inPostSym;
		theLast = Math.round((theLast + inSpr + Math.random()*(inSpr*2))*100)/100;
		inQue.mQD = "" + inPreSym + theLast + inPostSym;
		theLast = theAns;
		theLast = Math.round((theLast - inSpr - Math.random()*(inSpr*2))*100)/100;
		inQue.mQA = "" + inPreSym + theLast + inPostSym;
		inQue.mQE = "";
	} else if (theA == 3) {
		inQue.mA = "c";
		inQue.mQC = "" + inPreSym + theAns + inPostSym;
		theLast = Math.round((theLast + inSpr + Math.random()*(inSpr*2))*100)/100;
		inQue.mQD = "" + inPreSym + theLast + inPostSym;
		theLast = theAns;
		theLast = Math.round((theLast - inSpr - Math.random()*(inSpr*2))*100)/100;
		inQue.mQB = "" + inPreSym + theLast + inPostSym;
		theLast = Math.round((theLast - inSpr - Math.random()*(inSpr*2))*100)/100;
		inQue.mQA = "" + inPreSym + theLast + inPostSym;
		inQue.mQE = "";
	} else if (theA == 4) {
		inQue.mA = "d";
		inQue.mQD = "" + inPreSym + theAns + inPostSym;
		theLast = Math.round((theLast - inSpr - Math.random()*(inSpr*2))*100)/100;
		inQue.mQC = "" + inPreSym + theLast + inPostSym;
		theLast = Math.round((theLast - inSpr - Math.random()*(inSpr*2))*100)/100;
		inQue.mQB = "" + inPreSym + theLast + inPostSym;
		theLast = Math.round((theLast - inSpr - Math.random()*(inSpr*2))*100)/100;
		inQue.mQA = "" + inPreSym + theLast + inPostSym;
		inQue.mQE = "";
	}
	return inQue; 
 }

 function drawFractionNoF(inLHS,inNum,inDen,inAnswer) {
	var theOut = "<table cellspacing='1' cellpadding='1'><tr>";
	theOut += "<td rowspan='3' valign='center'>" + inLHS + "</td>";
	theOut += "<td rowspan='3' valign='center'>=</td>";
	theOut += "<td valign='center' align='center'>" + inNum + "</td>";
	theOut += "<td rowspan='3' valign='center'>=</td>";
	theOut += "<td rowspan='3' valign='center'>" + inAnswer + "</td>";
	theOut += "</tr>";
	theOut += "<tr>";
	theOut += "<td valign='center' align='center'><hr noshade></td>";
	theOut += "</tr>";
	theOut += "<tr>";
	theOut += "<td valign='center' align='center'>" + inDen + "</td>";
	theOut += "</tr></table>";
	return theOut;
}

function drawSimpleFraction(inNum,inDen) {
	var theOut = "<table cellspacing='1' cellpadding='1'>";
	theOut += "<tr><td valign='center' align='center'>" + inNum + "</td></tr>";
	theOut += "<tr><td valign='center' align='center'><hr noshade></td></tr>";
	theOut += "<tr><td valign='center' align='center'>" + inDen + "</td></tr>";
	theOut += "</table>";
	return theOut;
}
