var css_HIDE="FONT-SIZE=10pt;CURSOR:hand;PADDING-TOP:2px;HEIGHT:22px;";
var css_Span,css_Filter;
var StartColor,EndColor,CurrentColor,HightColor,WeekBackColor,GridColor,BorderColor;
function popCalendar(obj,colorStyle)
{	
	if (window.Calendar==undefined) Create();
	setColor(colorStyle);
	try{
		var Da;
		if (isDate(obj.value)==false)
			{	
				Da=new Date();
				if (obj.value!="") obj.value=getDate(Da);
			}
			else
			{	
				Da=new Date(obj.value.replace("-","/"));
			}
		Calendar.value=getDate(Da);
	}
	catch(e)
	{
		alert("错误的日期格式!")
		var Da=new Date();
	}
	Write(Da.getFullYear(),Da.getMonth()+1);
	Calendar.obj=obj;
	setPosition(obj);
	return false;
}
 //set position
 function setPosition(e)
		{
		var t=e.offsetTop+e.offsetHeight;
		var l=e.offsetLeft;
		var obj=e;
		while(e=e.offsetParent){
		t+=e.offsetTop;
		l+=e.offsetLeft;
		}
		if (l>(document.body.offsetWidth-Calendar.width+document.body.scrollLeft)&&(l-Calendar.width+obj.offsetWidth+22-document.body.scrollLeft)>=0){l-=(Calendar.width-obj.offsetWidth-22)}
		if (t>(document.body.offsetHeight-Calendar.height+document.body.scrollTop)&&(t-Calendar.height-obj.offsetHeight-document.body.scrollTop)>=0){t-=(Calendar.height+obj.offsetHeight);}else{t+=5;}
		Calendar.style.top=t;
		Calendar.style.left=l;
		window.Calendar.style.display="";
		Calendar.focus();
		Calendar.onblur=function(){if(document.activeElement!=this) Calendar.style.display='none'; else this.focus()}
		}
//set Color Style
function setColor(e)
{	eval(e);
	Calendar.Hweek.bgColor=WeekBackColor;
	Calendar.TT.borderColor=GridColor;
	Calendar.border.style.borderColor=BorderColor;
	css_Span="<SPAN style='WIDTH:100%;HEIGHT=100%;FONT-SIZE:10pt;FONT-WEIGHT: bold;COLOR:"+HightColor+";BORDER-COLOR: "+HightColor+";BORDER-WIDTH: 2px;BORDER-STYLE: dotted; POSITION: static'>";
	css_Filter="progid:dximagetransform.microsoft.gradient(gradienttype=0, StartColorstr="+StartColor+", EndColorstr="+EndColor+");";
	Calendar.head.cells[0].style.filter=css_Filter;
	Calendar.head.cells[1].style.filter=css_Filter;
	Calendar.head.cells[2].style.filter=css_Filter;
 }
