function calcPV(inFV, inNR, inNP, inC) {
	var outPV = inFV*Math.pow((1 + inNR/(100*inC)),(-inNP));
 	return outPV;
}

function calcFV(inPV, inNR, inNP, inC) {
	var outFV = inPV*Math.pow((1 + inNR/(100*inC)),(inNP));
 	return outFV;
}

function doCFCalc(form) {
 	var theCF0 = form.cf0.value;
 	if (!isNumber(theCF0)) {
 		return false;
 	}
 	if (theCF0 == "") theCF0 = 0;
 	var theC01 = form.c01.value;
 	if (!isNumber(theC01)) {
 		return false;
 	}
 	if (theC01 == "") theC01 = 0;
 	var theF01 = form.f01.value;
 	if (!isPositiveNumber(theF01)) {
 		return false;
 	}
 	if (theF01 == "") theF01 = 0;
 	var theC02 = form.c02.value;
 	if (!isNumber(theC02)) {
 		return false;
 	}
 	if (theC02 == "") theC02 = 0;
 	var theF02 = form.f02.value;
 	if (!isPositiveNumber(theF02)) {
 		return false;
 	}
 	if (theF02 == "") theF02 = 0;
 	var theC03 = form.c03.value;
 	if (!isNumber(theC03)) {
 		return false;
 	}
 	if (theC03 == "") theC03 = 0;
 	var theF03 = form.f03.value;
 	if (!isPositiveNumber(theF03)) {
 		return false;
 	}
 	if (theF03 == "") theF03 = 0;
 	var theC04 = form.c04.value;
 	if (!isNumber(theC04)) {
 		return false;
 	}
 	if (theC04 == "") theC04 = 0;
 	var theF04 = form.f04.value;
 	if (!isPositiveNumber(theF04)) {
 		return false;
 	}
 	if (theF04 == "") theF04 = 0;
 	var theC05 = form.c05.value;
 	if (!isNumber(theC05)) {
 		return false;
 	}
 	if (theC05 == "") theC05 = 0;
 	var theF05 = form.f05.value;
 	if (!isPositiveNumber(theF05)) {
 		return false;
 	}
 	if (theF05 == "") theF05 = 0;
 	var theC06 = form.c06.value;
 	if (!isNumber(theC06)) {
 		return false;
 	}
 	if (theC06 == "") theC06 = 0;
 	var theF06 = form.f06.value;
 	if (!isPositiveNumber(theF06)) {
 		return false;
 	}
 	if (theF06 == "") theF06 = 0;
 	var theC07 = form.c07.value;
 	if (!isNumber(theC07)) {
 		return false;
 	}
 	if (theC07 == "") theC07 = 0;
 	var theF07 = form.f07.value;
 	if (!isPositiveNumber(theF07)) {
 		return false;
 	}
 	if (theF07 == "") theF07 = 0;
 	var theC08 = form.c08.value;
 	if (!isNumber(theC08)) {
 		return false;
 	}
 	if (theC08 == "") theC08 = 0;
 	var theF08 = form.f08.value;
 	if (!isPositiveNumber(theF08)) {
 		return false;
 	}
 	if (theF08 == "") theF08 = 0;
 	var theC09 = form.c09.value;
 	if (!isNumber(theC09)) {
 		return false;
 	}
 	if (theC09 == "") theC09 = 0;
 	var theF09 = form.f09.value;
 	if (!isPositiveNumber(theF09)) {
 		return false;
 	}
 	if (theF09 == "") theF09 = 0;
 	var theC10 = form.c10.value;
 	if (!isNumber(theC10)) {
 		return false;
 	}
 	if (theC10 == "") theC10 = 0;
 	var theF10 = form.f10.value;
 	if (!isPositiveNumber(theF10)) {
 		return false;
 	}
 	if (theF10 == "") theF10 = 0;
 	var theNR = form.NRInput.value;
 	if (!isPositiveNumber(theNR)) {
 		return false;
 	}
 	if (theNR == "") theNR = 0;
 	var theFVWhen = form.FVWhenInput.value;
 	if (!isPositiveNumber(theFVWhen)) {
 		return false;
 	}
 	if (theFVWhen == "") theFVWhen = 0;
 	var theC;
 	var theCompounding = form.CInput.selectedIndex;
 	if (theCompounding == 0) {
 		theC = 1;
 	} else if (theCompounding == 1) {
 		theC = 2;
 	} else if (theCompounding == 2) {
 		theC = 4;
 	} else if (theCompounding == 3) {
 		theC = 12;
 	} else if (theCompounding == 4) {
 		theC = 52;
 	}
 	var thePV = 1.0*theCF0;
 	var theCnt = 1;
 	for (i=1; i <= theF01; i++) {
 		thePV = thePV + calcPV(theC01, theNR, theCnt, theC);
 		theCnt++;
 	}
 	for (i=1; i <= theF02; i++) {
 		thePV = thePV + calcPV(theC02, theNR, theCnt, theC);
 		theCnt++;
 	}
 	for (i=1; i <= theF03; i++) {
 		thePV = thePV + calcPV(theC03, theNR, theCnt, theC);
 		theCnt++;
 	}
 	for (i=1; i <= theF04; i++) {
 		thePV = thePV + calcPV(theC04, theNR, theCnt, theC);
 		theCnt++;
 	}
 	for (i=1; i <= theF05; i++) {
 		thePV = thePV + calcPV(theC05, theNR, theCnt, theC);
 		theCnt++;
 	}
 	for (i=1; i <= theF06; i++) {
 		thePV = thePV + calcPV(theC06, theNR, theCnt, theC);
 		theCnt++;
 	}
 	for (i=1; i <= theF07; i++) {
 		thePV = thePV + calcPV(theC07, theNR, theCnt, theC);
 		theCnt++;
 	}
 	for (i=1; i <= theF08; i++) {
 		thePV = thePV + calcPV(theC08, theNR, theCnt, theC);
 		theCnt++;
 	}
 	for (i=1; i <= theF09; i++) {
 		thePV = thePV + calcPV(theC09, theNR, theCnt, theC);
 		theCnt++;
 	}
 	for (i=1; i <= theF10; i++) {
 		thePV = thePV + calcPV(theC10, theNR, theCnt, theC);
 		theCnt++;
 	}
 	var theFV = calcFV(thePV,theNR,theFVWhen,theC);
 	form.PVOutput.value = "" + Math.round(thePV*100)/100;
 	form.FVOutput.value = "" + Math.round(theFV*100)/100;
 	form.FVWhenInput.value = "" + theFVWhen;
 	return true;
}

