//*******************************************
//DO NOT REMOVE THIS COPYWRITE INFO!
//College Funding Calculator
//2001 Daniel C. Peterson ALL RIGHTS RESERVED
//Created: 01/17/2001
//Last Modified: 05/27/2003
//This script may not be copied, edited, distributed or reproduced
//without express written permission from
//Daniel C. Peterson of Web Winder Website Services
//For commercial use rates, contact:
//Dan Peterson:
//Web Winder Website Services
//P.O. Box 11
//Bemidji, MN  56619
//dan@webwinder.com
//http://www.webwinder.com
//Commercial User Licence #:668-1052-14-1015
//Commercial Licence Date:2007-04-03
//*******************************************

function stripNum(num) {

var iPercent
var iDollar
var iSpace
var iComma
var numLength = num.length

//lalalla Line #114

if(numLength > 0) {

   num=num.toString();

   iPercent = num.indexOf("%");
   if(iPercent >= 0) {
      num=num.substring(0,iPercent) + "" + num.substring(iPercent + 1,numLength);
      numLength=num.length;
      }
   iDollar = num.indexOf("$");
   if(iDollar >= 0) {
      num=num.substring(0,iDollar) + "" + num.substring(iDollar + 1,numLength);
      numLength=num.length;
      }
   iSpace = num.indexOf(" ");
   if(iSpace >= 0) {
      num=num.substring(0,iSpace) + "" + num.substring(iSpace + 1,numLength);
      numLength=num.length;
      }
   iComma = num.indexOf(",");
   if(iComma >= 0) {
      while(iComma >=1) {
         num=num.substring(0,iComma) + "" + num.substring(iComma + 1,numLength);
         numLength=num.length;
         iComma = num.indexOf(",");
      }
      }

      num = eval(num);


} else {

num = 0;

}

return num;

}




function formatCurrency(num) {

var isNeg=0;

    if(num < 0) {
       num=num*-1;
       isNeg=1;
    }

    
	onum=Math.round(num*100)/100;
		
	integer=Math.floor(onum);

	if (Math.ceil(onum) == integer) {
		decimal="00";
	} else{
		decimal=Math.round((onum-integer)*100)
	}
	decimal=decimal.toString();
	if (decimal.length<2) decimal="0"+decimal;

	integer=integer.toString();
	var tmpnum="";
	var tmpinteger="";
	var y=0;

	for (x=integer.length;x>0;x--) {
		tmpnum=tmpnum+integer.charAt(x-1);
		y=y+1;
		if (y==3 & x>1) {
			tmpnum=tmpnum+",";
			y=0;
		}
	}

	for (x=tmpnum.length;x>0;x--) {
		tmpinteger=tmpinteger+tmpnum.charAt(x-1);
	}
		
	finNum="$"+tmpinteger+"."+decimal;

    if(isNeg == 1) {
       finNum = "-" + finNum;
    }

	return finNum;
}




function formatNumber(num) {

var isNeg=0;

    if(num < 0) {
       num=num*-1;
       isNeg=1;
    }

    
	onum=Math.round(num*100)/100;
		
	integer=Math.floor(onum);

	if (Math.ceil(onum) == integer) {
		decimal="00";
	} else{
		decimal=Math.round((onum-integer)*100)
	}
	decimal=decimal.toString();
	if (decimal.length<2) decimal="0"+decimal;

	integer=integer.toString();
	var tmpnum="";
	var tmpinteger="";
	var y=0;

	for (x=integer.length;x>0;x--) {
		tmpnum=tmpnum+integer.charAt(x-1);
		y=y+1;
		if (y==3 & x>1) {
			tmpnum=tmpnum+",";
			y=0;
		}
	}

	for (x=tmpnum.length;x>0;x--) {
		tmpinteger=tmpinteger+tmpnum.charAt(x-1);
	}
		
	finNum=tmpinteger+"."+decimal;

    if(isNeg == 1) {
       finNum = "-" + finNum;
    }

	return finNum;
}


