Skip to content
Snippets Groups Projects
script.js 8.75 KiB
Newer Older
nicrausaz's avatar
nicrausaz committed
$(document).ready(function () {
    initButtonsAction();
    if (location.pathname.split("/").slice(-1)[0] == "form.php") {
        initJobChange();
        initButtonsAction();
        initAddChildButtons();
        initAddRadioButtonEvent();
        initDateChecker();
        initDatepicker();
nicrausaz's avatar
nicrausaz committed
        checkRequired();
nicrausaz's avatar
nicrausaz committed
    }
});

nicrausaz's avatar
nicrausaz committed
function checkDate () {
    var birthdate = new Date(document.getElementById("birthApp").value);
    var cur = new Date();
    var diff = cur - birthdate; // This is the difference in milliseconds
    var age = Math.floor(diff / 31536000000); // Divide by 1000*60*60*24*365

    if (age <= 12 || birthdate.getFullYear() < 1910 || age > 60) {
        document.getElementById('errorBirthdate').innerHTML = 'Date invalide';
    }
    else {
        document.getElementById('errorBirthdate').innerHTML = '';
    }
nicrausaz's avatar
nicrausaz committed
}

function initDateChecker() {
    $("#birthApp").change(function () {
        checkDate();
    });

nicrausaz's avatar
nicrausaz committed
    $("#dejaCandAnneeInput").change(function () {
        checkYearDate("#dejaCandAnneeInput", "#dejaCandError", 0);
nicrausaz's avatar
nicrausaz committed
    });
    $("#anneeFin").change(function () {
        checkYearDate("#anneeFin", "#anneeFinError", 1);
    });
}

function addChildren(parentId, maxItems, childTemplate, addButtonId) {
nicrausaz's avatar
nicrausaz committed
    var i = $(parentId + ' > tbody').children().length + 1;
nicrausaz's avatar
nicrausaz committed
    if (i < maxItems) {
        var fullTemplate = childTemplate.replace(/\{(.*?)\}/g, i);
        $(parentId).append(fullTemplate);
        if (i == maxItems - 1) {
            $(addButtonId).hide(750);
        }
    }
}

function changeTitleFile(objFile) {
nicrausaz's avatar
nicrausaz committed
    var filename = $(objFile)[0].value.replace(/^.*[\\/]/, '')
    $(objFile.parentElement)[0].children[3].innerHTML = filename
    checkFileFormat($(objFile)[0]);
nicrausaz's avatar
nicrausaz committed
}

function checkFileFormat(obj) {
nicrausaz's avatar
nicrausaz committed
    var errorZone = $(obj.parentElement)[0].children[5]
    var allowedExtensions = ['pdf', 'jpeg', 'png', 'jpg'];
    var fileExtension = obj.value.split('.').pop().toLowerCase()
nicrausaz's avatar
nicrausaz committed

nicrausaz's avatar
nicrausaz committed
    if (allowedExtensions.indexOf(fileExtension) == -1) {
nicrausaz's avatar
nicrausaz committed
        // extension invalide
nicrausaz's avatar
nicrausaz committed
        errorZone.innerHTML = 'Format invalide'
nicrausaz's avatar
nicrausaz committed
    } else {
nicrausaz's avatar
nicrausaz committed
        // extension valide
nicrausaz's avatar
nicrausaz committed
        errorZone.innerHTML = '';
nicrausaz's avatar
nicrausaz committed
    }
}

function checkYearDate(toCheckValue, errorZone, coefficiantYear) {
    var inputYear = $(toCheckValue).val();
    var currentYear = (new Date).getFullYear() + coefficiantYear;

    if ((inputYear != parseInt(inputYear, 10)) || (currentYear < inputYear) || (inputYear.length != 4)) {
nicrausaz's avatar
nicrausaz committed
        $(errorZone).html('Date invalide');
nicrausaz's avatar
nicrausaz committed
    } else {
        $(errorZone).html("");
    }
}

function initButtonsAction() {
    $('#createAc').click(function () {
        window.open("https://guests.epfl.ch/selfaddform", '_blank');
    });
    $('#connectB').click(function () {
        document.location.href = "form.php";
    });
    $('#infoFilieres').click(function () {
        window.open("https://www.ict-berufsbildung.ch/fr/formation-professionnelle/formation-initiale-ict/", '_blank');
    });
    $('#infoMpt').click(function () {
        window.open("https://orientation.ch/dyn/show/3309", '_blank');
    });
    $('#retourHome').click(function () {
        document.location.href = "http://apprentis.epfl.ch/";
nicrausaz's avatar
nicrausaz committed
        // TODO: logout Tequila
nicrausaz's avatar
nicrausaz committed
    });
    $('#retourFormulaire').click(function () {
        history.go(-1);
    });
}

