/* --- Swazz Javascript Calendar ---
/* --- v 1.0 3rd November 2006
By Oliver Bryant
http://calendar.swazz.org */
/* --- alterred to handle blackouts
By Jim Mallmann */

/* blackout dates for  ticket requests on /benefits-training/ticket_request.asp */
var blackoutDatesTRQ = new Array(27);
 blackoutDatesTRQ[0] = "20100601";
 blackoutDatesTRQ[1] = "20100602";
 blackoutDatesTRQ[2] = "20100603";
 blackoutDatesTRQ[3] = "20100604";
 blackoutDatesTRQ[4] = "20100605";
 blackoutDatesTRQ[5] = "20100606";
 blackoutDatesTRQ[6] = "20100607";
 blackoutDatesTRQ[7] = "20100608";
 blackoutDatesTRQ[8] = "20100609";
 blackoutDatesTRQ[9] = "20100610";
blackoutDatesTRQ[10] = "20100611";
blackoutDatesTRQ[11] = "20100612";
blackoutDatesTRQ[12] = "20100613";
blackoutDatesTRQ[13] = "20100614";
blackoutDatesTRQ[14] = "20100615";
blackoutDatesTRQ[15] = "20100616";
blackoutDatesTRQ[16] = "20100617";


function Blackout1Month_GetObject(objID)
{
    if (document.getElementById) 
	{
		return document.getElementById(objID);
	}
    else if (document.all) 
	{
		return document.all[objID];
	}
    else if (document.layers) 
	{
		return document.layers[objID];
	}
	else
	{
		return MM_findObj(objID);
	}
}

// not used???
function Blackout1Month_CheckClickEvent(e) 
{
	e?evt=e:evt=event;
	CSE=evt.target?evt.target:evt.srcElement;
	if (Blackout1Month_GetObject('calendarWithBlackouts2Months'))
		if (!Blackout1Month_IsChildObject(CSE,Blackout1Month_GetObject('calendarWithBlackouts2Months')))
			Blackout1Month_GetObject('calendarWithBlackouts2Months').style.display='none';
}

// only used in above unused call
function Blackout1Month_IsChildObject(s,d) 
{
	while(s) 
	{
		if (s==d)
		{
			return true;
		}
		s=s.parentNode;
	}
	return false;
}

function Blackout1Month_SetLeft(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
	{
		curleft += obj.x;
	}
	return curleft+20;
}

function Blackout1Month_SetTop(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
	{
		curtop += obj.y;
	}
	return curtop;
}

// begin writing calendar layer
document.write('<table id="calendarWithBlackouts2Months" style="position:absolute;border-collapse:collapse;background:#FFFFFF;border-left:1px solid #ABABAB; border-right:1px solid #ABABAB; display:none;" cellpadding=2>');
document.write('<tr><td style="border-top:1px solid #ABABAB; cursor:pointer;" onclick="Blackout1Month_ChangeToPrevMonth()"><img src="../images/arrowleftmonth.gif"></td><td colspan=5 id="monthNameTDB2M" align="center" style="font:bold 13px Arial; border-top:1px solid #ABABAB;"></td><td align="right" style="border-top:1px solid #ABABAB; cursor:pointer;" onclick="Blackout1Month_ChangeToNextMonth()"><img src="../images/arrowrightmonth.gif"></td></tr>');
document.write('<tr><td align=center style="background:#ABABAB;font:12px Arial">S</td><td align=center style="background:#ABABAB;font:12px Arial">M</td><td align=center style="background:#ABABAB;font:12px Arial">T</td><td align=center style="background:#ABABAB;font:12px Arial">W</td><td align=center style="background:#ABABAB;font:12px Arial">T</td><td align=center style="background:#ABABAB;font:12px Arial">F</td><td align=center style="background:#ABABAB;font:12px Arial; border-right:1px solid #ABABAB;">S</td></tr>');
for(var kk = 1; kk <= 6; kk++) 
{
	document.write('<tr>');
	for(var tt = 1; tt <= 7; tt++) 
	{
		var num = 7 * (kk-1) - (-tt);
		document.write('<td id="cwb2M' + num + '" style="width:18px;height:18px">&nbsp;</td>');
	}
	document.write('</tr>');
}
document.write('</table>');

