/* Minification failed. Returning unminified contents.
(2,29): run-time error CSS1031: Expected selector, found ';'
(2,29): run-time error CSS1025: Expected comma or open brace, found ';'
(5,1): run-time error CSS1019: Unexpected token, found '$'
(5,2): run-time error CSS1019: Unexpected token, found '('
(5,11): run-time error CSS1031: Expected selector, found ')'
(5,11): run-time error CSS1025: Expected comma or open brace, found ')'
(221,2): run-time error CSS1019: Unexpected token, found ')'
(223,10): run-time error CSS1031: Expected selector, found 'validate('
(223,10): run-time error CSS1025: Expected comma or open brace, found 'validate('
(227,10): run-time error CSS1031: Expected selector, found 'bindConditionalValidationRules('
(227,10): run-time error CSS1025: Expected comma or open brace, found 'bindConditionalValidationRules('
(329,10): run-time error CSS1031: Expected selector, found 'getFileExtension('
(329,10): run-time error CSS1025: Expected comma or open brace, found 'getFileExtension('
(334,10): run-time error CSS1031: Expected selector, found 'validateResumeExtensions('
(334,10): run-time error CSS1025: Expected comma or open brace, found 'validateResumeExtensions('
(374,10): run-time error CSS1031: Expected selector, found 'GetLocationByPostionId('
(374,10): run-time error CSS1025: Expected comma or open brace, found 'GetLocationByPostionId('
(379,20): run-time error CSS1031: Expected selector, found '='
(379,20): run-time error CSS1025: Expected comma or open brace, found '='
(392,10): run-time error CSS1031: Expected selector, found 'ShowOtherEntryBox('
(392,10): run-time error CSS1025: Expected comma or open brace, found 'ShowOtherEntryBox('
(403,10): run-time error CSS1031: Expected selector, found 'GetJobDescription('
(403,10): run-time error CSS1025: Expected comma or open brace, found 'GetJobDescription('
 */
var appForm,
    resumeDocExtensionsArray;


