// Convert Numbers
function formatNum(Vnum) {

   if(Vnum > 99000000) {
   alert("Sorry, this calculator will not generate numbers larger that 99 million.");
   focus();
   } else {

   var V10million = parseInt(Vnum / 10000000);

   var Vcents = 0;

// Round number 
	Vnum = Math.round(Vnum*1)
	  
   	var V1million = (Vnum % 10000000)  / 1000000;
      if(V1million / 1000000 == 1) {
      V1million = 1;
      } else
      if(V1million < 1) {
      V1million = "0";
     } else {
      V1million = parseInt(V1million,10);
     }

    var V100thousand = (Vnum % 1000000)  / 100000;
      if(V100thousand / 100000 == 1) {
      V100thousand = 1;
      } else
      if(V100thousand < 1) {
      V100thousand = "0";
     } else {
      V100thousand = parseInt(V100thousand,10);
     }

   var V10thousand = (Vnum % 100000)  / 10000;
      if(V10thousand / 10000 == 1) {
      V10thousand = 1;
      } else
      if(V10thousand < 1) {
      V10thousand = "0";
      } else {
      V10thousand = parseInt(V10thousand,10);
      }

   var V1thousand = (Vnum % 10000)  / 1000;
      if(V1thousand / 1000 == 1) {
      V1thousand = 1;
      } else
      if(V1thousand < 1) {
      V1thousand = "0";
     } else {
      V1thousand = parseInt(V1thousand,10);
     }

   var Vhundreds = (Vnum % 1000)  / 100;
      if(Vhundreds / 100 == 1) {
      Vhundreds = 1;
      } else
      if(Vhundreds < 1) {
      Vhundreds = "0";
     } else {
      Vhundreds = parseInt(Vhundreds,10);
     }

   var Vtens = (Vnum % 100)  / 10;
      if(Vtens / 10 == 1) {
      Vtens = 1;
      } else
      if(Vtens < 1) {
      Vtens = "0";
     } else {
      Vtens = parseInt(Vtens,10);
     }

   var Vones = (Vnum % 10)  / 1;
      if(Vones / 1 == 1) {
      Vones = 1;
      } else
      if(Vones < 1) {
      Vones = "0";
     } else {
      Vones = parseInt(Vones,10);
     }

   var Vformat = "";

   if(Vnum >= 10000000) {
   Vformat = (V10million + "" + V1million + "," + V100thousand + "" + V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones);
   }
   else
   if(Vnum >= 1000000) {
   Vformat = (V1million + "," + V100thousand + "" + V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones);
   }
   else
   if(Vnum >= 100000) {
   Vformat = (V100thousand + "" + V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones);
   }
   else
   if(Vnum >= 10000) {
   Vformat = (V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones);
   }
   else
   if(Vnum >= 1000) {
   Vformat = (V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones);
   }
   else
   if(Vnum >= 100) {
   Vformat = (Vhundreds + "" + Vtens + "" + Vones);
   }
   else
   if(Vnum >= 10) {
   Vformat = (Vtens + "" + Vones);
   }
   else
   if(Vnum >= 1) {
   Vformat = (Vones);
   } else {
   Vformat = ("0." + Vcents);
   }

  return Vformat;

  }
}


