var retA;
var retB;
var sdA;
var sdB;
var corr;
var weight;
var gHaveProblem = 0;

function getCRChars() {
    var theAgent = navigator.userAgent;
    if (theAgent.indexOf("(Win") >= 0) {
    	return "\r\n"; 
    } else if (theAgent.indexOf("(Mac") >= 0) {
    	return "\r"; 
    } else {
    	return "\n";
    }
}


function newProblem(form) {
	gHaveProblem = 1;
    var CR = getCRChars();
    retA = Math.round(Math.random()*30) + 1; 
    retB = Math.round(Math.random()*15) + retA; 
    sdA = Math.round(Math.random()*(retA*1.5)) + 1; 
    sdB = Math.round(Math.random()*15) + sdA; 
    corr = (Math.round(Math.random()*100))/100; 
    weight = Math.round(Math.random()*8 + 1)*10;
    isNeg = Math.random(); 
    if (isNeg < 0.3) corr *= -1;
    probString = "Find the Expected Return, Variance, and Standard ";
    probString += "Deviation for a portfolio formed with Stocks 1 and 2. ";
    //probString += "Also, plot the Opportunity Set.";
    probString += "The Expected Return for Stock 1 is " + retA + "\%";
    probString += ", the Standard Deviation for Stock 1 is " + sdA + "\%";
    probString += ", the Expected Return for Stock 2 is " + retB + "\%";
    probString += ", the Standard Deviation for Stock 2 is " + sdB + "\%";
    probString += ", the Weight of Stock 1 in the portfolio is " + weight + "\%";
    probString += ", and the Correllation Coefficient between the returns ";
    probString += "on Stocks 1 and 2 is " + corr + ".";
    form.RandomProb.value = probString;
}

function doProblem() {
	if (gHaveProblem == 0) {
    	alert("Please click the New Problem button.");
  	} else {
  		document.PortfolioCalc.er1.value = "" + retA;
  		document.PortfolioCalc.er2.value = "" + retB;
  		document.PortfolioCalc.sd1.value = "" + sdA;
  		document.PortfolioCalc.sd2.value = "" + sdB;
  		document.PortfolioCalc.corr.value = "" + corr;
  		document.PortfolioCalc.w0.value = "" + weight;
    	doPortfolioCalc(document.PortfolioCalc);
    }	
}

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 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;
 }
 


