var clsCalendar = Class.create();

var _CALENDAR_REPEAT_FREQ_NONE = "NONE";
var _CALENDAR_REPEAT_FREQ_DAILY = "DAILY";
var _CALENDAR_REPEAT_FREQ_WEEKLY = "WEEKLY";
var _CALENDAR_REPEAT_FREQ_MONTHLY = "MONTHLY";
var _CALENDAR_REPEAT_FREQ_YEARLY = "YEARLY";

clsCalendar.prototype = {
	initialize: function(plan_flg) {
		this.block_id = "";
		this.post_url = _js_url + "/modules/calendar/calendar_addplan.php?plan_flag=" + plan_flg;
		this.token = "";
	},
	showTextarea: function() {
		if (typeof _compTextarea['calendar' + this.block_id] == "undefined") {
			_compTextarea['calendar' + this.block_id] = new compTextarea();
			_compTextarea['calendar' + this.block_id].setUploadAction(this.post_url);
			_compTextarea['calendar' + this.block_id].setTokenName(this.token);
			_compTextarea['calendar' + this.block_id].textareaEditShow('calendar' + this.block_id, "description" + this.block_id, "full");
		}
	},
	saveTextarea: function() {
		if (typeof _compTextarea['calendar' + this.block_id] != "undefined") {
			return _compTextarea['calendar' + this.block_id].getTextArea();
		} else {
			return "";
		}
	},
	initRepeat: function(repeat_flag) {
		this.form_el = $("calendar" + this.block_id);
		this.repeat_flag = repeat_flag;
		
		this.repeat_freq = _CALENDAR_REPEAT_FREQ_NONE;
		if (this.form_el.repeat_freq[0].checked) {
			this.repeat_freq = _CALENDAR_REPEAT_FREQ_DAILY;
		}
		if (this.form_el.repeat_freq[1].checked) {
			this.repeat_freq = _CALENDAR_REPEAT_FREQ_WEEKLY;
		}
		if (this.form_el.repeat_freq[2].checked) {
			this.repeat_freq = _CALENDAR_REPEAT_FREQ_MONTHLY;
		}
		if (this.form_el.repeat_freq[3].checked) {
			this.repeat_freq = _CALENDAR_REPEAT_FREQ_YEARLY;
		}

		if (this.repeat_flag != 0) {
			this.hideRepeatDaily();
			this.hideRepeatWeekly();
			this.hideRepeatMonthly();
			this.hideRepeatYearly();
			
			switch (this.repeat_freq) {
			case _CALENDAR_REPEAT_FREQ_DAILY:
				this.showRepeatDaily(true);
				break;
			case _CALENDAR_REPEAT_FREQ_WEEKLY:
				this.showRepeatWeekly(true);
				break;
			case _CALENDAR_REPEAT_FREQ_MONTHLY:
				this.showRepeatMonthly(true);
				break;
			case _CALENDAR_REPEAT_FREQ_YEARLY:
				this.showRepeatYearly(true);
				break;
			default:
			}
			if (this.form_el.repeat_terminator[0].checked) {
				this.showNoTerm();
			}
			if (this.form_el.repeat_terminator[1].checked) {
				this.showCountTerm();
			}
			if (this.form_el.repeat_terminator[2].checked) {
				this.showDateTerm();
			}
		} else {
			this.setNoRepeat();
		}
	},
	clickRepeatBtn: function() {
		var el = $("repeat" + this.block_id);
		el.style.display = "block";
		this.form_el.repeat_btn.style.display = "none";
		this.form_el.repeat_flag[1].checked = true;
		this.setRepeat();
	},
	setRepeat: function() {
		this.form_el.repeat_freq[0].disabled = false;
		this.form_el.repeat_freq[1].disabled = false;
		this.form_el.repeat_freq[2].disabled = false;
		this.form_el.repeat_freq[3].disabled = false;
		
		this.form_el.repeat_terminator[0].disabled = false;
		this.form_el.repeat_terminator[1].disabled = false;
		this.form_el.repeat_terminator[2].disabled = false;

		this.initRepeat(1);
	},
	setNoRepeat: function() {
		this.form_el.repeat_freq[0].disabled = true;
		this.form_el.repeat_freq[1].disabled = true;
		this.form_el.repeat_freq[2].disabled = true;
		this.form_el.repeat_freq[3].disabled = true;

		this.hideRepeatDaily();
		this.hideRepeatWeekly();
		this.hideRepeatMonthly();
		this.hideRepeatYearly();

		this.form_el.repeat_terminator[0].disabled = true;
		this.form_el.repeat_terminator[1].disabled = true;
		this.form_el.repeat_terminator[2].disabled = true;
		this.form_el.repeat_terminator_until.disabled = true; 
		this.form_el.repeat_terminator_count.disabled = true;
	},
	_hideRepeat: function() {
		switch (this.repeat_freq) {
		case _CALENDAR_REPEAT_FREQ_DAILY:
			this.hideRepeatDaily();
			break;
		case _CALENDAR_REPEAT_FREQ_WEEKLY:
			this.hideRepeatWeekly();
			break;
		case _CALENDAR_REPEAT_FREQ_MONTHLY:
			this.hideRepeatMonthly();
			break;
		case _CALENDAR_REPEAT_FREQ_YEARLY:
			this.hideRepeatYearly();
			break;
		default:
		}
	},
	showRepeatDaily: function(hide_flag) {
		if (typeof hide_flag == "undefined" || !hide_flag) {
			this._hideRepeat();
		}
		this.form_el.repeat_daily_interval.disabled = false;
		this.repeat_freq = _CALENDAR_REPEAT_FREQ_DAILY;
	},
	hideRepeatDaily: function() {
		this.form_el.repeat_daily_interval.disabled = true;
	},
	showRepeatWeekly: function(hide_flag) {
		if (typeof hide_flag == "undefined" || !hide_flag) {
			this._hideRepeat();
		}
		this.form_el.repeat_weekly_interval.disabled = false;
		for (var i=0; i<7; i++) {
			this.form_el["repeat_weekly_bydays[]"][i].disabled = false;
		}
		this.repeat_freq = _CALENDAR_REPEAT_FREQ_WEEKLY;
	},
	hideRepeatWeekly: function() {
		this.form_el.repeat_weekly_interval.disabled = true;
		for (var i=0; i<7; i++) {
			this.form_el["repeat_weekly_bydays[]"][i].disabled = true;
		}
	},
	showRepeatMonthly: function(hide_flag) {
		if (typeof hide_flag == "undefined" || !hide_flag) {
			this._hideRepeat();
		}
		this.form_el.repeat_monthly_interval.disabled = false;
		this.form_el.repeat_monthly_byday.disabled = false;
		this.form_el.repeat_monthly_bymonthday.disabled = false;
		this.repeat_freq = _CALENDAR_REPEAT_FREQ_MONTHLY;
	},
	hideRepeatMonthly: function() {
		this.form_el.repeat_monthly_interval.disabled = true;
		this.form_el.repeat_monthly_byday.disabled = true;
		this.form_el.repeat_monthly_bymonthday.disabled = true;
	},
	showRepeatYearly: function(hide_flag) {
		if (typeof hide_flag == "undefined" || !hide_flag) {
			this._hideRepeat();
		}
		this.form_el.repeat_yearly_interval.disabled = false;
		for (var i=0; i<12; i++) {
			this.form_el["repeat_yearly_bymonthes[]"][i].disabled = false;
		}
		this.form_el.repeat_yearly_byday.disabled = false;
		this.repeat_freq = _CALENDAR_REPEAT_FREQ_YEARLY;
	},
	hideRepeatYearly: function() {
		this.form_el.repeat_yearly_interval.disabled = true;
		for (var i=0; i<12; i++) {
			this.form_el["repeat_yearly_bymonthes[]"][i].disabled = true;
		}
		this.form_el.repeat_yearly_byday.disabled = true;
	},
	showNoTerm: function() {
		this.form_el.repeat_terminator_until.disabled = true; 
		this.form_el.repeat_terminator_count.disabled = true;
	},
	showCountTerm: function() {
		this.form_el.repeat_terminator_until.disabled = true;
		this.form_el.repeat_terminator_count.disabled = false;
	},
	showDateTerm: function() {
		this.form_el.repeat_terminator_until.disabled = false;
		this.form_el.repeat_terminator_count.disabled = true;
	},
	showCalendar: function() {
		if( this.form_el.repeat_terminator_until.disabled == false) { 
			showCalendar("repeat_terminator_until" + this.block_id);
		}
		return false; 
	}
};
var calendarCls = Array();

