function calcR(inRF, inRM, inB) {
	var outR = "";
	if (inRF >= inRM) {
		alert("Cannot compute the expected return (E[Rm] must be greater than Rf).");
		return outR;
	}	
 	outR = inRF + (inRM - inRF)*inB;
 	return outR;
}

function calcB(inR, inRF, inRM) {
	var outB = "";
	if (inRF >= inRM) {
		alert("Cannot compute the Beta (E[Rm] must be greater than Rf).");
		return outB;
	}	
	if (inRF > inR) {
		alert("Cannot compute the Beta (E[Ri] must be greater than or equal to  Rf).");
		return outB;
	}	
 	outB = (inR - inRF)/(inRM - inRF);
 	return outB;
}

function calcRM(inR, inRF, inB) {
	var outRM = "";
	if (inRF > inR) {
		alert("Cannot compute the Expected Return on the Market (E[Ri] must be greater than or equal to Rf).");
		return outRM;
	}
	if (inB == 0) {
		alert("Cannot compute the Expected Return on the Market (Beta equals 0).");
		return outRM;
	}	
	outRM = inRF + (inR - inRF)/inB;
	return outRM;
}

function calcRF(inR, inRM, inB) {
	var outRF = "";
	if (inB == 1) {
		alert("Cannot compute the Risk Free Rate (Beta equals 1).");
		return outRF;
	}	
	outRF = (inR - inRM*inB)/(1-inB);
	return outRF;	
}

function findR(form) {
 	var theRF = form.RFInput.value;
 	if (!isPositiveNumber(theRF)) {
 		return false;
 	}
 	if (theRF == "") theRF = 0;
 	theRF = parseFloat(theRF);
 	var theRM = form.RMInput.value;
 	if (!isPositiveNumber(theRM)) {
 		return false;
 	}
 	if (theRM == "") theRM = 0;
 	theRM = parseFloat(theRM);
 	var theB = form.BInput.value;
 	if (!isNumber(theB)) {
 		return false;
 	}
 	if (theB == "") theB = 0;
 	theB = parseFloat(theB);
 	var theR = calcR(theRF, theRM, theB);
 	form.RInput.value = "" + Math.round(theR*100)/100;
 	return true;
}
 
function findRF(form) {
 	var theR = form.RInput.value;
 	if (!isPositiveNumber(theR)) {
 		return false;
 	}
 	if (theR == "") theR = 0;
 	theR = parseFloat(theR);
 	var theRM = form.RMInput.value;
 	if (!isPositiveNumber(theRM)) {
 		return false;
 	}
 	if (theRM == "") theRM = 0;
 	theRM = parseFloat(theRM);
 	var theB = form.BInput.value;
 	if (!isNumber(theB)) {
 		return false;
 	}
 	if (theB == "") theB = 0;
 	theB = parseFloat(theB);
 	var theRF = calcRF(theR, theRM, theB);
 	form.RFInput.value = "" + Math.round(theRF*100)/100;
 	return true;
}

function findRM(form) {
 	var theR = form.RInput.value;
 	if (!isPositiveNumber(theR)) {
 		return false;
 	}
 	if (theR == "") theR = 0;
 	theR = parseFloat(theR);
 	var theRF = form.RFInput.value;
 	if (!isPositiveNumber(theRF)) {
 		return false;
 	}
 	if (theRF == "") theRF = 0;
 	theRF = parseFloat(theRF);
 	var theB = form.BInput.value;
 	if (!isNumber(theB)) {
 		return false;
 	}
 	if (theB == "") theB = 0;
 	theB = parseFloat(theB);
 	var theRM = calcRM(theR, theRF, theB);
 	form.RMInput.value = "" + Math.round(theRM*100)/100;
 	return true;
}

function findB(form) {
 	var theR = form.RInput.value;
 	if (!isPositiveNumber(theR)) {
 		return false;
 	}
 	if (theR == "") theR = 0;
 	theR = parseFloat(theR);
 	var theRF = form.RFInput.value;
 	if (!isPositiveNumber(theRF)) {
 		return false;
 	}
 	if (theRF == "") theRF = 0;
 	theRF = parseFloat(theRF);
 	var theRM = form.RMInput.value;
 	if (!isPositiveNumber(theRM)) {
 		return false;
 	}
 	if (theRM == "") theRM = 0;
 	theRM = parseFloat(theRM);
 	var theB = calcB(theR, theRF, theRM);
 	form.BInput.value = "" + Math.round(theB*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 launchCAPMCalc() {
    window.open("CAPMCalcWindow.html","Win2","menubar,resizable,height=205,width=315");
}