function initAddChildButtons() {
    $('#addSch').click(function () {
nicrausaz's avatar
nicrausaz committed
        var template = '<tr><td><input type="text" name="ecole{i}" id="ecole{i}" placeholder="Ecole" class="form-control"/></td><td><input type="text" name="lieuEcole{i}" id="lieuEcole{i}" placeholder="Lieu" class="form-control"/></td><td><input type="text" name="niveauEcole{i}" id="niveauEcole{i}" placeholder="Niveau" class="form-control"/></td><td><input type="text" name="anneesEcole{i}" id="name="anneesEcole{i}" placeholder="de-à (années)" class="form-control"/></td></tr>';
nicrausaz's avatar
nicrausaz committed
        addChildren('#scolaire', 6, template, '#addSch');
    });

    $('#addPro').click(function () {
nicrausaz's avatar
nicrausaz committed
        var template = '<tr><td><input type="text" class="form-control" name="employeurPro{i}" id="employeurPro{i}" placeholder="Employeur"/></td><td><input type="text" class="form-control" name="lieuPro{i}" id="lieuPro{i}" placeholder="Lieu"/></td><td><input type="text" class="form-control" name="activitePro{i}" id="activitePro{i}" placeholder="Activité"/></td><td><input type="text" class="form-control" name="anneesPro{i}" id="anneesPro{i}" placeholder="de-à (années)"/></td></tr>';
nicrausaz's avatar
nicrausaz committed
        addChildren('#activites', 4, template, '#addPro');
    });

    $('#addStage').click(function () {
nicrausaz's avatar
nicrausaz committed
        var template = '<tr><td><input type="text" class="form-control" name="activiteStage{i}" id="activiteStage{i}" placeholder="Métier"></td><td><input type="text" class="form-control" name="entrepriseStage{i}" id="entrepriseStage{i}" placeholder="Entreprise"></td></tr>';
nicrausaz's avatar
nicrausaz committed
        addChildren('#stages', 5, template, '#addStage');
    });

    $('#addInputFile').click(function () {
nicrausaz's avatar
nicrausaz committed
        var nextIndex = $('#newCertifZone > tbody').children().length + 1;
nicrausaz's avatar
nicrausaz committed
        if (nextIndex < 10) {
nicrausaz's avatar
nicrausaz committed
            $('#newCertifZone').append('<tr><td><label for="certifs'+ nextIndex +'">Annexe supplémentaire</label><br>'
            + '<input type="file" name="certifs'+ nextIndex +'" class="upload-input" id="certifs'+ nextIndex +'" onchange="changeTitleFile(this)"/>'
            + '<label for="certifs'+ nextIndex +'" class="btn btn-secondary btn-sm icon-right">Choisir...</label><br>'
            + '<small class="error" class="form-text text-muted"></small></td></tr>');
nicrausaz's avatar
nicrausaz committed
            if (nextIndex == 9) {
                $('#addInputFile').hide(750);
            }
        }
    });
}

function setMajState() {
    $('#maj1')[0].checked ? $("#representants").show(1000) : $("#representants").hide(1000);
}

function setDejaCandState() {
    if ($('#dejaCand1')[0].checked) {
        $("#dejaCandAnnee").hide(750);
nicrausaz's avatar
nicrausaz committed
        $("#dejaCandAnneeInput").val("");
nicrausaz's avatar
nicrausaz committed
        $("#dejaCandError").html("");
    } else {
        $("#dejaCandAnnee").show(750);
    }
}

function setLieuState() {
    if ($('#lieuLausanne')[0].checked) {
        $("#jbLausanne").show(0);
        $("#jbSion").hide(0);
    }
    else if ($('#lieuSion')[0].checked) {
        $("#jbSion").show(0);
        $("#jbLausanne").hide(0);
    }
}

function initAddRadioButtonEvent() {
    $('#maj1').change(setMajState);
    $('#maj2').change(setMajState);
    setMajState();

    $("#dejaCand1").change(setDejaCandState);
    $("#dejaCand2").change(setDejaCandState);
    setDejaCandState();

    $('#lieuLausanne').change(setLieuState);
    $('#lieuSion').change(setLieuState);
}

function clearRepresentants() {
    for (i = 1; i <= 2; i++) {
        $("#genreRep" + i + " :nth-child(1)").prop('selected', true);
        $("#nameRep" + i).val("");
        $("#surnameRep" + i).val("");
        $("#adrRep" + i).val("");
        $("#NPARep" + i).val("");
        $("#telRep" + i).val("");
    }
}

function initDatepicker() {
nicrausaz's avatar
nicrausaz committed
    $.datepicker.setDefaults($.datepicker.regional["fr"]);
    $("#birthApp").datepicker({ minDate: '-60y', maxDate: '-13y', dateFormat: "dd/mm/yy" });
nicrausaz's avatar
nicrausaz committed
}

function getFormation() {
    if ($('#lieuLausanne').is(":checked")) {
        return $("#jbLausanne option:selected")[0].value;
    }
    else {
        return $("#jbSion option:selected")[0].value;
    }
}

function initJobChange() {
    $(".jobSelectors").change(function () {
        var selectedFormation = getFormation();
        showPolyAndInfoDivs(selectedFormation);
    });
    $("#lieux").change(function () {
        var selectedFormation = getFormation();
        showPolyAndInfoDivs(selectedFormation);
    });
}

function showOnFormReturn(lieu) {
    if (lieu == 'Lausanne') {
        var selectedFormation = $("#jbLausanne option:selected")[0].value;
    }
    else if (lieu == 'Sion') {
        var selectedFormation = $("#jbSion option:selected")[0].value;
    }
    showListJob(lieu)
    showPolyAndInfoDivs(selectedFormation);
}
function showListJob(lieu) {
    if (lieu == 'Lausanne') {
        $("#jbSion").hide();
        $("#jbLausanne").show();
    }
    else if (lieu == 'Sion') {
        $("#jbSion").show();
        $("#jbLausanne").hide();
    }
}
function showPolyAndInfoDivs(selectedFormation) {
    if (selectedFormation != "menu") {
        $("#all").show(1000);
        selectedFormation == "informaticien" ? $("#infoOnly").show(1000) : $("#infoOnly").hide(500);
        selectedFormation == "informaticien" ? $("#griTest").show(1000) : $("#griTest").hide(500);
        selectedFormation == "polyMecanicien" ? $("#polyOnly").show(1000) : $("#polyOnly").hide(500);
    }
}

function checkRequired() {
    $("form").submit(function (e) {

        $($(this).find("[data-required]")).each(function () {
            if ($(this).val() == '') {
                alert("Certains champs requis n'ont pas été remplis.");
                e.preventDefault();
                return false;
            }
        });
        return true;
    });
}