var lastPostcode = '';
var lastEuretcoId = '';
$(document).ready(function() {
    GetLocations();
    //$('#clubRow').hide();
});

function GetLocations() {
    var postcode = $('#postcodeNumbers').attr('value');
    if(!postcode || postcode == lastPostcode)
        return;
    
    lastPostcode = postcode;
    $.ajax({
        url: 'http://loyalty.sport2000.nl/data/Ajax.php?request=locations&zip='+$.trim(postcode),
        type: 'GET',
        dataType: 'xml',
        timeout: 1000,
        error: function(){
            $('#vestiging').removeAttr("disabled");
        },
        success: function(xml){
            ParseLocations(xml);
            GetClubs();
        }
    });
}

function GetClubs() {
    
    //return;
    var euretcoId = $('#vestiging').attr('value');
    if(!euretcoId || euretcoId == lastEuretcoId) {
        $('#vereniging').attr('value', '');
        $('#clubSpan').html("Er zijn geen verenigingen gevonden voor de gekozen vestiging. <br /><br />");
        return;
    }
    
    lastEuretcoId = euretcoId;
    $.ajax({
        url: 'http://loyalty.sport2000.nl/data/Ajax.php?request=clubs&id='+$.trim(euretcoId),
        type: 'GET',
        dataType: 'xml',
        timeout: 1000,
        error: function() {
            $('#vereniging').attr('value', '');
            $('#clubSpan').html("Er zijn geen verenigingen gevonden voor de gekozen vestiging. <br /><br />");
        },
        success: function(xml){
            ParseClubs(xml);
        }
    });
}

function ParseClubs(xml) {
    //Clear and disable closest options
    $('#clubCol').children().remove();
    
    //No locations in response
    if($(xml).find('club').size() == 0) {
        $('#vereniging').attr('value', '');
        $('#clubSpan').html("Er zijn geen verenigingen gevonden voor de gekozen vestiging. <br /><br />");
        return;
    }

    //Get comma seperated 
    var activeVereniginen = $('#activeVereniginen').attr('value');
    activeVereniginen = activeVereniginen.split(",");
    
    $('#clubSpan').html("");
    //Loop through locations and add links
    $(xml).find('club').each(function(){
        $('<input name="vereniging[]" type="checkbox" />')
            .attr('value', $(this).find('clubid').text())
            .attr('id', 'clubid_' + $(this).find('clubid').text())
            .attr('checked', $.inArray($(this).find('clubid').text(), activeVereniginen) !== -1 ? 'checked' : '')
            .appendTo('#clubSpan')
            .after("<br />")
            .after('<label for="clubid_' + $(this).find('clubid').text() + '">' + $(this).find('title').text() + ", " + $(this).find('city').text() + "</label>")
        ;
    });

}

function ParseLocations(xml) {
    //Clear and disable closest options
    $('#vestiginsOptions').empty();
    
    //No locations in response
    if($(xml).find('location').size() == 0) {
        $('<li></li>')
            .html('Geen vestigingen gevonden')
            .appendTo('#vestiginsOptions')
        ;
        return;
    }

    //Loop through locations and add links
    $(xml).find('location').each(function(){
        $('<a></a>')
            .attr('id', $(this).find('euretcoid').text())
            .click(LocationClicked)
            .attr('href', '')
            .html($(this).find('city').text() + ' - ' + $(this).find('title').text())
            .appendTo('#vestiginsOptions')
            .wrap('<li></li>')
        ;
        $('#vestiginsOptions br:last').empty();
    });

    //Set option to closest location
    //var id = $(xml).find('location:first').find('location_id').text();
    //$('#vestiging option').removeAttr("selected");
    //$("#vestiging option[value='"+id+"']").attr('selected', 'selected');
    /*
    //Add link for other location
    $('<a></a>')
        .attr('id', 'anchorAndereVestiging')
        .attr('class', 'frm_txt')
        .appendTo('#vestiginsOptions')
        .html('Andere vestiging selecteren...')
        .attr('href', '#')
        .click( function() {
            $('#vestiging').removeAttr("disabled");
            return false;
        })
    ;
    */
}

function LocationClicked(event) {
    var id = $(event.target).attr('id');
    $('#vestiging option:selected').removeAttr("selected");
    $("#vestiging option[value='"+id+"']").attr('selected', 'selected');
    GetClubs();

    return false;
}
