var server_file = '/ajax_request/';

function show_mail_block(atype) {
    document.getElementById('alert_mess_' + atype).style.display = 'none';
    document.getElementById('fields_block_' + atype).style.display = 'block';
}

function mail_block_show_ava(obj_id, atype) {
    if (document.getElementById(obj_id)) document.getElementById(obj_id).style.display = 'block';

    $('#mess_' + atype + '_email_block').autogrow();
}

function rolet_block(rolet_id, speed, atype, obj) {
    if (obj) $(obj).hide();
    else $('#call_link_' + atype).hide();

    $('#' + rolet_id).slideDown(speed);
    $('#frbp_' + atype).hide();
    $('#show_button_' + atype).hide();

    $('#alert_mess_' + atype + '_email_block').html('').hide();
    $('#alert_mess_' + atype + '_callback_block').html('').hide();
    $('#alert_mess_' + atype + '_call_gager_block').html('').hide();

    $('#error_mess_' + atype + '_email_block').html('').hide();
    $('#error_mess_' + atype + '_callback_block').html('').hide();
    $('#error_mess_' + atype + '_call_gager_block').html('').hide();

    $('#alert_mess_block_' + atype + '_email_block').hide();
    $('#alert_mess_text_block_' + atype + '_email_block').html('');

    $('#mess_' + atype + '_email_block').autogrow();

    return false;
}

function rolet_block_show(rolet_id, speed, atype, type) {
    $('#call_link_' + atype).show();

    $('#' + rolet_id).slideUp(speed, function() {
        $('#frbp_' + atype).show();
        $('#show_button_' + atype).show();
    });
}

function show_mail_block_first(atype) {
    document.getElementById('mail_block_' + atype).style.display = 'block';
}

function show_mail_button_first(atype) {
    document.getElementById('show_button_' + atype).style.display = 'block';
    if (document.getElementById('call_link_' + atype)) document.getElementById('call_link_' + atype).style.display = 'inline';
}

function change_country(obj, obj_val, atype) {


    var prev_sel_menu = $('.selected_' + atype);
    var new_sel_menu = $('#cityes_' + atype + '_' + obj_val).next();

    prev_sel_menu.parent().hide();
    prev_sel_menu.removeClass('selected_' + atype);
    prev_sel_menu.removeClass(atype);

    new_sel_menu.parent().show();
    new_sel_menu.addClass('selected_' + atype);
    new_sel_menu.addClass(atype);
}

