addJavascript = function(fileName) {
	var th = document.getElementsByTagName('head')[0];
	var s = document.createElement('script');
	s.setAttribute('type','text/javascript');
	s.setAttribute('src',fileName);
	th.appendChild(s);
}
addStylesheet = function(fileName) {
	var th = document.getElementsByTagName('head')[0];
	var s = document.createElement('link');
	s.setAttribute('type','text/css');
	s.setAttribute('rel','stylesheet');
	s.setAttribute('href',fileName);
	th.appendChild(s);
}
college_calculate = function()
{
	var savings = document.getElementById("college_savings").value;
	if ( savings.length == 0 )
	{
		 alert("Please enter your current savings");
		 return;
	}
	savings = fixNumber(savings);
	savings = parseFloat(savings);
	if ( isNaN(savings) )
	{
		 alert("Please enter your current savings as a number");
		 return;
	}

	var deposit = document.getElementById("college_deposit").value;
	if (  deposit.length == 0 )
	{
		 alert("Please enter your yearly contributions");
		 return;
	}
	deposit  = fixNumber(deposit);
	deposit  = parseFloat(deposit);
	if ( isNaN(deposit) )
	{
		 alert("Please enter your yearly contributions as a number");
		 return;
	}

	var cost = document.getElementById("college_cost").value;
	if (  cost.length == 0 )
	{
		 alert("Please enter the current college cost");
		 return;
	}
	cost  = fixNumber(cost);
	cost  = parseFloat(cost);
	if ( isNaN(cost) )
	{
		 alert("Please enter the current college cost as a number");
		 return;
	}
	if ( cost < 0 )
	{
		 alert("Please enter a current college cost >= 0");
		 return;
	}


	var rate = document.getElementById("college_rate").value;
	if (  rate.length == 0 )
	{
		 alert("Please enter an investment rate of return");
		 return;
	}
	rate  = fixNumber(rate);
	rate  = parseFloat(rate);
	if ( isNaN(rate) )
	{
		 alert("Please enter the investment rate of return as a number");
		 return;
	}
	if ( rate < 0 )
	{
		 alert("Please enter an investment rate of return >= 0");
		 return;
	}
	rate = 1 + rate/100;


	var inflation = document.getElementById("college_inflation").value;
	if (  inflation.length == 0 )
	{
		 alert("Please enter an educational inflation rate");
		 return;
	}
	inflation  = fixNumber(inflation);
	inflation  = parseFloat(inflation);
	if ( isNaN(inflation) )
	{
		 alert("Please enter an educational inflation rate");
		 return;
	}
	if ( inflation < 0 )
	{
		 alert("Please enter an educational inflation rate >= 0");
		 return;
	}
	inflation = 1 + inflation/100;


	var children = new Array(4);
	children[0] = parseInt( document.getElementById("college_child1").value);
	children[1] = parseInt( document.getElementById("college_child2").value);
	children[2] = parseInt( document.getElementById("college_child3").value);
	children[3] = parseInt( document.getElementById("college_child4").value);
	

	var res = "<table cellpadding='0' cellspacing='0' class=\"data\">";
	
	res += "<tr><th>Year<th>Starting<br>Savings<th>Eduction<br>Expenses<br>Child1<th>Eduction<br>Expenses<br>Child2<th>Eduction<br>Expenses<br>Child3<th>Eduction<br>Expenses<br>Child4<th>Interest<br>Earned<th>Yearly<br>Contribution<th>Ending<br>Savings";

	var done = false;
	
	var money = savings;
	var currentCost = cost;
	var y = 0;
	var i;

	while ( !done )
	{
		res += "<tr><td>" + (y+1) + "<td>" + formatDollars(money);

		for (i=0; i<4; i++)
		{
			if ( children[i]+y >= 18 && children[i]+y <= 21 && children[i] != -1 )
			{
				res += "<td>" + formatDollars(currentCost);
				money = money - currentCost;
			}
			else res += "<td>$0";
		}		

		y++;
		done = true;
		for (i=0; i<4; i++)
		{
			if ( children[i] == -1 )
				continue;
			if ( children[i] + y <= 21 )
			{	
				done = false;
				break;
			}
		}

		currentCost *= inflation;
		if ( money > 0 )
		{
			res += "<td>" + formatDollars(money*rate-money);
			money = (money*rate);
		}
		else
			res += "<td>$0";

		money += deposit;
		res += "<td>" + formatDollars(deposit) + "<td>" + formatDollars(money);

	}

	res += "</table>";

	if ( money < 0 )
		res = "Unfortunately, this level of savings will not pay all of your childrens' college costs.<p>" + res;
	else
		res = "Congratulations, at this level of savings you will be able to pay all of your childrens' college costs.<p>" + res;
	
	var x = document.getElementById("college_dt_results");
	x.innerHTML = res;
	x.style.display = "block";

}
// RUN SCRIPT

addJavascript('calcutil.js');
addStylesheet('calc.css');

