﻿/*
*   修改记录：
*   Jamber,2008-6-27,修改样式，并去掉日历table里的Author属性；
*   增加returnToday函数，用来直接返回今天的日期
*
*
*
*
*
*/
   var today=new Date();  //define a Date object to get current time
   var desTxtId;          //the very input type=text element to get date
   var cal;               //the calendar interface 
   var tbl;
   var tblchild;
   var obj;
   var counter = 0;
   var browserName=navigator.appName;
   var isIE=browserName.indexOf("Microsoft")!=-1?true:false;
   var ctoday = today;
   var tonum1 = ctoday.getDate();
   var tonum2 = ctoday.setDate(1);
   var tonum3 = ctoday.getDay();
   var tonum = tonum1 + tonum3;
   function montharr(m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11)  //define an object includes month days 
   {
      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 fillcalendar()  // 'fill' data into the calendar
   {
  var monthDays=new montharr(31,28,31,30,31,30,31,31,30,31,30,31);
  var monthNames=new Array("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月");
  year=today.getFullYear();
  if(((year%4==0)&&(year%100!=0))||(year%400==0))monthDays[1]=29;
  nDays=monthDays[today.getMonth()];
  firstDay=today;
  firstDay.setDate(1);
  startDay=firstDay.getDay();
  column=0;
  tblchild=tbl.firstChild;
  tblchild.childNodes[1].childNodes[2].innerHTML=year+"年"+monthNames[today.getMonth()]
  for(var i=0;i<startDay;i++){tblchild.childNodes[3].childNodes[i].innerHTML="&nbsp;";column++;}
  j=3;
  var txt="";
  for(var i=1;i<=nDays;i++)
  {
          tblchild.childNodes[j].childNodes[column].innerHTML=i;
  column++;
  if(column==7){j=j+1;column=0;}
  }
  if(column<7&&column!=0)
     for(var i=column;i<7;i++)tblchild.childNodes[j].childNodes[i].innerHTML="&nbsp;";
   }
   
   function monthadd()    
   {
       clearCalendar();
   today.setMonth((today.getMonth()+1)%12);
   if(today.getMonth()==0)today.setFullYear(today.getFullYear()+1);
   fillcalendar();
   }
   
   function monthsub()
   {
       clearCalendar();
   today.setMonth((today.getMonth()+11)%12);
   if(today.getMonth()==11)today.setFullYear(today.getFullYear()-1);
   fillcalendar();
   }
   
  function goToday()
  {
    var today=new Date();
    changeyear(today.getFullYear());
    changemonth(today.getMonth() + 1);
  }
  
  
   function changeyear(yearValue)
   {
if(1900>yearValue||yearValue>2999){alert("年份范围有误！");return;}
today.setFullYear(yearValue);
clearCalendar();
fillcalendar();
   }
   
   function changemonth(monthValue)
   {
today.setMonth(monthValue-1);
clearCalendar();
fillcalendar();
   }
   
   function returndate(vday)
   {
       if(vday=="&nbsp;"){hidecal();return;}
       if(vday!="")
       { 
            month=today.getMonth()+1;
            year=today.getFullYear();
            desTxtId.value=year+"-"+month+"-"+vday;
       }
       else
            desTxtId.value=""
       hidecal();
   }
   
   //点击‘今天’时，直接返回值，如果需要转到今天请使用goToday
   function returnToday()
   {
        var now=new Date();
        month=now.getMonth()+1;
        year=now.getFullYear();
        day=now.getDate();
        desTxtId.value = year + "-" + month + "-" + day;
        hidecal();
   }
   
   
   function clearCalendar()
   {
  for(var i=3;i<tblchild.childNodes.length;i++)
     for(var j=0;j<tblchild.childNodes[3].childNodes.length;j++)
     tblchild.childNodes[i].childNodes[j].innerHTML="";
   }

  function buildtbl()
  {
        document.write("<div id='Calendar' style=\"background-color:#eee;display:none;position:absolute;width:180px;border:solid #5589B0 1px;padding:2px;z-index:100;\">");
        document.write("<table cellspacing='0' cellpadding='0' id='calTbl' width='180' align='center' border='0' style=\"cursor:hand;\">");
        document.write("<tr style='height:24px;background-image:url(../common/imgs/bg/qb_bg.gif);'>");
        document.write("<td  align='left' colspan='4' valign='middle'><a href='javascript:void(null)' onclick='returnToday();'><font  color='blue'>今天</font></a></th>");
        
        document.write("<td  align='left' colspan='2'><a style='TEXT-DECORATION:none;' href='javascript:void(null)' onclick='returndate(\"\")'' title='清空输入框中的内容'><font color='#ff0066'>清空</font></a></td>"); 
        document.write("<td  align='center'><img src = '../common/imgs/popwin/stop.gif' title='关闭该窗口' onclick='hidecal()'width=15 height=15 /></td>");
        document.write("</tr><tr height='20' bgcolor='#eef2f7'>");
        document.write("<td  align='center'><a  href='javascript:void(null)' onclick='changeyear(today.getFullYear()-1)' title='前一年'><font  face='Webdings' color='#C8C400'>7</font></a></th>");
        document.write("<td  align='center'><a  href='javascript:void(null)'onclick='monthsub()' title='前一月'><font  face='Webdings' color='#C8C400'>3</font></a></th>");
        document.write("<td  colspan='3' align='center' style=' font-size:9pt;font-weight:;color:black'>&nbsp;</th>");
        document.write("<td  align='center'><a href='javascript:void(null)' onclick='monthadd()' title='下一月'><font  face='Webdings' color='#C8C400'>4</font></a></th>");
        document.write("<td  align='center'><a href='javascript:void(null)' onclick='changeyear(today.getFullYear()+1)' title='下一年'><font  face='Webdings' color='#C8C400'>8</font></a></th>");
        document.write("</tr><tr height='20' bgcolor='#D6DDE9'>");
        document.write("<td  align='center' style='font-size:9pt;color:darkgreen;font-weight:'>日</th>");
        document.write("<td  align='center' style='font-size:9pt;color:darkgreen;font-weight:'>一</th>");
        document.write("<td  align='center' style='font-size:9pt;color:darkgreen;font-weight:'>二</th>");
        document.write("<td  align='center' style='font-size:9pt;color:darkgreen;font-weight:'>三</th>");
        document.write("<td  align='center' style='font-size:9pt;color:darkgreen;font-weight:'>四</th>");
        document.write("<td  align='center' style='font-size:9pt;color:darkgreen;font-weight:'>五</th>");
        document.write("<td  align='center' style='font-size:9pt;color:darkgreen;font-weight:'>六</th>");
        document.write("</tr>");
        for(var i=0;i<6;i++)
        {
            document.write("<tr height='18'>");
            for(var j=0;j<7;j++)
            {
              
                      if(j==0)varStyle="color:#ff0066";
                      else if(j==6)varStyle="color:#ff0066";
                      else varStyle="font-weight;color:black;";
                      if(tonum == ++counter)
                        varStyle="font-weight:bold;color:blue";
                      document.write("<td  align='center' width='14%' style='"+varStyle+"' onmousedown=returndate(this.innerHTML) onmouseover=this.style.backgroundColor='#DDD' onmouseout=this.style.backgroundColor=''></td>"); 

             }
             document.write("</tr >");
        }
      document.write("</table>");
      document.write("</div> ");
  cal=document.getElementById("Calendar");
  tbl=document.getElementById("calTbl");
  fillcalendar();
  hidecal();
  }
 
  function hidecal(){ cal.style.display="none"; }
  
  function showcal(obj)
  {
      desTxtId=obj;
      cal.style.display="";
      vheight=obj.offsetHeight;
      vwidth=obj.offsetWidth;
      vtop=offsetTop(obj) + vheight;
      vleft=offsetLeft(obj);
      if(isIE)
      {
         if(vtop+cal.offsetHeight>document.body.scrollTop+document.body.clientHeight)
            vtop=vtop+vheight-cal.offsetHeight;
         if(vleft+cal.offsetWidth>document.body.scrollLeft+document.body.clientWidth)
           vleft=offsetLeft(obj)-cal.offsetWidth;
         cal.style.top=vtop;
         cal.style.left=vleft;
      }
      else
      {
          if(vtop+cal.offsetHeight>window.innerHeight)
              vtop=vtop+vheight-cal.offsetHeight;
          if(vleft+cal.offsetWidth>window.innerWidth)
              vleft=offsetLeft(obj)-cal.offsetWidth;
          cal.style.top=vtop;
          cal.style.left=vleft;
      }
  }
  
  function offsetLeft(obj)
  {   
    x = obj.offsetLeft;
    for (objParent=obj.offsetParent;objParent;objParent=objParent.offsetParent)x+=objParent.offsetLeft;
    return x;
  }
  function offsetTop(obj)
  { 
     y = obj.offsetTop;
     for(objParent=obj.offsetParent;objParent;objParent=objParent.offsetParent)y+=objParent.offsetTop;
     return y;
  } 

  
  function document.onclick()
  {
      with(window.event.srcElement)
      {
         if(getAttribute("Author")==null && tagName!="INPUT" && tagName!="IMG")
            hidecal();
      }
  }
  
  buildtbl();
  
  
  
  