/*   dater.js  this script creates a formatted date string for the first or fourth Friday of the month and displays it in the ManaREAL sidebar.
	  last updated 08/16/09   */
/*---------------Set Up Functions-------------------*/

function getFri(firstDay,calcDate){
//calculate date of first & third Fridays using 'firstDay' and calcDate values

	if(firstDay==0){ //Sunday
		firstFri=new Date(calcDate+(k_day*5));
		thirdFri=new Date(calcDate+(k_day*19));
	} //end if
	else if(firstDay==1){ //Monday
		firstFri=new Date(calcDate+(k_day*4));
		thirdFri=new Date(calcDate+(k_day*18));
	} //end else if
	else if(firstDay==2){ //Tuesday
		firstFri=new Date(calcDate+(k_day*3));
		thirdFri=new Date(calcDate+(k_day*17));
	} //end else if
	else if(firstDay==3){ //Wednesday
		firstFri=new Date(calcDate+(k_day*2));
		thirdFri=new Date(calcDate+(k_day*16));
	} //end else if
	else if(firstDay==4){ // Thursday
		firstFri=new Date(calcDate+k_day);
		thirdFri=new Date(calcDate+(k_day*15));
	} //end esle if
	else if(firstDay==5){ // Friday
		firstFri=new Date(calcDate);
		thirdFri=new Date(calcDate+(k_day*14));
	} // end else if
	else{ // Saturday
		firstFri=new Date(calcDate+(k_day*6));
		thirdFri=new Date(calcDate+(k_day*20));
	} // end else
	// determine which Friday to display
	if(today <= firstFri){
		then = firstFri;
	} //end if
	else if (today <=thirdFri){
		then = thirdFri;
	} // end else if
	return then;
} // end function

// create prototype to convert numeric month data to short month name
Date.prototype.getMonthName = function() {
   moname = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
	return moname[this.getMonth()].substr(0,3); 
} //end prototype

/*---------------Begin Code-------------------*/

// get today's date
var today=new Date();
// extract the month and year
var thisMonth=today.getMonth();
var thisYear=today.getFullYear();
// determine the first day of the current month/year
var firstDate=new Date(thisYear,thisMonth,1);
// extract the day of week (0-6)
var firstDay=firstDate.getDay();
// convert to numeric value
var calcDate=Date.parse(firstDate);
// set #of milliseconds per day constant
if(thisMonth==10){ //mo is Nov.
	var k_day=86400000+3600000; //hour added to accommodate Nov wackyness
} //end if
else{
	var k_day=86400000;
} //end else

// get first & third Friday dates
var then=getFri(firstDay,calcDate);
// if 'today' falls AFTER the 3rd Friday...
if(today > thirdFri){
	firstDate = new Date(thisYear,thisMonth+1,0); //get first day of next month (last day of this mo +1 day)
	firstDay = firstDate.getDay(); // get day of week
	calcDate=Date.parse(firstDate); //convert to number format
	then=getFri(firstDay,calcDate); // process for next Fri date using 'getDate()'
} //end if

//display results
date = then.getDate();
moname = then.getMonthName();
year = then.getFullYear();
display = moname+". "+date+", "+year;
document.write(display);
