// when will my order ship
// Greg Mote 2011-12-01

function checkHoliday (dt_date) {
	// check simple dates (month/date - no leading zeroes)
	var n_date = dt_date.getDate(),
	n_month = dt_date.getMonth() + 1;
	var s_date1 = n_month + '/' + n_date;
	
	if ( s_date1 == '1/1' // New Year's Day
	|| s_date1 == '7/4' // Independence Day
	|| s_date1 == '12/25' // Christmas Day
	) return true;
	
	// weekday from beginning of the month (month/num/day)
	var n_wday = dt_date.getDay(),
	n_wnum = Math.floor((n_date - 1) / 7) + 1;
	var s_date2 = n_month + '/' + n_wnum + '/' + n_wday;
	
	if ( n_wday == 0 // Sunday
	|| n_wday == 6 // Saturday
	) return true;
	
	if ( s_date2 == '9/1/1'   // Labor Day, first Monday in September
	|| s_date2 == '11/4/4'  // Thanksgiving Day, fourth Thursday in November
	|| s_date2 == '2/1/3'  // President's Day, third Monday in February
	) return true;
	
	// weekday number from end of the month (month/num/day)
	var dt_temp = new Date (dt_date);
	dt_temp.setDate(1);
	dt_temp.setMonth(dt_temp.getMonth() + 1);
	dt_temp.setDate(dt_temp.getDate() - 1);
	n_wnum = Math.floor((dt_temp.getDate() - n_date - 1) / 7) + 1;
	var s_date3 = n_month + '/' + n_wnum + '/' + n_wday;
	
	if ( s_date3 == '5/1/1' // Memorial Day, last Monday in May
	) return true;
	
	// misc complex dates
	// Inauguration Day, January 20th every four years, starting in 1937.
	//if (s_date1 == '1/20' && (((dt_date.getFullYear() - 1937) % 4) == 0)
	//) return true;
	
	// Election Day, Tuesday on or after November 2.
	//if (n_month == 11 && n_date >= 2 && n_date < 9 && n_wday == 2
	//) return true;
	
	return false;
}

function shipsWhen (serverDateTime, availability) {
	var dayDelay = 0;
	// looks for next shipping day and writes the day of the week
	var dayofweek = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
	//var monthofyear = ["January","February","March","April","May","June","July","August","September", "October","November","December"];
	var monthofyear = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep", "Oct","Nov","Dec"];
	// what day is it?
	var currentDateTime = new Date(serverDateTime);
	var shipDate = new Date(currentDateTime);
	// is it past the shipping cut off (11:45)?
	if ((currentDateTime.getHours()+currentDateTime.getMinutes()/60) >= 11.75) {
		dayDelay++;
		shipDate.setDate(shipDate.getDate()+1); // try next day
		}
	// find next shipping day
	while (checkHoliday(shipDate)) {
		dayDelay++;
		shipDate.setDate(shipDate.getDate()+1); // try next day
		}
	// read the availability
	if ((availability.toLowerCase().indexOf("in stock") != -1) || (availability == "")) {
		shipDate.setHours(11);
		shipDate.setMinutes(45);
		msPerHour = 60 * 60 * 1000;
		shipHoursFull = (shipDate.getTime() - currentDateTime.getTime()) / msPerHour;
		shipHours = Math.floor(shipHoursFull);
		shipMinutes = Math.floor((shipHoursFull - shipHours) * 60);
		// output availabilty
		document.write("In Stock, ships ");
		if (dayDelay == 0) { document.write("today"); }
		else if (dayDelay == 1) { document.write("tomorrow"); }
		else { document.write(dayofweek[shipDate.getDay()].toLowerCase()); }
		/* document.write("? Order in the next ");
		if (shipHours > 0) {
			document.write(shipHours+" hour");
			if (shipHours > 0) { document.write("s"); }
			}
		if (shipHours > 0 && shipHours < 9) { document.write(" and "); }
		if (shipHours < 9) {
			document.write(shipMinutes+" minute");
			if (shipMinutes > 0) { document.write("s"); }
			}
		document.write(" and choose FedEx Standard Overnight"); */
	} else {
		// not available - write out availability message
		document.write("<a href='http://www.booqbags.com/helpdesk/Ordering-Shipping/When-will-my-backordered-item-ship_2' class='not available'>Out of stock, available "+availability+"</a>");
	}
}
