// Dynaamiset kentät
function update_search_form() {
  var selectedValues = document.getElementById('categorySelected'); // Kaikki valitut kentän arvot
  var specificOptionId; // Lisäkenttä, kuten autot.

  var categoryIds = new Array();
  for (var i = 0; i < selectedValues.length; i++) {
      categoryIds.push(selectedValues.options[i].value);
  }
  
  var announcementType = parseAnnouncementType(categoryIds);
  
  // Send the result with the form
  //document.getElementById("announcementType").value = announcementType;

  var enabledFields = getEnabledFields(categoryIds);

  var foundEnabledField = false;
  for (var i = 0; i < allSpecificFields.length; i++) {
      if (allSpecificFields[i] in enabledFields) {
          foundEnabledField = true;
          document.getElementById(allSpecificFields[i]).style.display = '';

          // Clear the price fields
          clearPriceFields(allSpecificFields[i]);
          
      } else {
          document.getElementById(allSpecificFields[i]).style.display = 'none';
      }
  }
  if (!foundEnabledField) {
      for (var i = 0; i < defaultFields.length; i++) {
          document.getElementById(allSpecificFields[i]).style.display = '';

          // Clear the price fields
          clearPriceFields(allSpecificFields[i]);
      }
  }
} //func

// Sets a common announcement type in the form if there is one
function parseAnnouncementType(categoryIds) {
  if (categoryIds.length > 0 && specificFields[categoryIds[0]] != null) {
    var announcementType = specificFields[categoryIds[0]][0];
    for (var i=1; i<categoryIds.length; i++) {
      if (specificFields[categoryIds[i]] == null ||
          announcementType != specificFields[categoryIds[i]][0]) {
        return null;
      }
    }
    return announcementType;
  } else {
    return null;
  }
}

// Returns specific fields shared by given categories
function getEnabledFields(categoryIds) {
    var selectedFields = new Object();
    // Continue only if the first category has at least one specific field
    if (categoryIds.length > 0 && specificFields[categoryIds[0]] != null) {
        // Copy potential specific fields to result
        for (var i in specificFields[categoryIds[0]][1]) {
            selectedFields[i] = specificFields[categoryIds[0]][1][i];
        }
        // Drop specific field from the result if it is not found from all the
        // other categories
        for (var i = 1; i < categoryIds.length; i++) {
            if (specificFields[categoryIds[i]] == null) {
                return new Object();
            }
            var currentFields = specificFields[categoryIds[i]][1];
            for (var selectedField in selectedFields) {
                if (!(selectedField in currentFields)) {
                    delete selectedFields[selectedField];
                }
            }
        }
    }
    return selectedFields;
}

function clearPriceFields(visibleField) {
  if (visibleField != 'sellFields') {
    if (document.advancedSearcher.minPrice) document.advancedSearcher.minPrice.value = '';
    if (document.advancedSearcher.maxPrice) document.advancedSearcher.maxPrice.value = '';
  }
  if (visibleField != 'carFields') { 
    if (document.advancedSearcher.minCarPrice) document.advancedSearcher.minCarPrice.value = '';
    if (document.advancedSearcher.maxCarPrice) document.advancedSearcher.maxCarPrice.value = '';
  }
  if (visibleField == 'carFields' || visibleField == 'sellFields') {
    if (document.advancedSearcher.minRealtyPrice) document.advancedSearcher.minRealtyPrice.value = '';
    if (document.advancedSearcher.maxRealtyPrice) document.advancedSearcher.maxRealtyPrice.value = '';
  }
}