// Copyright (c) 2004-2009 koikikukan All Rights Reserved.
// http://www.koikikukan.com/
// License is granted if and only if this entire
// copyright notice is included. By Yujiro ARAKI.

// Ver1.00 2006.03.15 initial version.
// Ver1.01 2006.04.11 fix bug.
// Ver1.02 2006.04.15 fix bug.
// Ver1.03 2006.04.16 fix bug.
// Ver1.04 2006.08.03 fix bug.
// Ver1.05 2007.04.21 fix bug.
// Ver1.06 2007.11.05 fix bug.
// Ver1.07 2009.04.19 for change low.

var currentYear;
var currentMonth;
var currentDay;

function setCurrentDate() {
	
	currentYear = ec3.today_year_num;
    currentYear = (currentYear < 2000) ? currentYear + 1900 : currentYear;
	currentMonth = ec3.today_month_num;
	currentDay = ec3.today_day_num;
	/*
    data = new Date();
    currentYear = data.getYear();
    currentYear = (currentYear < 2000) ? currentYear + 1900 : currentYear;
    currentMonth = data.getMonth() + 1;
    currentDay = data.getDate();*/
}

function isToday(year, month, ja_day) {
    if (year == currentYear && parseInt(month,10) == currentMonth && ja_day == currentDay) {
        return true;
    }
    return false;
}

function isSaturday(year, month, ja_day) {
    var week = new Date(year, month - 1, ja_day).getDay();
    if (week == 6) {
        return true;
    }
    return false;
}

//tiruru only
function isMonday(year, month, ja_day){
	var week = new Date(year,month - 1, ja_day).getDay();
	if (week == 1){
		return true;
	}
    switch(parseInt(month,10)) {
    case 1:
		if(ja_day == 1 || ja_day == 2 || ja_day == 3) {
			return true;
		}
		break;
	case 12:
		if(ja_day == 29 || ja_day == 30 || ja_day == 31){
			return true;
		}
		break;	
	}
	return false;
}
function isFirstwednesday(year, month){
    var wednesday;
    for(wednesday = 1; wednesday < 8; wednesday++) {
        if(new Date(year, month - 1, wednesday).getDay() == 3) {
		   break;
        }
    }
	return wednesday;
}
function istosyoHoliday(year, month, ja_day){
	var week = new Date(year,month - 1, ja_day).getDay();
	if (week == 3 && ja_day == isFirstwednesday(year, month)){
		return true;
	}
    switch(parseInt(month,10)) {
    case 3:
		if(ja_day <= 31 && ja_day >= 17) {
			return true;
		}
		break;
	}
	return false;
}

