//parent.父类的下要有用要去除

var iCalendarHeight = 150;
var iCalendarWidth = 250;
var iBonder = 5;
var iMinYear = 1970;
var iMaxYear = 2030;
var iDaysPerPage = 42;

var sYearMthBgColor = '#395592';
var sYearMthFontColor = '#FFFFFA';
var sTabDateBgColor = '#CCCCCC';
var sTabTrWeekBgColor = '#F5F5F5';
var sTabTrDateBgColor = '#FFFFFB';
var sTabTdMouseOverColor= '#E1E1E1';
var sTabTdMouseOutColor = '#FFFFFC';
var sSelectedBgColor = '#420042';
var sNotSelectedFontColor = '#000000';
var sSundayFontColor = '#FF0000';
var sSaturdayFontColor = '#FF0000';
var sTabTdPrevAndNextFontColor = '#BBBBBB';
var sTabTodayBgColor = '#F5F5F5';
var sTabTodayLableFontColor = '#FF0000';
var sIframePostfix = '_bk';

var isHid = true;
var arrDisplayedCalendars = new Array();

function initBK( sCalendarID ){
    document.writeln( '<Iframe id=\"' + (sCalendarID + sIframePostfix) + '\" class="Calendar" scrolling="no" frameborder=0 style="width: '+ iCalendarWidth +' ; height: '+ iCalendarHeight +'; position: absolute ; z-index: 0; display: none"></Iframe> ' );
}

function createNewDiv( sCalendarID )
{
    document.writeln( '<Div id='+ sCalendarID +' name= '+ sCalendarID +' class="Calendar" Author=Nick_For_His_Wife scrolling="no" frameborder=0 style="border:0px solid ;position: absolute; width: '+iCalendarWidth+'; height: '+iCalendarHeight+'; z-index: 100 ; display: none"></Div>' );
    initBK( sCalendarID );
}

function positionDiv( sInputBoxID , sCalendarID)
{
    var inputBox = document.getElementById( sInputBoxID );
    var frLeft = inputBox.offsetLeft;
    var frTop = inputBox.offsetTop;
    var tmp = inputBox;
    while ( tmp = tmp.offsetParent ){
        frLeft = frLeft + tmp.offsetLeft;
        frTop = frTop + tmp.offsetTop;    
    }
    frTop = frTop + inputBox.clientHeight;
    var calendar = document.getElementById( sCalendarID );
    calendar.style.left = frLeft;
    calendar.style.top = frTop;
    
    var bkFrame = document.getElementById( (sCalendarID + sIframePostfix) );
    bkFrame.style.left = frLeft;
    bkFrame.style.top = frTop;
    bkFrame.style.height = calendar.style.height;
    bkFrame.style.width = calendar.style.width;
}

function getToday()
{
	  return new Date();
}

function getDateFormat( dDate )
{
    var iYear = dDate.getFullYear();
    var iMth = ( dDate.getMonth() + 1 );
    var iDay = dDate.getDate();
	  if ( iMth < 10 )
  	{
  		iMth = '0' + iMth;
  	}
  	if ( iDay < 10 )
  	{
  		iDay = '0' + iDay;
  	}
  	return iYear + '-' + iMth + '-' + iDay;
}

function isLeapYear( iYear )
{
    if ((iYear%400==0) || ((iYear%4==0) && (iYear%100!=0)))
  		return true;
  	else
  		return false;
}

function getFitDate( iYear , iMth , iDay )
{
  	var iActualMth = iMth + 1;
  	// Not February
    if ( iActualMth != 2 )
  	{
  		if (( iDay == 31 )&&(( iActualMth == 4 )||( iActualMth == 6 )||( iActualMth == 9 )||( iActualMth == 11 )))
  		{
  			dNewDay = new Date( iYear , iMth , ( iDay - 1 ) );
  			return dNewDay;
  		}else
  		{
  			dNewDay = new Date( iYear , iMth , iDay );
  			return dNewDay;
  		}
  	}
  	// Is February
  	if ( iDay < 29 )
  	{
  		dNewDay = new Date( iYear , iMth , iDay );
  		return dNewDay;
  	}
      // Leap Year
  	if ( isLeapYear( iYear ) )
  	{
  		dNewDate = new Date( iYear , iMth , 29);
  		return dNewDate;
  	}
  	// Not Leap Year
  	dNewDate = new Date( iYear , iMth , 28);
  	return dNewDate;
}

