 var tblDayEnds = new Array(4);
    tblDayEnds[0] = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    tblDayEnds[1] = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    tblDayEnds[2] = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    tblDayEnds[3] = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

    function changeDate(from_dd, from_MM, from_yyyy, to_dd, to_MM, to_yyyy, sel_dd, sel_MM, sel_yyyy, iddd, idMMyyyy) {
        sel_dd = getValue(iddd);
        sel_MMyyyy = getValue(idMMyyyy);
        sel_MM = sel_MMyyyy != null ? sel_MMyyyy.split('.')[0] : null;
        sel_yyyy = sel_MMyyyy != null ? getValue(idMMyyyy).split('.')[1]: null;

        generateDate(from_dd, from_MM, from_yyyy, to_dd, to_MM, to_yyyy, sel_dd, sel_MM, sel_yyyy, iddd, idMMyyyy)
    }

    function generateDate(from_dd, from_MM, from_yyyy, to_dd, to_MM, to_yyyy, sel_dd, sel_MM, sel_yyyy, iddd, idMMyyyy) {
        document.getElementById(iddd).options.length = 0;
        document.getElementById(idMMyyyy).options.length = 0;

        from_dd_ISO = isoDate(from_dd);
        from_MM_ISO = isoDate(from_MM);
        from_yyyy_ISO = isoDate(from_yyyy);
        to_dd_ISO = isoDate(to_dd);
        to_MM_ISO = isoDate(to_MM);
        to_yyyy_ISO = isoDate(to_yyyy);

        if (from_yyyy_ISO + from_MM_ISO + from_dd_ISO > to_yyyy_ISO + to_MM_ISO + to_dd_ISO) {
            return;
        }
        else {
            for (k = from_yyyy; k <= to_yyyy; k++) {
                from_MM_sel = 1;
                to_MM_sel = 12;
                if (k == from_yyyy) { from_MM_sel = from_MM; }
                if (k == to_yyyy) { to_MM_sel = to_MM; }
                for (j = from_MM_sel; j <= to_MM_sel; j++) {
                    generateSelect(j + '.' + k, j + '.' + k, sel_MM + '.' + sel_yyyy, idMMyyyy)
                }
            }
            if (sel_yyyy == null || sel_MM == null || (from_yyyy == sel_yyyy && from_MM == sel_MM)) {
                from_dd_sel = from_dd;
            }
            else {
               from_dd_sel = 1;
            }
            if (to_yyyy == sel_yyyy && to_MM == sel_MM) {
                to_dd_sel = to_dd;
            }
            else {
                if (sel_yyyy != null && sel_MM != null) {
                    to_dd_sel = tblDayEnds[sel_yyyy % 4][sel_MM - 1];
                }
                else {
                    to_dd_sel = tblDayEnds[from_yyyy % 4][from_MM - 1];
                }
            }
            generateSelect(from_dd_sel, to_dd_sel, sel_dd, iddd);
        }
    }

    function generateSelect(from, to, sel, id) 
    {
        for (i = from; i <= to; i++) 
        {
            text = isoDate(i);
            var dropdown = document.getElementById(id);
            dropdown.options[dropdown.options.length] = new Option(text, text);
            if (isoDate(text) == isoDate(sel)) 
            {
                dropdown.options[dropdown.options.length - 1].selected = true
            }
        }
    }

    function isoDate(int_number) {
        if (int_number == null) {
            return null;
        }

        if (int_number.length != undefined) {
            if (int_number.indexOf('.') == -1) {
                if (int_number.length < 2) {
                    int_number = '0' + int_number;
                }
            }
            else {
                if (int_number.split('.')[0].length < 2) {
                    int_number = '0' + int_number;
                }
            }
        }
        else {
            if (int_number < 10) {
                int_number = '0' + int_number;
            }
        }
        return int_number;
    }

    function getValue(name) {
        var dropdown = document.getElementById(name);
        if (dropdown != null && dropdown.options.selectedIndex != -1) {
            return dropdown.options[dropdown.options.selectedIndex].value;
        }
        else {
            return null;
        }
    }

    function FillSelects(startd, startm, starty, endd, endm, endy, selectd, selectm, selecty, dateddInputName, dateMMyyyyInputName) {
        var dd = selectd;
        var MMyyyy = selectm + "." + selecty;
        var currentDateFromMMyyyy = document.getElementById(dateMMyyyyInputName);
        var currentDateFromdd = document.getElementById(dateddInputName);
        for (var i = 0; i < currentDateFromMMyyyy.options.length; i++) {
            if (currentDateFromMMyyyy.options[i].value == MMyyyy) {
                currentDateFromMMyyyy.selectedIndex = i;
            }
        }
        changeDate(startd, startm, starty, endd, endm, endy, selectd, selectm, selecty, dateddInputName, dateMMyyyyInputName);
        for (var j = 0; j < currentDateFromdd.options.length; j++) {
            if (currentDateFromdd.options[j].value == dd) {
                currentDateFromdd.selectedIndex = j;
            }
        }
        currentDateFromdd.value = dd;
        currentDateFromMMyyyy.value = MMyyyy;
    }