function isHoliday(year, month, ja_day) {
    var week = new Date(year, month - 1, ja_day).getDay();
    if (week == 0) {
        return true;
    }
    switch(parseInt(month,10)) {
    case 1:
        if (ja_day == 1) {
            return true;
        }
        if (ja_day == 2 && isSunday(year, month, 1)) {
            return true;
        }
        if (ja_day == (getFirstMonday(year, month) + 7)) {
            return true;
        }
        break;
    case 2:
        if (ja_day == 11) {
            return true;
        }
        if (ja_day == 12 && isSunday(year, month, 11)) {
            return true;
        }
        break;
    case 3:
        if(year > 1979 && year < 2100) {
            if (ja_day == parseInt(20.8431 + 0.242194 * (year - 1980) - parseInt((year - 1980) / 4))) {
                return true;
            }
            if (ja_day == (parseInt(20.8431 + 0.242194 * (year - 1980) - parseInt((year - 1980) / 4)) + 1) && isSunday(year, month, ja_day - 1)) {
                return true;
            }
        }
        break;
    case 4:
        if (ja_day == 29) {
            return true;
        }
        if (ja_day == 30 && isSunday(year, month, 29)) {
            return true;
        }
        break;
    case 5:
        if (ja_day == 3 || ja_day == 4 || ja_day == 5) {
            return true;
        }
        if (ja_day == 6 && (isSunday(year, month, 3) ||
                         isSunday(year, month, 4) ||
                         isSunday(year, month, 5))) {
            return true;
        }
        break;
    case 7:
        if (ja_day == (getFirstMonday(year, month) + 14)) {
            return true;
        }
        break;
    case 9:
        if (ja_day == (getFirstMonday(year, month) + 14)) {
            return true;
        }
        if(year > 1979 && year < 2100) {
            if (ja_day == parseInt(23.2488 + 0.242194 * (year - 1980) - parseInt((year - 1980) / 4))) {
                return true;
            }
            if (ja_day == (parseInt(23.2488 + 0.242194 * (year - 1980) - parseInt((year - 1980) / 4)) + 1) && isSunday(year, month, ja_day - 1)) {
                return true;
            }
        }
        var day2 = parseInt(ja_day, 10);
        if(day2 - 1 == (getFirstMonday(year, month) + 14))  {
            if(year > 2008 && year < 2100) {
                if (day2 + 1 == parseInt(23.2488 + 0.242194 * (year - 1980) - parseInt((year - 1980)  / 4))) {
                    return true;
                }
                if (day2 + 1 == (parseInt(23.2488 + 0.242194 * (year - 1980) - parseInt((year - 1980) / 4)) + 1) && isSunday(year, month, ja_day - 1)) {
                    return true;
                }
            }
        }
        break;
    case 10:
        if (ja_day == (getFirstMonday(year, month) + 7)) {
            return true;
        }
        break;
    case 11:
        if (ja_day == 3 || ja_day == 23) {
            return true;
        }
        if (ja_day == 4 && isSunday(year, month, 3)) {
            return true;
        }
        if (ja_day == 24 && isSunday(year, month, 23)) {
            return true;
        }
        break;
    case 12:
        if (ja_day == 23) {
            return true;
        }
        if (ja_day == 24 && isSunday(year, month, 23)) {
            return true;
        }
        break;
    }
    return false;
}

