$(document).ready(function() {
    //Selecteert de eerste optie van een groep radiobuttons.
    $('div#content_left form input:radio:first').attr('checked','checked');


    /*-----------------------------------------
    Continent selection
    -----------------------------------------*/ 
    $("select.select_continent").bind('change', function() {
        if ($("select.select_continent").val() != '') {
            window.location = $("a." + $("select.select_continent").val()).attr('href');
        }
    });
    
    
    /*-----------------------------------------
    Date picker
    -----------------------------------------*/
    $("input.datefield").each(function () {
        if ($(this).val() == '') {
            $(this).val('dd/mm/yyyy');
            $(this).css('color', 'silver');
        }
        
        if ($(this).val() != 'dd/mm/yyyy') {
            $(this).css('color', 'black');
        }
    })
    
    $("input.datefield").bind('focus change', function() {
        $(this).css('color', 'black');

        if ($(this).val() == 'dd/mm/yyyy') {
            $(this).val('');
        }
    });

    $("input.datefield").bind('blur', function() {
        if ($(this).val() == '') {
            $(this).val('dd/mm/yyyy');
            $(this).css('color', 'silver');
        }
    });

    $("input.datefield").bind('keydown', function(event) {
        if ((event.keyCode >= 48 && event.keyCode <= 57)
            || (event.keyCode >= 96 && event.keyCode <= 105)
            || event.keyCode == 8
            || event.keyCode == 37
            || event.keyCode == 39
            || event.keyCode == 46)
        {
            if (event.keyCode != 8) {
                if ($(this).val().length == 10) {
                    event.preventDefault();
                }
                if ($(this).val().length == 2) {
                    event.preventDefault();
                }
                if ($(this).val().length == 5) {
                    event.preventDefault();
                }
            }
        } else {
            event.preventDefault();
        }
    });

    $("input.datefield").bind('keyup', function(event) {
        if (event.keyCode != 8) {
            if ($(this).val().length == 2) {
                $(this).val($(this).val() + '/');
            }

            if ($(this).val().length == 5) {
                $(this).val($(this).val() + '/');
            }
        } else {

        }
    });
    
    //calendars for date fields    
    $("input.datefield").each(function () {
        $(this).datepicker();
    });
    
    
    /*-----------------------------------------
    Search categories
    -----------------------------------------*/
    //initial category options
    $.get('/search/ajaxgetlibrarycategories?library=' + $("select[name=library]").val() + '&category=' + $("input#category").val(), function(data) {
        if (data) {
            $("select[name=category]").html(data);
        }
    });
    
    //get category options onchange
    $("select[name=library]").bind('change', function() {
        $.get('/search/ajaxgetlibrarycategories?library=' + $(this).val(), function(data) {
            if (data) {
                $("select[name=category]").html(data);
            }
        });
        return false;
    });
        
    
    /*-----------------------------------------
    Password
    -----------------------------------------*/    
    if ($("input[name=usr_id]").val() != '') {
        if ($("input[name=usr_password]").val()) {
        $("input[name=usr_password]").setAtrr("class", 'text same');
        }
        
        if ($("input[name=confirm_password]").val()) {
            $("input[name=confirm_password]").setAtrr("class", 'text Identical');
        }        
    }
    
    
    /*-----------------------------------------
    Contact form country code telephone / fax
    -----------------------------------------*/ 
    $("select#country").bind("change", function() {
        $("input#countryphone").val("+ " + $(this).val());
        $("input#countryfax").val("+ " + $(this).val());
    });
});
