function calcPV(inFV, inNR, inNP, inC) {
	var outPV = inFV*Math.pow((1 + inNR/(100*inC)),(-inNP));
 	return outPV;
}

function calcIRR(inCFs, inNum) {
 	if (inNum == 0) return "";
 	if (inCFs[0] >= 0) return "";
 	var theSum = 0;
 	var theGuess = 10;
 	var theOldGuess = 10;
 	var theH = 0.001;
 	var thePV = 0;
 	var thePVH = 0;
 	var theDeriv = 0;
 	var theCnt = 0;
 	do {
 		thePV = inCFs[0];
 		thePVH = inCFs[0];
 		theSum = inCFs[0];
	 	for (i = 1; i <= inNum; i++) {
	 		theSum += inCFs[i];
	 		thePV += calcPV(inCFs[i],theGuess,i,1);
	 		thePVH += calcPV(inCFs[i],(theGuess + theH),i,1);
	 	}
	 	if (theSum < 0) return "";
	 	theDeriv = (thePVH - thePV)/theH;
	 	if (theDeriv != 0) {
	 		theOldGuess = theGuess;
	 		theGuess = theGuess - thePV/theDeriv;
	 	} else {
	 		return "";
	 	}	
	 	theCnt++;
	 	if (theCnt > 50) return "";
 	} while (Math.abs(thePV) > 0.01)
 	return theOldGuess;
}

function calcPayback(inCFs, inNum) {
 	if (inNum == 0) return "";
 	if (inCFs[0] >= 0) return "";
 	var theSum = 0;
 	var theYear = -1;
 	var theNCF = new Array(inNum + 1);
 	for (i = 0; i <= inNum; i++) {
 		theSum += inCFs[i];
 		if (i > 0) {
 			theNCF[i] = theNCF[i-1] + inCFs[i];
 		} else {
 			theNCF[i] = inCFs[i];
 		}
 		if ((theNCF[i] >= 0) && (theYear < 0)) {
 			theYear = i - 1;
 		}
 	}
 	if (theSum < 0) return "";
 	var thePayback;
 	if (theNCF[theYear + 1] == 0) {
 		return (theYear + 1);
 	}	
 	thePayback = theYear + (-theNCF[theYear])/inCFs[theYear + 1];
 	return thePayback;
}

function calcEAA(inCFs, inNum, inPV, inNR) {
 	if (inNum == 0) return "";
 	if (inPV == 0) return 0;
 	var theLife = 0;
 	for (i = 1; i <= inNum; i++) {
 		if (inCFs[i] != 0) {
 			theLife = i;
 		}
 	}
 	var theEAA = "";
 	if (theLife > 0) {
 		theEAA = inPV*(inNR/100)/(1-Math.pow(1+(inNR/100),-theLife));
 	}	
 	return theEAA;
}

