var rideOption = ['Select One', 'Riding the Bus', 'Carpooling', 'Vanpooling', 'Bicycling'];
var times_daily = 2;
var days_week = 5;
var weeks_month =  4;
var yearly = 12;
var cost_per_mile = .585;



function BuildCalculator()
{
	document.write('<table cellspacing="0px" cellpadding="0px" align="center" width="434px">');
	document.write('<tr>');
	document.write('<td>');

	document.write('<table cellspacing="0px" cellpadding="0px" align="center" width="434px">');
	
	document.write('<tr>');
	document.write('<td><font size="4" color="#018749">How much money can MCT save you?</font><br/><br/>');
	document.write('</td>');
	document.write('</tr>');
	
	document.write('<tr width="434px">');
	document.write('<td width="434px" colspan="2">');
	document.write("Calculate your savings by entering your one-way commute miles, selecting a transportation option and clicking 'Calculate'.");
	document.write('<br/><br/></td>');
	document.write('</tr>');



	document.write('<tr>');
	document.write('<td>');
	document.write('<table background="images/savings-calculator2.jpg" width="349px" height="155px" border="0" cellpacing="0" cellpadding="0px">');
	document.write('<tr height="100px">');
	document.write('<td align="bottom" width="120px"><input type="text" name="miles" size="6" onchange="changeMileage()" onselect="changeMileage()" ></td>');
	document.write('<td align="center"  width="118px"><input type="text" dir="rtl" name="monthlyCost" size="7" readonly="true"></td>');
	document.write('<td align="center" width="110px"><input type="text" dir="rtl" name="annualCost" size="7" readonly="true"></td>');
	document.write('</tr>');
	document.write('<tr height="20px"><td></td></tr>');
	
	
	document.write('<tr>');
	document.write('<td align="left" width="120px">');
	BuildCombo();
	document.write('</td>');
	document.write('<td align="center"  width="118px"><input type="text" dir="rtl" name="monthlySavings" size="7" readonly="true"></td>');
	document.write('<td align="center"  width="110px"><input type="text" dir="rtl" name="yearlySavings" size="7" readonly="true"></td>');
	document.write('</tr>');
	document.write('</table>');
	
	document.write('</td>');
        document.write('<td>');
	
	
	document.write('<table cellspacing="0px" cellpadding="0px" border="0">');
	document.write('<tr height="34px">');
	document.write('<td align="left" valign="middle" width="85px" height="34px"><img src="images/1line.jpg">');
	
	document.write('</td>');
	document.write('</tr>');
	
	document.write('<tr height="32px">');
	document.write('<td height="32px" align="right" valign="middle"><img id="image" src="images/calc.jpg" onclick="Calculate()" >');
	
	document.write('</td>');
	document.write('</tr>');
	
	document.write('<tr height="38px">');
	document.write('<td height="38px"><img src="images/2line.jpg">');
	
	document.write('</td>');
	document.write('</tr>');
	document.write('</table>');
	
	
	document.write('</td>');
	document.write('</tr>');
	
	
	document.write('</table><br/>');
	
	//document.write("Calculate your savings by entering your one-way commute miles, selecting a transportation option and clicking 'Calculate'. ");
	document.write('</td>');
	document.write('</tr>');
	document.write('</table>');
	

	
}

function BuildCombo()
{
	{
	
      	document.write('<center><select name="FromSelect" onchange="changeTransport()">');
        for(var i = 0; i < rideOption.length; i++)
        {
		   
		
		
			document.write('<option>' + rideOption[i] + '</option>');
	    	
		}
	    document.write('</select>');
	
	}




}

