/*----------------------------------------------------------------------------------------*/
$(document).ready(function()
{
	// If the form's submit button is clicked
	$('#submit_form').click(function()
	{
		// Send the form data as a POST to the validation script
		$.post('p/inc.validate.php', $('#site_form').serializeArray(),
		// When the result from the validation script is returned
		// execute the following. The result is passed in str_data
		function ( str_data )
		{
			// If the form is valid there's no output 
			// from the validation so submit the form
			if ( str_data == '' )
			{
				$('#form_message').fadeTo('fast',0);
				$('#site_form').submit();
				return true;
			}
			else
			{
				// Fade out the validation message, then fade
				// it in again with the new validation errors
				$('#form_message').fadeTo('slow',0,function()
				{
					$(this).html(str_data).fadeTo('slow',1);
				});
			}
		});

		return false;
	});
	
	// If the accom_list class exists in the page
	$( '.accom_list' ).each(function(){
		
		// get parent form
		myform 		= $( this ).closest( 'form' );		
		// get accomodation list
		myaccom 	= $( this );		
		// get sibling retreat select
		myretreat = myform.find( '.update_accom_list' );
		
		// loop through retreat list and add accom options to data field
		myretreat.find('option').each(function(){
			myclass = $( this ).attr('class');
			if (myclass == '')
			{
				myclass = 'none';
			}
			data = myaccom.find( 'option.' + myclass );
			$( this ).data('options', data);
		});
				
		get_accomm_updates ( myretreat );		
				
		get_stay_updates ( );
		
				
	});
	
	// Hide all the special requirements options until the user selects a site with .update_accom_list applied
	$( '#special_requirements' ).hide();
	
	$( '#units_required' ).hide();
	
	
	// When the user changes selects an LR from the list hide all the non-relevant options in the ment	
	$( '.update_accom_list' ).change ( function ( )
	{
		get_accomm_updates ( $(this) );
	});	
	
	$( '.update_stay' ).change ( function ( )
	{
		get_stay_updates ( );
	});
	
	// when feature changes, check to see if other is selected
	get_features_update ( $('#accommodation_feature_id') );
	$( '.update_features' ).change ( function ( )
	{
		get_features_update ( $('#accommodation_feature_id') );
	});
	
	// when country changes, check to see if other is selected
	get_country_update ( $('.update_country') );
	$( '.update_country' ).change ( function ( )
	{
		get_country_update ( $(this) );
	});	

});
/*----------------------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------------------*/
function get_accomm_updates ( element )
/*----------------------------------------------------------------------------------------*/
{
	// get parent form
	myform 		= element.closest( 'form' );		
	// get sibling accomodation list
	myaccom = myform.find( '.accom_list' );
	
	// Get the class of teh selected LR so we can work out which accommodation options to display
	str_lr_class	= element.find ( 'option:selected' ).attr ( 'class' );
	str_selected	= myaccom.find ( 'option:selected' ).attr ( 'value' );

	if (str_lr_class)
	{
		str_lr_class = str_lr_class.split(' ')[0];

		enable_select ( myaccom );
		myaccom.append(element.find ( 'option:selected' ).data( 'options' ));
		myaccom.val('');
		myaccom.val(str_selected);
	}
	else
	{
		disable_select ( myaccom );
	}
	

	if ( str_lr_class == 'whitemead' )
	{
		$( '#special_requirements' ).show();
	}
	else
	{
		$( '#special_requirements' ).hide();
	}
	
	if ( str_lr_class == 'ghyll' || str_lr_class == 'whitemead' )
	{
		$( '#units_required' ).show();
	}
	else
	{
		$( '#units_required' ).hide();
		$( '#units_required select' ).val( '' );
	}
}


