$(function() {

    $(".toggleDisplayNone").click(function(e) {
        $(this).parent().find('.displayNone')[$(this).attr("checked") ? "show" : "hide"]();
    });

    $(".toggleNext").click(function(e) {
       $(this).next()[$(this).next().is(":visible") ? "hide" : "show"]();
    });

    // allgemein
    $("#innerContent #bewerberTool table tr").each(function() {
        $(this).find("td:first").removeClass("textZelle");
    });
    $('.openNext').click(function(){
        if ($(this).next().hasClass('displayNone')) $(this).next().removeClass('displayNone');
        else $(this).next().addClass('displayNone');
    });

    $(".ttInfo").tooltip();
    $(".tabs").tabs();
    $(".dataTable").dataTable({
        "bPaginate": false,
        "bInfo": false
    });
    
    $('.date').datepicker({
        showWeek: true,
        weekHeader:"KW",
        firstDay:1,
        dateFormat: 'dd.mm.yy'
    });


    // Bewerber Anmeldung
    $('#bewerberTool #fachbereich').bind('click', function(){
        if ($(this).val() == 3) {
            $('#bewerberTool #fachbereich_text').removeAttr('disabled').removeClass('displayNone').addClass('displayBlock');
            $('#bewerberTool #fachbereich_text_label').removeClass('displayNone').addClass('displayBlock');
        } else {
            $('#bewerberTool #fachbereich_text').attr('disabled', 'disabled').val('').removeClass('displayBlock').addClass('displayNone');
            $('#bewerberTool #fachbereich_text_label').removeClass('displayBlock').addClass('displayNone');
        }
    });

    $('#bewerberTool #fachsemester').bind('click', function(){
        if ($(this).val() == 3) {
            $('#bewerberTool #fachsemester_text').removeAttr('disabled').removeClass('displayNone').addClass('displayBlock');
            $('#bewerberTool #fachsemester_text_label').removeClass('displayNone').addClass('displayBlock');
        } else {
            $('#bewerberTool #fachsemester_text').attr('disabled', 'disabled').val('').removeClass('displayBlock').addClass('displayNone');
            $('#bewerberTool #fachsemester_text_label').removeClass('displayBlock').addClass('displayNone');
        }
    });

    $('#suche_sonstige_checkbox').bind('click',function(){
        if ($(this).attr('checked', 'checked')) {
            $('#bewerberTool #suche_sonstige_text').removeAttr('disabled').removeClass('displayNone').addClass('displayBlock');
            $('#bewerberTool #suche_sonstige_label').removeClass('displayNone').addClass('displayBlock');
        } else {
            $('#bewerberTool #suche_sonstige_text').attr('disabled', 'disabled').val('').removeClass('displayBlock').addClass('displayNone');
            $('#bewerberTool #suche_sonstige_label').removeClass('displayBlock').addClass('displayNone');
        }
    });
    $('.geburtsdatum').bind('focus', function() {
       if ($(this).val() == '00' || $(this).val() == '0000') $(this).val('');
    });

    $('#uploadLebenslauf').click(function(){
       if (confirm("Sind Sie sich sicher, dass Ihr Lebenslauf keine direkten Kontaktdaten enthält?")) {
           location.href='anmeldung.php?step=backend_bewerber_uebersicht';
       } else return false;
    });

    


    // Bewerberbackend
    $('.teilnehmen').click(function(){
       if (confirm("Wollen Sie wirklich teilnehmen?")) {
           location.href='anmeldung.php?step=backend_bewerber_uebersicht';
       } else return false;
    });

    $('.absagen').click(function(){
       if (confirm("Wollen Sie wirklich absagen?")) {
           location.href='anmeldung.php?step=backend_bewerber_uebersicht';
       } else return false;
    });

    // Adminbackend
    $('.pdfFreigeben').click(function(){
       if (confirm("Wollen Sie das PDF wirklich freigeben?")) {
           location.href='admin.php?page=admin_bewerber_liste';
       } else return false;
    });

    $('.pdfFreigabeAufheben').click(function(){
       if (confirm("Wollen Sie die Freigabe wirklich aufheben?")) {
           location.href='admin.php?page=admin_bewerber_liste';
       } else return false;
    });

    $('input[name=beginn_datum]').bind('change', function() {
       if ($('input[name=ende_datum]').val() == '') $('input[name=ende_datum]').val($(this).val());

        var dateParts = $(this).val().split(".");
        var dateTime = new Date(dateParts[2], dateParts[1], dateParts[0]);
        var dayBefore = new Date(dateTime.getTime() - 86400000);
        var twoDaysBefore = new Date(dateTime.getTime() - 2 * 86400000);

        var dayBeforeString = dayBefore.getDate() + "." +  ( dayBefore.getMonth() < 10 ? "0" + dayBefore.getMonth() : dayBefore.getMonth()) + "." + dayBefore.getFullYear();
        var twoDaysBeforeString = twoDaysBefore.getDate() + "." +  ( twoDaysBefore.getMonth() < 10 ? "0" + twoDaysBefore.getMonth() : twoDaysBefore.getMonth()) + "." + twoDaysBefore.getFullYear();

       if ($('input[name=bewerbefrist_datum]').val() == '') $('input[name=bewerbefrist_datum]').val(twoDaysBeforeString);
       if ($('input[name=einladefrist_datum]').val() == '') $('input[name=einladefrist_datum]').val(dayBeforeString);
    });
});
