/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Usuwa wszystkie opcje pola typu select
 */
function clearSelectField(field) {
    while (field.options.length > 0)
        field.remove(0);
}

function setDayOptionsForYearAndMonth(dayField, year, month) {
    
    var day_counts = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

    var d_count;

    if (((0 == (year % 4)) && ((0 != (year % 100)) || (0 == (year % 400)))) && month == 1)
		d_count = 29;
    else
		d_count = day_counts[month];

    // zapamietanie starej wartosci
    val = dayField.value;
    clearSelectField(dayField);
    for (i = 1; i <= d_count; i++) {
        var option = document.createElement('option');
        i_str = i.toString();
        if (i < 10)
            i_str = "0" + i_str;
        option.value = i_str;
        option.text = i;
        if (navigator.appName == "Microsoft Internet Explorer")
           dayField.add(option)
        else
           dayField.add(option, null);        
       
    }
    // proba przywrocenia starej wartosci dnia
    if (day_counts[month] >= val)
        dayField.value = val
    else
        dayField.value = "01";
}

function initPolDaty(wyjazd_od_dzien, wyjazd_od_miesiac_rok, przyjazd_do_dzien, przyjazd_do_miesiac_rok) {
    var months = new Array("stycze\u0144", "luty", "marzec", "kwiecie\u0144", "maj",
        "czerwiec", "lipiec", "sierpie\u0144", "wrzesień", "październik", "listopad",
        "grudzie\u0144");
    // biezaca data
    var date = new Date();
    // konieczne, by mozna bylo pozniej iterowac ze skokiem miesiecznym
    date.setDate(1);
    var current_month = date.getMonth();
    var current_year = date.getFullYear();
    // pole miesiaca i roku od
    select_field1 = document.getElementById("wyjazd_od_miesiac_rok");
    select_field2 = document.getElementById("przyjazd_do_miesiac_rok");
    options1 = select_field1.options;
    options2 = select_field2.options;
    // usuniecie wszystkich opcji
    while (options1.length > 0) {
        select_field1.remove(0);
        //options.delete(options.length - 1);
    }
    while (options2.length > 0) {
        select_field2.remove(0);
        //options.delete(options.length - 1);
    }
    for (i = 0; i < 13; i++) {
        var option1 = document.createElement('option');
        var option2 = document.createElement('option');
        var m = date.getMonth() + 1;
        m_str = m.toString();
        if (m < 10)
            m_str = "0" + m_str;
        option1.value = date.getFullYear() + "-" + m_str;
        option1.text = months[m - 1] + " " + date.getFullYear();
        //select_field1.add(option1, null);
        //select_field1.add(option1);        
        if (navigator.appName == "Microsoft Internet Explorer")
            select_field1.add(option1)
        else
            select_field1.add(option1, null);
        option2.value = date.getFullYear() + "-" + m_str;
        option2.text = months[m - 1] + " " + date.getFullYear();
        //select_field2.add(option2, null);
        if (navigator.appName == "Microsoft Internet Explorer")
            select_field2.add(option2)
        else
           select_field2.add(option2, null);
        date.setMonth(date.getMonth() + 1);
    }    
    setDayOptionsForYearAndMonth(document.getElementById("wyjazd_od_dzien"),
      current_year, current_month);
    setDayOptionsForYearAndMonth(document.getElementById("przyjazd_do_dzien"),
      current_year, current_month);
    //window.alert(wyjazd_od_miesiac_rok);
    // jesli formularz byl juz wywolywany i wartosci zapamietane (czyli byly
    // bledy), to odtwarza zapamietane wartosci
    document.getElementById("wyjazd_od_dzien").value = wyjazd_od_dzien;
    document.getElementById("przyjazd_do_dzien").value = przyjazd_do_dzien;
    select_field1.value = wyjazd_od_miesiac_rok;
    select_field2.value = przyjazd_do_miesiac_rok;
}

function setDayOptionsForYearAndMonthField(dayField, yearAndMonthField) {
    var year = yearAndMonthField.value.substr(0, 4);
    var month = yearAndMonthField.value.substr(5, 2) - 1;
    setDayOptionsForYearAndMonth(dayField, year, month);
}