﻿//检测是否为IE浏览器?
var ie = false;
if((navigator.userAgent.toLowerCase().indexOf("opera") == -1) && (navigator.userAgent.toLowerCase().indexOf("msie") != -1)){
   ie = true;
}
var oDiv;

function hideElement( elmID, overDiv ){
      if( ie )
      {
        for( i = 0; i < document.all.tags( elmID ).length; i++ )
        {
          obj = document.all.tags( elmID )[i];
          if( !obj || !obj.offsetParent || obj.id=='oCalendarEnoMonth' || obj.id=='oCalendarEnoYear' || obj.id=='oCalendarChsoMonth' || obj.id=='oCalendarChsoYear')
          {
            continue;
          }
      
          // Find the element's offsetTop and offsetLeft relative to the BODY tag.
          objLeft   = obj.offsetLeft;
          objTop    = obj.offsetTop;
          objParent = obj.offsetParent;
          
          while( objParent.tagName.toUpperCase() != "BODY" )
          {
            objLeft  += objParent.offsetLeft;
            objTop   += objParent.offsetTop;
            objParent = objParent.offsetParent;
          }
      
          objHeight = obj.offsetHeight;
          objWidth = obj.offsetWidth;
      
          if(( overDiv.offsetLeft + overDiv.offsetWidth ) <= objLeft );
          else if(( overDiv.offsetTop + overDiv.offsetHeight ) <= objTop );
          else if( overDiv.offsetTop >= ( objTop + objHeight ));
          else if( overDiv.offsetLeft >= ( objLeft + objWidth ));
          else
          {
            obj.style.visibility = "hidden";
          }
        }
      }
}

 function showElement( elmID )
    {
      if( ie )
      {
        for( i = 0; i < document.all.tags( elmID ).length; i++ )
        {
          obj = document.all.tags( elmID )[i];
          
          if( !obj || !obj.offsetParent )
          {
            continue;
          }
        
          obj.style.visibility = "";
        }
      }
    }
	