function isSunday(year, month, ja_day) {
    var week = new Date(year, month - 1, ja_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;
}


function re_setWeekendAndHoliday(td_element,y,m,d) {

    var re_year = y;
    var re_month = m;
    var ja_day = d + 1;

	var re_td = td_element;

        if(re_td.innerHTML.indexOf('href') != -1){
            ja_day = re_td.getElementsByTagName('a')[0].innerHTML;
        } else {
            ja_day = re_td.innerHTML;
        }
		
		var oldclass = '';
		if(re_td.getAttribute('class')){
			var oldclass = re_td.getAttribute('class');
		}else if(re_td.getAttribute('className')){
			var oldclass = re_td.getAttribute('className');
		}

        if (isHoliday(re_year, re_month, ja_day)) {
            if (isToday(re_year, re_month, ja_day)) {
                re_td.setAttribute('class', 'tholiday ' + oldclass );
                re_td.setAttribute('className', 'tholiday ' + oldclass );
            } else if(isMonday(re_year,re_month,ja_day)){
                re_td.setAttribute('class', 'holiday monday '  + oldclass);
                re_td.setAttribute('className', 'holiday monday ' + oldclass);
			}else if(istosyoHoliday(re_year,re_month,ja_day)){
				re_td.setAttribute('class','holiday tosyoHoliday ' + oldclass);
				re_td.setAttribute('className','holiday tosyoHoliday ' + oldclass);
			}else {
                re_td.setAttribute('class', 'holiday '  + oldclass);
                re_td.setAttribute('className', 'holiday ' + oldclass);
            }
        } else if(isSaturday(re_year, re_month, ja_day)) {
            if (isToday(re_year, re_month, ja_day)) {
                re_td.setAttribute('class', 'tsaturday ' + oldclass );
                re_td.setAttribute('className', 'tsaturday ' + oldclass);
			}else if(istosyoHoliday(re_year,re_month,ja_day)){
				re_td.setAttribute('class','saturday tosyoHoliday' + oldclass);
				re_td.setAttribute('className','saturday tosyoHoliday ' + oldclass);
            } else {
                re_td.setAttribute('class', 'saturday ' + oldclass);
                re_td.setAttribute('className', 'saturday ' + oldclass);
            }
        } else if(isMonday(re_year,re_month,ja_day)){
			if(isToday(re_year,re_month,ja_day)) {
				re_td.setAttribute('calss','tmonday ' + oldclass);
				re_td.setAttribute('className','tmonday' + oldclass);
			} else {
				re_td.setAttribute('class','monday' + oldclass);
				re_td.setAttribute('className','monday ' + oldclass);
			}
		}else if(istosyoHoliday(re_year,re_month,ja_day)){
				re_td.setAttribute('class','tosyoHoliday' + oldclass);
				re_td.setAttribute('className','tosyoHoliday ' + oldclass);
		}else{
            if (isToday(re_year, re_month, ja_day)) {
                re_td.setAttribute('class', 'today ' + oldclass);
                re_td.setAttribute('className', 'today ' + oldclass);
            }
        }
}

//For this month calendar
function setWeekendAndHoliday(b_y,b_m) {
	var ja_element = document.getElementById("wp-calendar");
    var caption = ja_element.getElementsByTagName("caption");
	
    var date = caption[0].getElementsByTagName("a")[0].innerHTML;
    var number = date.match(/\d{1,}/g);
    var year = number[0];
    var mont = number[1];

    var tbody = ja_element.getElementsByTagName("tbody");
    var td = tbody[1].getElementsByTagName("td");

    var day;
    for (i = 0; i < td.length; i++) {
		var oldclass = '';
		if(td[i].getAttribute('class')){
			var oldclass = td[i].getAttribute('class');
		}else if(td[i].getAttribute('className')){
			var oldclass = td[i].getAttribute('className');
		}
		
        if(td[i].innerHTML.indexOf("href") != -1){
            day = td[i].getElementsByTagName("a")[0].innerHTML;
        } else {
            day = td[i].innerHTML;
        }
		
        if (isHoliday(year, mont, day)) {
            if (isToday(year, mont, day)) {
                td[i].setAttribute('class', 'tholiday' + oldclass);
                td[i].setAttribute('className', 'tholiday ' + oldclass);
            } else if(isMonday(year,mont,day)){
                td[i].setAttribute('class', 'holiday monday '  + oldclass);
                td[i].setAttribute('className', 'holiday monday ' + oldclass);
			}else if(istosyoHoliday(year,mont,day)){
				td[i].setAttribute('class','holiday tosyoHoliday ' + oldclass);
				td[i].setAttribute('className','holiday tosyoHoliday ' + oldclass);
            } else {
                td[i].setAttribute('class', 'holiday ' + oldclass);
                td[i].setAttribute('className', 'holiday ' + oldclass);
            }
        } else if(isSaturday(year, mont, day)) {
            if (isToday(year, mont, day)) {
                td[i].setAttribute('class', 'tsaturday ' + oldclass);
                td[i].setAttribute('className', 'tsaturday ' + oldclass);
			}else if(istosyoHoliday(year,mont,day)){
				td[i].setAttribute('class','saturday tosyoHoliday' + oldclass);
				td[i].setAttribute('className','saturday tosyoHoliday ' + oldclass);
            } else {
                td[i].setAttribute('class', 'saturday ' + oldclass);
                td[i].setAttribute('className', 'saturday ' + oldclass);
            }
        } else if(isMonday(year,mont,day)){
			if(isToday(year,mont,day)) {
				td[i].setAttribute('calss','tmonday ' + oldclass);
				td[i].setAttribute('className','tmonday ' + oldclass);
			} else {
				td[i].setAttribute('class','monday ' + oldclass);
				td[i].setAttribute('className','monday ' + oldclass);
			}
		}else if(istosyoHoliday(year,mont,day)){
				td[i].setAttribute('class','tosyoHoliday ' + oldclass);
				td[i].setAttribute('className','tosyoHoliday ' + oldclass);
		}else {
            if (isToday(year, mont, day)) {
                td[i].setAttribute('class', 'today ' + oldclass);
                td[i].setAttribute('className', 'today ' + oldclass);
            }
        }
    }

}
