//Calendar js
var d = new Date();
//get current month and year
TargetDateMonth = d.getMonth()
TargetDateYear = d.getFullYear()

CurMonth = TargetDateMonth;
CurYear = TargetDateYear;

//Previous Month Button Action, Valid Start Date s(YYYYMMDD), Valid End Date e(YYYYMMDD)
//selectedDate(YYYYMMDD) is the date slected by user, used to set style, empty string if no user selection
function prevMonth(s,e,selectedDate){
	if(!YMInRange(TargetDateYear,TargetDateMonth-1,s,e)){
		return;
	}
	TargetDateMonth--;
	if (TargetDateMonth == -1){
		TargetDateMonth = 11;
		TargetDateYear--;
	}
	writeCal(TargetDateMonth,TargetDateYear,s,e,selectedDate);
}

//Next Month Button Action, Valid Start Date s(YYYYMMDD), Valid End Date e(YYYYMMDD)
//selectedDate(YYYYMMDD) is the date slected by user, used to set style, empty string if no user selection
function nextMonth(s,e,selectedDate){
	if(!YMInRange(TargetDateYear,TargetDateMonth+1,s,e)){
		return;
	}
	TargetDateMonth++;
	if (TargetDateMonth == 12){
		TargetDateMonth = 0;
		TargetDateYear++;
	}
	writeCal(TargetDateMonth,TargetDateYear,s,e,selectedDate);
}

//Write Calendar with Month m, Year y, Valid Start Date s(YYYYMMDD), Valid End Date e(YYYYMMDD)
//selectedDate(YYYYMMDD) is the date slected by user, used to set style, empty string if no user selection
function writeCal(m,y,s,e,selectedDate){
//alert("m:"+m+"\ny: "+y+"\ns: "+s+"\ne: "+e+"\nselectedDate: "+selectedDate)
	if(!YMInRange(y,m,s,e)){
//		writeCal(parseInt(s.substr(4,2),10)-1,parseInt(s.substr(0,4),10),s,e,"");
//alert("not in range")
		return;
	}
	//Find First Day
	var d1 = new Date(y,m,1);
	DayNum = d1.getDay();

	//Find Last Day
	var d1 = new Date(y,m+1,1);
	var d2 = new Date(d1-1);
	DayCount = d2.getDate();

	//if only 4 rows, hide 1 row
	if (DayNum == 0 && DayCount == 28){
		document.getElementById('row4').style.display = "none";
	}else{
		document.getElementById('row4').style.display = "";
	}
//alert("TargetDateMonth:"+TargetDateMonth+"\nTargetDateYear: "+TargetDateYear)
//alert("DayNum:"+DayNum+"\nDayCount: "+DayCount+"\ns: "+s+"\ne: "+e+"\nselectedDate: "+selectedDate)
	writeMonthYear(TargetDateMonth,TargetDateYear);
	writeDay(DayNum,DayCount,s,e,selectedDate);
}

