// Copyright (c) 1999 by R. E. Mathis, III// All rights reserved.// Unauthorized use or duplication is prohibited.var gG = new Array(9);var gD = 0;var gR = 0;var gNumG = 0;function newProblem(form) {    randomPProblem(form)}function doProblem() {	scriptP(gD,gG,gR);}function randomPProblem(form) {	gD = Math.round((Math.random()*5 + 1)*100)/100;	gNumG = Math.round(Math.random()*2 + 3);	for (i=1; i < gNumG; i++) {		gG[i] = Math.round(Math.random()*19 + 1);	}	gG[gNumG] = Math.round(Math.random()*9 + 1);	gR = Math.round((Math.random()*9 + 2 + gG[gNumG])*10)/10;	var theOut = "Find the price for a stock given that ";	theOut += "the required return is " + gR + "\%, ";	theOut += "the current dividend is $" + gD + " per share, ";	for (i=1; i < gNumG; i++) {		theOut += "dividends are expected to grow at a rate of " + gG[i] + "\% during year " + i + ", ";	}	theOut += "and dividends are expected to grow at a rate of " + gG[gNumG] + "\% per year from that point on.";	form.RandomProb.value = theOut;}function scriptP(inD,inG,inR) {	document.SGStockCalc.DInput.value = inD;	for (i=1;i<=gNumG;i++) {		eval("document.SGStockCalc.GInput" + i + ".value = " + inG[i]);	}	for (i = gNumG + 1; i < 9; i++) {		eval("document.SGStockCalc.GInput" + i + ".value = ''");	}	document.SGStockCalc.RInput.value = inR;	document.SGStockCalc.POutput.value = "";	findP(document.SGStockCalc);	}function findP(form) {	var theG = new Array(9);	var theD = new Array(9);	var lastG = 8;	for (i=1; i<=8; i++) {	 	theG[i] = eval("form.GInput" + i + ".value");	 	if (!isNumber(theG[1])) {	 		return false;	 	}	 	if (theG[i] == "") {	 		lastG = i-1;	 		break;	 	}		 	theG[i] = parseFloat(theG[i]); 	} 	var theR = parseFloat(form.RInput.value); 	if (!isPositiveNumber(theR)) { 		return false; 	} 	if (theR == "") theR = 0; 	theD[0] = parseFloat(form.DInput.value); 	if (!isPositiveNumber(theD[0])) { 		return false; 	} 	if (theD[0] == "") theD[0] = 0; 	if (theG[lastG] >= theR) { 		alert("The constant growth rate must be less than the required return. " + theG[lastG] + " " + theR); 		form.POutput.value = ""; 		return true; 	} 	var theP = 0; 	for (i=1; i<lastG; i++) { 		theD[i] = theD[i-1]*(1+theG[i]/100); 		theP += theD[i]/Math.pow(1 + theR/100,i); 	} 	theD[lastG] = theD[lastG-1]*(1+theG[lastG]/100); 	theP += (theD[lastG]/((theR/100)-(theG[lastG]/100)))/Math.pow(1+theR/100,lastG-1); 	form.POutput.value = "" + Math.round(theP*100)/100; 	return true;}   function isPositiveNumber(inputStr) { 	var decFlag = false; 	if (inputStr == ".") { 		alert("Please make sure that only numbers are input."); 		return false; 	} 	for (var i = 0; i < inputStr.length; i++) { 		var oneChar = inputStr.substring(i,i+1); 		if (((oneChar >= "0") && (oneChar <= "9")) || ((oneChar == ".") && (decFlag == false))) { 		 		} else { 			alert("Please make sure that only numbers are input."); 			return false; 		} 		if (oneChar == ".") {	 		decFlag = true;	 	} 	}	return true; }  function isNumber(inputStr) { 	var decFlag = false; 	if (inputStr == ".") { 		alert("Please make sure that only numbers are input."); 		return false; 	} 	for (var i = 0; i < inputStr.length; i++) { 		var oneChar = inputStr.substring(i,i+1); 		if ((i == 0) && (inputStr.length > 1)) {	 		if (((oneChar >= "0") && (oneChar <= "9")) || ((oneChar == ".") && (decFlag == false)) || (oneChar == "-")) {	 			 		} else {	 			alert("Please make sure that only numbers are input.");	 			return false;	 		}	 	} else {	 		if (((oneChar >= "0") && (oneChar <= "9")) || ((oneChar == ".") && (decFlag == false))) {	 			 		} else {	 			alert("Please make sure that only numbers are input.");	 			return false;	 		}	 	}	 	if (oneChar == ".") {	 		decFlag = true;	 	}	 	}	return true; } function launchSGStockCalc() {    window.open("SGStockCalcWindow.html","Win2","menubar,resizable,height=425,width=315");}