
var cale_bg_color="#ffffff";
var cale_bgtitle_color="#9DD20C";
var cale_fonttitle_color="#dd0000";
var cale_font_color="#333333";

function caleTDMove(e){
	e.style.borderColor="#333333 #ffffff #ffffff #333333";
	e.style.background="#eeeeee ";
	
}
function caleTDOut(e){
	e.style.border="solid 1pt #c6c6c6"
	e.style.background="#ffffff ";
	}
function hideCale(){popCale.hide()}

function yearChange(){  //重写年份的<SELECT>
	var e=popCale.document.all('yearSel')
	e.options.length=0
	for (i=nYear-4;i<parseInt(nYear)+5;i++){
		e.options.length++
		e.options[e.options.length - 1].value=i
		e.options[e.options.length - 1].text=i
	}
	e.selectedIndex=4
}

function CaleClick(e){  //选定某一天时

	if(isNaN(parseInt(e.innerText))){	//如果是点击"今天"
		var d=new Date()
		tarObject.value=d.getFullYear()+'-'+(d.getMonth()+1)+'-'+d.getDate()
		e.style.borderColor="#848484"
	}
	else{
		tarObject.value=nYear+'-'+(parseInt(nMonth)+1)+'-'+e.innerText
		e.style.borderColor="#c6c6c6"
	}
	popCale.hide()
}

function changeYM(e,n){		//改变年份或月份﹐或点>>和<<时
	if (e.tagName=='SELECT'){
		if(e.value.length==4){nYear=e.value;yearChange()}
		else{nMonth=e.value}
	}
	else{nMonth=parseInt(nMonth)+n
		switch(nMonth){
		case 12:nYear++;nMonth=0;
			yearChange()
			popCale.document.all('monthSel').selectedIndex=0;break;
		case -1:nYear--;nMonth=11;
			yearChange()
			popCale.document.all('monthSel').selectedIndex=11;break;
		default:popCale.document.all('monthSel').selectedIndex=nMonth;break;
		}
	}
	rewriteCale()
}

function rewriteCale(){		//重写日历
	var newTb,newTR,newTD
	newTb=popCale.document.all('LiuzxCale').tBodies[0]
	for (i=0;i<=newTb.rows.length;i++)newTb.deleteRow(2)
	qtyDay=(new Date(nYear,parseInt(nMonth)+1,1) - new Date(nYear,nMonth,1))/24/3600/1000
	fDay=1-(new Date(nYear,nMonth,1).getDay())
	for (i=0;i<42;i++){
		if (i % 7==0)newTR=newTb.insertRow(newTb.rows.length - 2)
		newTD=newTR.insertCell()
		if (fDay>0 && fDay<=qtyDay)newTD.innerText=fDay
		newTD.style.border="solid 1pt #c6c6c6"
		newTD.align='center'
		newTD.onmouseover=Function("caleTDMove(this)")
		newTD.onmouseout=Function("caleTDOut(this)")
		newTD.onclick=Function("CaleClick(this)")
		fDay++
	}
}

function isDate(str){	//判断日期是否是合法的
	var reg = /^(\d{4})(-|\/)(0?[1-9]|1[0-2])(-|\/)(0?[1-9]|[12][0-9]|3[01])$/g; 
	var r = reg.exec(str); 
	if(r==null)return false;
	var d = new Date(r[1], r[3]-1,r[5]); 
	var newStr=d.getFullYear()+r[2]+(d.getMonth()+1)+r[2]+d.getDate()
	var reg=/(\/|-)(0*)( *)([1-9])/g
	var t=str.replace(reg,'$1$4')
	if (newStr==t)return true;
	return false;
}

function getDATE(s){	//设定那個控件加入日期
	tarObject=s
	if ((s.value!='')&&(isDate(s.value))){	//如果控件原來有日期并且是合法的
		var reg=/(\d{4})(-)(\d{1,2})(-)(\d{1,2})/
		var r=reg.exec(s.value);
		nYear=r[1];nMonth=r[3] - 1;
	}
	else{
		var d=new Date()
		nYear=d.getFullYear();nMonth=d.getMonth();
	}
	var e=event.srcElement
	popCale.show(-70,e.clientHeight+5,185,172,e)
	popCale.document.all('monthSel').selectedIndex=nMonth
	yearChange()
	rewriteCale()
}

//創建万年历
popCale = window.createPopup()
CaleBody = popCale.document.body
CaleBody.style.border="outset 1pt #cccccc"
CaleBody.style.fontSize = "9pt"
CaleBody.style.backgroundColor= "#c6c6c6"
CaleBody.style.cursor="hand"

var d=new Date()
var nYear=d.getFullYear()
var nMonth=d.getMonth()
var arMonth=new Array('一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月')
var qtyDay=(new Date(nYear,parseInt(nMonth)+1,1) - new Date(nYear,nMonth,1))/24/3600/1000
var strCale='<table bgcolor="'+cale_bg_color+'" id="LiuzxCale" ';
	strCale+='cellpadding="1"';
	strCale+=' cellspacing="0" style="font-size:9pt;height:170;cursor:hand;width:183">';
strCale+='<th bgcolor="'+cale_bg_color+'" style="color:'+cale_font_color+';"';
	strCale+=' onclick="parent.changeYM(this,-1)"><<';
strCale+='<th colspan=2 style="">';
strCale+='<select id="yearSel" onchange="parent.changeYM(this)" style="font:9pt;"></select>'
strCale+='<th colspan=3 style="">'
strCale+='<select id="monthSel" onchange="parent.changeYM(this)" style="font:9pt;">'
	for (i=0;i<12;i++){
		strCale+='<option value='+i+(i==nMonth+1?' selected':'')+'>'+arMonth[i]+'</option>'
	}
strCale+='</select><th bgcolor="'+cale_bg_color+'" style="color:'+cale_font_color+';"'
	strCale+=' onclick="parent.changeYM(this,1)">>></th>'
strCale+='<tr align=center style="color:'+cale_font_color+'" bgcolor="'+cale_bgtitle_color+'">'
strCale+='<td width=24 style="color:'+cale_fonttitle_color+'">日'
strCale+='<td width=24>一<td width=24>二<td width=24>三<td width=24>四<td width=24>五'
strCale+='<td width=24 style="color:'+cale_fonttitle_color+'">六</td></tr>'
var fDay=1-(new Date(nYear,nMonth,1).getDay())
for (i=0;i<42;i++){
	if (i % 7==0)strCale+='<tr align=center>'
	strCale+='<td style="" onmouseover="parent.caleTDMove(this)"'
		strCale+=' onmouseout="parent.caleTDOut(this)" onclick="parent.CaleClick(this)">'
		strCale+=(fDay>0&&fDay<=qtyDay?fDay:'')+'</td>'
	fDay++
}
strCale+='</tr>'
strCale+='<tr align=center bgcolor="'+cale_bgtitle_color+'">'
strCale+='<td >&nbsp;'
strCale+='<td colspan=2 onclick="parent.CaleClick(this)">今天'

strCale+='<td style="">&nbsp;'
strCale+='<td colspan=2 style=""'
	strCale+=' onclick="parent.hideCale();">关闭'

strCale+='<td style="">&nbsp;</td><tr>'
strCale+='</table>'
CaleBody.innerHTML=strCale
yearChange()
