
document.writeln("<div id='calenderdiv' style>日历加载中...</div>");
var press_tag;
function changecal(action,year,month)
{      
      var strcal;
      switch(action)
      {      
      case "nextmonth":
            if(month==11)
            {
                  month = 1;
                  year = year*1 + 1;
            }else{
                  month = month*1 + 2;
            }
            strcal = "<td align='right'><span onmouseover=\"this.className='arrow_over'\" onmouseout=\"this.className='arrow_out'\" class='arrow_out'  onclick='calender(" + year + "," + month +")' title='下一个月' style='cursor:hand;'>> </span></td>";
            break;
      case "premonth":
            if(month==0)
            {
                  month = 12;
                  year = year*1 - 1;
            }
            strcal = "<td align='left'><span onmouseover=\"this.className='arrow_over'\" onmouseout=\"this.className='arrow_out'\" class='arrow_out' onclick='calender(" + year + "," + month +")' title='上一个月' style='cursor:hand;'> <</span></td>";
            break;
      case "nextyear":
            year = year*1 + 1;
            month = month*1 + 1;
            strcal = "<span onmouseover=\"this.className='arrow_over'\" onmouseout=\"this.className='arrow_out'\" class='arrow_out' onclick='calender(" + year + "," + month +")' title='下一年' style='cursor:hand;'>>></span>";
            break;
      case "preyear": 
            year = year*1 - 1;
            month = month*1 + 1;
            strcal = "<span onmouseover=\"this.className='arrow_over'\" onmouseout=\"this.className='arrow_out'\" class='arrow_out' onclick='calender(" + year + "," + month +")' title='上一年' style='cursor:hand;'><<</span>";
            break;
      default:;
      }
      strcal = " " + strcal + " ";
      return(strcal);
}
 
function calender(cyear,cmonth)
{
      
      var d,d_date,d_day,d_month;
      //定义每月天数数组
      var monthdates = ["31","28","31","30","31","30","31","31","30","31","30","31"]      
      d = new Date();
      d_year = d.getYear();      //获取年份
      //判断闰月，把monthdates的二月改成29
      if (((d_year % 4 == 0) && (d_year % 100 != 0)) || (d_year % 400 == 0)) monthdates[1] = "29";
      
      if ((cyear != "" ) || (cmonth != ""))
      {
            //如果用户选择了月份和年份，则当前的时间改为用户设定
            d.setYear(cyear);
            d.setMonth(cmonth-1);            
            d.setDate(1);
      }
      d_month = d.getMonth();      //获取当前是第几个月
      d_year = d.getYear();      //获取年份
      d_date = d.getDate();      //获取日期
      
      if(d_year<2000){d_year = d_year + 1900}      
      var str;
      str = "<td colspan='7'><table width='100%' cellspacing='0' cellpadding='0'><tr>";
      str += changecal("premonth",d_year,d_month) 
	  str += "<td align='center'>"
      str += d_year + "年" + (d_month*1+1) + "月"
	 str += "</td>"
      str += changecal("nextmonth",d_year,d_month) 
      str += "</tr></table></td></tr><tr bgcolor='#166E9E' style='color: #ffffff;'>";
      str += "<td align='center'>日</td><td align='center'>一</td><td align='center'>二</td><td align='center'>三</td><td align='center'>四</td><td align='center'>五</td><td align='center'>六</td></tr>";
      str += "<tr bgcolor='#f6f6f6' style='color: #333333;'>";
      var firstday,lastday,totalcounts,firstspace,lastspace,monthdays;
      monthdays = monthdates[d.getMonth()];
      
      d.setDate(1);
      
      firstday = d.getDay();      
      
      firstspace = firstday;
      
      d.setDate(monthdays);
      
      lastday = d.getDay();
      
      lastspace = 6 - lastday;
      
      totalcounts = firstspace*1 + monthdays*1 + lastspace*1;      
            
      
      var count,flag,f_space,l_space;
      flag = 0;
      for(count=1;count<=totalcounts;count++)
      {
            if(flag==0)
            {
                  if(firstspace!=0)
                  {      
                        for(f_space=1;f_space<=firstspace;f_space++)
                        {      
                              str += "<td> </td>";
                              if(f_space!= firstspace) count++;      
                        }
                        flag = 1;
                        continue;
                  }
            }            
            
            if((count-firstspace)<=monthdays)
            {
                  //输出月份中的所有天数            
                  curday = d_year+","+(d_month*1+1)+","+(count - firstspace)+"|"
                  linkday = d_year+","+(d_month*1+1)+","+(count - firstspace)
                  var today = new Date();
                  if( (d_year == today.getYear()) && (d_month == today.getMonth()) && ((count-firstspace) == today.getDate()) )
                  {
 
                        str += "<td bgcolor='#f6f6f6' style='color: #0060ff;'>" + (count - firstspace) + "</td>";
                  }else{

                        str += "<td>" + (count - firstspace) + "</td>";
                  }
                  
                  if(count%7==0)
                  {
                        if(count<totalcounts)
                        {
                              str += "</tr><tr bgcolor='#f6f6f6' style='color: #333333;'>";
                        }else{
                              str += "</tr>";
                        }
                  }
            }else{
                  for(l_space=1;l_space<=lastspace;l_space++)
                  {
                        str += "<td> </td>";
                        if(l_space!= lastspace) count++;      
                  }
                  continue;
            }
      }
      str += "<tr><td colspan='7' style='text-indent:9px;'></td></tr></table>"
      document.getElementById("calenderdiv").innerHTML = "<table width='100%' bgcolor='#FFFFFF' cellspacing='1' cellpadding='2' border='0' style='font-face: Verdana; font-size: 11px; text-align: center;'><tr bgcolor='#ffffff' style='color: #333333;'>" + str + "</tr></table>";
}
//调用函数
calender("","")

