
function newProblem(form) {
    var retA = Math.round(Math.random()*30) + 1; 
    var retB = Math.round(Math.random()*15) + retA; 
    var sdA = Math.round(Math.random()*(retA*1.5)) + 1; 
    var sdB = Math.round(Math.random()*15) + sdA; 
    var corr = (Math.round(Math.random()*100))/100; 
    var weight = Math.round(Math.random()*8 + 1)*10;
    var isNeg = Math.random(); 
    if (isNeg < 0.3) corr *= -1;
   	form.er1.value = "" + Math.round(retA*100)/100;
   	form.er2.value = "" + Math.round(retB*100)/100;
   	form.sd1.value = "" + Math.round(sdA*100)/100;
   	form.sd2.value = "" + Math.round(sdB*100)/100;
   	form.corr.value = "" + Math.round(corr*100)/100;
   	form.w0.value = "" + Math.round(weight*100)/100;
   	form.per0.value = "";
   	form.pvar0.value = "";
   	form.psd0.value = "";
}

function doPortfolioCalc(form) {
 	var theER1 = form.er1.value;
 	if (!isNumber(theER1)) {
 		return false;
 	}
 	if (theER1 == "") theER1 = 0;
 	theER1 = parseFloat(theER1);
 	var theER2 = form.er2.value;
 	if (!isNumber(theER2)) {
 		return false;
 	}
 	if (theER2 == "") theER2 = 0;
 	theER2 = parseFloat(theER2);
 	var theSD1 = form.sd1.value;
 	if (!isPositiveNumber(theSD1)) {
 		return false;
 	}
 	if (theSD1 == "") theSD1 = 0;
 	theSD1 = parseFloat(theSD1);
 	var theSD2 = form.sd2.value;
 	if (!isPositiveNumber(theSD2)) {
 		return false;
 	}
 	if (theSD2 == "") theSD2 = 0;
 	theSD1 = parseFloat(theSD1);
 	var theCorr = form.corr.value;
 	if (!isNumber(theCorr)) {
 		return false;
 	}
 	if (theCorr == "") theCorr = 0;
 	theCorr = parseFloat(theCorr);
 	if ((theCorr > 1) || (theCorr < -1)) {
 		alert("The correlation coefficient must be in the range from -1 to +1.");
 		return false;
 	}
 	var theW = new Array(11);
 	for (i=0; i<= 0; i++) {
	 	theW[i] = eval("form.w" + i + ".value");
	 	if (!isNumber(theW[i])) {
	 		return false;
	 	}
	 	if (theW[i] == "") {
	 		theW[i] = 0;
	 		eval("form.w." + i + ".value = 0");
	 	}
	 	theW[i] = parseFloat(theW[i]);
	 	theW[i] = theW[i]/100;
 	}
 	var thePER = new Array(11);
 	var thePVAR = new Array(11);
 	var thePSD = new Array(11);
 	for (i=0; i<=0; i++) {
 		thePER[i] = theW[i]*theER1 + (1 - theW[i])*theER2;
 		thePVAR[i] = (theW[i]*theW[i]*theSD1*theSD1 + (1-theW[i])*(1-theW[i])*theSD2*theSD2 + 2*theW[i]*(1-theW[i])*theCorr*theSD1*theSD2)/10000;
 		thePSD[i] = Math.sqrt(thePVAR[i]);
 	}
 	for (i=0; i<=0; i++) {
 		eval("form.per" + i + ".value = " + Math.round(thePER[i]*100)/100);
 		eval("form.pvar" + i + ".value = " + Math.round(thePVAR[i]*100000)/100000);
 		eval("form.psd" + i + ".value = " + Math.round(thePSD[i]*10000)/100);
 	}
 	return true;
}

function clearPortfolioCalc(form) {
	form.er1.value = "";
	form.er2.value = "";
	form.sd1.value = "";
	form.sd2.value = "";
	form.corr.value = "";
	for (i=0; i<=0; i++) {
		eval("form.w" + i + ".value = " + Math.round(100 - i*10));
		eval("form.per" + i + ".value = ''");
		eval("form.pvar" + i + ".value = ''");
		eval("form.psd" + i + ".value = ''");
	}
}