//fd: first day (eg: Mon); ld: last day (eg: 31), Valid Start Date s(YYYYMMDD), Valid End Date e(YYYYMMDD)
//selectedDate(YYYYMMDD) is the date slected by user, used to set style, empty string if no user selection
function writeDay(fd,ld,s,e,selectedDate){ 
	
	selectedDate_year = parseInt(selectedDate.substr(0,4),10);
	selectedDate_month = parseInt(selectedDate.substr(4,2),10)-1;
	selectedDate_day = selectedDate.substr(6,2);
	cur_write_day = 1;
	for (row_i=0;row_i<=5;row_i++){
		for (col_i=0;col_i<=6;col_i++){
			DayId = row_i + "" + col_i;
			document.getElementById(DayId).className ='';
			document.getElementById(DayId).onMouseOver = setCalBg(1,document.getElementById(DayId));
			document.getElementById(DayId).onMouseOut = setCalBg(0,document.getElementById(DayId));
			if (row_i == 0 && col_i < fd){
				document.getElementById(DayId).innerHTML = "";
			}else if (row_i == 0 && col_i >= fd){
				document.getElementById(DayId).innerHTML = cur_write_day;
				if (!isValidDate(TargetDateYear,TargetDateMonth,document.getElementById(DayId).innerHTML,s,e)){
					document.getElementById(DayId).style.cursor = "default";
				}else{
					document.getElementById(DayId).style.cursor = "pointer";
				}

				if (parseInt(TargetDateYear,10) == selectedDate_year && parseInt(TargetDateMonth,10) == selectedDate_month && cur_write_day == selectedDate_day){
					document.getElementById(DayId).onMouseOver = setCalBg(2,document.getElementById(DayId));
					document.getElementById(DayId).onMouseOut = setCalBg(2,document.getElementById(DayId));
					document.getElementById(DayId).style.cursor = "default";
				}

				cur_write_day++;
				if (col_i == 0){
					the_row = "row" + row_i;
					document.getElementById(the_row).style.display = "";
				}
			}else if (cur_write_day > ld){
				document.getElementById(DayId).innerHTML = "";
				if (col_i == 0){
					the_row = "row" + row_i;
					document.getElementById(the_row).style.display = "none";
				}
			}else{
				document.getElementById(DayId).innerHTML = cur_write_day;
				if (!isValidDate(TargetDateYear,TargetDateMonth,document.getElementById(DayId).innerHTML,s,e)){
					document.getElementById(DayId).style.cursor = "default";
				}else{
					document.getElementById(DayId).style.cursor = "pointer";
				}

				
				if (parseInt(TargetDateYear,10) == selectedDate_year && parseInt(TargetDateMonth,10) == selectedDate_month && cur_write_day == selectedDate_day){
					document.getElementById(DayId).onMouseOver = setCalBg(2,document.getElementById(DayId));
					document.getElementById(DayId).onMouseOut = setCalBg(2,document.getElementById(DayId));
					document.getElementById(DayId).style.cursor = "default";
				}

				cur_write_day++;
				if (col_i == 0){
					the_row = "row" + row_i;
					document.getElementById(the_row).style.display = "";
				}
			}
		}
	}
}

//Select Date Action, Valid Start Date s(YYYYMMDD), Valid End Date e(YYYYMMDD)
//selectedDate(YYYYMMDD) is the date slected by user, used to set style, empty string if no user selection
function selectDate(dateObj,s,e,selectedDate){

	selectedDate_year = parseInt(selectedDate.substr(0,4),10);
	selectedDate_month = parseInt(selectedDate.substr(4,2),10)-1;
	selectedDate_day = parseInt(selectedDate.substr(6,2),10);
	cur_write_day = parseInt(dateObj.innerHTML,10);

	if (dateObj.innerHTML != ""){
		if (isValidDate(TargetDateYear,TargetDateMonth,dateObj.innerHTML,s,e)){
			if (parseInt(TargetDateYear,10) == selectedDate_year && parseInt(TargetDateMonth,10) == selectedDate_month && cur_write_day == selectedDate_day){
				selectedMonth = addZero(TargetDateMonth+1);
				location.href = "#";
			}else{
				selectedMonth = addZero(TargetDateMonth+1);
				location.href = "index.php?selectedDate=" + TargetDateYear + selectedMonth + addZero(dateObj.innerHTML) + "&did=" + dateObj.id;
			}
		}else{
		}
	}
}

function addZero(i){
	if (i < 10)
		return "0" + i;
	else
		return i;
}

//Return the Fullname of the Month
function monthFullName(i){
	var currentPath=location.href.toString();
	var switchTc='/tc/';
	var switchEn='/en/';
	var switchSc = '/sc/';
	if (currentPath.indexOf(switchTc) != -1){
		switch(i){
			case 0:
			  return "1月";
			  break;    
			case 1:
			  return "2月";
			  break;
			case 2:
			  return "3月";
			  break;
			case 3:
			  return "4月";
			  break;
			case 4:
			  return "5月";
			  break;
			case 5:
			  return "6月";
			  break;
			case 6:
			  return "7月";
			  break;
			case 7:
			  return "8月";
			  break;
			case 8:
			  return "9月";
			  break;
			case 9:
			  return "10月";
			  break;
			case 10:
			  return "11月";
			  break;
			case 11:
			  return "12月";
			  break;
			default:
			  return "";
		}
	}else if (currentPath.indexOf(switchSc) != -1){
		switch(i){
			case 0:
			  return "1月";
			  break;    
			case 1:
			  return "2月";
			  break;
			case 2:
			  return "3月";
			  break;
			case 3:
			  return "4月";
			  break;
			case 4:
			  return "5月";
			  break;
			case 5:
			  return "6月";
			  break;
			case 6:
			  return "7月";
			  break;
			case 7:
			  return "8月";
			  break;
			case 8:
			  return "9月";
			  break;
			case 9:
			  return "10月";
			  break;
			case 10:
			  return "11月";
			  break;
			case 11:
			  return "12月";
			  break;
			default:
			  return "";
		}
	}else{
		switch(i){
			case 0:
			  return "January";
			  break;    
			case 1:
			  return "February";
			  break;
			case 2:
			  return "March";
			  break;
			case 3:
			  return "April";
			  break;
			case 4:
			  return "May";
			  break;
			case 5:
			  return "June";
			  break;
			case 6:
			  return "July";
			  break;
			case 7:
			  return "August";
			  break;
			case 8:
			  return "September";
			  break;
			case 9:
			  return "October";
			  break;
			case 10:
			  return "November";
			  break;
			case 11:
			  return "December";
			  break;
			default:
			  return "";
		}
	}
}

