$(document).ready(function(){var $selects=$('form.frm-course-finder select');var $courseTypes=$('#cf-course-type');var $courseTitles=$('#cf-course-title');var $locations=$('#cf-course-location');var emptySelect=function(el){while(el.options.length>1){el.options[1]=null;}
$(el).find('optgroup').remove();}
var setSelectedIndexByValue=function(select,val){select.selectedIndex=$(select).find('option[value='+val+']').prevAll().length;}
var getCourses=function(el){var $form=$(el.form);var $trigger=$(el);var courseTypes=$courseTypes.get(0);var courseTitles=$courseTitles.get(0);var locations=$locations.get(0);switch($trigger.attr('id')){case courseTypes.id:emptySelect(courseTitles);if(locations.selectedIndex==0){emptySelect(locations);}
break;case courseTitles.id:courseTypes.selectedIndex=0;if(locations.selectedIndex==0){emptySelect(locations);}
break;case locations.id:if(locations.selectedIndex==0){emptySelect(locations);}
if(courseTitles.selectedIndex==0){emptySelect(courseTitles);}
break;}
$('#cf-num-courses-found').text('Loading, please wait');$.ajax({'type':'POST','url':'/rest/mavencourses_coursefinder/course-finder','data':$form.serialize(),'success':function(r){var courseTypesIndex=0;var courseTypesValue=$courseTypes.val();var courseTitlesIndex=0
var courseTitlesValue=$courseTitles.val();var locationsIndex=0
var locationsValue=$locations.val();if(($trigger.attr('id')==courseTitles.id)&&(courseTitles.selectedIndex>0)){$.each(r.categories,function(i){var category=this;$.each(category.courses,function(j){var course=this;if(course.courseId==courseTitlesValue){setSelectedIndexByValue(courseTypes,category.categoryId);}});});}else if(courseTitles.options.length==1){$.each(r.categories,function(i){var category=this;var optgroup=document.createElement('optgroup');optgroup.label=category.categoryTitle;courseTitles.appendChild(optgroup);$.each(category.courses,function(j){var course=this;var l=courseTitles.options.length;if(course.courseId==courseTitlesValue){courseTitlesIndex=l;}
var opt=new Option(course.courseTitle,course.courseId);opt.value=course.courseId;opt.text=course.courseTitle;$courseTitles.find('optgroup:last').get(0).appendChild(opt);$courseTitles.find('optgroup:last option:last').get(0).text=course.courseTitle;});});courseTitles.selectedIndex=courseTitlesIndex;}
if(locations.options.length==1){$.each(r.locations,function(i){var loc=this;var l=locations.options.length;if(loc.locationId==locationsValue){locationsIndex=l;}
locations.options[locations.options.length]=new Option(loc.locationName,loc.locationId);});locations.selectedIndex=locationsIndex;}
if(parseInt(r.numTotalCourses)==0){$('#cf-num-courses-found').html('0 &nbsp; <a href="/course-schedule/">View course schedule</a>.');}else{$('#cf-num-courses-found').text(r.numTotalCourses);}}});}
$('a.cf-clear-selection').click(function(ev){ev.preventDefault();var $select=$(this).prevAll('select.pseudo-select');$select.get(0).selectedIndex=0;$select.change();});$('a.cf-clear-value').click(function(ev){ev.preventDefault();var $input=$(this).prev();$input.val('');getCourses($input.get(0));});$selects.change(function(ev){getCourses(this);});$('input.date-picker').change(function(ev){getCourses(this);}).datepicker({minDate:'+1',dateFormat:'dd/mm/yy',onSelect:function(val,dp){switch(this.id){case'cf-date-from':$('#cf-date-to').datepicker('option','minDate',$(this).datepicker('getDate'));break;}
getCourses(this);}});if($('#cf-date-from').val()!=''){$('#cf-date-to').datepicker('option','minDate',$(this).datepicker('getDate'));}
getCourses($('#cf-date-from').get(0));});