$(document).ready(function () {
    resumeDocExtensionsArray = new Array();

    resumeDocExtensionsArray.push('doc');
    resumeDocExtensionsArray.push('docx');
    resumeDocExtensionsArray.push('pdf');

    appForm = $('#frmEmploymentApplication');

    appForm.find('.selectpicker').selectpicker();

    appForm.find('.datepicker').datepicker();

    appForm.on('click', '.btn-navigation', function () {
        var activeButton = $(this),
            activeForm = appForm.children('.form-horizontal.active-form'),
            activeFormIndex = parseInt(activeForm.data('idx'), 10),
            nextForm = appForm.children('[data-idx="' + (activeFormIndex + 1) + '"]'),
            nextFormIndex = parseInt(nextForm.data('idx'), 10),
            prevForm = appForm.children('[data-idx="' + (activeFormIndex - 1) + '"]'),
            prevFormIndex = parseInt(prevForm.data('idx'), 10);

        if (activeButton.attr('name').indexOf('Next') > 0) {
            appForm.validate();

            bindConditionalValidationRules();

            if (appForm.valid()) {
                if (nextForm.length > 0) {
                    appForm.children('.form-horizontal').removeClass('active-form').addClass('hidden')
                        .end().find('[role="progressbar"]').attr('aria-valuenow', parseInt(nextForm.data('progress-bar-percent'), 10))
                        .width(parseInt(nextForm.data('progress-bar-percent'), 10) + '%')
                        .find('.emp-progress-percentage').text(nextForm.data('progress-bar-title'));
                    nextForm.removeClass('hidden').addClass('active-form');

                    if (nextFormIndex == 7) {
                        activeButton.closest('.row').find('[type="submit"]').removeClass('hidden')
                            .end().end().attr('disabled', true);
                    }

                    activeButton.siblings('button').removeAttr('disabled');
                }
            }
        }
        else {
            if (prevForm.length > 0) {
                appForm.children('.form-horizontal').removeClass('active-form').addClass('hidden')
                    .end().find('[role="progressbar"]').attr('aria-valuenow', parseInt(prevForm.data('progress-bar-percent'), 10))
                    .width(parseInt(prevForm.data('progress-bar-percent'), 10) + '%')
                    .find('.emp-progress-percentage').text(prevForm.data('progress-bar-title'));
                prevForm.removeClass('hidden').addClass('active-form');

                if (prevFormIndex < 7) {
                    activeButton.closest('.row').find('[type="submit"]').addClass('hidden');
                }

                activeButton.siblings().removeAttr('disabled');
            }
        }

    });

    appForm.find('[data-name="work-history-form"] .emp-work-history-panel').filter(':not([data-idx="0"])').addClass('hidden');
    appForm.find('[data-name="work-history-form"] .emp-work-history-panel').filter(':not([data-idx="0"])').addClass('hidden');

    appForm.on('click', '.emp-work-history-panel-remove-btn', function () {

        var activeCloseBtn = $(this),
            activePanel = activeCloseBtn.closest('.emp-work-history-panel'),
            activePanelIdx = parseInt(activePanel.data('idx'), 10);

        activePanel.addClass('hidden')
            .siblings('[data-idx="' + (activePanelIdx - 1) + '"]').find('.emp-work-history-panel-add-btn').removeClass('hidden');
    });

    appForm.on('click', '.emp-work-history-panel-add-btn', function () {

        var activeAddBtn = $(this),
            activePanel = activeAddBtn.closest('.emp-work-history-panel'),
            activePanelIdx = parseInt(activePanel.data('idx'), 10);

        activePanel.siblings('[data-idx="' + (activePanelIdx + 1) + '"]').removeClass('hidden');
        activeAddBtn.addClass('hidden');
    });



    appForm.find('[data-name="educational-background-form"] .emp-school-history-panel').filter(':not([data-idx="0"])').addClass('hidden');
    appForm.find('[data-name="educational-professional-background-form"] .emp-school-history-panel').filter(':not([data-idx="0"],[data-idx="1"])').addClass('hidden');

    appForm.on('click', '.emp-school-history-panel-remove-btn', function () {
        var activeCloseBtn = $(this),
            activePanel = activeCloseBtn.closest('.emp-school-history-panel'),
            activePanelIdx = parseInt(activePanel.data('idx'), 10);

        activePanel.addClass('hidden')
            .siblings('[data-idx="' + (activePanelIdx - 1) + '"]').find('.emp-school-history-panel-add-btn').removeClass('hidden');
    });

    appForm.on('click', '.emp-school-history-panel-add-btn', function () {

        var activeAddBtn = $(this),
            activePanel = activeAddBtn.closest('.emp-school-history-panel'),
            activePanelIdx = parseInt(activePanel.data('idx'), 10);

        activePanel.siblings('[data-idx="' + (activePanelIdx + 1) + '"]').removeClass('hidden');
        activeAddBtn.addClass('hidden');
    });



    appForm.find('[data-name="educational-background-form"] .emp-certification-history-panel').filter(':not([data-idx="0"])').addClass('hidden');

    appForm.on('click', '.emp-certification-history-panel-remove-btn', function () {
        var activeCloseBtn = $(this),
            activePanel = activeCloseBtn.closest('.emp-certification-history-panel'),
            activePanelIdx = parseInt(activePanel.data('idx'), 10);

        activePanel.addClass('hidden')
            .siblings('[data-idx="' + (activePanelIdx - 1) + '"]').find('.emp-certification-history-panel-add-btn').removeClass('hidden');
    });

    appForm.on('click', '.emp-certification-history-panel-add-btn', function () {
        var activeAddBtn = $(this),
            activePanel = activeAddBtn.closest('.emp-certification-history-panel'),
            activePanelIdx = parseInt(activePanel.data('idx'), 10);

        activePanel.siblings('[data-idx="' + (activePanelIdx + 1) + '"]').removeClass('hidden');
        activeAddBtn.addClass('hidden');
    });

    appForm.on('change', '.emp-software-skills-panel .emp-other-software-rdo', function () {
        var activeRadio = $(this);

        if (activeRadio.is(':checked')) {
            activeRadio.closest('table').children('tfoot').removeClass('hidden');
        }
        else {
            activeRadio.closest('table').children('tfoot').addClass('hidden');
        }
    });



    appForm.on('change', '.emp-language-skills-panel [name="GenInfoViewModel.PersonalInfoViewModel.IsBilingual"]', function () {
        var activeRadio = $(this);

        if (activeRadio.val() === "True") {
            activeRadio.closest('.panel').find('.emp-language-proficiencies-table-container').removeClass('hidden')
                .end().find('.emp-non-english-language select').removeAttr('disabled').selectpicker('refresh');
        }
        else {
            activeRadio.closest('.panel').find('.emp-language-proficiencies-table-container').addClass('hidden')
                .end().find('.emp-non-english-language select').attr('disabled', 'disabled').selectpicker('refresh');
        }
    });

    appForm.on('change', 'input[name="GenInfoViewModel_PersonalInfoViewModel_ResumeFileName"]', function () {

        validateResumeExtensions($(this));
    });

    appForm.on('submit', function () {

        var Filedata = document.getElementById("GenInfoViewModel_PersonalInfoViewModel_ResumeFileName");
        if (Filedata.files.length > 0) {
            appForm.validate();
            bindConditionalValidationRules();

            if (appForm.valid()) {
                $('#btnSubmitForm').prop('disabled', true);
                return true;
            }
            else {
                $('#btnSubmitForm').prop('disabled', false);
                return false;
            }
        }
        else {
            errorContainer = appForm.find('[data-valmsg-for="GenInfoViewModel_PersonalInfoViewModel_ResumeFileName"]');
            errorContainer.removeClass('field-validation-valid').addClass('field-validation-error').html('Please uploade resume');
            return false;
        }


    });

    var regExp = /[a-z]/i;
    appForm.on('keydown keyup', '.numbers-only', function (e) {
        var value = (e.key || String.fromCharCode(e.which));

        // No letters
        if (regExp.test(value) && value != "Backspace" && value != "Delete" && value != "Tab") {
            e.preventDefault();
            return false;
        }
    });
    //$("input").keyup(function () {
    //    var nextIndex = parseInt($(this).index()) + 1;

    //    if ($(this).val().length == $(this).attr("maxlength")) {
    //        alert(nextIndex + "/" + $(this).index());
    //        $("input")[nextIndex].focus();
    //    }
    //})
    appForm.on('keyup', 'input[id*="NetworkPrefix"]', function () {
        if (this.value.length == this.maxLength) {
            $(this).closest('.form-group').find('input[id*="FirstComponent"]').focus();
        }
    });

    appForm.on('keyup', 'input[id*=FirstComponent]', function () {
        if (this.value.length == this.maxLength) {
            $(this).closest('.form-group').find('input[id*=SecondComponent]').focus();
        }
    });
});

