/* -----   Steps to add calendar events.
 
1. Find 'script' folder. 
 
2. Open calendar.js in Notepad.
 
3. Find commented script - "Change Anchor tag here".
 
4. Change Day, Year & month of the event.
    For example = (dayNo == '23' && pYear=='2007' && pMonth =='2')  it means ( event date is 23, Year is 2007 & Month is 2nd - means Feb. )
 
5. If you want to add 3-4 events in a month then you have to copy the following whole tag & paste it one by one. Like : 
 
    if(dayNo == '23' && pYear=='2007' && pMonth =='2')
       {
        td.style.backgroundColor = "yellow";
         td.style.color = "red";
         td.style.fontWeight = "bold";
       }
 
6. Find commented script - "Add Location Here". 
 
7. There also have to do the same changes as above.
 
8. Then please find "calendar.html" file.
 
9. There you have to add event details. Like:
    
Marathi Vishwa Shri Ganeshotsav

Saturday, 9th September '06

Venue: West Windsor Plainsboro high School North 90 Grovor's Mill Road, Plainsboro, NJ 08536

Male singer: Shri Nrendra Datar.
Female Singer: Ms. Samidha Joglekar
Harmonium: Raya Bidye
Tabla: Shri. Kishor Kulkarni
Samalochan: Shri. Nana Datar

Admission: FREE for MV members, Non-members and Guests $ 20, Children (Age 5-10) $ 10
Baby sitting available after 2:00 PM for children ages 3 and above, $ 10 per child
RSVP is required for lunch as well as for baby-sitting. 
 
10. Please find the line - <a name="23" id="23"></a>
 
11. Here you have to add your event date instead of this 23.  */ 

		var DayInMilliseconds = 24 * 60 * 60 * 1000;
		var TodaysDate = new Date();
		var SelectedDate = TodaysDate;
		var OriginalDate = TodaysDate;
		var ColumnDayMapping = Array( 1, 2, 3, 4, 5, 6, 0 ); // 0-Monday ... 6-Sunday
		var previousYear = SelectedDate.getFullYear();


