var days = new makeArray(6, 7);
var cur_date = new Date();
var sel_date = new Date(cur_date.getYear(), cur_date.getMonth(), cur_date.getDate());
var sel_row = -1;
var sel_col = -1;
var today = new Date(cur_date.getYear(), cur_date.getMonth(), cur_date.getDate());

function setSelectByValue(selectfield,value)
{
  if(value == null || value.length == 0 ) 
     return;
  for(var i=0;i<selectfield.length;i++)
  {
     if(selectfield.options[i].value==value)
     {
        selectfield.selectedIndex=i;
        break;
     }
  }
} 

function makeArray(m, n)
{
  this.length = m;
  for(var i = 0; i < m; i++) this[i] = new Array(n);
  return this;
}

function clear_calendar()
{
  for(rows=0; rows<6; rows++)
    for(cols=0; cols<7; cols++) {
      id = "C" + rows.toString() + cols.toString();
      document.all(id).innerText = "　";
      document.all(id).borderColor = "#EBEBEB";
      document.all(id).borderColorLight = "#C0C0C0";
      document.all(id).borderColorDark = "";
      document.all(id).bgColor = "";
      document.all(id).style.color = "#404040";
      document.all(id).style.textAlign = "center";
      days[rows][cols] = false;
    }
}

function setdate_Selected(row, col, flag)
{
  s = "C" + row.toString() + col.toString();
  if(flag) {
    document.all(s).style.color = "#FF3366";
    document.all(s).borderColorLight = "#808080";
    document.all(s).borderColorDark = "#808080"
    document.all(s).bgColor = "#F8F8F8"
    // document.all(s).bgColor = "#F80000"
  } else {
    document.all(s).style.color = "";
    document.all(s).borderColorLight = "#C0C0C0";
    document.all(s).borderColorDark = ""
    document.all(s).bgColor = "";
  }
}

function formatDate(datevalue)
{
  var year = ""+datevalue.getFullYear();
  var month = ""+(datevalue.getMonth()+1);
  var day = ""+datevalue.getDate();

  if(month.length == 1 ) month="0"+month;
  if(day.length == 1 ) day="0"+day;

  return year+"-"+month+"-"+day ;
}

function set_selected(row, col)
{
  if(sel_row != -1 && sel_col != -1) setdate_Selected(sel_row, sel_col, false);
  sel_row = row;
  sel_col = col;
  setdate_Selected(sel_row, sel_col, true);
  
  day = parseInt(document.all("C" + row.toString() + col.toString()).innerText);
  sel_date.setFullYear(document.all("year").value,document.all("month").value, day);
  
  try {
    date_clicked(formatDate(sel_date))
  }
  catch(e)
  {
    alert("date_clicked not defined");
  }
  /*
  document.all.sel_date.value = ;
  // document.all.checkindate.value = document.all.sel_date.value ;  
  datefield.value = document.all.sel_date.value ;  
  */

}

function calendar()
{
  clear_calendar();

  var thedate = new Date(cur_date.getYear(), cur_date.getMonth(), 1);
  var enddate = new Date(cur_date.getYear(), cur_date.getMonth() + 1, 1);

  rows = 0;
  cols = thedate.getDay();
  day = 1;
  while(thedate < enddate) {
    document.all("C" + rows.toString() + cols.toString()).innerText = day;
    if(thedate.getTime() == sel_date.getTime())  {
      if(sel_row == -1 || sel_col == -1) {
        sel_row = rows;
        sel_col = cols;
      }
      setdate_Selected(rows, cols, true);
      days[rows][cols] = true;
    } else
      if(thedate < today) {
        document.all("C" + rows.toString() + cols.toString()).style.color = "#C0C0C0";
      } else
        days[rows][cols] = true;

    cols++;
    if(cols > 6) {
      cols = 0;
      rows++;
    }
    thedate.setDate(++day);
  }
}

function date_change()
{
  cur_date.setFullYear(document.all.year.value, document.all.month.value, 1);
  
  calendar();
}