function clearCFCalc(form) {
	form.cf0.value = "0";
	form.c01.value = "0";
	form.f01.value = "1";
	form.c02.value = "0";
	form.f02.value = "1";
	form.c03.value = "0";
	form.f03.value = "1";
	form.c04.value = "0";
	form.f04.value = "1";
	form.c05.value = "0";
	form.f05.value = "1";
	form.c06.value = "0";
	form.f06.value = "1";
	form.c07.value = "0";
	form.f07.value = "1";
	form.c08.value = "0";
	form.f08.value = "1";
	form.c09.value = "0";
	form.f09.value = "1";
	form.c10.value = "0";
	form.f10.value = "1";
	form.NRInput.value = "10";
	form.PVOutput.value = "";	
	form.FVWhenInput.value = "10";
	form.FVOutput.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;
 }

var gNumQ = 8;
var gCF = new Array(gNumQ+1);
var gNR = 0.00;
var gWhen = 8;
var gCP = 0;
var gCPString = "Annual";
var gAnnualOnly = false;
var gRepeat = 1;

function newProb(form) {
	gCF[0] = 0;
	gCF[1] = Math.round(Math.random()*10)*100;
	gRepeat = Math.random();
	if (gRepeat < 0.4) {
		gCF[2] = gCF[1];
	} else {
		gCF[2] = Math.round(Math.random()*10)*100;
	}
	gRepeat = Math.random();
	if (gRepeat < 0.4) {
		gCF[3] = gCF[2];
	} else {
		gCF[3] = Math.round(Math.random()*10)*100;
	}
	gRepeat = Math.random();
	if (gRepeat < 0.4) {
		gCF[4] = gCF[3];
	} else {
		gCF[4] = Math.round(Math.random()*10)*100;
	}
	gRepeat = Math.random();
	if (gRepeat < 0.4) {
		gCF[5] = gCF[4];
	} else {
		gCF[5] = Math.round(Math.random()*10)*100;
	}
	gRepeat = Math.random();
	if (gRepeat < 0.4) {
		gCF[6] = gCF[5];
	} else {
		gCF[6] = Math.round(Math.random()*10)*100;
	}
	gRepeat = Math.random();
	if (gRepeat < 0.4) {
		gCF[7] = gCF[6];
	} else {
		gCF[7] = Math.round(Math.random()*10)*100;
	}
	gRepeat = Math.random();
	if (gRepeat < 0.4) {
		gCF[8] = gCF[7];
	} else {
		gCF[8] = Math.round(Math.random()*10)*100;
	}
	gNR = Math.round(Math.random()*2000)/100;
	gWhen = 8;
	if (form.AnnualBox.checked) {
		gAnnualOnly = true;
	} else {
		gAnnualOnly = false;
	}
	if (gAnnualOnly) {
  		gCP = 0;
  		gCPString = "Annual";
  	} else {
    	gCP = Math.round(Math.random()*3);
    	if (gCP == 0) {
    		gCPString = "Annual";
    	} else if (gCP == 1) {
    		gCPString = "Semiannual";
    	} else if (gCP == 2) {
    		gCPString = "Quarterly";
    	} else if (gCP == 3) {
    		gCPString = "Monthly";
    	}
  	}
  	form.fNR.value="" + gNR;
  	form.Compounding.value = gCPString;
  	form.fCF0.value="" + gCF[0];
  	form.fCF1.value="" + gCF[1];
  	form.fCF2.value="" + gCF[2];
  	form.fCF3.value="" + gCF[3];
  	form.fCF4.value="" + gCF[4];
  	form.fCF5.value="" + gCF[5];
  	form.fCF6.value="" + gCF[6];
  	form.fCF7.value="" + gCF[7];
  	form.fCF8.value="" + gCF[8];
}

