var gERi = 0;
var gRf = 0;
var gERm = 0;
var gBeta = 0;

var gPR = "E[Ri] Button";

var gType = 0; // ERi1 =1, ERi2 =5, ERi3 =6, Rf =2, ERm =3, Beta =4, if 0 => no problem
var gNoMRP = true;

function myAlert(theERI,theRF,theERM,theBETA,thePR) { 
  var ETF = "\nEnter the Following:\n"; 
  var ERI = "Expected Return on Stock i = "; 
  var RF = "\nRisk Free Rate = "; 
  var ERM = "\nExpected Return on the Market = "; 
  var BETA = "\nBeta = "; 
  var PR = "\n\nThen Press the "; 
  if (gType == 0) {
     alert("Please click the New Problem button.");
  } else {      
     alert(ETF+ERI+theERI+RF+theRF+ERM+theERM+BETA+theBETA+PR+thePR); 
  } 
}

function newProblem(form) { // make 40% of the problems for E(Ri)
  gType = Math.round(Math.random()*4)+1;
   if (gType <= 2) {
     randomERIProblem(form);
  } else if (gType == 3) {
     randomRFProblem(form);
  } else if (gType == 4) {
     randomERMProblem(form);
  } else if (gType == 5) {
     randomBETAProblem(form);
  }  
}

function doProblem() { // make 40% of the problems for E(Ri)
  if (gType == 0) {
     alert("Please click the New Problem button.");
  } else if (gType <= 2) {
     scriptERi();
  } else if (gType == 3) {
     scriptRf();
  } else if (gType == 4) {
     scriptERm();
  } else if (gType == 5) {
     scriptBeta();
  }
}

function scriptERi() {
	document.CAPMCalc.RFInput.value = gRf;
	document.CAPMCalc.RMInput.value = gERm;
	document.CAPMCalc.BInput.value = gBeta;
	var theOut = calcR(gRf,gERm,gBeta);
	theOut = Math.round(theOut*100)/100;
	document.CAPMCalc.RInput.value = theOut;
}

function scriptRf() {
	document.CAPMCalc.RInput.value = gERi;
	document.CAPMCalc.RMInput.value = gERm;
	document.CAPMCalc.BInput.value = gBeta;
	var theOut = calcRF(gERi,gERm,gBeta);
	theOut = Math.round(theOut*100)/100;
	document.CAPMCalc.RFInput.value = theOut;
}

function scriptERm() {
	document.CAPMCalc.RInput.value = gERi;
	document.CAPMCalc.RFInput.value = gRf;
	document.CAPMCalc.BInput.value = gBeta;
	var theOut = calcRM(gERi,gRf,gBeta);
	theOut = Math.round(theOut*100)/100;
	document.CAPMCalc.RMInput.value = theOut;
}

function scriptBeta() {
	document.CAPMCalc.RInput.value = gERi;
	document.CAPMCalc.RFInput.value = gRf;
	document.CAPMCalc.RMInput.value = gERm;
	var theOut = calcB(gERi,gRf,gERm);
	theOut = Math.round(theOut*100)/100;
	document.CAPMCalc.BInput.value = theOut;
}

function randomERIProblem(form) {
  gRf = (Math.round(Math.random()*100 + 10))/10; 
  gERm = (Math.round(Math.random()*100 + gRf*10 + 10))/10; 
  gBeta = (Math.round(Math.random()*30 + 1))/10;
  gERi = 0; 
  gPR = "E[Ri] Button";
  var theOutText = "";
  theOutText += "Find the Expected Return on Stock i given that the "; 
  theOutText += "Expected Return on the Market Portfolio is "; 
  theOutText += "" + gERm + "\%, the Risk-Free Rate is "; 
  theOutText += "" + gRf + "\%, and the Beta for Stock i is " + gBeta + ".";
  form.RandomProb.value = theOutText;                    
 }
 
 function randomRFProblem(form) {
  gRf = (Math.round(Math.random()*100 + 10))/10; 
  gERm = (Math.round(Math.random()*100 + gRf*10 + 10))/10; 
  gBeta = (Math.round(Math.random()*30 + 1))/10;
  gERi = (Math.round(10*(gRf + (gERm - gRf)*gBeta)))/10;  
  gRf = 0; 
  gPR = "Rf Button";
  var theOutText = "";
  theOutText += "Find the Risk-Free Rate given that the "; 
  theOutText += "Expected Return on Stock i is "; 
  theOutText += "" + gERi + "\%, the Expected Return on the Market Portfolio is "; 
  theOutText += "" + gERm + "\%, and the Beta for Stock i is " + gBeta + ".";
  form.RandomProb.value = theOutText;                     
 }
 
 function randomERMProblem(form) {
  gRf = (Math.round(Math.random()*100 + 10))/10; 
  gERi = (Math.round(Math.random()*100 + gRf*10 + 10))/10; 
  gBeta = (Math.round(Math.random()*30 + 5))/10;
  gERm = 0; 
  gPR = "E[Rm] Button";
  var theOutText = "";
  theOutText += "Find the Expected Return on the Market Portfolio given that the "; 
  theOutText += "Expected Return on Stock i is "; 
  theOutText += "" + gERi + "\%, the Risk-Free Rate is ";
  theOutText += "" + gRf + "\%, and the Beta for Stock i is " + gBeta + ".";
  form.RandomProb.value = theOutText;
 }
 
 function randomBETAProblem(form) {
  gRf = (Math.round(Math.random()*110 + 10))/10; 
  gERm = (Math.round(Math.random()*110 + gRf*10 +10))/10; 
  gERi = (Math.round(Math.random()*110 + gRf*10))/10;
  gBeta = 0; 
  gPR = "Beta Button";
  var theOutText = "";
  theOutText += "Find the Beta for Stock i given that the "; 
  theOutText += "Expected Return on Stock i is "; 
  theOutText += "" + gERi + "\%, the Expected Return on the Market Portfolio is "; 
  theOutText += "" + gERm + "\%, and  the Risk-Free Rate is "; 
  theOutText += "" + gRf + "\%.";
  form.RandomProb.value = theOutText;
 }


//end paste
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 launchCGStockCalc() {
    window.open("CGStockCalcWindow.html","Win2","menubar,resizable,height=205,width=315");
}