function helpPortfolioCalc(form) {
 	var theER1 = form.er1.value;
 	if (!isNumber(theER1)) {
 		return false;
 	}
 	if (theER1 == "") theER1 = 0;
 	theER1 = parseFloat(theER1);
 	var theER2 = form.er2.value;
 	if (!isNumber(theER2)) {
 		return false;
 	}
 	if (theER2 == "") theER2 = 0;
 	theER2 = parseFloat(theER2);
 	var theSD1 = form.sd1.value;
 	if (!isPositiveNumber(theSD1)) {
 		return false;
 	}
 	if (theSD1 == "") theSD1 = 0;
 	theSD1 = parseFloat(theSD1);
 	var theSD2 = form.sd2.value;
 	if (!isPositiveNumber(theSD2)) {
 		return false;
 	}
 	if (theSD2 == "") theSD2 = 0;
 	theSD1 = parseFloat(theSD1);
 	var theCorr = form.corr.value;
 	if (!isNumber(theCorr)) {
 		return false;
 	}
 	if (theCorr == "") theCorr = 0;
 	theCorr = parseFloat(theCorr);
 	if ((theCorr > 1) || (theCorr < -1)) {
 		alert("The correlation coefficient must be in the range from -1 to +1.");
 		return false;
 	}
 	var theW = new Array(11);
 	for (i=0; i<= 0; i++) {
	 	theW[i] = eval("form.w" + i + ".value");
	 	if (!isNumber(theW[i])) {
	 		return false;
	 	}
	 	if (theW[i] == "") {
	 		theW[i] = 0;
	 		eval("form.w." + i + ".value = 0");
	 	}
	 	theW[i] = parseFloat(theW[i]);
	 	theW[i] = theW[i]/100;
 	}
 	var thePER = new Array(11);
 	var thePVAR = new Array(11);
 	var thePSD = new Array(11);
 	for (i=0; i<=0; i++) {
 		thePER[i] = theW[i]*theER1 + (1 - theW[i])*theER2;
 		thePVAR[i] = (theW[i]*theW[i]*theSD1*theSD1 + (1-theW[i])*(1-theW[i])*theSD2*theSD2 + 2*theW[i]*(1-theW[i])*theCorr*theSD1*theSD2)/10000;
 		thePSD[i] = Math.sqrt(thePVAR[i]);
 	}
 	var theOut = "<HTML><HEAD><TITLE>Portfolio Help</TITLE></HEAD><BODY bgcolor='white'>";
 	theOut += "<H2>Portfolio Help</H2>";
 	theOut += "<H3>Expected Return:</H3>";
 	theOut += "<p>E[R<SUB>p</SUB>] = " + Math.round(theW[0]*100)/100 + "(" + Math.round(theER1*100)/100 + "\%) + (1 - " + Math.round(theW[0]*100)/100 + ")(" + Math.round(theER2*100)/100 + "\%)</p>";
 	theOut += "<p>E[R<SUB>p</SUB>] = " + Math.round(thePER[0]*100)/100 + "\%</p>";
 	theOut += "<H3>Variance:</H3>";
 	theOut += "<p>Var[R<SUB>p</SUB>] = (" + Math.round(theW[0]*100)/100 + ")<SUP>2</SUP>(" + Math.round(theSD1*100)/10000 + ")<SUP>2</SUP> + (1 - " + Math.round(theW[0]*100)/100 + ")<SUP>2</SUP>(" + Math.round(theSD2*100)/10000 + ")<SUP>2</SUP> + 2(" + Math.round(theW[0]*100)/100 + ")(1 - " + Math.round(theW[0]*100)/100 + ")(" + Math.round(theCorr*1000)/1000 + ")(" +  Math.round(theSD1*100)/10000 + ")(" + Math.round(theSD2*100)/10000 + ")</p>";
 	theOut += "<p>Var[R<SUB>p</SUB>] = " + Math.round(thePVAR[0]*10000)/10000 + "</p>";
 	theOut += "<H3>Standard Deviation:</H3>";
 	theOut += "<p>StdDev[R<SUB>p</SUB>] = (" + Math.round(thePVAR[0]*10000)/10000 + ")<SUP>1/2</SUP></p>";
 	theOut += "<p>StdDev[R<SUB>p</SUB>] = " + Math.round(thePSD[0]*10000)/10000 + " = " + Math.round(thePSD[0]*10000)/100 + "\%</p>";
 	theOut += "<CENTER>";
	theOut += "<P><FORM NAME=\"Form1\">";
	theOut += "<INPUT TYPE=\"button\" VALUE=\"   OK   \" onClick=\"window.close();\"></FORM></P></CENTER>";
 	theOut += "</BODY></HTML>";
 	var newWin = window.open("","","scrollbars,resizable,width=450,height=400");
 	if (newWin != null) {
 		newWin.document.write(theOut);
 		newWin.document.close();
 		newWin.focus();
 	}
 	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;
 }
 


