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