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;
 }
 
function launchCBCalc() {
    window.open("CBCalcWindow.html","Win1","menubar,resizable,height=320,width=334");
}

