jQuery(document).ready(function(){
jQuery('select[name^="search_location_"]').on('change',function(){
jQuery(this).nextAll('select[name^="search_location_"]').children('option').remove();
if(typeof(jQuery(this).multiselect)!='undefined'){
jQuery(this).nextAll('select[name^="search_location_"]').last().multiselect('refresh');
}
jQuery(this).closest('form').find('[name=location]').attr('value',this.value)
jQuery(this).closest('form').find('[name="location[]"]').children('option').remove();
var new_options=jQuery(this).children('option:selected').clone();
jQuery(this).closest('form').find('[name="location[]"]').append(new_options);
jQuery(this).closest('form').find('[name="location[]"]').children('option').prop('selected',true);
if(this.selectedIndex > 0){
next_location_element=jQuery(this).nextAll('select[name^="search_location_"]').first();
next_location_element.append('<option>'+inmolink_multilevel_location.loading +'</option>')
var parent_location=this.value;
var child_locations=jQuery(this).nextAll('select[name^="search_location_"]').length;
var data={
'action': 'get_multilevel_location',
'parent': parent_location,
'depth':child_locations==1 ? 0:1 ,
'empty':typeof(jQuery(this).multiselect)!='undefined' ? 0:1,
};
var ajaxurl=inmolink_multilevel_location.ajaxurl;
jQuery.post(ajaxurl, data, function(response){
jQuery(next_location_element).children('option').remove();
var new_options=jQuery(response).children('option').clone();
if(data['empty']){
if(data['parent'])
jQuery(next_location_element).append('<option value="'+data['parent']+'">'+inmolink_multilevel_location.area+'</option>');
else
jQuery(next_location_element).append('<option value="'+data['parent']+'">'+inmolink_multilevel_location.sublocation+'</option>');
}
jQuery(next_location_element).append(new_options);
if(typeof(jQuery().multiselect)!='undefined')
jQuery(next_location_element).multiselect('refresh');
});
}else{
next_location_element=jQuery(this).nextAll('select[name^="search_location_"]').children();
next_location_element.append('<option value="" selected="selected">'+inmolink_multilevel_location.sublocation+'</option>')
}});
jQuery('#search_location_2').on('change',function(){
if(jQuery('#search_location_2').val()==''){
if(jQuery('#search_location_1').val()!=''){
var new_options=jQuery('#search_location_1').find('option:selected').clone();
jQuery('[name="location[]"]').append(new_options);
jQuery('[name="location[]"]').children('option').prop('selected',true);
}}
});
});