var startDay;
var startMonth;
var startYear;

var endDay;
var endMonth;
var endYear;

var firstDate;
var firstId;
var secondDate;
var secondId;
var numClicks = 0;

function changeMonth( month, year )
{
	location = 'availability.php?month=' + month + '&year=' + year;
}

function selectDay( day, month, year )
{
	if( numClicks == 0 )
	{
		if( firstId != null )
		{
			document.getElementById(firstId).style.background = 'white';
			document.getElementById(firstId).style.color = '#036';
		}
		if( secondId != null )
		{
			document.getElementById(secondId).style.background = 'white';
			document.getElementById(secondId).style.color = '#036';
			shadeInterval( new Date( startYear, startMonth-1, startDay ), new Date( endYear, endMonth-1, endDay ), '#fff' );
		}
		startDay = day;
		startMonth = month;
		startYear = year;
		
		var el = document.getElementById('startDate');
		
		var startDate = new Date( year, month-1, day );
		el.innerHTML = startDate.toLocaleDateString();
		
		document.getElementById('endDate').innerHTML = '';

		firstId = day + ',' + month + ',' + year;
		var dateEl = document.getElementById(firstId);
		dateEl.style.background='#036';
		dateEl.style.color='#fff';

		numClicks = 1;
	}
	else
	{
		// if the second click is a date before the first
		if( ( Number(year) < Number(startYear) ) || 
			( ( Number(year) == Number(startYear ) ) && ( Number(month) < Number(startMonth) )) || 
			( ( Number(month) == Number(startMonth) ) && ( Number(day) < Number(startDay) ) ) )
		{
			endYear = startYear;
			endMonth = startMonth;
			endDay = startDay;
			startDay = day;
			startMonth = month;
			startYear = year;
		}
		else
		{
			endDay = day;
			endMonth = month;
			endYear = year;
		}
		
		secondDate = day + ", " + month;
		numClicks = 0;

		var startDate = new Date( startYear, startMonth-1, startDay );
		var endDate = new Date( endYear, endMonth-1, endDay );
		
		secondId = day + ',' + month + ',' + year;
		el = document.getElementById( secondId );
		el.style.background='#036';
		el.style.color='#fff';
		
		if( shadeInterval( startDate, endDate, '#98abbc' ) == true )
		{
			document.getElementById('startDate').innerHTML = startDate.toLocaleDateString();
			document.getElementById('endDate').innerHTML = endDate.toLocaleDateString();
			document.getElementById('start').value = startDate.getTime();
			document.getElementById('end').value = endDate.getTime();			
		}
		else
		{
			alert( "Unfortunately, the selected time period from " + startDate.toLocaleDateString() + " to " + endDate.toLocaleDateString() + " is unavailable. Please select again.");
			shadeInterval( startDate, endDate, '#fff' );
			document.getElementById(firstId).style.background = 'white';
			document.getElementById(firstId).style.color = '#036';
			document.getElementById(secondId).style.background = 'white';
			document.getElementById(secondId).style.color = '#036';
			document.getElementById('startDate').innerHTML = '';
			document.getElementById('start').value = '';
			document.getElementById('end').value = '';
			
		}
	}
	
	function shadeInterval( date1, date2, colour )
	{
		var time = date1.getTime();
		var millisecDay = 60 * 60 * 24 * 1000;
		while( time < ( date2.getTime() - millisecDay ) )
		{
			
			time += millisecDay;
			var newDate = new Date( time );
			var dateString = newDate.getDate() + ',' + ( newDate.getMonth() + 1 ) + ',' + ( newDate.getFullYear() );
			var el = document.getElementById( dateString );
			if( el == null )
			{
				return false;
			}
			else
				el.style.background=colour;
		}
		return true;
	}
	
	function validate(form)
	{
		if( !eregi( '^[a-z ]+$', form.name ) ) return false;
		if( !eregi( '^[a-z0-9-]+@[a-z0-9-]+\.[a-z]+{\.[a-z]+}?', form.email) ) return false;

		return true; 
	}
}