//-----------------------------------------------------------------------------------//-->
//--------------       Show the calendar for the given month    -----------/////////////-->
//--------------       Created By : - Vaibhav                   ---------- ////////////-->
//--------------       Created Date :-  17/1/07                 -----------////////////-->
/ --------------       Retrun : ----                            -----------//////////-->
//-----------------------------------------------------------------------------------//-->

		 function ShowCalendar(pYear, pMonth, pMonthDay)
		 {
		 	// Valid entries only
		 	pYear  = parseInt( pYear );  pYear = (isNaN( pYear ) || pYear < 1599 || pYear > 2999) ? TodaysDate.getFullYear() : 			pYear;
		 	pMonth = parseInt( pMonth ); pMonth = isNaN( pMonth ) ? (TodaysDate.getMonth() + 1) : pMonth;
		 	pMonthDay = parseInt( pMonthDay ); pMonthDay = isNaN( pMonthDay ) ? TodaysDate.getDate() : pMonthDay;

			previousYear = pYear;

		 	SelectedDate = new Date();
			SelectedDate.setFullYear( pYear, pMonth - 1, pMonthDay );
			document.frmDateSelection.theDate.value = formatDate( SelectedDate );

		 	// Get first and last days of the month
		 	var FirstDayOfMonth = new Date(), FirstDayOfNextMonth = new Date(), LastDayOfMonth = new Date();
		 	FirstDayOfMonth.setFullYear( pYear, pMonth - 1, 1 );

		 	if(pMonth < 12) FirstDayOfNextMonth.setFullYear( pYear, pMonth, 1 );
		 	else FirstDayOfNextMonth.setFullYear( pYear + 1, 0, 1 );

		 	LastDayOfMonth.setTime( FirstDayOfNextMonth.getTime() - DayInMilliseconds );

		 	// Loop over TD's and set their values accordingly
		 	var dayNo = 0;
		 	for( var r = 0; r < 6; r++ )
		 	{
		 		for(var c = 0; c < 7; c++)
		 		{
		 			var td = document.getElementById("GridTD_" + r + "_" + c);
		 			// Clean up the td
		 			td.innerHTML = "&nbsp;";
					td.style.backgroundColor = "";
					
 					 
 					
 					td.style.color = "white";
 					td.style.fontWeight = "normal";
					

		 			// If not started counting, check for start
		 			if( dayNo == 0 )
		 				if ( FirstDayOfMonth.getDay() == ColumnDayMapping[c] )
		 					dayNo = 1;

		 			if( dayNo > 0 && dayNo <= LastDayOfMonth.getDate() )
		 			{
		 				td.innerHTML = "<a class='text' href='javascript:void(0);' 												onclick='javascript:ShowCalendar("+pYear+","+pMonth+","+dayNo+")'>"+ dayNo + "</a>";

						//======================================================================================//-->
						//-    Change Anchor tag here (Change value only If condition) --------/////////////-->
						//------------------------------------------------------------------------- ////////////-->
						//------   dayNo = Day ; pYear = Year ; pMonth = Month
						//========================================================================////////////-->
						
							if(dayNo == '23' && pYear=='2007' && pMonth =='2')
							{
								td.style.backgroundColor = "#FFFFFF";
	 							td.style.color = "red";
	 							td.style.fontWeight = "bold";
							}
							/*if(dayNo == '9' && pYear=='2007' && pMonth =='3')
							{
								td.style.backgroundColor = "yellow";
	 							td.style.color = "red";
	 							td.style.fontWeight = "bold";
							}*/

						// ====================================================================================////-->
						//=====================================================================================//-->
					
						
		 				if( (TodaysDate.getFullYear() == pYear) && (TodaysDate.getMonth() == pMonth - 1) && 									(TodaysDate.getDate() == dayNo) )
		 				{
 							td.style.fontWeight = "bold";
		 				}

		 				if( pMonthDay == dayNo )
		 				{
							
 							td.style.backgroundColor = "yellow";
 							td.style.color = "red";
 							td.style.fontWeight = "bold";

						//======================================================================================//-->
						//-    Add Location Here    (Change value only If condition)   --------/////////////-->
						//--------------------------------------------------------------------//////////////-->
						//     Please confirm above if condition value is same 
						//------------------------------------------------------------------------- ////////////-->
						//------   dayNo = Day ; pYear = Year ; pMonth = Month
						//========================================================================////////////-->

							if(dayNo == '23' && pYear=='2007' && pMonth =='2')
								{
								location.href = "calendar.htm#"+ 														(dayNo);
								return false;

								}
							/*if(dayNo == '9' && pYear=='2007' && pMonth =='3')
								{
								location.href = "calendar.htm#"+ 														(dayNo);
								return false;

								}*/

						// ====================================================================================////-->
						//=====================================================================================//-->

		 				}
		 						 				
		 				dayNo++;
		 			}
		 		}
		 	}
		 }


//-----------------------------------------------------------------------------------//-->
//--------------       Returned Date format                     -----------/////////////-->
//--------------       Created By : - Vaibhav                   ---------- ////////////-->
//--------------       Created Date :-  17/1/07                 -----------////////////-->
/ --------------       Retrun : Date Format                     -----------//////////-->
//-----------------------------------------------------------------------------------//-->

		 function formatDate( dateObject )
		 {
		 	var pMonth = dateObject.getMonth() + 1;
		 	var pMonthDay = dateObject.getDate();
		 	var pYear = dateObject.getFullYear();

		 	// Show day, month and year readings
		 	var disp_Month = (pMonth < 10) ? "0" + pMonth : pMonth;
		 	var disp_Day = (pMonthDay  < 10) ? "0" + pMonthDay  : pMonthDay;
		 	document.frmDateSelection.month.selectedIndex = pMonth - 1;
		 	document.frmDateSelection.year.value = pYear;
		 	return disp_Day + "/" + disp_Month + "/" + pYear;
		 }

	
//-----------------------------------------------------------------------------------//-->
//--------------       Month Changed                            -----------/////////////-->
//--------------       Created By : - Vaibhav                   ---------- ////////////-->
//--------------       Created Date :-  17/1/07                 -----------////////////-->
/ --------------       Retrun : Change Month                    -----------//////////-->
//-----------------------------------------------------------------------------------//-->

		 function MonthOrYearChanged()
		 {
			ShowCalendar( document.frmDateSelection.year.value, document.frmDateSelection.month.selectedIndex + 1, 1 );
		 }


