var aDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var aWeek = new Array("日", "月", "火", "水", "木", "金", "土");

var nDate = new Date();
var nYear = nDate.getFullYear();
var nMonth = nDate.getMonth();
var nToday = nDate.getDate();

var tDays;
var nFirstDay;

var tYear = nYear;
var tMonth = nMonth;
var tToday = nToday;


var toolYear;
var toolMonth;

var ci, cj, ck;

if (typeof(EventContoroller) == 'undefined') {
    EventContoroller ={};
}

function minical_init()
{
    minical_print(nYear,nMonth)
}

function minical_print(y,m)
{
    if(m < 10){
        spacer = " ";
    }else{
        spacer = "";
    }
    $("minicalYM").innerHTML = y+"年" + spacer + (m+1)+"月";

    tDays = aDays[m];
    if (m == 1) {        /* うるう年のチェック */
        if (y % 400 == 0) tDays =29;
        else if (y % 100 == 0) tDays = 28;
        else if (y % 4 == 0) tDays= 29;
    }

    nDate.setDate(1);
    nDate.setMonth(m);
    nDate.setFullYear(y);

    nFirstDay = nDate.getDay();

    $("minical").innerHTML = drawMiniCal(y,m);
}

function toolCalPrint (elm, y, m, statical) {
	if (typeof statical == 'undefined') {
		statical = false;
	}

    tDays = aDays[m];
    if (m == 1) {        /* うるう年のチェック */
        if (y % 400 == 0) tDays =29;
        else if (y % 100 == 0) tDays = 28;
        else if (y % 4 == 0) tDays= 29;
    }

    if(m < 10){
        spacer = " ";
    }else{
        spacer = "";
    }

    ym = y+"年" + spacer + (m+1)+"月";

    nDate.setDate(1);
    nDate.setMonth(m);
    nDate.setFullYear(y);

    nFirstDay = nDate.getDay();

    targetElm = elm + "_calendar";

    $(targetElm).innerHTML = "";

    toolcaldiv1 = document.createElement('div');
    toolcaldiv1.className = "calendarBox";
    if (!statical) {
    	toolcaldiv1.style.position = "absolute";
    	toolcaldiv1.style.zIndex = 1000;
    }

    toolcaldiv2 = document.createElement('div');
    toolcaldiv2.className = "calendarTitle";

    toolhead = document.createElement(statical ? 'h4' : 'h2');
    toolheadspan = document.createElement('span');
    toolheadspan.innerHTML = ym + "&nbsp;"

    toolheadprev = document.createElement('a');
    toolheadprev.href="javascript:void(0);";
    Event.observe(toolheadprev ,
		  'click',
		  function() { toolcal_prevMonth(elm, y, m, statical) }.bind(this), false);
    toolheadprevimg = document.createElement('img');
    toolheadprevimg.src ="/img/arr_m_001.gif";
    toolheadprevimg.style.margin = 0;

    toolheadprev.appendChild(toolheadprevimg);

    toolheadthis = document.createElement('a');
    toolheadthis.href="javascript:void(0);";
    Event.observe(toolheadthis ,
		  'click',
		  function() { toolcal_thisMonth(elm, statical) }.bind(this), false);
    toolheadthisimg = document.createElement('img');
    toolheadthisimg.src ="/img/btn_m_month.gif";
    toolheadthisimg.style.margin = 0;

    toolheadthis.appendChild(toolheadthisimg);

    toolheadnext = document.createElement('a');
    toolheadnext.href="javascript:void(0);";
    Event.observe(toolheadnext ,
		  'click',
		  function() { toolcal_nextMonth(elm, y, m, statical) }.bind(this), false);
    toolheadnextimg = document.createElement('img');
    toolheadnextimg.src ="/img/arr_m_002.gif";
    toolheadnextimg.style.margin = 0;

    toolheadnext.appendChild(toolheadnextimg);

    toolheadspan.appendChild(toolheadprev);
    toolheadspan.appendChild(toolheadthis);
    toolheadspan.appendChild(toolheadnext);

    toolhead.appendChild(toolheadspan);
    toolcaldiv2.appendChild(toolhead);

    toolheadp = document.createElement('p');

    if (!statical) {
    	toolheadpa = document.createElement('a');
    	toolheadpa.href="javascript:void(0);";
    	Event.observe(toolheadpa ,
    		'click',
    		function() { tooyCalHide(elm) }.bind(this), false);
    	toolheadpaimg = document.createElement('img');
    	toolheadpaimg.src ="/img/disp01_btn_001.gif";

    	toolheadpa.appendChild(toolheadpaimg);
    	toolheadp.appendChild(toolheadpa);
    }

    toolcaldiv2.appendChild(toolheadp);
    toolcaldiv1.appendChild(toolcaldiv2);

    minicaldiv = document.createElement('div');
    minicaldiv.style.clear = "both";
    minicaldiv.innerHTML = drawToolCal(y, m, elm, statical);

    toolcaldiv1.appendChild(minicaldiv);

    $(targetElm).appendChild(toolcaldiv1);
}

