﻿// JScript File

//Calculates the per mile cost based on gas price, vehicle mileage and 
// pre-calculated maintenance costs.
function CalculatePerMileCost(vehicleType, gasPrice)
{
    var ReturnValue;
    if(vehicleType == "Small Sedan")
    {
        ReturnValue = (eval(gasPrice) / eval(30.06))+ eval(.0398) + eval(.0055) ;
    }
    
    else if(vehicleType == "Medium Sedan")
    {
        ReturnValue = eval(gasPrice) / eval(24.5) + eval(.0467) + eval(.0085);
    }
    
    else if(vehicleType == "Large Sedan")
    {
        ReturnValue = eval(gasPrice) / eval(22.5) + eval(.0507) + eval(.0077);
    }
    
    else if(vehicleType == "SUV")
    {
        ReturnValue = eval(gasPrice) / eval(17.5) + eval(.0547) + eval(.0093);
    }
    
    
    return ReturnValue;
}

//Shows annual ownership costs based on vehicle type.
function DisplayAnnualOwnershipCost(vehicleType, monthlyCost, itemToFill)
{
    var MonthlyCommuteCost = monthlyCost;
    var AnnualCommuteCost = MonthlyCommuteCost * 12; 
    
    
    if(vehicleType == "Small Sedan")
    {
        itemToFill.innerText = "$ " + ((eval(358) + eval(MonthlyCommuteCost)) - 30) + " per month or $ " + ((eval(4303) + eval(AnnualCommuteCost)) - 360) + " per year**";
        
    }
    else if(vehicleType == "Medium Sedan")
    {
        
        itemToFill.innerText = "$ " + ((eval(470) + eval(MonthlyCommuteCost)) - 30) + " per month or $ " + ((eval(5642) + eval(AnnualCommuteCost)) - 360) + " per year**";
        
    }
    else if(vehicleType == "Large Sedan")
    {
        
        itemToFill.innerText = "$ " + ((eval(563) + eval(MonthlyCommuteCost)) - 30) + " per month or $ " + ((eval(6763) + eval(AnnualCommuteCost)) - 360) + " per year**";
        
    }
    else if(vehicleType == "SUV")
    {
        
        itemToFill.innerText = "$ " + ((eval(566) + eval(MonthlyCommuteCost)) - 30) + " per month or $ " + ((eval(6790) + eval(AnnualCommuteCost)) - 360) + " per year**";
        
    
    }
}

function CalculateCommute(CalculatorForm)
{
    
    var TotalPerMileCost = CalculatePerMileCost(CalculatorForm.VehicleType.value, CalculatorForm.GasPrice.value);
    var DailyTotalMileageCost = eval(TotalPerMileCost) * eval(CalculatorForm.CommuteDistance.value)
    
    var TotalDailyCost = eval(Math.round(eval(DailyTotalMileageCost)));
    
    var TotalMonthlyCost = CalculateMonthlyCost(CalculatorForm);// Math.round(((TotalDailyCost * 22)+ eval(CalculatorForm.ParkingCost.value)) * 100) / 100;
    var TotalAnnualCost = Math.round(TotalMonthlyCost * 12) ;
    
    document.body.all["MonthlyPOVCost"].innerText = " $ " + TotalMonthlyCost + " a month or" + " $ " + TotalAnnualCost + " per year.* " ;
    document.body.all["FootNoteCell"].innerText = "Excludes cost of owning a vehicle.";
    
    
    document.body.all["POVComparison"].innerText = " $ " + TotalMonthlyCost + " a month";// or" + " $ " + TotalAnnualCost + " per year.* " ;
    /*These values are set in the Compared to the Bus Section */
    document.body.all["CommuteSavings"].innerText = "$ " + (Math.round((TotalMonthlyCost - 30) ) ) + " per month or $ " + Math.round(TotalAnnualCost - 360) + " per year.";
    
    
    
    
   DisplayAnnualOwnershipCost(CalculatorForm.VehicleType.value, TotalMonthlyCost, document.body.all["OwnershipTotal"]);
    
}


