window.onload = function (){
    setEvent_to_OBJ("document.getElementById('year')" ,"change","iniD") 
    setEvent_to_OBJ("document.getElementById('month')","change","iniD") 
    setEvent_to_OBJ("document.getElementById('year2')" ,"change","iniD") 
    setEvent_to_OBJ("document.getElementById('month2')","change","iniD") 
    ini();
}
function setEvent_to_OBJ(objNameStr,eventTypeNameStr,fncNameStr){
    eval(objNameStr+".on"+eventTypeNameStr+"="+ fncNameStr);
}

arrM = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
var dayT = new Date;
var strY = dayT.getYear();
var strM = dayT.getMonth();
var strD = dayT.getDate();
if(strY < 1900){strY += 1900;}//NN系対策

function ini(){
    iniY();
    iniM();
    iniD();
}
function iniY(){
    var yr = document.getElementById("year");
    yr.options.length = 2;
    yr.options[0].text  = strY;
    yr.options[1].text  = strY + 1;
    yr.options[0].value = strY;
    yr.options[1].value = strY + 1;
    yr.options[0].selected = "selected";
	
    var yr2 = document.getElementById("year2");
    yr2.options.length = 2;
    yr2.options[0].text  = strY;
    yr2.options[1].text  = strY + 1;
    yr2.options[0].value = strY;
    yr2.options[1].value = strY + 1;
    yr2.options[0].selected = "selected";
}
function iniM(){
    var mt = document.getElementById("month");
    mt.options.length = 12;
    for(var i=0;i<12;i++){
        mt.options[i].text  = i + 1;
        mt.options[i].value = i + 1;
        if(i == strM){mt.options[i].selected = "selected";}
    }
	
    var mt2 = document.getElementById("month2");
    mt2.options.length = 12;
    for(var i=0;i<12;i++){
        mt2.options[i].text  = i + 1;
        mt2.options[i].value = i + 1;
        if(i == strM){mt2.options[i].selected = "selected";}
    }
}
function iniD(){
    var yr    = document.getElementById("year");
    var dt    = document.getElementById("date");
    var mt    = document.getElementById("month");
    var u_flg = 0;//うるう年か否か
    var d_flg = 0;//年月が現在と同じか否か
    //月末の日数
    var lngD  = arrM[mt.options[mt.selectedIndex].value];
    if(mt.options[mt.selectedIndex].value == 2){
        u_flg = uruu(yr.options[yr.selectedIndex].value);
    }
    if(u_flg != 0){lngD ++;}
    if(mt.options[mt.selectedIndex].value == strM + 1 &&
        yr.options[yr.selectedIndex].value == strY){d_flg = 1;}

    dt.options.length = lngD;
    for(var i=0;i<lngD;i++){
        dt.options[i].text  = i + 1;
        dt.options[i].value = i + 1;
        if(i == strD - 1 && d_flg == 1){
            dt.options[i].selected = "selected";
        }
    }
    if(d_flg == 0){
        dt.options[0].selected = "selected";
    }
	
    var yr2    = document.getElementById("year2");
    var dt2    = document.getElementById("date2");
    var mt2    = document.getElementById("month2");
    //月末の日数
    var lngD2  = arrM[mt2.options[mt2.selectedIndex].value];
    if(mt2.options[mt2.selectedIndex].value == 2){
        u_flg = uruu(yr2.options[yr2.selectedIndex].value);
    }
    if(u_flg != 0){lngD2 ++;}
    if(mt2.options[mt2.selectedIndex].value == strM + 1 &&
        yr2.options[yr2.selectedIndex].value == strY){d_flg = 1;}

    dt2.options.length = lngD2;
    for(var i=0;i<lngD2;i++){
        dt2.options[i].text  = i + 1;
        dt2.options[i].value = i + 1;
        if(i == strD - 1 && d_flg == 1){
            dt2.options[i].selected = "selected";
        }
    }
    if(d_flg == 0){
        dt2.options[0].selected = "selected";
    }
}
function uruu(year){
    if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
        return 1;
    }else{
        return 0;
    }
}