/**********************************************************************
HTML CALENDAR CONTROL
**********************************************************************
**	
**	Created By:     Michael Barwell (based upon work by Patrick Milliken)
**	Creation Date:  1/3/2002
**	
**	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. 
**	
**	Modified By:    Michael Barwell
**					Altered to handle Australian (and European) date formats because all of the javascript
**					date parameters are mm/dd/yy
**	Last Mod Date:  1/3/2002
**  08/10/02	MB	Altered for ADGP
**	
**********************************************************************/

/*--------------------------------------------------------------
 Declare and initialize global variables
----------------------------------------------------------------*/
var ActiveDate
var theMonth
var theDate
var theYear
var theFirst
var firstDay
var FormNumber

/*--------------------------------------------------------------
 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)
//	FOM = new Date('1/' + theMonth + '/' + theYear)
	firstDay = FOM.getDay()
	lastDay = getLastDay(theMonth, theYear)
	// ColWidth = 35
	ColWidth = 25
	vBGColourTop = '#CC0000'
	vBGColourBottom = '#CC0000'

	// alert(theDate)
		// alert(theMonth)
		// alert(theYear)
		
	
	// Build the Header.
	
	HTML = '<html><head><title>Calendar</title>'
		+ '<link rel="stylesheet" href="/adgp_library/javascript/mbcal_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">'
		+ '<td colspan="7" bgcolor="' + vBGColourTop + '">'
		+ '<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))
//				if (theDay + "/" + theMonth + "/" + 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(\'' + theMonth + "/" + theDay + "/" + theYear + '\')">'
//					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="' + vBGColourBottom + '" cellpadding="3" cellspacing="0" border="0">'
		+ '<tr><td align="center" valign="middle">'
		+ '</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, DD/MM/YYYY + YYYY-MM-YY
----------------------------------------------------------------*/
function getDate(DateVal)
	{
			return((DateVal.getMonth() + 1) + "/" + DateVal.getDate() + "/" + 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 
		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)
   {
	// alert("vDateFormat =" + vDateFormat + "ReturnEl=" + ReturnEl + " The date=" + theDate)

	var vMonth = theDate.substring(0,theDate.indexOf("/"))
	var vDay = theDate.substring(theDate.indexOf("/")+1,theDate.lastIndexOf("/"))
	var vYear = theDate.substring(theDate.lastIndexOf("/")+1,theDate.length + 1)

	switch (vDateFormat)
	{
	case "DD/MM/YYYY":
		theDate = vDay + "/" + vMonth + "/" + vYear
		break;
	case "DD/MM/YY":
		theDate = vDay + "/" + vMonth + "/" + vYear.substr(2,2)
		break;
	case "YYYY-MM-DD":
		theDate = vYear + "-" + vMonth + "-" + vDay
		break;
	default:
		theDate = vMonth + "/" + vDay + "/" + vYear
	}

	document.forms["composeform"].elements[ReturnEl].value = theDate;
	if( (document.forms["composeform"].elements[ReturnEl].onkeypress) )
	{
		document.forms["composeform"].elements[ReturnEl].onkeypress();
	}
	if( (document.forms["composeform"].elements[ReturnEl].onclick) )
	{
	    document.forms["composeform"].elements[ReturnEl].onclick();
	}
	
	CloseNWin()
   }   
   
