// JavaScript Document

var months = new Array("一", "二", "三","四", "五", "六", "七", "八", "九","十", "十一", "十二");
var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31,30, 31, 30, 31);
var days = new Array("日","一", "二", "三","四", "五", "六");
var classTemp;
var calendarHover="calendarHover";
var btoday=new getToday();
var byear=btoday.year;
var bmonth=btoday.month;
var newCal;

function getDays(bmonth, byear) 
{ if (1 == bmonth)
     return ((0 == byear % 4) && (0 != (byear % 100))) ||(0 == byear % 400) ? 29 : 28;
  else
     return daysInMonth[bmonth];
}

function getToday() 
{  this.now = new Date();
   this.year = this.now.getFullYear();
   this.month = this.now.getMonth();
   this.day = this.now.getDate();
}

function Calendar() 
{  newCal = new Date(byear,bmonth,1); 
   btoday = new getToday();   
   var bday = -1;   
   var startDay = newCal.getDay(); 
   var endDay=getDays(newCal.getMonth(), newCal.getFullYear());
   var daily = 0;   
   if ((btoday.year == newCal.getFullYear()) &&(btoday.month == newCal.getMonth()))
      bday = btoday.day;
   var caltable = document.all.caltable.tBodies.calendar;  
   var intDaysInMonth =getDays(newCal.getMonth(), newCal.getFullYear()); 
   
   for (var intWeek = 0; intWeek < caltable.rows.length;intWeek++)
     for (var intDay = 0;intDay < caltable.rows[intWeek].cells.length;intDay++)
     {
      var cell = caltable.rows[intWeek].cells[intDay];   
   
      var montemp=(newCal.getMonth()+1)<10?("0"+(newCal.getMonth()+1)):(newCal.getMonth()+1);         
      if ((intDay == startDay) && (0 == daily)){ daily = 1;}
      var daytemp=daily<10?("0"+daily):(daily);
      var d="<"+newCal.getFullYear()+"-"+montemp+"-"+daytemp+">";
       if(bday==daily)
      cell.className="calendarNow";
      else if(diarydays.indexOf(d)!=-1)
         cell.className="calendarLink";
		
      else if(intDay==6)
      cell.className = "calendarDaySat";
      else if (intDay==0)
      cell.className ="calendarDaySun";
      else
      cell.className="calendarTd";
   
      if ((daily > 0) && (daily <= intDaysInMonth))  
      {  cell.innerText = daily;
         daily++;
      }
      else
        cell.innerText = "";
    } 
  document.all.byear.value=byear;
  document.all.bmonth.value=bmonth+1;
}

function subMonth()
{
  if ((bmonth-1)<0)
  {
     bmonth=11;
  byear=byear-1;
  }
  else
  { 
    bmonth=bmonth-1;
  }
  Calendar();
}
function addMonth()
{
  if((bmonth+1)>11)
  {
    bmonth=0;
    byear=byear+1;
  }
  else
  { 
    bmonth=bmonth+1;
  }
  Calendar();
}

function getDiary() 
{
  var mon=(newCal.getMonth()+1)<10?("0"+(newCal.getMonth()+1)):(newCal.getMonth()+1);
  var bday=event.srcElement.innerText<10?("0"+event.srcElement.innerText):(event.srcElement.innerText);
  var d="<"+newCal.getFullYear()+"-"+mon+"-"+bday+">";
  if ("TD" == event.srcElement.tagName)
  if (("" != event.srcElement.innerText)&&(diarydays.indexOf(d)!=-1))
  {
   diary.location="show.php?ttt="+newCal.getFullYear()+"-"+mon+"-"+bday;
   window.open(diary.location,"_blank");
  }
}

function setDate() 
{
  if (document.all.bmonth.value<1||document.all.bmonth.value>12)
  {
    alert("月的有效范围在1-12之间!");
 return;
  }
  byear=Math.ceil(document.all.byear.value);
  bmonth=Math.ceil(document.all.bmonth.value-1);
  Calendar();
}