// this adds an event listener that causes any click outside of the calendar to hide the calendar
document.all?document.attachEvent('onclick',Blackout1Month_CheckClickEvent):document.addEventListener('click',Blackout1Month_CheckClickEvent,false);
// end writing calendar layer


// Calendar script
var blackout1month_globalDaysOut = 1; // Correct value for /benefits-training/ticket_request.asp
var blackout1month_globalNow = new Date;
var blackout1month_globalStartDate = new Date;
blackout1month_globalStartDate.setDate(blackout1month_globalNow.getDate() + blackout1month_globalDaysOut);
var blackout1month_globalStartMonth = 5;  // remember, JS months are one less than actual months
var blackout1month_globalStartYear = 2010;
//var globalStartDayOfMonth = globalStartDate.getDate();
var blackout1month_globalCurrentMonth = 5; //blackout1month_globalNow.getMonth();
var blackout1month_globalCurrentYear = 2010; //blackout1month_globalNow.getFullYear();
var blackout1month_globalEndDate = new Date();
blackout1month_globalEndDate.setFullYear(2011, (1-1), 1);
var blackout1month_globalEndMonth = blackout1month_globalEndDate.getMonth();
var blackout1month_globalEndYear = blackout1month_globalEndDate.getFullYear();

var blackout1month_globalDateTextBoxObject;
function Blackout1Month_LoadCalendar(dateTextBox) 
{
	blackout1month_globalDateTextBoxObject = dateTextBox;
	Blackout1Month_GetObject('calendarWithBlackouts2Months').style.left = Blackout1Month_SetLeft(dateTextBox);
	Blackout1Month_GetObject('calendarWithBlackouts2Months').style.top = Blackout1Month_SetTop(dateTextBox) + dateTextBox.offsetHeight;
	Blackout1Month_GetObject('calendarWithBlackouts2Months').style.display = '';
//	Blackout1Month_GetObject('dd1').style.display='none';
	
	// First check date is valid
	currentDate = dateTextBox.value;
	currentDateArray = currentDate.split('/');
	isDate = true;
	for(var k = 0; k < currentDateArray.length; k++) 
	{
		if (isNaN(currentDateArray[k]))
		{
			isDate=false;
		}
	}
	if (isDate & (currentDateArray.length==3)) 
	{
		currentMonth = currentDateArray[0]-1;
		currentDay = currentDateArray[1];
		currentYear = currentDateArray[2];
		Blackout1Month_PrepCalendar(currentDay, currentMonth, currentYear);
	}
	else
	{
		Blackout1Month_PrepCalendar('', blackout1month_globalStartMonth, blackout1month_globalStartYear);
	}
}

function Blackout1Month_SetEventTarget(e)
{
	var el;
	if(e.target) el=e.target;
	else if(e.srcElement) el=e.srcElement;
	if(el.nodeType==3) el=el.parentNode; // defeat Safari bug
	return el;
}

function Blackout1Month_GrabEventObject(e)
{
	if(!e) e=window.event;
	return e;
}

// ### LEFT OFF HERE RENAMING FUNCTIONS TO HAVE "Blackout1Month_" PREFIX ###
function Blackout1Month_AddMouseOverEventToTarget(e) 
{
	Blackout1Month_SetEventTarget(Blackout1Month_GrabEventObject(e)).style.background='#FFCC66';
}

function Blackout1Month_AddMouseOutEventToTarget(e) 
{
	Blackout1Month_SetEventTarget(Blackout1Month_GrabEventObject(e)).style.background='#C4D3EA';
}

function Blackout1Month_AddOnClickEventToDaySquareFirstCalendar(e) 
{
	// set date textbox value
	// REMEMBER:	changes to the ID prefix for each calendar square need to be reflected 
	// 				in the start index of the substring of this call
	blackout1month_globalDateTextBoxObject.value = firstCalendarB2MSquareArray[Blackout1Month_SetEventTarget(Blackout1Month_GrabEventObject(e)).id.substring(5,Blackout1Month_SetEventTarget(Blackout1Month_GrabEventObject(e)).id.length)];
	// re-hide calendar
	Blackout1Month_GetObject('calendarWithBlackouts2Months').style.display = 'none';
//	CallAjaxToLoadTimes();
}

