// JavaScript code Copyright Rock Mathis 1998. All Rights Reserved.
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()*5); 
		if (theType <= 2) {
			mQue[i] = new randomPProblem();
		} else if (theType == 3) {
			mQue[i] = new randomGProblem();
		} else if (theType == 4) {
			mQue[i] = new randomRProblem();
		} else if (theType == 5) {
			mQue[i] = new randomDProblem();
		} 
	}
}

function drawMCQuestions() {
	createQuestions();
	theOut = "<h1>Stock 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='5000'></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>Stock 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 randomPProblem(form) {
	var theQue = new Question();
 	var theD = Math.round((Math.random()*5 + 1)*100)/100;
 	var theG = Math.round((Math.random()*9 + 1)*10)/10;
 	var theR = Math.round((Math.random()*9 + 2 + theG)*10)/10;
 	var theDivNum = Math.round(Math.random());
 	var theDOut = "" + Math.round(theD*100)/100;
 	var theGOut = "" + Math.round(theG*10)/10;
 	var theGOut1 = "" + Math.round(theG*100)/10000;
 	var theROut = "" + Math.round(theR*10)/10;
 	var theROut1 = "" + Math.round(theR*100)/10000;
 	var theP = calcP(theDivNum, theD, theG, theR);
 	var thePOut = Math.round(theP*100)/100;
 	theQue.mQ = "Find the price for a stock given that ";
 	if (theDivNum == 0) {
		theQue.mQ += "the current dividend is $" + theDOut + " per share, ";
	} else {
		theQue.mQ += "the next dividend is $" + theDOut + " per share, ";
	}
 	theQue.mQ += "the required return is " + theROut + "\%, ";
 	theQue.mQ += "and the growth rate in dividends is " + theGOut + "\% per year.";
 	theQue.mE = "<P>The <b>Stock Price</b> is calculated as follows:</P>";
 	if (theDivNum == 0) {
		theQue.mE += drawFraction("P<SUB>0</SUB>","D<SUB>0</SUB>(1 + g)","r - g",theDOut + "(1 + " + theGOut1 + ")",theROut1 + " - " + theGOut1,"$" + thePOut);
 	} else {
 		theQue.mE += drawFraction("P<SUB>0</SUB>","D<SUB>1</SUB>","r - g",theDOut,theROut1 + " - " + theGOut1,"$" + thePOut);
 	}
 	theQue.mAns = thePOut;
	theQue.mN = 4;
	theQue.mT = "MC";
	theQue = buildAnswers(theQue, theP, 2, "$", "");
 	return theQue;
 }
 
 function randomGProblem(form) {
	var theQue = new Question();
 	var theD = Math.round((Math.random()*5 + 1)*100)/100;
 	var theG = Math.round((Math.random()*9 + 1)*100)/100;
 	var theR = Math.round((Math.random()*9 + 2 + theG)*10)/10;
 	var theDivNum = Math.round(Math.random());
 	var theDOut = "" + Math.round(theD*100)/100;
 	var theGOut = "" + Math.round(theG*100)/100;
 	var theGOut1 = "" + Math.round(theG*100)/10000;
 	var theROut = "" + Math.round(theR*10)/10;
 	var theROut1 = "" + Math.round(theR*100)/10000;
 	var theP = calcP(theDivNum, theD, theG, theR);
 	var thePOut = Math.round(theP*100)/100;
 	theQue.mQ = "Find the dividend growth rate for a stock given that ";
 	if (theDivNum == 0) {
		theQue.mQ += "the current dividend is $" + theDOut + " per share, ";
	} else {
		theQue.mQ += "the next dividend is $" + theDOut + " per share, ";
	}
 	theQue.mQ += "the required return is " + theROut + "\%, ";
 	theQue.mQ += "and the stock price is $" + thePOut + " per share.";
 	theQue.mE = "<P>The <b>Dividend Growth Rate</b> is calculated as follows:</P>";
 	if (theDivNum == 0) {
		theQue.mE += drawFraction("g","P<SUB>0</SUB>r - D<SUB>0</SUB>","D<SUB>0</SUB> + P<SUB>0</SUB>",thePOut + "(" + theROut1 + ") - " + theDOut,theDOut + " + " + thePOut,theGOut1 + " = " + theGOut + "\%");
 	} else {
 		theQue.mE += "<TABLE><TR valign='center'><TD>g = r - </TD><TD>" + drawSimpleFraction("D<SUB>1</SUB>","P<SUB>0</SUB>") + "</TD><TD> = " + theROut1 + " - </TD><TD>" + drawSimpleFraction(theDOut,thePOut) + "</TD><TD> = " + theGOut1 + " = " + theGOut + "\%</TD></TR></TABLE>";
 	}
 	theQue.mAns = theGOut;
	theQue.mN = 4;
	theQue.mT = "MC";
	theQue = buildAnswers(theQue, theG, .3, "", "\%");
 	return theQue;
 }

 function randomRProblem(form) {
	var theQue = new Question();
 	var theD = Math.round((Math.random()*5 + 1)*100)/100;
 	var theG = Math.round((Math.random()*9 + 1)*100)/100;
 	var theR = Math.round((Math.random()*9 + 2 + theG)*10)/10;
 	var theDivNum = Math.round(Math.random());
 	var theDOut = "" + Math.round(theD*100)/100;
 	var theGOut = "" + Math.round(theG*100)/100;
 	var theGOut1 = "" + Math.round(theG*100)/10000;
 	var theROut = "" + Math.round(theR*10)/10;
 	var theROut1 = "" + Math.round(theR*100)/10000;
 	var theP = calcP(theDivNum, theD, theG, theR);
 	var thePOut = Math.round(theP*100)/100;
 	theQue.mQ = "Find the required return on a stock given that ";
 	if (theDivNum == 0) {
		theQue.mQ += "the current dividend is $" + theDOut + " per share, ";
	} else {
		theQue.mQ += "the next dividend is $" + theDOut + " per share, ";
	}
 	theQue.mQ += "the growth rate in dividends is " + theGOut + "\%, ";
 	theQue.mQ += "and the stock price is $" + thePOut + " per share.";
 	theQue.mE = "<P>The <b>Required Return</b> is calculated as follows:</P>";
 	if (theDivNum == 0) {
		theQue.mE += "<TABLE><TR valign='center'><TD>r = </TD><TD>" + drawSimpleFraction("D<SUB>0</SUB>(1 + g)","P<SUB>0</SUB>") + "</TD><TD> + g = </TD><TD>" + drawSimpleFraction(theDOut + "(1 + " + theGOut1 + ")",thePOut) + "</TD><TD> + " + theGOut1 + " = " + theROut1 + " = " + theROut + "\%</TD></TR></TABLE>";
 	} else {
 		theQue.mE += "<TABLE><TR valign='center'><TD>r = </TD><TD>" + drawSimpleFraction("D<SUB>1</SUB>","P<SUB>0</SUB>") + "</TD><TD> + g = </TD><TD>" + drawSimpleFraction(theDOut,thePOut) + "</TD><TD> + " + theGOut1 + " = " + theROut1 + " = " + theROut + "\%</TD></TR></TABLE>";
 	}
 	theQue.mAns = theROut;
	theQue.mN = 4;
	theQue.mT = "MC";
	theQue = buildAnswers(theQue, theR, .3, "", "\%");
 	return theQue;
 }

