/**
 * Created by voknil on 26.06.14.
 */

function preview_action(this_id, table_id, mode, form_id, csrf, filter_id) {
    var id_prefix = 'form_preview_';
    var target_url = 'forms.php?table=' + table_id;
    if (filter_id) target_url += '&filter=' + filter_id;
    target_url += "&mode=" + mode + '&form=' + form_id;
    var this_window = window;
    switch (this_id) {
        case id_prefix + 'start':
            document.getElementById(this_id).style.backgroundImage = "url('images/process_small.gif')";
            $.ajax({
                url: target_url + '&ajax_call',
                type: 'post',
                data: {csrf: csrf},
                success: function () {
                    this_window.opener.location.reload();
                    this_window.close();
                }
            });
            break;
        case id_prefix + 'edit':
            location.href = target_url + '&edit&ajax_edit';
            break;
        case id_prefix + 'archive':
            document.getElementById(this_id).style.backgroundImage = "url('images/process_small.gif')";
            document.getElementById(this_id).style.backgroundPosition = "0 0";
            $.ajax({
                url: target_url,
                type: 'post',
                data: {
                    csrf: csrf,
                    sel: 'archive_form',
                    form_id: form_id
                },
                success: function () {
                    try {
                        this_window.opener.remove_view_form(form_id, mode);
                    }
                    catch (e) {
                        this_window.opener.location.reload();
                    }
                    finally {
                        this_window.close();
                    }
                }
            });
            break;
        case id_prefix + 'delete':
            document.getElementById(this_id).style.backgroundImage = "url('images/process_small.gif')";
            document.getElementById(this_id).style.backgroundPosition = "0 0";
            $.ajax({
                url: target_url + '&delete',
                type: 'post',
                data: {
                    csrf: csrf,
                    form_id: form_id,
                    delete: 'delete'
                },
                success: function () {
                    try {
                        this_window.opener.remove_view_form(form_id, mode);
                    }
                    catch (e) {
                        this_window.opener.location.reload();
                    }
                    finally {
                        this_window.close();
                    }
                }
            });
            break;
    }
}