function getNextYear( sCalendarID , sInputBoxID , iYear , iMth , iDay )
{
    if ( ( iYear + 1 ) > iMaxYear )
  	{
  		dCurrDate = getFitDate( iMinYear , iMth , iDay );
  	}
    else
	  {
        dCurrDate = getFitDate( ( iYear + 1 ) , iMth , iDay );
    }
	  displayCalendarFrame( sCalendarID , sInputBoxID , dCurrDate );
}

function getPreviousYear( sCalendarID , sInputBoxID , iYear , iMth , iDay )
{
  	if ( ( iYear - 1 ) < iMinYear )
  	{
  		dCurrDate = getFitDate( sInputBoxID , iMaxYear , iMth , iDay );
  	}
      else
  	{
  	  dCurrDate = getFitDate( ( iYear - 1 ) , iMth , iDay );
    }
  	displayCalendarFrame( sCalendarID , sInputBoxID , dCurrDate );
}

function getNextMonth( sCalendarID , sInputBoxID , iYear , iMth , iDay )
{
    if ( ( iMth + 1 ) > 11 )
	  {
		  dCurrDate = getNextYear( sCalendarID , sInputBoxID , iYear , 0 , iDay );
		  return;
	  }
    else
	  {
      dCurrDate = getFitDate( iYear , (iMth + 1) , iDay);
    }
	  displayCalendarFrame( sCalendarID , sInputBoxID , dCurrDate );
}

function getPreviousMonth( sCalendarID , sInputBoxID , iYear , iMth , iDay )
{
    if ( ( iMth - 1 ) < 0 )
	  {
		  dCurrDate = getPreviousYear( sCalendarID , sInputBoxID , iYear , 11 , iDay );
		  return;
	  }
      else
	  {
        dCurrDate = getFitDate( iYear , (iMth - 1) , iDay );
    }
	  displayCalendarFrame( sCalendarID , sInputBoxID , dCurrDate );
}

function getMonthDaysNum( iYear , iMth )
{
	  if ( iMth == 1 )
	  {
        if ( isLeapYear( iYear ) )
        {
            return 29;
        }
        return 28;
    }
    switch ( iMth )
	  {
        case 0 : return 31;
        case 2 : return 31;
        case 3 : return 30;
        case 4 : return 31;
        case 5 : return 30;
        case 6 : return 31;
        case 7 : return 31;
        case 8 : return 30;
        case 9 : return 31;
        case 10 : return 30;
        case 11 : return 31;
    }
}

function getAllDays( iYear , iMth )
{
    arrAllDays = new Array();
	  dFirstDay = new Date( iYear , iMth , 1 );
    var iWeekDay = dFirstDay.getDay();
    if ( iWeekDay != 0 )
	  {
        switch( iWeekDay )
        {
    		case 6 : arrAllDays.push( 0 );
    		case 5 : arrAllDays.push( 0 );
    		case 4 : arrAllDays.push( 0 );
    		case 3 : arrAllDays.push( 0 );
    		case 2 : arrAllDays.push( 0 );
    		case 1 : arrAllDays.push( 0 );
    	}
    }
    var iMthDaysNum = getMonthDaysNum( iYear , iMth );
	  for (var i = 0 ; i < iMthDaysNum ; i++ )
	  {
		    arrAllDays.push( i + 1 );
	  }
	  var iArrLen = arrAllDays.length;
	  for( var i=iArrLen ; i < iDaysPerPage ; i++ )
	  {
	    arrAllDays.push( 0 );
	  }
	  return arrAllDays;
}

function getValidDate( sDate )
{
    arrDate = sDate.split( "-" );
  	if ( arrDate.length != 3 ) // if it is not valid format return today
  	{
  		return new Date();
  	}
  	var iYear = arrDate[0]
  	var iMth = arrDate[1];
  	var iDay = arrDate[2];
  	return getFitDate( iYear , (iMth-1) , iDay);
}

