/* =========================================================================

JavaScript Source File

NAME:  日期选择控件

AUTHOR:  方旭尘, 
DATE  : 2004-3-2

COMMENT: 

============================================================================ */

var oDiv = document.createElement("DIV");	//创建用来显示的层
var showLeft = 0;	//显示的左边界
var showTop = 0;	//显示的上边界
var showWidth = 0;	//显示的宽度
var showHeight = 0; //显示的高度
var oElement;	//用于显示依靠的对象
var formatDate="YYYY-MM-DD";	//日期显示方式
var objId = "calendar";	//显示的ID
var clickElement;	//调用得对象
var calElement;	//日期对象

var languageName="cn";	//内容显示的语言 value:"cn" "en"
var currentMonth,currentYear;	//当前月份,当前年份
var nowDate,nowMonth,nowYear;	 //今天的日期
var i=0;
var j=0;

d=new Date();
nowDate=d.getDate();
nowMonth=d.getMonth();
nowYear=d.getYear();

currentMonth=nowMonth;
currentYear=nowYear;

arrWeekCaption_cn = new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
arrWeekCaption_en = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
arrMonthCaption_cn= new Array("一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月");
arrMonthCaption_en= new Array("January","February","March","April","May","June","July","August","September","October","November","December");

var oPopup = window.createPopup();
var oPopupBody = oPopup.document.body;
var oPopupDocument = oPopup.document.all;

function MonthView(){}

//*********初始化控件*********
function MonthView.initialize(){
    
    output ='<table cellpadding=0 cellspacing=1 style="border: 1 solid black;width:300;cursor:default" id="tblMonthView" onselectstart="return false">';
    output+='   <tr>';
    output+='      <td>';
    output+='        <table width="100%" cellpadding=0 cellspacing=0>';
    output+='            <tr style="padding-top:6;padding-bottom:6;background:menu;" id="trCaption">';
    output+='				   <td width="5%">&nbsp;</td>';
	output+='				   <td width="10%"><div align="center" style="font-size:12;cursor:hand">&lt;&lt;</div></td>';
	output+='				   <td width="20%"><div align="center" style="font-size:12" id="spanCurrentYear">2004年</div></td>';
	output+='				   <td width="10%"><div align="center" style="font-size:12;cursor:hand">&gt;&gt;</div></td>';
	output+='				   <td width="10%">&nbsp;</td>';
	output+='				   <td width="10%"><div align="center" style="font-size:12;cursor:hand">&lt;&lt;</div></td>';
	output+='				   <td width="20%"><div align="center" style="font-size:12" id="spanCurrentMonth">三月</div></td>';
	output+='				   <td width="10%"><div align="center" style="font-size:12;cursor:hand">&gt;&gt;</div></td>';
	output+='				   <td width="5%">&nbsp;</td>';
	output+='            </tr>';
    output+='        </table>';
    output+='      </td>';
    output+='    </tr>';
    output+='    <tr>';
    output+='      <td>';
    output+='        <table width="100%" cellpadding=0 cellspacing=1 id="tblShowDay" style="font-size:12">';
    output+='            <tr align="center"><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>';
    output+='            <tr><td colspan=7 height="1" style="background:black" id="tdLine"></td></tr>';
    output+='            <tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>';
    output+='        </table>';
    output+='      </td>';
    output+='    </tr>';
    output+='    <tr>';
    output+='      <td>';
    output+='        <table width="100%" cellpadding=0 cellspacing=1 id="tblToday" style="font-size:12">';
    output+='            <tr><td width="20"></td><td></td></tr>';
    output+='        </table>';
    output+='      </td>';
    output+='    </tr>';
    output+='</table>';
    
    /*oDiv.innerHTML = output;
	oDiv.style.position = "absolute";
	oDiv.style.left = showLeft;
	oDiv.style.top = showTop;
	oDiv.id = objId;
	oDiv.onblur = MonthView.hide;
	document.body.appendChild(oDiv);
	calElement = document.getElementById(objId);
	clickElement = event.srcElement;
    MonthView.setCurrentCaption();
    MonthView.setWeekCaption();
    MonthView.bulidDay();
    MonthView.setShowHideToday(false);
	MonthView.setBodyBg("#F3EBD8");
	MonthView.setCaptionBg("#91BEC4");
	document.body.attachEvent('onmousedown',MonthView.mousedDown);*/
	oPopupBody.innerHTML = output;
	calElement = oPopupBody;
	MonthView.setCurrentCaption();
	MonthView.setWeekCaption();
	MonthView.bulidDay();
	showWidth = parseInt(oPopupDocument.tblMonthView.currentStyle.width);
	MonthView.setShowHideToday(false);
	MonthView.setBodyBg("#F3EBD8");
	MonthView.setCaptionBg("#91BEC4");
	oPopupDocument.trCaption.cells[1].onclick=MonthView.showCurrentDateYearPreview;
	oPopupDocument.trCaption.cells[3].onclick=MonthView.showCurrentDateYearNext;
	oPopupDocument.trCaption.cells[5].onclick=MonthView.showCurrentDateMonthPreview;
	oPopupDocument.trCaption.cells[7].onclick=MonthView.showCurrentDateMonthNext;
	oPopup.show(showLeft,showTop,showWidth,showHeight);
}

