// Mileage cost calculator
$(function(){

	// ################## Actiovate form buttons  ################## 
	$('#nosubmit').hide();
	$('#FormButtons').show();
	
	
	// ################## Prevent Car Ownership modificztion ################## 
	$('#CarCost').change(function() {
		//if ($('#CarCost').val()!='.552')
			 alert ('This value is fixed. Please click on "source" to know more.');
			$('#CarCost').val('.552');
			return false;
		//}
	});

	// ################## Max out days per month to 30 ################## 
	$('#DriveDays').change(function() {
		if ($('#DriveDays').val()>30) {
		$('#DriveDays').val(30);
		}
		return false;
	});


	// ##################  Clearform  ################## 
	$('#clearbutton').click(function() {								 
		$('#DriveDays').val("");
		 $('#MilesPerDay').val("");
		 $('#PriceGallonGas').val("");
		 $('#CarMPG').val("");
		 $('#CostPerDay').val("");
		 $('#CostPerMonth').val("");
		 $('#LbsCOPerMonth').val("");

		return false;
	});

	// ##################  Calculate  ################## 
	$('#submitbutton').click(function() {								 
		//Read form values
		var DriveDays = $("input[name=DriveDays]").val(); 
		if (DriveDays>30) {
				DriveDays =30;
				$('#DriveDays').val(30)}
		var MilesPerDay = $("input[name=MilesPerDay]").val(); 
		var CarMPG = $("input[name=CarMPG]").val(); 
		var PriceGallonGas = $("input[name=PriceGallonGas]").val(); 
		
		//Abort if any form fields are left blank
		if (DriveDays == '' || MilesPerDay == '' || CarMPG == '' || PriceGallonGas == '' ){
			alert ('All fields are required.');
			return false;
		}
		
		//Abort if values are not numbers
		var numberRegex = /^[+-]?\d+(\.\d+)?([eE][+-]?\d+)?$/;

		if(!numberRegex.test(DriveDays) || !numberRegex.test(MilesPerDay)  || !numberRegex.test(CarMPG)  || !numberRegex.test(PriceGallonGas)) {
			alert ('Please use digits only.');
			return false;
		}


		//Abort if values are zeros

		if (DriveDays == 0 || MilesPerDay == 0 || CarMPG == 0 || PriceGallonGas == 0 ){
			alert ('All fields are required.');
			return false;
		}

		//Calculations

		//# of work days per month x daily mileage = miles commuted per month
		//Miles commuted per month / mpg = gallons of gas used per month
		//Price per gallon x gallons of gas used per month  + (.552 x monthly mileage) = Monthly commute cost
		//Monthly commute cost /22 days per month = Daily commute cost
		//Gallons of gas per month x 19.564 LBS of CO2 emitted per gallon of gas burned

		MonthlyMiles = DriveDays*MilesPerDay;
		MonthlyGallons = MonthlyMiles / CarMPG;
		CostPerMonth = (MonthlyGallons*PriceGallonGas)+(MonthlyMiles*.552);
		CostPerDay = CostPerMonth/DriveDays ;
		EmissionsMonth=MonthlyGallons*19.564.toFixed(2);
		EmissionsMonth=EmissionsMonth.toFixed(2);
		CostPerDay = CostPerDay.toFixed(2); 
		CostPerMonth = CostPerMonth.toFixed(2);
									 
		 //Display results on page
		 $('#CostPerDay, #CostPerMonth, #EmissionsMonth').html('');
		 $('#CostPerDay').val("$"+CostPerDay);
		 $('#CostPerMonth').val("$"+CostPerMonth);
		 $('#LbsCOPerMonth').val(EmissionsMonth);
		return false;
	});




});

