var weekend = [0,6];
var weekendColor = "#e0e0e0";
var weekdayColor = "#c0c0c0";
var fontface = "Verdana, Arial, Helvetica, sans-serif";
var fontsize = 1;			
var gNow = new Date();
var ggWinContent;
var ggPosX = -1;
var ggPosY = -1;
var curYear = 2009;

Calendar.DOMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
Calendar.lDOMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

hdMonth = [01, 01, 01, 01, 04, 04, 04, 04, 05, 05, 05, 07, 10, 10, 10, 12, 12];
hdDay   = [01, 26, 27, 28, 04, 10, 11, 13, 01, 02, 28, 01, 01, 03, 26, 25, 26];
 
function Calendar(p_item, p_month, p_year, p_format) {
	if ((p_month == null) && (p_year == null))	return;

	if (p_month == null) {
		this.gMonthName = null;
		this.gMonth = null;
		this.gYearly = true;
	} else {
		this.gMonthName = Calendar.get_month(p_month);
		this.gMonth = new Number(p_month);
		this.gYearly = false;
	}

	this.gYear = p_year;
	this.gFormat = p_format;
	this.gBGColor = "white";
	this.gFGColor = "black";
	this.gTextColor = "black";
	this.gHeaderColor = "black";
	this.gReturnItem = p_item;
}

Calendar.get_month = Calendar_get_month;
Calendar.get_daysofmonth = Calendar_get_daysofmonth;
Calendar.calc_month_year = Calendar_calc_month_year;

function Calendar_get_month(monthNo) {
	return MM[monthNo];
}

function Calendar_get_daysofmonth(monthNo, p_year) {

	if ((p_year % 4) == 0) {
		if ((p_year % 100) == 0 && (p_year % 400) != 0)
			return Calendar.DOMonth[monthNo];
	
		return Calendar.lDOMonth[monthNo];
	} else
		return Calendar.DOMonth[monthNo];
}

function Calendar_calc_month_year(p_Month, p_Year, incr) {
	
	var ret_arr = new Array();
	
	if (incr == -1) {
		if (p_Month == 0) {
			ret_arr[0] = 11;
			ret_arr[1] = parseInt(p_Year) - 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) - 1;
			ret_arr[1] = parseInt(p_Year);
		}
	} else if (incr == 1) {
		if (p_Month == 11) {
			ret_arr[0] = 0;
			ret_arr[1] = parseInt(p_Year) + 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) + 1;
			ret_arr[1] = parseInt(p_Year);
		}
	}
	
	return ret_arr;
}

function Calendar_calc_month_year(p_Month, p_Year, incr) {

	var ret_arr = new Array();
	
	if (incr == -1) {
		if (p_Month == 0) {
			ret_arr[0] = 11;
			ret_arr[1] = parseInt(p_Year) - 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) - 1;
			ret_arr[1] = parseInt(p_Year);
		}
	} else if (incr == 1) {
		if (p_Month == 11) {
			ret_arr[0] = 0;
			ret_arr[1] = parseInt(p_Year) + 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) + 1;
			ret_arr[1] = parseInt(p_Year);
		}
	}
	
	return ret_arr;
}


Calendar.prototype.getMonthlyCalendarCode = function() {
	var vCode = "";
	var vHeader_Code = "";
	var vData_Code = "";
	

	vCode += ("<div align=center><TABLE BORDER=0 BGCOLOR=\"" + this.gBGColor + "\" style='font-size:" + fontsize + "pt;'>");
	
	vHeader_Code = this.cal_header();
	vData_Code = this.cal_data();
	vCode += (vHeader_Code + vData_Code);
	vCode += "<tr><td colspan=7 align=center >"+myFooter+"<td><tr></TABLE></div>";
	return vCode;
}