//*********当前年、月的显示*********
function MonthView.setCurrentCaption()
	{
	oPopupDocument.spanCurrentYear.innerHTML=currentYear+"年";
	if (languageName=="cn")
		oPopupDocument.spanCurrentMonth.innerHTML=arrMonthCaption_cn[currentMonth];
	else
		oPopupDocument.spanCurrentMonth.innerHTML=arrMonthCaption_en[currentMonth];
	}
    
//*********星期表头的显示*********
function MonthView.setWeekCaption()
	{
	if (languageName=="cn")
		arrCaption=arrWeekCaption_cn;
	else
		arrCaption = arrWeekCaption_en;
	for ( i=0; i<arrCaption.length; i++)
		oPopupDocument.tblShowDay.rows[0].cells[i].innerHTML=arrCaption[i];
	}	

//*********设置每月的日期*********
function MonthView.bulidDay()
	{
    var rows = 1;
    arrMonthCount=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
    if ((currentYear % 400 ==0) || ((currentYear % 100!=0)&&(currentYear % 4==0)))
        arrMonthCount[1]=29;
    
	rowDay=2;
    
	//初始化表格tblShowDay,每次开始的时候只显示2行
	while (oPopupDocument.tblShowDay.rows.length>rowDay)
        oPopupDocument.tblShowDay.deleteRow(rowDay);
    
    firstDay=new Date(currentYear,currentMonth,1);
    tempRow=oPopupDocument.tblShowDay.insertRow();
    //填充日期
    for ( j=0 ;j<firstDay.getDay();j++)
        tempCell=tempRow.insertCell();

	for ( i=1 ;i<=arrMonthCount[currentMonth]; i++){
        
        tempCell=tempRow.insertCell();
        tempCell.style.textAlign="center";
        tempCell.innerHTML=i;
		tempCell.style.cursor = "hand";
		tempCell.style.border = "1 solid #F3EBD8";
        //设置单元格的各个事件
		tempCell.onclick=MonthView.Click;
		tempCell.onmousemove = MonthView.mouseMove;
		tempCell.onmouseout = MonthView.mouseOut;
		if ( (currentYear==nowYear) && (nowMonth==currentMonth) && (nowDate == i) )
			{
			tempCell.style.backgroundColor = "red";
			tempCell.style.border = "1 solid #F3EBD8";
			tempCell.title = "今天";
			}
        if ((i+firstDay.getDay()) %7 == 0 && i!=arrMonthCount[currentMonth]){tempRow=oPopupDocument.tblShowDay.insertRow();rows++}
    }
    showHeight = (rows==6)?"159":"142";
}

function resetMonthView(){
	MonthView.setCurrentCaption()
    MonthView.bulidDay();
    oPopup.show(showLeft,showTop,showWidth,showHeight);
}

//*********重新设置当前年月(减年)*********
function MonthView.showCurrentDateYearPreview(){
    currentYear--;
	resetMonthView();
}