function doCBCalc(form) {
	var theC = 1;
 	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 theNumCFs = parseInt(theF01) + parseInt(theF02) + parseInt(theF03) + parseInt(theF04);
 	theNumCFs = theNumCFs + parseInt(theF05) + parseInt(theF06) + parseInt(theF07);
 	theNumCFs = theNumCFs + parseInt(theF08) + parseInt(theF09) + parseInt(theF10);
 	var theCFs = new Array(theNumCFs+1);
 	theCFs[0] = parseFloat(theCF0);
 	var thePV = 1.0*theCF0;
 	var theCnt = 1;
 	for (i=1; i <= theF01; i++) {
 		thePV = thePV + calcPV(theC01, theNR, theCnt, theC);
 		theCFs[theCnt] = parseFloat(theC01);
 		theCnt++;
 	}
 	for (i=1; i <= theF02; i++) {
 		thePV = thePV + calcPV(theC02, theNR, theCnt, theC);
 		theCFs[theCnt] = parseFloat(theC02);
 		theCnt++;
 	}
 	for (i=1; i <= theF03; i++) {
 		thePV = thePV + calcPV(theC03, theNR, theCnt, theC);
 		theCFs[theCnt] = parseFloat(theC03);
 		theCnt++;
 	}
 	for (i=1; i <= theF04; i++) {
 		thePV = thePV + calcPV(theC04, theNR, theCnt, theC);
 		theCFs[theCnt] = parseFloat(theC04);
 		theCnt++;
 	}
 	for (i=1; i <= theF05; i++) {
 		thePV = thePV + calcPV(theC05, theNR, theCnt, theC);
 		theCFs[theCnt] = parseFloat(theC05);
 		theCnt++;
 	}
 	for (i=1; i <= theF06; i++) {
 		thePV = thePV + calcPV(theC06, theNR, theCnt, theC);
 		theCFs[theCnt] = parseFloat(theC06);
 		theCnt++;
 	}
 	for (i=1; i <= theF07; i++) {
 		thePV = thePV + calcPV(theC07, theNR, theCnt, theC);
 		theCFs[theCnt] = parseFloat(theC07);
 		theCnt++;
 	}
 	for (i=1; i <= theF08; i++) {
 		thePV = thePV + calcPV(theC08, theNR, theCnt, theC);
 		theCFs[theCnt] = parseFloat(theC08);
 		theCnt++;
 	}
 	for (i=1; i <= theF09; i++) {
 		thePV = thePV + calcPV(theC09, theNR, theCnt, theC);
 		theCFs[theCnt] = parseFloat(theC09);
 		theCnt++;
 	}
 	for (i=1; i <= theF10; i++) {
 		thePV = thePV + calcPV(theC10, theNR, theCnt, theC);
 		theCFs[theCnt] = parseFloat(theC10);
 		theCnt++;
 	}
 	var thePB = calcPayback(theCFs, theNumCFs);
 	var theIRR = calcIRR(theCFs, theNumCFs);
 	var theEAA = calcEAA(theCFs, theNumCFs, thePV, theNR);
 	form.NPVOutput.value = "" + Math.round(thePV*100)/100;
 	if (theIRR == "") {
 		form.IRROutput.value = "";
 	} else {
 		form.IRROutput.value = "" + Math.round(theIRR*100)/100;
 	}
 	if (thePB == "") {
 		form.PBOutput.value = "";
 	} else {
 		form.PBOutput.value = "" + Math.round(thePB*100)/100;
 	}	
 	if (theEAA == "") {
 		form.EAAOutput.value = "";
 	} else {
 		form.EAAOutput.value = "" + Math.round(theEAA*100)/100;
 	}	
 	return true;
}

function clearCBCalc(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.NPVOutput.value = "";	
	form.IRROutput.value = "";
	form.PBOutput.value = "";
	form.EAAOutput.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] = -Math.round(Math.random()*11+7)*100;
	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;
	
  	form.fNR.value="" + gNR;
  	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;
	clearCBCalc(document.CBCalculator);
	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);
	doCBCalc(document.CBCalculator);
}

function scriptSetRate(inRate) {
	document.CBCalculator.NRInput.value = inRate;
}

function scriptSetCellValue(inIndex,inVal,inFreq) {
	if (inIndex == 0) {
		document.CBCalculator.cf0.value = inVal;
	} else if (inIndex == 1) {
		document.CBCalculator.c01.value = inVal;
		document.CBCalculator.f01.value = inFreq;
	} else if (inIndex == 2) {
		document.CBCalculator.c02.value = inVal;
		document.CBCalculator.f02.value = inFreq;
	} else if (inIndex == 3) {
		document.CBCalculator.c03.value = inVal;
		document.CBCalculator.f03.value = inFreq;
	} else if (inIndex == 4) {
		document.CBCalculator.c04.value = inVal;
		document.CBCalculator.f04.value = inFreq;
	} else if (inIndex == 5) {
		document.CBCalculator.c05.value = inVal;
		document.CBCalculator.f05.value = inFreq;
	} else if (inIndex == 6) {
		document.CBCalculator.c06.value = inVal;
		document.CBCalculator.f06.value = inFreq;
	} else if (inIndex == 7) {
		document.CBCalculator.c07.value = inVal;
		document.CBCalculator.f07.value = inFreq;
	} else if (inIndex == 8) {
		document.CBCalculator.c08.value = inVal;
		document.CBCalculator.f08.value = inFreq;
	} else if (inIndex == 9) {
		document.CBCalculator.c09.value = inVal;
		document.CBCalculator.f09.value = inFreq;
	} else if (inIndex == 10) {
		document.CBCalculator.c10.value = inVal;
		document.CBCalculator.f10.value = inFreq;
	}
}