function showCalendar( sInputBoxID , sCalendarID )
{
	  closeAllDisplayedCalendar();
	  inputBox = document.getElementById( sInputBoxID );
    addcalendarDisplay( sCalendarID );
  	var sValue = inputBox.value;
  	calendar = document.getElementById( sCalendarID );
  	dCurrDate = getValidDate( sValue );
  	positionDiv( sInputBoxID , sCalendarID ) ;
  	displayCalendarFrame( sCalendarID , sInputBoxID , dCurrDate );
  	isHid = false;
}

function displayCalendarFrame( sCalendarID , sInputBoxID , dCurrDate )
{
    calendar = document.getElementById( sCalendarID );
    var frameText;
    var iYear = dCurrDate.getFullYear();
    var iMth = dCurrDate.getMonth();
    var iDay = dCurrDate.getDate();
    arrAllDays = getAllDays( iYear , iMth );
    frameText="\n<table onselectstart=\"return false;\" border='0' cellpadding='0' cellspacing='0' bgcolor=\'"+ sYearMthBgColor+"\' width='100%' height='15' style=\"color:"+ sYearMthFontColor +" ; font-weight:bolder;border:0px solid\">"+"\n<tr>\n";
    frameText+="<td width=8>";
    frameText+="<input type='Button' value='&lt;&lt;' width='8' height='11' border='0'  onclick=\"getPreviousYear(\'"+ calendar.id +"\', \'" + sInputBoxID + "\'," + iYear + "," + iMth + "," + iDay + ")\">";
    frameText+="</td>\n";
    frameText+="<td vAlign=middle align='center'>";
    frameText+="<select bgColor=\'"+ sYearMthBgColor +"\' fontColor="+ sYearMthFontColor +" id='"+calendar.id+"SelectYear' name='"+calendar.id+"SelectYear' onClick=\"isHid=false;\" onChange=\"showSelectYear( \'"+sInputBoxID + "\',\'"+ calendar.id +"\', window.document.all."+calendar.id+"SelectYear ," + iMth + " , "+ iDay +" )\">";
	  frameText+=initSelectYearOption( iYear );
	  frameText+="</select>";
    frameText+="</td>\n";
    frameText+="<td width=8>";
    frameText+="<input type='Button' value='&gt;&gt;' width='8' height='11' border='0'  onclick=\"getNextYear(\'"+ calendar.id +"\', \'"+ sInputBoxID + "\'," + iYear + "," + iMth + "," + iDay + ")\">";
    frameText+="</td>\n";
    frameText+="<td width=8>";
    frameText+="<input type='Button' value='&lt;' width='8' height='11' border='0'  onclick=\"getPreviousMonth(\'"+ calendar.id +"\', \'"+sInputBoxID+"\'," + iYear + "," + iMth + "," + iDay + ")\">";
    frameText+="</td>\n";
    frameText+="<td vAlign=middle align='center' width='50'>";
	  frameText+="<select bgColor=\'"+ sYearMthBgColor +"\' fontColor="+ sYearMthFontColor +" id='"+calendar.id+"SelectMonth' name='"+calendar.id+"SelectMonth' onClick=\"isHid=false;\" onChange=\"showSelectMth( \'"+sInputBoxID + "\',\'"+ calendar.id +"\'," + iYear + ", window.document.all."+calendar.id+"SelectMonth , " + iDay +" )\">";
	  frameText+=initSelectMthOption( iMth );
	  frameText+="</select>";
    frameText+="</td>\n";
    frameText+="<td width=8>";
    frameText+="<input type='Button' value='&gt;' width='8' height='11' border='0'  onclick=\"getNextMonth(\'"+ calendar.id +"\', \'" + sInputBoxID + "\'," + iYear + "," + iMth + "," + iDay + ")\">";
    frameText+="</td>"+"\n";
    frameText+="</tr>"+"\n";
    frameText+="</table>"+"\n";
    frameText+="<table onselectstart=\"return false;\" border='0' cellpadding='0' cellspacing='1' width='100%' bgcolor='"+sTabDateBgColor+"'>"+"\n";
    frameText+="<tr bgcolor="+ sTabTrWeekBgColor +">"+"\n";
    frameText+="<td><center><font color="+sSundayFontColor+">Sun</font></center></td>"+"\n";
    frameText+="<td><center>Mon</center></td>"+"\n";
    frameText+="<td><center>Tue</center></td>"+"\n";
    frameText+="<td><center>Wed</center></td>"+"\n";
    frameText+="<td><center>Thu</center></td>"+"\n";
    frameText+="<td><center>Fri</center></td>"+"\n";
    frameText+="<td><center><font color="+sSaturdayFontColor+">Sat</center></td>"+"\n";
    frameText+="</tr>"+"\n";
    var arrDaysLength = arrAllDays.length;
    var sValue = 0;
    for( var i=0 ; i < arrDaysLength ; i+=7 )
    {
        frameText+="<tr bgcolor="+sTabTrDateBgColor+">"+"\n";
        for( var iCol = 0 ; iCol < 7 ; iCol++ )
        {
            sValue = arrAllDays[ i + iCol ];
            if ( sValue == 0 ) // Previous or Next Month Day.
            {
                frameText+= "<td width=35><center><font color="+sTabTdPrevAndNextFontColor+">"+"."+"</font></center></td>"+"\n"
                continue;
            }
            sValue = sValue - 0;
            if ( sValue == iDay ) // selected Date
            {
                frameText+="<td width=35 style=\"background:"+sSelectedBgColor+"\" onClick=\"outputValue(\'"+sInputBoxID+"\',"+ iYear +","+ iMth +","+ sValue +");closeCalendar( \'" + calendar.id + "\');\"><center><font color='#FFFFFF'>"+sValue+"</font></center></td>"+"\n";
                continue;
            }
            // current month not select days.
            if ( iCol == 0 ) // Sunday
            {
                frameText+="<td width=35 onMouseOver=\"this.style.background=\'"+ sTabTdMouseOverColor +"\'\" onMouseOut=\"this.style.background=\'"+sTabTdMouseOutColor+"\'\" onClick=\"outputValue(\'"+sInputBoxID+"\',"+ iYear +","+ iMth +","+ sValue +");closeCalendar(\'" + calendar.id + "\');\"><center><font color="+sSundayFontColor+">"+sValue+"</font></center></td>"+"\n";
                continue;
            }
            if ( iCol == 6 ) // Saturday
            {
                frameText+="<td width=35 onMouseOver=\"this.style.background=\'"+ sTabTdMouseOverColor+"\'\" onMouseOut=\"this.style.background=\'"+sTabTdMouseOutColor+"\'\" onClick=\"outputValue(\'"+sInputBoxID+"\',"+ iYear +","+ iMth +","+ sValue +");closeCalendar(\'" + calendar.id + "\');\"><center><font color="+ sSaturdayFontColor+">"+sValue+"</font></center></td>"+"\n";
                continue;
            }
            frameText+="<td width=35 onMouseOver=\"this.style.background=\'"+sTabTdMouseOverColor+"\'\" onMouseOut=\"this.style.background=\'"+sTabTdMouseOutColor+"\'\" onClick=\"outputValue(\'"+sInputBoxID+"\',"+ iYear +","+ iMth +","+ sValue +");closeCalendar(\'" + calendar.id + "\');\"><center><font color=\'"+sNotSelectedFontColor+"\'>"+sValue+"</font></center></td>"+"\n";
        }
    }
    frameText+="</tr>"+"\n";
    frameText+="</table>"+"\n";
    frameText+="<table onselectstart=\"return false;\" cellpadding='0' cellspacing='0' bgcolor="+ sTabTodayBgColor +" width='100%' height='15'>"+"\n<tr>\n";
    dToday = getToday();
    var iTodayYear = dToday.getFullYear();
    var iTodayMth = dToday.getMonth();
    var iTodayDay = dToday.getDate();
    var sToday = getDateFormat( dToday );
    frameText+="<td width=\'" + iCalendarWidth + "\' title=\"Today: "+ sToday+"\" onclick=\"outputValue(\'"+sInputBoxID+ "\'," + iTodayYear + "," + iTodayMth + "," + iTodayDay +");closeCalendar(\'" + calendar.id + "\');\">";
    frameText+="<font color="+sTabTodayLableFontColor+">Today: </font>"+sToday;
    frameText+="</td>\n";
    frameText+="<td>";
    frameText+="<input type='Button' width='100%' border='0' value='x'  onclick=\"closeCalendar(\'" + calendar.id + "\');\">";
    frameText+="</td>\n";
    frameText+="</tr>\n";
    calendar.innerHTML = frameText;
    calendar.style.display="";
    
    bkFrame = document.getElementById( sCalendarID + sIframePostfix );  
    bkFrame.style.display = "block";
}

