/**********************************************************************
HTML CALENDAR CONTROL
copyright (c) 2001 The Ashley Group, Ltd.
ALL RIGHTS RESERVED
**********************************************************************

Created By:     Patrick Milliken
Creation Date:  5/15/2001

Purpose:        The Calendar Control is a pop-up calendar that writes
				a user-selected date back to a specified text box on
				the calling form. The calling form opens a pop-up
				window, then this code dynamically writes the calendar
				HTML to the window. No separate Calendar.HTM exists
				for this control.

Modified By:    Patrick Milliken
Last Mod Date:  6/11/2001

**********************************************************************/

/*--------------------------------------------------------------
 Declare and initialize global variables
----------------------------------------------------------------*/
var ActiveDate
var theMonth
var theDate
var theYear
var theFirst
var firstDay

/*--------------------------------------------------------------
 Build the HTML for the calendar based on the date argument then
 write the HTML to the pop-up window.
----------------------------------------------------------------*/
function doCalendar(ActiveDate)
	{
	Today = new Date()
	theDate = ActiveDate.getDate()
	theMonth = ActiveDate.getMonth() + 1
	theYear = ActiveDate.getFullYear()
	FOM = new Date(theMonth + '/1/' + theYear)
	firstDay = FOM.getDay()
	lastDay = getLastDay(theMonth, theYear)
	ColWidth = 35
	
	// Build the Header.
	
	HTML = '<html><head><title>Calendar</title>'
		+ '<link rel="stylesheet" href="cal_htm.css" type="text/css">'
		+ '<style type="text/css">a {text-decoration:none;} a:hover {color:#CC0000; text-decoration:underline;}</style></head>'
		+ '<body bgcolor="#000000" text="#000000" link="#000066" vlink="#000066" '
		+ 'alink="#CC0000" topmargin="0" leftmargin="0" marginheight="0" marginwidth="0">'
		+ '<form action="calendar.htm" method="post" name="frmForm">'
		+ '<table border="0" cellspacing="1" cellpadding="1" align="center">'
		+ '<tr>'
		+ '<td colspan="7" bgcolor="#003399">'
		+ '<table width="100%" border="0" cellspacing="0" cellpadding="0" height="16">'
		+ '<tr>'
		+ '<td align="left" valign="middle" width="25%" nowrap>'
		+ '<a href="javascript:opener.changeYear(\'' + ActiveDate + '\', -1)" title="Go To Last Year"><span class="HeaderCell">&lt;&lt;</span></a><span class="HeaderCell">&nbsp;&nbsp;</span>'
		+ '<a href="javascript:opener.changeMonth(\'' + ActiveDate + '\', -1)" title="Go To Last Month"><span class="HeaderCell">&lt;</span></a></td>'
		+ '<td align="center" valign="middle">'
		+ '<span class="HeaderCell">' + returnMonth(theMonth) + ' ' + theYear + '</span></td>'
		+ '<td align="right" valign="middle" width="25%" nowrap>'
		+ '<a href="javascript:opener.doToday()" title="Go To Today"><span class="HeaderCell">^</span></a><span class="HeaderCell">&nbsp;&nbsp;</span>'
		+ '<a href="javascript:opener.changeMonth(\'' + ActiveDate + '\', 1)" title="Go To Next Month"><span class="HeaderCell">&gt;</span></a><span class="HeaderCell">&nbsp;&nbsp;</span>'
		+ '<a href="javascript:opener.changeYear(\'' + ActiveDate + '\', 1)" title="Go To Next Year"><span class="HeaderCell">&gt;&gt;</span></a></td>'
        + '</tr>'
		+ '</table>'
		+ '</td>'
		+ '</tr>'
		+ '<tr>'
		+ '<th width="' + ColWidth + '" bgcolor="#CCCCCC" align="center"><span class="BodyCell"><b>S</b></span></th>'
		+ '<th width="' + ColWidth + '" bgcolor="#CCCCCC" align="center"><span class="BodyCell"><b>M</b></font></th>'
		+ '<th width="' + ColWidth + '" bgcolor="#CCCCCC" align="center"><span class="BodyCell"><b>T</b></span></th>'
		+ '<th width="' + ColWidth + '" bgcolor="#CCCCCC" align="center"><span class="BodyCell"><b>W</b></span></th>'
		+ '<th width="' + ColWidth + '" bgcolor="#CCCCCC" align="center"><span class="BodyCell"><b>T</b></span></th>'
		+ '<th width="' + ColWidth + '" bgcolor="#CCCCCC" align="center"><span class="BodyCell"><b>F</b></span></th>'
		+ '<th width="' + ColWidth + '" bgcolor="#CCCCCC" align="center"><span class="BodyCell"><b>S</b></span></th>'
		+ '</tr>'
	
	// Build the Body.

	HTML += '<tr align="center" valign="middle">'
	
	for (i = 1; i <= 5; i++)
		{
		for (j = 1; j <= 7; j++)
			{
			theDay = (j + ((i - 1) * 7) - firstDay)
			if (theDay < 1 && firstDay + lastDay > 35)
				{
				theDay = (35 - firstDay) + j
				}
			if (theDay < 1 || theDay > lastDay)
				HTML += '<td width="' + ColWidth + '" height="22" bgcolor="#CCCCCC">&nbsp;</td>'
			else
				{
				if (theMonth + "/" + theDay + "/" + theYear == getDate(UserDate) && getDate(UserDate) != getDate(Today))
					HTML += '<td width="' + ColWidth + '" height="22" bgcolor="#FFFFFF"><span class="UserDate">' + theDay + '</span></td>'
				else
					{
					HTML += '<td width="' + ColWidth + '" height="22" width="27" bgcolor="#FFFFFF"><a href="javascript:opener.ReturnValue(\'' + theDay + "/" + theMonth + "/" + theYear + '\')">'
					if (theMonth == Today.getMonth() + 1 && theDay == Today.getDate() && theYear == Today.getFullYear())
						HTML += '<span class="Today">' + theDay + '</span></a></td>'
					else
						HTML += '<span class="BodyCell">' + theDay + '</span></a></td>'
					}
				}
			}
		if (i != 5)
			HTML += '</tr><tr align="center" valign="middle">'
		}
	
	// Build the Footer.
	
	HTML += '</tr></table>'
		+ '<table width="100%" bgcolor="#003399" cellpadding="3" cellspacing="0" border="0">'
		+ '<tr><td align="center" valign="middle">'
		+ '<span class="Address">&copy; 2002-2005 </span><a href="http://www.cybernetindia.com" target="_blank">'
		+ '<span class="Address">cybernetindia Icons pvt. ltd.</span></a>'
		+ '</td></tr></table>'
		+ '<input type="hidden" name="ActiveDate" value="' + ActiveDate + '">'
		+ '</form></body></html>'
	
	// Write the complete HTML to the pop-up window.
	
	nWin.document.open("text/html")
	nWin.document.write(HTML)
	nWin.document.close()
	}
	