function computeForm(form) {

//VERIFY REQUIRED ENTRIES

if(form.name.value == "") {
   alert("Please enter student's name in line #1.");
   form.name.focus();
   } else
if(form.yearsTill.selectedIndex == 0) {
   alert("Please select the number of years until your student will be starting college (drop-down menu in line #2).");
   form.yearsTill.focus();
   } else
if(form.annIncome.value == "") {
   alert("Please enter your gross annual income in line #5.");
   form.annIncome.focus();
   } else {
//START VERIFICATION PATH

//SET OPTIONAL BLANK FIELDS EQUAL TO ZERO

var VnowSave = stripNum(form.nowSave.value);
if(VnowSave == "") {
   VnowSave = 0;
   } else {
   VnowSave = VnowSave;
   }

var Voutside = stripNum(form.outside.value);
if(Voutside == "") {
   Voutside = 0;
   } else {
   Voutside = Voutside;
   }

//GET TABLE FACTORS

var Vname = form.studentName.value;

var VyearsTill = 0;
var realRate = 0;
var investFactor = 0;
var SVFactor = 0;

if(form.yearsTill.selectedIndex == 1) {
   VyearsTill = 1;
   SVFactor = 1.08;
   investFactor = 12.53;
   realRate = 1.00;
   } else
if(form.yearsTill.selectedIndex == 2) {
   VyearsTill = 2;
   SVFactor = 1.17;
   investFactor = 26.11;
   realRate = 1.96;
   } else
if(form.yearsTill.selectedIndex == 3) {
   VyearsTill = 3;
   SVFactor = 1.26;
   investFactor = 40.81;
   realRate = 2.89;
   } else
if(form.yearsTill.selectedIndex == 4) {
   VyearsTill = 4;
   SVFactor = 1.36;
   investFactor = 56.73;
   realRate = 3.78;
   } else
if(form.yearsTill.selectedIndex == 5) {
   VyearsTill = 5;
   SVFactor = 1.47;
   investFactor = 73.97;
   realRate = 4.63;
   } else
if(form.yearsTill.selectedIndex == 6) {
   VyearsTill = 6;
   SVFactor = 1.59;
   investFactor = 92.64;
   realRate = 5.45;
   } else
if(form.yearsTill.selectedIndex == 7) {
   VyearsTill = 7;
   SVFactor = 1.71;
   investFactor = 112.86;
   realRate = 6.24;
   } else
if(form.yearsTill.selectedIndex == 8) {
   VyearsTill = 8;
   SVFactor = 1.85;
   investFactor = 134.76;
   realRate = 7.00;
   } else
if(form.yearsTill.selectedIndex == 9) {
   VyearsTill = 9;
   SVFactor = 2.00;
   investFactor = 158.48;
   realRate = 7.73;
   } else
if(form.yearsTill.selectedIndex == 10) {
   VyearsTill = 10;
   SVFactor = 2.16;
   investFactor = 184.17;
   realRate = 8.44;
   } else
if(form.yearsTill.selectedIndex == 11) {
   VyearsTill = 11;
   SVFactor = 2.33;
   investFactor = 211.98;
   realRate = 9.11;
   } else
if(form.yearsTill.selectedIndex == 12) {
   VyearsTill = 12;
   SVFactor = 2.52;
   investFactor = 242.11;
   realRate = 9.76;
   } else
if(form.yearsTill.selectedIndex == 13) {
   VyearsTill = 13;
   SVFactor = 2.72;
   investFactor = 274.74;
   realRate = 10.39;
   } else
if(form.yearsTill.selectedIndex == 14) {
   VyearsTill = 14;
   SVFactor = 2.94;
   investFactor = 310.08;
   realRate = 10.99;
   } else
if(form.yearsTill.selectedIndex == 15) {
   VyearsTill = 15;
   SVFactor = 3.17;
   investFactor = 348.35;
   realRate = 11.56;
   } else
if(form.yearsTill.selectedIndex == 16) {
   VyearsTill = 16;
   SVFactor = 3.43;
   investFactor = 389.79;
   realRate = 12.12;
   } else
if(form.yearsTill.selectedIndex == 17) {
   VyearsTill = 17;
   SVFactor = 3.70;
   investFactor = 434.68;
   realRate = 12.65;
   } else
if(form.yearsTill.selectedIndex == 18) {
   VyearsTill = 18;
   SVFactor = 4.00;
   investFactor = 483.29;
   realRate = 13.17;
   } else {
   SVFactor = 0;
   VyearsTill = 0;
   investFactor = 0;
   realRate = 0;
   }

//GET CURRENT YEAR AND COMPUTE START YEAR

var today = new Date();

var nowYear = today.getYear();

//ADDED
if(nowYear < 1900) {
nowYear = eval(nowYear) + eval(1900);
}

var VstartYear = eval(nowYear) + eval(VyearsTill);

//GET YEARS SINCE 2006 & ADD TO YEARS TILL START

var sinceBase = (eval(nowYear) - eval(2006)) + eval(VyearsTill);

//GET STUDENT BASE YEAR COST (2006)


var studentType = "";
var collegeType = "";
var tuition = 0;
var booksSupplies = 0;
var roomBoardCost = 0;
var transport = 0;
var otherExp = 0;
var totalAnnualExp = 0;

//IF PUBLIC, DO FOLLOWING
if(form.HcollegeType.value == 1) {

collegeType = "public";

if(form.Hlocal.value == 1) {
tuition = 5491; //2000 - 3510, 2003 - 4081
} else {
tuition = 13164; //2000 - 9020, 2003 - 10428
}

booksSupplies = 894; //2000 - 786, 2003 - 786

if(form.HstudentType.value == 1 || (form.HstudentType.value == 2 && form.Hlocal.value == 2)) {
   roomBoardCost = 6636; //2000 - 4960, 2003 - 5582
   studentType = "resident";
   transport = 852; //2000 - 643, 2003 - 749
   otherExp = 1693; //2000 - 1521, 2003 - 1643
   } else {
   roomBoardCost = 6476; //2000 - 2444, 2003 - 5730
   studentType = "commuter";
   transport = 1168; //2000 - 1014, 2003 - 1013
   otherExp = 1962; //2000 - 1557, 2003 - 1853
}

totalAnnualExp = eval(tuition) + eval(booksSupplies) + eval(roomBoardCost) + eval(transport) + eval(otherExp);

} else {
//ELSE IF PRIVATE, DO FOLLOWING

collegeType = "private";

tuition = 21235; //2000 - 16332, 2003 - 18273

booksSupplies = 904; //2000 - 730, 2003 - 807

if(form.HstudentType.value == 1) {
   roomBoardCost = 7791; //2000 - 6209, 2003 - 6779
   studentType = "resident";
   transport = 691; //2000 - 573, 2003 - 645
   otherExp = 1295; //2000 - 1102, 2003 - 1173
   } else {
   roomBoardCost = 7249; //2000 - 2495, 2003 - 6239
   studentType = "commuter";
   transport = 1060; //2000 - 926, 2003 - 957
   otherExp = 1622; //2000 - 1221, 2003 - 1419
}

totalAnnualExp = eval(tuition) + eval(booksSupplies) + eval(roomBoardCost) + eval(transport) + eval(otherExp);

} //END IF ESLE STATEMENT THAT DETERMINES WHETHER PUBLIC OR PRIVATE

var collegeLocal = "";

if(form.Hlocal.value == 1) {
   collegeLocal = "in-state";
   } else {
   collegeLocal = "out-of-state";
   }

//GET INFLATION RATE
VinflateRate = 0;
var VinflateRateDisplay = eval(form.inflateRate.selectedIndex) + eval(1);
VinflateRate = VinflateRateDisplay / 100;
VinflateRate = eval(VinflateRate) + eval(1);

var baseCost = totalAnnualExp;
var accumBase = 0;

//INFLATE COLLEGE BASE YEAR FOR 4 YEARS
var count4 = 0;

while(count4 < 4) {
  baseCost = baseCost * VinflateRate;
  accumBase = eval(accumBase) + eval(baseCost);
  count4 = count4 + 1;
  }

var testBase = totalAnnualExp;

//INFLATE 2006 COSTS BY 6% PER YEAR

var count = 0;

while(count < sinceBase) {
  accumBase = accumBase * VinflateRate;
  count = count + 1;
  }

var VfutureCost = accumBase;

form.futureCost.value =  formatCurrency(VfutureCost);
//form.futureCost.value =  formatCurrency(testBase);

form.startYear.value = VstartYear;

//CALCULATE FUTURE VALUE OF PRESENT SAVINGS

VsaveFV = VnowSave * SVFactor;

form.saveFV.value = formatCurrency(VsaveFV);

//ADD OUTSIDE FUNDING TO FUTURE VALUE OF SAVINGS

var VtotFunds = eval(VsaveFV) + eval(Voutside);

form.totFunds.value = formatCurrency(VtotFunds);

//CALCULATE SAVINGS GAP

var VsaveGap = eval(VfutureCost) - eval(VtotFunds);

form.saveGap.value = formatCurrency(VsaveGap);

//CALCULATE MONTHLY SAVINGS REQUIRED

var VmoSave = VsaveGap / investFactor;

form.moSave.value = formatCurrency(VmoSave);

//CALCULATE PERCENT OF INCOME

var VannIncome = stripNum(form.annIncome.value);

var presValue = VsaveGap / SVFactor;

var initInvest = presValue / realRate;

var VpercentIncome = initInvest / VannIncome * 100;

form.percentIncome.value = formatNumber(VpercentIncome) + "%";

form.summary.value = ("Assuming a " + (form.inflateRate.selectedIndex + 1 ) + "% annual increase in total college tuition costs and an 8% annual return on investment, by the year " + form.startYear.value + ", a four-year, " + collegeType + ", " + collegeLocal + " degree for " + Vname + " will cost " + form.futureCost.value + ". Considering the future value of your present savings and expected funding from outside sources, you will still need to come up with " + form.saveGap.value + " between now and September of " + form.startYear.value + ".  To do so you will need to deposit " + form.moSave.value + " to " + Vname + "'s college fund each month from now until " + Vname + " starts college.  Assuming your income keeps pace with inflation, this means you'll need to invest " + form.percentIncome.value + " of your gross annual income.");

//END VERIFICATION PATH
   }
    
}

