function initWorldAdvantagesSearchForm() {
  $('worldAdvantageCardId').observe('change', findCountries);
  $('worldAdvantageCityId').observe('change', findCategories);
  $('worldAdvantageCountryId').observe('change', findCities);
  $('worldAdvantageMainCategoryId').observe('change', findWorldDiscounts);
}
addOnLoadListener(initWorldAdvantagesSearchForm);

function findCountries() {
  clearSelection('worldAdvantageCountryId');
  clearSelection('worldAdvantageCityId');
  clearSelection('worldAdvantageMainCategoryId');
  $('worldAdvantagesCountriesContainer').removeClassName('indicator');
  if ($F('worldAdvantageCardId') != 0) {
    $('worldAdvantagesCountriesContainer').addClassName('indicator');
    var params = "command=getCountriesByCardId&cardId=" + $F('worldAdvantageCardId');
    new Ajax.Request(currentPageUrl, {parameters: params, onSuccess: fetchCountries});
  }
}

function fetchCountries(transport, countries) {
  $('worldAdvantagesCountriesContainer').removeClassName('indicator');
  if (countries.size() > 0) {
    $('worldAdvantageCountryId').update();
    createOption('worldAdvantageCountryId', 0, " -- vali siit -- ");
    countries.each(function(country) {
      createOption('worldAdvantageCountryId', country.id, country.name);
    });
    $('worldAdvantageCountryId').show();
  }
}

function findCities() {
  clearSelection('worldAdvantageCityId');
  clearSelection('worldAdvantageMainCategoryId');
  $('worldAdvantagesCitiesContainer').removeClassName('indicator');
  if ($F('worldAdvantageCountryId') != 0) {
    $('worldAdvantagesCitiesContainer').addClassName('indicator');
    findWorldDiscounts();
    var params = "command=findCitiesByCountryIdAndCardId&cardId=" + $F('worldAdvantageCardId') + "&countryId=" + $F('worldAdvantageCountryId');
    new Ajax.Request(currentPageUrl, {parameters: params, onSuccess: fetchCities});
  }
}

function fetchCities(transport, cities) {
  $('worldAdvantagesCitiesContainer').removeClassName('indicator');
  if (cities && cities.size() > 0) {
    $('worldAdvantageCityId').update();
    createOption('worldAdvantageCityId', 0, " -- vali siit -- ");
    cities.each(function(city) {
      createOption('worldAdvantageCityId', city.id, city.name);
    });
    $('worldAdvantageCityId').show();
  }
}

function findWorldDiscounts() {
  $('worldAdvantagesContainer').update();
  if ($F('worldAdvantageCardId') != 0 && $F('worldAdvantageCountryId') != 0 && $F('worldAdvantageCityId')) {
    $('worldAdvantagesContainer').addClassName('indicator');
    var params = "command=findWorldAdvantages&cardId=" + $F('worldAdvantageCardId') + "&countryId=" + $F('worldAdvantageCountryId') + "&cityId=" + $F('worldAdvantageCityId') + "&mainCategoryId=" + ($('worldAdvantageMainCategoryId').visible() ? $F('worldAdvantageMainCategoryId') : "");
    new Ajax.Request(currentPageUrl, {parameters: params, onSuccess: showDiscounts});
  }
}

function showDiscounts(transport) {
  //$('log').innerHTML = transport.responseText;
  discounts = eval('(' + transport.responseText + ')');
  //alert("discounts size: " + discounts.size());
  $('worldAdvantagesContainer').removeClassName('indicator');
  if (discounts && discounts.size() > 0) {
    var template = new Template("<div style=\"margin-bottom: 1em; clear: both;\"><span style=\"font-weight: bold;\">#{localName}</span><br/><span>#{subCategory}</span><br/><span>#{summary}</span></div>");
    discounts.each(function(discount) {
      $('worldAdvantagesContainer').update($('worldAdvantagesContainer').innerHTML + template.evaluate(discount));
    });
  }
}

function findCategories() {
  findWorldDiscounts();
  clearSelection('worldAdvantageMainCategoryId');
  if ($F('worldAdvantageCardId') != 0 && $F('worldAdvantageCountryId') != 0 && $F('worldAdvantageCityId') != 0) {
    $('worldAdvantagesMainCategoriesContainer').addClassName('indicator');
    var opts = "command=findMainCategories&cardId=" + $F('worldAdvantageCardId') + "&countryId=" + $F('worldAdvantageCountryId') + "&cityId=" + $F('worldAdvantageCityId');
    new Ajax.Request(currentPageUrl, {parameters: opts, onSuccess: showCategories});
  }
}

function showCategories(transport, categories) {
  //$('log').update(transport.responseText);
  $('worldAdvantagesMainCategoriesContainer').removeClassName('indicator');
  if (categories && categories.size() > 0) {
    $('worldAdvantageMainCategoryId').update();
    createOption('worldAdvantageMainCategoryId', 0, " -- vali siit -- ");
    categories.each(function(category) {
      createOption('worldAdvantageMainCategoryId', category.id, category.name);
    });
    $('worldAdvantageMainCategoryId').show();
  }
}

function createOption(parentId, value, name) {
  var option = document.createElement('option');
  option.setAttribute('value', value);
  option.appendChild(document.createTextNode(name));
  $(parentId).appendChild(option);
}

function clearSelection(itemId) {
  $(itemId).update();
  $(itemId).hide();
}
