function filter_result() {
    var url = "/search/events";
    
    var keywords = $('#keywords')[0].value;
    var country_id = $('#country')[0].value;
    var state_id   = $('#state')[0].value;
    var city_id    = $('#city')[0].value;
    var category_id = $('#category_id')[0].value;
    
    if (keywords) {
        url = url + '/' + keywords;
    }
    url = url + "/country_id=" + country_id;
    if (state_id != 0) {
        url = url + "/state_id=" + state_id;
    }
    if (city_id != 0) {
        url = url + "/city_id=" + city_id;
    }
    if (category_id != 0) {
        url += '/category_id=' + category_id;
    }
    
    window.location = url;
}

function attend(type, username, group_code, event_id) {
    var url = '/event/ajax?action=' + type + '&username=' + username + '&group_code=' + group_code + '&event_id=' + event_id;
    
    $('#event' + event_id).text('Confirming');
    
    $.getJSON(url, function(json) {
        var event_selector = '#event' + json.event_id;
        var error = json.error;
        if (! error) {
            var event_id = json.event_id;
            var username = json.username;
            var group_code = json.group_code;
            var is_leave = json.is_leave;
            var html = '<a href="javascript:attend(';
            if (is_leave == 1) {
                html = html + "'add', ";
            } else {
                html = html + "'remove', ";
            }
            html = html + "'" + username + "', '" + group_code + "', " + event_id + ');">';
            if (is_leave == 1) {
                html = html + "Attend";
            } else {
                html = html + "Leave&nbsp;";
            }
            html = html + "</a>";

            $(event_selector).html(html);
        } else {
            if (error == 'LOGIN') {
                window.location = '/login';
            } else {
                $(event_selector).text(error);
            }
        }
    } );
}