/*----------------------------------------------------------------------------------------*/
function get_stay_updates (  )
/*----------------------------------------------------------------------------------------*/
{		
	// get parent form
	myform 		= $( '#be_date_month' ).closest( 'form' );		
	// get sibling accomodation list
	myaccom = myform.find( '.accom_list' );
	
	// get the accom type from the update_accom_list select or the radio buttons
	/*var myaccom = $(".update_accom_list option:selected");
	
	if(myaccom) {
		var str_accom = myaccom.attr("className").split(' ')[1];

		// get from the radio buttons
		if(str_accom == 'mixed')
			str_accom = $("#special_requirements input[type='radio']:checked").val();
	}*/
	
	// get the accom type from drop down (unit, pitch, room)
	arr_accom	= myaccom.find( 'option:selected' );
	if (arr_accom.length) {
		arr_accom	= arr_accom.attr ( 'class' ).split( ' ' );
		var str_accom	= arr_accom[1];
	}
	else
	{
		var str_accom = '';
	}

	/******************************/		
	/* Set days of month in select */
	if ($( '#be_date_month' ).val ( )) {
		
		current_day = $( '#be_date_day' ).val ( );
		enable_select ( $( '#be_date_day' ), true );
		$( '#be_date_day' ).append($( '#be_date_day' ).data( 'options' ));
		$( '#be_date_day' ).val(current_day);
		
		var str_days =  Number ( daysInMonth( $( '#be_date_month' ).val ( ) ) );
				
		var currentTime = new Date();
		
		$( '#be_date_day option' ).each(function (){
			
			val = $(this).attr('value');
			thisTime = new Date($( '#be_date_month' ).val ( ) + '-' + val);
			
			if (val != '')
			{
				flag = true;
				if (Number ( val ) > str_days) {
					flag = false;
				}	
				else if (thisTime.getTime() < currentTime.getTime()) {
					flag = false;
				}		
				else if (str_accom == 'unit')
				{
					str_day 	= get_day_of_week ( $( '#be_date_month' ).val ( ), val );
					if (str_day != 'Mon' && str_day != 'Fri') flag = false;
				}
				if (!flag) {
					$(this).remove();
				}
			}
			
		});
	}
	else
	{		
		disable_select ( $( '#be_date_day' ) );
	}
	/*******************************/

	// set the hidden date field which is what is being passed to the external provider
	// date is in the format yyyy-mm-dd, needs to be in mm/dd/yyyy
	var date = ($("#be_date_month").val() + "-" + $("#be_date_day").val()).split('-');

	$("#be_datein").val(date[1] + "/" + date[2] + "/" + date[0]);

	if (myaccom.val ()) {
		str_day 	= get_day_of_week ( $( '#be_date_month' ).val ( ), $( '#be_date_day' ).val ( ) );
		
		str_stay	= $( '#be_stay' ).val ( );
		enable_select ( $( '#be_stay' ), true );
		$( '#be_stay' ).append($( '#be_stay' ).data( 'options' ));
		$( '#be_stay' ).val(str_stay);

		if (str_accom == 'unit')
		{
			$( '#be_stay' ).find ( 'option' ).each ( function ( )
			{
				str_value		= $(this).attr ( 'value' );
				if ( str_value )
				{
					if (str_value != 3 && str_value != 4 && str_value != 7)
					{
						$(this).remove();
					}
					else if (str_day == 'Mon' && str_value == 3)
					{
						$(this).remove();
					}
					else if (str_day == 'Fri' && str_value == 4)
					{
						$(this).remove();
					}
				}
			});
		}
	}
	else
	{
		disable_select( $( '#be_stay' ) );
	}
}

/*----------------------------------------------------------------------------------------*/
function get_features_update ( element )
/*----------------------------------------------------------------------------------------*/
{	
	// get parent form
	myform 		= element.closest( 'form' );		
	// get sibling accomodation list
	myaccom = myform.find( '.accom_list' );
	
	str_selected	= myaccom.find ( 'option:selected' ).attr ( 'value' );

	if (str_selected == '')
	{
		disable_select ( element );
	}
	else
	{
		myvalue = element.find ( 'option:selected' ).attr ( 'value' );
		enable_select ( element, true );
		element.append(element.data( 'options' ));
		element.val(myvalue);		
		
		element.find ( 'option' ).each ( function ( )
		{
			if ($(this).attr ( 'id' ) != 'accommodation_id_' + str_selected)
			{
				if ($(this).attr ( 'value' ) != '') $(this).remove();
			}
		});	
		element.val(myvalue);
	}	
}

/*----------------------------------------------------------------------------------------*/
function get_country_update ( element )
/*----------------------------------------------------------------------------------------*/
{
	str_id				= element.attr ( 'id' );
	str_name			= element.attr ( 'name' );
	str_selected	= element.find( ':selected' ).attr ( 'text' );
	
	if ( str_selected == 'Country not listed' )
	{
		element.after ( '<input class="text" id="' + str_id + '_text" name="' + str_name + '" maxlength="50" value="" />' );
		element.remove ( );
	}
}

/*----------------------------------------------------------------------------------------*/
function get_day_of_week ( str_monthyear, str_date )
/*----------------------------------------------------------------------------------------*/
{
	var str_day = '';
	if ( str_monthyear != '' && str_date != '')
	{
		arr_date			= str_monthyear.split('-');
		obj_date			= new Date ( arr_date[0], arr_date[1]-1, str_date );
		
		arr_days			= [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ];
		str_day				= arr_days[obj_date.getDay ( )];	
	}
	return str_day;
}

/*----------------------------------------------------------------------------------------*/
function daysInMonth( str_monthyear )
/*----------------------------------------------------------------------------------------*/
{		
	str_days = 0;
	if (str_monthyear)
	{
		arr_date			= str_monthyear.split('-');
		obj_date			= new Date ( arr_date[0], arr_date[1]-1, 32 );
		str_days = 32 - obj_date.getDate();
	}
	return str_days;
}

/*----------------------------------------------------------------------------------------*/
function disable_select ( element )
/*----------------------------------------------------------------------------------------*/
{
	if (element.length) {		
		data = element.find( 'option' );
		if (!element.data('options')) element.data('options', data);
		s = '&nbsp;';
		element.find( '*' ).remove();
		
		if (element.attr('id') != 'be_date_day')
		{
			blank = '...'+s+s+s+s+s+s+s+s+s+s+s+s+s+s+s+s+s+s+s+s+s+s+s+s+s+s+s+s;
		}
		else
		{
			blank = '...'+s+s+s+s+s+s;
		}
		
		element.append( '<option value="">'+blank+'</option>' );
		//element.attr ( 'disabled', 'disabled' );
	}
}

/*----------------------------------------------------------------------------------------*/
function enable_select ( element, noempty )
/*----------------------------------------------------------------------------------------*/
{
	if (element.length) {
		data = element.find( 'option' );
		if (!element.data('options')) element.data('options', data);
		element.find( '*' ).remove();
		//element.attr ( 'disabled', 'false' );
		if (!noempty) element.append( '<option value="">Please select...</option>' );
	}
}
