document.write("<div id=\"w1\" style=\"position:absolute;top:100;left:200;z-index:100;display:'none'\">");
document.write("<form name=\"calendar_form\">");
document.write("<input type=\"hidden\" name=\"oneyear\" />");
document.write("<input type=\"hidden\" name=\"onemonth\" />");
document.write("<input type=\"hidden\" name=\"field_name\" value=\"\" />");
document.write("</form>");
document.write("<span id=\"calendar\"></span>");
document.write("</div>");

function show_calendar(){
	var id="";
	var id_name="";

	for(var i=0;i<show_calendar.arguments.length;i++){
		if(i==(show_calendar.arguments.length-1))
			id+=show_calendar.arguments[i];
		else
			id+=show_calendar.arguments[i]+"#";

		if(!i)
			id_name=show_calendar.arguments[i];
	}

	var now_day=new Date();
	var now_year=now_day.getFullYear();
	var now_month=now_day.getMonth();

	document.calendar_form.oneyear.value=now_year;

	document.calendar_form.onemonth.value=now_month;
	document.getElementById('w1').style.display='';
	document.calendar_form.field_name.value=id;

	show("");

	document.getElementsByName(id_name)[0].style.position="relative";
	document.getElementById('w1').style.left=document.getElementsByName(id_name)[0].offsetLeft+"px";
	document.getElementById('w1').style.top=(document.getElementsByName(id_name)[0].offsetTop+document.getElementsByName(id_name)[0].offsetHeight+2)+"px";
	cal_hide_overlap();
}

function give_date(d1){
	var year_tmp=document.calendar_form.oneyear.value;
	var month_tmp=(parseInt(document.calendar_form.onemonth.value)+1);
	if(month_tmp<10)
		month_tmp="0"+month_tmp;
	if(d1<10)
		d1="0"+d1;

	var nkname=document.calendar_form.field_name.value;
	var str=nkname.split("#");

	if(str.length==1){
		document.getElementsByName(str[0])[0].value=year_tmp+"/"+month_tmp+"/"+d1;
	}
	if(str.length==3){
		document.getElementsByName(str[0])[0].value=year_tmp;
		document.getElementsByName(str[1])[0].value=month_tmp;
		document.getElementsByName(str[2])[0].value=d1;
	}
	document.getElementById('w1').style.display="none";
	cal_show_overlap();
}

function add0(num_str){
	return (num_str<10) ? "0"+num_str : num_str;
}

