$('document').ready(function () {
    var falcode_state = false;

    $('#falcode').on('blur', function () {
        var falcode = $('#falcode').val();
        var loc = window.location.href.match(/^.*\//);
        loc = loc + 'addFaculty.php';
        if (falcode == '') {
            falcode_state = false;
            return;
        }
        $.ajax({
            url: loc,
            type: 'post',
            data: {
                'falcode_check': 1,
                'falcode': falcode,
            },
            success: function (response) {
                //alert('resp' + response)
                if (response == 'taken') {
                    falcode_state = false;
                    $('#falcode').parent().removeClass();
                    $('#falcode').parent().addClass("form_error");
                    $('#falcode').siblings("span").text('Sorry... Faculty Code already taken');
                } else if (response == 'not_taken') {
                    //alert("not taken");
                    falcode_state = true;
                    $('#falcode').parent().removeClass();
                    $('#falcode').parent().addClass("form_success");
                    $('#falcode').siblings("span").text('Faculty Code available');
                }
            }
        });
    });


    $('#reg_btn').on('click', function () {
        var falcode = $('#falcode').val();
        var description = $('#description').val();

        if (falcode_state == false) {
            $('#error_msg').text('Fix the errors in the form first');
            return false;
        } else {
            // proceed with form submission
            $.ajax({
                url: loc,
                type: 'post',
                data: {
                    'save': 1,
                    'falcode': falcode,
                    'description': description,
                },
                success: function (response) {

                    alert('resp' + response)
                    alert('Faculty of ' + description + ' saved');
                    $('#falcode').val('');
                    $('#description').val('');

                }
            
            });
            return false;
        }
        return false;
    });
    return false;
});