var srcElem; //reference of the object who owns calendar
var oldBGC;  //remember old TD's backgroup color
var separator;

//open calendar
function Open(srcElementID)
{
	srcElem = window.document.all(srcElementID);
	if (srcElem==null) return;
	if (srcElem.value==null) return;
	
	var text = srcElem.value;
	if(text.length!=0)
	{
		Sel_Date.title=text;
		replaceTextChild(document.getElementById('Sel_Date'), text);
		text=text.split(" ");
		element.all(element.id + '_Year').value=text[2];
		element.all(element.id + '_Month').value=parseInt(getMonthString(text[0]),10)-1;
		Changed(false);
	}
	
	element.style.top = window.event.clientY+20;
	element.style.left = 20;	//window.event.clientX;
	element.style.display = '';
}

//when the year and month changed
function Changed(toToday)
{
	var dtYear, dtMonth, dtBegin
	if (toToday == true)
	{
		dtYear = (new Date()).getFullYear();
		dtMonth = (new Date()).getMonth();
	}
	else
	{
		dtYear = element.all(element.id + '_Year').value;
		dtMonth = element.all(element.id + '_Month').value;
	}
	
	dtBegin = new Date();
	dtBegin.setDate(1);
	dtBegin.setMonth(dtMonth);
	dtBegin.setYear(dtYear);
	element.all(element.id + '_Month').selectedIndex = dtMonth;
	GenerateCalendarDate(element, dtBegin);
}

//left click the mouse on calendar
function Clicked()
{
	var eSrc = window.event.srcElement;
	if (eSrc.id == 'CalendarDate') srcElem.value = eSrc.title;
	element.style.display = 'none';
}

//get first weekday(sunday) of the date
function GetFirstWeekday(dtDate)
{
	var i = dtDate.getDay();
	dtDate.setDate(dtDate.getDate() - i);
	return dtDate;
}

//formate the date / return the date
function DateFormat(dtDate)
{
	var strRtn;	
	strRtn=getMonthString(dtDate.getMonth()+1)+ " ";
	if (dtDate.getDate() < 10) strRtn = strRtn + '0' + dtDate.getDate();
	else strRtn = strRtn + dtDate.getDate();
	strRtn+=", " +dtDate.getFullYear();
	return strRtn;
}

//generate cladndar
function GenerateCalendar(oCalendar)
{
	element=oCalendar;
	GenerateCalendarHeader(oCalendar);
	GenerateCalendarSelection(oCalendar);
	GenerateCalendarBody(oCalendar);
	Changed(true);
}

//create the header of calendar
function GenerateCalendarHeader(oCalendar)
{
	var strHTML;
	strHTML = '<div align="right" id="' + oCalendar.id + '_Title" ';
	strHTML += 'OnSelectStart="javascript:return false;" >';
	strHTML += '<table border="0" cellpadding="0" cellspacing="0" width="100%" ';
	strHTML += 'style="color:white;background-color:Midnightblue;font-size:12px;">';
	strHTML += '<tr>';
	strHTML += '<td align="left" style="padding:4px">';
	strHTML += '<span onclick="Changed(true);" id="Sel_Date" ';
	strHTML += 'style="font-size:12px; color:white" ';
	strHTML += 'onmouseover="javascript:this.style.color=\'red\'" ';
	strHTML += 'onmouseout="javascript:this.style.color=\'white\'" ';
	strHTML += 'title="' + DateFormat((new Date)) + '">' + DateFormat((new Date)) + '</span>';
	strHTML += '</td>';
	strHTML += '<td align="right" style="padding:4px">';
	strHTML += '<span onclick="javascript:' + element.id +'.style.display=\'none\';" ';
	strHTML += 'style="font-size:bolder; color:white" ';
	strHTML += 'onmouseover="javascript:this.style.color=\'red\'" ';
	strHTML += 'onmouseout="javascript:this.style.color=\'white\'" ';
	strHTML += 'title="' +pb_closecalendar+ '">&nbsp;X</span>';
	strHTML += '</td></tr></table>';
	strHTML += '</div>';
	oCalendar.insertAdjacentHTML("BeforeEnd", strHTML);
}