Calendar.prototype.show = function() {
	var vCode = "";


	var prevMMYYYY = Calendar.calc_month_year(this.gMonth, this.gYear, -1);
	var prevMM = prevMMYYYY[0];
	var prevYYYY = prevMMYYYY[1];

	var nextMMYYYY = Calendar.calc_month_year(this.gMonth, this.gYear, 1);
	var nextMM = nextMMYYYY[0];
	var nextYYYY = nextMMYYYY[1];
	
	ggWinContent += ("<TABLE WIDTH='100%' BORDER=1 CELLSPACING=0 CELLPADDING=0 BGCOLOR='#e0e0e0' style='font-size:" + fontsize + "pt;'><TR><TD ALIGN=center>");
	ggWinContent += ("<A HREF=\"javascript:void(0);\" " +
		"onMouseOver=\"window.status='Go back one month'; return true;\" " +
		"onMouseOut=\"window.status=''; return true;\" " +
		"onClick=\"Build(" + 
		"'" + this.gReturnItem + "', '" + prevMM + "', '" + prevYYYY + "', '" + this.gFormat + "'" +
		");" +
		"\"><FONT FACE='" + fontface + "' ><B>&lt; </B></FONT><\/A></TD><TD ALIGN=center>");
	ggWinContent += ("<FONT FACE='" + fontface + "' ><B>");
	if (lang.substring(1,4)=="Eng")
		ggWinContent += (this.gMonthName + " " + this.gYear);
	else	
		ggWinContent += ( this.gYear+ " " + this.gMonthName);
	ggWinContent += "</B>";
	ggWinContent += ("</TD><TD ALIGN=center>");
	ggWinContent += ("<A HREF=\"javascript:void(0);\" " +
		"onMouseOver=\"window.status='Go forward one month'; return true;\" " +
		"onMouseOut=\"window.status=''; return true;\" " +
		"onClick=\"Build(" + 
		"'" + this.gReturnItem + "', '" + nextMM + "', '" + nextYYYY + "', '" + this.gFormat + "'" +
		");" +
		"\"><FONT FACE='" + fontface + "' ><B>></B></FONT><\/A></TD></TR></TABLE>");


	vCode = this.getMonthlyCalendarCode();
	ggWinContent += vCode;
}



Calendar.prototype.cal_header = function() {
	var vCode = "";
	
	vCode = vCode + "<TR>";
	vCode = vCode + "<TD WIDTH='14%' ALIGN='center'><font FACE='" + fontface + "' COLOR='RED'><B>"+WW[0]+"</B></FONT></TD>";
	vCode = vCode + "<TD WIDTH='14%' ALIGN='center'><font FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>"+WW[1]+"</B></FONT></TD>";
	vCode = vCode + "<TD WIDTH='14%' ALIGN='center'><font FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>"+WW[2]+"</B></FONT></TD>";
	vCode = vCode + "<TD WIDTH='14%' ALIGN='center'><font FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>"+WW[3]+"</B></FONT></TD>";
	vCode = vCode + "<TD WIDTH='14%' ALIGN='center'><font FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>"+WW[4]+"</B></FONT></TD>";
	vCode = vCode + "<TD WIDTH='14%' ALIGN='center'><font FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>"+WW[5]+"</B></FONT></TD>";
	vCode = vCode + "<TD WIDTH='16%' ALIGN='center'><font FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>"+WW[6]+"</B></FONT></TD>";
	vCode = vCode + "</TR>";
	
	return vCode;
}