//Create
function Create()
{
window.Calendar=document.createElement("DIV");
document.body.insertBefore(Calendar);
Calendar.width=310;
Calendar.height=170;
with(Calendar)
{
	borderColor="black";
	style.cssText="Z-INDEX:2000;display='none';position: absolute;width: 310px;height: 170px;";
	innerHTML="<iframe width=100% height=100% scrolling=no frameborder=0></iframe>"
}
//border
Calendar.border=document.createElement("DIV");
Calendar.insertAdjacentElement("AfterBegin",Calendar.border);
with(Calendar.border)
{
	borderColor="black";
	style.cssText="position: absolute;top:0px;left:0px;width:100%;height: 100%;";
	style.borderWidth="1px";
	style.borderStyle="solid";
}
//body
Calendar.TT=document.createElement("Table");
Calendar.border.insertAdjacentElement("AfterBegin",Calendar.TT);
	with(Calendar)
	{	
		TT.style.cssText="FONT-SIZE:9pt;width:100%;height:100%;tableLayout:fixed;position:absolute;top:0px,left:0px;border-width:1px;border-style:solid;border-collapse:collapse";
		TT.border=1;
		TT.cellSpacing="0";
		TT.cellPadding="0";
		TT.onselectstart=function(){return false}
		//Head
		Calendar.head=TT.insertRow();
		myTr=Calendar.head;
		myTr.style.height="25px";
		myTd=myTr.insertCell();
		myTd.align="center";
		myTd.style.fontFamily="webdings";
		myTd.style.borderRightWidth=0;
		myTd.onmouseover=function(){Calendar.onblur=""}
		myTd.onmouseout=function(){setFocus()}
		myTd.innerHTML="<A hideFocus=true title='上一年' href='javaScript:setYear(-1)')><Font color='black'>9</Font></A>&nbsp<A hideFocus=true title='上一月' href='javaScript:setMonth(-1)'><Font color='black'>7</Font></A>";
		//Center
		myTd=myTr.insertCell();
		myTd.align="center";
		myTd.valign="middle";
		//Select Year
		Calendar.Syear=document.createElement("SPAN");
		Calendar.Syear.onmouseover=function(){this.style.color=HightColor;this.style.textDecoration="underline"}
		Calendar.Syear.onmouseout=function(){this.style.color="black";this.style.textDecoration=""}
		Calendar.Syear.style.cssText=css_HIDE;
		Calendar.Syear.onclick=function(){this.style.display='none';Calendar.Dyear.style.display="";Calendar.onblur="";Calendar.Dyear.focus()}
		Calendar.Syear.style.width=67;
		myTd.insertBefore(Calendar.Syear);
		Calendar.Dyear=document.createElement("Select")
		for (var i=2058;i>=1949;i--)
		{	
			myop=new Option();
			myop.value=i;
			myop.text=i+"年";
			Calendar.Dyear.options.add(myop);
		}
		Calendar.Dyear.style.display='none';
		Calendar.Dyear.onblur=function(){this.style.display='none';	Calendar.Syear.style.display="";if (inRect(event.x,event.y)==false) Calendar.style.display='none'}
		Calendar.Dyear.onchange=function(){Write(Calendar.Dyear.value,Calendar.Dmonth.value)};
		myTd.insertBefore(Calendar.Dyear);
		//Select Month
		Calendar.Smonth=document.createElement("SPAN");
		Calendar.Smonth.onmouseover=function(){this.style.color=HightColor;this.style.textDecoration="underline"}
		Calendar.Smonth.onmouseout=function(){this.style.color="black";this.style.textDecoration=""}
		Calendar.Smonth.style.cssText=css_HIDE; 
		Calendar.Smonth.onclick=function(){this.style.display='none';Calendar.Dmonth.style.display="";Calendar.onblur="";Calendar.Dmonth.focus()}
		Calendar.Smonth.style.width=53;
		myTd.insertBefore(Calendar.Smonth);
		Calendar.Dmonth=document.createElement("Select")
		for (var i=1;i<=12;i++)
		{	
			myop=new Option();
			myop.value=i;
			myop.text=i+"月";
			Calendar.Dmonth.options.add(myop);
		}
		Calendar.Dmonth.style.display='none';
		Calendar.Dmonth.onblur=function(){this.style.display='none';Calendar.Smonth.style.display="";if (inRect(event.x,event.y)==false) Calendar.style.display='none'}
		Calendar.Dmonth.onchange=function(){Write(Calendar.Dyear.value,Calendar.Dmonth.value)};
		myTd.insertBefore(Calendar.Dmonth);
		myTd.style.borderLeftWidth=0;
		myTd.style.borderRightWidth=0;
		myTd.style.fontWeight="bold";
		myTd.onmouseover=function(){Calendar.onblur=""}
		myTd.onmouseout=function(){if (document.activeElement!=Calendar.Dyear && document.activeElement!=Calendar.Dmonth) setFocus()}
		myTd.colSpan=5;
		
		myTd=myTr.insertCell();
		myTd.align="center";
		myTd.style.borderLeftWidth=0;
		myTd.style.fontFamily="webdings";
		myTd.onmouseover=function(){Calendar.onblur=""}
		myTd.onmouseout=function(){setFocus()}
		myTd.innerHTML="<A hideFocus=true title='下一月' href='javaScript:setMonth(1)'><Font color='black'>8</Font></A>&nbsp<A  hideFocus=true title='下一年' href='javaScript:setYear(1)'><Font color='black'>:</Font></A>";
		
		//Week
		var week=new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
		Calendar.Hweek=TT.insertRow();
		myTr=Calendar.Hweek;
		for (var i=0;i<=6;i++)
		{
			myTd=myTr.insertCell();
			myTd.align="center";
			myTd.vAling="middle";
			myTd.onmouseover=function(){Calendar.onblur=""}
			myTd.onmouseout=function(){setFocus()}
			myTd.innerText=week[i];
		}
		
		for (var i=1;i<=6;i++)
		{
		myTr=TT.insertRow();
			for (var ii=1;ii<=7;ii++)
				{
					myTd=myTr.insertCell();
					myTd.align="center";
					myTd.vAlign="middle";
					myTd.style.cursor="hand";
					myTd.style.height="20px";
					myTd.onmouseover=function(){this.bgColor=CurrentColor;Calendar.onblur=""}
					myTd.onmouseout=function(){this.bgColor="white";setFocus()}
					myTd.onclick=function(){_click()};	
				}
		}
	}
}
//Set focus
function setFocus()
	{
	if (Calendar.style.display=="" )
		{	
			Calendar.focus();
			Calendar.onblur=function(){Calendar.style.display='none'}
		}	
	}
//click
function _click()
	{	var myTd=getParent(event.srcElement,"TD");
		setStyle(myTd);
		myTd.bgColor="white";
		Calendar.obj.value=getValue(myTd);
		Calendar.style.display='none';
		Calendar.obj.fireEvent("onchange");
	}
//set Style
function setStyle(myTd)
	{
	if (Calendar.current!=undefined) 
		{
			with (Calendar.current)
				{	
					if(value.substr(0,1)=="+" || value.substr(0,1)=="-") 
						innerText=value.substr(1); 
					else 
						innerText=value;
				}					
		}
		myTd.innerHTML=css_Span+myTd.innerHTML+"</SPAN>";
		Calendar.current=myTd;
	}