function help0(form) {
form.help.value = "";
form.nowSave.focus();
form.help.value = "";
//form.help.value = ("Instructions: To see a more detailed instruction/explanation of //any text-entry field, simply click in the desired text field and the //instructions/explanations will appear in this text area.");
}

function help00(form) {
form.help.value = "";
form.helpB.value = "";
form.futureCost.focus();
//form.help.value = ("Instructions: To see a more detailed instruction/explanation of //any text-entry field, simply click in the desired text field and the //instructions/explanations will appear in this text area.");
}

function help1(form) {
form.helpB.value = "";
form.help.value = ("Line #1: ENTER: The name of the student you are making college plans for.");
}

function help2(form) {
var Vname = form.studentName.value;
form.helpB.value = "";
form.help.value = ("Line #2: ENTER: Select the number of years until " + Vname + " starts college.");
}

function help3(form) {
var Vname = form.studentName.value;
form.helpB.value = "";
form.help.value = ("Line #2: SELECT: Number of years.\r\rLine #3: ENTER: The amount you currently have set aside to fund " + Vname + "'s future college education.");
}

function help4(form) {
form.helpB.value = "";
form.help.value = ("Line #4: ENTER: The amount you expect to get from outside sources (grants, gifts, financial aid, etc.).");
}

function help5(form) {
form.helpB.value = "";
form.help.value = ("Line #5: ENTER: Your annual gross income (before taxes and deductions).");
}

