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系対策
//年のID配列
var yr = [];
//月のID配列
var mt = [];
//日のID配列
var dt = [];
//配列番号
var selectNum = 0;
$(document).ready(function ()
{
    //年と月の変更に伴い日を変える
    $(".year").each(function (i)
    {
        var myId = $(this).attr("id");
        $(this).change(function ()
        {
            iniD(yr[i], mt[i], dt[i]);
        });
    });
    $(".month").each(function (i)
    {
        var myId = $(this).attr("id");
        $(this).change(function ()
        {
            iniD(yr[i], mt[i], dt[i]);
        });
    });
	//日付セットごとにini関数を設定
    $(".selectGroup").each(function (i)
    {
        $(this).find("select").each(function (i)
        {
            var myId = $(this).attr("id");
            //配列に追加する
            if (i == 0) {
                yr.push(myId);
            }
            else if (i == 1) {
                mt.push(myId);
            }
            else if (i == 2) {
                dt.push(myId);
            }
        });
        ini(selectNum);
        selectNum++;
    });
});

function ini(myNum)
{
    iniY(yr[myNum]);
    iniM(mt[myNum]);
    iniD(yr[myNum], mt[myNum], dt[myNum]);
}
function iniY(yrId)
{
    var yr = document.getElementById(yrId);
    yr.options.length = 5;
    //今年から5年分表示
    yr.options[0].text = strY ;
    yr.options[1].text = strY + 1;
    yr.options[2].text = strY + 2;
    yr.options[3].text = strY + 3;
    yr.options[4].text = strY + 4;
    yr.options[0].value = strY;
    yr.options[1].value = strY + 1;
    yr.options[2].value = strY + 2;
    yr.options[3].value = strY + 3;
    yr.options[4].value = strY + 4;
    yr.options[0].selected = "selected";
}
function iniM(mtId)
{
    var mt = document.getElementById(mtId);
    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";
        }
    }
}
function iniD(yrId, mtId, dtId)
{
    var yr = document.getElementById(yrId);
    var mt = document.getElementById(mtId);
    var dt = document.getElementById(dtId);
    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";
    }
}
function uruu(year)
{
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
        return 1;
    }
    else {
        return 0;
    }
}
