
var ogTaskGroup;
var oxmlTaskGroupDoc  ;
var oTskCntDiv;
 
	
var TSKSTATE = {'RUNNING':1
,'STOP':2
,'PAUSE':4
,'FORDELETE':8
,'EXPIRED':16
,'OVERDAY':32
};

function DelTskTag(_Id3) 
{
    //alert('删除任务列表中的任务delTblSpan_'+_Id3);
    var spanId = 'delTblSpan_'+_Id3;
    var oSpan = Comm.$(spanId);
    if(oSpan)
    {
      oSpan.click();  
    }
}

function test(idx) 
{
    alert(idx);
} 

function StopUpdateLeftTimeLoop(bStop, _Id) 
{
    var _Id7 = 'leftTimeSpan_'+ _Id;
    var oLeftTimerTag = Comm.$(_Id7);
    if (!oLeftTimerTag) 
    {
        return;
    } 
    
    oLeftTimerTag.Stop = bStop; 
    if (oLeftTimerTag.timer) 
    {
        if (bStop=='1') 
        {
            clearTimeout(oLeftTimerTag.timer);oLeftTimerTag.timer=null;
        }
    }
    
    clearTimeout(oLeftTimerTag.timer2);
    if (oLeftTimerTag.bTimerCleared && oLeftTimerTag.bTimerCleared == '1') 
    { 
        oLeftTimerTag.bTimerCleared = '0';
    }else
    {
        oLeftTimerTag.timer2 = Comm.setTimeout(StopUpdateLeftTimeLoop, 500, bStop, _Id); 
    }
}
	
function UpdateLeftTime(nTotalSeconds, loopTimes, nState, y,m,d,h,minu,sec, _Id) 
{
    var _Id7 = 'leftTimeSpan_'+ _Id;
    var oLeftTimerTag = Comm.$(_Id7);
 
    if (!oLeftTimerTag) 
    {
        return;
    } 
    
    if (oLeftTimerTag.timer) 
    {
        clearTimeout(oLeftTimerTag.timer);oLeftTimerTag.timer=null;
    }
    
    oLeftTimerTag.nTotalSeconds = nTotalSeconds;
    
    var s = '';//'剩余时间： ';
    var nTmp = parseInt(nState,10);
    switch(nTmp) 
    {
    case TSKSTATE.RUNNING://正在运行
	    break;
    case TSKSTATE.STOP: //stop
        s = '已停止';
	    break;
    case TSKSTATE.PAUSE: //暂停
        s = '已暂停';
	    break;	    
    case TSKSTATE.FORDELETE: //待删除
        s = '待删除';
	    break;	
    case TSKSTATE.EXPIRED: //过期
        s = '已过期';
	    break;	
    case TSKSTATE.OVERDAY: //过期
        s = '触发时机超过一天';
	    break;	
    default:
	    break;
    } 
          
    if (nTmp&TSKSTATE.OVERDAY) 
    {
        s = '触发时机超过一天';
    }
    
    if ( nState != ''+TSKSTATE.RUNNING ) 
    { 
        oLeftTimerTag.innerHTML = s;
        return;        
    }
     
    if ((y=='0' && m=='0' && d=='0' && h=='0' && minu =='0' && sec =='0') || loopTimes=='0') 
    {
        oLeftTimerTag.innerHTML = '倒计时结束';
        return;
    }

    var oJson = eval(Comm.GetDateNxtSecs(y,m,d,h,minu,sec,-1));
    nTotalSeconds--;

    y    = oJson[0].y;
    m    = oJson[0].m;
    d    = oJson[0].d;
    h    = oJson[0].h;
    minu = oJson[0].minu;
    sec  = oJson[0].sec;

    s='';
    if (y!=0) 
    {
       s += y+'年 ';
    }else
    {
        if (m!=0) 
        {
           s += m+'月 ';
        }else
        {
            if (d!=0) 
            {
               s += d+'天 ';
            }else
            {
                if (h!=0) 
                {
                   s += h+'小时 ';
                }
                
                if (minu!=0) 
                {
                   s += minu+'分 ';
                } 
                
                if (sec!=0) 
                {
                   s += sec+'秒';
                }             
            }            
        }    
    } 

    if (loopTimes=='-1') 
    {
       s +=',无限次';
    }else
    {
       s +=','+loopTimes+'次';
    }  
    //(y==0 && m==0 && d==0 && h==0 && minu ==0 && sec <=0) || 
    if (loopTimes=='0') 
    {
        oLeftTimerTag.innerHTML = '任务结束';
        return;
    }else
    {
        oLeftTimerTag.innerHTML = s; 
    }
    
    if (oLeftTimerTag.Stop == '1' ) 
    {
        oLeftTimerTag.timer = Comm.setTimeout(UpdateLeftTime, 1000*24*60*60*1000, nTotalSeconds, loopTimes, nState, y, m, d, h, minu, sec, _Id);
        oLeftTimerTag.bTimerCleared = '1';    
    }else
    {
        oLeftTimerTag.timer = Comm.setTimeout(UpdateLeftTime, 1000, nTotalSeconds, loopTimes, nState, y, m, d, h, minu, sec, _Id); 
    }
}
 
