//该文件是在用户选择日期时用到。
//下面是使用的例子
//带*号的表示关键的
//*<link rel="stylesheet" href="../css/css.css">
//*<script language="javascript1.2" src="../js/DateSelect.JS"></script>
//<form name=form1><table><tr><td>
//*<input type="text" name="s1" size=20>
//*<img src="../images/DateSelect.gif" Title="打开或关闭日期选择" onclick="ClickDateSelectPic(this,form1.s1);">
//</td></tr></table></form>
//在文件的后面一定要带下面的DIV
//*<div id="DivDateSelect" style="position:absolute;display:none;border:1px solid #245B7D;"></div>


///////////////////////////////////////////////
//在通用时间对象中保存当前时间
var DateOB=new Date();
///////////////////////////////////////////////
//把要传给日期的输入框的对象保存在变量中
var DateInputName="";
////////////////////////////////////////////////
//该函数显示日期为DateOB的日历
function DateDisplay(){
 var a="";
 a=a+"<table cellspacing=\"0\" cellpadding=\"0\" width=140 height=20 bgcolor=\"#CEDAE7\">";
 a=a+"<tr align=center>";
 a=a+"<td width=\"12%\" onmouseover=\"this.className='ds_border';\" onmouseout=\"this.className='';\" onclick=\"subYear();\" title=\"减小年份\">&lt;&lt;</td>";
 a=a+"<td width=\"12%\" onmouseover=\"this.className='ds_border';\" onmouseout=\"this.className='';\" onclick=\"subMonth();\" title=\"减小月份\" style=\"cursor: hand;\">&lt;</td>";
 a=a+"<td width=\"52%\"><b>"+DateOB.getFullYear()+"</b><b>年</b><b>"+(DateOB.getMonth()+1)+"</b><b>月</b></td>";
 a=a+"<td width=\"12%\" onmouseover=\"this.className='ds_border';\" onmouseout=\"this.className='';\" onclick=\"addMonth();\" title=\"增加月份\" style=\"cursor: hand;\">&gt;</td>";
 a=a+"<td width=\"12%\" onmouseover=\"this.className='ds_border';\" onmouseout=\"this.className='';\" onclick=\"addYear();\" title=\"增加年份\" style=\"cursor: hand;\">&gt;&gt;</td>";
 a=a+"</tr>";
 a=a+"</table>";
 var week=new Array("日","一","二","三","四","五","六");
 a=a+"<table cellspacing=\"0\" cellpadding=\"0\" width=140 height=20>";
 a=a+"<tr align=center>";
 var i;
 for(i in week){
  a=a+"<td>"+week[i]+"</td>";
 }
 a=a+"</tr>";
 a=a+"</table>";
 //取得当月1号是星期几
 var myDate=new Date(DateOB.getFullYear(),DateOB.getMonth(),1);
 var FirstDay=myDate.getDay()
 //取得当月有多少天
 var MaxDay;
 var ThisMonth=parseInt(DateOB.getMonth())+1;
 var ThisYear=DateOB.getFullYear();
 switch(ThisMonth)
 {
  case 1:
  case 3:
  case 5:
  case 7:
  case 8:
  case 10:
  case 12:
   MaxDay=31;
   break;
  case 4:
  case 6:
  case 9:
  case 11:
   MaxDay=30;
   break;
  case 2:
   if((ThisYear%4==0)&&(ThisYear%100!=0)||(ThisYear%400==0)){
    MaxDay=29;
   }else{
    MaxDay=28;
   }
 }
 var j;
 var k=0;
 //判断年月是否为当前年月
 var NowDate=new Date();
 var IsNowMonth=false;
 if(NowDate.getFullYear()==DateOB.getFullYear()&&NowDate.getMonth()==DateOB.getMonth()){
  IsNowMonth=true;
 }
 var NowDay=NowDate.getDate();
 //显示日期
 var DisplayDay;
 a=a+"<table cellspacing=\"0\" cellpadding=\"0\" width=140 height=20 bgcolor=\"#EEEEEE\">";
 for(i=0;i<6;i++){
  a=a+"<tr align=center>";
  for(j=0;j<7;j++){
   DisplayDay=parseInt(k-FirstDay)+1;
   if(k<FirstDay||DisplayDay>MaxDay){
    a=a+"<td>&nbsp;</td>";
   }else{
    if(IsNowMonth&&DisplayDay==NowDay){
     a=a+"<td onclick=\"ClickDate("+DisplayDay+");\" onmouseover=\"this.className='ds_border';\" onmouseout=\"this.className='';\" style=\"color:red;\">"+DisplayDay+"</td>";
    }else{
     a=a+"<td onclick=\"ClickDate("+DisplayDay+");\" onmouseover=\"this.className='ds_border';\" onmouseout=\"this.className='';\">"+DisplayDay+"</td>";
    }
   }
   k=parseInt(k)+1;
  }
  a=a+"</tr>";
  if((parseInt(k-FirstDay)+1)>MaxDay){
   i=6;
  }
 }
 a=a+"</table>";
 DivDateSelect.innerHTML=a;
}

///////////////////////////////////
//该函数是当按了减少年份时处理
function subYear(){
 var NewYear=DateOB.getFullYear()-1;
 DateOB.setFullYear(NewYear);
 DateDisplay();
}
///////////////////////////////////
//该函数是当按了增加年份时处理
function addYear(){
 var NewYear=parseInt(DateOB.getFullYear())+1;
 DateOB.setFullYear(NewYear);
 DateDisplay();
}
///////////////////////////////////
//该函数是当按了减少月份时处理
function subMonth(){
 var NewMonth=DateOB.getMonth()-1;
 DateOB.setMonth(NewMonth);
 DateDisplay();
}
///////////////////////////////////
//该函数是当按了增加月份时处理
function addMonth(){
 var NewMonth=parseInt(DateOB.getMonth())+1;
 DateOB.setMonth(NewMonth);
 DateDisplay();
}
///////////////////////////////////
//该函数是当按了日期时处理
function ClickDate(DateNo){
 var DateStr=DateOB.getFullYear()+"-"+(parseInt(DateOB.getMonth())+1)+"-"+DateNo;
 if(DateInputName!=""){
  DateInputName.value=DateStr;
  DivDateSelect.style.display="none";
  DateInputName="";
 }
}
///////////////////////////////////
//该函数是当点了日期选择的图标时，显示日期选择块并把该图标对象和输入框对象传递过来
function ClickDateSelectPic(PicOB,DateInputOB){
 if(DivDateSelect.style.display=="block"){
  DivDateSelect.style.display="none";
  DateInputName="";
 }else{
  var a=GetLeft(PicOB);
  var b=GetTop(PicOB);
  DivDateSelect.style.left=a;
  DivDateSelect.style.top=b+20;
  DivDateSelect.style.display="block";
  DateDisplay();
  DateInputName=DateInputOB;
 }
}
///////////////////////////////////
//取得某对象的绝对Left坐标
function GetLeft(OBName){
 var l=OBName.offsetLeft;
 while(OBName=OBName.offsetParent){
  l+=OBName.offsetLeft;
 }
 return l;
}
///////////////////////////////////
//取得某对象的绝对Top坐标
function GetTop(OBName){
 var t=OBName.offsetTop;
 while(OBName=OBName.offsetParent){
  t+=OBName.offsetTop;
 }
 return t;
}