var monthAbbrArray = new Array('JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC');
var normalLastDayOfMonthArray = new Array('31','28','31','30','31','30','31','31','30','31','30','31');
var leapYearLastDayOfMonthArray = new Array('31','29','31','30','31','30','31','31','30','31','30','31');
var firstCalendarB2MSquareArray = new Array(42);
var secondCalendarB2MSquareArray = new Array(42);

function Blackout1Month_SetDayAvailableStyle(obj) 
{
	obj.style.background='#C4D3EA';
	obj.style.font='10px Arial';
	obj.style.color='#333333';
	obj.style.textAlign='center';
	obj.style.textDecoration='none';
	obj.style.border='1px solid #6487AE';
	obj.style.cursor='pointer';
}

function Blackout1Month_SetDayNotAvailableStyle(obj) 
{
	obj.style.background='#C4D3EA';
	obj.style.font='10px Arial';
	obj.style.color='#ABABAB';
	obj.style.textAlign='center';
	obj.style.textDecoration='line-through';
	obj.style.border='1px solid #6487AE';
	obj.style.cursor='default';
}

function Blackout1Month_SetDaySelectedStyle(obj) 
{
	obj.style.background='#FFF799';
	obj.style.font='bold 10px Arial';
	obj.style.color='#333333';
	obj.style.textAlign='center';
	obj.style.border='1px solid #6487AE';
	obj.style.cursor='pointer';
}

function Blackout1Month_Convert_YYYYMMDD_ToDate(yyyymmdd)
{
	if(!isNaN(parseInt(yyyymmdd)) && yyyymmdd.length == 8)
	{
		if(yyyymmdd.substr(4,1) == "0")
		{
			return new Date(yyyymmdd.substr(0,4), parseInt(yyyymmdd.substr(5,1))-1, yyyymmdd.substr(6,2));
		}
		else
		{
			return new Date(yyyymmdd.substr(0,4), parseInt(yyyymmdd.substr(4,2))-1, yyyymmdd.substr(6,2));
		}
	}
	else
	{
		return new Date();
	}
}

