$(document).ready(function()
{
	// add new sections
	$('#section_id').change(function()
	{
		$('#course_id').empty().html('<option value="">- Please select a course -</option>');
		$.getJSON('/ajaxInterface/ajax_booking.php?section_id=' + this.value, function(json)
		{
			myHtml = '';
			for (i in json) myHtml += '<option value="' + json[i]['siteElement_id'] + '">' + json[i]['title'] + '</option>';
			$('#course_id').append(myHtml);
		});
	});

	// add new sections
	$('#course_id').change(function()
	{
		// get course description and update div accordingly
		$.getJSON('/ajaxInterface/ajax_booking.php?course_id=' + this.value + '&course_description=1', function(json) { $('#course_description').html(json['textShort']); });

		// update venue select box
		$('#venue_id').empty().html('<option value="">- Please select a venue -</option>');
		$.getJSON('/ajaxInterface/ajax_booking.php?course_id=' + this.value, function(json)
		{
			myHtml = '';
			for (i in json) 
			{
				myHtml += '<option value="' + json[i]['courseVenue_id'] + '">' + json[i]['name'] + '</option>';
			}
			
			$('#venue_id').append(myHtml);
		});
	});

	$('#venue_id').change(function()
	{
		// update date select box
		$('#courseSession_id').empty().html('<option value="">- Please select a date -</option>');
		
		$.getJSON('/ajaxInterface/ajax_booking.php?course_id=' + document.getElementById('course_id').value + '&venue_id=' + this.value, function(json)
		{
			myHtml = '';
			for (i in json) myHtml += '<option value="' + json[i]['courseSession_id'] + '">' + json[i]['dateStart'] + ' - ' + json[i]['dateEnd'] + '</option>';
			$('#courseSession_id').append(myHtml);
		});
	});

	// submit form on add_booking press
	$('#add_booking').click(function() { $('#booking_form').submit(); return false; });

	// on form submission (TODO: add ajax functionality for adding new bookings)
	/*$('#booking_form').submit(function()
	{
		sectionId = $('#section_id').attr('value');
		courseId = $('#siteElement_id').attr('value');
		courseSessionsId = $('#courseSession_id').attr('value');
		if (sectionId && courseId && courseSessionsId)
		{
			$.post(document.location, { section_id: sectionId, siteElement_id: courseId, courseSession_id: courseSessionsId }, function(data)
			{
				if (data == 1) alert('Successfully added your booking');
			});
		}
	});*/
});