//get Value
function getValue(myTd)
	{
	switch(myTd.value.toString().substr(0,1))
		{
			case "-":
				var Da=new Date(Calendar.year,Calendar.month-1 ,1);
				Da.setDate(-0);
				return Da.getFullYear()+"-"+(Da.getMonth()+1)+"-"+myTd.value.substr(1);
			break;
			case "+" :
				var Da=new Date(Calendar.year,Calendar.month-1,1);
				Da.setDate(+32);
				return Da.getFullYear()+"-"+(Da.getMonth()+1)+"-"+myTd.value.substr(1);
			break;
			default:
				return Calendar.year+"-"+Calendar.month+"-"+myTd.value;
					
		}	
	}
//next year pre year
function setYear(N)
{	
	if (Calendar.year+N<1000 || Calendar.year+N>9999){return}
	Write(Calendar.year+N,Calendar.month);	
}
//next month pre month
function setMonth(N)
{	var Da=new Date(Calendar.year,Calendar.month-1,1);
	if (N==-1) Da.setDate(-0);else Da.setDate(+32);
	Write(Da.getFullYear(),Da.getMonth()+1);
}
//fill cell day
function Write(year,month)
{	
	var Da=new Date(year,month-1,1);
	var N=Da.getDay();
	var myTT=Calendar.TT;
	var D=1;
	var fColor="black";
	var sN="";//记录值 来区分当前月还是上一个月
	Calendar.year=parseInt(year);
	Calendar.month=parseInt(month);
	Calendar.Dyear.value=year;
	Calendar.Syear.innerText=year+"    年";
	Calendar.Smonth.innerText=month+"    月";
	Calendar.Dmonth.value=month;
	for (var i=2;i<=7;i++)
	{
		for (var ii=N;ii<=6;ii++)
		{
			myTT.rows[i].cells[ii].value=sN+D;
			//是否为今天
			V=getValue(myTT.rows[i].cells[ii]);
			if (V==getDate(new Date()))
					myTT.rows[i].cells[ii].style.color="red";
			else
					myTT.rows[i].cells[ii].style.color=fColor;
			//是否为当前时间
			if (V==Calendar.value)
				{
					myTT.rows[i].cells[ii].innerHTML=css_Span+D+"</SPAN>";
					Calendar.current=myTT.rows[i].cells[ii];
				}
			else
				myTT.rows[i].cells[ii].innerText=D;
			
			if (D==getLastDay(year,month)) {D=1;fColor="Gray";sN="+"} else D++;
		}
	N=0;
	}
	//上一个月在本页显示的几天
	N=Da.getDay()-1;
	Da.setDate(-0);
	year=Da.getYear();
	month=Da.getMonth();
	for (var i=N;i>=0;i--)
		{	D=(getLastDay(year,month+1)+(i-N))
			myTT.rows[2].cells[i].value="-"+D
				//是否为今天
			V=getValue(myTT.rows[2].cells[i]);
			if (V==getDate(new Date()))
					myTT.rows[2].cells[i].style.color="red";
			else
					myTT.rows[2].cells[i].style.color=fColor;
			//是否为当前时间
			if (V==Calendar.value)
				{	
					myTT.rows[2].cells[i].innerHTML=css_Span+D+"</SPAN>";
					Calendar.current=myTT.rows[2].cells[i];
				}
			else
				myTT.rows[2].cells[i].innerText=D;
		}
}
// get month last Day
function getLastDay(year,month)
{	var Da=new Date(year,month,1);
	Da.setDate(-0);
	return Da.getDate();
	
}
//get parent
function getParent(myElement,myTagName){
	if (myElement.tagName==myTagName){
			return myElement;
		}
	else{return getParent(myElement.parentElement,myTagName)}
}
//get date string
function getDate(date)
{	var S=date.getFullYear();
	 S+="-"+(date.getMonth()+1).toString();
	 S+="-"+date.getDate();
	return S;
}
//date data validate
function isDate(dateVar) {
    function GetFullYear(year) {
        return (year + parseInt(2000)) - ((year < 2029) ? 0 : 100);
    }
    var  m;
    var yearFirstExp = new RegExp("^\\s*((\\d{4})|(\\d{2}))([-./])(\\d{1,2})\\4(\\d{1,2})\\s*$");
        m = dateVar.match(yearFirstExp);
        var day, month, year;
        if (m != null) {
            day = m[6];
            month = m[5];
            year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10))
        }
        else {
              return false;          
        }
        month -= 1;
        var date = new Date(year, month, day);
        return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) ? true : false;
  }
 //OBJ value is Date
 function validateDate()
 {
	var src=event.srcElement;
		if (isDate(src.value)==false && src.value!="")
		{
			alert("错误的日期格式!");
			Da=new Date();
			src.value=getDate(Da);
			src.focus();
			src.select();
			return true;
		}
}
	
//a point in the Calendar rect
function inRect(x,y)
{
	var rect=Calendar.getBoundingClientRect();
	x+=2;
	y+=2;
	if (x>=rect.left && x<=rect.right && y>=rect.top && y<=rect.bottom)
	{
	return true;
	}
	else
	{
	return false;
	}

}