function randomDProblem(form) {
	var theQue = new Question();
 	var theD = Math.round((Math.random()*5 + 1)*100)/100;
 	var theG = Math.round((Math.random()*9 + 1)*10)/10;
 	var theR = Math.round((Math.random()*9 + 2 + theG)*10)/10;
 	var theDivNum = Math.round(Math.random());
 	var theDOut = "" + Math.round(theD*100)/100;
 	var theGOut = "" + Math.round(theG*10)/10;
 	var theGOut1 = "" + Math.round(theG*100)/10000;
 	var theROut = "" + Math.round(theR*10)/10;
 	var theROut1 = "" + Math.round(theR*100)/10000;
 	var theP = calcP(theDivNum, theD, theG, theR);
 	var thePOut = Math.round(theP*100)/100;
 	theQue.mQ = "";
 	if (theDivNum == 0) {
		theQue.mQ += "Find the current dividend for a stock given that the stock price is $" + thePOut + " ";
	} else {
		theQue.mQ += "Find the next dividend for a stock given that the stock price is $" + thePOut + " ";
	}
	theQue.mQ += "the growth rate in dividends is " + theGOut + "\% per year, and ";
 	theQue.mQ += "the required return is " + theROut + "\%.";
 	if (theDivNum == 0) {
 		theQue.mE = "<P>The <b>Current Dividend</b> is calculated as follows:</P>";
		theQue.mE += drawFraction("D<SUB>0</SUB>","P<SUB>0</SUB>(r - g)","1 + g",thePOut + "(" + theROut1 + " - " + theGOut1 + ")","1 + " + theGOut1,"$" + theDOut);
 	} else {
 		theQue.mE = "<P>The <b>Next Dividend</b> is calculated as follows:</P>";
 		theQue.mE += "<P>D<SUB>1</SUB> = P<SUB>0</SUB>(r - g) = " + thePOut + "(" + theROut1 + " - " + theGOut1 + ") = $" + theDOut + "</P>";
 	}
 	theQue.mAns = theDOut;
	theQue.mN = 4;
	theQue.mT = "MC";
	theQue = buildAnswers(theQue, theD, .3, "$", "");
 	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 drawFraction(inLHS,inNumFormula,inDenFormula,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'>" + inNumFormula + "</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 += "<td valign='center' align='center'><hr noshade></td>";
	theOut += "</tr>";
	theOut += "<tr>";
	theOut += "<td valign='center' align='center'>" + inDenFormula + "</td>";
	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;
}

function calcP(inDNum, inD, inG, inR) {
	var outP = "";
	inG = inG/100;
	inR = inR/100;
	if (inG >= inR) {
		alert("Cannot compute the stock price (r must be greater than g).");
		return outP;
	}	
 	if (inDNum == 0) {
 		outP = inD*(1 + inG)/(inR - inG);
 	} else {
 		outP = inD/(inR - inG);
 	}
 	return outP;
}

function calcD(inDNum, inG, inR, inP) {
	var outD = "";
	inG = inG/100;
	inR = inR/100;
	if (inG >= inR) {
		alert("Cannot compute the dividend (r must be greater than g).");
		return outD;
	}	
	if (inG <= -1) {
		alert("Cannot compute the dividend (g must be greater than -100\%).");
		return outD;
	}	
 	if (inDNum == 0) {
 		outD = (inP*(inR - inG))/(1 + inG);
 	} else {
 		outD = (inP*(inR - inG));
 	}
 	return outD;
}

function calcG(inDNum, inD, inR, inP) {
	var outG = "";
	inR = inR/100;
	if (inP == 0) {
		alert("Cannot compute the growth rate (Po must be positive).");
		return outG;
	}	
 	if (inDNum == 0) {
 		if ((inD/inP) == -1) {
 			alert("Cannot compute the growth rate.");
 			return outG;
 		}	
 		outG = (inR - (inD/inP))/((inD/inP)+1);
 	} else {
 		outG = inR - (inD/inP);
 	}
	return outG*100;
}

function calcR(inDNum, inD, inG, inP) {
	var outR = "";
	inG = inG/100;
	if (inP <= 0) {
		alert("Cannot compute the required return (Po must be positive).");
		return outR;
	}	
 	if (inDNum == 0) {
 		outR = (inD*(1 + inG))/(inP) + inG;
 	} else {
 		outR = (inD)/(inP) + inG;
 	}
	return outR*100;	
}
