
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;	
}

function findP(form) {
 	var theG = form.GInput.value;
 	if (!isNumber(theG)) {
 		return false;
 	}
 	if (theG == "") theG = 0;
 	var theR = form.RInput.value;
 	if (!isPositiveNumber(theR)) {
 		return false;
 	}
 	if (theR == "") theR = 0;
 	var theD = form.DInput.value;
 	if (!isPositiveNumber(theD)) {
 		return false;
 	}
 	if (theD == "") theD = 0;
 	var theDNum;
 	var theDNum = form.DivNum.selectedIndex;
 	if (theDNum == 0) {
 		theDNum = 0;
 	} else {
 		theDNum = 1;
 	}
 	var theP = calcP(theDNum, theD, theG, theR);
 	form.PInput.value = "" + Math.round(theP*100)/100;
 	return true;
}
 
function findD(form) {
	var theG = form.GInput.value;
 	if (!isNumber(theG)) {
 		return false;
 	}
 	if (theG == "") theG = 0;
 	var theR = form.RInput.value;
 	if (!isPositiveNumber(theR)) {
 		return false;
 	}
 	if (theR == "") theR = 0;
 	var theP = form.PInput.value;
 	if (!isPositiveNumber(theP)) {
 		return false;
 	}
 	if (theP == "") theP = 0;
 	var theDNum;
 	var theDNum = form.DivNum.selectedIndex;
 	if (theDNum == 0) {
 		theDNum = 0;
 	} else {
 		theDNum = 1;
 	}
 	var theD = calcD(theDNum, theG, theR, theP);
	form.DInput.value = "" + Math.round(theD*100)/100;
	form.DivNum.selectedIndex = theDNum;
	return true;
}

function findG(form) {
 	var theD = form.DInput.value;
 	if (!isPositiveNumber(theD)) {
 		return false;
 	}
 	if (theD == "") theD = 0;
 	var theR = form.RInput.value;
 	if (!isPositiveNumber(theR)) {
 		return false;
 	}
 	if (theR == "") theR = 0;
 	var theP = form.PInput.value;
 	if (!isPositiveNumber(theP)) {
 		return false;
 	}
 	if (theP == "") theP = 0;
 	var theDNum;
 	var theDNum = form.DivNum.selectedIndex;
 	if (theDNum == 0) {
 		theDNum = 0;
 	} else {
 		theDNum = 1;
 	}
 	var theG = calcG(theDNum, theD, theR, theP);
 	form.GInput.value = "" + Math.round(theG*100)/100;
 	return true;
 }
 
 function findR(form) {
 	var theD = form.DInput.value;
 	if (!isPositiveNumber(theD)) {
 		return false;
 	}
 	if (theD == "") theD = 0;
 	var theG = form.GInput.value;
 	if (!isNumber(theG)) {
 		return false;
 	}
 	if (theG == "") theG = 0;
 	var theP = form.PInput.value;
 	if (!isPositiveNumber(theP)) {
 		return false;
 	}
 	if (theP == "") theP = 0;
 	var theDNum;
 	var theDNum = form.DivNum.selectedIndex;
 	if (theDNum == 0) {
 		theDNum = 0;
 	} else {
 		theDNum = 1;
 	}
 	var theR = calcR(theDNum, theD, theG, theP);
	form.RInput.value = "" + Math.round(theR*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 launchCGStockCalc() {
    window.open("CGStockCalcWindow.html","Win2","menubar,resizable,height=205,width=315");
}