// Calculate Submitted form
function computeForm(form)  {
// Verify all required fields were entereed in correct format
	if (document.getElementById('InterestSavings').value == "") {
		alert("Please enter Savings Annual Rate of Returrn")
		document.getElementById('InterestSavings').focus()
		return false
	}
	if (document.getElementById('InterestLoan').value == ""){
		alert("Please enter Loan Interest Rate.")
		document.getElementById('InterestLoan').focus()
		return false
	}
	if (document.getElementById('ChildAge').value == ""){
		alert("Please enter Child's Age.")
		document.getElementById('ChildAge').focus()
		return false
	}
	else if (isNaN(document.getElementById('ChildAge').value)) {
		alert("Invalid Child's age.  Please enter a number between 1 and 18.")
		document.getElementById('ChildAge').focus()
		return false
	}
	else if (document.getElementById('ChildAge').value < 1) {
		alert("Child's age has to be at least 1.")
		document.getElementById('ChildAge').focus()
		return false
	}
	else if (document.getElementById('ChildAge').value > 18) {
		alert("Child's age has to be less than 18.")
		document.getElementById('ChildAge').focus()
		return false
	}
	if (document.getElementById('LengthSavings').value == ""){
		alert("Please enter Years Till College")
		document.getElementById('LengthSavings').focus()
		return false
	}
	if (isNaN(document.getElementById('InterestSavings').value)) {
		alert("Invalid Annual Rate of Return.")
		document.getElementById('InterestSavings').focus()
		return false
	}

	if (isNaN(document.getElementById('InterestLoan').value)) {
		alert("Invalid Loan Interest Rate.")
		document.getElementById('InterestLoan').focus()
		return false
	}
//verify that school type was chosen
	SchoolType = -1
	
		if (document.getElementById('SchoolType').value=="Public" || document.getElementById('SchoolType').value=="Private")
			{
			SchoolType = 1
			}
	if (SchoolType == -1) {
		//alert("Please choose School Type")
		//return false
	}
	
	var LengthSavings = document.getElementById('LengthSavings').value;

// Estimated Collage cost beginning with year 2003 - 2004
/////////// Edit These Three variables to uadate college cost///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//Start Year for both Public and Private College cost - 4 numbers
	var StartYear = 2003
	
	// Cost of 4 year Public College education beginning with StartYear, separated by ','
	var PublicCollegeCost = new Array (59622, 62603, 65733, 69020, 72471, 76094, 79899, 83894, 88089, 92493, 97118, 101974, 107072, 112426, 118047, 123950, 130147, 136655, 143487, 150662, 158195);

	// Cost of 4 year Private College education beginning with StartYear, separated by ','
	var PrivateCollegeCost = new Array (127325, 133692, 140376, 147395, 154765, 162503, 170628, 179160, 188118, 197523, 207400, 217770, 228658, 240091, 252096, 264700, 277935, 291832, 306424, 321745, 337832);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	var CurrentDate = new Date();
	var CurrentYear = CurrentDate.getFullYear();
	
	var CollegeEntryYear = eval(CurrentYear) + eval(LengthSavings);
	var YearDifference  = eval(CollegeEntryYear) - eval(StartYear);
	var CollegeCost = 0

// Set Collage Cost based on School Type selected 0 = Public, 1 = Private


	if (document.getElementById('SchoolType').value=="Public") {
		CollegeCost = eval(PublicCollegeCost[YearDifference]);
	}
	else {
		CollegeCost = eval(PrivateCollegeCost[YearDifference]);
	}

	document.getElementById('EducationCost').value = "$" + formatNum(CollegeCost)
	
	var VsaveBal = 0;
    var iSavings = document.getElementById('InterestSavings').value;
    var iLoan = document.getElementById('InterestLoan').value;

// Convert interest rate if whole number is entered.
    if (iSavings > 1.0) {iSavings = document.getElementById('InterestSavings').value / 100} else {iSavings = document.getElementById('InterestSavings').value};
    if (iLoan > 1.0) {iLoan = document.getElementById('InterestLoan').value / 100} else {iLoan = document.getElementById('InterestLoan').value};

// Calculate Savings
   
   var LengthLoan = document.getElementById('LengthLoan').value;
   var count1 = 0;

   iSavings = iSavings / 12;

   var MonthsSavings = LengthSavings * 12;

   var factor1 = eval(1) + eval(iSavings);

   var denom1 = 1;
    
    while(count1 < MonthsSavings) {
       denom1 = denom1 * factor1;
        count1 = eval(count1) + eval(1);
        }

    var MonthlyContribution = eval(denom1) - eval(1);

    MonthlyContribution = iSavings / MonthlyContribution;
    MonthlyContribution = MonthlyContribution * CollegeCost;
	

    document.getElementById('MonthlyContribution').value = "$" + formatNum(MonthlyContribution);
	
	var TotalDeposits = MonthlyContribution * MonthsSavings
	document.getElementById('DepositTotal').value = "$" + formatNum(TotalDeposits);
	
	var TotalInterestEarned = CollegeCost - TotalDeposits
	document.getElementById('InterestEarned').value = "$" + formatNum(TotalInterestEarned);

// Calculate Loan

	var MonthsLoan = LengthLoan * 12;
   	iLoan = iLoan / 12;
	divisor = 1;
	var factor = 1;

	for (var j = 0; j < MonthsLoan; j++) {
		factor = factor * (eval(1) + eval(iLoan));
		}

	var periodPmt = (CollegeCost * factor * iLoan) / (eval(factor) - eval(1));
	var MonthlyPayment = periodPmt / divisor;

	document.getElementById('MonthlyPayment').value = "$" + formatNum(MonthlyPayment);

	var TotalPayments = MonthlyPayment * MonthsLoan
	document.getElementById('PaymentTotal').value = "$" + formatNum(TotalPayments);
	
	var TotalInterestPaid = TotalPayments - CollegeCost
	document.getElementById('InterestPaid').value = "$" + formatNum(TotalInterestPaid);
	
// Calculate Difference
	var Difference = TotalPayments - TotalDeposits;
	document.getElementById('DifferenceTotal').value = "$" + formatNum(Difference);
	
	}
	
