function getYears() {
    $.ajax({
        url: 'LottoFunctions.php',
        data: {action: 'Years'},
        type: "POST",
        success: function(data){ onYearsCallback(data); },
        error: function(mes) {alert(mes);}
    });
}

function onYearsCallback(value) {
    var obj = jQuery.parseJSON(value); 
    $("#LottoYearSelect option").remove();
    if($(obj).size() > 0){
        var htmlArr = [];
        $.each(obj, function(key, val){
            htmlArr.push('<option value="' + val + '">' + val + '</option>');
        });
    
        $("#LottoYearSelect").html(htmlArr.join(''));
        
        YearSelectionChange();
    }
}

function YearSelectionChange() {
    //this.options[this.selectedIndex].value
    var value = $('#LottoYearSelect option:selected').val();
    $.ajax({
        url: 'LottoFunctions.php',
        data: {action: 'MonthByYear', year: value},
        type: "POST",
        success: function(data){ onYearSelectionChangeCallback(data); },
        error: function(mes) {alert(mes);}
    });
}

function onYearSelectionChangeCallback(value) {
    var obj = jQuery.parseJSON(value); 
    $("#LottoMonthSelect option").remove();
    if($(obj).size() > 0){
        var htmlArr = [];
        $.each(obj, function(key, val){
    	    htmlArr.push('<option value="' + val + '">' + val + '</option>');
        });
    
        $("#LottoMonthSelect").html(htmlArr.join(''));
       
        MonthSelectionChange();
	}
}

function MonthSelectionChange() {
    //this.options[this.selectedIndex].value
    var yearValue = $('#LottoYearSelect option:selected').val();
    var monthValue = $('#LottoMonthSelect option:selected').val();
    $.ajax({
        url: 'LottoFunctions.php',
        data: {action: 'DayByYearAndMonth', year: yearValue, month: monthValue},
        type: "POST",
        success: function(data){ onMonthSelectionChangeCallback(data); },
        error: function(mes) {alert(mes);}
    });
}

function onMonthSelectionChangeCallback(value) {
    var obj = jQuery.parseJSON(value); 
    $("#LottoDaySelect option").remove();
    if($(obj).size() > 0){
        var htmlArr = [];
        $.each(obj, function(key, val){
            htmlArr.push('<option value="' + key + '">' + key + ', ' + val + '</option>');
        });
    
        $("#LottoDaySelect").html(htmlArr.join(''));
    }
    
    DaySelectionChange();
}

function DaySelectionChange() {
    
    var yearValue = $('#LottoYearSelect option:selected').val();
    var monthValue = $('#LottoMonthSelect option:selected').val();
    var dayValue = $('#LottoDaySelect option:selected').val();
    $.ajax({
        url: 'LottoFunctions.php',
        data: {action: 'Ziehung', year: yearValue, month: monthValue, day: dayValue},
        type: "POST",
        success: function(data){ onDaySelectionChangeCallback(data); },
        error: function(mes) {alert(mes);}
    });
}

function onDaySelectionChangeCallback(value) {
    //return;
    var obj = jQuery.parseJSON(value); 
    //$("#ergbnis).remove();
    if($(obj).size() > 0){
        var htmlArr = [];
        $.each(obj, function(key, val){
            var str =  '<div value="' + key + '">' + val.join(', ') + '</option>';
        	//alert(key);    
	/*
            $.each(val, function (name, nummer)) {
                str = str + 
            });
            */
            htmlArr.push(str);
        });
    
        $("#ergebnis").html(htmlArr.join('<br />'));
    }
}

