/* Copyright 2010 Shiren Vijiasingam
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version, provided full attribution is provided.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

See <http://www.gnu.org/licenses/> for a copy of the GNU General Public License.
*/
function processData(form) {
// Store form values into vars. Values not hardcoded for extensibility and future-scalability
var finThirtyDay = form.thirtyDay.value;
var finPayPer = form.payPer.value;
var finDiscPayPer = form.discPayPer.value;
var thirtyDayLabel = form.thirtyDayLabel.value;
var payPerLabel = form.payPerLabel.value;
var vacDay = eval(form.vac.value)
var holDay = eval(form.hol.value)
var moreDay = eval(form.more.value)
// Logic to set the extra days to zero if fields intentionally erased/left blank
vacDay = (vacDay == undefined) ? 0 : vacDay;
holDay = (holDay == undefined) ? 0 : holDay;
moreDay = (moreDay == undefined) ? 0 : moreDay;
// Parse form date selection into date objects and calculate duration between
var startDate = Date.parse(form.month.value + "/" + form.day.value + "/" + form.year.value);
var endDate = Date.parse(form.month2.value + "/" + form.day2.value + "/" + form.year2.value);
var startFormatDate = new Date(startDate);
var endFormatDate = new Date(endDate);
var diff_date =  endDate - startDate;
// Set month variable by dividing the appropriate number of milliseconds
var num_months = diff_date/2628000000;
// Get number of months for monthly pass. Uses ceiling attribute to be inclusive of entire month if partial month selected  
var finThirtyTotal = (Math.ceil(num_months)*finThirtyDay)
// Define vars for business day calculation, inc. day of week || Needed for the daily pass calculation
var busWeeks, nonFinBusDays, busModifier = 0;
var busWeekday1 = startFormatDate.getDay();                // day of week
var busWeekday2 = endFormatDate.getDay();
// Cast new variables and modify operators for accurate calculation
busWeekday1 = (busWeekday1 == 0) ? 7 : busWeekday1;   // change Sunday from 0 to 7
busWeekday2 = (busWeekday2 == 0) ? 7 : busWeekday2;
if ((busWeekday1 > 5) && (busWeekday2 > 5)) busModifier = 1;  // adjustment if both days on weekend
busWeekday1 = (busWeekday1 == 7) ? 6 : busWeekday1;    // only count weekdays
busWeekday2 = (busWeekday2 == 7) ? 6 : busWeekday2;
busWeeks = Math.floor(diff_date / 604800000)
// Depending on day of week of start vs end date, adjust calculations accordingly
if (busWeekday1 < busWeekday2) {
  nonFinBusDays = (busWeeks * 5) + (busWeekday2 - busWeekday1)
} else {
  nonFinBusDays = ((busWeeks + 1) * 5) - (busWeekday1 - busWeekday2)
}
// Modifier logic to account for start/end dates on weekends or if start/end are on the same day
if (busWeekday1 == busWeekday2) {					// logic to check if start and end dates fall on same day
  if (busWeekday1 == 6) {						// if day is weekend, logic to deduct 5 multiplier days
    busModifier += 5
  }
  else {										// if day is weekend, logic to deduct 6 multiplier days
    busModifier += 6								// deduction occurs via iAdjust variable to be deducted from final sum
  }
}
// Adjustments to final number to get actual business days
nonFinBusDays -= busModifier                            // take into account both days on weekend
var finBusDays = (nonFinBusDays + 1);                         // add 1 because dates are inclusive
var trainBusDays = finBusDays
// Subtract holidays/vacation and add in extra travel. Double the number since it will be round-trip travel
trainBusDays -= vacDay
trainBusDays -= holDay
trainBusDays += moreDay
var trainTrips = (trainBusDays*2)
// Calculate costs, multiple, then divide to get 2 decimal places
var finPayPerTotal = Math.round((trainTrips*finDiscPayPer)*100)/100
// Define conclusion variables
var concText = ""
var costVariance, extraRide = 0
// If statement to determine cheaper option
if (finThirtyTotal > finPayPerTotal) {
costVariance = (Math.round((finThirtyTotal-finPayPerTotal)*100)/100)
extraRide = Math.floor(costVariance/finPayPer)
concText = "The cheaper option is the " + payPerLabel + " by $" + costVariance + ", or by " + extraRide + " full-fare rides."
}
else if (finThirtyTotal < finPayPerTotal) {
costVariance = (Math.round((finPayPerTotal-finThirtyTotal)*100)/100)
extraRide = Math.floor(costVariance/finPayPer)
concText = "The cheaper option is the " + thirtyDayLabel + " by $" + costVariance + ", or by " + extraRide + " full-fare rides."
}
else {
concText = "It's a wash. Both net out the same for the selected duration."
}
// Write result to the result div
var resultText=document.getElementById("result")
resultText.innerHTML="<strong>" + thirtyDayLabel + "</strong>" + ": " + Math.ceil(num_months) + " months x $" + finThirtyDay + " = $" + finThirtyTotal +"<br/>" + "<strong>" + payPerLabel + "</strong>" + ": " + trainTrips + " trips x $"+ finDiscPayPer + "/ride (discounted) = $" + finPayPerTotal + "<br/><br/>" + concText;
}
