// prepare the form when the DOM is ready 
$(document).ready(function() { 
    var options = { 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse,  // post-submit callback 
 
        // other available options: 
        url:       '/users/user/registration',        // override for form's 'action' attribute 
        dataType: 'json'        // 'xml', 'script', or 'json' (expected server response type) 
    }; 
    
    var optionsPopup = { 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponsePopup,  // post-submit callback 
 
        // other available options: 
        url:       '/users/user/registration',        // override for form's 'action' attribute 
        dataType: 'json'        // 'xml', 'script', or 'json' (expected server response type) 
    }; 
 
    var optionsLogin = { 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponseLogin,  // post-submit callback 
 
        // other available options: 
        url:       '/site/login',        // override for form's 'action' attribute 
        dataType: 'json'        // 'xml', 'script', or 'json' (expected server response type) 
    }; 
    
     var optionsLoginPopup = { 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponseLoginPopup,  // post-submit callback 
 
        // other available options: 
        url:       '/site/login',        // override for form's 'action' attribute 
        dataType: 'json'        // 'xml', 'script', or 'json' (expected server response type) 
    }; 
    
    // bind to the form's submit event 
    $('#login_form_popup').submit(function() { 
        $(this).ajaxSubmit(optionsLoginPopup); 
        return false; 
    }); 
    
     // bind to the form's submit event 
    $('#login_form').submit(function() { 
        $('.errors_login').html('');
        $(this).ajaxSubmit(optionsLogin); 
        return false; 
    }); 
    // bind to the form's submit event 
    $('#sh_reg_form').submit(function() { 
        $(this).ajaxSubmit(options); 
        return false; 
    }); 
    
    // bind to the form's submit event 
    $('#reg_form_popup').submit(function() { 
        $(this).ajaxSubmit(optionsPopup); 
        return false; 
    }); 
    
    $(".submit_form_reg").on('click', function () {
        $('#sh_reg_form').ajaxSubmit(options); 
        return false; 
    });
    
    $(".submit_form_reg_popup").on('click', function () {
        $('#reg_form_popup').ajaxSubmit(optionsPopup); 
        return false; 
    });
    
    $(".submit_form_login_popup").on('click', function () {
        $('#login_form_popup').ajaxSubmit(optionsLoginPopup); 
        return false; 
    });
    
    $(".submit_form_login").on('click', function () {
        $('#login_form').ajaxSubmit(optionsLogin); 
        return false; 
    });
    
    $(".login_form_link").on('click', function () {
        $('#login_form').show();
        $('#sh_reg_form').hide();
    });
    
    $(".login_form_popup_link").on('click', function () {
        $('#login_form_popup').show();
        $('#reg_form_popup').hide();
    });
    
    $(".reg_popup_link").on('click', function () {
        $('#reg_form_popup').show();
        $('#login_form_popup').hide();
    });
    
    $(".reg_form_link").on('click', function () {
        $('#sh_reg_form').show();
        $('#login_form').hide();
    });
    
    $('body').on('click', '.call_form_reg', function(){
       link = $(this);
       title = 'Регистрация на курс <br>'+link.data('title');
       spec_propotion = link.data('spec');
       
       $('.popup__title2').html(title);
       if(spec_propotion){
          $('.spec_propotition').show();
       }
       else {
          $('.spec_propotition').hide(); 
       }
       /*jQuery.ajax({
           type: 'POST',
           url: "/school/lessons-admin/update-form-document",
           data: {'docId': doc},
           success: function(data){  
              jQuery('#doc-upd').append(
                    data      
              );
           }
        });*/
    });
}); 
 
// pre-submit callback 
function showRequest(formData, jqForm, options) { 
    var queryString = $.param(formData); 
 
    return true; 
} 
 
// post-submit callback 
function showResponse(responseText, statusText, xhr, $form)  { 
    $('.errors-reg').html('');
    if (responseText.errors){
        $.each(responseText.errors, function(key,value) {
            $('.errors-reg').append(value+'<br>');
            $('#sh_reg_form .field-user-'+key).addClass('has-error');
          });
    }
    else{
        $('#sh_reg_form').html('<div class="alert alert-block alert-success">Регистрация успешно завершена.</div>');
    }   
}  
// post-submit callback 
function showResponsePopup(responseText, statusText, xhr, $form)  { 
    $('.errors-reg_popup').html('');
    if (responseText.errors){
        $.each(responseText.errors, function(key,value) {
            $('.errors-reg_popup').append(value+'<br>');
            $('#reg_form_popup .field-user-'+key).addClass('has-error');
          });
    }
    else{
        $('#sh_reg_form').html('<div class="alert alert-block alert-success">Регистрация успешно завершена.</div>');
        $('#reg_form_popup').html('<div class="alert alert-block alert-success">Регистрация успешно завершена.</div>');
    }   
}
// post-submit callback 
function showResponseLogin(responseText, statusText, xhr, $form)  { 
    $('.errors_login').html('');
    if (responseText.errors){
        $.each(responseText.errors, function(key,value) {
            $('.errors_login').append(value+'<br>');
            $('#login_form .field-loginform-'+key).addClass('has-error');
          });
    } 
} 

// post-submit callback 
function showResponseLoginPopup(responseText, statusText, xhr, $form)  { 
    $('.errors_login_popup').html('');
    if (responseText.errors){
        $.each(responseText.errors, function(key,value) {
            $('.errors_login_popup').append(value+'<br>');
            $('#login_form_popup .field-loginform-'+key).addClass('has-error');
          });
    } 
} 