/*--------------------------------------------------------------
 Return a date formatted as MM/DD/YYYY.
----------------------------------------------------------------*/
function getDate(DateVal)
	{
	return(DateVal.getDate() + "/" + (DateVal.getMonth()+1) + "/" + DateVal.getFullYear())
	}
	
/*--------------------------------------------------------------
 Handle a click on the last or next month links.
----------------------------------------------------------------*/
function changeMonth(ActiveDate, offset)
	{
	ActiveDate = new Date(ActiveDate)
	theMonth = ActiveDate.getMonth() + (1 + offset)
	theYear = ActiveDate.getFullYear()
	if (theMonth < 1)
		{
		theMonth = 12
		theYear--
		}
	else if (theMonth > 12)
		{
		theMonth = 1
		theYear++
		}
	ActiveDate = new Date(theMonth + '/' + ActiveDate.getDate() + '/' + theYear)
	doCalendar(ActiveDate)
	}

/*--------------------------------------------------------------
 Handle a click on the last or next year links.
----------------------------------------------------------------*/
function changeYear(ActiveDate, offset)
	{
	ActiveDate = new Date(ActiveDate)
	theMonth = ActiveDate.getMonth() + 1
	theYear = ActiveDate.getFullYear() + (1 * offset)
	ActiveDate = new Date(theMonth + '/' + ActiveDate.getDate() + '/' + theYear)
	doCalendar(ActiveDate)
	}
	
/*--------------------------------------------------------------
 Handle a click on the Go To Today link.
----------------------------------------------------------------*/
function doToday()
	{
	ActiveDate = new Date()
	doCalendar(ActiveDate)
	}

/*--------------------------------------------------------------
 Return the last day of the month for the specified month and
 year (note the leap year calculation).
----------------------------------------------------------------*/
function getLastDay(theMonth, theYear)
	{
	if (theMonth != 2)
		{
		if (theMonth == 4 || theMonth == 6 || theMonth == 9 || theMonth == 11)
			return(30)
		else
			return(31)
		}
	else
		{
		if (theYear % 4 > 0)
			return(28)
		else if (theYear % 100 > 0)
			return(29)
		else if (theYear % 4 == 0)
			return(29)
		else
			return(28)
		}
	}
	
/*--------------------------------------------------------------
 Return the name of the month based on the month's numeric
 value.
----------------------------------------------------------------*/
function returnMonth(theMonth)
	{
	if (theMonth == 1)
		return('January')
	else if (theMonth == 2)
		return('February')
	else if (theMonth == 3)
		return('March')
	else if (theMonth == 4)
		return('April')
	else if (theMonth == 5)
		return('May')
	else if (theMonth == 6)
		return('June')
	else if (theMonth == 7)
		return('July')
	else if (theMonth == 8)
		return('August')
	else if (theMonth == 9)
		return('September')
	else if (theMonth == 10)
		return('October')
	else if (theMonth == 11)
		return('November')
	else if (theMonth == 12)
		return('December')
	}

/*--------------------------------------------------------------
 When a user clicks on a day of the month, write the date value
 back to the specified text box on the calling form.
----------------------------------------------------------------*/
function ReturnValue(theDate)
   {
	document.forms[0].elements[ReturnEl].value = theDate
	CloseNWin()
   }   
   