function addcalendarDisplay( sCalendarID )
{
    var iArrLength = arrDisplayedCalendars.length;
    if ( iArrLength ==0 )
    {
        arrDisplayedCalendars.push( sCalendarID );
        return;
    }
    var sValue;
    for( var i=0 ; i < iArrLength ; i++ )
    {
        sValue = arrDisplayedCalendars[ i ];
        if ( sValue == sCalendarID )
        {
            return;
        }
    }
    arrDisplayedCalendars.push( sCalendarID );
}

function removeCalendarDisplay( sCalendarID )
{
    var iArrLength = arrDisplayedCalendars.length;
    if ( iArrLength == 0 )
    {
        return;
    }
    var sValue;
    for( var i=0 ; i < iArrLength ; i++ )
    {
        sValue = arrDisplayedCalendars[ i ];
        if ( sValue == sCalendarID )
        {
            arrDisplayedCalendars.splice( i , 1 );
            return;
        }
    }
}

function outputValue( sInputBoxID , iYear , iMth , iDay )
{
    inputBox = document.getElementById( sInputBoxID );
    dCurrDate = new Date( iYear , iMth , iDay );
    inputBox.value = getDateFormat( dCurrDate );
}

function closeCalendar( sCalendarID )
{
    var calendar = document.getElementById( sCalendarID );
    calendar.style.display="none";
    removeCalendarDisplay( sCalendarID );
    var bkFrame = document.getElementById( (sCalendarID + sIframePostfix) );
    bkFrame.style.display = "none";
}