function tooyCalHide(elm){
    targetElm = elm + "_calendar";
    $(targetElm).innerHTML = "";
}

function toolCalInitInputWindow(){
    targetElm = "event_start_date_calendar";
    $(targetElm).innerHTML = "";
    targetElm = "event_end_date_calendar";
    $(targetElm).innerHTML = "";
    targetElm = "public_start_date_calendar";
    $(targetElm).innerHTML = "";
}

function drawToolCal(y, m, elm, statical)
{
    var toolCalDate;
    var holidayName = '';
    var ret;

    if (statical) {
    	ret = "<table cellspacing='0' cellpadding='0' class='calTool' style='height:130px;margin-left:45px;width:240px;'><tr><th class='sun'>日</th><th>月</th><th>火</th><th>水</th><th>木</th><th>金</th><th class='sat'>土</th></tr></tr>";
    } else {
    	ret = "<table cellspacing='0' cellpadding='0' class='calTool' style='width:140px;'><tr><th class='sun'>日</th><th>月</th><th>火</th><th>水</th><th>木</th><th>金</th><th class='sat'>土</th></tr></tr>";
    }

    for (ci = -nFirstDay, cj = 0; ci < tDays; ci++) {
        if ((ci+1) == nToday && y==nYear && m==nMonth) {
            sclass = "today";      // 今日
        } else if (cj==0) {
            sclass = "sun";      // 日曜
        } else if (cj==6) {
            sclass = "sat";      // 土曜
        } else {
            sclass = "";  // 平日
        }
        if (ci < 0) {
        	nDate.setFullYear(y);
        	nDate.setMonth(m);
        	nDate.setDate(ci);

            holidayName = "";
            toolCalDate = getToolCalDate(y, (m-1), (nDate.getDate() + 1));
            if (typeof(calHolidayMstData[toolCalDate]) != 'undefined') {
                sclass = "sun";
                holidayName = calHolidayMstData[toolCalDate];
            }
            ret += "<td class='"+sclass+"'><p class='otr'><a href='javascript:setDateCalendar(\"" + elm + "\"," + y + "," + (m-1) + "," + (nDate.getDate() + 1) + "," + statical + ");' hidefocus='on' tabindex='1' class='actionlink' title='" + holidayName +"'>"+ (nDate.getDate() + 1) +"</a></p></td>";

        } else {          /* 1日〜末日までを挿入 */

            holidayName = "";
            toolCalDate = getToolCalDate(y, m, (ci + 1));
            if (typeof(calHolidayMstData[toolCalDate]) != 'undefined') {
                sclass = "sun";
                holidayName = calHolidayMstData[toolCalDate];
            }
            ret += "<td class='"+sclass+"'><p><a href='javascript:setDateCalendar(\"" + elm + "\"," + y + "," + m + "," + (ci + 1) + "," + statical + ");' hidefocus='on' tabindex='1' class='actionlink' title='" + holidayName + "'>"+ (ci + 1) +"</a></p></td>";
        }
        if (++cj == 7 && ci != tDays) {
            ret += "</tr><tr>";
            cj = 0;
        }
    }
    if( cj != 0){
        ck = 1;
        for (ci = cj; ci < 7; ci++) {
            if (ci==0) {
                sclass = "sun";      // 日曜
            } else if (ci==6) {
                sclass = "sat";      // 土曜
            } else {
                sclass = "";  // 平日
            }

            holidayName = "";
            toolCalDate = getToolCalDate(y, (m+1), ck);
            if (typeof(calHolidayMstData[toolCalDate]) != 'undefined') {
                sclass = "sun";
                holidayName = calHolidayMstData[toolCalDate];
            }
            ret += "<td class='"+sclass+"'><p class='otr'><a href='javascript:setDateCalendar(\"" + elm + "\"," + y + "," + (m+1) + "," + ck + "," + statical + ");' hidefocus='on' tabindex='1' class='actionlink' title='" + holidayName + "'>"+ ck +"</a></p></td>";
            ck ++;
        }
    }
    ret += "</tr></table>";
    return ret;
}