function calenderSetDialog(id, year, month, month_name, day,day_name, day_of_week,block_id) 
{
	var dialog_obj = $(id);
	var date = $("date" + block_id);
	var subjectDate = $("subjectDate" + block_id + "_" + parseInt(day));
	
	var subject = $("subject" + block_id);
	var frm_addplan = $("frm_addplan" + block_id);
	
	if(date) {
		//日付
		date.href = _js_url + "/modules/calendar/calendar_daily.php?block_id=" + block_id + "&year=" + parseInt(year) + "&month=" + parseInt(month) + "&date=" + parseInt(day) + "#" + block_id;
		date.innerHTML = month_name+day_name + "(" + day_of_week + ")";
	}
	
	//件名をすべて非表示
	if(subject) {
		for (var i = 0; i < subject.childNodes.length; i++) {
			var child = subject.childNodes[i];
			child.style.display = "none";
		}
		if(subjectDate) {
			//件名あり
			subjectDate.style.display = "";
		}
	}
	if (frm_addplan != null) {
		frm_addplan.action = _js_url + "/modules/calendar/calendar_addplan.php?block_id=" + block_id + "&plan_id=0&year=" + parseInt(year) + "&month=" + parseInt(month) + "&date=" + parseInt(day) + "#" + block_id;
	}
	dialog_obj.style.display = "none";
	dialog_obj.style.display = "";
}
function calenderChangeAllday(form_el) 
{
	if (form_el.allday.checked) {
		form_el.start_hour.disabled = true;
		form_el.start_minute.disabled = true;
		form_el.end_hour.disabled = true;
		form_el.end_minute.disabled = true;
	} else {
		form_el.start_hour.disabled = false;
		form_el.start_minute.disabled = false;
		form_el.end_hour.disabled = false;
		form_el.end_minute.disabled = false;
	}
}