function doProb() {
	var j;
	var k;
	var n;
	clearCFCalc(document.CFCalculator);
	scriptSetCellValue(0,gCF[0],1);
	k = 0;
	for (i = 1; i <= gNumQ; i++) {
		j = 1;
		k++;
		n = i;
		while ((i <= gNumQ) && (gCF[i] == gCF[i+1])) {
			i++;
			j++;	
		}
		if (i == gNumQ && gCF[n] == 0) {
			// do nothing
		} else {	
			scriptSetCellValue(k,gCF[n],j);
		}
	}
	scriptSetRate(gNR);
	scriptSetWhen(gWhen);
	if (gAnnualOnly) {
		scriptSetCompounding(0);
	} else {
		scriptSetCompounding(gCP);
	}
	doCFCalc(document.CFCalculator);
}

function scriptSetRate(inRate) {
	document.CFCalculator.NRInput.value = inRate;
}

function scriptSetCompounding(inVal) {
	document.CFCalculator.CInput.selectedIndex = inVal;
}

function scriptSetWhen(inWhen) {
	document.CFCalculator.FVWhenInput.value = inWhen;
}

function scriptSetCellValue(inIndex,inVal,inFreq) {
	if (inIndex == 0) {
		document.CFCalculator.cf0.value = inVal;
	} else if (inIndex == 1) {
		document.CFCalculator.c01.value = inVal;
		document.CFCalculator.f01.value = inFreq;
	} else if (inIndex == 2) {
		document.CFCalculator.c02.value = inVal;
		document.CFCalculator.f02.value = inFreq;
	} else if (inIndex == 3) {
		document.CFCalculator.c03.value = inVal;
		document.CFCalculator.f03.value = inFreq;
	} else if (inIndex == 4) {
		document.CFCalculator.c04.value = inVal;
		document.CFCalculator.f04.value = inFreq;
	} else if (inIndex == 5) {
		document.CFCalculator.c05.value = inVal;
		document.CFCalculator.f05.value = inFreq;
	} else if (inIndex == 6) {
		document.CFCalculator.c06.value = inVal;
		document.CFCalculator.f06.value = inFreq;
	} else if (inIndex == 7) {
		document.CFCalculator.c07.value = inVal;
		document.CFCalculator.f07.value = inFreq;
	} else if (inIndex == 8) {
		document.CFCalculator.c08.value = inVal;
		document.CFCalculator.f08.value = inFreq;
	} else if (inIndex == 9) {
		document.CFCalculator.c09.value = inVal;
		document.CFCalculator.f09.value = inFreq;
	} else if (inIndex == 10) {
		document.CFCalculator.c10.value = inVal;
		document.CFCalculator.f10.value = inFreq;
	}
}
