function calcS1(inS, inG) {
	var outS1 = inS*(1 + inG/100);
 	return outS1;
}

function calcSFC(inS, inCAP) {
	var outSFC = inS/(inCAP/100);
 	return outSFC;
}

function calcEFN(theS,theNI,theARE,theTCA,theNFA,theAP,theS1,theSFC) {
	theS = parseFloat(theS);
	theNI = parseFloat(theNI);
	theARE = parseFloat(theARE);
	theTCA = parseFloat(theTCA);
	theNFA = parseFloat(theNFA);
	theAP = parseFloat(theAP);
	theS1 = parseFloat(theS1);
	theSFC = parseFloat(theSFC);
	var outEFN = 0;
	if (theS1 < theSFC) {
		outEFN = ((theTCA + theNFA - theAP)/theS)*(theS1 - theS) - (theNI/theS)*(theS1)*(theARE/theNI);
	} else {
		outEFN = ((theTCA - theAP)/theS)*(theSFC - theS) - (theNI/theS)*(theSFC)*(theARE/theNI);
		outEFN += ((theTCA - theAP)/theS)*(theS1 - theSFC) + ((theNFA)/theSFC)*(theS1 - theSFC) - (theNI/theS)*(theS1-theSFC)*(theARE/theNI);
	}
	return outEFN;
}

function clearEFN(form) {
	form.SInput.value = "";
	form.NIInput.value = "";
	form.AddREInput.value = "";
	form.TCAInput.value = "";
	form.NFAInput.value = "";
	form.APInput.value = "";
	form.GInput.value = "";
	form.CAPInput.value = "";
	form.S1Output.value = "";
	form.SFCOutput.value = "";
	form.EFNOutput.value = "";
}

function findEFN(form) {
 	var theS = form.SInput.value;
 	if (!isPositiveNumber(theS)) {
 		return false;
 	}
 	if (theS == "") theS = 0;
 	var theNI = form.NIInput.value;
 	if (!isNumber(theNI)) {
 		return false;
 	}
 	if (theNI == "") theNI = 0;
 	var theARE = form.AddREInput.value;
 	if (!isPositiveNumber(theARE)) {
 		return false;
 	}
 	if (theARE == "") theARE = 0;
 	var theTCA = form.TCAInput.value;
 	if (!isPositiveNumber(theTCA)) {
 		return false;
 	}
 	if (theTCA == "") theTCA = 0;
 	var theNFA = form.NFAInput.value;
 	if (!isPositiveNumber(theNFA)) {
 		return false;
 	}
 	if (theNFA == "") theNFA = 0;
 	var theAP = form.APInput.value;
 	if (!isPositiveNumber(theAP)) {
 		return false;
 	}
 	if (theAP == "") theAP = 0;
 	var theG = form.GInput.value;
 	if (!isNumber(theG)) {
 		return false;
 	}
 	if (theG == "") theG = 0;
 	var theCAP = form.CAPInput.value;
 	if (!isPositiveNumber(theCAP)) {
 		return false;
 	}
 	if (theCAP == "") {
 		alert("The \% of Capacity must be greater than 0\%");
 		return false;
 	}
 	if (parseFloat(theS) < parseFloat(theNI)) {
 		alert("The Sales must be greater than Net Income." + theS + " " + theNI);
 		return false;
 	}
 	if (parseFloat(theARE) > parseFloat(theNI)) {
 		alert("The Net Income must be greater than the Additions to RE.");
 		return false;
 	}
 	var theS1 = calcS1(theS, theG);
 	var theSFC = calcSFC(theS, theCAP);
 	var theEFN = calcEFN(theS,theNI,theARE,theTCA,theNFA,theAP,theS1,theSFC);
 	form.S1Output.value = "" + Math.round(theS1*100)/100;
 	form.SFCOutput.value = "" + Math.round(theSFC*100)/100;
 	form.EFNOutput.value = "" + Math.round(theEFN*100)/100;
 	return true;
}
 
 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 launchTVMCalc() {
    window.open("EFNCalcWindow.html","Win2","menubar,resizable,height=300,width=610");
}