//Write the Month and Year at the top of the Calendar
function writeMonthYear(m,y){
	var currentPath=location.href.toString();
	var switchTc='/tc/';
	var switchEn='/en/';
	var switchSc = '/sc/';
	if (currentPath.indexOf(switchTc) != -1){
		document.getElementById('monthyear').innerHTML = y + "年" + monthFullName(m);
	}else if (currentPath.indexOf(switchSc) != -1){
		document.getElementById('monthyear').innerHTML = y + "年" + monthFullName(m);
	}else{
		document.getElementById('monthyear').innerHTML = monthFullName(m) + " " + y;
	}
}

//Set Background Color of the Calendar
function setCalBg(i,obj){
	if (i == 1 && obj.className !='calHl'){
		obj.className ='calMouseOver';
	}else if (i == 2){	// select a day
		obj.className ='calHl';
//	}else if (i == 3){	// remove a selected day
//		obj.className ='calMouseOver';
	}else if (obj.className !='calHl'){
		obj.className ='';
	}
}

//Check if particular year y and month m is in the range between Start Date s(YYYYMMDD) and End Date e(YYYYMMDD)
function YMInRange(y,m,s,e){
	
//alert("y:"+y+"\nm: "+m+"\ns: "+s+"\ne: "+e)

	s_year = parseInt(s.substr(0,4),10);
	s_month = parseInt(s.substr(4,2),10)-1;
	e_year = parseInt(e.substr(0,4),10);
	e_month = parseInt(e.substr(4,2),10)-1;

	if (parseInt(y,10) < s_year){
		return false;
	}else if (parseInt(y,10) > e_year){
		return false;
	}else if (parseInt(y,10) >= s_year && parseInt(y,10) <= e_year){
		if (parseInt(y,10) == s_year){
			if (parseInt(m,10) < s_month){
				return false;
			}
		}
		if (parseInt(y,10) == e_year){
			if (parseInt(m,10) > e_month){
				return false;
			}
		}
	}
	return true;
}

//Check if particular year y and month m and day d is in the range between Start Date s(YYYYMMDD) and End Date e(YYYYMMDD)
function isValidDate(y,m,d,s,e){
	s_year = parseInt(s.substr(0,4),10);
	s_month = parseInt(s.substr(4,2),10)-1;
	s_day = parseInt(s.substr(6,2),10);
	e_year = parseInt(e.substr(0,4),10);
	e_month = parseInt(e.substr(4,2),10)-1;
	e_day = parseInt(e.substr(6,2),10);

	var particularDay = new Date();
 	particularDay.setFullYear(y,m,d);
	var startDay = new Date();
 	startDay.setFullYear(s_year,s_month,s_day);
	var endDay = new Date();
 	endDay.setFullYear(e_year,e_month,e_day);

	if (particularDay >= startDay && particularDay <= endDay)
		return true;
	else
		return false;
}

//Select Date Action, Valid Start Date s(YYYYMMDD), Valid End Date e(YYYYMMDD)
//selectedDate(YYYYMMDD) is the date slected by user, used to set style, empty string if no user selection
function outOfCalendarMsg(dateObj,s,e,selectedDate,err_msg){
	selectedDate_year = parseInt(selectedDate.substr(0,4),10);
	selectedDate_month = parseInt(selectedDate.substr(4,2),10)-1;
	selectedDate_day = parseInt(selectedDate.substr(6,2),10);
	cur_write_day = parseInt(dateObj.innerHTML,10);
	if (!isValidDate(TargetDateYear,TargetDateMonth,dateObj.innerHTML,s,e)){
		alert(err_msg);
	}
}