$(document).ready(function()
{
	// constants
	var monthselect = $('select#datemonth');
	var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];

	// date variance variables
	var today = new Date();
	var disabled = new Date();
//	disabled.setDate(disabled.getDate() + 2);
	var selected = new Date();
	selected.setDate(today.getDate() + 2);
	
	// set up the calendar months
	for(var i=0;i<12;i++)
	{
		var month = new Date();
		month.setDate(1);
		month.setHours(1);
		month.setMonth(month.getMonth()+i);
		
		var str = months[month.getMonth()]+' '+month.getFullYear();
		var modifier = month.toString();
		
		monthselect.append('<option value="'+modifier+'">'+str+'</option>');
	}
	
	// events
	// month select dropdown
	monthselect.change(function(e)
	{
		var seldate = new Date(Date.parse(e.target.value));
		$("#calendar").datepicker('setDate', seldate);
	});
	// guests select dropdown
	$('select#fguests').change(function(e)
	{
		var rooms = $('select#frooms').attr('value');
		var guests = this.value;
		var optrooms;
		
		if(guests > 4 && rooms < 3)
		{
			optrooms = 3;
		} else if(guests > 2 && rooms < 2)
		{
			optrooms = 2;
		}
		
		if(optrooms)
		{
			$('select#frooms option').each(function(i, val)
			{
				if(val.value == optrooms)
				{
					$('select#frooms').attr('selectedIndex',i);
				}
			});
		}
	});
	// rooms select dropdown
	$('select#frooms').change(function(e)
	{
		var rooms = this.value;
		var guests = $('select#fguests').attr('value');
		var optguests;
		
		if(rooms < 2 && guests > 2)
		{
			optguests = 2
		} else if(rooms < 3 && guests > 4)
		{
			optguests = 4
		}
		
		if(optguests)
		{
			$('select#fguests option').each(function(i, val)
			{
				if(val.value == optguests)
				{
					$('select#fguests').attr('selectedIndex',i);
				}
			});
		}
	});

	// set up the calendar
	$("#calendar").datepicker(
	{
		beforeShowDay: function(dt)
		{
			var date = dt.getDate();
			if(date < 10)
			{
				date = '0'+date;
			}
			var month = dt.getMonth()+1;
			if(month < 10)
			{
				month = '0'+month;
			}
			var dtiso = dt.getFullYear()+'-'+month+'-'+date;
			if(dateupdate[dtiso])
			{
				if(dateupdate[dtiso]['type'] == 1)
				{
					return [false, 'calendar-disabled', 'Unavailable'];
				} else if(dateupdate[dtiso]['type'] == 2)
				{
					return [true,'calendar-holiday','Holiday'];
				}
			} else if(dt <= disabled)
			{
				return [false, 'calendar-disabled', 'Unavailable'];
			}
			
			if(dt.getDay() >= 5)
			{
				return [true,'calendar-available calendar-weekend','Available'];
			}

			return [true,'calendar-available','Available'];
		},
		onSelect: function(dateText, inst)
		{
			updateform(dateText);
		},
		dateFormat: 'yy-mm-dd'
	});
	updateform(selected);
	
});

// calendar to form functionality
function updateform(dt)
{
	var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
	var seldate = new Date(Date.parseDate(dt,"Y-m-d"));
	var daterule = getrule(dt);
	var nights = 2;
	var guests = 2;
	var selnights = $('select#fnights').attr('value');
	var selguests = $('select#fguests').attr('value');

	// update the form field
	$('#fvcheckin').html(months[seldate.getMonth()] + ' ' + seldate.getDate() + ', ' + seldate.getFullYear());
	$('input#fcheckin').attr('value',dt);
	
	// test whether there are rules associates with this date
	if(daterule['type'] == 2)
	{
		nights = daterule['nights'] ? daterule['nights'] : 2;
		guests = daterule['guests'] ? daterule['guests'] : 2;
	}

	$("select#fnights").html('');
	$("select#fguests").html('');
	
	for(var i=nights;i<15;i++)
	{
		var option = $('<option>'+i+'</option>').appendTo("select#fnights");
		if(selnights == i)
		{
			option.attr('selected','selected');
		}
	}
	for(var i=guests;i<7;i++)
	{
		var option = $('<option>'+i+'</option>').appendTo("select#fguests");
		if(selguests == i)
		{
			option.attr('selected','selected');
		}
	}
	
	// trigger the change events in the form
	$('select#fguests').trigger('change');
}

// the getrule function
function getrule(dtiso)
{
	return dateupdate[dtiso] ? dateupdate[dtiso] : {'type':0};
}

// form validation
function validate_booking1()
{
	var e = '';

	if(!$('input#fcheckin').attr('value') || $('input#fcheckin').attr('value') == '')
	{
		e = 'You need to select a date to proceed, please click your desired check-in date on the calendar.';
	}
	
	if(e)
	{
		alert(e);
		return false;
	}
	
	return true;
}
function go_booking()
{
	if(validate_booking1()) { document.bookingform1.action = '/Bookings/Booking-step2/'; document.bookingform1.submit(); }
}