function UpdateTskTag(_Id, sKey, sVal) 
{
    var xPath    = '//Task[Id="'+ _Id +'"]';
    var currNode = ogTaskGroup.xmlDoc.selectSingleNode(xPath);
     
    sKey = sKey.toLowerCase();
    if (sKey == 'updatetimes') 
    {
        currNode = currNode.selectSingleNode('Freq/Times');
        if (currNode) 
        { 
            currNode.text = sVal;
        }
    }
}//UpdateTskTag

function CreatTskTag( oParent, sCnt, _Id, sState )
{
    var oTaskSpan = document.createElement( 'span' );
    oTaskSpan.className = 'tskBar';
 
    oTaskSpan.id = _Id;
    
    var _Id0 = 'dateSelectSpan' + ( Math.random() * 1000000 ).toFixed( 0 );
    var _Id1 = 'delTblSpan_'  + _Id;//( Math.random() * 1000000 ).toFixed( 0 );
    var _Id2 = 'editTblSpan_' + _Id;// + ( Math.random() * 1000000 ).toFixed( 0 );
    var _Id3 = 'movedownTblSpan_'+ _Id; 
    var _Id4 = 'moveupTblSpan_'+ _Id;
    
    var _Id5 = 'pauseTblSpan_'+ _Id;
    var _Id6 = 'startTblSpan_'+ _Id;
    var _Id7 = 'leftTimeSpan_'+ _Id;

    oTaskSpan.innerHTML = '<span class="left tskTagName" title="' + sCnt + '" id="' + _Id0 + '">' + sCnt + '</span>' 
    + '<b class="right tskDel"      onmouseover="this.className=\'right tskDelOver\';"     onmouseout="this.className=\'right tskDel\';"      title="删除" id="' + _Id1 + '">&nbsp;</b>'
    + '<b class="right tskEdit"     onmouseover="this.className=\'right tskEditOver\';"    onmouseout="this.className=\'right tskEdit\';"     title="编辑" id="' + _Id2 + '">&nbsp;</b>'
    + '<b class="right tskMoveDn"   onmouseover="this.className=\'right tskMoveDnOver\';"  onmouseout="this.className=\'right tskMoveDn\';"   title="下移" id="' + _Id3 + '">&nbsp;</b>'
    + '<b class="right tskMoveUp"   onmouseover="this.className=\'right tskMoveUpOver\';"  onmouseout="this.className=\'right tskMoveUp\';"   title="上移" id="' + _Id4 + '">&nbsp;</b>';
 
    if (parseInt(sState,10) & TSKSTATE.PAUSE) 
    {
        //pause
        oTaskSpan.innerHTML+= '<b class="right tskPause"  onmouseover="this.className=\'right tskPauseOver\';" onmouseout="this.className=\'right tskPause\';"  title="开始" id="' + _Id5 + '">&nbsp;</b>'
        + '<b class="right tskStart"  onmouseover="this.className=\'right tskStartOver\';" onmouseout="this.className=\'right tskStart\';"  title="暂停" id="' + _Id6 + '" style="display:none;">&nbsp;</b>';
        
    }else
    {
        //run
        oTaskSpan.innerHTML+= '<b class="right tskPause"  onmouseover="this.className=\'right tskPauseOver\';" onmouseout="this.className=\'right tskPause\';"  title="开始" id="' + _Id5 + '" style="display:none;">&nbsp;</b>'
        + '<b class="right tskStart"  onmouseover="this.className=\'right tskStartOver\';" onmouseout="this.className=\'right tskStart\';"  title="暂停" id="' + _Id6 + '">&nbsp;</b>';
    }
    
    oTaskSpan.innerHTML+='<span class="right tskTimeLeftSpan" id="'+_Id7+'"> 1 天 2 小时 30 分 40 秒</span>';
    
    Comm.addEvent(oTaskSpan,'mouseover',function () 
    {
        this.className='tskBarhover';
    });
    
    Comm.addEvent(oTaskSpan,'mouseout',function () 
    {
        this.className='tskBar';
    });
    
    //获得第一个子节点，然后调用insertBefore
    var oFirstChild = oParent.firstChild;
    if(oFirstChild)
    {
        oParent.insertBefore( oTaskSpan ,oFirstChild);
    }else
    {
        oParent.appendChild( oTaskSpan );
    }
    
    Comm.addEvent(Comm.$(_Id3),'click',function () 
    {
        //下移 
        var oSpan = this.parentNode;
        var oNextSpan = oSpan.nextSibling;
        if(oNextSpan)
        {
            var i = 0;

            for ( i = oParent.childNodes.length-1; i >=0 ; i-- )
            {
                if ( oParent.childNodes[i].id == this.parentNode.id )
                {
                    break;
                }
            }
            
            if(i>=0)
            {
                window.external.swapNode( oParent.childNodes[i+1].id , oParent.childNodes[i].id );
                                    
                Comm.swapNode(oNextSpan,oSpan);
                this.className = 'right tskMoveDn';
                oSpan.className = 'tskBar';               
            }
        } 
    });
    
    Comm.addEvent(Comm.$(_Id4),'click',function () 
    {
        //上移 
        var oSpan = this.parentNode;
        var oPrevSpan = oSpan.previousSibling;
        if(oPrevSpan)
        {
            var i = 0;
            for ( i = oParent.childNodes.length-1; i >=0 ; i-- )
            {
                if ( oParent.childNodes[i].id == this.parentNode.id )
                {
                    break;
                }
            }
            
            if(i>=0)
            { 
                window.external.swapNode( oParent.childNodes[i].id,   oParent.childNodes[i-1].id );
                    
                Comm.swapNode(oPrevSpan,oSpan);
                this.className = 'right tskMoveUp'; 
                oSpan.className = 'tskBar';                 
            }
        }
    });
    
    Comm.addEvent( Comm.$( _Id2 ), 'click', function ()
    {
        //编辑任务 
        var xPath = '//Task[Id="'+this.parentNode.id +'"]';
        var currNode = ogTaskGroup.xmlDoc.selectSingleNode(xPath);

        //需要将currNode对应的xml,发送给子任务对话框。 
        window.external.showAddTaskDlg( currNode.xml );

    } );
    Comm.addEvent( Comm.$( _Id6 ), 'click', function ()
    {
        //暂停任务
        this.style.display='none';
        Comm.$( _Id5 ).style.display='block';
        var bComeFromChild = 0; 
        
        var xPath = '//Task[Id="'+this.parentNode.id +'"]/State';
        var oState = ogTaskGroup.xmlDoc.selectSingleNode(xPath);       
        var nState = parseInt(oState.text,10);
    
        window.external.PauseTsk('0',_Id, TSKSTATE.PAUSE, bComeFromChild);        
        

        if (oState) 
        {
            oState.text = nState|TSKSTATE.PAUSE;
        }    
        var oLeftTimerTag = Comm.$(_Id7);
        if (oLeftTimerTag.timer) 
        {
            clearTimeout(oLeftTimerTag.timer);oLeftTimerTag.timer=null;
        }
    } );
    
    Comm.addEvent( Comm.$( _Id5 ), 'click', function ()
    {
        //开始任务
        this.style.display='none';
        Comm.$( _Id6 ).style.display='block';
        var bComeFromChild = 0;
          
        //var _Id7 = 'leftTimeSpan_'+ _Id;
        var oLeftTimerTag = Comm.$(_Id7);
        
        var xPath = '//Task[Id="'+this.parentNode.id +'"]/State';
        var oState = ogTaskGroup.xmlDoc.selectSingleNode(xPath);
    
        var nState = parseInt(oState.text,10);
    
        window.external.PauseTsk(oLeftTimerTag.nTotalSeconds+'',_Id, TSKSTATE.RUNNING, bComeFromChild);        
        

        if (oState) 
        {
            oState.text = nState|TSKSTATE.RUNNING;
        }  
 
    } );
    
    Comm.addEvent( Comm.$( _Id7 ), 'click', function ()
    {
        //模拟编辑按钮动作
        Comm.$( _Id2 ).click();
    } );
    
    Comm.addEvent( Comm.$( _Id0 ), 'click', function ()
    {
        //模拟编辑按钮动作
        Comm.$( _Id2 ).click();
    } );

    Comm.addEvent( Comm.$( _Id1 ), 'click', function ()
    {
        var tipmsg = '确定要删除 ' + sCnt + ' 吗?';
        if (!confirm(tipmsg))
        {
            return ;
        }

        var i = 0;

        for ( i = oParent.childNodes.length-1; i >=0 ; i-- )
        {
            if ( oParent.childNodes[i].id == this.parentNode.id )
            {
                break;
            }
        }

        var currNode = ogTaskGroup.xmlDoc.childNodes[oParent.childNodes.length-1-i];

        //通知C++,删除内存映射XML相关节点
        window.external.delTaskXml( currNode.xml );

        //alert(i+', '+currNode.xml);
        ogTaskGroup.xmlDoc.removeChild( currNode );

        //删除当前标签
        oTaskSpan.parentNode.removeChild( oTaskSpan );
    } );

}//CreatTskTag



