var SEARCHABLE_NUMBER_OF_MONTHS = 3;

function nknavi_js_reset_dates(year_select_id, month_select_id, day_select_id, this_year, this_month, this_day){
    var in_year_box = document.getElementById(year_select_id);
    var in_month_box = document.getElementById(month_select_id);
    var in_day_box = document.getElementById(day_select_id);
    
    var in_year = in_year_box.options[in_year_box.selectedIndex].text;
    var in_month = in_month_box.options[in_month_box.selectedIndex].text;
    var in_day = in_day_box.options[in_day_box.selectedIndex].text;
    
    var days_in_selected_month = days_in_month(in_year, in_month);

    var start_month;
    var end_month;
    var start_day;
    var end_day;

    if(in_year == this_year){
        if(this_month + SEARCHABLE_NUMBER_OF_MONTHS - 1 > 12){
            start_month = this_month;
            end_month = 12;
        }
        else{
            start_month = this_month;
            end_month = this_month + SEARCHABLE_NUMBER_OF_MONTHS - 1;
        }
    }
    else{
        start_month = 1;
        end_month = this_month + SEARCHABLE_NUMBER_OF_MONTHS - 1 - 12;
    }

    if(in_month == this_month && in_year == this_year){
        start_day = this_day;
        end_day = days_in_month(this_year, this_month)
    }
    else{
        start_day = 1;
        end_day = days_in_month(in_year, in_month);
    }

    in_month_box.length = 0;
    in_day_box.length = 0;

    for(i = start_month; i <= end_month; i++){
        var month_option = document.createElement('option');
        month_option.text = i;
        month_option.value = i;

        try{
            in_month_box.add(month_option, null); // standards compliant; doesn't work in IE
        }
        catch(ex){
            in_month_box.add(month_option); // IE only
        }
    }

    for(i = start_day; i <= end_day; i++){
        var day_option = document.createElement('option');
        day_option.text = i;
        day_option.value = i;

        try{
            in_day_box.add(day_option, null); // standards compliant; doesn't work in IE
        }
        catch(ex){
            in_day_box.add(day_option); // IE only
        }
    }

    for(i = 0; i < in_month_box.length; i++){
        if(in_month_box.options[i].text == in_month){
            in_month_box.selectedIndex = i;
            break;
        }
    }

    if(in_day > days_in_selected_month){
        in_day = days_in_selected_month;
    }
    
    for(i = 0; i < in_day_box.length; i++){
        if(in_day_box.options[i].text == in_day){
            in_day_box.selectedIndex = i;
            break;
        }
    }
}

function days_in_month(in_year, in_month)
{
	return (32 - new Date(in_year, in_month - 1, 32).getDate());
}

