  function update_area() {
    if ($('type_select').value != "land") {
      $('id_urban_area_container').hide();
      $('id_other_area_container').hide();
      return true;
    }
    
    if ($('subtype_select').value == "") {
      return true;
    }

    if ($('subtype_select').value == "urban") {
      $('id_urban_area_container').show();
      $('id_other_area_container').hide();
    } else {
      $('id_urban_area_container').hide();
      $('id_other_area_container').show();
    }
  }

  function update_subtype(id, type) {
    $(id).disabled = true;
        if(type=="") {empty = "True";}else{empty="False";} 
    new Ajax.Updater({success: id}, 
             '/ajax/subtype/', 
             { method: 'get', 
               parameters : {type: type, empty: empty},
               onComplete: function(transport) {$(transport.request.container.success).disabled = false;update_area();}});
  }

  function update_buyer_subtype(id, type) { 
    $(id).disabled = true;
        if(type=="") {empty = "True";}else{empty="False";}
    new Ajax.Updater({success: id}, 
             '/ajax/subtype/', 
             { method: 'get', 
               parameters : {type: type, empty: empty},
               onComplete: function(transport) {$(transport.request.container.success).disabled = false;}});
  }

  function update_municipality(id, island) {
    $(id).disabled = true;
        if(island=="") {empty = "True";}else{empty="False";} 
    new Ajax.Updater({success: id}, 
             '/ajax/municipalities/', 
             { method: 'get', 
               parameters : {island: island, empty: empty},
               onComplete: function(transport) {$(transport.request.container.success).disabled = false;}});
  }

  function update_locality(id, municipality) {
    $(id).disabled = true;
        if(municipality=="") {empty = "True";}else{empty="False";} 
    new Ajax.Updater({success: id}, 
             '/ajax/localities/', 
             { method: 'get', 
               parameters : {municipality: municipality, empty: empty},
               onComplete: function(transport) {$(transport.request.container.success).disabled = false;}});
  }
  
  function min_price_event_handler(event) {
    var min_price_value = event.target.value;
        
        var max_price_id = event.target.id.replace("min", "max");   
        var max_price_value = $(max_price_id).value;
        
    $(max_price_id).disabled = true;
    new Ajax.Updater({success: max_price_id}, 
              '/ajax/prices/', 
              { method: 'get', 
              parameters : {min_price: min_price_value, max_price: max_price_value, value: "max"}, 
              onComplete: function(transport) {$(transport.request.container.success).disabled = false;} });
  }
  
  function max_price_event_handler(event) {
        var max_price_value = event.target.value;

        var min_price_id = event.target.id.replace("max", "min");
    var min_price_value = $(min_price_id).value;
        
    if(min_price_value == "" || min_price_value == "0" || parseInt(min_price_value) > parseInt(max_price_value) ) {
      $(min_price_id).disabled = true;
      new Ajax.Updater({success: min_price_id}, 
               '/ajax/prices/', 
               { method: 'get', 
                 parameters : {min_price: min_price_value, max_price: max_price_value, value: "min"},
                 onComplete: function(transport) {$(transport.request.container.success).disabled = false;} });
    }
  }
  
  function island_event_handler(event) {
    var island = event.target.value;
        var municipality_id = event.target.id.replace("island", "municipality");
    update_municipality(municipality_id, island);
        
        var municipality_value = "";
        var locality_id = event.target.id.replace("island", "locality");
    update_locality(locality_id, municipality_value);
  }

  function municipality_event_handler(event) {
    var municipality_value = event.target.value;
    var locality_id = event.target.id.replace("municipality", "locality");
        update_locality(locality_id, municipality_value);
  }

    function type_event_handler(event) {
        var type = event.target.value;
        var subtype_id = event.target.id.replace("type", "subtype");
        update_subtype(subtype_id, type);

        if(type == "land") {
            $("id_estate_state").disabled = true;
        } else {
            $("id_estate_state").disabled = false;
        }
    }

    function buyer_type_event_handler(event) {
        var type = event.target.value;
        var target_id = event.target.id;
        update_buyer_subtype(target_id.replace("type", "subtype").replace("id_", "id_min_"), type);
        
        var max_estate_type_id = target_id.replace("type", "subtype").replace("id_", "id_max_");
        
        if(type=="garage" || type=="space" || type=="wharehouse" || type=="land") {
            $(max_estate_type_id).disabled = true;
       } else {
        update_buyer_subtype(max_estate_type_id, type);
       }
    }
    
  function location_event_handler(event) {
        var location = event.target.value;

        if(location == "azores") {
            $("id_mainland_municipality_container").hide();
            $("island_select").disabled = false;
        } else {
            $("id_mainland_municipality_container").show();
            $("island_select").disabled = true;
            $("municipality_select").disabled = true;
            $("locality_select").disabled = true;
        }
  }

  function clear_reference_search(event) {
    $('reference_search_input').value = "";
  }

  function set_outer_heights() {
    var content_height = $('content').getHeight() + 1;
    var outer_height_css = content_height + 'px'; 
        $('outer_left_content').setStyle({'height': outer_height_css});
    $('outer_right_content').setStyle({'height': outer_height_css});

    var footer_content_height = $('footer_content').getHeight() + 1;
    var footer_outer_height_css = footer_content_height + 'px';
    $('outer_left_footer').setStyle({'height': footer_outer_height_css});
    $('outer_right_footer').setStyle({'height': footer_outer_height_css});
  }

    //function cena_cena() {
    //    Lightbox.start($("natal_link"));
    //} 

  function init_buyer() {
    $('id_island').observe('change', island_event_handler);
    $('id_municipality').observe('change', municipality_event_handler);
    $('id_min_price').observe('change', min_price_event_handler);
    $('id_max_price').observe('change', max_price_event_handler);
    $('id_estate_type').observe('change', buyer_type_event_handler);

    update_municipality("id_municipality", "");
    update_locality("id_locality", "");
    update_subtype("id_min_estate_subtype", "");
    update_subtype("id_max_estate_subtype", "");

    update_buyer_subtype("id_min_estate_subtype", "");
    update_buyer_subtype("id_max_estate_subtype", "");
  }

  function init() {
    //set_outer_heights();

        // NATAL
        //if($("natal_link") != null) {
        //    Lightbox.initialize({animate: false, imageDataLocation : 'north'});
        //    window.setTimeout('cena_cena()', 1500);
        //}
        
    $('island_select').observe('change', island_event_handler);
    $('municipality_select').observe('change', municipality_event_handler);
    $('min_price_select').observe('change', min_price_event_handler);
    $('max_price_select').observe('change', max_price_event_handler);
    $('reference_search_input').observe('click', clear_reference_search);
    $('type_select').observe('change', type_event_handler);
    $('subtype_select').observe('change', update_area);
    $('location_select').observe('change', location_event_handler);

    update_municipality("municipality_select", "");
    update_locality("locality_select", "");
    update_subtype("subtype_select", "");

    update_area();
    $('id_mainland_municipality_container').hide();
  }