//*********重新设置当前年月(加年)*********
function MonthView.showCurrentDateYearNext(){
    currentYear++;
	resetMonthView();
}

//*********重新设置当前年月(减月)*********
function MonthView.showCurrentDateMonthPreview(){
    currentMonth--
    if (currentMonth<0) {currentMonth=11 ;currentYear--}   
    resetMonthView();
}

//*********重新设置当前年月(加年)*********
function MonthView.showCurrentDateMonthNext(){
	currentMonth++
	if (currentMonth>11) {currentMonth=0 ;currentYear++}
	resetMonthView();
}

//*********设置控件显示语言*********
function MonthView.setLanguage(itsName){
    languageName=itsName
    MonthView.setCurrentCaption()
    MonthView.setWeekCaption()
    MonthView.setShowHideToday(true)
}

//*********设置当前年、月的背景色*********
function MonthView.setCaptionBg(itsColor){
    oPopupDocument.trCaption.style.background=itsColor
}

//*********设置主窗体的背景色*********
function MonthView.setBodyBg(itsColor)
	{
	calElement.style.background = itsColor;
	}

//*********边框设置*********
function MonthView.setBorder(itsBorder){
    tblMonthView.style.border=itsBorder
}

//*********设置分割线的颜色*********
function MonthView.setLineColor(itsColor){
    tdLine.style.background=itsColor
}

//*********显示／隐藏今日日期*********
function MonthView.setShowHideToday(flag){
    el=oPopupDocument.tblToday.rows[0].cells[1]
    if (flag){        
        if (languageName=="cn"){
            el.innerHTML="今日："+nowYear+"-"+(nowMonth+1)+"-"+nowDate
        }else{
            el.innerHTML="Today:"+nowYear+"-"+(nowMonth+1)+"-"+nowDate
        }
        
        el.style.display="block"
    }else{
        el.style.display="none"
    }
}

//*********click事件*********
function MonthView.Click()
	{
    MonthView_value = dateFormat(currentYear,(currentMonth+1),this.innerHTML);
	oElement.value = MonthView_value;
	MonthView.hide()
	}

//*********mousemove事件*********
function MonthView.mouseMove()
	{
	this.style.backgroundColor = '#C1D2EE';
	this.style.border = '1 solid #316AC5';
	}

//*********mouseout事件*********
function MonthView.mouseOut()
	{
	if (this.title != "今天")
		{
		this.style.backgroundColor = "#F3EBD8";
		this.style.border = "1 solid #F3EBD8";
		}
	else
		{
		this.style.backgroundColor = "red";
		this.style.border = "1 solid #F3EBD8";
		}
	}

//*********mousedown事件*********
function MonthView.mousedDown()
	{
	src = event.srcElement;
	if (clickElement.contains(src))
		return;
	if (calElement.contains(src))
		return;
	MonthView.hide();
	}

//*********格式化日期显示*********
function dateFormat(year,month,day)
	{
	if (formatDate.indexOf("YYYY")==-1)
		year = year.toString().substring(2,4);
	if (formatDate.indexOf("MM")!=-1)
		if (month<10)
			month = "0"+month;
	if (formatDate.indexOf("DD")!=-1)
		if(day<10)
			day = "0"+day;
	var returnValue = formatDate.replace(/Y+/,year);
	returnValue = returnValue.replace(/M+/,month);
    returnValue = returnValue.replace(/D+/,day);

	return returnValue;
	}

//*********控件显示方法*********
function showAtElement(oEle,formatdate,left)
	{
	if (formatdate!=null)
		formatDate = formatdate.toUpperCase();
	if (arguments.length<3) left=0;
	oElement = document.getElementById(oEle);
	showLeft = oElement.getBoundingClientRect().left +window.screenLeft-left ;
	showTop = oElement.getBoundingClientRect().top + oElement.offsetHeight+window.screenTop;
	MonthView.initialize();
	}

//*********控件关闭*********
function MonthView.hide()
	{
	/*document.body.detachEvent('onmousedown',MonthView.mousedDown);
	calElement.outerHTML = "";
	calElement = null;
	oElement = null;
	clickElement = null;*/
	oPopup.hide();
	}