function CalculateMonthlyCost(CalculatorForm)
{
    
    var TotalPerMileCost = CalculatePerMileCost(CalculatorForm.VehicleType.value, CalculatorForm.GasPrice.value);
    var DailyTotalMileageCost = eval(TotalPerMileCost) * eval(CalculatorForm.CommuteDistance.value)
    
    var TotalDailyCost = eval(DailyTotalMileageCost);
    
    var ReturnValue = Math.round((TotalDailyCost * 22)+ eval(CalculatorForm.ParkingCost.value)) ;
    
    
    return ReturnValue;
}

function CalculateVanpoolRiderCost(numberOfPassengers, roundTripMiles, pricePerGallon, CalculatorForm)
{   
    var GasPrice;
    var ReturnValue;
    if(eval(pricePerGallon))
    {
        GasPrice = pricePerGallon;
    
    }
    else
    {
        GasPrice = 2.69;
    }
    var SevenPassengerCost = new Array(800, 810, 820, 840, 870);
    var TwelvePassengerCost = new Array(950, 970, 990, 1010, 1040);
    var FifteenPassengerCost = new Array(1025, 1045, 1065, 1085, 1115);
    var VanMpgValues = new Array(26, 18, 15);
    var VanPricePerMile;
    var ViaSubsidy = 20;
    
    
    if(eval(numberOfPassengers) < 7)
    {
        ReturnValue = Math.round(SevenPassengerCost[GetMileageArrayPosition(roundTripMiles)]/ eval(numberOfPassengers));
        VanPricePerMile = (GasPrice / VanMpgValues[0]) / (eval(numberOfPassengers) + 1);
        
    }    
    else if(eval(numberOfPassengers) < 12)
    {    
        ReturnValue = Math.round(TwelvePassengerCost[GetMileageArrayPosition(roundTripMiles)] / eval(numberOfPassengers));
        VanPricePerMile = (GasPrice / VanMpgValues[1]) / (eval(numberOfPassengers) + 1) ;
    }
    else
    {
        ReturnValue = Math.round(FifteenPassengerCost[GetMileageArrayPosition(roundTripMiles)] / eval(numberOfPassengers));
        VanPricePerMile = (GasPrice / VanMpgValues[2]) / (eval(numberOfPassengers) + 1) ;
    }
    
    ReturnValue = Math.round(ReturnValue - 20);
    var RiderShare = ReturnValue;
    
    var RiderGasShare = Math.round((VanPricePerMile)* (roundTripMiles * 22)) ;
    var RiderTotal = Math.round(RiderShare + RiderGasShare);
    
    document.body.all["RiderShare"].innerText = "$ " + ReturnValue + " per month***";
    document.body.all["RiderGasShare"].innerText = "$ " + RiderGasShare  + " per month****";
    
    document.body.all["TotalVanpoolCost"].innerText = "$ " + RiderTotal + " per month";
     
    var MontlyCommuteCost = CalculateMonthlyCost(CalculatorForm);
    var AnnualCommuteCost = Math.round(MontlyCommuteCost * 12) ;
    
     
    document.body.all["VanpoolSavings"].innerText = "$ " + Math.round(MontlyCommuteCost - RiderTotal) + " per month or $" + Math.round(AnnualCommuteCost - (RiderTotal * 12)) + " per year" ;
    document.body.all["CommuteCostVanpoolSection"].innerText = "$ " + MontlyCommuteCost  + " per month or $" + AnnualCommuteCost + " per year" ;
    
    DisplayAnnualOwnershipCost(CalculatorForm.VehicleType.value, RiderTotal, document.body.all["VanpoolOwnershipTotal"])
}





function GetMileageArrayPosition(miles)
{
    var ReturnValue;
    
    if(eval(miles) < 41)
    {
        ReturnValue = 0;
    }
    else if(eval(miles) <61)
    {
        ReturnValue = 1;
    }
    else if(eval(miles) < 101)
    {
        ReturnValue = 2;
    }
    else if(eval(miles) < 151)
    {
        ReturnValue = 3;
    }
    else if(eval(miles) < 201)
    {
        ReturnValue = 4;
    }
    
    
    
    return ReturnValue;

}