var vn="Microsoft Internet Explorer";
var some;
if (navigator.appName!=vn) {
  some=1900; }
else {
some=0; }

var backcolor, txt_color, bdr_color, txt_decor;
var colwidth="250";

function TTY_overlink(cellobjpt) { 
  backcolor = cellobjpt.style.backgroundColor;
  bdr_color = cellobjpt.style.borderColor;
  txt_color = cellobjpt.style.color;
  txt_decor = cellobjpt.style.textDecoration;
  cellobjpt.style.backgroundColor = "FFFF33";
  cellobjpt.style.borderColor = "000000";
//  cellobjpt.style.color = "FF0033";
//  cellobjpt.style.color = "914637";
//  cellobjpt.style.textDecoration = "Blink";
}

function TTY_out_link(cellobjpt) {
  cellobjpt.style.backgroundColor = backcolor;
  cellobjpt.style.borderColor = bdr_color;
  cellobjpt.style.color = txt_color;
  cellobjpt.style.textDecoration = txt_decor;
}

function MonthDaysArr(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11)
{
  this[0]  = m0;
  this[1]  = m1;
  this[2]  = m2;
  this[3]  = m3;
  this[4]  = m4;
  this[5]  = m5;
  this[6]  = m6;
  this[7]  = m7;
  this[8]  = m8;
  this[9]  = m9;
  this[10] = m10;
  this[11] = m11;
}

function WeekDayNmArr(w0, w1, w2, w3, w4, w5, w6)
{
  this[0]  = w0;
  this[1]  = w1;
  this[2]  = w2;
  this[3]  = w3;
  this[4]  = w4;
  this[5]  = w5;
  this[6]  = w6;
}

