/*
parses url like:    http://kahten.co.il/ru/admin/city/view/2
and returns path:   http://kahten.co.il/ru/admin/city/
*/
function findActionUrl(){
    return window.location.href.replace(/^(.*?\/)(view)([\w\/%]*\/)?(\w+(?:\.\w+)?)?(?:\?([\w=&%\-\+]+))?(?:#(\w+))?$/, "$1");
}

function editElement()
{
    var selected_checkbox = $("table.usual tbody input:checkbox[checked=true]:not(.select_all):first");
    if (sel_id = $(selected_checkbox).val()) {
        window.location.replace(findActionUrl()+"edit/"+sel_id);
    }
    else {
        message(__t("No records selected"), 'warning', 1500);
    }
    return false;
}

function deleteElement()
{
    var ids = new Array();
    $("table.usual tbody input:checkbox[checked=true]:not(.select_all)").each(function(){
        ids.push($(this).val());
    });
    if (ids.length>0) {
        // send deletion request
        $.post(findActionUrl()+"delete", { 'ids[]':ids },
        function(data){
            // just reload current page to see results and messages from server
            $.get(window.location.href, {}, function(data){
                $.unblockUI();
                // we do delay() to leave some time to make unblockUI effects
                $('#content').delay(300).slideUp("fast", function(){
                    $(this).empty().html(data).slideDown("fast");
                });
            });
        }, "json");
    }
    else {
        message(__t("No records selected"), 'warning', 1500);
    }
}
