// ------------------------------------------------------------------ //
//                                                                    //
//  Copyright (c) 2001-2002 Cascadia Information Technologies, Inc.   //
//                                                                    //
//  Cascadia. Building a Better Internet.(TM)                         //
//                                                                    //
// ------------------------------------------------------------------ //


// Declare and seed our globals
var monthname = ['January','February','March','April','May','June', 'July','August','September','October','November','December'];
var ms_in_day = 24 * 60 * 60 * 1000;
var cur_id;

function openCal(CAL)
{
	var calLayer = document.getElementById( CAL + '_div' );

	// Save the current cal so we know what it is..
	cur_id = CAL;

	// If there's already a calendar, clear it, if not draw it.
	if ( calLayer.innerHTML ) { calLayer.innerHTML = ''; }
	else                      { drawCal( CAL, 0 ); }

}

function drawCal( CAL, OFFSET )
{
	var input_date = getDate( eval( "window.document.mform." + CAL + ".value" ) );
	var draw_date;

	// Do we change the month??
	if      ( OFFSET ==  0 ) { draw_date = input_date; }
	else if ( OFFSET == -1 ) { draw_date = prev_month_last_date;  }
	else if ( OFFSET ==  1 ) { draw_date = next_month_first_date; }

	// Get the current date's information
	var month = draw_date.getMonth();
	var date  = draw_date.getDate();
	var year  = draw_date.getFullYear();

	// Calc some key dates
	var month_first_date = new Date( year, month, 1, 12 );
	var first_sun_date   = new Date( month_first_date.getTime() - ( ms_in_day * ( month_first_date.getDay() == 0 ? 7 : month_first_date.getDay() ) ) );

	prev_month_last_date  = new Date( month_first_date.getTime() - ms_in_day );
	next_month_first_date = new Date( year + ( month == 11 ? 1 : 0 ), ( month == 11 ? 0 : month + 1 ), 1 );

	// Start the table that holds the calendar
	var str =  '<table border="1" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="cursor:hand">' + "\n"
	+          '	<tr>' + "\n"
	+          '		<td width="100%">' + "\n"
	+          '			<table border="0" cellpadding="3" cellspacing="0" width="100%">' + "\n"
	+          '				<tr>' + "\n"
	+          '					<td height="19" class="calendar" align="left" valign="center"><a href="JavaScript:drawCal( cur_id, -1 )"><img src="graphics/leftarrow.gif" width="17" height="17" border="0"></a></td>' + "\n"
	+          '					<td height="19" class="calendar" align="center" valign="center">' + "\n"
	+          '						<font class="cal_month">' + monthname[month] + ' ' + year + '</font>' + "\n"
	+          '					</td>' + "\n"
	+          '					<td height="19" class="calendar" align="right" valign="center"><a href="JavaScript:drawCal( cur_id, 1 )"><img src="graphics/rightarrow.gif" width="17" height="17" border="0"></a></td>' + "\n"
	+          '				</tr>' + "\n"
	+          '			</table>' + "\n"
	+          '		</td>' + "\n"
	+          '	</tr>' + "\n"
	+          '	<tr>' + "\n"
	+          '		<td width="100%">' + "\n"
	+          '			<table border="0" cellpadding="3" cellspacing="0" width="100%">' + "\n"
	+          '				<tr>' + "\n"
	+          '					<td>' + "\n"
	+          '						<table border="0" cellpadding="0" cellspacing="1" width="154">' + "\n"
	+          '							<tr align="center">' + "\n"
	+          '								<td width="22" align="center"><font class="cal_day">S</font></td>' + "\n"
	+          '								<td width="22" align="center"><font class="cal_day">M</font></td>' + "\n"
	+          '								<td width="22" align="center"><font class="cal_day">T</font></td>' + "\n"
	+          '								<td width="22" align="center"><font class="cal_day">W</font></td>' + "\n"
	+          '								<td width="22" align="center"><font class="cal_day">T</font></td>' + "\n"
	+          '								<td width="22" align="center"><font class="cal_day">F</font></td>' + "\n"
	+          '								<td width="22" align="center"><font class="cal_day">S</font></td>' + "\n"
	+          '							</tr>' + "\n"
	+          '							<tr>' + "\n"
	+          '								<td colspan="7" bgcolor="#000000"><img src="/images/trans.gif" width="1" height="1" border="0"></td>' + "\n"
	+          '							</tr>' + "\n";


	// We need to start the calendar with the first Sunday, and define some variables
	var index_date = first_sun_date;
	var index_day;
	var calclass;
	var cur_date = new Date();

	// Loop through the six weeks that make up the calendar
	for( var week = 0; week < 6; week++ )
	{
		// Start the row
		str += '							<tr>' + "\n";

		// Loop through the 7 days in this week
		for( var day = 0; day < 7; day++ )
		{
			// Get the current day
			index_day = index_date.getDate();

			// See what class we should use for this cell
			if ( month == index_date.getMonth() )                           { calclass = 'cal_thismonth'; }
			if ( month != index_date.getMonth() )                           { calclass = 'cal_othermonth'; }

			if ( index_date.getFullYear() == input_date.getFullYear() &&
			     index_date.getMonth()    == input_date.getMonth() &&
				 index_day                == input_date.getDate() )         { calclass = 'cal_selectedday'; }

			if ( index_date.getFullYear() == cur_date.getFullYear() &&
			     index_date.getMonth()    == cur_date.getMonth() &&
				 index_day                == cur_date.getDate() )           { calclass = ( calclass == 'cal_selectedday' ) ? 'cal_selectedtoday' : 'cal_currentday';  }

			// Make the cell with the date
			str +=     '								<td align="right" class="' + calclass + '">' + "\n"
			+          '									<a href="JavaScript:SetDate( ' + "'" + CAL + "', '" + LinkDate(index_date) + "'" + ')" class="' + calclass + '">' + index_day + '</a>' + "\n"
			+          '								</td>' + "\n";

			// Increment by one day
			index_date = new Date( index_date.getTime() + ms_in_day );
		}

		// Close the row
		str += '							</tr>' + "\n";
	}

	// Make the 'today' button
	str += '							<tr>' + "\n"
	+      '								<td colspan="7" bgcolor="#000000"><img src="/images/trans.gif" width="1" height="1" border="0"></td>' + "\n"
	+      '							</tr>' + "\n"
	+      '							<tr>' + "\n"
	+      '								<td colspan="7" align="center">' + "\n"
	+      '									<table border="0" cellpadding="3" cellspacing="0">' + "\n"
	+      '										<tr>' + "\n"
	+      '											<td><input type="button" value=" Today " class="cal_today_button" onClick="SetDate( ' + "'" + CAL + "', '" + LinkDate(cur_date) + "'" + ')"></td>' + "\n"
	+      '										</tr>' + "\n"
	+      '									</table>' + "\n"
	+      '								</td>' + "\n"
	+      '							</tr>' + "\n";

	// Close the tables
	str += '						</table>' + "\n"
	+      '					</td>' + "\n"
	+      '				</tr>' + "\n"
	+      '			</table>' + "\n"
	+      '		</td>' + "\n"
	+      '	</tr>' + "\n"
	+      '</table>' + "\n";

	// Draw the calendar
	var calLayer = window.document.getElementById( CAL + '_div' );
	calLayer.innerHTML = str;
	return;
}

function getDate( str )
{
	var new_darray = str.split( "\/" );
	return( str.indexOf( "\/" ) != -1 ? new Date( new_darray[2], new_darray[0] - 1, new_darray[1] ) : new Date() );
}

function LinkDate( DATE )
{
	return ( DATE.getMonth() + 1 ) + '/' + DATE.getDate() + '/' + DATE.getFullYear();
}

function SetDate( CAL, VALUE )
{
	var field = eval( 'window.document.mform.' + CAL );
	field.value = VALUE;

	var calLayer = document.getElementById( CAL + '_div' );
	calLayer.innerHTML = '';
}