// day selected
function Blackout1Month_PrepCalendar(theDay, theMonth, theYear) 
{
	var tomorrow = new Date();
	
	// create date that represents tomorrow
	tomorrow.setDate(tomorrow.getDate() + blackout1month_globalDaysOut);

	// create date that represents first day of first calendar
	var firstDayOfFirstMonth = new Date(blackout1month_globalCurrentYear, blackout1month_globalCurrentMonth, 1);
	// returns a number that's 0-6, where 0 = Sunday and 6 = Saturday
	var firstDayOfMonthDayOfWeekNum = firstDayOfFirstMonth.getDay();
	var firstActiveDatePossible = firstDayOfFirstMonth;
	if(tomorrow > firstDayOfFirstMonth)
	{
		firstActiveDatePossible = tomorrow;
	}
	
	Blackout1Month_GetObject('monthNameTDB2M').innerHTML = monthAbbrArray[blackout1month_globalCurrentMonth]+ ' ' + blackout1month_globalCurrentYear;
	var thisLastDayOfMonthArray = ((theYear % 4) == 0)?leapYearLastDayOfMonthArray:normalLastDayOfMonthArray;

	var firstSquareOfTheMonth = firstDayOfMonthDayOfWeekNum + 1;
	var lastSquareOfTheMonth = firstDayOfMonthDayOfWeekNum + parseInt(thisLastDayOfMonthArray[firstDayOfFirstMonth.getMonth()]);
	for(var d = 1; d <= 42; d++) 
	{
		var thisDayOfTheMonth = d - firstDayOfMonthDayOfWeekNum;

		Blackout1Month_SetDayAvailableStyle(Blackout1Month_GetObject('cwb2M'+parseInt(d)));

		//  first test: d >= firstSquareOfTheMonth
		// 				testing to see that we've at least reached the first of the month
		// second text: d <= lastSquareOfTheMonth
		// 				testing to see that we haven't passed the last day of the month
		if ((d >= firstSquareOfTheMonth) && (d <= lastSquareOfTheMonth)) 
		{
			//  first test: thisDayOfTheMonth < firstActiveDatePossible.getDate()
			// 				testing to see that the day of the first active date possible hasn't been passed
			// second text: blackout1month_globalCurrentMonth == firstActiveDatePossible.getMonth()
			// 				testing to see that the month of the first active date possible hasn't been passed
			var dateIsNotAvailable = ((thisDayOfTheMonth < firstActiveDatePossible.getDate()) && (blackout1month_globalStartMonth == firstActiveDatePossible.getMonth()));
			var dateIsSelected = false;
			var dateIsBlackedOut = false;
			if(!dateIsNotAvailable)
			{
				// check blackout array
				var tempDate = new Date(blackout1month_globalCurrentYear, blackout1month_globalCurrentMonth, thisDayOfTheMonth);
				var numBlackoutDates = blackoutDatesTRQ.length;
				var displayFirstBlackoutDate = true;
				for(var i = 0; i <= numBlackoutDates-1; i++)
				{
					var thisDateString = blackoutDatesTRQ[i];
					var thisBlackoutDate = Blackout1Month_Convert_YYYYMMDD_ToDate(thisDateString);
					displayFirstBlackoutDate = false;
					if(tempDate.getDate() == thisBlackoutDate.getDate() && tempDate.getMonth() == thisBlackoutDate.getMonth() && tempDate.getYear() == thisBlackoutDate.getYear())
					{
						dateIsBlackedOut = true;
						break;
					}
				}
				if(!dateIsBlackedOut)
				{
					//  first test: theDay != ''
					// 				testing to see that a selected day was passed in
					// second text: thisDayOfTheMonth == theDay)
					// 				testing to see that the day we're testing is the selected day that was passed in
					//  third test: firstDayOfFirstMonth.getMonth() == theMonth
					// 				testing to see that the month for this calendar matches the selected month that was passed in
					dateIsSelected = ((theDay != '') && (thisDayOfTheMonth == theDay) && (firstDayOfFirstMonth.getMonth() == theMonth));
				}
			}
			var thisDate = new Date();
			thisDate.setFullYear(theYear, theMonth, thisDayOfTheMonth);
			var endDateHasPassed = (thisDate >= blackout1month_globalEndDate);
			if(dateIsNotAvailable || dateIsBlackedOut || endDateHasPassed)
			{
				Blackout1Month_SetDayNotAvailableStyle(Blackout1Month_GetObject('cwb2M'+parseInt(d)));
			}
			else if(dateIsSelected)
			{
				Blackout1Month_SetDaySelectedStyle(Blackout1Month_GetObject('cwb2M'+parseInt(d)));
			}
			else if(!dateIsBlackedOut)
			{
				Blackout1Month_SetDayAvailableStyle(Blackout1Month_GetObject('cwb2M'+parseInt(d)));
			}

			Blackout1Month_GetObject('cwb2M' + parseInt(d)).onmouseover = (dateIsNotAvailable || dateIsBlackedOut)?null:Blackout1Month_AddMouseOverEventToTarget;
			Blackout1Month_GetObject('cwb2M' + parseInt(d)).onmouseout = (dateIsNotAvailable || dateIsBlackedOut)?null:Blackout1Month_AddMouseOutEventToTarget;
			Blackout1Month_GetObject('cwb2M' + parseInt(d)).onclick = (dateIsNotAvailable || dateIsBlackedOut)?null:Blackout1Month_AddOnClickEventToDaySquareFirstCalendar;
			Blackout1Month_GetObject('cwb2M' + parseInt(d)).innerHTML = thisDayOfTheMonth;	

			firstCalendarB2MSquareArray[d] = '' + (blackout1month_globalCurrentMonth - (-1)) + '/' + ((thisDayOfTheMonth)) + '/' + blackout1month_globalCurrentYear;
		}
		else 
		{
			Blackout1Month_GetObject('cwb2M'+d).innerHTML='&nbsp;';
			Blackout1Month_GetObject('cwb2M'+parseInt(d)).onmouseover=null;
			Blackout1Month_GetObject('cwb2M'+parseInt(d)).onmouseout=null;
			Blackout1Month_GetObject('cwb2M'+parseInt(d)).style.cursor='default';
		}
	}
}