function closeAllDisplayedCalendar()
{
    if (isHid)
    {
        var iArrLength = arrDisplayedCalendars.length;
        if ( iArrLength == 0 )
        {
            return;
        }
        var sValue;
        for ( var i=0 ; i < iArrLength ; i++ )
        {
            sValue = arrDisplayedCalendars[ i ];
            var calendar = document.getElementById( sValue );
            calendar.style.display = "none";
            arrDisplayedCalendars.splice( i , 1 );
            var bkFrame = document.getElementById( (sValue + sIframePostfix) );
            bkFrame.style.display = "none";
        }
    }
    isHid = true;
}

function showSelectMth( sInputBoxID , sCalendarID , iYear , selectMth ,iDay )
{
    var iSelectMth = selectMth.options[ selectMth.selectedIndex ].value ;
  	dSelect = getFitDate( iYear , ( iSelectMth - 1) , iDay );
  	displayCalendarFrame( sCalendarID , sInputBoxID , dSelect );
  	isHid = true;
}

function showSelectYear( sInputBoxID , sCalendarID , selectYear , iMth ,iDay )
{
    var iSelectYear = selectYear.options[ selectYear.selectedIndex ].value ;
  	dSelect = getFitDate( iSelectYear , iMth , iDay );
  	displayCalendarFrame( sCalendarID , sInputBoxID , dSelect );
  	isHid = true;
}

function initSelectMthOption( iMth )
{
    var sCode;
    var iActualMth = iMth + 1;
    for ( var i=1 ; i<=12 ; i++ )
    {
        if ( i == iActualMth )
        {
            sCode += '<option value='+ i +' selected>'+i;
        }
        else
        {
            sCode += '<option value='+ i +'>'+i;
        }
    }
    return sCode;
}

function initSelectYearOption( iYear )
{
    var sCode ;
    for ( var i= iMinYear ; i <= iMaxYear ; i++ )
    {
        if ( i ==iYear )
        {
            sCode += '<option value='+ i +' selected>'+i;
        }
        else
        {
            sCode += '<option value='+ i +'>'+i;
        }
    }
    return sCode;
}
// click other side of current web page will close calendar.
document.onclick = closeAllDisplayedCalendar;

