/*
include/jschedule/datefunc.js (Last Update 2001.02.19)
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
   ESMPRO/JMSS WebClient (JSchedule) Version 6.0
   Copyright (C) NEC Corporation/NEC Soft, Ltd. 2001
   All rights reserved
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/

///////////////////////* Date *////////////////////////
//  NN4΍...
//var YEAR_MIN = 1994;
//var YEAR_MAX = 2019;
//var MonthDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);


function ReMakeDays(objSelYear,objSelMonth,objSelDay)
{
    var i;
//  var selYear = objSelYear.selectedIndex +YEAR_MIN;
    var selYear = objSelYear.selectedIndex +1994;
    var selMonth= objSelMonth.selectedIndex+1;
    var selDay  = objSelDay.selectedIndex  +1;

    if ((((selYear%4==0&&selYear%100!=0)||selYear%400==0)?1:0)){
      //MonthDays[1] = 29;
      var MonthDays = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
    }else{
      //MonthDays[1] = 28;
      var MonthDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
    }

    for(i=1; i<=MonthDays[selMonth-1]; i++){
        objSelDay.options[i-1]=new Option(i,i);
    }

    //Set the length of the select list
    objSelDay.length=MonthDays[selMonth-1];
    //justify
    if (objSelDay.length < selDay){
        objSelDay.selectedIndex = MonthDays[selMonth-1]-1;
    }else{
        objSelDay.selectedIndex = selDay-1;
    }
}

function initYMD(objSelYear,objSelMonth,objSelDay,curYear,curMonth,curDay){
//  objSelYear.selectedIndex  = curYear  - YEAR_MIN;
    objSelYear.selectedIndex  = curYear  - 1994;
    objSelMonth.selectedIndex = curMonth - 1;
    objSelDay.selectedIndex   = curDay   - 1;
}

function initHM(objSelHour,objSelMinute,curHour,curMinute){
    objSelHour.selectedIndex   = curHour;
    objSelMinute.selectedIndex = curMinute;
}


function drawYearOptions(){
//  for (i=YEAR_MIN; i<=YEAR_MAX; i++) {
    for (i=1994; i<=2019; i++) {
        document.write("<option value="+i+">"+i);
    }
}

function drawMonthOptions(){
    for (i=1; i<=12; i++) {
        document.write("<option value="+i+">"+i);
    }
}
function drawDayOptions(){
    for (i=1; i<=31; i++) {
        document.write("<option value="+i+">"+i);
    }
}

function drawShiftDayOptions(strShiftDay){
    for (i=0; i<=365; i++) {
        if (strShiftDay != i){
            document.write("<option value="+i+">"+i);
        }else{
            document.write("<option value="+i+" selected>"+i);
        }
    }
}

function drawHourOptions(){
    var str;
    for (i=0; i<=23; i++) {
        str = "0" + i;
        document.write("<option value="+i+">"+ str.substring(str.length-2));
    }
}

function drawMinuteOptions(){
    var str;
    for (i=0; i<=59; i++) {
        str = "0" + i;
        document.write("<option value="+i+">"+ str.substring(str.length-2));
    }
}

function drawSecondOptions(){
    var str;
    for (i=0; i<=59; i++) {
        str = "0" + i;
        document.write("<option value="+i+">"+ str.substring(str.length-2))
    }
}

function CheckFromToDate(nYearFm,nMonthFm,nDayFm,nYearTo,nMonthTo,nDayTo){
    var bRet;

    bRet = CheckDate(nYearFm,nMonthFm,nDayFm);
    if (!bRet){
        return false;
    }

    bRet = CheckDate(nYearTo,nMonthTo,nDayTo)
    if (!bRet){
        return false;
    }

    varDateFm = new Date(nYearFm,nMonthFm-1,nDayFm);
    varDateTo = new Date(nYearTo,nMonthTo-1,nDayTo);
    if (varDateFm > varDateTo){
        alert("Jn̓tIƂĐݒ肷邱Ƃ͂ł܂B");
    //alert("varDateFm=" + varDateFm);
    //alert("varDateTo=" + varDateTo);
        return false;
    }
    return true;
}

function CheckDate(nYear,nMonth,nDay){

    // Numeric Check
/*
    if( !( isNaN(nYear)
        &&isNaN(nMonth)
        &&isNaN(nDay) ) ){
        alert("pȊO͎wł܂");
        return false;
    }
*/

    // Year Check
    if(nYear.length!=4){
        alert("N͐Sœ͂Ă");
        return false;
    }

    // Month Check
    if( nMonth < 1 || nMonth > 12 ){
        alert(nMonth + "" + "݂͑܂");
        return false;
    }

    // Date Check
    if(    nDay <= 0
        || nDay > 30+((nMonth==4||nMonth==6||nMonth==9||nMonth==11)?0:1)
        || (nMonth==2&&nDay>28+(((nYear%4==0&&nYear%100!=0)||nYear%400==0)?1:0))
      ){
        alert(nYear + "N" + nMonth + "" + nDay + "݂͑܂");
        return false;
    }

    //OK
    return true;
}