function validate(objAppForm) {

}

function bindConditionalValidationRules() {
    appForm.find('[name="AppFormViewModel.HeardFromText"]').rules('add', {
        required: {
            depends: function (element) {
                if (appForm.find('[name="AppFormViewModel.HeardFromID"]').val() === "5") {
                    if ($(element).val() != "" || $(element).val() != undefined) {
                        $(element).siblings('[data-valmsg-for="' + $(element).attr('name') + '"]').removeClass('field-validation-error').empty();
                        return true;
                    }
                    else {                        
                        return false;
                    }
                }
            }
        },
        messages: {
            required: '* This field is required.'
        }
    });

    appForm.find('[name="GenInfoViewModel.AddressAndContactViewModel.ReferrerName"]').rules('add', {
        required: {
            depends: function (element) {
                if (appForm.find('[name="GenInfoViewModel.AddressAndContactViewModel.IsReferredByExistingEmployee"]:checked').val() === "False") {
                    $(element).siblings('[data-valmsg-for="' + $(element).attr('name') + '"]').addClass('field-validation-valid')
                        .removeClass('field-validation-error').empty();
                }

                return (appForm.find('[name="GenInfoViewModel.AddressAndContactViewModel.IsReferredByExistingEmployee"]:checked').val() === "True");
            }
        },
        messages: {
            required: '* This field is required.'
        }
    });

    appForm.find('[name="GenInfoViewModel.AddressAndContactViewModel.RelativeName"]').rules('add', {
        required: {
            depends: function (element) {
                if (appForm.find('[name="GenInfoViewModel.AddressAndContactViewModel.IsRelatedToExistingEmployee"]:checked').val() === "False") {
                    $(element).siblings('[data-valmsg-for="' + $(element).attr('name') + '"]').addClass('field-validation-valid')
                        .removeClass('field-validation-error').empty();
                }

                return (appForm.find('[name="GenInfoViewModel.AddressAndContactViewModel.IsRelatedToExistingEmployee"]:checked').val() === "True");
            }
        },
        messages: {
            required: '* This field is required.'
        }
    });
    appForm.find('[name="GenInfoViewModel.ProofOfIdentityViewModel.ConvictionDate"]').rules('add', {
        required: {
            depends: function (element) {
                if (appForm.find('[name="GenInfoViewModel.ProofOfIdentityViewModel.IsEverConvicted"]').val() === "False") {
                    $(element).siblings('[data-valmsg-for="' + $(element).attr('name') + '"]').addClass('field-validation-valid')
                        .removeClass('field-validation-error').empty();
                }

                return (appForm.find('[name="GenInfoViewModel.ProofOfIdentityViewModel.IsEverConvicted"]:checked').val() === "True");
            }
        },
        messages: {
            required: '* This field is required.'
        }
    });

    appForm.find('[name="GenInfoViewModel.ProofOfIdentityViewModel.ConvictionDetails"]').rules('add', {
        required: {
            depends: function (element) {
                if (appForm.find('[name="GenInfoViewModel.ProofOfIdentityViewModel.IsEverConvicted"]:checked').val() === "False") {
                    $(element).siblings('[data-valmsg-for="' + $(element).attr('name') + '"]').addClass('field-validation-valid')
                        .removeClass('field-validation-error').empty();
                }

                return (appForm.find('[name="GenInfoViewModel.ProofOfIdentityViewModel.IsEverConvicted"]:checked').val() === "True");
            }
        },
        messages: {
            required: '* This field is required.'
        }
    });

    for (var idx = 0; idx < 5; idx++) {
        appForm.find('[name="WorkHistoryViewModel.EmploymentDetailsList[' + idx + '].EndDate"]').rules('add', {
            required: {
                depends: function (element) {
                    if (appForm.find('[name="WorkHistoryViewModel.EmploymentDetailsList[' + idx + '].IsCurrentEmployer"]:checked').val() === "True") {
                        $(element).siblings('[data-valmsg-for="' + $(element).attr('name') + '"]').addClass('field-validation-valid')
                            .removeClass('field-validation-error').empty();
                    }

                    return (appForm.find('[name="WorkHistoryViewModel.EmploymentDetailsList[' + idx + '].IsCurrentEmployer"]:checked').val() === "False");
                }
            },
            messages: {
                required: '* This field is required.'
            }
        });
    }
}