function Calculate()
{
    var miles = document.Form1.miles.value;
    var miles1 = trim(miles);
    var test = mileageExists(miles1);
    var test1 = validMileage(miles);
    
    

    if (test == true && test1 == true)
    {
		var fromIndex = document.Form1.FromSelect.selectedIndex;
		var monthlySavings;
		var yearlySavings;
		
    
		var monthlyCost = times_daily * days_week * weeks_month * cost_per_mile * miles;
		var annualCost = monthlyCost * yearly;
	
		document.Form1.monthlyCost.value = '$' + monthlyCost.toFixed(2);
		document.Form1.annualCost.value = '$' + annualCost.toFixed(2);
	
		if (fromIndex == 1)
		{
			monthlySavings = monthlyCost - 60;
			yearlySavings = monthlySavings * 12;
			if (monthlySavings < 0)
			{
				
				
				monthlySavings = Math.abs(monthlySavings.toFixed(2));
				yearlySavings = Math.abs(yearlySavings.toFixed(2));
				document.Form1.monthlySavings.value = '($' + monthlySavings + ')';
				document.Form1.yearlySavings.value = '($' + yearlySavings + ')';
			}
			else
			{
				document.Form1.monthlySavings.value = '$' + monthlySavings.toFixed(2);
				document.Form1.yearlySavings.value = '$' + yearlySavings.toFixed(2);	
			}
	
		}
		else if (fromIndex == 2)
		{
			monthlySavings = monthlyCost/2;
			yearlySavings = monthlySavings * 12;
			document.Form1.monthlySavings.value = '$' + monthlySavings.toFixed(2);
			document.Form1.yearlySavings.value = '$' + yearlySavings.toFixed(2);	
		
	
		}
		else if (fromIndex == 3)
		{
			monthlySavings = monthlyCost - 125;
			yearlySavings = monthlySavings * 12;
			if (monthlySavings < 0)
			{
				
				
				monthlySavings = Math.abs(monthlySavings.toFixed(2));
				yearlySavings = Math.abs(yearlySavings.toFixed(2));
				document.Form1.monthlySavings.value = '($' + monthlySavings + ')';
				document.Form1.yearlySavings.value = '($' + yearlySavings + ')';
			}
			
			else
			{
				document.Form1.monthlySavings.value = '$' + monthlySavings.toFixed(2);
				document.Form1.yearlySavings.value = '$' + yearlySavings.toFixed(2);	
			}
		
	
		}
		else if (fromIndex == 4)
		{
			monthlySavings = monthlyCost;
			yearlySavings = monthlySavings * 12;
			document.Form1.monthlySavings.value = '$' + monthlySavings.toFixed(2);
			document.Form1.yearlySavings.value = '$' + yearlySavings.toFixed(2);	
		
	
		}
		else
		{
			monthlySavings = 0;
			yearlySavings = monthlySavings * 12;
			document.Form1.monthlySavings.value = '$' + monthlySavings.toFixed(2);
			document.Form1.yearlySavings.value = '$' + yearlySavings.toFixed(2);	
		
	
		}
		document.Form1.image.src = "images/calconclick.jpg"

	}
}

function mileageExists(miles)
{
	
	
	if(miles.length == 0)
	{
		alert("Please enter your one-way commute miles.");
		document.Form1.miles.focus();
		return false;
		
	}
	else
	{
		return true;
	}
}

function validMileage(mile)
{
	
	
	if (mile < 0)
	{
		alert("Please enter a postive number.");
		document.Form1.miles.focus();
		document.Form1.miles.select();
		return false;
	
	}
	else if(isNaN(mile))
	{
		alert("Please enter a positive number not character(s).")
		document.Form1.miles.focus();
		document.Form1.miles.select();
		return false;
	}
	else
	{
		
		return true;
	}
}

function changeTransport()
{
	document.Form1.monthlySavings.value = " ";
	document.Form1.yearlySavings.value = " ";
	document.Form1.image.src = "images/calc.jpg"
}

function changeMileage()
{
	document.Form1.monthlyCost.value = " ";
	document.Form1.annualCost.value = " ";
	document.Form1.monthlySavings.value = " ";
	document.Form1.yearlySavings.value = " ";
	document.Form1.image.src = "images/calc.jpg"
}
function resetFigures()
{
	document.Form1.miles.value = " ";
	document.Form1.monthlyCost.value = " ";
	document.Form1.annualCost.value = " ";
	document.Form1.monthlySavings.value = " ";
	document.Form1.yearlySavings.value = " ";
	document.Form1.image.src = "images/calc.jpg";

}
function trim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
} 
