comm_calculate = function() {  

	var miles = document.getElementById("comm_miles").value;
	if ( miles.length == 0 )
	{
		 alert("Please enter commute distance in miles (one way).");
		 return;
	}
	miles = fixNumber(miles);
	miles = parseFloat(miles);
	if ( isNaN(miles) )
	{
		 alert("Please enter commute distance (miles, one way) as a number");
		 return;
	}
	if ( miles <= 0 )
	{
		 alert("You aren't driving any miles, so you won't be spending anything on gas");
		 return;
	}

	var mpg = document.getElementById("comm_mpg").value;
	if ( mpg.length == 0 )
	{
		 alert("Please enter the MPG of your current vehicle.");
		 return;
	}
	mpg = fixNumber(mpg);
	mpg = parseFloat(mpg);
	if ( isNaN(mpg) )
	{
		 alert("Please enter the MPG of your current vehicle as a number.");
		 return;
	}
	if ( mpg <= 0 )
	{
		alert("Please enter an MPG greater than 0, unless you're riding a bike, in which case you don't spend anything on your commute. Congratulations!")
		return;
	}
	
	var cost = document.getElementById("comm_cost").value;
	if ( cost.length == 0 )
	{
		 alert("Please enter the cost of a gallon of gas");
		 return;
	}
	cost = fixNumber(cost);
	cost = parseFloat(cost);
	if ( isNaN(cost) )
	{
		 alert("Please enter the cost of a gallon of gas as a number");
		 return;
	}
	if ( cost <= 0 )
	{
		 alert("If gas is free, your commute doesn't cost you anything.");
		 return;
	}
	
	var parking = document.getElementById("comm_parking").value;
	if ( parking.length > 0 )
	{
		parking = fixNumber(parking);
		parking = parseFloat(parking);
		if ( isNaN(parking) )
		{
			 alert("Please enter the cost of parking as a number");
			 return;
		}
		if ( parking < 0 )
		{
			parking = 0;
		}
	}

	var costDay = (( 2 * miles / mpg ) * cost) + parking;
	var costWeek = 5 * (costDay);
	var costYear = 250 * (costDay);

	var results = "<p>Your daily commute costs you " + formatDollars(costDay) + " round-trip.</p>";
	results += "<p>A five-day working week will run up " + formatDollars(costWeek) + " in commuting costs.</p>";
	results += "<p>With a 250-workday year, you spend " + formatDollars(costYear) + " per year commuting by car.</p>";

	 
	var x = document.getElementById("comm_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="comm_dt_calculator" class="dt_calculator">\n\
	<h2>Commuting Cost Calculator</h2>\n\
	<p class="small">Provided by DollarTimes.com</p>\n\
	<p class="instructions">How much money do you spend on commuting by car?</p>\n\
	<div class="a">Miles to Work<br />(one way)</div>\n\
	<div class="b"></div>\n\
	<div class="c"><input id="comm_miles" type="text" value="15" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	\n\
	<div class="a">Miles per Gallon</div>\n\
	<div class="b"></div>\n\
	<div class="c"><input id="comm_mpg" type="text" value="20" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Cost of Gas per Gallon</div>\n\
	<div class="b"></div>\n\
	<div class="c"><input id="comm_cost" type="text" value="3.80" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Cost of Parking<br />(per day, if any)</div>\n\
	<div class="b"></div>\n\
	<div class="c"><input id="comm_parking" type="text" value="" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="d"><div class="dt_button" onclick="comm_calculate()" onMouseover="this.className=\'dt_button_sel\'" onMouseout="this.className=\'dt_button_out\'">Calculate</div></div>\n\
	<div id="comm_dt_results" class="dt_results padded-table">&nbsp;</div>\n\
</div>\n\
';



var calcUrl = "http://www.dollartimes.com/calculators/on-your-site/calculator.php?id=5";

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('comm_dt_calculator');
		if(calcDiv)	{
			// Put link on bottom of calculator box
			par.removeChild(link)			
			calcDiv.appendChild(link);	
		}
	}
}