function clearForm(form)

	{

    document.getElementById('ChildAge').value = "";
    document.getElementById('InterestSavings').value = "";
    document.getElementById('InterestLoan').value = "";
    document.getElementById('LengthLoan').value = "";

}

function updateLength(form)

{
	if (document.getElementById('ChildAge').value < 1) {
		alert("Child's age has to be at least 1.")
		document.getElementById('ChildAge').focus()
		return false
	}
	else if (document.getElementById('ChildAge').value > 17) {
		alert("Child's age has to be less than 18.")
		document.getElementById('ChildAge').focus()
		return false
	}
	else {
		document.getElementById('LengthSavings').value = 18 - document.getElementById('ChildAge').value;
	}
}

function clearAge(form)

{
	if (isNaN(document.getElementById('LengthSavings').value)) {
		alert("Invalid Number of Years Till College.  Please enter a number between 1 and 17.")
		document.getElementById('LengthSavings').focus()
		return false
	}
	else if (document.getElementById('LengthSavings').value < 1){
		alert("Invalid Number of Years Till College.  Please enter a number between 1 and 17.")
		document.getElementById('LengthSavings').focus()
		return false
	}
	else if (document.getElementById('LengthSavings').value > 17){
		alert("Invalid Number of Years Till College.  Please enter a number between 1 and 17.")
		document.getElementById('LengthSavings').focus()
		return false
	}
	else {
	}
}

function verifyLoanLength(form) 

{
	if (isNaN(document.getElementById('LengthLoan').value)) {
		alert("Invalid Loan Length. Please enter a number between 1 and 100.")
		document.getElementById('LengthLoan').focus()
		return false
	}
	else if (document.getElementById('LengthLoan').value < 1){
		alert("Invalid Loan Length. Please enter a number between 1 and 100.")
		document.getElementById('LengthLoan').focus()
		return false
	}
	else if (document.getElementById('LengthLoan').value > 100){
		alert("Invalid Loan Length. Please enter a number between 1 and 100.")
		document.getElementById('LengthLoan').focus()
		return false
	}
	else {
	}
}

function verifyInterestSavings(form) 

{
	if (isNaN(document.getElementById('InterestSavings').value)) {
		alert("Invalid Annual Rate of Return.")
		document.getElementById('InterestSavings').focus()
		return false
	}
}

function verifyInterestLoan(form) 

{
	if (isNaN(document.getElementById('InterestLoan').value)) {
		alert("Invalid Loan Interest Rate.")
		document.getElementById('InterestLoan').focus()
		return false
	}
}
