//XMLHttpRequestオブジェクト生成
function createHttpRequest(){
	// Win ie用
	if(window.ActiveXObject){
		try{
			// MSXML2以降用
			return new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try{
				// 旧MSXML用
				return new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e2) {
				return null;
			}
		}
	}else if(window.XMLHttpRequest){
		// Win ie以外
		return new XMLHttpRequest();
	}else{
		return null;
	}
}

// カレンダー入力枠
function calendarForm(year, month, day) {
	url = './calendarForm.php?year=' + year + '&month=' + month + '&day=' + day;
	
	var httpObj = createHttpRequest();
	httpObj.open( 'GET', url, true );
	
	httpObj.onreadystatechange = function(){
		if(httpObj.readyState == 4){
			on_loaded_form(httpObj);
		}
	}
	
	httpObj.send("dummy");
}

// カレンダー編集
function calendarEditForm(year, month, day, id) {
	url = './calendarForm.php?ACT_edit=1&calendar_id=' + id + '&year=' + year + '&month=' + month + '&day=' + day;
	
	var httpObj = createHttpRequest();
	httpObj.open( 'GET', url, true );
	
	httpObj.onreadystatechange = function(){
		if(httpObj.readyState == 4){
			on_loaded_form(httpObj);
		}
	}
	
	httpObj.send("dummy");
}

// 削除
function calendarDel(id) {
	url = './calendarForm.php?ACT_del=1&calendar_id=' + id;
	if (confirm('削除しますか？')) {
		location.href = url;
	}
}

// カレンダー
function calendarHtml(year, month) {
	url = './calendar.php?ACT_calendar=1&year=' + year + '&month=' + month;

	var httpObj = createHttpRequest();
	httpObj.open( 'GET', url, true );
	
	httpObj.onreadystatechange = function(){
		if(httpObj.readyState == 4){
			on_loaded_cal(httpObj);
		}
	}
	
	httpObj.send("dummy");
}

// カレンダー詳細
function calendarDetail(year, month, day) {
	url = './calendar.php?ACT_calendar_detail=1&year=' + year + '&month=' + month + '&day=' + day;

	var httpObj = createHttpRequest();
	httpObj.open( 'GET', url, true );
	
	httpObj.onreadystatechange = function(){
		if(httpObj.readyState == 4){
			on_loaded_form(httpObj);
		}
	}
	
	httpObj.send("dummy");
}

// カレンダーフォーム入力チェック
function checkCalendarForm(f) {

	var flag = true;
	var radioFlag = false;
	if (f.calendar_title.value == "") {
		document.getElementById('calendarTitleError').innerHTML = 'タイトルを入力してください';
		flag = false;
	}
	for(i=0;i<f.calendar_reminder_flag.length; i++){
		if (f.calendar_reminder_flag[i].checked) {
			radioFlag = true;
			break;
		}
	}
	
	if (radioFlag == false) {
		document.getElementById('calendarReminderError').innerHTML = 'お知らせメールを選択してください';
		flag = false;
	}
	
	if (flag == true) {
		return true;
	} else {
		return false;
	}
}

// TOP用カレンダー
function topCalendarHtml(year, month) {
	url = './index.php?ACT_calendar=1&year=' + year + '&month=' + month;

	var httpObj = createHttpRequest();
	httpObj.open( 'GET', url, true );
	
	httpObj.onreadystatechange = function(){
		if(httpObj.readyState == 4){
			on_loaded_cal_top(httpObj);
		}
	}
	
	httpObj.send("dummy");
}

// コールバック関数
function on_loaded_form(obj){
	res = obj.responseText;
	
	document.getElementById('CalendarMain').innerHTML = res;
}

function on_loaded_cal(obj){
	res = obj.responseText;
	
	document.getElementById('SideDate').innerHTML = res;
}

function on_loaded_cal_top(obj){
	res = obj.responseText;
	
	document.getElementById('Calendar').innerHTML = res;
}


