<!--
//---------- Define Variables --------------------

var n = 0;
//N_WEEKS: the date will be n weeks from today (7 * n days).
var N_WEEKS = 1;

//N_WEEKS_WEEKDAY: the date will be n weeks from today, on a specific day of the week.
var N_WEEKS_WEEKDAY = 2;
var strDayOfWeek = "Saturday";

//MONTHLY: the date will be the last day of the month.
var MONTHLY = 3;

//N_MONTHLY: the date will be the next upcoming day in the array of values in arrDates[].
var N_MONTHLY = 4;
var arrDates = new Array(2);
arrDates[0] = 14;
arrDates[1] = 31;

//One of the values defined above.
var nRecurrence = N_MONTHLY;
//-----------------------------------------------

//-------- Do the work --------------------------
var promoDate = GetPromotionDate();
document.write('<center><span style=\"font-family: arial, sans-serif; font-size: 10pt; font-weight: bold;\">');
document.write(promoDate);
document.write('</center></span>');
//-----------------------------------------------

//-------- Functions ----------------------------

function GetPromotionDate()
{
    var promoDate = new Date();
    var nCurrentDay = promoDate.getDate();
    var nCurrentMonth = promoDate.getMonth();
    var nCurrentYear = promoDate.getFullYear();
    //promoDate.setDate(15);
    //promoDate.setMonth(0);
    
    switch (nRecurrence)
    {
    case N_WEEKS:
        promoDate = AddDays( promoDate, 7 * n );
        break;
    case N_WEEKS_WEEKDAY:
        promoDate = AddDays( promoDate, 7 * n );
        promoDate = GetNextDayOfWeek( GetDayOfWeekNumber( strDayOfWeek ), promoDate );
        break;
    case MONTHLY:
        promoDate.setDate( GetDaysInMonth( nCurrentMonth, nCurrentYear ) );
        break;
    case N_MONTHLY:
        promoDate = GetClosestUpcomingDate( arrDates, promoDate );
        break;
    default:
        promoDate.setDate( GetDaysInMonth( nCurrentMonth, nCurrentYear ) );
        break;   
    }

    return promoDate.toLocaleDateString();
}

// returns the number of days in the month, accounting for leap years. 
function GetDaysInMonth( nMonth, nYear )
{
    var nDays = 0;
    var arrMonth = new Array();
    arrMonth[0] = 31;
    arrMonth[1] = 28;
    arrMonth[2] = 31;
    arrMonth[3] = 30;
    arrMonth[4] = 31;
    arrMonth[5] = 30;
    arrMonth[6] = 31;
    arrMonth[7] = 31;
    arrMonth[8] = 30;
    arrMonth[9] = 31;
    arrMonth[10] = 30;
    arrMonth[11] = 31;
    
    nDays = arrMonth[nMonth];
    
    if ( (nMonth == 1) && (IsLeapYear( nYear )) )
        nDays = 29;
    
    return nDays;
}

//returns true if the year is a leap year
function IsLeapYear( nYear )
{
	if ( nYear % 4 == 0)
	{
		if ( nYear % 100 != 0)
		{
			return true;
		}
		else
		{
			if ( nYear % 400 == 0)
				return true;
			else
				return false;
		}
	}
    return false;
}

//Adds a number of days to the given date, changing the month and year if need be. 
function AddDays( date, nDaysToAdd )
{
    var nDay = date.getDate();
    var nMonth = date.getMonth();
    var nYear = date.getFullYear();
    var nDaysInMonth = GetDaysInMonth( nMonth, nYear );
    
    if ( (nDay + nDaysToAdd) > nDaysInMonth )
    {
        var nDifference = (nDaysInMonth - nDay) + 1;
        nDaysToAdd = nDaysToAdd - nDifference;
        date.setDate( 1 );
        nMonth = nMonth + 1;
        if ( nMonth > 11 )
        {
            nMonth = 0;
            date.setFullYear( nYear + 1 );
        }
        date.setMonth( nMonth );
        date = AddDays( date, nDaysToAdd );
    } else
        date.setDate( nDay + nDaysToAdd );
    
    return date;
}

//Returns the date of the next nDayOfWeek.
//For example, if nDayOfWeek is Saturday (5), and the date is Wednesday(3), March 29, 
// then the return value will be the next saturday: Saturday, April 1
function GetNextDayOfWeek( nDayOfWeek, date )
{
    var nDay = date.getDay();
    if ( nDayOfWeek <= nDay )
        nDayOfWeek = nDayOfWeek + 7;
    var nDaysToAdd = nDayOfWeek - nDay;
    
    date = AddDays( date, nDaysToAdd );
    return date;
}

// returns the number value of the given day of week.
function GetDayOfWeekNumber( strDay )
{
    strDay = strDay.toLowerCase();
    switch (strDay)
    {
    case "sunday":
        return 0;
        break;
    case "monday":
        return 1;
        break;
    case "tuesday":
        return 2;
        break;
    case "wednesday":
        return 3;
        break;
    case "thursday":
        return 4;
        break;
    case "friday":
        return 5;
        break;
    case "saturday":
        return 6;
        break;
    default:
        return 0;
        break;
    }
    
    return 0;
}

//Out of an array of dates, returns the date that is the closest upcoming date to the given date. 
//For instance, if arrDates has two values, 15 and 31, and the given date is Janurary 10, the return date will be January 15. 
function GetClosestUpcomingDate( arrDates, date )
{
    var nMinDays = 100;
    var nClosestDay = 1;
    var nDaysUntil = 0;
    //Find the closest date in the array.
    for (x in arrDates)
    {
        nDaysUntil = GetNumberOfDaysUntil( arrDates[x], date );
        if ( nDaysUntil < nMinDays )
        {
            nMinDays = nDaysUntil;
            nClosestDay = arrDates[x];
        }
    }
    
    if ( nClosestDay < date.getDate() )
    {
        //increment month
        var nMonth = date.getMonth() + 1;
        if ( nMonth > 11 )
        {
            nMonth = 0;
            date.setFullYear( date.getFullYear() + 1 );
        }
        date.setMonth( nMonth );
    }
    var nDaysInMonth = GetDaysInMonth( date.getMonth(), date.getYear() );
    if ( nClosestDay > nDaysInMonth )
        nClosestDay = nDaysInMonth;
    date.setDate( nClosestDay );
    return date;   
}

//Gets the number of days from the given date until the next nTargetDay, where nTargetDay 
// is a number between 1 and the number of days in a month. 
function GetNumberOfDaysUntil( nTargetDay, date )
{
    var nCurrentDay = date.getDate();
    var nCurrentMonth = date.getMonth();
    var nCurrentYear = date.getFullYear();
    var nDaysInMonth = GetDaysInMonth( nCurrentMonth, nCurrentYear );
    var nDaysUntil;
    
    if ( nTargetDay < nCurrentDay )
    {
        var nDaysLeftInMonth = nDaysInMonth - nCurrentDay;
        nDaysUntil = nDaysLeftInMonth + nTargetDay;
    }
    else
    {
        if (nTargetDay > nDaysInMonth)
            nTargetDay = nDaysInMonth;
        nDaysUntil = nTargetDay - nCurrentDay;
    }
    return nDaysUntil;
}
//---------------------------------------------------

// -->