﻿// JScript 文件
function DateUtils(){
    this.getDaysInMonth=getDaysInMonth;
}
function getDaysInMonth(yy,mm){
    var d=new Date(yy,mm-1,1);
    var c=1;
    while(d.getMonth()+1==mm){
        d.setDate(c);
        c++;
    }
    return(c-2);
}
function Canlendar(oY,oM,oCtl,oIdx,oHasTime){
    this.yy=oY;
    this.mm=oM-1;
    this.ctrl=oCtl;
    this.indx=oIdx;
    this.createPanel=createPanel;
    this.createToolBar=createToolBar;
    this.createCanBody=createCanBody;
    this.destroyPanel=destroyPanel;
    this.goPrev=goPrev;
    this.goNext=goNext;
    this.hasTime=oHasTime;
}
function destroyPanel(){
    for(i=0;i<document.getElementsByName("oCanLayer").length;i++){
        document.getElementsByName("oCanLayer").item(i).removeNode(true);
    }
    if(document.getElementById("oFrame")!=null)
    {
        document.getElementById("oFrame").removeNode(true);
    }
}
function createPanel(){
    this.destroyPanel();
    var obj=document.getElementsByName(this.ctrl).item(this.indx);
    var leftPos=getAbsolutePosX(obj)+obj.offsetWidth;
    var topPos=getAbsolutePosY(obj);       
    oDiv=document.createElement("DIV");
    oDiv.id="oCanLayer";
    oDiv.name="oCanLayer";
    oDiv.style.cssText="z-index:10000;background-color:#eeeeee;position:absolute;left:"+leftPos+"px;top:"+topPos+"px;width:200px;";
    tblTB=this.createToolBar();
    oDiv.appendChild(tblTB);
    tblCA=this.createCanBody();
    oDiv.appendChild(tblCA);
    document.body.appendChild(oDiv);
    //////////////////////
    oFrame=document.createElement("IFRAME");
    oFrame.id="oFrame";
    oFrame.style.left=oDiv.style.left;
    oFrame.style.top=oDiv.style.top;
    oFrame.style.width=oDiv.offsetWidth;
    oFrame.style.height=oDiv.offsetHeight;
    oFrame.style.zIndex=9999;
    oFrame.src="javascript:false";
    oFrame.style.position="absolute";
    document.body.appendChild(oFrame);     
}
function createToolBar(){
    var oTbl=document.createElement("TABLE");
    oTbl.style.cssText="width:207px;border:1px solid black";
    var oTr=oTbl.insertRow();
    ///////////////////////
    var oTd=oTr.insertCell();
    oTd.innerHTML="<button onclick=goPrevY("+this.yy+","+(this.mm+1)+",'"+this.ctrl+"',"+this.indx+","+this.hasTime+")><<</button><button onclick=goPrev("+this.yy+","+(this.mm+1)+",'"+this.ctrl+"',"+this.indx+","+this.hasTime+")><</button>";
    oTd.style.cssText="width:10%;";
    ///////////////////////
    var oTd=oTr.insertCell();
    oTd.innerHTML=this.yy+"-"+(this.mm+1);
    oTd.style.cssText="width:70%;text-align:center;font-size:10pt";
    ///////////////////////
    var oTd=oTr.insertCell();
    oTd.innerHTML="<button onclick=goNext("+this.yy+","+(this.mm+1)+",'"+this.ctrl+"',"+this.indx+","+this.hasTime+")>></button><button onclick=goNextY("+this.yy+","+(this.mm+1)+",'"+this.ctrl+"',"+this.indx+","+this.hasTime+")>>></button>";
    oTd.style.cssText="width:10%"
    var oTd=oTr.insertCell();
    oTd.innerHTML="<button onclick='destroyPanel()'>关</button>";
    oTd.style.cssText="width:10%";
    ///////////////////////
    return(oTbl);
}
function createCanBody(c,i){
    var oTbl=document.createElement("TABLE");
    oTbl.style.cssText="width:207px;border:1px solid black;font-size:9pt";
    var oTr=oTbl.insertRow();
    ////////////////////////////////
    var oTd=oTr.insertCell();
    oTd.innerHTML="日";
    oTd.style.cssText="width:14.2%;text-align:center;";    
    ////////////////////////////////
    var oTd=oTr.insertCell();
    oTd.innerHTML="一";
    oTd.style.cssText="width:14.2%;text-align:center;";    
    ////////////////////////////////
    var oTd=oTr.insertCell();
    oTd.innerHTML="二";
    oTd.style.cssText="width:14.2%;text-align:center;";
    ////////////////////////////////
    var oTd=oTr.insertCell();
    oTd.innerHTML="三";
    oTd.style.cssText="width:14.2%;text-align:center;"; 
    ////////////////////////////////
    var oTd=oTr.insertCell();
    oTd.innerHTML="四";
    oTd.style.cssText="width:14.2%;text-align:center;"; 
    ////////////////////////////////
    var oTd=oTr.insertCell();
    oTd.innerHTML="五";
    oTd.style.cssText="width:14.2%;text-align:center;"; 
    ////////////////////////////////
    var oTd=oTr.insertCell();
    oTd.innerHTML="六";
    oTd.style.cssText="width:14.2%;text-align:center;";  
    ////////////////////////////////
    var du=new DateUtils();
    var totalDays=du.getDaysInMonth(this.yy,this.mm+1);
    var curDate=new Date(this.yy,this.mm,1);
    var fstWeekDay=curDate.getDay();
    var oTr=oTbl.insertRow();
    var d=new Date();
    var prevMonthTotalDays=du.getDaysInMonth(this.mm==0?this.yy-1:this.yy,this.mm==0?12:this.mm);
    var cpd=prevMonthTotalDays-fstWeekDay+1;
   
    for(l=0;l<fstWeekDay;l++){
        var oTd=oTr.insertCell();
        oTd.innerText=cpd++;
        oTd.style.cssText="text-align:center;color:silver";
    }
    var curWeekDay=fstWeekDay;
    for(l=1;l<=totalDays;l++){
        var oTd=oTr.insertCell();
        //oTd.innerText=l;
        oTd.innerHTML="<a style='color:blue;text-decoration:none' href='javascript:' onclick=returnDate("+this.yy+","+(this.mm+1)+","+l+",'"+this.ctrl+"',"+this.indx+","+this.hasTime+") id="+this.yy+"-"+(this.mm+1)+"-"+l+">"+l+"</a>"
        oTd.style.cssText="text-align:center;";
        if(curWeekDay==6||curWeekDay==0)oTd.style.backgroundColor="silver";
        curWeekDay++;
        if(curWeekDay==7){
            curWeekDay=0;
            if(!(curWeekDay==0&&i==totalDays)){
                oTr=oTbl.insertRow();
            }
        }
        if(l==d.getDate()&&this.yy==d.getYear()&&this.mm==d.getMonth())oTd.style.backgroundColor="gold";
    }
    if(curWeekDay!=0){
        for(i=1;i<8-curWeekDay;i++){
            var oTd=oTr.insertCell();
            oTd.innerText=i;
            oTd.style.cssText="text-align:center;color:silver";
        }
        
    }
    
    if(!this.hasTime)return(oTbl);
    
    var oTr=oTbl.insertRow();
    var oTd=oTr.insertCell();
    oTd.colSpan=7;
    oTd.style.textAlign="center";
    var oDateText=document.createElement("INPUT");
    oDateText.style.cssText="width:100%";
    oDateText.id="dateText";
    oDateText.name="dateText";
    oTd.appendChild(oDateText);
    
    var oTr=oTbl.insertRow();
    var oTd=oTr.insertCell();
    oTd.colSpan=7;
    oTd.style.textAlign="center";
       
    var oHour=document.createElement("SELECT");
    oHour.id="dateHour";
    oHour.name="dateHour";
    var oMinute=document.createElement("SELECT");
    oMinute.id="dateMinute";
    oMinute.name="dateMinute";
    var oSecond=document.createElement("SELECT");
    oSecond.id="dateSecond";
    oSecond.name="dateSecond";
    //var oButton=document.createElement("BUTTON");
    for(i=1;i<24;i++){
        var oHourOp=document.createElement("OPTION");
        oHourOp.text=i+"时";
        oHourOp.value=i;
        if(i==d.getHours()+1)oHourOp.selected=true;
        oHour.options.add(oHourOp);
    }
    for(i=0;i<60;i++){
        var oMinuteOp=document.createElement("OPTION");
        oMinuteOp.text=(i<10?"0"+i:i)+"分";
        oMinuteOp.value=(i<10?"0"+i:i);
        if(i==d.getMinutes())oMinuteOp.selected=true;
        oMinute.options.add(oMinuteOp);
    }
    for(i=0;i<60;i++){
        var oSecondOp=document.createElement("OPTION");
        oSecondOp.text=(i<10?"0"+i:i)+"秒";
        oSecondOp.value=(i<10?"0"+i:i);
        if(i==d.getSeconds())oSecondOp.selected=true;
        oSecond.options.add(oSecondOp);
    }
    //oButton.value="确定";
    //oButton.onclick="returnTime("+this.ctrl+","+this.indx+")";
    oTd.appendChild(oHour);
    oTd.appendChild(oMinute);
    oTd.appendChild(oSecond);
    //oTd.appendChild(oButton);
    oTd.innerHTML+="<button onclick=returnTime('"+this.ctrl+"',"+this.indx+")>确定</button>";
    return(oTbl);
}
function goPrev(oy,om,ctrlName,ctrlIndx,hasTime){
    if(om==1){
      oy=oy-1;
      om=12;
    } else {
      om=om-1;
    }
    ca=new Canlendar(oy,om,ctrlName,ctrlIndx,hasTime);
    ca.createPanel();   
}
function goNext(oy,om,ctrlName,ctrlIndx,hasTime){
    if(om==12){
      oy=oy+1;
      om=1;
    } else {
      om=om+1;
    }
    ca=new Canlendar(oy,om,ctrlName,ctrlIndx,hasTime);
    ca.createPanel();    
}
function goPrevY(oy,om,ctrlName,ctrlIndx,hasTime){
    ca=new Canlendar(oy-1,om,ctrlName,ctrlIndx,hasTime);
    ca.createPanel();    
}
function goNextY(oy,om,ctrlName,ctrlIndx,hasTime){
    ca=new Canlendar(oy+1,om,ctrlName,ctrlIndx,hasTime);
    ca.createPanel();   
}
function returnDate(xy,xm,xd,xc,xi,hasTime){
    if(hasTime){
        var oTd=document.getElementById(xy+"-"+xm+"-"+xd);
        document.getElementById("dateText").value=xy+"-"+xm+"-"+xd;    
    } else {
        document.getElementsByName(xc).item(xi).value=xy+"-"+xm+"-"+xd;
        destroyPanel();
    }    
}
function returnTime(xc,xi){
    if(document.getElementById("dateText").value==""){
        alert("请先选择日期!");
        return;
    }
    oD=document.getElementById("dateText").value;
    oH=document.getElementById("dateHour").value;
    oM=document.getElementById("dateMinute").value;
    oS=document.getElementById("dateSecond").value;
    document.getElementsByName(xc).item(xi).value=oD+" "+oH+":"+oM+":"+oS;
    destroyPanel();
}
function getAbsolutePosX(obj){
    var l=obj.offsetLeft;
    while(obj=obj.offsetParent)
    l+=obj.offsetLeft;
    return l;
}
function getAbsolutePosY(obj){
    var t=obj.offsetTop;
    while(obj=obj.offsetParent)
    t+=obj.offsetTop;
    return t;
}
function showCan(objName,i,showTime){
    da=new Date();
    ca=new Canlendar(da.getYear(),da.getMonth()+1,objName,i,showTime);
    ca.createPanel();
}