Calendar.prototype.cal_data = function() {
	var vDate = new Date();
	vDate.setDate(1);
	vDate.setMonth(this.gMonth);
	vDate.setFullYear(this.gYear);
	var vNowDay =   gNow.getDate();
	var vNowMonth = gNow.getMonth();
	var vNowYear =  gNow.getFullYear();
	var vFirstDay=vDate.getDay();
	var vDay=1;
	var vLastDay=Calendar.get_daysofmonth(this.gMonth, this.gYear);
	var vOnLastDay=0;
	var vCode = "";


	vCode = vCode + "<TR>";
	for (i=0; i<vFirstDay; i++) {
		vCode = vCode + "<TD WIDTH='14%'" + this.write_weekend_string(i, -1) + "><FONT FACE='" + fontface + "'>&nbsp; </FONT></TD>";
	}


	for (j=vFirstDay; j<7; j++) {
		
		vCode = vCode + "<TD ALIGN=RIGHT WIDTH='14%'" + this.write_weekend_string(j, vDay) + "><FONT FACE='" + fontface + "'>";
		if ((vNowYear*10000 + vNowMonth*100 + vNowDay) <=  (this.gYear*10000 + this.gMonth*100 +vDay)) {
				vCode+= "<A HREF='javascript:void(0);' " + 
				"onMouseOver=\"window.status='set date to " + this.format_data(vDay) + "'; return true;\" " +
				"onMouseOut=\"window.status=' '; return true;\" " +
				"onClick=\"document." + this.gReturnItem + ".value='" + 
				this.format_data(vDay) + 
				"';getFromCalender();ggPosX=-1;ggPosY=-1;nd();nd();\">";
		}		
		vCode+=this.format_day(j, vDay) + "</A>" + "</FONT></TD>";
		vDay=vDay + 1;
	}
	vCode = vCode + "</TR>";

	// Write the rest of the weeks
	for (k=2; k<7; k++) {
		vCode = vCode + "<TR>";

		for (j=0; j<7; j++) {
			vCode = vCode + "<TD ALIGN=RIGHT WIDTH='14%'" + this.write_weekend_string(j, vDay) + "><FONT FACE='" + fontface + "'>";
			if ((vNowYear*10000 + vNowMonth*100 + vNowDay) <=  (this.gYear*10000 + this.gMonth*100 +vDay)) {
				vCode+= "<A HREF='javascript:void(0);' " + 
				"onMouseOver=\"window.status='set date to " + this.format_data(vDay) + "'; return true;\" " +
				"onMouseOut=\"window.status=' '; return true;\" " +
				"onClick=\"document." + this.gReturnItem + ".value='" + 
				this.format_data(vDay) + 
				"';getFromCalender();ggPosX=-1;ggPosY=-1;nd();nd();\">";
			}		
			vCode+=this.format_day(j, vDay) + "</A>" + "</FONT></TD>";
			vDay=vDay + 1;

			if (vDay > vLastDay) {
				vOnLastDay = 1;
				break;
			}
		}

		if (j == 6)
			vCode = vCode + "</TR>";
		if (vOnLastDay == 1)
			break;
	}
	
	// Fill up the rest of last week with proper blanks, so that we get proper square blocks
	for (m=1; m<(7-j); m++) {
		if (this.gYearly)
			vCode = vCode + "<TD ALIGN=RIGHT WIDTH='14%'" + this.write_weekend_string(j+m, -1) + 
			"><FONT FACE='" + fontface + "' COLOR='gray'> </FONT></TD>";
		else
			vCode = vCode + "<TD ALIGN=RIGHT WIDTH='14%'" + this.write_weekend_string(j+m, -1) + 
			"><FONT FACE='" + fontface + "' COLOR='gray'>" + m + "</FONT></TD>";
	}
	
	return vCode;
}

Calendar.prototype.format_day = function(day, vday) {
	var i;
	var vNowDay = gNow.getDate();
	var vNowMonth = gNow.getMonth();
	var vNowYear = gNow.getFullYear();
	var vNowDay =   gNow.getDate();
	var vNowMonth = gNow.getMonth();
	var vNowYear =  gNow.getFullYear();
	
	var str=vday;
	if ((vNowYear*10000 + vNowMonth*100 + vNowDay) >=  (this.gYear*10000 + this.gMonth*100 +vday)) 
		str = "<FONT COLOR=\"black\">" + vday + "</FONT>";			
			for (i=0; i< hdMonth.length; i++)
		if ((this.gMonth==hdMonth[i]-1 && hdDay[i]==vday && this.gYear==curYear)|| day==0)
			str = "<FONT COLOR=\"RED\">" + vday + "</FONT>";	
	if (vday == vNowDay && this.gMonth == vNowMonth && this.gYear == vNowYear)
		str="<B>"+str+"</B>";

	return str;		
		
}

Calendar.prototype.write_weekend_string = function(day, vday) {
	var i;
	var str = " BGCOLOR=\"" + weekdayColor + "\"";
	if (p_item=="queryform.D1"){
		titleColor ="#6495ED";
	}else{ 	
		titleColor ="#84A5FD";
	}
		
	// Return special formatting for the weekend day.
	for (i=0; i<weekend.length; i++) 
		if (day == weekend[i]) 
			str=" BGCOLOR=\"" + weekendColor + "\"";

	if (vday == document.queryform.leavedate.value && this.gMonth == document.queryform.leavemonth.value-1)
		str=" BGCOLOR=ORANGE";
	if (vday == document.queryform.deptdate.value && this.gMonth == document.queryform.deptmonth.value-1 && this.gYear == document.queryform.deptyear.value){
		str=" BGCOLOR=" + titleColor;
	}	
	if (lang.substring(0,1)=="H"){
		ref=this.gYear * 10000 + this.gMonth * 100 + vday;	
		deptref=parseInt(document.queryform.deptyear.value) * 10000 + parseInt((document.queryform.deptmonth.value-1)) * 100 + parseInt(document.queryform.deptdate.value);
		rtnref=parseInt(document.queryform.leaveyear.value) * 10000 + parseInt((document.queryform.leavemonth.value-1)) * 100 + parseInt(document.queryform.leavedate.value);
		if (vday!=-1 && ref>=deptref && ref<rtnref){
			str=" BGCOLOR=" + titleColor;
		}
	}			
				
	return (str);

}