function CalConv( nyear, nmonth, nday )
{
    var FIRSTYEAR = 1936;
    var LASTYEAR = 2031;
    var LunarCal = [
                   new tagLunarCal( 23, 3, 2, 17, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0 ), /* 1936 */
                   new tagLunarCal( 41, 0, 4, 23, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1 ),
                   new tagLunarCal( 30, 7, 5, 28, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1 ),
                   new tagLunarCal( 49, 0, 6, 33, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 ),
                   new tagLunarCal( 38, 0, 0, 38, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ), /* 1940 */
                   new tagLunarCal( 26, 6, 2, 44, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0 ),
                   new tagLunarCal( 45, 0, 3, 49, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 ),
                   new tagLunarCal( 35, 0, 4, 54, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 ),
                   new tagLunarCal( 24, 4, 5, 59, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1 ), /* 1944 */
                   new tagLunarCal( 43, 0, 0, 5, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1 ),
                   new tagLunarCal( 32, 0, 1, 10, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1 ),
                   new tagLunarCal( 21, 2, 2, 15, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 ),
                   new tagLunarCal( 40, 0, 3, 20, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 ), /* 1948 */
                   new tagLunarCal( 28, 7, 5, 26, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ),
                   new tagLunarCal( 47, 0, 6, 31, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1 ),
                   new tagLunarCal( 36, 0, 0, 36, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 ),
                   new tagLunarCal( 26, 5, 1, 41, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1 ), /* 1952 */
                   new tagLunarCal( 44, 0, 3, 47, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1 ),
                   new tagLunarCal( 33, 0, 4, 52, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0 ),
                   new tagLunarCal( 23, 3, 5, 57, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1 ),
                   new tagLunarCal( 42, 0, 6, 2, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1 ), /* 1956 */
                   new tagLunarCal( 30, 8, 1, 8, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0 ),
                   new tagLunarCal( 48, 0, 2, 13, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0 ),
                   new tagLunarCal( 38, 0, 3, 18, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 ),
                   new tagLunarCal( 27, 6, 4, 23, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0 ), /* 1960 */
                   new tagLunarCal( 45, 0, 6, 29, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0 ),
                   new tagLunarCal( 35, 0, 0, 34, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 ),
                   new tagLunarCal( 24, 4, 1, 39, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0 ),
                   new tagLunarCal( 43, 0, 2, 44, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0 ), /* 1964 */
                   new tagLunarCal( 32, 0, 4, 50, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1 ),
                   new tagLunarCal( 20, 3, 5, 55, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0 ),
                   new tagLunarCal( 39, 0, 6, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0 ),
                   new tagLunarCal( 29, 7, 0, 5, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1 ), /* 1968 */
                   new tagLunarCal( 47, 0, 2, 11, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 ),
                   new tagLunarCal( 36, 0, 3, 16, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0 ),
                   new tagLunarCal( 26, 5, 4, 21, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1 ),
                   new tagLunarCal( 45, 0, 5, 26, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1 ), /* 1972 */
                   new tagLunarCal( 33, 0, 0, 32, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1 ),
                   new tagLunarCal( 22, 4, 1, 37, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1 ),
                   new tagLunarCal( 41, 0, 2, 42, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1 ),
                   new tagLunarCal( 30, 8, 3, 47, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1 ), /* 1976 */
                   new tagLunarCal( 48, 0, 5, 53, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1 ),
                   new tagLunarCal( 37, 0, 6, 58, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 ),
                   new tagLunarCal( 27, 6, 0, 3, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0 ),
                   new tagLunarCal( 46, 0, 1, 8, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0 ), /* 1980 */
                   new tagLunarCal( 35, 0, 3, 14, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1 ),
                   new tagLunarCal( 24, 4, 4, 19, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1 ),
                   new tagLunarCal( 43, 0, 5, 24, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1 ),
                   new tagLunarCal( 32, 10, 6, 29, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1 ), /* 1984 */
                   new tagLunarCal( 50, 0, 1, 35, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 ),
                   new tagLunarCal( 39, 0, 2, 40, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1 ),
                   new tagLunarCal( 28, 6, 3, 45, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0 ),
                   new tagLunarCal( 47, 0, 4, 50, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 ), /* 1988 */
                   new tagLunarCal( 36, 0, 6, 56, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0 ),
                   new tagLunarCal( 26, 5, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1 ),
                   new tagLunarCal( 45, 0, 1, 6, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0 ),
                   new tagLunarCal( 34, 0, 2, 11, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0 ), /* 1992 */
                   new tagLunarCal( 22, 3, 4, 17, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 ),
                   new tagLunarCal( 40, 0, 5, 22, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 ),
                   new tagLunarCal( 30, 8, 6, 27, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1 ),
                   new tagLunarCal( 49, 0, 0, 32, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1 ), /* 1996 */
                   new tagLunarCal( 37, 0, 2, 38, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 ),
                   new tagLunarCal( 27, 5, 3, 43, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1 ),
                   new tagLunarCal( 46,  0, 4, 48, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1 ), /* 1999 */
                   new tagLunarCal( 35,  0, 5, 53, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1 ), /* 2000 */
                   new tagLunarCal( 23,  4, 0, 59, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ),
                   new tagLunarCal( 42,  0, 1,  4, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ),
                   new tagLunarCal( 31,  0, 2,  9, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0 ),
                   new tagLunarCal( 21,  2, 3, 14, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1 ), /* 2004 */
                   new tagLunarCal( 39,  0, 5, 20, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 ),
                   new tagLunarCal( 28,  7, 6, 25, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1 ),
                   new tagLunarCal( 48,  0, 0, 30, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1 ),
                   new tagLunarCal( 37,  0, 1, 35, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1 ), /* 2008 */
                   new tagLunarCal( 25,  5, 3, 41, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 ),
                   new tagLunarCal( 44,  0, 4, 46, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 ),
                   new tagLunarCal( 33,  0, 5, 51, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ),
                   new tagLunarCal( 22,  4, 6, 56, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 ), /* 2012 */
                   new tagLunarCal( 40,  0, 1,  2, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 ),
                   new tagLunarCal( 30,  9, 2,  7, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1 ),
                   new tagLunarCal( 49,  0, 3, 12, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1 ),
                   new tagLunarCal( 38,  0, 4, 17, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0 ), /* 2016 */
                   new tagLunarCal( 27,  6, 6, 23, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1 ),
                   new tagLunarCal( 46,  0, 0, 28, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0 ),
                   new tagLunarCal( 35,  0, 1, 33, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0 ),
                   new tagLunarCal( 24,  4, 2, 38, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 ), /* 2020 */
                   new tagLunarCal( 42,  0, 4, 44, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 ),
                   new tagLunarCal( 31,  0, 5, 49, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0 ),
                   new tagLunarCal( 21,  2, 6, 54, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1 ),
                   new tagLunarCal( 40,  0, 0, 59, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 ), /* 2024 */
                   new tagLunarCal( 28,  6, 2,  5, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0 ),
                   new tagLunarCal( 47,  0, 3, 10, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1 ),
                   new tagLunarCal( 36,  0, 4, 15, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1 ),
                   new tagLunarCal( 25,  5, 5, 20, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0 ), /* 2028 */
                   new tagLunarCal( 43,  0, 0, 26, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1 ),
                   new tagLunarCal( 32,  0, 1, 31, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0 ),
                   new tagLunarCal( 22,  3, 2, 36, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0 ) ];


    /* 西曆年每月之日數 */
    var SolarCal = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];

    /* 西曆年每月之累積日數, 平年與閏年 */
    var SolarDays = [ 
                  0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 396,
                  0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366, 397 ];

    var AnimalIdx   = ["馬 ", "羊 ", "猴 ", "雞 ", "狗 ", "豬 ", "鼠 ", "牛 ", "虎 ", "兔 ", "龍 ", "蛇 " ];
    var LocationIdx = ["南",  "東",  "北",  "西" ];