function getFileExtension(filename) {
    var ext = /^.+\.([^.]+)$/.exec(filename);
    return ext == null ? "" : ext[1];
}

function validateResumeExtensions(sender) {

    var Filedata = document.getElementById("GenInfoViewModel_PersonalInfoViewModel_ResumeFileName");

    var fileName = $(sender).val(),
        ext = getFileExtension(fileName),
        errorContainer = appForm.find('[data-valmsg-for="' + $(sender).attr('name') + '"]');
    ext = ext.toLowerCase();
    if ($(sender).val().length <= 100) {

        if (parseInt($.inArray(ext, resumeDocExtensionsArray)) > parseInt(-1)) {
            var FileSize = Filedata.files[0].size;
            if (FileSize <= 5242880) {
                errorContainer.removeClass('field-validation-error').addClass('field-validation-valid').html("");
                errorContainer = appForm.find('[data-valmsg-for="GenInfoViewModel_PersonalInfoViewModel_ResumeFileName"]');
                errorContainer.removeClass('field-validation-valid').addClass('field-validation-error').html("");
                return true;
            }
            else {
                errorContainer.removeClass('field-validation-valid').addClass('field-validation-error').html('Please upload 5 MB file size or less than 5 MB')
                //$(sender).replaceWith($(sender).clone(true));
                $(sender).val('');
                return false;
            }
        }
        else {
            errorContainer.removeClass('field-validation-valid').addClass('field-validation-error').html('Only .doc, .docx, .pdf files are allowed!')
            //$(sender).replaceWith($(sender).clone(true));
            $(sender).val('');
            return false;
        }
    }
    else {
        errorContainer.removeClass('field-validation-valid').addClass('field-validation-error')
            .html('Please change the document name only 100 characters are allowed!');
        //$(sender).replaceWith($(sender).clone(true));
        $(sender).val('');
        return false;
    }
}
function GetLocationByPostionId() {
    var PositionId = $("#AppFormViewModel_PositionID").val();
    var json = { PositionId: PositionId };
    $.getJSON("/EmploymentApplication/GetOpenLocationByPositionId", json, updateFields_popup);
}
updateFields_popup = function (data) {

    if (data.length > 0) {
        $('#AppFormViewModel_LocationID').html("");
        for (var i = 0; i < data.length; i++) {
            $('#AppFormViewModel_LocationID').append('<option value="' + data[i].Value + '">' + data[i].Text + '</option>').selectpicker('refresh');;
        }
    }
    else
        optionsValues.append('<option value="0"> No item found..</option>');
};


function ShowOtherEntryBox() {
    var selectedHeardFromID = $("#AppFormViewModel_HeardFromID").val();
    if (selectedHeardFromID == '5')//Other
    {
        $("#HeardFromText").removeClass('hidden');
    }
    else {
        $("#HeardFromText").addClass('hidden');
        $("#AppFormViewModel_HeardFromText").val('');
    }
}
function GetJobDescription() {
    debugger
    var PositionId = $("#AppFormViewModel_PositionID").val();
    if (PositionId > 0) {
        $.ajax({
            url: "/EmploymentApplication/GetJobDescription",
            type: "GET",
            data: { PositionId: PositionId , JobId:0},
            async: false,
            success: function (data) {
                $("#AppFormViewModel_JobDescription").val(data);
            },
            error: function (req, status, error) {
                $("#AppFormViewModel_JobDescription").val("");
            }
        });
    }
}