//update 20080604
function getToolCalDate(y, m, d)
{
    var toolCalM = m;
    switch (toolCalM) {
        case -1:
            toolCalM = 11;
            break;
        case 0:
            toolCalM = 12;
            break;
    }
    toolCalM = (toolCalM < 10) ? ('0' + String(toolCalM)) : String(toolCalM);
    var toolCalD = (d < 10) ? ('0' + String(d)) : String(d);
    var toolCalDate = String(y) + toolCalM + toolCalD;

    return DateUtil.calMonth(toolCalDate, 1);


}



function setDateCalendar( elm, y, m, d, statical ){
	if (typeof statical == 'undefined') {
		statical = false;
	}

    if(m < 0){
        m = 11;
        y -= 1;
    }
    if(m > 11){
        m = 0;
        y += 1;
    }

    $(elm).value = ("0000" + y) . slice(-4) + "" + "" + ("00" + (m+1)) . slice(-2) + "" + ("00" + d) . slice(-2);

    if (!statical) {
    	targetElm = elm + "_calendar";
    	$(targetElm).innerHTML = "";
    }
    if($('calendar_theme_left')){
    	$('calendar_theme_left').style.visibility='';
    }
}


function drawMiniCal(y,m)
{
    var holidayName = '';
    ret = "<table cellspacing='0' cellpadding='0' class='calMenu'><tr><th class='sun'>日</th><th>月</th><th>火</th><th>水</th><th>木</th><th>金</th><th class='sat'>土</th></tr></tr>";
    for (ci = -nFirstDay, cj = 0; ci < tDays; ci++) {
        if (ci < 0) {
            nDate.setFullYear(y);
            nDate.setMonth(m);
            nDate.setDate(ci);
            dd = nDate.getDate()+1;
            mm = nDate.getMonth();
            yy = nDate.getFullYear();
        }else{
        	nDate.setFullYear(y);
        	nDate.setMonth(m);
        	nDate.setDate(ci+1);

            dd = nDate.getDate();
            mm = nDate.getMonth()+1;
            yy = nDate.getFullYear();
        }

        holidayName = "";
        if ((ci+1) == nToday && yy==nYear && (mm-1)==nMonth) {
            sclass = "today";      // 今日
        } else if ( isHoliday(yy,mm,dd) ){
            sclass = "sun";      // 日曜
            holidayName = getHolidayName(yy,mm,dd);
        } else if (cj==0) {
            sclass = "sun";      // 日曜
        } else if (cj==6) {
            sclass = "sat";      // 土曜
        } else {
            sclass = "";  // 平日
        }
        if (ci < 0) {
            ret += "<td class='"+sclass+"'><p class='otr' title='" + holidayName + "'>"+ dd +"</p></td>";
        } else {          /* 1日〜末日までを挿入 */
            ret += "<td class='"+sclass+"'><p title='" + holidayName + "'>"+ dd +"</p></td>";
        }
        if (++cj == 7 && ci != tDays) {
            ret += "</tr><tr>";
            cj = 0;
        }
    }
    if( cj != 0){
        ck = 1;
        nDate.setDate(ck);
        nDate.setMonth(mm);
        nDate.setFullYear(y);
        mm = nDate.getMonth()+1;
        yy = nDate.getFullYear();
        for (ci = cj; ci < 7; ci++) {
            holidayName = "";
            if (ci==0) {
                sclass = "sun";      // 日曜
            } else if ( isHoliday(yy,mm,ck) ){
                sclass = "sun";      // 日曜
                holidayName = getHolidayName(yy,mm,ck);
            } else if (ci==6) {
                sclass = "sat";      // 土曜
            } else {
                sclass = "";  // 平日
            }
            ret += "<td class='"+sclass+"'><p class='otr' title='" + holidayName + "'>"+ck+"</p></td>";
            ck ++;
        }
    }
    ret += "</tr></table>";
    return ret;
}