//if (!IsInteger(nyear) || !IsInteger(nmonth) || !IsInteger(nday)) return alert("请输入合法农历年月日数值");
    var LunarYear = parseInt( nyear );
    var LunarMonth = parseInt( nmonth );
    var LunarDate = parseInt( nday );

    if ( LunarYear < FIRSTYEAR || LunarYear >= LASTYEAR ) return alert( "请输入1936-2031有效年份" );

    var y = LunarYear - FIRSTYEAR ;
    var im = LunarCal[y].Intercalation;
    var lm = LunarMonth;

    if ( lm < 0 )
    {
        if ( lm != -im )
            return alert( "请输入有效月份" );
    }
    else if ( lm < 1 || lm > 12 ) return alert( "请输入有效月份" );

    if ( im != 0 )
    {
        if ( lm > im )
            lm++;
        else if ( lm == -im )
            lm = im + 1;
    }

    lm--;

    if ( LunarDate > LunarCal[y].MonthDays[lm] + 29 )
        return alert( "农历日期不正确" );

    var acc = 0;

    for ( i = 0; i < lm; i++ )
    {
        acc += LunarCal[y].MonthDays[i] + 29;
    }

    acc += LunarCal[y].BaseDays + LunarDate;

    var leap = GetLeap( LunarYear );

    for ( i = 13; i >= 0; i-- )
    {
        if ( acc > SolarDays[leap*14+i] )
            break;
    }

    var SolarDate = acc - SolarDays[leap*14 + i]  ;
    var SolarYear,SolarMonth;
    
    if ( i <= 11 )
    {
        SolarYear = LunarYear;
        SolarMonth = i + 1;
    }
    else
    {
        SolarYear = LunarYear + 1;
        SolarMonth = i - 11;
    }

    leap = GetLeap( SolarYear );
    y = SolarYear - FIRSTYEAR;

    //acc = SolarDays[leap][SolarMonth-1] + SolarDate;
    acc = SolarDays[leap*14 + SolarMonth-1] + SolarDate;

