function add_watchlist_entry(id)
{   
    $('#watchlist').load('./index.php5?cmd=ajax&mod=watchlist&action=add&id=' + id);
    return false;
}

function delete_watchlist_entry(id)
{
    $('#watchlist').load('./index.php5?cmd=ajax&mod=watchlist&action=delete&id=' + id);
    return false;
}

function another_skill() {

    var i = $('#skills > input').length + 1;
    $('#skills').append('<label for="skill_'+i+'">&nbsp;</label><input id="skill_'+i+'" name="skill[]" type="text" /><br class="clear" />');
    return false;
}

function getUploadMessage(){
    msg = 'Das Senden des Formulars kann in Abhängigkeit von der Größe der Dateien, Ihrer Internetanbindung und der Auslastung des Servers etwas Zeit in Anspruch nehmen!';
    if($('#file_1').val != ''){
        alert(msg);
    }
}

function get_profession()
{   
    if($('#prof_picker').val().length >= 1 )//&& $('#prof_picker').val().length < 2)
    {        
        $.get(
              "./index.php5",
              {cmd : 'ajax', mod : 'profession', action : $('#prof_picker').val().substr(0,1)},
              function(data){
                  $('#prof_selection').empty();
                  $(data).each(function(){                    
                    $('#prof_selection').append('<option value="' + this.id + '">' +  this.name + '</option>');
                  });                  
              },
              'json'
        );
    }
}

function get_skill()
{
    if($('#skill_picker').val().length == 2 )//&& $('#prof_picker').val().length < 2)
    {
        $.get(
              "./index.php5",
              {cmd : 'ajax', mod : 'skill', action : $('#skill_picker').val()},
              function(data){
                  $('#skill_selection').empty();
                  $(data).each(function(){
                    $('#skill_selection').append('<option value="' + this.id + '">' +  this.name + '</option>');
                  });
              },
              'json'
        );
    }
}

function get_categories()
{    
    if($('#category_picker').val().length == 2 )//&& $('#prof_picker').val().length < 2)
    {
        $.get(
              "./index.php5",
              {cmd : 'ajax', mod : 'category', action : $('#category_picker').val()},
              function(data){
                  $('#category_selection').empty();
                  $(data).each(function(){
                    $('#category_selection').append('<option value="' + this.id + '">' +  this.name + '</option>');
                  });
              },
              'json'
        );
    }
}

function get_category()
{
    if($('#category_picker').val().length == 2 )//&& $('#prof_picker').val().length < 2)
    {
        $.get(
              "./index.php5",
              {cmd : 'ajax', mod : 'category', action : $('#category_picker').val()},
              function(data){
                  $('#category_selection').empty();
                  $(data).each(function(){
                    $('#category_selection').append('<option value="' + this.id + '">' +  this.name + '</option>');
                  });
              },
              'json'
        );
    }
}

function showCatSkills(id)
{
    $('#skillcontainer > div').each(function(){
       $(this).css('display', 'none');
    });
    $('#skill_box_'+ id).css('display', 'block');
    return false;
}

//function another_profession()
//{
//    var i = $('#professions > input').length + 1;
//    var content = '<input type="text"  name="profession_'+ i +'" class="add_profession" /><br class="clear" />';
//    $('#professions').append(content);
//}

function another_upload()
{
    var i = $('#uploads > input[type="file"]').length + 1;
    if(i < 5)
    {
        var content = '<input name="file_'+ i + '" type="file" size="50" maxlength="100000" accept="image/*"><br class="clear"/>';
        $('#uploads').append(content);
    }
    else
    {
        alert("Bitte nicht mehr als 4 Dateien speichern!");
    }
    return false;
}