var monthDays = new MonthDaysArr(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var mon1stDay = new MonthDaysArr(0,   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0);
var dayNames  = new WeekDayNmArr("星期日","星期一","星期二","星期三","星期四","星期五","星期六");

var dayENames = new WeekDayNmArr("SUN","MON","TUE","WED","THU","FRI","SAT");

var monDayInd = new MonthDaysArr("", "", "", "", "", "", "", "", "", "", "", "");


var nCurrYear = 0;         // School Year
var n1stSepWD = 0;         // 01 Sep, (nCurrYear) --- weekday
var nFeb_Days = 0;         // Feb, (nCurrYear+1)  --- no of days

// ==========================================================================


function SetMon1stDayArr()
{
  var i = 0;

  monthDays[1] = nFeb_Days;
  mon1stDay[8] = n1stSepWD;

  for (i=8; i<11; i++)
    mon1stDay[i+1] = (mon1stDay[i] + monthDays[i]) % 7;

  mon1stDay[0] = (mon1stDay[11] + monthDays[11]) % 7;
  for (i=0; i<7; i++)
    mon1stDay[i+1] = (mon1stDay[i] + monthDays[i]) % 7;

}

//function setMon1stDayArr(nYear)
//{
//  var nY = nYear + 1;
//  var i = 0;
//
//  if (((nY % 4 == 0) && (nY % 100 != 0)) || (nY % 400 == 0)) monthDays[1] = 29;
//
//  mon1stDay[8] = 5;
//  for (i=8; i<11; i++)
//    mon1stDay[i+1] = (mon1stDay[i] + monthDays[i]) % 7;

//  mon1stDay[0] = (mon1stDay[11] + monthDays[11]) % 7;
//  for (i=0; i<7; i++)
//    mon1stDay[i+1] = (mon1stDay[i] + monthDays[i]) % 7;
//
//}

function setCurrentDay(nMonNo)
{
  var tmpDay = new Date();

  return (tmpDay.getMonth()==nMonNo ? tmpDay.getDate() : 0)
}

function SetMonthShown(nYear)
{
// return the calendar month to be shown

  var tmpDay = new Date();
  var nMonthShown = tmpDay.getMonth();

  nMonthShown = ((tmpDay.getYear()==nYear) && (nMonthShown==7) ? 8 : nMonthShown);

  return (nMonthShown)
}

function showMonLayer(nL_n1, nL_n2)
{
  eval('monLayer'+((nL_n1+12) % 12)+'.style.display="none"');
  eval('monLayer'+((nL_n2+12) % 12)+'.style.display=""');
}

function drawMonth(nMonNo, nYear)
{
  var nDays      = monthDays[nMonNo];
  var nStartWDay = mon1stDay[nMonNo];
  var nMonth = nMonNo + 1;
  var nDay   = setCurrentDay(nMonNo);
  var i, column = 0;

  var td_str, t_char = "";

  document.writeln('<div id="monLayer'+nMonNo+'" style="display:none">');
  document.writeln('<table width="100%" border="1" cellspacing="0" cellpadding="2">');

  document.writeln('<tr>');

  td_str = '<td class="row_00" ';
  if (nMonNo != 8)
  {
    td_str += 'style="cursor:hand" ';
    td_str += 'onClick="showMonLayer(' + nMonNo + ', ' + (nMonNo-1) + ')"';
    td_str += '>&nbsp;<<&nbsp;</td>';
  }
  else
  {
    td_str += '>&nbsp;</td>';
  }
  document.writeln(td_str);

  td_str =  (nMonNo < 8 ? nYear+1 : nYear) + "年" + " " + nMonth + "月";
  document.writeln('<td colspan="5" class="row_00" style="cursor:hand"');
  document.writeln(' onMouseOver=""');
  document.writeln(' onMouseOut =""');
  document.writeln('>',td_str);
  document.writeln('</td>');

  td_str = '<td class="row_00" ';
  if (nMonNo != 7)
  {
    td_str += 'style="cursor:hand" ';
    td_str += 'onClick="showMonLayer(' + nMonNo + ', ' + (nMonNo+1) + ')"';
    td_str += '>&nbsp;>>&nbsp;</td>';
  }
  else
  {
    td_str += '>&nbsp;</td>';
  }
  document.writeln(td_str);

  document.writeln('</tr>');

  document.writeln('<tr>');
  if (true)
  {
    document.writeln('<td class="row_01" width="26">日</th>');
    document.writeln('<td class="row_01" width="26">一</th>');
    document.writeln('<td class="row_01" width="26">二</th>');
    document.writeln('<td class="row_01" width="26">三</th>');
    document.writeln('<td class="row_01" width="26">四</th>');
    document.writeln('<td class="row_01" width="26">五</th>');
    document.writeln('<td class="row_01" width="26">六</th>');
  }
  else
  {
    document.writeln('<td class="row_01" width="26">SU</td>');
    document.writeln('<td class="row_01" width="26">MO</td>');
    document.writeln('<td class="row_01" width="26">TU</td>');
    document.writeln('<td class="row_01" width="26">WE</td>');
    document.writeln('<td class="row_01" width="26">TH</td>');
    document.writeln('<td class="row_01" width="26">FR</td>');
    document.writeln('<td class="row_01" width="26">SA</td>');
  }
  document.writeln('</tr>');

  document.writeln('<tr>');
  for (i=0; i<nStartWDay; i++)
  { 
    document.writeln('<td class="e_ddcell" width="26">&nbsp;</td>');
    column++;
  }

  for (i=1; i<=nDays; i++)
  {
    if (column == 7)
    {
      document.writeln('</tr>'); 
      document.writeln('<tr>'); 
      column = 0;
    }
    
    eval('td_str = MonEvt'+nMonth+'('+i+')')
    td_str = (td_str>"" ? "x" : "o") + '_ddcell'

    t_char = show_DayInd(i,nMonth)
    t_char = (t_char=="h" ? "color:#FF0000" : "")
    t_char += (t_char>"" ? ";" : "") + (i == nDay ? "border-color:#0000FF;background-color: #FFFF88" : "")

    td_str = '<td id="calcell'+i+'" class="'+td_str+'" width="26"'
    td_str += (t_char>"" ? ' style="'+t_char+'"' : '')
    td_str += ' onMouseOver="TTY_overlink(this);show_DayEvent('+i+', '+nMonth+')"'
    td_str += ' onMouseOut ="TTY_out_link(this);clr_DayEvent()">'+i+'</td>'

    document.writeln(td_str);
    column++;
  }

  for (i=column; i<7; i++)
  { 
    document.writeln('<td class="e_ddcell" width="26">&nbsp;</td>');
  }
  document.writeln('</tr>');
  document.writeln('</table>');
  document.writeln('</div>');
}

function calendar()
{
  var curDay = new Date();
  var i = 0;

  document.writeln('<table width="100%" border="0" cellspacing="0" cellpadding="2" align="CENTER" bgcolor="FFA928">');
//  document.writeln('<table width="',colwidth,'" border="0" cellspacing="0" cellpadding="2" align="CENTER" bgcolor="FFA928">');
  document.writeln('<tr><td>');

  SetCalendarBase();
  SetMon1stDayArr();
  SetMonDayInd();

  for (i=0; i<12; i++)
  {
    drawMonth(i, nCurrYear);
  }

  i = SetMonthShown(nCurrYear);
  showMonLayer(i, i);
//  eval('monLayer'+curDay.getMonth()+'.style.display=""');

  document.writeln('<table width="100%" border="1" cellspacing="0" cellpadding="2">');
  document.writeln('<form name="DayEvt" onSubmit="0">');
  document.writeln('<tr><td class="row_00">');
//  document.writeln('<textarea name="EvtItem" align="TOP" rows="5" cols="23"></textarea>');
  document.writeln('<textarea name="EvtItem" align="TOP" rows="5" cols="25" style="font-size:13px"></textarea>');
  document.writeln('</td></tr>');
  document.writeln('</form>');
  document.writeln('</table>');

  document.writeln('</td></tr>');
  document.writeln('</table>');
}

function clr_DayEvent()
{
  document.DayEvt.EvtItem.value = ""
}

function show_DayEvent(nDayNo, nMonth)
{
//  eval('document.DayEvt.EvtItem.value = MonEvt'+nMonth+'('+nDayNo+')')

  var ss = show_DayInd(nDayNo,nMonth);
  var nn = parseInt(ss,"16");

//  ss = ((ss>='1') && (ss<='6') ? "(Day "+ss+")\n" : "");

  if (nn>=1 && nn<=10) {  
    ss = "Week " + (nn<6 ? "1" : "2") + "--";
    nn = ((nn-1)%5)+1;
    ss = "(" + ss + dayENames[nn] + ")\n";
  }
  else
  {
    ss = "";
  }
  eval('ss += MonEvt'+nMonth+'('+nDayNo+')');

  document.DayEvt.EvtItem.value = ss
}

function show_DayInd(nDayNo, nMonth)
{
  var mon_str = monDayInd[nMonth-1];
//  return (mon_str.charAt(nDayNo))
  return (monDayInd[nMonth-1].charAt(nDayNo-1))
}


// ========================================================================














// ========================================================================



function todayInfo(value)
{
  if (value == 1)
  {
     document.writeln('<table width="100%" border="0" cellspacing="0" cellpadding="2" align="CENTER" bgcolor="FFA928">');
//     document.writeln('<table width="',colwidth,'" border="0" cellspacing="0" cellpadding="2" align="CENTER" bgcolor="FFA928">');
//     document.writeln('<table width="',colwidth,'" border="0" cellspacing="0" cellpadding="2" bgcolor="FFE5BF">');
     document.writeln('<tr><td>');
     document.writeln('<table width="100%" border="1" cellspacing="0" cellpadding="2" bgcolor="FFE5BF">');
     document.writeln('<tr bgcolor="FFF4EB">');
     document.writeln('<td class="row_00">&nbsp;有用資訊&nbsp;</td></tr>');
     document.writeln('<tr bgcolor="FFF4EB"><td>');
     document.writeln('<table width="100%" border="0" cellspacing="0" cellpadding="0">');
     document.writeln('<form name="info_tab" onSubmit="0">');
     document.writeln('<tr>');
//     document.writeln('<td class="row_02" width="36">&nbsp;日期 :</td>');
     document.writeln('<td class="row_02" width="40">&nbsp;日期 :</td>');
     document.writeln('<td class="row_02"><INPUT TYPE="Text" NAME="date_fd" ALIGN="TOP" size="20" style="font-size:13px"></td>');
//     document.writeln('<td class="row_02"><INPUT TYPE="Text" NAME="date_fd" ALIGN="TOP" size="20"></td>');
//     document.writeln('<td class="row_02"><INPUT TYPE="Text" NAME="date_fd" ALIGN="TOP" size="18"></td>');
     document.writeln('</tr><tr>');
     document.writeln('<td class="row_02">&nbsp;時間 :</td>');
     document.writeln('<td class="row_02"><INPUT TYPE="Text" NAME="time_fd" ALIGN="TOP" size="20" style="font-size:13px"></td>');
//     document.writeln('<td class="row_02"><INPUT TYPE="Text" NAME="time_fd" ALIGN="TOP" size="20"></td>');
//     document.writeln('<td class="row_02"><INPUT TYPE="Text" NAME="time_fd" ALIGN="TOP" size="18"></td>');
     document.writeln('</tr><tr>');
     document.writeln('<td class="row_02">&nbsp;提示 :</td>');
//     document.writeln('<td class="row_02"><textarea name="EvtItem" align="TOP" rows="2" cols="18"></textarea></td>');
     document.writeln('<td class="row_02"><textarea name="EvtItem" align="TOP" rows="2" cols="18" style="font-size:13px"></textarea></td>');
     document.writeln('</tr>');
     document.writeln('</form>');
     document.writeln('</table>');
     document.writeln('</td></tr>');
     document.writeln('</table>');
     document.writeln('</td></tr>');
     document.writeln('</table>');
  }
}

var timerID = null;
var timerRunning = false;

function stopclock()
{
  if (timerRunning)
    clearTimeout(timerID);
  timerRunning = false;
}

function showtime()
{
  var now = new Date()
  var nHours = now.getHours()
  var nMin   = now.getMinutes()
  var nSec   = now.getSeconds()
  var timeValue = ""

  var nMonth = now.getMonth() + 1;
  var nDay   = now.getDate();
  var dateValue = ""

  dateValue =  now.getYear() + some + "年" + " ";
  dateValue += nMonth + "月" + " ";
  dateValue += nDay + "日" + " ";
  dateValue += "(" + dayNames[now.getDay()] + ")";


  if (nHours >=  1 && nHours <  4)  {timeValue = ("凌晨")}
  if (nHours >=  4 && nHours <  6)  {timeValue = ("清晨")}
  if (nHours >=  6 && nHours <  9)  {timeValue = ("早上")}
  if (nHours >=  9 && nHours < 12)  {timeValue = ("上午")}
  if (nHours >= 12 && nHours < 13)  {timeValue = ("中午")}
  if (nHours >= 13 && nHours < 18)  {timeValue = ("下午")}
  if (nHours >= 18 && nHours < 19)  {timeValue = ("傍晚")}
  if (nHours >= 19 && nHours < 24)  {timeValue = ("晚上")}
  if (nHours < 1)                   {timeValue = ("午夜")}

  timeValue +=  ((nHours > 12) ? nHours - 12 : nHours)
  timeValue += ((nMin < 10) ? ":0" : ":") + nMin
  timeValue += ((nSec < 10) ? ":0" : ":") + nSec
  document.info_tab.time_fd.value = timeValue
  document.info_tab.date_fd.value = dateValue
  eval('document.info_tab.EvtItem.value = MonEvt'+nMonth+'('+nDay+')')

  timerID = setTimeout("showtime()",1000)
  timerRunning = true
}

function startclock()
{
  stopclock();
  showtime();
}