//    var weekday = ( acc + LunarCal[y].BaseWeekday ) % 7;
//    kc = acc + LunarCal[y].BaseKanChih;
//    kan = kc % 10;
//    chih = kc % 12;

    alert( "阳历为：" + SolarYear + "年" + SolarMonth + "月" + SolarDate + "日 " );

    nyear = "";
    nmonth = "";
    nday = "";
    return 0;
}

/* 闰年, 返回 0 平年, 1 闰年 */
function GetLeap( year )
{
    if ( year % 400 == 0 )
        return 1;
    else if ( year % 100 == 0 )
        return 0;
    else if ( year % 4 == 0 )
        return 1;
    else
        return 0;
}

function tagLunarCal( d, i, w, k, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13 )
{
    this.BaseDays = d;         /* 1 月 1 日到正月初一的累计日 */
    this.Intercalation = i;    /* 闰月月份. 0==此年沒有闰月 */
    this.BaseWeekday = w;      /* 此年 1 月 1 日为星期减 1 */
    this.BaseKanChih = k;      /* 此年 1 月 1 日之干支序号减 1 */
    this.MonthDays = [ m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13 ]; /* 此农历年每月之大小, 0==小月(29日), 1==大月(30日) */
}

Comm.ReadyDoc( function ()
{
    //oxmlTaskGroupDoc = new ActiveXObject( "Msxml2.Domdocument" );
    //oxmlTaskGroupDoc.async = false;
    //var xmlDoc =  new ActiveXObject( "Msxml2.Domdocument" );
    oxmlTaskGroupDoc = new OHSXml();
    
    ogTaskGroup = new OHSXml();
    ogTaskGroup.xmlDoc = oxmlTaskGroupDoc.xmlDoc.createElement( "Tasks" );
    //ogTaskGroup = oxmlTaskGroupDoc.createElement("Tasks");
    oTskCntDiv = Comm.$( 'TskCntDiv' );
 
});

