$(function()
{
    $('#inputGroupSubmit').attr('disabled', 'disabled');
    // Выбор факультета
    $('#facultySelect').change(function(){
        if (($('#facultySelect option:selected').val()!= '0')) {
                $('#inputGroupSubmit').removeAttr('disabled');
                $("#departmentSelect").html('');
                $('#departmentSelect').attr('disabled', 'disabled');
                $.post( URLdir + 'handler/settings/getDepartmentsList', 
                        {'facultyID': $('#facultySelect option:selected').val()}, 
                        function(data){
                            $.each(data, function(i) {
                                var dep = data[i];
                                if (!dep.Name) dep.Name = 'Служебная кафедра';
                                $("#departmentSelect").append('<option value="' + dep.ID + '">' + dep.Name + '</option>');
                            });
                            $("#departmentSelect").removeAttr("disabled");
                        }, 
                        "json");
        }
        else
        {
                $('#departmentSelect').attr('disabled', 'disabled');
        }
    });
    
    $('#firstName').change(function(){
        if (($('#firstName').val()!= '0')) {
            $('#inputGroupSubmit').removeAttr('disabled');
        }
    }); 
    
    $('#secondName').change(function(){
        if (($('#secondName').val()!= '0')) {
            $('#inputGroupSubmit').removeAttr('disabled');
        }
    }); 
    
    $('#lastName').change(function(){
        if (($('#lastName').val()!= '0')) {
            $('#inputGroupSubmit').removeAttr('disabled');
        }
    }); 
    
    $('#departmentSelect').change(function(){
        if (($('#departmentSelect option:selected').val()!= '0')) {
            $('#inputGroupSubmit').removeAttr('disabled');
        }
    }); 
    
    $('#jobPositionSelect').change(function(){
        if (($('#jobPositionSelect option:selected').val()!= '0')) {
            $('#inputGroupSubmit').removeAttr('disabled');
        }
    }); 
    
    $('#inputGroupSubmit').click(function(){
        $('#inputGroupSubmit').attr('disabled', 'disabled');
        $.post(URLdir + 'handler/settings/editProfile', 
        {
            'firstName': $('#firstName').val(),
            'secondName': $('#secondName').val(),
            'lastName': $('#lastName').val(),
            'jobPositionID': $('#jobPositionSelect option:selected').val(),
            'departmentID': $('#departmentSelect option:selected').val(),
        }, 
        function(data){
            $('.inputGroupMessages').html('<ul>');
            $.each(data.messages, function(i){
                $('.inputGroupMessages').append('<li>' + data.messages[i]);
            });
            $('.inputGroupMessages').append('</ul>');
            $('#inputGroupSubmit').removeAttr('disabled');
            window.location.href = sitePath;
        }, "json");
    });
});