//db日期控件
function DBDateInput(parent) {
	var obj = new DateInput(parent);
	DBControl_Bind(obj.Input);
  if(isIE)
	obj.onpropertychange=DBDateInput_onpropertychange
  else
	addEventTimerListener(obj,DBDateInput_onpropertychange);
	return obj;
}
function DBDateInput_onpropertychange(sender)//属性变化处理
{
  var n;
  if(isIE) {
	  n=event.propertyName;
	  sender=this;
  }
  else{
	  if(sender.OldDataField!=sender.DataField){
		 n='DataField';
		 sender.OldDataField=sender.DataField;
	  }
	  else if(sender.OldDataSet!=sender.DataSet){
		 n='DataSet';
		 sender.OldDataSet=sender.DataSet;
	  }
  }	
  switch(n){
	case 'DataSet':
	  sender.Input.DataSet=sender.DataSet;	 
	  break;
	case 'DataField':
	  sender.Input.DataField=sender.DataField;	 
	  break;
	case 'style.width':
	  sender.Input.style.width=sender.style.pixelWidth-24;
	  break;
  }
}

//构造函数
function DateInput(parent)   
{
	var obj = new DateTimeInput( parent);
    obj.Input.onchange=DI_editchange;
    obj.getdatestring=function(){return DateToStr(this.date);};
	obj.Init();
	return obj;
}
function DI_editchange()
{
  var p=this.parent;
  p.Value=this.value;
  p.DateTime=this.value+' 00:00:00.0';
}

//构造函数
function DateTimeInput(parent)   
{
  var pdiv,obj=document.createElement('div');//创建一个控制节点

  if(parent){pdiv=parent;}else pdiv=document.body;//获取父块
  pdiv.appendChild(obj);
  obj.Input=document.createElement('input');
  setStyle(obj.Input);
  obj.Input.style.height=21;
  obj.Init=DTI_init;
  obj.getdatestring=DTI_getdatestring;
  obj.Input.parent=obj;
  obj.Input.onchange=DTI_editchange;
  obj.button=document.createElement('input');
  obj.CellWidth=26;
  obj.CellHeight=22;
  setFont(obj.button);
  obj.button.type='button';
  obj.button.style.backgroundColor=clWhite;
  obj.button.style.width=23;
  obj.button.style.height=22;
  obj.button.style.backgroundRepeat='no-repeat'; 
  obj.button.style.backgroundImage="url("+jcl_ResourceURL+"datetimedrag.gif)";
  obj.button.parent=obj;
  obj.button.onclick=DTI_buttonclick;
  obj.appendChild(obj.Input);
  obj.appendChild(obj.button);
  //属性
  obj.option1=new Array();
  obj.option2=new Array();
  obj.date=new Date();
  //方法
  obj.CreateCalendar=DTI_createcalendar;//创建日历
  obj.AddSubCalendar=DTI_addsubcalendar;//处理日历
  obj.ClickYearItem=DTI_clickyearitem;//改变年
  obj.ClickMonthItem=DTI_clickmonthitem;//改变月
  obj.ChangeDate=DTI_changedate;//处理日期变化
  obj.ChooseDate=DTI_choosedate;//选择日期
  obj.onpropertychange=DTI_onpropertychange;//属性变化的事件
  obj.oncontextmenu=div_oncontextmenu;
  obj.Init();
  setFont(obj);
  return obj;
}
function DTI_onpropertychange()//属性变化处理
{
  var n=event.propertyName;
  switch(n){
	case 'style.width':
	  this.Input.style.width=this.style.pixelWidth-24;
	  break;
  }
}

function DTI_init(){
  this.date=new Date();	
  this.Input.value=this.getdatestring();
}
function DTI_getdatestring()
{
  return DateTimeToStr(this.date);
}