function draw_calendar()
{
      document.write("<table width=156 cellspacing=0 cellpadding=0 style='font-size:12px' align='center'><thead>");
      document.write("<tr><td height=30 colspan=7 align='center' valign='middle' id='calendar_form_select'>");
      document.write("<span><select name=year onchange=date_change()>");
      var d = new Date();
      var startyear = new Date().getYear();
      var endyear = startyear+11;
      for(i = startyear ; i <= endyear; i++) {
        s = "<option value='" + i.toString()+"'";
        if(d.getYear() == i) s = s + " selected";
        document.write(s + ">" + i.toString() + "年</option>");
      }
      document.write("</select></span><span><select name=month class=sel onchange=date_change()>");
      for(i = 0; i < 12; i++) {
        s = "<option value='" + i.toString()+"'";
        if(d.getMonth() == i) s = s + " selected";
        document.write(s + ">" + (i + 1) + "月</option>");
      }
      document.write("</select>	</span></td></tr><tr><th id=W0 align='center' style='color:#ed1c24'>日</th><th id=W1  align='center'>一</th><th id=W2  align='center'>二</th>");
      document.write("<th id=W3  align='center'>三</th><th id=W4  align='center'>四</th><th id=W5  align='center'>五</th><th id=W6  align='center' style='color:#ed1c24'>六</th></tr></thead><tbody>");

      for(i = 0; i < 6; i++) 
      {
        s = "<tr>";
        for(j = 0; j < 7; j++)
          s = s + "<td id=C" + i.toString() + j.toString() + ">　</td>";
        document.write(s + "</tr>");
      }
      document.write("</tbody></table>");
  }


function initialize_calendar()
{
  for(cols = 0; cols < 7; cols++) {
    document.all("W" + cols.toString()).borderColor = "#ed1c24";
    document.all("W" + cols.toString()).borderColorLight = "#ed1c24";
  }
  
  /*
  if(document.all.initsel_date.value != "") {
    sel_date.setTime(Date.parse(document.all.initsel_date.value));
    cur_date.setTime(sel_date.getTime());
    
    setSelectByValue(document.all.year,sel_date.getYear());
    setSelectByValue(document.all.month,sel_date.getMonth());
    
  } 
  else
  {
    document.all.sel_date.value = sel_date.getYear()+"-"+(sel_date.getMonth() + 1) + "-"  + sel_date.getDate();
  }  
  document.all.sel_date.value = sel_date.getYear()+"-"+(sel_date.getMonth() + 1) + "-"  + sel_date.getDate();
  */
  
  calendar();
  
}

function get_srcElement()
{
  e = window.event.srcElement;
 // while (e.id == "") {
   // if(e.tagName == "BODY") break;
  //  e = e.parentElement
 // }
  return e;
}

function document_onmouseover()
{
  var e = get_srcElement();
  if(e.id.charAt(0) == "C") {
    row = parseInt(e.id.charAt(1));
    col = parseInt(e.id.charAt(2));
	
	if(days[row][col]) {
		document.all(e.id).style.cursor = "hand";		
		if (!(sel_row == row && sel_col == col))
		{
			document.all(e.id).bgColor = "#6e6e6e";
			document.all(e.id).style.color = "#FFFFFF";
		}
	}
    
  }
}

function document_onmouseout()
{
  var e = get_srcElement();
  if(e.id.charAt(0) == "C") {
    row = parseInt(e.id.charAt(1));
    col = parseInt(e.id.charAt(2));
	
	if(days[row][col]) {
		document.all(e.id).style.cursor = "";
		if (!(sel_row == row && sel_col == col))
		{
			document.all(e.id).bgColor = "";
			document.all(e.id).style.color = "#404040";
		}
	}
    
  }
}

function document_click()
{
  var e = get_srcElement();
  if(e.id.charAt(0) == "C") {
    row = parseInt(e.id.charAt(1));
    col = parseInt(e.id.charAt(2));
    if(days[row][col]) set_selected(row, col);
  }
}

document.onclick = document_click;
document.onmouseover = document_onmouseover;
document.onmouseout = document_onmouseout;