function PopupCalendar(InstanceName)
{
	///Global Tag
	this.instanceName=InstanceName;
	///Properties
	this.separator="-"
	this.oBtnTodayTitle="Today"
	this.oBtnCancelTitle="Cancel"
	this.oBtnCleanTitle="清空"
	this.weekDaySting=new Array("S","M","T","W","T","F","S");
	this.monthSting=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	this.Width=220+'px';
	this.currDate=new Date();
	this.today=new Date();
	this.startYear=1970;
	this.endYear=2020;
	///Css
	this.divBorderCss="1px solid #666666";
	this.tableBorderColor="#CCCCCC"
	///Method
	this.Init=CalendarInit;
	this.Fill=CalendarFill;
	this.Refresh=CalendarRefresh;
	this.Restore=CalendarRestore;
	///HTMLObject
	this.oTaget=null;
	this.oPreviousCell=null;
	this.sDIVID=InstanceName+"oDiv";
	this.sTABLEID=InstanceName+"oTable";
	this.sMONTHID=InstanceName+"oMonth";
	this.sYEARID=InstanceName+"oYear";
}
function CalendarInit()				///Create panel
{
	var sMonth,sYear
	sMonth=this.currDate.getMonth();
	sYear=this.currDate.getYear();
	htmlAll="<div id='"+this.sDIVID+"' style='z-index:99999;display:none;position:absolute;width:"+this.Width+";border:"+this.divBorderCss+";padding:2px;background-color:#FFFFFF'>";
	htmlAll+="<div align='center'>";
	/// Month
	htmloMonth="<select id='"+this.sMONTHID+"' onchange=CalendarMonthChange("+this.instanceName+") style='width:50%'>";
	for(i=0;i<12;i++)
	{			
		htmloMonth+="<option value='"+i+"'>"+this.monthSting[i]+"</option>";
	}
	htmloMonth+="</select>";
	/// Year
	htmloYear="<select id='"+this.sYEARID+"' onchange=CalendarYearChange("+this.instanceName+") style='width:50%'>";
	for(i=this.startYear;i<=this.endYear;i++)
	{
		htmloYear+="<option value='"+i+"'>"+i+"</option>";
	}
	htmloYear+="</select></div>";
	/// Day
	htmloDayTable="<table id='"+this.sTABLEID+"' width='220' border=0 cellpadding=2 cellspacing=1 bgcolor='"+this.tableBorderColor+"'>";
	htmloDayTable+="<tbody bgcolor='#ffffff'style='font-size:13px'>";
	for(i=0;i<=6;i++)
	{
		if(i==0)
			htmloDayTable+="<tr bgcolor='#98B8CD'>";
		else
			htmloDayTable+="<tr>";
		for(j=0;j<7;j++)
		{
			if(i==0)
			{
				htmloDayTable+="<td height='20' align='center' valign='middle' style='cursor:pointer'>";
				htmloDayTable+=this.weekDaySting[j]+"</td>"
			}
			else
			{
				htmloDayTable+="<td height='20' align='center' valign='middle' style='cursor:pointer'";
				if(ie){
					htmloDayTable+=" onmouseover=CalendarCellsMsOver("+this.instanceName+")";
					htmloDayTable+=" onmouseout=CalendarCellsMsOut("+this.instanceName+")";
				}
				htmloDayTable+=" onclick=CalendarCellsClick(this,"+this.instanceName+")>";
				htmloDayTable+="&nbsp;</td>"
			}
		}
		htmloDayTable+="</tr>";	
	}
	htmloDayTable+="</tbody></table>";
	/// Today Button
	htmloButton="<div align='center' style='padding:3px'>"
	htmloButton+="<button style='width:30%;height:20px;padding:2px 0px 0px 0px;border:1px solid #BCD0DE;background-color:#eeeeee;cursor:pointer'"
	htmloButton+=" onclick=CalendarTodayClick("+this.instanceName+")>"+this.oBtnTodayTitle+"</button>&nbsp;"
	htmloButton+="<button style='width:30%;height:20px;padding:2px 0px 0px 0px;border:1px solid #BCD0DE;background-color:#eeeeee;cursor:pointer'"
	htmloButton+=" onclick=Clean("+this.instanceName+")>"+this.oBtnCleanTitle+"</button>&nbsp;"
	htmloButton+="<button style='width:30%;height:20px;padding:2px 0px 0px 0px;border:1px solid #BCD0DE;background-color:#eeeeee;cursor:pointer'"
	htmloButton+=" onclick=CalendarCancel("+this.instanceName+")>"+this.oBtnCancelTitle+"</button> "
	htmloButton+="</div>"
	/// All
	htmlAll=htmlAll+htmloMonth+htmloYear+htmloDayTable+htmloButton+"</div>";
	document.write(htmlAll);
	this.Fill();
}
function CalendarFill()			///
{
	var sMonth,sYear,sWeekDay,sToday,oTable,currRow,MaxDay,sDaySn,sIndex,rowIndex,cellIndex,oSelectMonth,oSelectYear
	sMonth=this.currDate.getMonth();
	sYear=this.currDate.getYear();
	sWeekDay=(new Date(sYear,sMonth,1)).getDay();
	sToday=this.currDate.getDate();
	//对FireFox获取年有误,进行修改
	if(!ie){
		sYear+=1900;
	}	
	//alert(this.sTABLEID);
	oTable=document.getElementById(this.sTABLEID);  
	currRow=oTable.rows[1];
	MaxDay=CalendarGetMaxDay(sYear,sMonth);
	oSelectMonth=document.getElementById(this.sMONTHID); 
	oSelectMonth.selectedIndex=sMonth;
	oSelectYear=document.getElementById(this.sYEARID);  
	for(i=0;i<oSelectYear.length;i++)
	{
		if(parseInt(oSelectYear.options[i].value)==sYear)oSelectYear.selectedIndex=i;
	}
	////
	for(sDaySn=1,sIndex=sWeekDay;sIndex<=6;sDaySn++,sIndex++)
	{

		if(sDaySn==sToday)
		{
			currRow.cells[sIndex].innerHTML="<font color=red><b>"+sDaySn+"</b></font>";
			this.oPreviousCell=currRow.cells[sIndex];
		}
		else
		{
			currRow.cells[sIndex].innerHTML=sDaySn;
			currRow.cells[sIndex].style.color="#666666";	
		}
		CalendarCellSetCss(0,currRow.cells[sIndex]);
	}
	for(rowIndex=2;rowIndex<=6;rowIndex++)
	{
		if(sDaySn>MaxDay)break;
		currRow=oTable.rows[rowIndex];
		for(cellIndex=0;cellIndex<currRow.cells.length;cellIndex++)
		{
			if(sDaySn==sToday)
			{
				currRow.cells[cellIndex].innerHTML="<font color=red><b>"+sDaySn+"</b></font>";
				this.oPreviousCell=currRow.cells[cellIndex];
			}
			else
			{
				currRow.cells[cellIndex].innerHTML=sDaySn;	
				currRow.cells[cellIndex].style.color="#666666";	
			}
			CalendarCellSetCss(0,currRow.cells[cellIndex]);
			sDaySn++;
			if(sDaySn>MaxDay)break;	
		}
	}
}
function CalendarRestore()					/// Clear Data
{	
	var oTable
	oTable=document.getElementById(this.sTABLEID);
	for(i=1;i<oTable.rows.length;i++)
	{
		for(j=0;j<oTable.rows[i].cells.length;j++)
		{
			CalendarCellSetCss(0,oTable.rows[i].cells[j]);
			oTable.rows[i].cells[j].innerHTML="&nbsp;";
		}
	}	
}
function CalendarRefresh(newDate)					///
{
	this.currDate=newDate;
	this.Restore();	
	this.Fill();	
}
function CalendarCellsMsOver(oInstance)				/// Cell MouseOver
{
	var myCell
	myCell=event.srcElement;
	CalendarCellSetCss(0,oInstance.oPreviousCell);
	if(myCell)
	{
		CalendarCellSetCss(1,myCell);
		oInstance.oPreviousCell=myCell;
	}
}
function CalendarCellsMsOut(oInstance)				/// Cell MouseOut
{
	var myCell
	myCell=event.srcElement;
	CalendarCellSetCss(0,myCell);	
}
function CalendarCellsClick(oCell,oInstance)
{
	var sDay,sMonth,sYear,newDate
	sYear=oInstance.currDate.getFullYear();
	sMonth=oInstance.currDate.getMonth();
	sDay=oInstance.currDate.getDate();
	if(oCell.innerText!=" ")
	{
		//对FireFox获取年或天有误,进行修改
		if(!ie){
			var reg = /\d{1,2}/;
			var cHtml = oCell.innerHTML;
			sDay = parseInt(cHtml.match(reg));
			if(sYear<1000){
				sYear+=1900;
				//alert("year +1900");
			}
		}
		else{
			sDay=parseInt(oCell.innerText);
			//alert("is IE");
		}
		if(sDay!=oInstance.currDate.getDate())
		{
			//alert(sYear+"-"+sMonth+"-"+sDay);
			newDate=new Date(sYear,sMonth,sDay);
			oInstance.Refresh(newDate);
		}
	}
	sDateString=sYear+oInstance.separator+CalendarDblNum(sMonth+1)+oInstance.separator+CalendarDblNum(sDay);		///return sDateString
	if(oInstance.oTaget.tagName=="INPUT")
	{
		oInstance.oTaget.value=sDateString;
	}
	document.getElementById(oInstance.sDIVID).style.display="none";
	showElement( 'SELECT' );
	showElement( 'APPLET' );		
}
function CalendarYearChange(oInstance)				/// Year Change
{
	var sDay,sMonth,sYear,newDate
	sDay=oInstance.currDate.getDate();
	sMonth=oInstance.currDate.getMonth();
	sYear=document.getElementById(oInstance.sYEARID).value
	newDate=new Date(sYear,sMonth,sDay);
	oInstance.Refresh(newDate);
}
function CalendarMonthChange(oInstance)				/// Month Change
{
	var sDay,sMonth,sYear,newDate
	sDay=oInstance.currDate.getDate();
	sMonth=document.getElementById(oInstance.sMONTHID).value
	sYear=oInstance.currDate.getYear();
	//对FireFox获取年有误,进行修改
	if(!ie){
		sYear+=1900;
	}
	newDate=new Date(sYear,sMonth,sDay);
	oInstance.Refresh(newDate);	
}
function CalendarTodayClick(oInstance)				/// "Today" button Change
{	
	oInstance.Refresh(new Date());		
}
function getDateString(oInputSrc,oInstance)
{
	if(oInputSrc&&oInstance) 
	{
		oDiv = document.getElementById("oCalendarChsoDiv");
		CalendarDiv=oDiv;
		oInstance.oTaget=oInputSrc;
		CalendarDiv.style.left=CalendargetPos(oInputSrc,"Left");
		CalendarDiv.style.top=CalendargetPos(oInputSrc,"Top")+oInputSrc.offsetHeight;
		if(CalendarDiv.style.display=="none"){
			CalendarDiv.style.display="";
			hideElement( 'SELECT', document.getElementById("oCalendarChsoDiv") );
			hideElement( 'APPLET', document.getElementById("oCalendarChsoDiv") );
		}
		else{
			CalendarDiv.style.display="none";
			showElement( 'SELECT' );
			showElement( 'APPLET' );
		}
	}	
}
function CalendarCellSetCss(sMode,oCell)			/// Set Cell Css
{
	// sMode
	// 0: OnMouserOut 1: OnMouseOver 
	if(sMode)
	{
		//oCell.style.border="1px solid #5589AA";
		oCell.style.backgroundColor="#BCD0DE";
	}
	else
	{
		//oCell.style.border="1px solid #FFFFFF";
		oCell.style.backgroundColor="#FFFFFF";
	}	
}
function CalendarGetMaxDay(nowYear,nowMonth)			/// Get MaxDay of current month
{
	var nextMonth,nextYear,currDate,nextDate,theMaxDay
	nextMonth=nowMonth+1;
	if(nextMonth>11)
	{
		nextYear=nowYear+1;
		nextMonth=0;
	}
	else	
	{
		nextYear=nowYear;	
	}
	currDate=new Date(nowYear,nowMonth,1);
	nextDate=new Date(nextYear,nextMonth,1);
	theMaxDay=(nextDate-currDate)/(24*60*60*1000);
	return theMaxDay;
}
function CalendargetPos(el,ePro)				/// Get Absolute Position
{
	var ePos=0;
	while(el!=null)
	{		
		ePos+=el["offset"+ePro];
		el=el.offsetParent;
	}
	return ePos;
}
function CalendarDblNum(num)
{
	if(num<10) 
		return "0"+num;
	else
		return num;
}
function CalendarCancel(oInstance)			///Cancel
{
	CalendarDiv=oDiv;  //document.all[oInstance.sDIVID];
	CalendarDiv.style.display="none";
	showElement( 'SELECT' );
	showElement( 'APPLET' );
}
//清空日期框