function help5A(form) {
form.helpB.value = "";
form.help.value = ("Line #6: ENTER: Your annual gross income (before taxes and deductions).");
}

function help6A(form) {
form.helpB.value = "";
form.help.value = ("Line #7: ENTER: Select resident or commuter.");
}

function help6(form) {
form.helpB.value = "";
form.help.value = ("Line #8: ENTER: Select private or public.");
}

function help7(form) {
form.helpB.value = "";
form.help.value = ("Line #9: ENTER: Select in-state or out-of-state.");
}

function help8(form) {
var Vname = form.studentName.value;
form.help.value = "";
form.helpB.value = ("Line #10: RESULT: This is the year " + Vname + " will be starting college.");
}

function help9(form) {
var Vname = form.studentName.value;
form.help.value = "";
var VinflateRateDisplay = eval(form.inflateRate.selectedIndex) + eval(1);
form.helpB.value = ("Line #11: RESULT: Using a " + VinflateRateDisplay + "% annual inflation rate this is how much you will need to have saved by the time " + Vname + " starts college (" + form.startYear.value + ").");
}

function help10(form) {
form.help.value = "";
form.helpB.value = ("Line #12: RESULT: This is how much your present savings will grow to, assuming an 8% annual growth rate.");
}

function help11(form) {
form.help.value = "";
form.helpB.value = ("Line #13: RESULT: This is the combined total of your present saving's future value and the funding from outside sources.");
}

function help12(form) {
form.help.value = "";
form.helpB.value = ("Line #14: RESULT: This amount is the difference between how much you have available and how much you will need, i.e., the amount you still need to come up with.");
}

function help13(form) {
var Vname = form.studentName.value;
form.help.value = "";
form.helpB.value = ("Line #15: RESULT: Assuming an 8% annual growth rate, this is how much you will need to deposit to " + Vname + "'s college fund each month, between now and September of " + form.startYear.value + ".");
}

function help14(form) {
var Vname = form.studentName.value;
form.help.value = "";
form.helpB.value = ("Line #16: RESULT: Assuming your income keeps pace with inflation, this is the percentage of your gross income you will need to invest annually until " + Vname + " enters college.");
}

function changeStudentType1(form) {
form.HstudentType.value = 1;
computeForm(form);
}

function changeStudentType2(form) {
form.HstudentType.value = 2;
computeForm(form);
}

function changeCollegeType1(form) {
form.HcollegeType.value = 1;
computeForm(form);
}

function changeCollegeType2(form) {
form.HcollegeType.value = 2;
computeForm(form);
}

function changeLocal1(form) {
form.Hlocal.value = 1;
computeForm(form);
}

function changeLocal2(form) {
form.Hlocal.value = 2;
computeForm(form);
}
