function filter_form_submit() {
    this_form = $(this);
    form_data = this_form.serializeArray();

    var filter_frames = false;
    var actSemArgument = {};
    var relationalSemPref = {'relation': '',
    						 'role': '',
    						 'attribute': ''};
    var semArgumentsAlternatives = [];
    var semArguments = [];
  
    form_data = $.map(form_data, function(elem)
    {
      if (elem.name != 'filter_frames' && !isPartOfSemArgFilter(elem)) {
    	  return elem;
      }
      else {
    	  if(elem.name == 'filter_frames') {
    		  filter_frames = elem.value;
    	  }
    	  else if(elem.name == 'negation') {
    		  if(!jQuery.isEmptyObject(actSemArgument)) {
    			  semArguments.push(actSemArgument);
    		  }
    		  actSemArgument = {'negation': elem.value,
				  	  			'role': '',
				  	  			'attribute': '',
				  	  			'general_prefs': [],
				  	  			'synset_prefs': [],
				  	  			'relational_prefs': []}
    	  }
    	  else if(elem.name == 'or') {
    		  if(!jQuery.isEmptyObject(actSemArgument)) {
    			  semArguments.push(actSemArgument);
    			  actSemArgument = {};
    		  }
    		  semArgumentsAlternatives.push(semArguments);
    		  semArguments = [];
    	  } 
    	  else if(elem.name == 'role' || elem.name == 'attribute') {
    		  actSemArgument[elem.name] = elem.value;
    	  }
    	  else if(elem.name == 'general_pref' || elem.name == 'synset_pref') {
    		  actSemArgument[elem.name+'s'].push(elem.value);
    	  }
    	  else if(elem.name.startsWith('relational_pref')) {
    		  if(elem.name.endsWith('relation')) {
    			  relationalSemPref = {'relation': elem.value,
 						 			   'role': '',
 						               'attribute': ''};
    		  }
    		  else if(elem.name.endsWith('role')) {
    			  relationalSemPref['role'] = elem.value;
    		  }
    		  else if(elem.name.endsWith('attribute')) {
    			  relationalSemPref['attribute'] = elem.value;
    			  actSemArgument['relational_prefs'].push(relationalSemPref);
    			  relationalSemPref = {};
    		  }
    	  }
      }
    });
    if(!jQuery.isEmptyObject(actSemArgument)) {
		  semArguments.push(actSemArgument);
	}
    if(semArguments.length > 0) {
    	semArgumentsAlternatives.push(semArguments);
    }
	
    
    form_data.push({name: 'filter_frames', value: filter_frames});
    form_data.push({name: 'sem_arguments', value: semArgumentsAlternatives});
    
    act_lemma_id = window.prev_lemma_id;
    if(window.activeLemmaPanel != 'preview_lemma')
      act_lemma_id = window.lemma_id;
    
    form_data.push({name: 'lemma_id', value: act_lemma_id})

  $.ajaxJSON({
      method: 'post',
      url: ajax_filter_form_submit,
      data: {
        form_data: form_data
      },

    callback: function(result) {
	  $('#search-panel-dialog').dialog('close');
	  if(result['filter_frames'])
	  {
		  if(window.activeLemmaPanel == 'preview_lemma')
		  {
			  window.prev_filter_schema_type_val = result['schema_type'];
			  window.prev_filter_reflex_val = result['reflex'];
			  window.prev_filter_neg_val = result['negativity'];
			  window.prev_filter_pred_val = result['predicativity'];
			  window.prev_filter_opinion_val = result['opinion'];
			  window.prev_filter_aspect_val = result['aspect'];
			  window.prev_filter_position_val = result['position'];
			  window.prev_filter_argument_val = result['argument'];
			  $('#prev_frame_filter #schema_type_filter').val(result['schema_type']);
			  $('#prev_frame_filter #reflex_filter').val(result['reflex']);
			  $('#prev_frame_filter #neg_filter').val(result['negativity']);
			  $('#prev_frame_filter #pred_filter').val(result['predicativity']);
			  $('#prev_frame_filter #opinion_filter').val(result['opinion']);
			  $('#prev_frame_filter #aspect_filter').val(result['aspect']);
			  $('#prev_frame_filter #argument_filter').val(result['argument']);
			  $('#prev_frame_filter #position_filter').val(result['position']);
			  $('#prev_frame_filter #argument_filter').trigger('change');
		  }
		  else
		  {
			  window.filter_schema_type_val = result['schema_type'];
			  window.filter_reflex_val = result['reflex'];
			  window.filter_neg_val = result['negativity'];
			  window.filter_pred_val = result['predicativity'];
			  window.filter_opinion_val = result['opinion'];
			  window.filter_aspect_val = result['aspect'];
			  window.filter_position_val = result['position'];
			  window.filter_argument_val = result['argument']; 
			  $('#frame_filter #schema_type_filter').val(result['schema_type']);
			  $('#frame_filter #reflex_filter').val(result['reflex']);
			  $('#frame_filter #neg_filter').val(result['negativity']);
			  $('#frame_filter #pred_filter').val(result['predicativity']);
			  $('#frame_filter #opinion_filter').val(result['opinion']);
			  $('#frame_filter #aspect_filter').val(result['aspect']);
			  $('#frame_filter #argument_filter').val(result['argument']);
			  $('#frame_filter #position_filter').val(result['position']);
			  $('#frame_filter #argument_filter').trigger('change');
		  }
	  }
	  grid.trigger("reloadGrid");      
    },
    error_callback: function(xhr, status, error) {
      error_alert(status + ': ' + error);
    },
    bad_data_callback: function(result) {
        return true;
    },
  });
  return false;
}

function isPartOfSemArgFilter(field) {
	if(field.name == 'or' || field.name == 'negation' || 
	   field.name == 'role' || field.name == 'attribute' || 
	   field.name == 'general_pref' || field.name == 'synset_pref' || 
	   field.name.startsWith('relational_pref')) {
		return true;
	}
	return false;
}

function addSemArgFilter(buttonElem) {
	var semArgsElem = $(buttonElem).parent().parent();
	semArgsElem.append('<p id="sem-argument"></p>');
	semArgsElem.children().last().load(ajax_sem_arg_form);
}

function addArgAlternative(buttonElem) {
	var semArgsElem = $(buttonElem).parent().parent();
	semArgsElem.append('<div><hr class="alterSeparator"><input type="hidden" name="or" value="or"><strong>lub</strong> <button type="button" onclick="removeAlternative(this)">Usuń</button></div>');
}

function removeAlternative(buttonElem) {
	$(buttonElem).parent().remove();
}

function removeSemArgFilter(buttonElem) {
	$(buttonElem).parent().parent().remove();
}

function addSelectivePreferenceFilter(buttonElem) {
	var selPrefsElem = $(buttonElem).parent().parent();
	var selPrefType = selPrefsElem.find('#id_preference_type').first().val();
	selPrefsElem.append('<p id="sel-preference"></p>');
	if(selPrefType === 'general') {
		selPrefsElem.children().last().load(ajax_general_preference_form);
	}
	else if(selPrefType === 'synset') {
		selPrefsElem.children().last().load(ajax_synset_preference_form);
	}
	else if(selPrefType === 'relation') {
		selPrefsElem.children().last().load(ajax_relational_preference_form);
	}
}

function removeSelPreferenceFilter(buttonElem) {
	$(buttonElem).parent().remove();
}