//Blackout1Month_PrepCalendar('', blackout1month_globalStartMonth, blackout1month_globalStartYear);
//Blackout1Month_GetObject('calendarWithBlackouts2Months'+cc).style.visibility='hidden';

function Blackout1Month_ChangeToNextMonth() 
{
	thisLastDayOfMonthArray=((blackout1month_globalCurrentYear%4)==0)?leapYearLastDayOfMonthArray:normalLastDayOfMonthArray;
	
	blackout1month_globalCurrentMonth += 1;
	if (blackout1month_globalCurrentMonth >= 12) 
	{
		blackout1month_globalCurrentMonth=0;
		blackout1month_globalCurrentYear++;
	}
	Blackout1Month_DayOfMonthCheck();
	Blackout1Month_PrepCalendar('', blackout1month_globalCurrentMonth, blackout1month_globalCurrentYear);
}

function Blackout1Month_ChangeToPrevMonth() 
{
	thisLastDayOfMonthArray=((blackout1month_globalCurrentYear%4)==0)?leapYearLastDayOfMonthArray:normalLastDayOfMonthArray;
	
	blackout1month_globalCurrentMonth-=1;
	if (blackout1month_globalCurrentMonth < 0) 
	{
		blackout1month_globalCurrentMonth=11;
		blackout1month_globalCurrentYear--;
	}
	Blackout1Month_DayOfMonthCheck();
	Blackout1Month_PrepCalendar('', blackout1month_globalCurrentMonth, blackout1month_globalCurrentYear);
}

function Blackout1Month_DayOfMonthCheck() 
{
	if ((blackout1month_globalCurrentYear > blackout1month_globalStartYear) || ((blackout1month_globalCurrentYear == blackout1month_globalStartYear) && (blackout1month_globalCurrentMonth >= blackout1month_globalStartMonth)))
	{
		if(blackout1month_globalCurrentYear == 2011 && blackout1month_globalCurrentMonth == 0)
		{
			//alert("trying to go to January 2010");
			//alert("blackout1month_globalCurrentYear: " + blackout1month_globalCurrentYear 
			//+ "\nblackout1month_globalEndYear: " + blackout1month_globalEndYear
			//+ "\nblackout1month_globalCurrentMonth: " + blackout1month_globalCurrentMonth
			//+ "\nblackout1month_globalEndMonth: " + blackout1month_globalEndMonth
			//+ "\nblackout1month_globalEndDate.getDate(): " + blackout1month_globalEndDate.getDate());
		}
		if ((blackout1month_globalCurrentYear < blackout1month_globalEndYear) 
			|| ((blackout1month_globalCurrentYear == blackout1month_globalEndYear) && (blackout1month_globalCurrentMonth < blackout1month_globalEndMonth)) 
			|| ((blackout1month_globalCurrentYear == blackout1month_globalEndYear) && (blackout1month_globalCurrentMonth == blackout1month_globalEndMonth) && blackout1month_globalEndDate.getDate() > 1)
			)
		{
			return;
		}
		else
		{
			var stopAtYear = blackout1month_globalEndYear;
			var stopAtMonth = blackout1month_globalEndMonth;
			if(blackout1month_globalEndDate.getDate() == 1)
			{
				stopAtMonth--;
				if(stopAtMonth < 0)
				{
					stopAtMonth = 11;
					stopAtYear--;
				}
			}
			blackout1month_globalCurrentYear = stopAtYear;
			blackout1month_globalCurrentMonth = stopAtMonth;
		}
	}
	else
	{
		blackout1month_globalCurrentYear = blackout1month_globalStartYear;
		blackout1month_globalCurrentMonth = blackout1month_globalStartMonth;
	}
//		cfd=globalStartDayOfMonth;
}