$(document).ready(function() {

    jQuery.listen('change', '.change_country', function() {
        change_country($(this), $(this).val(), $(this).attr('name').replace('country_', ''));
        return false;
    });

    jQuery.listen('mouseup', '#mail_block_button', function() {
        send_mail($(this), $(this).attr('name'), $(this).parent().parent().next().attr('name'));
        return false;
    });

    function send_mail(but, atype, type) {

        var atype_suffix = '_' + atype;
        hide_error_mess_suffix(atype);

        $('#error_mess' + atype_suffix).hide();
        $('#alert_mess' + atype_suffix).hide();
		
		$('#alert_mess_block_agreement_' + atype).hide();

        var vars_arr = check_form_v2(atype);
        var vars = new Object();
        jQuery.each(vars_arr['vars'], function(i, field) {
            vars[i.replace(atype_suffix, '')] = field;
        });

        var objects = vars_arr['objects'];
        var error = vars_arr['error'];

        if (error == 0) {

            loading_nice_form('show', type);

            $('.' + atype).attr("disabled", true);
            but.attr("disabled", true);

            $.ajax({
                async: true,
                type: 'post',
                data: {
                    vars: vars,
                    server_class: 'mailto_ajax_v2'
                },
                url: server_file,
                dataType: 'json',
                success: function(data) {

                    clear_error_fields(objects);

                    var sicret_fields = autoreg_all_sicret_fields_clear(atype);

                    $('#name' + atype_suffix).show();
                    $('#phone' + atype_suffix).val($('#phone' + atype_suffix).next().val()).addClass('gr');

                    if (atype == 'mailto_firm_email_block' || atype == 'mailto_board_email_block') {

                        if (data['error'] == false) {

                            objects['mess' + atype_suffix].addClass('gr');
                            objects['mess' + atype_suffix].val(objects['mess' + atype_suffix].next().val());
                            objects['mess' + atype_suffix].height('');

                            if (data['new_form']) $('#email_block' + atype_suffix.replace('_email_block', '')).html(data['new_form']);

                            if (data['error_mess']) {
                                $('#error_mess' + atype_suffix).html(data['error_mess']).show();
                                sicret_fields['pass_block'].show();
                                sicret_fields['pre_pass'].show();
                                sicret_fields['pre_pass'].addClass(atype)
                            }

                            if (data['alert_mess']) {
                                $('#alert_mess' + atype_suffix).html(data['alert_mess']).show();
                                $('#fields_block' + atype_suffix).hide();
                                $('#alert_mess' + atype_suffix).delay(3000).fadeOut(400, function() {
                                    $('#email_block' + atype_suffix.replace('_email_block', '')).hide();
                                    $('#show_button' + atype_suffix.replace('_email_block', '')).show();
                                    $('#frbp' + atype_suffix.replace('_email_block', '')).show();
                                    $('#fields_block' + atype_suffix).show();
                                    $('#call_link' + atype_suffix.replace('_email_block', '')).show();
                                });
                            }

                            sicret_fields['pass_block'].show();
                            sicret_fields['pre_pass'].show();
                            sicret_fields['pre_pass'].addClass(atype);

                            $('#mail_link_open_form' + atype_suffix).show();

                        } else {
                            if (data['error'] == 1) {
                                show_error_mess_suffix('Неверно указаны логин или пароль', atype);
                                sicret_fields['pass_block'].show();
                                sicret_fields['pre_pass'].show();
                                sicret_fields['pre_pass'].addClass(atype)
                            }
                            if (data['error'] == 2) {
								show_error_mess_suffix ('Этот e-mail уже присутствует в базе данных, но не подтвержден.<br><a name="'+atype+'" class="autoreg_reg_mail_confirm" href="#">Отправить подтверждение</a> на '+vars['email'], atype);
                                sicret_fields['pass_block'].show();
                                sicret_fields['pre_pass'].show();
                                sicret_fields['pre_pass'].addClass(atype)
                            }
                            if (data['error'] == 3) {
                                sicret_fields['name_block'].show();
                                sicret_fields['name'].addClass(atype)
                            }
                            if (data['error'] == 4) {
                                show_error_mess_suffix(data['error_mess'], atype);
                            }

                            set_error_fields_suffix(data['error_fields'], atype);
                        }

                    } else if (atype == 'mailto_firm_callback_block' || atype == 'mailto_board_callback_block') {
                        if (data['error'] == false) {
                            if (data['alert_mess']) {
                                $('#alert_mess' + atype_suffix).html(data['alert_mess']).show();
                                $('#fields_block' + atype_suffix).hide();
                                $('#alert_mess' + atype_suffix).delay(3000).fadeOut(400, function() {
                                    $('#callback_block' + atype_suffix.replace('_callback_block', '')).hide();
                                    $('#show_button' + atype_suffix.replace('_callback_block', '')).show();
                                    $('#frbp' + atype_suffix.replace('_callback_block', '')).show();
                                    $('#fields_block' + atype_suffix).show();
                                    $('#call_link' + atype_suffix.replace('_callback_block', '')).show();
                                });
                            }
                        }
                    } else if (atype == 'mailto_firm_call_gager_block' || atype == 'mailto_board_call_gager_block') {
                        if (data['error'] == false) {
                            if (data['alert_mess']) {
                                $('#alert_mess' + atype_suffix).html(data['alert_mess']).show();
                                $('#fields_block' + atype_suffix).hide();
                                $('#alert_mess' + atype_suffix).delay(3000).fadeOut(400, function() {
                                    $('#call_gager_block' + atype_suffix.replace('_call_gager_block', '')).hide();
                                    $('#show_button' + atype_suffix.replace('_call_gager_block', '')).show();
                                    $('#frbp' + atype_suffix.replace('_call_gager_block', '')).show();
                                    $('#fields_block' + atype_suffix).show();
                                    $('#call_link' + atype_suffix.replace('_call_gager_block', '')).show();
                                });
                            }
                        }
                    }

                    $('.' + atype).attr("disabled", false);
                    but.attr("disabled", false);
                    loading_nice_form('hide', type);
                }
            });

        }

    }
}); 
