﻿$(document).ready(function () {
    $('input[AutoComplete]').focus(function () {
        $('div.AutoComplete').fadeOut();
        var acb = $(this).attr('AutoComplete');

        if ($.browser.msie && jQuery.browser.version < 8) {
            $('#' + acb).css('left', '0px');
            $('#' + acb).css('top', $(this).offset().top - 140 + 'px')
        }

        $('#' + acb).fadeIn();
    });

    $('input').focus(function () {
        if ($(this).attr('AutoComplete') == undefined) {
            $('div.AutoComplete').fadeOut();
        }
    });
});

$(document).ready(function() 
{
    $("input[AutoComplete=land]").keypress(function (event) 
    {
        if($("#land .AutoCompleteLijst > li").length == 2 && event.which != 8 && event.which != 0)
        {
            return false;
        }
    });

    $("input[AutoComplete=sport]").keypress(function (event) 
    {
        if($("#sport .AutoCompleteLijst > li").length == 2 && event.which != 8 && event.which != 0)
        {
            return false;
        }
    });

    $("input[AutoComplete*=club-speler]").keypress(function (event) 
    {
        var $acbDivId = $(this).attr('AutoComplete');
        if($("#" + $acbDivId + " .AutoCompleteLijst > li").length == 2 && event.which != 8 && event.which != 0)
        {
            return false;
        }
    });

    $("input[AutoComplete=land]").keyup(function () 
    {
        if($(this).val().length > 0)
        {
            $.ajax({ 
                type: "POST",
                url: "../services/Service.asmx/GeefLanden",
                data: "{invoer:'" + $('input[autocomplete=land]').val() + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(response) {
                    var myObject = eval(response);
                    $("#land .AutoCompleteLijst").empty();
                    
                    $("#land .AutoCompleteLijst").append($('<li id="0"></li>').html("Wis invoer"));
                    
                    for (var i = 0; i <= myObject.d.length - 1; i++) 
                    {
                        if(myObject.d.length == 1)
                        {
                            $("#land .AutoCompleteLijst").append($('<li id="' + myObject.d[i].ID + '" class="Active"></li>').html(myObject.d[i].Naam)); 
                        }
                        else
                        {
                            $("#land .AutoCompleteLijst").append($('<li id="' + myObject.d[i].ID + '"></li>').html(myObject.d[i].Naam)); 
                        }
                    }
                },
                error: function(response) {
                    alert('fout');
                    if (response.length != 0) 
                    {
                        alert(response.status + ' ' + response.statusText);
                    }
                }
            });
        }
    });

    $("input[AutoComplete*=club-speler], input[AutoComplete*=finalist], input[AutoComplete*=kampioen]").keyup(function () 
    {
        if($(this).val().length > 0)
        {
            var $acbDivId = $(this).attr('AutoComplete');
            var $bekend = '';
            var $groep = '';
            if($(this).attr('RelatedAutoCompleteControlID'))
            {
                $bekend = $("[id$=" + $(this).attr('RelatedAutoCompleteControlID') + "]").val();
            }
            if($(this).attr('GroupControlID'))
            {
                $groep = $("[id$=" + $(this).attr('GroupControlID') + "]").val();
                
                if($groep.length > 1)
                {
                    $groep = '';
                }
            }
            
            $.ajax({ 
                type: "POST",
                url: "../services/Service.asmx/GeefClubsOfSpelers",
                data: "{invoer:'" + $(this).val() + "', autoCompleteID:'" + $acbDivId + "', bekend:'" + $bekend + "', groep:'" + $groep + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(response) {
                    var myObject = eval(response);
                    $("#" + $acbDivId + " .AutoCompleteLijst").empty();
                    
                    $("#" + $acbDivId + " .AutoCompleteLijst").append($('<li id="0"></li>').html("Wis invoer"));
                    
                    for (var i = 0; i <= myObject.d.length - 1; i++) 
                    {
                        if(myObject.d.length == 1)
                        {
                            $("#" + $acbDivId + " .AutoCompleteLijst").append($('<li id="' + myObject.d[i].ID + '" class="Active"></li>').html(myObject.d[i].VolledigeNaam)); 
                        }
                        else
                        {
                            $("#" + $acbDivId + " .AutoCompleteLijst").append($('<li id="' + myObject.d[i].ID + '"></li>').html(myObject.d[i].VolledigeNaam)); 
                        }
                    }
                },
                error: function(response) {
                    alert('fout');
                    if (response.length != 0) 
                    {
                        alert(response.status + ' ' + response.statusText);
                    }
                }
            });
        }
    });

    $("input[AutoComplete=sport]").keyup(function () 
    {
        if($(this).val().length > 0)
        {
            var $individueel = '';
            if($(this).attr('IndividualSport'))
            {
                $individueel = $(this).attr('IndividualSport');
            }
        
            $.ajax({ 
                type: "POST",
                url: "../services/Service.asmx/GeefSporten",
                data: "{invoer:'" + $('input[autocomplete=sport]').val() + "', individueleSport:'" + $individueel + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(response) {
                    var myObject = eval(response);
                    $("#sport .AutoCompleteLijst").empty();
                    
                    $("#sport .AutoCompleteLijst").append($('<li id="0"></li>').html("Wis invoer"));
                    
                    for (var i = 0; i <= myObject.d.length - 1; i++) 
                    {
                        if(myObject.d.length == 1)
                        {
                            $("#sport .AutoCompleteLijst").append($('<li id="' + myObject.d[i].ID + '" class="Active"></li>').html(myObject.d[i].Naam)); 
                        }
                        else
                        {
                            $("#sport .AutoCompleteLijst").append($('<li id="' + myObject.d[i].ID + '"></li>').html(myObject.d[i].Naam)); 
                        }
                    }
                },
                error: function(response) {
                    alert('fout');
                    if (response.length != 0) 
                    {
                        alert(response.status + ' ' + response.statusText);
                    }
                }
            });
        }
    });

    $("input[AutoComplete=competitie]").keyup(function () 
    {
        if($(this).val().length > 0)
        {
            $.ajax({ 
                type: "POST",
                url: "../services/Service.asmx/GeefCompetities",
                data: "{invoer:'" + $('input[autocomplete=competitie]').val() + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(response) {
                    var myObject = eval(response);
                    $("#competitie .AutoCompleteLijst").empty();
                    
                    $("#competitie .AutoCompleteLijst").append($('<li id="0"></li>').html("Wis invoer"));
                    
                    for (var i = 0; i <= myObject.d.length - 1; i++) 
                    {
                        if(myObject.d.length == 1)
                        {
                            $("#competitie .AutoCompleteLijst").append($('<li id="' + myObject.d[i].ID + '" class="Active"></li>').html(myObject.d[i].Naam)); 
                        }
                        else
                        {
                            $("#competitie .AutoCompleteLijst").append($('<li id="' + myObject.d[i].ID + '"></li>').html(myObject.d[i].Naam)); 
                        }
                    }
                },
                error: function(response) {
                    alert('fout');
                    if (response.length != 0) 
                    {
                        alert(response.status + ' ' + response.statusText);
                    }
                }
            });
        }
    });

    $("input[AutoComplete=kenner]").keyup(function () 
    {
        if($(this).val().length > 0)
        {
            $.ajax({ 
                type: "POST",
                url: "../services/Service.asmx/GeefKenners",
                data: "{invoer:'" + $('input[autocomplete=kenner]').val() + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(response) {
                    var myObject = eval(response);
                    $("#kenner .AutoCompleteLijst").empty();
                    
                    $("#kenner .AutoCompleteLijst").append($('<li id="0"></li>').html("Wis invoer"));
                    
                    for (var i = 0; i <= myObject.d.length - 1; i++) 
                    {
                        if(myObject.d.length == 1)
                        {
                            $("#kenner .AutoCompleteLijst").append($('<li id="' + myObject.d[i].ID + '" class="Active"></li>').html(myObject.d[i].GebruikersNaam)); 
                        }
                        else
                        {
                            $("#kenner .AutoCompleteLijst").append($('<li id="' + myObject.d[i].ID + '"></li>').html(myObject.d[i].GebruikersNaam)); 
                        }
                    }
                },
                error: function(response) {
                    alert('fout');
                    if (response.length != 0) 
                    {
                        alert(response.status + ' ' + response.statusText);
                    }
                }
            });
        }
    });

    $("input[AutoComplete=teams]").keyup(function () {
        if ($(this).val().length > 0) {
            $.ajax({
                type: "POST",
                url: "../services/Service.asmx/GeefTeamNamen",
                data: "{invoer:'" + $('input[autocomplete=teams]').val() + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    var myObject = eval(response);
                    $("#teams .AutoCompleteLijst").empty();

                    $("#teams .AutoCompleteLijst").append($('<li id="0"></li>').html("Wis invoer"));

                    for (var i = 0; i <= myObject.d.length - 1; i++) {
                        if (myObject.d.length == 1) {
                            $("#teams .AutoCompleteLijst").append($('<li id="' + myObject.d[i] + '" class="Active"></li>').html(myObject.d[i]));
                        }
                        else {
                            $("#teams .AutoCompleteLijst").append($('<li id="' + myObject.d[i] + '"></li>').html(myObject.d[i]));
                        }
                    }
                },
                error: function (response) {
                    alert('fout');
                    if (response.length != 0) {
                        alert(response.status + ' ' + response.statusText);
                    }
                }
            });
        }
    });
    
    $('ul.AutoCompleteLijst li').live('click', function()
    {
        $(this).parent('ul').children('li').removeClass('Active');
        
        var $acbId = $(this).parent().parent().attr('id');
        var $hfId = $(this).parent().parent().attr('title');
        $(this).parent().removeClass('Active');
        var $acbWaarde = $(this).text();
        
        if($acbWaarde == "Wis invoer")
        {
            $("[id$='" + $hfId + "']").val("");
            $("input[AutoComplete='" + $acbId + "']").val("");
            $("input[AutoComplete='" + $acbId + "']").focus();
            $("#" + $acbId + " .AutoCompleteLijst").empty();
            return false;
        }
        
        if($(this).text().indexOf('(') > 0)
        {
            $acbWaarde = $(this).text().slice(0, $(this).text().length - 6);
        }
        
        $("[id$='" + $hfId + "']").val($acbWaarde + ";" + $(this).attr('id'));
        $("input[AutoComplete='" + $acbId + "']").val($acbWaarde);
        
        $(this).addClass('Active');
        $("#" + $acbId).fadeOut();
    });
});