function show(Direction){
	var mm=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	var mname=new Array("01月","02月","03月","04月","05月","06月","07月","08月","09月","10月","11月","12月");

	if(Direction=="-"){
		document.calendar_form.onemonth.value=parseInt(document.calendar_form.onemonth.value)-1;
		if(document.calendar_form.onemonth.value==-1){
			document.calendar_form.onemonth.value=11;
			document.calendar_form.oneyear.value=parseInt(document.calendar_form.oneyear.value)-1;
		}
	}
	if(Direction=="+"){
		document.calendar_form.onemonth.value=parseInt(document.calendar_form.onemonth.value)+1;
		if(document.calendar_form.onemonth.value==12){
			document.calendar_form.onemonth.value=0;
			document.calendar_form.oneyear.value=parseInt(document.calendar_form.oneyear.value)+1;
		}
	}
	if(Direction=="--")
		document.calendar_form.oneyear.value=parseInt(document.calendar_form.oneyear.value)-1;
	if(Direction=="++")
		document.calendar_form.oneyear.value=parseInt(document.calendar_form.oneyear.value)+1;
	if(Direction=="#"){
		var now_day=new Date();
		var now_year=now_day.getFullYear();
		var now_month=now_day.getMonth();
		var now_day=now_day.getDate();
		document.calendar_form.onemonth.value=now_month;
		document.calendar_form.oneyear.value=now_year;
		give_date(now_day);
	}

	year=document.calendar_form.oneyear.value;
	month=document.calendar_form.onemonth.value;
	var dayy=new Date(year,month,1);
	var dayz=new Date();
	var week=dayy.getDay();
	var today=dayz.getDate();
	var yyy=dayz.getFullYear();
	var mmm=dayz.getMonth();
	var column=0;
	var HTML_str="";
	var num=1;

	if((year%4==0 && year%100!=0) || year%400==0)
		mm[1]=29;

	HTML_str+="<table border=\"2\" cellspacing=\"0\" cellpadding=\"0\" bordercolor=\"#C0C0C0\" bgcolor=\"#ffffff\">";
	HTML_str+="<tr><td>";
	HTML_str+="<table border=\"0\" width=\"140\" cellpadding=\"0\">";
	HTML_str+="<tr><td>";
	HTML_str+="<table cellSpacing=\"0\" width=\"140\" bgColor=\"#ffffff\" border=\"0\">";
	HTML_str+="<tr height=\"18\">";
	HTML_str+="<td vAlign=\"center\" align=\"left\" width=\"160\" height=\"18\" style=\"font-family: 新細明體; font-size: 9pt; line-height: 18px\" colspan=\"7\">";
	HTML_str+="<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#E0E0E0\">";

	HTML_str+="<tr>";
	HTML_str+="<td width=\"10\" height=\"22\"></td>";
	HTML_str+="<td height=\"22\"><a href=\"javascript:show('-');\"><img height=\"18\" title=\"上一月\" src=\"/images/prev.gif\" width=\"10\" border=\"0\" /></a></td>";
	HTML_str+="<td height=\"22\" align=\"center\" valign=\"bottom\" style=\"font-family: 新細明體; font-size: 12pt;\">"+year+"年 "+mname[month]+"</td>";
	HTML_str+="<td height=\"22\"><a href=\"javascript:show('+');\"><img style=\"cursor:pointer\" height=\"18\" title=\"下一月\" src=\"/images/next.gif\" width=\"10\" border=\"0\" /></a></td>";
	HTML_str+="<td height=\"22\" width=\"18\" align=\"center\"><img style=\"cursor:pointer\" onclick=\"document.getElementById('w1').style.display='none';cal_show_overlap();\" height=\"18\" title=\"關閉\" src=\"/images/close.gif\" width=\"18\" border=\"0\" /></td>";
	HTML_str+="</tr></table></td></tr>";

	HTML_str+="<tr><td colSpan=\"7\"><table border=\"0\"><tr>";
	HTML_str+="<td class=\"SOME\" valign=\"bottom\" align=\"right\" width=\"22\" height=\"22\" style=\"font-family: 新細明體; font-size: 12pt;\"><font color='#FF0000'>日</font></td>";
	HTML_str+="<td class=\"SOME\" valign=\"bottom\" align=\"right\" width=\"22\" height=\"22\" style=\"font-family: 新細明體; font-size: 12pt;\">一</td>";
	HTML_str+="<td class=\"SOME\" valign=\"bottom\" align=\"right\" width=\"22\" height=\"22\" style=\"font-family: 新細明體; font-size: 12pt;\">二</td>";
	HTML_str+="<td class=\"SOME\" valign=\"bottom\" align=\"right\" width=\"22\" height=\"22\" style=\"font-family: 新細明體; font-size: 12pt;\">三</td>";
	HTML_str+="<td class=\"SOME\" valign=\"bottom\" align=\"right\" width=\"22\" height=\"22\" style=\"font-family: 新細明體; font-size: 12pt;\">四</td>";
	HTML_str+="<td class=\"SOME\" valign=\"bottom\" align=\"right\" width=\"22\" height=\"22\" style=\"font-family: 新細明體; font-size: 12pt;\">五</td>";
	HTML_str+="<td class=\"SOME\" valign=\"bottom\" align=\"right\" width=\"22\" height=\"22\" style=\"font-family: 新細明體; font-size: 12pt;\"><font color='#FF0000'>六</font></td>";
	HTML_str+="</tr></table></td></tr>";

	HTML_str+="<tr><td colSpan=\"7\" height=\"1\"></td></tr>";

	HTML_str+="<tr align=center>";
	for(var i=0;i<week;i++){
		HTML_str+="<td height=\"20\" bgcolor='#f0f3f3'>&nbsp;";
		column++;
	}
	for(var i=1;i<=mm[month];i++){
		var showmsg=year+"/"+add0((parseInt(month)+1))+"/"+add0(i);
		var n1=year+add0((parseInt(month)+1))+add0(i);
		var n2=yyy+add0((parseInt(mmm)+1))+add0(today);
		if(i==today && mmm==month && yyy==year)
			HTML_str+="<td height=\"20\" bgcolor='orange' style=\"font-family: 新細明體; font-size: 12pt;\"><font color=\"black\">"+i+"</font>";
		else if((Date.parse((parseInt(month)+1) + "/" + i + "/" + year)).valueOf() < (new Date()).valueOf())
			HTML_str+="<td height=\"20\" bgcolor='#f0f3f3' style=\"font-family: 新細明體; font-size: 12pt;\"><font color=\"#C0C0C0\">"+i+"</font>";
		else if(column==0 || column==6)
			HTML_str+="<td height=\"20\" bgcolor='#f0f3f3' style=\"cursor:pointer;font-family: 新細明體; font-size: 12pt;\" onclick='give_date("+i+")' onmouseover=\"this.style.backgroundColor='yellow';\" onmouseout=\"this.style.backgroundColor='#f0f3f3';\"><font color=\"red\">"+i+"</font>";
		else
			HTML_str+="<td height=\"20\" bgcolor='#f0f3f3' style=\"cursor:pointer;font-family: 新細明體; font-size: 12pt;\" onclick='give_date("+i+")' onmouseover=\"this.style.backgroundColor='#FFFF00';\" onmouseout=\"this.style.backgroundColor='#f0f3f3';\"><font color=\"black\">"+i+"</font>";
		column++;
		if(column==7 && i<mm[month]){
			column=0;
			HTML_str+="<tr height=\"20\" align=center>";
		}
	}
	while(column<7 && column>0){
		HTML_str+="<td height=\"20\" bgcolor='#f0f3f3'>&nbsp;";
		column++;
	}
	HTML_str+="</table>";
	HTML_str+="</td></tr></table></td></tr></table>";

	document.getElementById("calendar").innerHTML=HTML_str;
}

