////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

function fetchCourseResults() {
	
	var url = _base + 'CourseFinder/fetchresults.php';
    var form = document.course_finder;						
		
    var pars = Object.toQueryString({
            	'q':url,
				'qt': 'new',
				'w': form.w.value,		
				'course_type': form.course_type.options[form.course_type.selectedIndex].value,
				'specialisation':form.specialisation.options[form.specialisation.selectedIndex].value,
				'location':form.course_location.options[form.course_location.selectedIndex].value,
				'course_year': form.course_year.options[form.course_year.selectedIndex].value,						
				'school':form.school.options[form.school.selectedIndex].value,
				'minimum_entry_level':form.minimum_entry_level.options[form.minimum_entry_level.selectedIndex].value,
				'length_of_lessons':form.length_of_lessons.options[form.length_of_lessons.selectedIndex].value,
				'maximum_class_size':form.maximum_class_size.options[form.maximum_class_size.selectedIndex].value,
				'lessons_per_week':form.lessons_per_week.options[form.lessons_per_week.selectedIndex].value,
				'minimum_student_age':form.minimum_student_age.options[form.minimum_student_age.selectedIndex].value
				});

	      new Ajax('index-ajax.php', {postBody: pars, update: $('results')}).request();
}

function fetchCourseSpecialisations(coursetype) {

	var url = _base + 'CourseFinder/fetchdata.php';
	var pars = Object.toQueryString({
            	'q':url,
				'action':'specialisation',
				'coursetype': coursetype
				});
	new Ajax('index-ajax.php', {postBody: pars, onComplete: fetchCourseLocations, update: $('cf_specialisation')}).request();
}

function fetchCourseLocations() {

    var form = document.course_finder;	
	var url = _base + 'CourseFinder/fetchdata.php';
	
	var pars = Object.toQueryString({
            	'q':url,
				'action':'locations',
				'coursetype': form.course_type.options[form.course_type.selectedIndex].value,
				'specialisation': form.specialisation.options[form.specialisation.selectedIndex].value
				});
				
	new Ajax('index-ajax.php', {postBody: pars, onComplete: fetchCourseSchools, update: $('cf_location')}).request();
}

function fetchCourseSchools() {

  var form = document.course_finder;	
	var url = _base + 'CourseFinder/fetchdata.php';

	if (form.course_location.options.length==2) {
	 	form.course_location.selectedIndex=1;
	}
		 	
	var pars = Object.toQueryString({
            	'q':url,
				'action':'schools',
				'coursetype': form.course_type.options[form.course_type.selectedIndex].value,
				'specialisation': form.specialisation.options[form.specialisation.selectedIndex].value,
				'location': form.course_location.options[form.course_location.selectedIndex].value
				});
				
	new Ajax('index-ajax.php', {postBody: pars, onComplete: checkSchoolOptions ,update: $('cf_schools')}).request();
}

function checkSchoolOptions() {
  var form = document.course_finder;	
	if (form.school.options.length==2) {
	 	form.school.selectedIndex=1;
		fetchCourseResults();
	}
}