function another_company()
{
    var i = $('#companies > input[type="text"]').length ;
    var content = '<input type="text" id="job'+ i +'" name="job_'+ i +'"  />' +
    '<label class="radiolabel" >als Leiharbeiter t&auml;tig? </label>' +
    '<input type="radio" name="temp_empl_'+ i +'" value="ja" class="radio"  /><div class="floatLeft radio">ja&nbsp;&nbsp;</div>' +
    '<input type="radio" name="temp_empl_'+ i +'" value="nein" class="radio" checked="checked" /><div class="floatLeft radio">nein</div><br class="clear" />';
    $('#companies').append(content);
    return false;
}

function remove_profession(o)
{
    $(o).parent().remove();
    get_profession();
}

function remove_category(o)
{
    $(o).parent().remove();
    get_categories()();
}

function remove_skill(o)
{
    $(o).parent().remove();
    get_skill()();
}

function assume_profession(id, name)
{    
    id = (typeof(id) == 'undefined') ? $('#prof_selection').val() : id;
    name = (typeof(name) == 'undefined') ?  $('#prof_selection :selected').text() : name;   

    var MAXCOUNT = 10;
    var content = '';
    var i = $('#selected_prof > li').length + 1 ;
    
    if(i <= MAXCOUNT && id != null && name != null)
    {
        var exist = false;
        $('#selected_prof > li > input').each(function(){
            if($(this).val() == $('#prof_selection').val())
                exist = true;
        });
        if(!exist)
        {
            content = '<li> '+ i +'. '+ name;
            content += '<button class="row_delete" title="Beruf entfernen" onclick="remove_profession(this);">-</button>';
            content += '<input type="hidden" name="profession_' + i + '" value="'+ id +'" />';
            content += '</li>'
            $('#selected_prof').append( content);
            $('#prof_selection :selected').remove();
        }
        else
        {
            alert(unescape('Beruf wurde bereits ausgew%E4hlt!'));
        }
    }
    else
    {
        alert('Maximal 10 Berufe bitte eingeben!');
    }
    return false;
}

function assume_skill(id, name)
{    
    id = (typeof(id) == 'undefined') ? $('#skill_selection').val() : id;
    name = (typeof(name) == 'undefined') ?  $('#skill_selection :selected').text() : name;

    var MAXCOUNT = 100;
    var content = '';
    var i = $('#selected_skill > li').length + 1 ;

    if(i <= MAXCOUNT && id != null && name != null)
    {
        var exist = false;
        $('#selected_skills > li > input').each(function(){
            if($(this).val() == $('#skill_selection').val())
                exist = true;
        });
        if(!exist)
        {
            content = '<li> '+ i +'. '+ name;
            content += '<button class="row_delete" title="Beruf entfernen" onclick="remove_skill(this);">-</button>';
            content += '<input type="hidden" name="skill_' + i + '" value="'+ id +'" />';
            content += '</li>'
            $('#selected_skill').append( content);
            $('#skill_selection :selected').remove();
        }
        else
        {
            alert(unescape('Beruf wurde bereits ausgew%E4hlt!'));
        }
    }
    else
    {
        alert('Maximal 10 Berufe bitte eingeben!');
    }
    return false;
}

function assume_category(id, name)
{
    id = (typeof(id) == 'undefined') ? $('#category_selection').val() : id;
    name = (typeof(name) == 'undefined') ?  $('#category_selection :selected').text() : name;

    var MAXCOUNT = 100;
    var content = '';
    var i = $('#selected_categories > li').length + 1 ;

    if(i <= MAXCOUNT && id != null && name != null)
    {
        var exist = false;
        $('#selected_categories > li > input').each(function(){
            if($(this).val() == $('#category_selection').val())
                exist = true;
        });
        if(!exist)
        {
            content = '<li> '+ i +'. '+ name;
            content += '<button class="row_delete" title="Beruf entfernen" onclick="remove_category(this);">-</button>';
            content += '<input type="hidden" name="category_' + i + '" value="'+ id +'" />';
            content += '</li>'
            $('#selected_categories').append( content);
            $('#category_selection :selected').remove();
        }
        else
        {
            alert(unescape('Beruf wurde bereits ausgew%E4hlt!'));
        }
    }
    else
    {
        alert('Maximal 10 Berufe bitte eingeben!');
    }
    return false;
}

