


// base figures for school cost
var baseCost = new Array();
baseCost[0] = 11500.00;
baseCost[1] = 36000.00; 
baseCost[2] = 25000.00;
var costPerYear = new Array();
costPerYear[0] = 0;
costPerYear[1] = 0;
costPerYear[2] = 0;
costPerYear[3] = 0;

function getPercent(obj)
{	// performs data validation and returns float value
  // parameter must be a form element with a 'value' attribute
  // containing a parseFloat()-able piece of data, followed by a percent sign
  var val = obj.value+"";
	var psign = val.indexOf("%", 0);
	if(psign > -1)
	{	var val = val.substring(0, psign);
	} else
	{	alert("This value has been incorrectly entered:\n\n-->"+val+"\n\nInput should be a percentage.");
    return 0.0;
	}
	if(parseFloat(val))
	{	return parseFloat(val)/100;
  } else
	{	alert("This value has been incorrectly entered:\n\n-->"+val+"\n\nInput should be a percentage.");
		return 0.00;
	}
}  

function getNumber(obj) {
// performs data validation and returns float value
// parameter must be a form element with a 'value' attribute
// containing a parseFloat()-able piece of data, eg: 1.5, or empty
  var numString1 = obj.value+"";
  var num = 0;
  var comma = numString1.indexOf(",", 0);
  if (!parseFloat(numString1) || comma > -1){
		if(numString1 == "" || numString1 == " ")
    { 
    }
    else if (comma > -1)
    { alert("This value has been incorrectly entered:\n\n-->"+numString1);
    } else
    { alert("This value has been incorrectly entered:\n\n-->"+numString1);
    }
  }else
	{ num = parseFloat(numString1);
  }
	return num; 
}

function makeDollars(number) 
{ //formats number to two decimal places
  var rounded = Math.round(number);
  var dollars = rounded+".00";
  return dollars;
}

function getFV(rate, nper, pmt, pv, type)
{	var rPowN = Math.pow((1.0 + rate), nper);
	var fv = pv * rPowN + (pmt*(1+rate*type)*((rPowN - 1)/rate)); 
    return fv;
}

// compares the available funds during each year of college
// to the payment that must be made -- returns false if any 
// payment exceeds the available funds.
function compareFundsToCost(rate, nper, pmt, pv, type, percent)
{	
	if (costPerYear[0] <= 0)
	{	alert("You need to repeat the first step, \"Estimating the Projected\nCost of College\" before proceeding to this step.\nOtherwise, the monthly contribution value will be too low.");
		return true;
	}	
	var funds = getFV(rate/12, (nper)*12, pmt, pv, type);
	for(loop=0;loop < costPerYear.length;loop++)
	{	
		funds -= costPerYear[loop]*percent;
		if (funds < 0)
		{ return false;
		} else 
		{ var tempFunds = getFV(rate/12, 12, pmt, funds, type);	
		  funds = tempFunds;
	    }
	}
	return true;
}

function getPMT(pv, fv, rate, nper)
{ var rPowN = Math.pow((1.0 + rate), nper);
  var numerator = fv - (pv*rPowN); 
  var denominator = (1.0 + rate)*((rPowN - 1.0)/rate);
  return numerator/denominator;
}

function getTotalPC()
{ var bcindex, yrindex, inflate, future;
	with (document.theform)
	{   bcindex = s1.selectedIndex;
		yrindex = s2.selectedIndex + 1;
        inflate = getPercent(t6);
		future = 0.0;
		if (inflate < 1.0)
		{   for(loop=0;loop<4;loop++)
			{	costPerYear[loop] = getFV(inflate, yrindex+loop, 0, baseCost[bcindex], 0);
    	    	future += costPerYear[loop];
			}
		} else alert("College-cost inflation must be a percentage less than 100%\n\neg., 7% inflation");	 
		t1.value = makeDollars(future);
	}
	return true;
}


function getTargetAmt()
{ with (document.theform)
	{	var amt = getNumber(t1);
		var pct = parseFloat(s3.options[s3.selectedIndex].value);
	    t3.value = makeDollars(amt*pct);
	}
	return true;	
}

function getEstReturn()
{ 	with(document.theform)
	{	var rate = parseFloat(s4.options[s4.selectedIndex].value);
		t4.value = (Math.round(rate*100))+"%";
	}
	return true;
}


// estimate monthly contributions if period of investment is until 4th year of college
// then, use compareFundsToCost() to test monthly contrib. values until just high 
// enough so that all tuition can be paid 

function getMonthly()
{ 	var pv, fv, rate, nper, pmt, percent;
  	with(document.theform)
	{	pv = getNumber(t2);
	    fv = getNumber(t3);
		rate = getPercent(t4);
		if (rate < 1.0)
		{	nper = s2.selectedIndex;
			pmt = getPMT(pv, fv, rate/12, (nper+5)*12);
			percent = parseFloat(s3.options[s3.selectedIndex].value);
			var tooLow = true;
			while(tooLow)
			{	
				if (compareFundsToCost(rate, nper+1, pmt, pv, 1, percent))
					tooLow = false;
				else 
					pmt++;
			}
			if (pmt > 0) 
				t5.value = makeDollars(pmt);
			else 
				t5.value = "0.00";
			           
		}
    	else alert("Rate of return must be a percentage less than 100%\n\neg., 12% return"); 
	}
	return true;
}

function goTo(aSelect)
{	if(aSelect.selectedIndex != 0)
  {	top.document.location.href=aSelect.options[aSelect.selectedIndex].value;
  }
	return true;
}		

function popPage(some_url)
{ var details="width=300,height=250,top=0,left=0,titlebar=yes,menubar=yes,st atusbar=yes,toolbar=yes,scrollbars=yes";
	window.open(some_url, "pop_win", details); return false;
}