//generate calendar selecttion(year and month)
function GenerateCalendarSelection(oCalendar)
{
	var strHTML, today, i;
	var oSelect, oOption;
	
	today = new Date();
	strHTML = '<div id="' + oCalendar.id + '_Selection" align="center" OnSelectStart="javascript:return false;">';
	strHTML += '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
	strHTML += '<tr>';
	strHTML += '<td align="center">';
	strHTML += '<select id="' + oCalendar.id + '_Year" '
	strHTML += 'onchange="Changed(false);" '
	strHTML += 'size="1" style="font-size:12px; background-color:azure;"></select>&nbsp;&nbsp;';
	strHTML += '</td>';
	strHTML += '<td align="center">';
	strHTML += '<select id="' + oCalendar.id + '_Month" '
	strHTML += 'onchange="Changed(false);" '
	strHTML += 'size="1" style="font-size:12px; background-color:azure;"></select>';
	strHTML += '</td>';
	strHTML += '</tr>';
	strHTML += '</table>';
	strHTML += '</div>';
	oCalendar.insertAdjacentHTML("BeforeEnd", strHTML);
	
	oSelect = oCalendar.all(oCalendar.id + '_Year');
	oSelect.style.cursor = 'hand';
	
	for (i=StartTime.substring(0,4) ;i<=EndTime.substring(0,4);i++)
	{
		oOption = window.document.createElement('option');
		oOption.value = i + '';
		oOption.text = i;
		oSelect.options.add(oOption);
		if (oOption.value == today.getFullYear()+'') oOption.selected = true;
	}
	
	oSelect = oCalendar.all(oCalendar.id + '_Month');
	oSelect.style.cursor = 'hand';
	//getMonth: 0~11
	for (i=0;i<=11;i++)
	{
		oOption = window.document.createElement('option');
		oOption.value = i + '';
		oOption.text = getMonthText(i + 1);
		oSelect.options.add(oOption);
		if (oOption.value == eval(today.getMonth() + 1)) oOption.selected = true;
	}
}

//geerate date tabled(empty)
function GenerateCalendarBody(oCalendar)
{
	var strHTML, oTable, oTr, oTd;
	var today;
	
	strHTML = '<table id="' + oCalendar.id + '_Body" width="100%" height="100%" ';
	strHTML += 'OnSelectStart="javascript:return false;" ';
	strHTML += 'onclick="Clicked();" ';
	strHTML += 'onMouseOver="CalendarOnmouseover();" ';
	strHTML += 'onMouseOut="CalendarOnmouseout();" ';
	strHTML += 'border="0" cellpadding="2" cellspacing="1" ';
	strHTML += 'style="background-color:dimgray;"></table>';
	oCalendar.insertAdjacentHTML('BeforeEnd', strHTML);
	oTable = oCalendar.all(oCalendar.id + '_Body');
	oTable.style.fontSize = "9pt";
	oTable.style.textAlign = "center";
	oTable.style.cursor = "hand";
	
	var Weekdays = new Array(schedule_sun,schedule_mon,schedule_tur,schedule_wed,schedule_thu,schedule_fri,schedule_sat);
	oTr = oTable.insertRow(-1);
	if (oTr!=null)
	{
		for (i=0;i<=6;i++)
		{
			oTd = oTr.insertCell(-1);
			oTd.style.backgroundColor = 'Darkgray'; //'LightSteelBlue';
			oTd.innerText = Weekdays[i];
		}
	}
	
	var today = new Date();	//generate all colums and rows
	for (i=1;i<=6;i++)
	{
		oTr = oTable.insertRow(-1);
		for (j=1;j<=7;j++)
		{
			oTd = oTr.insertCell(-1);
			oTd.id = "CalendarDate";	//elements with this id: selectable date
			oTd.title = '';
			oTd.innerText = 0;
		}
	}
}

