
// 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 ( 8, 8, 6, 4, 6, 6, 6, 6 );
var arr_berth	= new Array ( 8, 8, 6, 6, 6, 6 ); /* short menu with no beroom or berth info */

/*----------------------------------------------------------------------------------------*/
function onload_reset_selects ( )
/*----------------------------------------------------------------------------------------*/
{
//reset_options ( 'be_stay' );
	reset_options ( 'be_adults' );
	reset_options ( 'be_children' );
	reset_options ( 'be_special' );
}

/*----------------------------------------------------------------------------------------*/
function accom_change (  )
/*----------------------------------------------------------------------------------------*/
{
	var int_accom_index	= document.getElementById ( 'be_accom' ).selectedIndex;
	var	int_berth				= arr_berth[int_accom_index - 1]
	var arr_special			= new Array ( 'Adjacent parking', 'Pet unit' );
	var mxd_stay				= arr_stay;
	var str_day					= get_day_of_week ( document.getElementById ( 'be_date' ).value );
	
	// Modify the default arr_special and mxd_stay for different types of accomodation / pitches
	if ( int_accom_index != 0 ) // Bungalow (8 Berth)
	{
		if ( int_accom_index == 5 || int_accom_index == 6 ) // Caravan or Tents
		{
			mxd_stay	= 14;	

			if ( int_accom_index == 5 ) // Caravan Pitch 
			{
				arr_special	= new Array ( 'Hard standing' );
			}
			else // Tent Pitch 
			{
				arr_special	= new Array ( 'Electric hook-up' );
			}

			setWeekDays( "date_of_stay", 1, 1, 1, 1, 1, 1, 1 );
		}
		else
		{
			if ( int_accom_index == 1 ) // Bungalow (8 Berth)
			{
				arr_special	 = new Array ( 'Adjacent parking' );
			}
			else if ( int_accom_index == 4  ) // Premier Apartment (6 Berth)
			{
				arr_special	 = new Array ( 'Adjacent parking', 'Pet unit', 'Ground floor', 'First floor' );
			}

			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 );
			}

			setWeekDays( "date_of_stay", 1, 0, 0, 0, 1, 0, 0 );
		}

		populate_options ( 'be_stay',	mxd_stay );
		populate_options ( 'be_adults', int_berth );
		populate_options ( 'be_children', int_berth - 1 );
		populate_options ( 'be_special',	arr_special );
	}

	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 ( int_accom_index == 5 || int_accom_index == 6 ) // Caravan or Tents
	{
		mxd_stay	= 14;	
		populate_options ( 'be_stay',	mxd_stay );
	}
	else if ( int_accom_index != 0 && str_day == 'Mon' )
	{
		mxd_stay	= new Array ( 4, 7, 11, 14 );
		populate_options ( 'be_stay',	mxd_stay );
	}
	else if ( int_accom_index != 0 && str_day == 'Fri' )
	{
		mxd_stay	= new Array ( 3, 7, 10, 14 );
		populate_options ( 'be_stay',	mxd_stay );
	}
	else
	{
		reset_options ( 'be_stay' );
	}

	return true;
}

/*----------------------------------------------------------------------------------------*/
function adults_change ( )
/*----------------------------------------------------------------------------------------*/
{
	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	= 0;
	
	if ( obj_select != null )
	{
		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;
}
