/*
	Javascript Calendar Functions
	by WA2UZO@arrl.net
	
	Use freely but please give credit.
*/

/************************************************************************
                                                                        
	IsMajorHoliday()     
	
	Returns TRUE if the date is a major holiday.                                                     
                                                                        
************************************************************************/

function IsMajorHoliday(date, month)
{
	return false;

	// This function needs some work but not now...
	
	// New Years
	if (month == 1 && date == 1)
		return true;
	// July 4th
	if (month == 7 && date == 4)
		return true;
	// XMas eve
	if (month == 12 && date == 24)
		return true;
	// XMas
	if (month == 12 && date == 25)
		return true;
		
	return false;
}

/************************************************************************
                                                                        
	DateIsAfter()     
	
	Returns TRUE if the date passed in has passed.                                                     
                                                                        
************************************************************************/

function DateIsAfter(date, month, year)
{
	d = new Date();
	
	if (year < FY4N(d.getYear()))
		return true;
	
	// getMonth in JavaScript returns January as 0, not 1
	if ((year <= FY4N(d.getYear())) && (month-1 < d.getMonth()))
		return true;
		
	if ((year <= FY4N(d.getYear())) && (month-1 <= d.getMonth()) && (date < d.getDate()))
		return true;
	
	return false;
}

/************************************************************************
                                                                        
	IsToday()     
	
	Returns TRUE if the date passed in is today.                                                     
                                                                        
************************************************************************/

function IsToday(date, month, year)
{
	d = new Date();
	// getMonth in JavaScript returns January as 0, not 1
	return (date == d.getDate()) && (month-1 == d.getMonth()) && (year == d.getYear());
}

/************************************************************************
                                                                        
	IsValidDate()                                                          
                                                                        
	Given a day, month, year input, IsValidDate() will return TRUE if the  
	date is valid and FALSE if not.                                        
                                                                        
************************************************************************/

function IsValidDate(date, month, year) 
{
	if ((year > 0) && 
   	 	(month > 0) && (month <= 12) &&
      	(date > 0) && (date <= DaysInMonth(month, year)))
      	return true;
	return false;
}

/************************************************************************
                                                                        
	IsLeapYear()                                                          
                                                                        
	Returns TRUE if the year is a leap year.     
                                                                        
************************************************************************/

function IsLeapYear(year)
{
 	if ((year % 400) == 0) {
 		return true;
 	} else if (((year % 4) == 0) && ((year % 100) != 0)) {
 		return true;
 	}
 	return false;
}

/************************************************************************
                                                                        
	DaysInMonth()                                                          
                                                                        
	Given a month (range JANUARY..DECEMBER <1..12>) and a year,            
	DaysInMonth() will return the number of days in the input month. Works 
	for leap years. If the input month/year is invalid, 0 is returned.     
                                                                        
************************************************************************/

function DaysInMonth(month, year) 
{ 	
	if (year <= 0) 
    	return 0;
   
    if ((month < 1) || (month > 12))
    	return 0;

    switch (month) {
	    case 1: return 31;
	    case 2:
	        if (IsLeapYear(year)) {
	        	return 29;
	        } else {
	        	return 28;
	        }
	    case 3: return 31;
	    case 4: return 30;
	    case 5: return 31;
	    case 6: return 30;
	    case 7: return 31;
	    case 8: return 31;
	    case 9: return 30;
	    case 10: return 31;
	    case 11: return 30;
	    case 12: return 31;
        	break;
   }
   return 0;
}

/************************************************************************
                                                                        
	DaysInYear()                                                           
                                                                        
	Given an input year, DaysInYear() will compute the number of days in   
	the year by calculating the number of days in each month of the year   
	and summing the values. Probably could be done a little faster, but if 
	DaysInMonth() ever changes, those changes will be reflected here with  
	no additional coding! If year is invalid, 0 is returned.               
                                                                        
************************************************************************/

function DaysInYear(year) 
{
	if (year <= 0)
	 	return 0;
	 	
	var daysInYear = 0;
	var month = 1;
	while (month <= 12) {
		daysInYear += DaysInMonth(month, year);
		month++;
	}
		
   	return daysInYear;
}

/************************************************************************
                                                                        
	FirstDayOfYear()                                                      
                                                                        
	Returns the first day of the year as 0 (Sunday)..6 (Saturday) 
	                                                                        
************************************************************************/