function minical_thisMonth()
{
    tYear = nYear;
    tMonth = nMonth;
    minical_print(nYear,nMonth);
}

function minical_nextMonth()
{
    tMonth += 1;
    if(tMonth > 11){
        tMonth = 0;
        tYear += 1;
        if(tYear > nYear + 10){
            tYear = nYear + 10;
            tMonth = 11;
        }
    }
    minical_print(tYear,tMonth);
}

function minical_prevMonth()
{
    tMonth -= 1;
    if(tMonth < 0){
        tMonth = 11;
        tYear -= 1;
        if(tYear < nYear - 10){
            tYear = nYear - 10;
            tMonth = 0;
        }
    }
    minical_print(tYear,tMonth);
}

EventContoroller.showDateSchedule = function(elm) {

    var date_value = $(elm).value;
    if (
        !(
            this.FormType == 3 && $F('calendar_type') == 2 &&
            (elm == "event_start_date" || elm == "event_end_date")
        )
    )
    {
        toolYear = "";
        toolMonth = "";

        if(date_value != ""){
            toolYear  = date_value.substr(0,4) * 1;
            toolMonth = date_value.substr(4,2) * 1 -1;
        }

        if(toolYear > (tYear + 10) || toolYear < (tYear - 10) || toolYear == ""){
            toolYear = nYear * 1;
        }
        if(toolMonth > 11 || toolMonth < 0 || toolMonth == ""){
            toolMonth = nMonth * 1;
        }
        toolCalPrint(elm,toolYear,toolMonth);
    }
}

EventContoroller.hideDateSchedule = function(elm) {
    targetElm = elm + "_calendar";
    $(targetElm).innerHTML = "";
}

function toolcal_nextMonth(elm, y, m, statical)
{
    m = m +1;
    if(m > 11){
        m = 0;
        y = y + 1;
        if(y > nYear + 10){
            y = nYear + 10;
            m = 11;
        }
    }
    toolCalPrint(elm, y, m, statical);
}

function toolcal_prevMonth(elm, y, m, statical)
{
    m = m - 1;
    if(m < 0){
        m = 11;
        y = y -1;
        if(y < nYear - 10){
            y = nYear - 10;
            m = 0;
        }
    }
    toolCalPrint(elm, y, m, statical);
}

function toolcal_thisMonth(elm, statical)
{
    toolMonth = nMonth;
    toolYear = nYear;
    toolCalPrint(elm, toolYear, toolMonth, statical);
}

var currentYear;
var currentMonth;
var currentDay;

function setCurrentDate() {
    data = new Date();
    currentYear = data.getYear();
    currentYear = (currentYear < 2000) ? currentYear + 1900 : currentYear;
    currentMonth = data.getMonth() + 1;
    currentDay = data.getDate();
}

function isToday(year, month, day) {
    if (year == currentYear && parseInt(month,10) == currentMonth && day == currentDay) {
        return true;
    }
    return false;
}

function isSaturday(year, month, day) {
    var week = new Date(year, month - 1, day).getDay();
    if (week == 6) {
        return true;
    }
    return false;
}

function isHoliday(year, month, day) {

    var calMonth = (month < 10) ? ('0' + String(month)) : String(month);
    var calDay = (day < 10) ? ('0' + String(day)) : String(day);
    var calDate = String(year) + calMonth + calDay;

    if (typeof(calHolidayMstData[calDate]) == 'undefined') {
        return false;
    }

    return true;

}

function getHolidayName(year, month, day) {

    var calMonth = (month < 10) ? ('0' + String(month)) : String(month);
    var calDay = (day < 10) ? ('0' + String(day)) : String(day);
    var calDate = String(year) + calMonth + calDay;

    if (typeof(calHolidayMstData[calDate]) == 'undefined') {
        return null;
    }

    return calHolidayMstData[calDate];
}

function isSunday(year, month, day) {
    var week = new Date(year, month - 1, day).getDay();
    if (week == 0) {
        return true;
    }
    return false;
}

function getFirstMonday(year, month) {
    var monday;
    for(monday = 1; monday < 8; monday++) {
        if(new Date(year, month - 1, monday).getDay() == 1) {
            break;
        }
    }
    return monday;
}