function Clean(oInstance)
{
	//alert(oInstance.oTaget.name);
	if(oInstance.oTaget.tagName=="INPUT")
	{
		oInstance.oTaget.value='';
	}
	document.getElementById(oInstance.sDIVID).style.display="none";
	showElement( 'SELECT' );
	showElement( 'APPLET' );			
}
if(ie){
	//按ESC键关闭日历--IE模型
	document.onkeydown = function hide(){
		var oDiv = document.getElementById("oCalendarChsoDiv");
		if(event.keyCode==27){
			oDiv.style.display="none";	
			showElement( 'SELECT' );
			showElement( 'APPLET' );	
		}
	}
	//alert("IE模型下关闭");
}
else{
	//按ESC键关闭日历--W3C模型
	document.onkeydown = function hide(event){
		var oDiv = document.getElementById("oCalendarChsoDiv");
		if(event.keyCode==27){
			oDiv.style.display="none";		
			showElement( 'SELECT' );
			showElement( 'APPLET' );
		}
	}
	//alert("W3C模型下关闭");
}





var oCalendarEn=new PopupCalendar("oCalendarEn");
oCalendarEn.Init();
var oCalendarChs=new PopupCalendar("oCalendarChs");
oCalendarChs.weekDaySting=new Array("日","一","二","三","四","五","六");
oCalendarChs.monthSting=new Array("一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月");
oCalendarChs.oBtnTodayTitle="今天";
oCalendarChs.oBtnCancelTitle="取消";
oCalendarChs.Init();

//<!--
//点其他位置关闭日历


//document.onclick = function hide2(){
//	var oDiv = document.getElementById("oCalendarChsoDiv");
//	if(oShow==2){
//		oDiv.style.display="none";		
//		oShow = false;
//		oShow = 0;
//		//alert(oShow);
//	}
//	if(oShow==1){
//		oShow = 2;
//		//alert(oShow);
//	}
//}
//-->