function submitFlyerSearchForm()
{
    with (document.formEventFlyer)
    {
        if (isNaN(flyer_search_date_from_day.value) || 
            parseInt(flyer_search_date_from_day.value, 10) > 31 || parseInt(flyer_search_date_from_day.value, 10) < 1)
        {
            alert('U dient een geldige dag in te voeren');
            flyer_search_date_from_day.focus();   
        }
        else if (isNaN(flyer_search_date_from_month.value) || 
            parseInt(flyer_search_date_from_month.value, 10) > 12 || parseInt(flyer_search_date_from_month.value, 10) < 1)
        {
            alert('U dient een geldige maand in te voeren');
            flyer_search_date_from_month.focus();   
        }
        else if (isNaN(flyer_search_date_from_year.value) || 
            parseInt(flyer_search_date_from_year.value, 10) > 3000 || parseInt(flyer_search_date_from_year.value, 10) < 1000)
        {
            alert('U dient een geldig jaar in te voeren');
            flyer_search_date_from_year.focus();   
        }
        else if (isNaN(flyer_search_date_to_day.value) || 
            parseInt(flyer_search_date_to_day.value, 10) > 31 || parseInt(flyer_search_date_to_day.value, 10) < 1)
        {
            alert('U dient een geldige dag in te voeren');
            flyer_search_date_to_day.focus();   
        }
        else if (isNaN(flyer_search_date_to_month.value) || 
            parseInt(flyer_search_date_to_month.value, 10) > 12 || parseInt(flyer_search_date_to_month.value, 10) < 1)
        {
            alert('U dient een geldige maand in te voeren');
            flyer_search_date_to_month.focus();   
        }
        else if (isNaN(flyer_search_date_to_year.value) || 
            parseInt(flyer_search_date_to_year.value, 10) > 3000 || parseInt(flyer_search_date_to_year.value, 10) < 1000)
        {
            alert('U dient een geldig jaar in te voeren');
            flyer_search_date_to_year.focus();   
        }
        else
        {
             var _date_from = new Date(parseInt(flyer_search_date_from_year.value, 10), parseInt(flyer_search_date_from_month.value, 10)-1, parseInt(flyer_search_date_from_day.value, 10)); 
             var _date_to   = new Date(parseInt(flyer_search_date_to_year.value, 10), parseInt(flyer_search_date_to_month.value, 10)-1, parseInt(flyer_search_date_to_day.value, 10), 23, 59, 59); 
             var check_date = new Date();
             check_date.setHours(0, 0, 0, 0);
             flyer_search_date_from.value = flyer_search_date_from_day.value+"-"+flyer_search_date_from_month.value+"-"+flyer_search_date_from_year.value; 
             flyer_search_date_to.value = flyer_search_date_to_day.value+"-"+flyer_search_date_to_month.value+"-"+flyer_search_date_to_year.value; 
             
             if (_date_from.getTime() > _date_to.getTime())
             {
                alert('De startdatum is groter dan de einddatum');   
             }
             else if (_date_to.getTime() < check_date.getTime())
             {
                if (confirm('De einddatum ligt in het verleden.\r\nWeet u zeker dat u door wilt gaan met zoeken?'))
                {
                    item_id.value=-1; 
                    submit(); 
                }     
             }
             else if (_date_from.getTime() < check_date.getTime())
             {
                if (confirm('De startdatum ligt in het verleden.\r\nWeet u zeker dat u door wilt gaan met zoeken?'))
                {
                    submit(); 
                }     
             }
             else
             {
                submit();    
             }
        }
    }
}

function addFlyerEvent(occur_id, item_title, show_fast)
{
    $.ajax({
        url: 'handles/toggle_flyer_event.php',
        data: 'occur_id='+occur_id+'&item_title='+item_title,
        success: function(xml){
            if ($(xml).find('status').text() == 'OK')
            {
                $('#btn_select_random_items').css('visibility', 'hidden');
                $('#btn_delete_selected_items').css('visibility', 'visible');
                var cur_html = $('#selected_flyer_events').html();
                if (cur_html.match(/Er zijn nog geen evenementen geselecteerd/) != null)
                {
                    cur_html = '';   
                }
                var new_html = '<div style="display: none;" id="selected_flyer_item_'+occur_id+'">';
                new_html += '<table width="100%" padding="0" margin="0"><tr><td style="font-weight: bold; color: #084FA2; border: none; padding: 0px;">';
                new_html += item_title;
                new_html += '</td><td style="border: none; padding: 0px; text-align: right;"><a href="javascript:void(0);" onclick="removeFlyerEvent('+occur_id+');">verwijder</a>';
                new_html += '</td></tr></table></div>';
                $('#selected_flyer_events').html(cur_html+new_html);
                if (show_fast == true)
                {
                    $('#flyer_item_'+occur_id).css('display', 'none');
                    $('#selected_flyer_item_'+occur_id).css('display', 'block');
                }
                else
                {
                    $('#flyer_item_'+occur_id).slideUp();
                    $('#selected_flyer_item_'+occur_id).slideDown();    
                }
                num_selected_items = $(xml).find('event_count').text(); 
                count_html  = '('+$(xml).find('event_count').text();
                count_html += ' van ';
                count_html += $(xml).find('event_max').text()+')';
                $('#count_flyer_events').html(count_html);
                if ($(xml).find('event_count').text() == $(xml).find('event_max').text())
                {
                    $('.input_toggle_flyer_event').attr("disabled", true);   
                }
            }
        }
    });   
}

function selectRandomItems(a_array)
{
    var timeout = 300;
    for (var i in a_array)
    {
        setTimeout('addFlyerEvent('+i+', "'+a_array[i]+'", '+true+')', timeout);
        timeout += 300;
    }   
}

function removeFlyerEvent(occur_id)
{
    $.ajax({
        url: 'handles/toggle_flyer_event.php',
        data: 'occur_id='+occur_id,
        success: function(xml){
            if ($(xml).find('status').text() == 'OK')
            {
                $('#selected_flyer_item_'+occur_id).slideUp();
                num_selected_items = $(xml).find('event_count').text();
                count_html  = '('+$(xml).find('event_count').text();
                count_html += ' van ';
                count_html += $(xml).find('event_max').text()+')';
                $('#count_flyer_events').html(count_html);
                $('.input_toggle_flyer_event').removeAttr("disabled");
            }
        }
    });   
}

function showFlyer()
{
    if (num_selected_items < num_maximum_items)
    {
        if (confirm('U heeft slechts '+num_selected_items+' van de '+num_maximum_items+' evenementen geselecteerd.\r\nWeet u zeker dat u verder wilt gaan?'))
        {
            window.open('poster/index.php');   
        }
    }
    else
    {
        window.open('poster/index.php');    
    }
}

function showMoreEvents()
{
    $('#btn_show_more_events').css('display', 'none');
    $('#ico_show_more_events').css('display', 'block');
    $.ajax({
        url: 'handles/load_flyer_events.php',
        success: function(html) {
            $('#ico_show_more_events').slideUp();
            if (html != '')
            {
                var dt = new Date();
                var new_id = dt.getTime();
                var app = '<div id="'+new_id+'" style="display: none;">'
                app += html;
                app += '</div>';
                $('#event_search_results').append(app);   
                $('#'+new_id).slideDown();
                $('#btn_show_more_events').fadeIn();    
            }
        }
    }); 
}
