lunch_calculate = function()
{
	var sack = document.getElementById("lunch_sack").value;
	if ( sack.length == 0 )
	{
		 alert("Please enter the price of your sack lunch");
		 return;
	}
	sack = fixNumber(sack);
	sack = parseFloat(sack);
	if ( isNaN(sack) )
	{
		 alert("Please enter the price of your sack lunch as a number");
		 return;
	}
	if ( sack < 0 )
	{
		 alert("Please enter a sack lunch price >= 0");
		 return;
	}


	var out = document.getElementById("lunch_out").value;
	if ( out.length == 0 )
	{
		 alert("Please enter the price of going out to eat");
		 return;
	}
	out = fixNumber(out);
	out = parseFloat(out);
	if ( isNaN(out) )
	{
		 alert("Please enter the going out price as a number");
		 return;
	}
	if ( out < 0 )
	{
		 alert("Please enter a going out price >= 0");
		 return;
	}

	if ( sack >= out )
	{
		alert("The cost of your sack lunch is greater or equal to the cost of going out.  You won't save any money.");
		return;
	}


	var days = document.getElementById("lunch_days").value;
	if ( days.length == 0 )
	{
		 alert("Please enter the number of days per week you go out to eat");
		 return;
	}
	days = fixNumber(days);
	days = parseFloat(days);
	if ( isNaN(days) )
	{
		 alert("Please enter the number of days per week you go out to eat as a number");
		 return;
	}
	if ( days < 1 || days > 7 )
	{
		 alert("Please enter the number of days per week you go out to eat, between 1 and 7");
		 return;
	}



	var years = document.getElementById("lunch_years").value;
	if ( years.length == 0 )
	{
		 alert("Please enter the number of years to calculate");
		 return;
	}
	years = fixNumber(years);
	years = parseFloat(years);
	if ( isNaN(years) )
	{
		 alert("Please enter the number of years to calculate as a number");
		 return;
	}
	if ( years < 1 || years > 100 )
	{
		 alert("Please nter the number of years to calculate between 1 and 100");
		 return;
	}
	

	var rate = document.getElementById("lunch_rate").value;
	if ( rate.length == 0 )
	{
		 alert("Please enter an interest rate");
		 return;
	}
	rate = fixNumber(rate);
	rate = parseFloat(rate);
	if ( isNaN(rate) )
	{
		 alert("Please enter the interest rate as a number");
		 return;
	}
	if ( rate < 0 )
	{
		alert("Please enter a rate of interest >= 0" );
	}


	var results = "<table cellpadding=\"0\" cellspacing=\"0\" class=\"data\">";
	results += "<tr><th>Year<th>Starting Amount<th>Interest Earned<th>Money Saved<th>Ending Amount";
	var i;
	var money = 0;
	for (i=1; i<=years; i++)
	{
		results += "<tr><td>" + i + "<td>" + formatDollars(money);
		var interest = money * (rate / 100);
		results += "<td>" + formatDollars(interest);
		var saved = (out-sack)*days*50;
		results += "<td>" + formatDollars(saved);
		money = money + interest + saved;
		results += "<td>" + formatDollars(money);
	}
	 
	var x = document.getElementById("lunch_dt_results");

	x.innerHTML = results;
	x.style.display = "block";
	}// modified css: http://www.dollartimes.com/calculators/on-your-site/calc-css.php
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('title', 'dt-calc-style');
	s.setAttribute('rel','stylesheet');
	s.setAttribute('href',fileName);
	th.appendChild(s);
}


addJavascript('http://www.dollartimes.com/script/calcutil.js');
addStylesheet('http://www.dollartimes.com/calculators/on-your-site/calc-css.php');

var out = '\
<div id="lunch_dt_calculator" class="dt_calculator">\n\
	<h2>Bring or Buy Lunch Calculator</h2>\n\
	<p class="small">Provided by DollarTimes.com</p>\n\
	<p class="instructions">This will calculate how much one can save by bringing a sack lunch to work rather than going out to eat. Interest is compounded yearly. We assume you work 50 weeks a year.</p>\n\
	<div class="a">Sack Lunch Price</div>\n\
	<div class="b">$</div>\n\
	<div class="c"><input id="lunch_sack" type="text" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Cost of Going Out</div>\n\
	<div class="b">$</div>\n\
	<div class="c"><input id="lunch_out" type="text" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Days Per Week You Go Out</div>\n\
	<div class="b"></div>\n\
	<div class="c"><input id="lunch_days" type="text" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Years to Calculate</div>\n\
	<div class="b"></div>\n\
	<div class="c"><input id="lunch_years" type="text" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Rate of Return</div>\n\
	<div class="b">%</div>\n\
	<div class="c"><input id="lunch_rate" type="text" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="d"><button onclick="lunch_calculate()">Calculate</button></div>\n\
	<div id="lunch_dt_results" class="dt_results">&nbsp;</div>\n\
</div>\n\
';



var calcUrl = "http://www.dollartimes.com/calculators/on-your-site/calculator.php?id=4";

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 == calcUrl) 
	{
		scriptEl = t;
		break;
	}
}
if(!scriptEl)
{
	document.write('\n<p>Failed loading calculator. The code might have been updated. Please get the correct code at <a href="http://www.dollartimes.com/calculators/on-your-site/'+calcUrl+'">http://dollartimes.com/calculators/on-your-site/'+calcUrl+'</a></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.toString().match("/calculators/")) {

		link.style.fontSize="80%";
		var calcDiv = document.getElementById('lunch_dt_calculator');
		if(calcDiv)	{
			// Put link on bottom of calculator box
			par.removeChild(link)			
			calcDiv.appendChild(link);	
		}
	}
}
