/************************************************************************** 
*************************************************************************** 
*  Program Name: $Id: calendar_control.js,v 1.20 2007/08/25 23:21:54 neo Exp $
*  Program Author:  Michael T. Schock
*  Creation Date: 05-15-2006
*  CVS Revision: $Revision: 1.20 $
*  Copyright (c) 2006
*************************************************************************** 
*************************************************************************** 
*  Program Summary:
*  Javascript functions for the calendar class
*
*
*************************************************************************** 
**************************************************************************/
//  Function to populate the calendar
function Populate_Calendar(month, year, controlNumber)
{
	//  Variables
	var tempDate;
	var dateString;
	var startDay;
	var dayNumber;
	var loop;
	var divName;
	
	//  Create a date string for the 1st day of the month
	dateString = month + ' 1, ' + year;
	
	//  Get the date object for the dateString
	tempDate = new Date(dateString);
	
	//  Determine the start day for the month
	startDay = tempDate.getDay();
	
	//  Get number of days in the month
	dayNumber = Get_Month_Days(month, year);
	
	//  Clear the data from the days of the month
	Clear_Days(controlNumber);
	
	//  Loop and populate the days of the month
	for(loop = (startDay + 1); loop <= (dayNumber+startDay); loop++)
	{
		//  Get Div Name
		divName = 'Calendar_' + loop + '_' + controlNumber;
		
		//  Fill Div with date
		document.getElementById(divName).innerHTML = (loop - startDay);
	}

	//  Fill the blank days with 'nbsp'
	Fill_Blanks(controlNumber);

	//  Populate the Year
	document.getElementById('Calendar_Year' + '_' + controlNumber).innerHTML = year;
	
	//  Populate the Month
	document.getElementById('Calendar_Month' + '_' + controlNumber).innerHTML = month;
}

//  Function to clear the text from the Div's for the calendar
function Clear_Days(controlNumber)
{
	//  Variables
	var loop;

	//  Loop and clear all the days on the calendar
	for(loop = 1; loop <= 42; loop++)
	{
		//  Get Div Name
		divName = 'Calendar_' + loop + '_' + controlNumber;

		//  Fill Div with date
		document.getElementById(divName).innerHTML = "";
	}
}

//  Function to fill the blank days with nbsp
function Fill_Blanks(controlNumber)
{
	//  Variables
	var loop;
	var dayData;

	//  Loop and clear all the days on the calendar
	for(loop = 1; loop <= 42; loop++)
	{
		//  Get Div Name
		divName = 'Calendar_' + loop + '_' + controlNumber;

		//  Get the data
		dayDate = document.getElementById(divName).innerHTML;

		//  If the data is blank, fill with a nbsp
		if(dayDate == "")
			document.getElementById(divName).innerHTML = "<b>&nbsp</b>";
	}
}

//  Function to return the number of days in a given month.  (also accounts for leap year)
function Get_Month_Days(month, year)
{
	//  Variables
	var daysNumeric = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var loop;
	var monthIndex;
	var leapFlag;
	
	//  Set variables
	leapFlag = 0;
	monthIndex = 0;
	
	//  Determine the index
	for(loop = 0; loop < 12; loop++)
	{
		if(month == textMonth[loop])
		{
			monthIndex = loop;
		}
	}
	
	//  If February, check for leap year
	if(monthIndex == 1)
	{
		leapFlag = Check_Leap_Year(year);
		if(leapFlag == 1)
		{
			daysNumeric[1] = 29;
		}
	}
	
	//  Return the number of days
	return(daysNumeric[monthIndex]);
	
}

//  Function to check if a given year is a leap year.
function Check_Leap_Year(year)
{
	//  Variables
	var returnCode = 0;
	
	//  Look for the leap year
	if(year%4 == 0)
	{
		if(year%100 == 0)
		{
			if(year%400 == 0)
			{
				returnCode = 1;
			}
		}
		else
		{
			returnCode = 1;
		}
	}
	
	//  Send back leap status
	return(returnCode);
}

//  Function to populate the 'Date' text box, when a date is pressed on the calendar.
function Create_Date(divObject, controlNumber, pageElement)
{
	//  Variables
	var month;
	var year;
	var day;
	var dateString;
	var pageElement;
	var pageElementValue;
	var newDate = new Date();
	var dateSeconds;

	//  Get the Day
	day = divObject.innerHTML;

	//  Get the Month
	month = document.getElementById('Calendar_Month' + '_' + controlNumber).innerHTML;
	
	//  Get the Year
	year = document.getElementById('Calendar_Year' + '_' + controlNumber).innerHTML;
	
	//  Build string
	dateString = month + ' ' +day + ', ' + year;
	
	//  Convert the date
	dateSeconds = Date.parse(dateString);
	newDate.setTime(dateSeconds);
	
	controlMonth = newDate.getMonth() + 1;
	if(controlMonth < 10)
		controlMonth = '0' + controlMonth;
		
	controlDay = newDate.getDate();
	if(controlDay < 10)
		controlDay = '0' + controlDay;
		
	controlYear = newDate.getFullYear();
	
	//  Put the string into the date field
	if(day == "<b>&nbsp;</b>" || day == "<B>&nbsp;</B>")
	{
		document.getElementById(pageElement).value = '';
		document.getElementById('Calendar_Hidden_Month' + '_' + controlNumber).value = '';
		document.getElementById('Calendar_Hidden_Year' + '_' + controlNumber).value = '';
	}
	else
	{
		document.getElementById(pageElement).value = controlYear + '-' + controlMonth + '-' + controlDay;
		document.getElementById('Calendar_Hidden_Month' + '_' + controlNumber).value = month;
		document.getElementById('Calendar_Hidden_Year' + '_' + controlNumber).value = year;
	}

}

//  Function to go back 1 month.
function Decrement_Month(controlNumber)
{
	//  Variables
	var month;
	var year;
	var monthIndex;
	
	//  Get the current month and year from the calendar
	month = document.getElementById('Calendar_Month' + '_' + controlNumber).innerHTML;
	year = document.getElementById('Calendar_Year' + '_' + controlNumber).innerHTML;
	
	//  Determine the index
	for(loop = 0; loop < 12; loop++)
	{
		if(month == textMonth[loop])
		{
			monthIndex = loop;
		}
	}
	
	//  Check to see if the year needs to be rolled back
	if(monthIndex == 0)
	{
		//  Decrement the year
		year--;
		
		//  Set the month index to 'December'
		monthIndex = 11; 
	}
	else
	{
		//  Decrement the month index
		monthIndex--;
	}
	
	//  Write the month and year to the calendar
	Populate_Calendar(textMonth[monthIndex], year, controlNumber);
	
}

//  Function to go forward 1 month.
function Increment_Month(controlNumber)
{
	//  Variables
	var month;
	var year;
	var monthIndex;
	
	//  Get the current month and year from the calendar
	month = document.getElementById('Calendar_Month' + '_' + controlNumber).innerHTML;
	year = document.getElementById('Calendar_Year' + '_' + controlNumber).innerHTML;
	
	//  Determine the index
	for(loop = 0; loop < 12; loop++)
	{
		if(month == textMonth[loop])
		{
			monthIndex = loop;
		}
	}
	
	//  Check to see if the year needs to be rolled forward
	if(monthIndex == 11)
	{
		//  Decrement the year
		year++;
		
		//  Set the month index to 'January'
		monthIndex = 0; 
	}
	else
	{
		//  Decrement the month index
		monthIndex++;
	}
	
	//  Write the month and year to the calendar
	Populate_Calendar(textMonth[monthIndex], year, controlNumber);
}