Calendar.prototype.format_data = function(p_day) {
	var vMonth = 1 + this.gMonth;
	vMonth = (vMonth.toString().length < 2) ? "0" + vMonth : vMonth;
	var vY4 = new String(this.gYear);
	var vDD = (p_day.toString().length < 2) ? "0" + p_day : p_day;
	return (vY4 + "-" + vMonth + "-" + vDD);
}

function Build(p_item, p_month, p_year, p_format) {

	gCal = new Calendar(p_item, p_month, p_year, p_format);
	gCal.gBGColor="white";
	gCal.gLinkColor="black";
	gCal.gTextColor="black";
	gCal.gHeaderColor="darkgreen";
	ggWinContent = "";

	gCal.show();
     
	if (p_item=="queryform.D1"){
		titleColor="#3399FF";
	}else{ 	
		titleColor="#84A5FD";
	}
			 
 	overlib(ggWinContent, AUTOSTATUSCAP, STICKY, CLOSECLICK, CSSSTYLE,
	TEXTSIZEUNIT, "pt", TEXTSIZE, 4, CAPTIONSIZEUNIT, "pt", CAPTIONSIZE, 10, CLOSESIZEUNIT, "pt", CLOSESIZE, 12,
	CAPTION, myCaption, FIXX, ggPosX, FIXY, ggPosY, CLOSECOLOR, "#ffffff", BGCOLOR, titleColor);
		
//	window.scroll(ggPosX, ggPosY);
}

function show_calendar() {

	
	p_item = arguments[0];	
	
	lang=document.queryform.lang.value;
	
	if (lang=="HBig5" || lang=="PBig5"){
		MM = ["る", "る", "る", "る", "きる", "せる",
		"る", "る", "る", "る", "る", "る"];
		WW=["ら", "", "", "", "", "き", "せ"];
		myFooter="<font size=2 color=red>ら</font><font size=2 color=black>翠猭﹚安戳</font>";
	}

    if (lang=="HGB" || lang=="PGB"){
		MM = ["一月", "二月", "三月", "四月", "五月", "六月",
		"七月", "八月", "九月", "十月", "十一月", "十二月"];
		WW=["日", "一", "二", "三", "四", "五", "六"];
		myFooter="<font size=2 color=red>红日</font><font size=2 color=black>为香港法定假期</font>";
	}	
	
	if (lang=="HEng" || lang=="PEng"){

		MM = ["January", "February", "March", "April", "May", "June",
		"July", "August", "September", "October", "November", "December"];
		WW=["S", "M", "T", "W", "T", "F", "S"];
		myFooter="<font size=1 color=black>H. K. public holidays are colored in </font><font size=1 color=red>red</font>";	}
	
	if (lang=="PBig5") {
	
	  	if (p_item=="queryform.D1")
			myCaption="祘ら戳";
	  	else 	
			myCaption="祘ら戳";
	} 
	if (lang=="PGB"){
		if (p_item=="queryform.D1")
			myCaption="去程日期";
		else 	
			myCaption="回程日期";
	}
	if (lang=="PEng"){
	    if (p_item=="queryform.D1")
			myCaption="Depture date";
		else 	
			myCaption="Return date";
	}
	if (lang=="HBig5"){ 
		if (p_item=="queryform.D1")
			myCaption="ら戳";
		else 	
			myCaption="癶ら戳";
	}
	if (lang== "HGB"){ 
		if (p_item=="queryform.D1")
			myCaption="入住日期";
		else 	
			myCaption="退住日期";
	}
	if (lang== "HEng"){
		if (p_item=="queryform.D1")
			myCaption="Check-in date";
		else 	
			myCaption="Check-out date";
	}
		
	if (arguments[1] == null)
		p_month = new String(gNow.getMonth());
	else
		p_month = arguments[1];
	if (arguments[2] == "" || arguments[2] == null)
		p_year = new String(gNow.getFullYear().toString());
	else
		p_year = arguments[2];
	if (arguments[3] == null)
		p_format = "YYYY-MM-DD";
	else
		p_format = arguments[3];

	Build(p_item, p_month, p_year, p_format);
}