function getHTML(id,year,month)
{
  str = "<HTML><HEAD><TITLE>Calendar</TITLE>";
  str += "<STYLE type=\"text/css\"><!--";
  str += "td,th,input,select{font-size:12px}";
  str += "a{color:black;text-decoration:none}";
  str += "a:hover{color:red;text-decoration:underline}";
  str += "a.dir{color:blue;text-decoration:none}";
  str += "a.dir:hover{color:red;text-decoration:none}";
  str += "a.week6{color:#6666FF;text-decoration:none}";
  str += "a.week6:hover{color:red;text-decoration:underline}";
  str += "a.week7{color:#FF6666;text-decoration:none}";
  str += "a.week7:hover{color:red;text-decoration:underline}";
  str += "--></STYLE></HEAD>";
  str += "<BODY>";
  str += getBODY(id,year,month);
  str += "</BODY></HTML>";
  return str;
}

function getBODY(id,year,month)
{
  str = "<table border ALIGN=center><TR>";
  str += "<TD>[<A class=\"dir\" href=\"javascript:document.close();document.write(opener.getHTML('"+id+"',"+(year-1)+","+month+"))\"><<</A>]</TD>";
  str += "<TD>[<A class=\"dir\" href=\"javascript:document.close();document.write(opener.getHTML('"+id+"',"+year+","+((month==1)?12:(month-1))+"))\"><</A>]</TD>";
  str += "<TD>[<A class=\"dir\" href=\"javascript:opener.document.forms[0]."+id+".value='';self.close()\">Clear</A>]</TD>";
  str += "<TD>[<A class=\"dir\" href=\"javascript:document.close();document.write(opener.getHTML('"+id+"',"+year+","+((month==12)?1:(month+1))+"))\">></A>]</TD>";
  str += "<TD>[<A class=\"dir\" href=\"javascript:document.close();document.write(opener.getHTML('"+id+"',"+(year+1)+","+month+"))\">>></A>]</TD>";
  str += "</TR></table><HR>";
  str += getCalendar(id,year,month);
  return str;
}

function getCalendar(id,year,month)
{
  monthDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  if ( ( (year%4==0) && (year%100!=0) ) || (year%400==0) ) monthDays[1] = 29;

  nDays = monthDays[month-1];
  
  today.setYear(year);
  today.setMonth(month-1);
  today.setDate(1);
  startDay = today.getDay();
  
  str = "<table border ALIGN=center><TR><TH colSpan=7 height=18><FONT color=#336699>";
  str += year+" / "+month+" </FONT></TH></TR>";
  str += "<TR bgcolor=#CCCCCC><TH>1</TH><TH>2</TH><TH>3</TH><TH>4</TH><TH>5</TH><TH>6</TH><TH>7</TH></TR>";
  str += "<TR>";
  column = 0;
  for (i=0; i<startDay; i++)
  {
    str += "<TD></TD>";
    column++;
  }
  for (i=1; i<=nDays; i++)
  {
    if (column == 0 ) {
      str += "<TD><A href=\"javascript:opener.document.forms[1]."+id+".value='"+year+"-"+patch(month)+"-"+patch(i)+"';self.close()\" class=\"week7\">"+i+"</A></TD>";
    } else if (column == 6 ) {
      str += "<TD><A href=\"javascript:opener.document.forms[1]."+id+".value='"+year+"-"+patch(month)+"-"+patch(i)+"';self.close()\" class=\"week6\">"+i+"</A></TD>";
    } else {
      str += "<TD><A href=\"javascript:opener.document.forms[1]."+id+".value='"+year+"-"+patch(month)+"-"+patch(i)+"';self.close()\">"+i+"</A></TD>";
    }
    column++;
    if (column == 7) {
      str += "</TR><TR>"; 
      column = 0;
    }
  }
  str += "</table>";
  return str;
}

function patch(n)
{
  return (n-10<0) ? ("0"+n) : (""+n);
}

function setDate(id)
{
  try { newwin.close(); } catch(Exception) {}
  td = document.forms[1].all[id].value;
  today = new Date();
  if (td == "") {
    year = today.getYear();
    if (year<100) year += 1900;
    month = today.getMonth()+1;
  } else {
    year = td.substring(0,4)-0;
    month = td.substring(5,7)-0;
	//month=td.substring(5,td.lastIndexOf("-"))-0;
  }
  newwin = window.open('','','height=240,width=164,top='+(event.screenY-100)+',left='+(event.screenX-200));
  newwin.document.write(getHTML(id,year,month));
}