//avoid overlap with dropdownlist
var cal_hide_selects = new Array();
function cal_fetch_object_posleft(elm)
{
  var left = elm.offsetLeft;
  while((elm = elm.offsetParent) != null)
  {
    left += elm.offsetLeft;
  }
  return left;
}
function cal_fetch_object_postop(elm)
{
  var top = elm.offsetTop;
  while((elm = elm.offsetParent) != null)
  {
    top += elm.offsetTop;
  }
  return top;
}
function cal_check_overlap(selectobj, m)
{
  s = new Array();
  s['L'] = cal_fetch_object_posleft(selectobj);
  s['T'] = cal_fetch_object_postop(selectobj);
  s['R'] = s['L'] + selectobj.offsetWidth;
  s['B'] = s['T'] + selectobj.offsetHeight;

  if (s['L'] >= m['L'] && s['L'] <= m['R'] && ((s['T'] >= m['T'] && s['T'] <= m['B']) || (s['B'] >= m['T'] && s['B'] <= m['B']))) { return true; }
  else if (s['R'] >= m['L'] && s['R'] <= m['R'] && ((s['T'] >= m['T'] && s['T'] <= m['B']) || (s['B'] >= m['T'] && s['B'] <= m['B']))) { return true; }
  else if (s['B'] >= m['T'] && s['T'] <= m['B'] && ((s['L'] >= m['L'] && s['L'] <= m['R']) || (s['R'] >= m['R'] && s['R'] <= m['R']))) { return true; }
  else if (m['B'] >= s['T'] && m['T'] <= s['B'] && ((m['L'] >= s['L'] && m['L'] <= s['R']) || (m['R'] >= s['R'] && m['R'] <= s['R']))) { return true; }
  else { return false; }
}
function cal_hide_overlap()
{
  cal_hide_selects = new Array();
  var obj = document.getElementById('w1');
	var leftpx = cal_fetch_object_posleft(obj);
	var toppx = cal_fetch_object_postop(obj);
	menuarea = {
		"L" : leftpx,
		"T" : toppx,
		"R" : leftpx + obj.offsetWidth,
		"B" : toppx + obj.offsetHeight
	};
  var selects = document.getElementsByTagName("select");
  for (var i = 0; i < selects.length; i++)
  {
    if (cal_check_overlap(selects[i], menuarea)) //&& selects[i].style.visibility != 'hidden')
    {
      cal_hide_selects.push(selects[i]);
      selects[i].style.visibility = 'hidden';
    }
  }
}
function cal_show_overlap()
{
try
{
  for (var sel in cal_hide_selects)
  {
    cal_hide_selects[sel].style.visibility = 'visible';
  }
}
catch (e)
{
}
  cal_hide_selects = new Array();
}
