
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");
}

