function update_name_select(name,location,date)
{
	$("#course_name_select").html("<option value='any'>Loading</option>").load( "/ajax_server/fetch?service=ajax.course.names&course_location="+location+"&course_date="+date, function(data)
	{
		$(this).html(data);
		$("#course_name_select option[value='" + name + "']").attr("selected", "selected");
	});
}

function update_location_select(name,location,date)
{
	$("#course_location_select").html("<option value='any'>Loading</option>").load( "/ajax_server/fetch?service=ajax.course.locations&course_name="+name+"&course_date="+date, function(data)
	{
		$(this).html(data);
		$("#course_location_select option[value='" + location + "']").attr("selected", "selected");
	});
}

function update_date_select(name,location,date)
{
	$("#course_date_select").html("<option value='any'>Loading</option>").load( "/ajax_server/fetch?service=ajax.course.dates&course_name="+name+"&course_location="+location, function(data)
	{
		$(this).html(data);
		$("#course_date_select option[value='" + date + "']").attr("selected", "selected");
	});
}

function name_select_change()
{
	var name = $("#course_name_select :selected").val();
	var location = $("#course_location_select :selected").val();
	var date = $("#course_date_select :selected").val();
	
	if (location == "any") update_location_select(name,location,date);
	if (date == "any") update_date_select(name,location,date);
}
function location_select_change()
{
	var name = $("#course_name_select :selected").val();
	var location = $("#course_location_select :selected").val();
	var date = $("#course_date_select :selected").val();
	
	if (name == "any") update_name_select(name,location,date);
	if (date == "any") update_date_select(name,location,date);
}
function date_select_change()
{
	var name = $("#course_name_select :selected").val();
	var location = $("#course_location_select :selected").val();
	var date = $("#course_date_select :selected").val();
	
	if (name == "any") update_name_select(name,location,date);
	if (location == "any") update_location_select(name,location,date);
}

function find_courses_form_filter()
{
	var course_name = $("#course_name_select :selected").val();
	var course_location = $("#course_location_select :selected").val();
	var course_date = $("#course_date_select :selected").val();
	
	var url = "/training/course_list.html";
	url += "?course_name="+course_name;
	url += "&course_location="+course_location;
	url += "&course_date="+course_date;

	$(location).attr('href',url);
}

function find_courses_form_all()
{
	var url = "/training/course_list.html?course_name=any&course_location=any&course_date=any";
	$(location).attr('href',url);
}

function getUrlVars() 
{
	var vars = {};
	var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) 
				{
					vars[key] = value;
				});
	return vars;
}

jQuery(document).ready(function(){
	var name = getUrlVars()["course_name"];
	if (name == undefined) name = "any";
	var location = getUrlVars()["course_location"];
	if (location == undefined) location = "any";
	var date = getUrlVars()["course_date"];
	if (date == undefined) date = "any";
	
	update_name_select(name,location,date);
	update_location_select(name,location,date);
	update_date_select(name,location,date);
	
});
