// Ship Date Calculator
// ©2009 Lymphedema Products, LLC
// All Rights Reserved.

function find_ship_date(business_days_before_ship) {
  potential_ship_date = new Date();
  var month_names = ["January","February","March","April","May","June","July","August","September","October","November","December"];
  var day_names = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
  if (ship_today() == false || check_against_holiday_list(potential_ship_date) == false || check_against_weekend(potential_ship_date) == false || business_days_before_ship > 1) {
	// past noon Eastern time - can't ship today or today is not a non-holiday business day or product is not a same-day ship item
	var ship_date = get_next_business_date(business_days_before_ship);
    var date_suffix = add_date_suffix(ship_date);
    var formatted_ship_date = day_names[ship_date.getDay()] + ", " + month_names[ship_date.getMonth()] + " " + ship_date.getDate() + date_suffix;
    var formatted_ship_date = "Ships " + formatted_ship_date + ".";
  }
  else {
    // shipping today
    formatted_ship_date = ship_today();
  }
  return formatted_ship_date;
}

function find_arrival_date(business_days_before_ship,ship_method) {
  potential_ship_date = new Date();
  var month_names = ["January","February","March","April","May","June","July","August","September","October","November","December"];
  var day_names = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
  if (ship_method == 4) {
    var ship_days = 1;
	var ship_days_end_range = 5;
  }
  else {
    var ship_days = ship_method;
  }
  var arrival_date = get_next_business_date(business_days_before_ship + ship_days);
  var arrival_end_date = get_next_business_date(business_days_before_ship + ship_days_end_range);
  var date_suffix = add_date_suffix(arrival_date);
  var formatted_arrival_date = day_names[arrival_date.getDay()] + ", " + month_names[arrival_date.getMonth()] + " " + arrival_date.getDate() + date_suffix;
  var date_suffix = add_date_suffix(arrival_end_date);
  var formatted_arrival_end_date = day_names[arrival_end_date.getDay()] + ", " + month_names[arrival_end_date.getMonth()] + " " + arrival_end_date.getDate() + date_suffix;
  if (ship_method == 4) {
    var formatted_arrival_date = "Between " + formatted_arrival_date + " and " + formatted_arrival_end_date;
  }
  else {
	var formatted_arrival_date = formatted_arrival_date;
  }
  return formatted_arrival_date;
}

function get_next_business_date(business_days_before_ship) {
  var potential_ship_date = new Date();
  var num_business_days_used = business_days_before_ship;
  var is_a_business_day = false;
  do {
	potential_ship_date.setTime(potential_ship_date.getTime() + (24 * 60 * 60 * 1000));
	is_a_business_day = (check_against_holiday_list(potential_ship_date) && check_against_weekend(potential_ship_date));
	if (is_a_business_day) {
	  num_business_days_used -= 1;
	}
  }
  while (is_a_business_day == false || num_business_days_used > 0);
  return potential_ship_date;
}

function check_against_holiday_list(potential_ship_date) {
  var holidays = "#1/1/2009#,#5/25/2009#,#7/4/2009#,#9/7/2009#,#11/26/2009#,#11/27/2009#,#12/25/2009#,#12/31/2009#";
  var formatted_date = "#" + (potential_ship_date.getMonth() + 1) + "/" + potential_ship_date.getDate() + "/" + potential_ship_date.getFullYear() + "#";
  var not_a_holiday = (holidays.indexOf(formatted_date) == -1);
  return not_a_holiday;
}

function check_against_weekend(potential_ship_date) {
  var not_a_weekend = (potential_ship_date.getDay() != 6 && potential_ship_date.getDay() != 0);
  return not_a_weekend;
}

function ship_today() {
  var current_time = new Date();
  var gmt_offset = current_time.getTimezoneOffset()/60;
  var current_hour = current_time.getHours() + gmt_offset - 5;
  var current_minute = current_time.getMinutes();
  if (current_hour > 11) {
    return false;
  }
  else {
	var remaining_minutes = Math.floor((12*60) - (current_hour*60) - current_minute);
	if (remaining_minutes > 60) {
	  var remaining_hours = Math.floor(remaining_minutes / 60);
	  var remaining_minutes = remaining_minutes - (remaining_hours * 60);
	  var remaining_time = remaining_hours + ((remaining_hours == 1) ? " hour" : " hours") + " and " + remaining_minutes + " minutes";
	}
	else {
	  var remaining_time = remaining_minutes + " minutes";
	}
    var ship_today_message = "Ships today if you order in the next " + remaining_time + ".";
	return ship_today_message;
  }
}

function add_date_suffix(ship_date) {
  if (ship_date.getDate() == 1) {
    var date_suffix = "st";
  }
  else if (ship_date.getDate() == 2) {
    var date_suffix = "nd";
  }
  else if (ship_date.getDate() == 3) {
    var date_suffix = "rd";
  }
  else {
    var date_suffix = "th";
  }
  return date_suffix;
}