function DTI_editchange()
{
  var p=this.parent;
  p.Value=this.value;
  p.DateTime=this.value;
}
function DTI_buttonclick()
{
  var p=this.parent;
  var c=p.calendar;
  p.style.zIndex=2;
  if(c){
	if(c.style.visibility=='hidden'){
	  c.style.visibility = 'visible';
	}
	else {
	  c.style.visibility = 'hidden';
	  p.style.zIndex=0;
	}
  }
  else{p.AddSubCalendar();}
  if(p.OnButtonClick)p.OnButtonClick(this);
}
//生成日历
function DTI_createcalendar()
{
	this.Type='';
	this.tr=new Array();
	//this.td=new Array();
	this.tds=new Array();
	this.rect=new Array();
	this.rects=new Array();
	//生成日历图层
	this.calendar=document.createElement('div');
//	this.calendar.style.left=0;
	this.calendar.style.top=this.Input.style.height;
	this.calendar.style.width=188;
	this.calendar.style.height=172;
	this.calendar.style.borderWidth=1;
	this.calendar.style.borderStyle='solid';
	this.calendar.style.borderColor=clBlack;
	this.calendar.style.backgroundColor=clHightGray;
    this.span0=document.createElement('span');
	this.calendar.appendChild(this.span0);
	this.span0.innerHTML='选择 ';
	 //生成年可选下拉菜单
    this.yearselect=document.createElement('select');       
    this.yearselect.innerHTML='2002';
	this.calendar.appendChild(this.yearselect);
	this.yearselect.onchange=DTI_clickyearitem;
	this.yearselect.parent=this;
//    this.yearselect.style.height=21;
	for(var i=1900;i<2101;i++)
	{	
		
		this.option2[i]=document.createElement('OPTION');
		this.option2[i].text=i;
		if(this.date.getYear()!=2099)
		{
			if(i<2000)
			{
				if((i-1900)==(this.date.getYear()+1))
					this.option2[i].selected=true;
            }
			else if(i==(this.date.getYear()+1))
			{
				this.option2[i].selected=true;
			}
		}
		else
			this.option2[2099].selected=true;
		if(i<2100)
			this.yearselect.options.add(this.option2[i]);
	}
	this.span1=document.createElement('span');
	this.calendar.appendChild(this.span1);
	this.span1.innerHTML='年 ';
	//生成月可选下拉菜单
	this.monthselect=document.createElement('select');       
    this.calendar.appendChild(this.monthselect);
	this.monthselect.onchange=DTI_clickmonthitem;
    this.monthselect.parent=this;

	for(var i=1;i<14;i++)
	{	
		this.option1[i]=document.createElement('option');
		this.option1[i].text=i;
		if(i!=13)
		this.monthselect.options.add(this.option1[i]);
	}
	this.span2=document.createElement('span');
	this.calendar.appendChild(this.span2);
	this.span2.innerHTML='月 ';
    this.div1=document.createElement('div');
	this.calendar.appendChild(this.div1);
	this.div1.style.position='relative';
	this.div1.style.background='#CCCCCC';
	this.div1.style.left=2;	
//	this.div1.style.width=this.calendar.style.pixelWidth-6;	

	this.span3=document.createElement('span');
	this.div1.appendChild(this.span3);
	var space='&nbsp;&nbsp;&nbsp;&nbsp;';
	this.span3.innerHTML='日'+space;
	this.span4=document.createElement('span');
	this.div1.appendChild(this.span4);
	this.span4.innerHTML='一'+space;
	this.span5=document.createElement('span');
	this.div1.appendChild(this.span5);
	this.span5.innerHTML='二'+space;
	this.span6=document.createElement('span');
	this.div1.appendChild(this.span6);
	this.span6.innerHTML='三'+space;
	this.span7=document.createElement('span');
	this.div1.appendChild(this.span7);
	this.span7.innerHTML='四'+space;
	this.span8=document.createElement('span');
	this.div1.appendChild(this.span8);
	this.span8.innerHTML='五'+space;
	this.span9=document.createElement('span');
	this.div1.appendChild(this.span9);
	this.span9.innerHTML='六'+space;
//	this.monthselect[this.date.getMonth()].selected=true;

    //生成日期
	this.div2=document.createElement('div');
	this.div2.style.position='relative';
	this.div2.style.background='#F3F3F3';
	this.div2.style.left=2;
	this.calendar.appendChild(this.div2);
	this.Table=document.createElement('table');
	this.Table.border=1;
    this.Table.cellSpacing="0";
    this.Table.cellPanding='0';
    this.Table.borderColor=clSilver;
    this.Table.style.tableLayout='fixed';
    this.Table.style.borderCollapse='collapse';//表线的格式


	this.Body=document.createElement('tbody');
    this.Table.appendChild(this.Body);
    this.div2.appendChild(this.Table);
	//生成天数
	var year=this.date.getYear();
	var nDays=[31,30,31,30,31,30,31,31,30,31,30,31];
	var day=1;
    if ((year%100 == 0 && year%400 == 0) || (year%100 != 0 && year%4 == 0))
		nDays[1] = 29
	else 
		nDays[1] = 28;
	for(var i=0;i<6;i++)
    {
		var td = new Array();
		for(var j=0;j<7;j++)
		{  
			this.rect[j]=document.createElement('TABLE');
//            this.rect[j].style.border='1 solid black';
			this.rect[j].style.width=this.CellWidth;
			this.rect[j].style.height=this.CellHeight;
			this.rect[j].style.position='absolute';
			this.rect[j].border=1;
			this.rect[j].cellSpacing="0";
			this.rect[j].cellPanding='0';
			this.rect[j].borderColor=clSilver;
			this.rect[j].style.tableLayout='fixed';
			this.rect[j].style.borderCollapse='collapse';//表线的格式
			if(i>1) 
			{
				this.rect[j].style.top=this.CellHeight*i;
			    if(i>3)
				{
					this.rect[j].style.top=this.CellHeight*i;
				}
			}
			else
			{
                this.rect[j].style.top=this.CellHeight*i;
			}
			if(j>4) 
			{
				this.rect[j].style.left=this.CellWidth*j;
			}
			else
			{
                this.rect[j].style.left=this.CellWidth*j;
			}
            var body=document.createElement('tbody');
		    this.rect[j].appendChild(body);
		    var tr=document.createElement('TR');
		    td[j]=document.createElement('TD');
		    td[j].onclick = DTI_choosedate;
			td[j].parent = this;
			if(i==0&&day<nDays[this.date.getMonth()]+1)
			{   
				var day1=this.date.getDate();
				this.date.setDate('1');
				if(this.date.getDay()>j)
			       td[j].innerHTML='';
		        else
                   td[j].innerHTML=day++;
				this.date.setDate(day1);
			}
			else if(i!=0&&day<nDays[this.date.getMonth()]+1)
			{  
                td[j].innerHTML=day++;
			} 
		         else if(i!=0)
				 {
					 td[j].innerHTML='';
				 }
			this.day=this.date.getDate();
			if(this.day>nDays[this.date.getMonth()])
				this.day=nDays[this.date.getMonth()];
			if(this.day==td[j].innerHTML)
			{
				td[j].style.backgroundColor=clAqua;
//				td[j].style.color='#F3F3F3';
			}
			setFont(td[j]);
			tr.appendChild(td[j]);
		    body.appendChild(tr);
			this.div2.appendChild(this.rect[j]); 
		}

	    this.rects[i]=this.rect;
		this.tds[i]=td;
	}
}
//处理日历
function DTI_addsubcalendar()
{
  this.CreateCalendar();
  this.appendChild(this.calendar);
}
//改变年
function DTI_clickyearitem()
{
	this.parent.Type='year';
	for(var i=1900;i<2101;i++)
    {
		if(this.parent.option2[i].selected==true)
			this.parent.date.setYear(this.parent.option2[i].innerHTML);
	}
	this.parent.ChangeDate();
	this.parent.date.setDate(this.parent.day);
    this.parent.Input.value=this.parent.getdatestring();
	this.parent.Input.onchange();
}
//改变月
function DTI_clickmonthitem()
{   
	this.parent.Type='month';
	for(var i=1;i<12;i++)
    {   
		if(this.parent.option1[i].selected==true)
		{
			this.parent.date.setMonth(this.parent.option1[i].innerHTML-1);
		    this.parent.month=this.parent.option1[i].innerHTML-1;
		}
	}
	if(this.parent.option1[12].selected==true)
	{
		this.parent.month=this.parent.option1[i].innerHTML-1;
		this.parent.date.setMonth(this.parent.option1[i].innerHTML-1); 
	}
	this.parent.ChangeDate();
	this.parent.date.setDate(this.parent.day);
    this.parent.Input.value=this.parent.getdatestring();
	this.parent.Input.onchange();
}
//处理日期的改变
function DTI_changedate()
{
	var year=this.date.getYear();
	var nDays=[31,30,31,30,31,30,31,31,30,31,30,31];
	var day=1;
    if ((year%100 == 0 && year%400 == 0) || (year%100 != 0 && year%4 == 0))
		nDays[1] = 29
	else 
		nDays[1] = 28 
	var month=0;
	if(this.Type=='year')
		month=this.date.getMonth();
	if(this.Type=='month')
		month=this.month;
	for(var i=0;i<6;i++)
	{
		for(var j=0;j<7;j++)
		{  
			if(i==0&&day<nDays[month]+1)
			{  
				var day1=this.date.getDate();
				this.date.setDate('1');
				if(this.date.getDay()>j)
			       this.tds[i][j].innerHTML='';
		        else
                   this.tds[i][j].innerHTML=day++;
			    this.date.setDate(day1);
			}
			else if(i!=0&&day<nDays[month]+1)
			{  
                this.tds[i][j].innerHTML=day++;
			} 
		         else if(i!=0)
				 {
					 this.tds[i][j].innerHTML='';
				 } 
		    this.tds[i][j].style.backgroundColor=clHightGray;
//			this.tds[i][j].style.color='black';
			if(this.day>nDays[month])
				this.day=nDays[month];
			if(this.day==this.tds[i][j].innerHTML)
			{
				this.tds[i][j].style.backgroundColor=clAqua;
//				this.tds[i][j].style.color='#F3F3F3';
			}
		}
	}
}
function DTI_choosedate()
{
	if(this.innerHTML!='')
    {  
		for(var i=0;i<6;i++)
		 for(var j=0;j<7;j++)
			{ 
				this.parent.tds[i][j].style.backgroundColor=clHightGray;
//				this.parent.tds[i][j].style.color='black';
			}
        this.style.background=clAqua;
//		this.style.color='#F3F3F3';
		this.parent.date.setDate(this.innerHTML);
		this.parent.day=this.innerHTML;
	    this.parent.Input.value=this.parent.getdatestring();
    	this.parent.Input.onchange();
		this.parent.calendar.style.visibility = 'hidden';
  	    this.parent.style.zIndex=0;
	}
}