//-----------------------------------------------------------------------------------//-->
//--------------       Year Changed                             --------/////////////-->
//--------------       Created By : - Vaibhav                   ---------- ////////////-->
//--------------       Created Date :-  17/1/07                 -----------////////////-->
/ --------------       Retrun : Change Year                     -----------//////////-->
//-----------------------------------------------------------------------------------//-->

		function YearChanged( howMuch )
		{
			document.frmDateSelection.year.value = parseInt( document.frmDateSelection.year.value ) + parseInt( howMuch );
			MonthOrYearChanged();
		}


//-----------------------------------------------------------------------------------//-->
//--------------      Parse a date string and return the object -----------/////////////-->
//--------------       Created By : - Vaibhav                   ---------- ////////////-->
//--------------       Created Date :-  17/1/07                 -----------////////////-->
/ --------------       Retrun : Date                            -----------//////////-->
//-----------------------------------------------------------------------------------//-->

		function getDateObject( dateString )
		{
			var arr = Array();
			var valid = false;
			var day = -1, month = -1, year = -1;

			// dd/mm/yyyy format
			if( dateString.indexOf("/") > 0 )
			{
				valid = true;
				arr   = dateString.split("/");
				day = parseInt(arr[0],10);
				if(arr.length > 0) month = parseInt(arr[1],10);
				if(arr.length > 1) year  = parseInt(arr[2],10);
			}
			// yyyy-mm-dd
			else if( dateString.indexOf("-") > 0 )
			{
				valid = true;
				arr   = dateString.split("-");
				day = parseInt(arr[2],10);
				if(arr.length > 0) month = parseInt(arr[1],10);
				if(arr.length > 1) year  = parseInt(arr[0],10);
			}

			if(! valid ) return TodaysDate;

			day = parseInt(arr[0],10);
			if(arr.length > 0) month = parseInt(arr[1],10);
			if(arr.length > 1) year  = parseInt(arr[2],10);

			var theDate = new Date();

			theDate.setFullYear( year, month - 1, day );
			
			return theDate;
		}

	
//-----------------------------------------------------------------------------------//-->
//--------------       Initialize The Calendar               -----------/////////////-->
//--------------       Created By : - Vaibhav                ---------- ////////////-->
//--------------       Created Date :-  17/1/07              -----------////////////-->
/ --------------       Retrun : Calendar                     -----------//////////-->
//-----------------------------------------------------------------------------------//-->

		function Initialize( fromDateObj )
		{
			previousYear = SelectedDate.getFullYear();
			
			if( ! fromDateObj )
			{
				var validSelected = false;
				try
				{
					if(window.opener != null)
						SelectedDate = opener.GetDateSelectorDate();
					if( typeof(SelectedDate) == "object" )
					{
						validSelected = true;
					}
					else
					{
						SelectedDate = getDateObject( SelectedDate );
						validSelected = true;
					}
				}
				catch( ex )
				{
					validSelected = false;
				}
				if( ! validSelected ) SelectedDate = TodaysDate;
				
				OriginalDate = SelectedDate;
			}
			else
			{
				SelectedDate = fromDateObj;
			}
			
			document.frmDateSelection.year.focus();
			
			ShowCalendar( SelectedDate.getFullYear(), SelectedDate.getMonth() + 1, SelectedDate.getDate() );
						
		}
//-----------------------------------------------------------------------------------//-->
//--------------       Display Date                          ----------/////////////-->
//--------------       Created By : - Vaibhav                ---------- ////////////-->
//--------------       Created Date :-  16/1/07              -----------////////////-->
/ --------------       Retrun : Date                         -----------//////////-->
//-----------------------------------------------------------------------------------//-->

		function returnDateString()
		{
			try
			{
				if(window.opener != null)
					opener.SetDateSelectorDate( document.frmDateSelection.theDate.value );
			}
			catch( ex )
			{
				alert("Wrong use of returnDateString() function\n" + ex.toString());
			}
			window.close();
		}
		
	 
