
// Permiited number of nights stay for accomodation
var arr_stay	= new Array ( 3, 4, 7, 10, 11, 14 );
// The order of arr_berth MUST match the order in the accomodation meny
var arr_berth	= new Array ( 2, 2, 2, 4, 4, 5, 5, 8, 8, 8 );


/*----------------------------------------------------------------------------------------*/
function onload_reset_selects (  )
/*----------------------------------------------------------------------------------------*/
{
//reset_options ( 'be_stay' );
	reset_options ( 'be_adults' );
	reset_options ( 'be_children' );
}

/*----------------------------------------------------------------------------------------*/
function accom_change (  )
/*----------------------------------------------------------------------------------------*/
{
	var int_accom_index	= document.getElementById ( 'be_accom' ).selectedIndex;
	var	int_berth				= arr_berth[int_accom_index - 1]
	var mxd_stay				= arr_stay;
	var str_day					= get_day_of_week ( document.getElementById ( 'be_date' ).value );
	
	// Modify the default mxd_stay for different types of accomodation / pitches
	if ( str_day == 'Mon' )
	{
		mxd_stay	= new Array ( 4, 7, 11, 14 );
	}
	else if ( str_day == 'Fri' )
	{
		mxd_stay	= new Array ( 3, 7, 10, 14 );
	}

	populate_options ( 'be_stay',	mxd_stay );
	populate_options ( 'be_adults', 8 );
	populate_options ( 'be_children', 7 );
//populate_options ( 'be_adults', int_berth );
//populate_options ( 'be_children', int_berth - 1 );

	return true;
}

/*----------------------------------------------------------------------------------------*/
function date_change ( )
/*----------------------------------------------------------------------------------------*/
{
	var int_accom_index	= document.getElementById ( 'be_accom' ).selectedIndex;
	var mxd_stay				= arr_stay;
	var str_day					= get_day_of_week ( document.getElementById ( 'be_date' ).value );

	// Modify the number of nights according to the accomodation and day of week
	if ( str_day == 'Mon' )
	{
		mxd_stay	= new Array ( 4, 7, 11, 14 );
		populate_options ( 'be_stay',	mxd_stay );
		populate_options ( 'be_adults', 8 );
		populate_options ( 'be_children', 7 );
	}
	else if ( str_day == 'Fri' )
	{
		mxd_stay	= new Array ( 3, 7, 10, 14 );
		populate_options ( 'be_stay',	mxd_stay );
		populate_options ( 'be_adults', 8 );
		populate_options ( 'be_children', 7 );
	}
	else
	{
		reset_options ( 'be_stay' );
		reset_options ( 'be_adults' );
		reset_options ( 'be_children' );
	}

	return true;
}

/*----------------------------------------------------------------------------------------*/
function adults_change ( )
/*----------------------------------------------------------------------------------------*/
{
	/* Functionality not required at launch of site, but will be reintroduced later
	var int_accom_index	= document.getElementById ( 'be_accom' ).selectedIndex;
	var	int_berth				= arr_berth[int_accom_index - 1]
	var int_adults			= document.getElementById ( 'be_adults' ).selectedIndex;

	populate_options	( 'be_children', int_berth - int_adults );
	*/
	return true;
}

/*----------------------------------------------------------------------------------------*/
function populate_options ( str_select_id, mxd_options )
/*----------------------------------------------------------------------------------------*/
{
	// Clear all the options and capture any selected value
	var mxd_selected	= reset_options ( str_select_id );

	// Get the select object
	var obj_select		= document.getElementById ( str_select_id );
	var	int_index 		= 0;
	
	// If the mxd_options is an integeer, populate the menu with an incremental value
	if ( typeof mxd_options == 'number' )
	{
		for ( i = 1; i <=  mxd_options; i++ )
		{
			// Define new text and value for option. Text AND value is required for
			// cross-browser ajax compatibilty
			obj_select.options[i] = new Option ( i, i );
			
			if ( i == mxd_selected )
			{
				int_index = i;
			}
		}
	}
	// If the options array is present, use this as data to populate the select
	else if ( mxd_options )
	{
		for ( i = 0; i <  mxd_options.length; i++ )
		{
			// Define new text and value for option. Text AND value is required for
			// cross-browser ajax compatibilty
			obj_select.options[i + 1]	= new Option ( mxd_options[i], mxd_options[i] );

			if ( mxd_options[i] == mxd_selected )
			{
				int_index = i + 1;
			}
		}
	}

	// If we have an value for the index then set it
	obj_select.selectedIndex = int_index;

	return true;
}

/*----------------------------------------------------------------------------------------*/
function reset_options ( str_select_id )
/*----------------------------------------------------------------------------------------*/
{
	// Get the select object
	var obj_select		= document.getElementById ( str_select_id );
	var mxd_selected	= obj_select.options[obj_select.selectedIndex].value;
	
	// You can't put obj_select.options.length directly in the for loop
	// otherwise it's value is refe=reshd on each loop
	var int_length = obj_select.options.length;

	// Set each of the options to NULL to remove them from the select.
	// Bizarrely, you have to remove the last eoption first, and so on
	// otherwise it won't reset the options properly
	for ( i = 1; i < int_length; i++ )
	{
		int_index = int_length - i;
		obj_select.options[int_index] = null;
	}
	
	// Return the selected value, so it can be reselected
	return mxd_selected;
}

/*----------------------------------------------------------------------------------------*/
function get_day_of_week ( str_date )
/*----------------------------------------------------------------------------------------*/
{
	var str_day = '';
	
	if ( str_date )
	{
		arr_days			= [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ];
		arr_date			= str_date.split('/');
		// Make sure the numbers are integers to be passed into new Date
		arr_date[0]		= Number ( arr_date[0] );
		arr_date[1]		= Number ( arr_date[1] ) - 1;
		arr_date[2]		= Number ( arr_date[2] );
		arr_date[2]		= ( arr_date[2] < 100	? 2000 + arr_date[2] : arr_date[2] );

		obj_date			= new Date ( arr_date[2], arr_date[1], arr_date[0] );
		str_day				= arr_days[obj_date.getDay ( )];
	}

	return str_day;
}