function SendTskXmlToHTML( s )
{
    //var xmlDoc =  new ActiveXObject( "Msxml2.Domdocument" );
    var xmlDoc = new OHSXml();
    //xmlDoc.async = false;
    xmlDoc.loadXML( s );

    var childNodes = xmlDoc.xmlDoc.selectNodes( "Tasks/*" );

    for ( var i = 0; i < childNodes.length; i++ )
    {
        //alert(childNodes[i].xml);
        AddTskXml( childNodes[i].xml );
    }
}//SendTskXmlToHTML


function AddTskXml( s )
{
    var xmlDoc = new OHSXml();
    xmlDoc.loadXML( s );
 
    var nameNode = xmlDoc.xmlDoc.selectSingleNode( "//Name" );
    
    var node = xmlDoc.selectSingleNode( "//Id" );
    
    var oState = xmlDoc.selectSingleNode( "//State" );
    //alert('oState:'+oState.text);

    var oOldNode = ogTaskGroup.xmlDoc.selectSingleNode( "//Id[.='" + node.text + "']" );

    var root = xmlDoc.xmlDoc.documentElement;
 
 
    if ( oOldNode )
    {
        //关停倒计时器，如果上次有的话
        StopUpdateLeftTimeLoop(1,node.text);
    
        //存在原节点,则直接替换
        ogTaskGroup.xmlDoc.replaceChild( node.parentNode, oOldNode.parentNode );

        var o = oOldNode.parentNode.firstChild;
        var oOldLine = Comm.$( oOldNode.text ).firstChild;
        oOldLine.innerText = nameNode.text;
    }
    else
    {
        ogTaskGroup.xmlDoc.appendChild( root );
        CreatTskTag( oTskCntDiv, nameNode.text, node.text , oState.text);
    }


}

