/***************************
* Copyright (c) GrocerySavings.ca
* All Rights Reserved.
*
* Author: Ben Cho
* Website: grocerysavings.ca, localgrocerydeals.com
* CopyRight: 2007
*
* Utility file responsible for managing dates and times
****************************/

/*
* for no frills the sale starts on thursday and ends on the following wednesday 
*
* for all other grocery stores sale starts on saturday and ends on the following friday
* @returns	returns the appropriate expiry date depending today's date
*			if today is Sunday to wednesday return Wednesday
			if today is Thursday to Friday return Friday
*/
function getCookieExpiryDate()
{
	var SUNDAY = 0;
	var WEDNESDAY = 3;
	var THURSDAY = 4;
	var FRIDAY = 5;
	var SATURDAY = 6;

	// today's date
	var currentDate = new Date();
	// get the day part of today's date
	var today = currentDate.getDay();
	// the date to return back to the caller
	var returnDate = currentDate;

	// get the difference in days between wednesday and today
	var dayDiffFromWednesday = WEDNESDAY - today;

	// get the difference in days between wednesday and today
	var dayDiffFromThursday = THURSDAY - today;

	// get the difference in days between friday and today
	var dayDiffFromFriday = FRIDAY - today;


	// START - set expire date to this wednesday
	// after sunday, WEDNESDAY is the first day to expire all the saved grocery shopping list and print grocery list carts
	// > 0 => Sunday -> Wednesday
	if (dayDiffFromWednesday >= SUNDAY)
	{
		returnDate.setDate(returnDate.getDate() + dayDiffFromWednesday);
	}

	// today is saturday
	if (today == SATURDAY)
	{
		returnDate.setDate(returnDate.getDate() + 4);
	}
	// END - set expire date to this wednesday
	// START - set expire date to this thursday
	else if (dayDiffFromThursday >= 0 && dayDiffFromThursday <= 1)
	{
		returnDate.setDate(returnDate.getDate() + dayDiffFromThursday);
	}
	// END - set expire date to this thursday
	// START - set expire date to this friday
	else if (dayDiffFromFriday >= 0 && dayDiffFromFriday <= 1)
	{
		returnDate.setDate(returnDate.getDate() + dayDiffFromFriday);
	}
	// END - set expire date to this friday

	// set the time to one second before midnight
	returnDate = setOneSecBeforeMidnight(returnDate);

	return returnDate;
}

/*
* sets the passed in date to 11:59 59 pm (one second before midnight)
*/
function setOneSecBeforeMidnight(thisDate)
{
	thisDate.setHours(23);
	thisDate.setMinutes(59);
	thisDate.setSeconds(59);

	return thisDate;
}

/*
* get a date for the coming friday 11:59:59 pm
*/
function getFridayExpiryDate()
{
	var FRIDAY = 5;
	var SATURDAY = 6;
	var SUNDAY = 7;

	// today's date
	var currentDate = new Date();

	// get the day part of today's date
	var today = currentDate.getDay();

	returnDate = currentDate;

	// get the difference in days between friday and today
	var dayDiffFromFriday = 0;
	if (today == SATURDAY)
	{
		dayDiffFromFriday = 6;
	}
	else if (today == SUNDAY)
	{
		dayDiffFromFriday = 5;
	}
	else
	{
		dayDiffFromFriday = FRIDAY - today;
	}

	returnDate.setDate(currentDate.getDate() + dayDiffFromFriday);

	// set the time to one second before midnight
	returnDate = setOneSecBeforeMidnight(returnDate);

	return returnDate;
}

// returns the int of the month
function getMonthInNum(strMonth)
{
var monthArray = new Array();
	monthArray['Jan'] = 1;
	monthArray['Feb'] = 2;
	monthArray['Mar'] = 3;
	monthArray['Apr'] = 4;
	monthArray['May'] = 5;
	monthArray['Jun'] = 6;
	monthArray['Jul'] = 7;
	monthArray['Aug'] = 8;
	monthArray['Sep'] = 9;
	monthArray['Oct'] = 10;
	monthArray['Nov'] = 11;
	monthArray['Dec'] = 12;

	return monthArray[strMonth];
}