function FirstDayOfYear(year) {
	return ((((year - 1900) + (year - 1900 + 3) / 4)) % 7)
}

/************************************************************************
                                                                        
	FirstDayOfMonth()                                                      
                                                                        
	Given a valid month in theMonth (JANUARY..DECEMBER <1..12>),           
	FirstDayOfMonth() will return the actual day (SUNDAY..SATURDAY <0..6>) 
	that theMonth starts on. If month/year is invalid, 0 is returned.      
                                                                        
************************************************************************/

function FirstDayOfMonth(month, year) 
{
    var monthIndex = 0;
    var totalDays = 0;

    if (year <= 0)
   		return 0;

   	if ((month < 1) || (month > 12))
   		return 0;

   	for (monthIndex = 0; monthIndex < month; monthIndex++)
   		totalDays += DaysInMonth(monthIndex, year);
   
  	return Math.floor((FirstDayOfYear(year) + totalDays) % 7); // fix8/10 added math.floor to fix january 2002 which came back as 2.25
}

/************************************************************************
                                                                        
	DayOfMonth()                                                           
                                                                        
	Given a valid input date, DayOfMonth() will return the actual day      
	(SUNDAY..SATURDAY <0..6>). If invalid date, -1 is returned.            
                                                                        
************************************************************************/

function DayOfMonth(date, month, year)
{
   	return((IsValidDate(date, month, year) == false) ? (-1) :
           ((FirstDayOfMonth(month, year) + (date - 1)) % 7));
}

/************************************************************************
                                                                        
	IndexOf()                                                           
                                                                        
	Returns the index of the day in the current month. That is, if you pass
	11 JULY 2001 since the 11th is the SECOND Wednesday, it will return 2.            
                                                                        
************************************************************************/

function IndexOf(date)
{
	return Math.ceil(date / 7);
}

/************************************************************************
                                                                        
	DateOf()                                                           
                                                                        
	Returns the date of the month of the given index, day, month, year.
	That is, DateOf(1, 2, 7, 2001) will return the 3, the DATE of the FIRST
	TUESDAY in JULY 2001. Returns 0 if out of range.
                                                                        
************************************************************************/

function DateOf(index, day, month, year)
{
   	if ((day < 0) || (day > 6))
   		return 0;
   		
   	if ((month < 1) || (month > 12))
   		return 0;

    if (year <= 0)
   		return 0;

	var daysInMonth = DaysInMonth(month, year);
	
   	for (var dateIndex = 1; dateIndex <= daysInMonth; dateIndex++) {
   		if ((IndexOf(dateIndex) == index) && (DayOfMonth(dateIndex, month, year) == day)) {
   			return dateIndex;
   		}
	}
	
	return 0;
}

/************************************************************************
                                                                        
	GetMonthName()      
	
	Returns the name of the month.                                                     
                                                                        
************************************************************************/

function GetMonthName(month)
{
    switch (month) {
	    case 1: return "January";
	    case 2: return "February";
	    case 3: return "March";
	    case 4: return "April";
	    case 5: return "May";
	    case 6: return "June";
	    case 7: return "July";
	    case 8: return "August";
	    case 9: return "September";
	    case 10: return "October";
	    case 11: return "November";
	    case 12: return "December";
   }
   return "";
}

/************************************************************************
                                                                        
	GetDayName()      
	
	Returns the name of the day.                                                     
                                                                        
************************************************************************/

function GetDayName(day)
{
    switch (day) {
	    case 0: return "Sunday";
	    case 1: return "Monday";
	    case 2: return "Tuesday";
	    case 3: return "Wednesday";
	    case 4: return "Thursday";
	    case 5: return "Friday";
	    case 6: return "Saturday";
   }
   return "";
}

/************************************************************************
                                                                        
	AS() (AbbreviateString)   
	
	Return the first 3 characters of a string (abbreviate it).                                                     
                                                                        
************************************************************************/

function AS(str)
{
	var s = new String(str)
	return s.substr(0,3);
}

/************************************************************************
                                                                        
	FY4N (FixYearForNetscape)   
	
	Netscape is really lame in the sense that it returns 101 for the year 2001.
	This function helps fix Netscape's shortcoming.                                                     
                                                                        
************************************************************************/

function FY4N(year)
{
	if (year < 1900)
		return year+1900;
	else
		return year;
}