/*

	Script file for the calendar in the search form.
	
*/



//var calendar_FirstDayOfWeek = 0; // Monday is the first
/*
var calendar_MonthName = [
	"January", "February", 
	"March", "April", "May", 
	"June", "July", "August",
	"Septemper", "October", "November", 
	"December" ];
*/	
var calendar_FormName = "frmSearch";
var calendar_DateFieldName = "date";
var calendar_PeriodFieldName = "period";


var calendar_SelectedDate = null;

function calendar_SetDate( date )
{
	if( date == calendar_SelectedDate )
		return;

	// Remove old highlight
	if( calendar_SelectedDate != null ) 
	{
		var oTdOld = document.getElementById( "search-calendar." + calendar_DateToString( calendar_SelectedDate ) );
		if( oTdOld != null )
			oTdOld.className = "day";
	}
	
	// Highlight the new selected date
	if( date != null ) 
	{
		var oTdNew = document.getElementById( "search-calendar." + calendar_DateToString( date ) );
		oTdNew.className = "selected-day";
	}
	
	// Change date in the input field
	var oField = document.forms[ calendar_FormName ][ calendar_DateFieldName ];
	var oPeriodField = document.forms[ calendar_FormName ][ calendar_PeriodFieldName ];
	if( date != null )
	{
		oField.value = calendar_DateToString( date );
		if( oPeriodField.disabled )
		{
			oPeriodField.disabled = false;
		}
	}
	else 
	{
		oField.value = "";
		oPeriodField.disabled = true;
	}
		
	// Set old date
	calendar_SelectedDate = date;
}

function calendar_NumToString( num, length )
{
	var s = String( num );
	while( s.length < length )
		s = "0" + s;
	return s;
}

function calendar_DateToString( date )
{
	return String( date.getFullYear() ) 
		+ "-" + calendar_NumToString( date.getMonth() + 1, 2 ) 
		+ "-" + calendar_NumToString( date.getDate(), 2 );
}

function calendar_FillMonth( date )
{
	var oCal = document.getElementById( "search-calendar" );
	var aBodies = oCal.getElementsByTagName( "tbody" );
	var aDays = aBodies[1];
	
	// Remove old TDs
	for( var i = aDays.firstChild; i != null; i = i.nextSibling )
	{
		calendar_RemoveChildNodes( i );
	}
	
	// Get first day of month
	var dFirstDay = new Date( date.getFullYear(), date.getMonth(), 1 );
	var nFirstDayOfWeek = dFirstDay.getDay();
	if( nFirstDayOfWeek == 0 )
		nFirstDayOfWeek = 7;
	nFirstDayOfWeek--; // Monday == 0
	
	var dLastDay = new Date( date.getFullYear(), date.getMonth() + 1, 0 );
	var nLastDay = dLastDay.getDate();
	
	var aDaysRows = aDays.getElementsByTagName( "tr" );
	var nCurrentRow = 0;

	// Fill body with TDs
	for( var i = 0; i < 42; i++ )
	{
		var tagTd = document.createElement( "td" );
		
		var tdClass = "";
		if( i >= nFirstDayOfWeek && i < nLastDay + nFirstDayOfWeek ) 
		{
			var nCurDay = i - nFirstDayOfWeek + 1;
			
			// Create <a>
			var tagA = document.createElement( "a" );
			if ( i%7 > 4 )
			{
				tagA.className = "weekend" ;
			}
			tagA.setAttribute( "href", "#" );
			tagA.onclick = calender_MakeSetDayFunction( new Date( date.getFullYear(), date.getMonth(), nCurDay ) );
			
			// Insert text
			var text = document.createTextNode( nCurDay );
			tagA.appendChild( text );
			
			// Append to <td>
			tagTd.appendChild( tagA );

			// Assign Id to this td
			tagTd.id = "search-calendar." + String( date.getFullYear() ) 
				+ "-" + calendar_NumToString( date.getMonth() + 1, 2 ) 
				+ "-" + calendar_NumToString( nCurDay, 2 );
				
			tdClass = "day";
		}
		else
		{
			var text = document.createTextNode( String.fromCharCode( 160 ) );
			tagTd.appendChild( text );
			tdClass = "empty";
		}

		tagTd.setAttribute( "class", tdClass );
		aDaysRows[nCurrentRow].appendChild( tagTd );
		
		if( ( i + 1 ) % 7 == 0 ) 
			nCurrentRow++;
	}
	
	// Write month
	var oMonth = document.getElementById( "search-calendar.month" );
	calendar_RemoveChildNodes( oMonth );
	var sMonth = Months[date.getMonth()] + " " + date.getFullYear();
	oMonth.appendChild( document.createTextNode( sMonth ) );
	
	var oA;
	// Change left arrow
	oA = document.getElementById( "search-calendar.left" );
	oA.onclick = function GoLeft() 
	{
		var d = new Date( date.getFullYear(), date.getMonth() - 1, 1 )
		calendar_FillMonth( d );
		calendar_SetDate( d );
		return false;
	}

	// Change right arrow
	oA = document.getElementById( "search-calendar.right" );
	oA.onclick = function GoRight() 
	{
		var d = new Date( date.getFullYear(), date.getMonth() + 1, 1 );
		calendar_FillMonth( d );
		calendar_SetDate( d );
		return false;
	}
}

function calendar_RemoveChildNodes( node )
{
	while( node.firstChild != null )
		node.removeChild( node.firstChild );
}

function calender_MakeSetDayFunction( date )
{
	return function()
	{
		calendar_SetDate( date );
		return false;
	}
}