//filled date data
function GenerateCalendarDate(oCalendar, dtBegin)
{
	var i, oCalendar, cDates;
	var today, dtMonth;
	
	today = new Date();
	dtMonth = dtBegin.getMonth();
	dtBegin = GetFirstWeekday(dtBegin);
	cDates = oCalendar.all('CalendarDate');
	for (i=0;i<=cDates.length-1;i++)
	{
		cDates[i].title = DateFormat(dtBegin);
		cDates[i].innerText = dtBegin.getDate() + '';
		cDates[i].style.backgroundColor = 'Cornsilk';
		cDates[i].style.color = 'Black'; 			
		if (DateFormat(dtBegin) == DateFormat(today))			//today
		{
			cDates[i].style.backgroundColor = 'Wheat'; 			//'Cornflowerblue';
			cDates[i].style.color = 'Black'; 					//'Cornflowerblue';
		}
		else
		{
			if (dtBegin.getMonth() != dtMonth)					//not today's month
			{
				cDates[i].style.backgroundColor = 'Gainsboro';
				cDates[i].style.color = 'Darkgray';
			}
			else
			{
				if (dtBegin.getDay()==0 || dtBegin.getDay()==6)	//weekend
				{
					cDates[i].style.backgroundColor = 'Pink';
					cDates[i].style.color = 'Black';
				}
			}
		}
		yy=dtBegin.getFullYear();
		mm = dtBegin.getMonth();
		dd=dtBegin.getDate();
		if(typeof(Segment[yy])!="undefined")
		{
			if(typeof(Segment[yy][mm+1])!="undefined")
			{
				if(typeof(Segment[yy][mm+1][dd])!="undefined")
				{
					if(Segment[yy][mm+1][dd]==true) cDates[i].style.backgroundColor = '#38508d';
				}
			}
		}
		dtBegin.setDate(dtBegin.getDate() + 1);
	}
}

//initial calendar
function CalendarInitial(element,separa, startyear, systemyear)
{
	StartTime=startyear+ "/00/00";
	EndTime=systemyear+ "/00/00";
	var strHTML;
	if (element.tagName != 'DIV') return;
	if (element.id=='' || element.id==null) return;
	element.innerHTML="";	//clear all html in element
	
	//設定物件樣式
	element.style.display = 'none';
	element.style.zIndex = 1000;
	element.style.textAlign = "center";
	element.style.border = "2 outset Silver";
	element.style.fontSize = "9pt";
	element.style.position = "absolute";
	element.style.top = 0;
	element.style.left = 0;
	element.style.backgroundColor = "Darkgray";
	element.style.cursor = "hand";
	element.style.width = "250";
	element.style.height = "";
	if (separa==null || separa=='') separator = '.';
	else separator = separa;
	
	GenerateCalendar(element);
}

function CalendarOnmouseover()
{
	var eSrc = window.event.srcElement;
	if (eSrc.id == 'CalendarDate')		//滑鼠移入日期方格
	{
		oldBGC = eSrc.style.color;		//先記錄目前方格的前景色
		eSrc.style.color = 'red';		//再換掉目前方格的前景色
		eSrc.style.fontWeight = 'bold';
	}
}

function CalendarOnmouseout()
{
	var eSrc = window.event.srcElement;
	if (eSrc.id == 'CalendarDate')		//滑鼠移出日期方格
	{
		eSrc.style.color = oldBGC;		//以記錄起來的背景色還原
		eSrc.style.fontWeight = 'normal';
	}
}

var StartTime="0000/00/00"; EndTime="0000/00/00";
var Segment=new Array();

function getMonthText(mm)
{
	if(mm==1) return pb_jan;
	else if(mm==2) return pb_feb;
	else if(mm==3) return pb_mar;
	else if(mm==4) return pb_apr;
	else if(mm==5) return pb_may;
	else if(mm==6) return pb_jun;
	else if(mm==7) return pb_jul;
	else if(mm==8) return pb_aug;
	else if(mm==9) return pb_sep;
	else if(mm==10) return pb_oct;
	else if(mm==11) return pb_nov;
	else if(mm==12) return pb_dec;
	else return "";
}