var out = '\
<div id="college_dt_calculator" class="dt_calculator" style="width: 520px">\n\
	<p class="instructions">This page will calculate if you are saving enough to pay for all of your childrens&#39; college education.</p>\n\
	<p class="instructions"><strong>*</strong>In 2006-2007, the average tuition plus room and board of private colleges was $30,367. Costs at four-year public colleges averaged $12,796.</p>\n\
	<div class="a">Current College Savings</div>\n\
	<div class="b">$</div>\n\
	<div class="c"><input id="college_savings" type="text" value="0" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Yearly Contribution</div>\n\
	<div class="b">$</div>\n\
	<div class="c"><input id="college_deposit" type="text" value="0" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Current College Costs <strong>*</strong></div>\n\
	<div class="b">$</div>\n\
	<div class="c"><input id="college_cost" type="text" value="30,367" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Child 1 Age</div>\n\
	<div class="b"></div>\n\
	<div class="c">\n\
		<select id="college_child1">          <option value="0">0 Years          <option value="1">1 Years          <option value="2">2 Years          <option value="3">3 Years          <option value="4">4 Years          <option value="5">Kindergarten          <option value="6">1st Grade          <option selected="selected" value="7">2nd Grade          <option value="8">3rd Grade          <option value="9">4th Grade          <option value="10">5th Grade          <option value="11">6th Grade          <option value="12">7th Grade          <option value="13">8th Grade          <option value="14">9th Grade          <option value="15">10th Grade          <option value="16">11th Grade          <option value="17">12th Grade          <option value="18">College Freshman          <option value="19">College Sophomore          <option value="20">College Junior          <option value="21">College Senior</select>\n\
	</div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Child 2 Age</div>\n\
	<div class="b"></div>\n\
	<div class="c">\n\
		<select id="college_child2">          <option value="-1">N/A	<option value="0">0 Years          <option value="1">1 Years          <option value="2">2 Years          <option value="3">3 Years          <option value="4">4 Years          <option value="5">Kindergarten          <option value="6">1st Grade          <option value="7" SELECTED>2nd Grade          <option value="8">3rd Grade          <option selected="selected" value="9">4th Grade          <option value="10">5th Grade          <option value="11">6th Grade          <option value="12">7th Grade          <option value="13">8th Grade          <option value="14">9th Grade          <option value="15">10th Grade          <option value="16">11th Grade          <option value="17">12th Grade          <option value="18">College Freshman          <option value="19">College Sophomore          <option value="20">College Junior          <option value="21">College Senior</select>\n\
	</div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Child 3 Age</div>\n\
	<div class="b"></div>\n\
	<div class="c">\n\
		<select id="college_child3">	<option selected="selected" value="-1">N/A          <option value="0">0 Years          <option value="1">1 Years          <option value="2">2 Years          <option value="3">3 Years          <option value="4">4 Years          <option value="5">Kindergarten          <option value="6">1st Grade          <option value="7">2nd Grade          <option value="8">3rd Grade          <option value="9">4th Grade          <option value="10">5th Grade          <option value="11">6th Grade          <option value="12">7th Grade          <option value="13">8th Grade          <option value="14">9th Grade          <option value="15">10th Grade          <option value="16">11th Grade          <option value="17">12th Grade          <option value="18">College Freshman          <option value="19">College Sophomore          <option value="20">College Junior          <option value="21">College Senior</select>\n\
	</div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Child 4 Age</div>\n\
	<div class="b"></div>\n\
	<div class="c">\n\
		<select id="college_child4">	<option selected="selected" value="-1">N/A          <option value="0">0 Years          <option value="1">1 Years          <option value="2">2 Years          <option value="3">3 Years          <option value="4">4 Years          <option value="5">Kindergarten          <option value="6">1st Grade          <option value="7">2nd Grade          <option value="8">3rd Grade          <option value="9">4th Grade          <option value="10">5th Grade          <option value="11">6th Grade          <option value="12">7th Grade          <option value="13">8th Grade          <option value="14">9th Grade          <option value="15">10th Grade          <option value="16">11th Grade          <option value="17">12th Grade          <option value="18">College Freshman          <option value="19">College Sophomore          <option value="20">College Junior          <option value="21">College Senior</select>\n\
	</div>\n\
	<div class="a">Investment Rate of Return</div>\n\
	<div class="b">%</div>\n\
	<div class="c"><input id="college_rate" type="text" value="8" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Education Inflation</div>\n\
	<div class="b">%</div>\n\
	<div class="c"><input id="college_inflation" type="text" value="6" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="clear"></div>\n\
	\n\
	<div class="d"><button onclick="college_calculate()">Calculate</button></div>\n\
	<div id="college_dt_results" class="dt_results padded-table">&nbsp;</div>\n\
</div>\n\
';
var url = 'college-savings.htm';

//var baseUrl = 'http://localhost/';
//var scriptUrl = 'on-your-site/' + url.split('.')[0] + '.js';
var baseUrl = '';
var scriptUrl = url.split('.')[0] + '.js';

var scriptEls = document.getElementsByTagName('script')
var scriptEl=false;
for(i=0; i<scriptEls.length; i++)
{
	var t = scriptEls[i];
	var src = (t.getAttribute('src'));
	if(src == (baseUrl + scriptUrl))
	{
		scriptEl = t;
		break;
	}
}
if(!scriptEl)
{
	//document.write('\n<p>Failed loading calculator. </p>');
}
else
{
	var par = scriptEl.parentNode;
	var link = par.getElementsByTagName('a')[0];
	var el = document.createElement('div');
	el.innerHTML = out;
	par.appendChild(el);
	
	if(link && link.href==baseUrl + 'college-savings-calculator.htm') {
		link.style.fontSize="80%";
		var calcDiv = document.getElementById('college_dt_calculator');
		if(calcDiv)	{
			// Put link on bottom of calculator box
			par.removeChild(link)			
			calcDiv.appendChild(link);	
		}
	}
}