jQuery(function($){
        $.datepicker.regional['de'] = {clearText: 'löschen', clearStatus: 'aktuelles Datum l�schen',
                closeText: 'schlie�en', closeStatus: 'ohne �nderungen schlie�en',
                prevText: '&#x3c;zur�ck', prevStatus: 'letzten Monat zeigen',
                nextText: 'Vor&#x3e;', nextStatus: 'n�chsten Monat zeigen',
                currentText: 'heute', currentStatus: '',
                monthNames: ['Januar','Februar','M�rz','April','Mai','Juni',
                'Juli','August','September','Oktober','November','Dezember'],
                monthNamesShort: ['Jan','Feb','M�r','Apr','Mai','Jun',
                'Jul','Aug','Sep','Okt','Nov','Dez'],
                monthStatus: 'anderen Monat anzeigen', yearStatus: 'anderes Jahr anzeigen',
                weekHeader: 'Wo', weekStatus: 'Woche des Monats',
                dayNames: ['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'],
                dayNamesShort: ['So','Mo','Di','Mi','Do','Fr','Sa'],
                dayNamesMin: ['So','Mo','Di','Mi','Do','Fr','Sa'],
                dayStatus: 'Setze DD als ersten Wochentag', dateStatus: 'W�hle D, M d',
                dateFormat: 'dd.mm.yy', firstDay: 1,
                initStatus: 'Wähle ein Datum', isRTL: false};
        $.datepicker.setDefaults($.datepicker.regional['de']);
});


$(document).ready(function ()
{
    $('#profile_form').validationEngine();
    $('#person_form').validationEngine();
    $('#new_skill_form').validationEngine();
    $('#new_category_form').validationEngine();
    $('#newsletter').validationEngine();
    $('#watchlist_request').validationEngine();
    $('#callback_form').validationEngine();
    $('#application4').validationEngine();

    $('#profile_form').submit(function(){
        if($('input:file').length > 0){
            getUploadMessage();
        }
    });


    if ($('#start_date').length > 0) {
        $('#start_date').datepicker();
    }
    if ($('#end_date').length > 0) {
        $('#end_date').datepicker();
    }

    $('#assume_button').click(function () {
       assume_profession();
       return false;
    });



    $('#assume_skill').click(function () {
       assume_skill();
       return false;
    });

    $('#assume_category').click(function () {
       assume_category();
       return false;
    });

    $('#another_upload').click(function(){
        return another_upload();
    });

    $('#prof_picker').keyup(function(){
        get_profession();
    });

    $('#prof_picker').click(function(){
        $('#prof_picker').val('');
    });

    $('#skill_picker').keyup(function(){
        get_skill();
    });

     $('#category_picker').keyup(function(){
        get_categories();
    });
    
    

    $('#another_skill').click(function () {
       another_skill();
    });

    $('#another_company').click(function(){
        return another_company();
    });

    $('#another_profession').click(function(){
        another_profession();
    });
    $('#driving_licence').click(function(){
        $('#add_driving_licence').html('Hallo');
    });

    $('#no_driving_licence').click(function(){
        $('#add_driving_licence').html('');
    });

    $('.add_profession').click(function(){
        alert('Ajax einf�gen');
    });

    var professionRecommendations = [];
//    var professionRecommendationsCache = {};
    
    $("#profession").autocomplete({
            source: professionRecommendations,
            search: function(event, ui) {
                if ($(event.target).val().length >= 2) {

                    $.ajax({
                        url: 'index.php5?cmd=ajax&action=professionrecommendations&search='+$(event.target).val(),
                        data: null,
                        success: function (data) {
                            $('#profession').autocomplete('option', 'source', data);
                        },
                        dataType: 'json',
                        async: false
                    });
                } else if ($(event.target).val().length < 3) {
                    event.preventDefault();
                }
            }
    });
});
