function zwCalendar(id, d, b, t, c)
{
	this.id=id;
	this.showYears=b;
	//this.name=name;
	this.control=document.getElementById(id);
	this.container=document.getElementById(id+'_cnt');
	this.inputType=t;
	this.weekDay=false;
	this.daySelector=false;
	this.monthSelector=false;
	this.comboDrop=false;
	this.disabled=d;
	this.callBack=c;

	this.mode=0;
	this.date=new Date();

	if(this.inputType==1)
	{
		this.weekDay=document.getElementById(id+'_wday');
		this.daySelector=document.getElementById(id+'_day');
		this.monthSelector=document.getElementById(id+'_month');
		this.yearSelector=document.getElementById(id+'_year');
		this.comboDrop=document.getElementById(id+'_combo');
	}

	this.container.zwCalendar=this;
	this.control.zwCalendar=this;

	this.getObjects=function()
	{
		this.dropObj=document.getElementById('_zwcalendareditor');
		this.detailMonth=document.getElementById('_zwcalendarmonthdetail');
		this.detailYear=document.getElementById('_zwcalendaryeardetail');
		this.detailDecade=document.getElementById('_zwcalendardecadedetail');

		this.viewCalendar=document.getElementById('_zwcalendarviewcalendar');
		this.viewMonths=document.getElementById('_zwcalendarviewmonths');
		this.viewYears=document.getElementById('_zwcalendarviewyears');
	}

	this.show=function()
	{
		var oDrop;
		var difX=0, difY=0;
		var oRp, oRo;
		var oP;

		//oP=(this.inputType==1?this.container:this.control);
		oP=this.container;
		if(oP) // && this.dropObj.style.display=='none'
		{
			this.getObjects();
			this.initDate();
			this.setMode(0);
			this.update();
			_zwContext.show(this.dropObj, oP, difX, difY);
		}
	}

	this.hide=function()
	{
		_zwContext.hide();
	}

	this.blur=function()
	{
		//this.hide();
	}

	this.next=function(e)
	{
		switch(this.mode)
		{
			case 1:
				this.nextYear();
				break;
			case 2:
				this.nextDecade();
				break;
			case 0:
			default:
				this.nextMonth();
				break;
		}
		_stopEventPropagation(e, true);
	}

	this.updateFields=function()
	{
		if(this.inputType==1)
		{
			this.weekDay.innerHTML=this.getDayName(this.wd);
			this.daySelector.value=this.day;
			this.monthSelector.value=this.month+1;
			this.yearSelector.value=this.year;
		}
	}

	this.prev=function(e)
	{
		switch(this.mode)
		{
			case 1:
				this.prevYear();
				break;
			case 2:
				this.prevDecade();
				break;
			case 0:
			default:
				this.prevMonth();
				break;
		}
		_stopEventPropagation(e, true);
	}

	this.update=function()
	{
		switch(this.mode)
		{
			case 1:
				this.setMonths();
				break;
			case 2:
				this.setYears();
				break;
			case 0:
			default:
				this.setCalendar();
				break;
		}
	}

	this.nextDecade=function()
	{
		this.decade+=10;
		this.update();
	}

	this.prevDecade=function()
	{
		this.decade-=10;
		this.update();
	}

	this.nextYear=function()
	{
		this.date.setYear(this.date.getFullYear()+1);
		this.setDateParts();
		this.update();
	}

	this.prevYear=function()
	{
		this.date.setYear(this.date.getFullYear()-1);
		this.setDateParts();
		this.update();
	}

	this.nextMonth=function()
	{
		this.date.setMonth(this.date.getMonth()+1);
		this.setDateParts();
		this.update();
	}

	this.prevMonth=function()
	{
		this.date.setMonth(this.date.getMonth()-1);
		this.setDateParts();
		this.update();
	}

	this.initDate=function()
	{
		var p=this.control.value.split('/');

		if(p.length==3)
		{
			d=new Date(p[2], p[1]-1, p[0]);
		} else {
			d=new Date();
		}

		this.date=d;
		this.selDate=new Date(d.getFullYear(), d.getMonth(), d.getDate());
		this.setDateParts();
	}

	this.setDateParts=function()
	{
		this.wd=this.date.getDay();
		this.day=this.date.getDate();
		this.month=this.date.getMonth();
		this.year=this.date.getFullYear();
		this.decade=Math.round(this.year/10)*10;
	}

	this.getDayName=function(i)
	{
		switch(i)
		{
			case 0:
				n=zw.jslng.sunday_abbr_med;
				break;
			case 1:
				n=zw.jslng.monday_abbr_med;
				break;
			case 2:
				n=zw.jslng.tuesday_abbr_med;
				break;
			case 3:
				n=zw.jslng.wednesday_abbr_med;
				break;
			case 4:
				n=zw.jslng.thursday_abbr_med;
				break;
			case 5:
				n=zw.jslng.friday_abbr_med;
				break;
			case 6:
				n=zw.jslng.saturday_abbr_med;
				break;
			default:
				n='';
				break;
		}
		return n;
	}

	this.setCalendar=function()
	{
		var i, j, c, t, f, m, l;

		t=new Date(this.year, this.month, 1);
		f=t.getDay();

		switch(this.month)
		{
			case 0:
				m=zw.jslng.january;
				break;
			case 1:
				m=zw.jslng.february;
				break;
			case 2:
				m=zw.jslng.march;
				break;
			case 3:
				m=zw.jslng.april;
				break;
			case 4:
				m=zw.jslng.may;
				break;
			case 5:
				m=zw.jslng.june;
				break;
			case 6:
				m=zw.jslng.july;
				break;
			case 7:
				m=zw.jslng.august;
				break;
			case 8:
				m=zw.jslng.september;
				break;
			case 9:
				m=zw.jslng.october;
				break;
			case 10:
				m=zw.jslng.november;
				break;
			case 11:
				m=zw.jslng.december;
				break;
			default:
				m='';
				break;
		}
		this.detailMonth.innerHTML=m+' '+t.getFullYear();

		if(f<=1) f+=7;
		for(i=f;f>1;f--)
			t.setDate(t.getDate()-1);

		for(i=0;i<6;i++)
		{
			for(j=0;j<7;j++)
			{
				c=document.getElementById('_calendar_d_'+i+'_'+j);
				c.zwDate=new Date(t.getFullYear(), t.getMonth(), t.getDate());
				c.innerHTML=t.getDate();

				l=c.className.replace(/ (cmscaldis|cmscalsel|cmscalovr)/g, '');
				if(t.getMonth()!=this.month) l+=' cmscaldis';
				if(t.getDate()==this.selDate.getDate() && t.getMonth()==this.selDate.getMonth() && t.getFullYear()==this.selDate.getFullYear())
					l+=' cmscalsel';
				c.className=l;
				t.setDate(t.getDate()+1);
			}
		}
	}

	this.setMonths=function()
	{
		this.detailYear.innerHTML=this.year;
	}

	this.setYears=function()
	{
		var i, j, d, s, c;

		s=this.decade-1;
		this.detailDecade.innerHTML=this.decade+'-'+(this.decade+9);

		for(i=0;i<3;i++)
		{
			for(j=0;j<4;j++)
			{
				c=document.getElementById('_calendar_dc_'+i+'_'+j);
				c.innerHTML=s;
				s++;
			}
		}
	}

	this.setMode=function(m)
	{
		this.mode=m;
		this.detailMonth.style.display=(this.mode==0?'block':'none');
		this.detailYear.style.display=(this.mode==1?'block':'none');
		this.detailDecade.style.display=(this.mode==2?'block':'none');

		this.viewCalendar.style.display=(this.mode==0?'block':'none');
		this.viewMonths.style.display=(this.mode==1?'block':'none');
		this.viewYears.style.display=(this.mode==2?'block':'none');

		this.update();
	}

	this.rotateMode=function(e)
	{
		if(this.mode<2) this.setMode(++this.mode);
		_stopEventPropagation(e, true);
	}

	this.monthMouseDown=function(e)
	{
		e=(window.event?window.event:e);
		var o=(window.event?e.srcElement:e.target);
		var s=o.id;
		s=parseInt(s.replace(/[^0-9]/g, ''));
		if(!isNaN(s))
		{
			this.date.setMonth(s);
			this.setDateParts();
			this.setMode(0);
		}
	}

	this.dayMouseDown=function(e)
	{
		var o, t;
		e=(window.event?window.event:e);
		o=(window.event?e.srcElement:e.target);
		t=o.zwDate;
		if(t) this.setDate(t)
	}

	this.setDate=function(t)
	{
		var d, m, y, e;
		d=new String(t.getDate());
		m=new String(t.getMonth()+1);
		y=new String(t.getFullYear());
		this.control.value=(d.length<2?'0':'')+d+'/'+(m.length<2?'0':'')+m+'/'+y;
		this.initDate();
		this.updateFields();
		_zwContext.hide();
		if(this.callBack)
		{
			e=false;
			eval('if(typeof '+this.callBack+'==\'function\') e='+this.callBack+';');
			if(e) e.call(this, this);
		}
	}


	this.yearMouseDown=function(e)
	{
		e=(window.event?window.event:e);
		var o=(window.event?e.srcElement:e.target);
		var s=parseInt(o.innerHTML);

		if(!isNaN(s))
		{
			this.date.setYear(s);
			this.setDateParts();
			this.setMode(1);
		}
	}

	this.cellOver=function(e)
	{
		var o, c;
		e=(window.event?window.event:e);
		o=(window.event?e.srcElement:e.target);

		while(o.tagName!='TD' && o.tagName!='TABLE')
			o=o.parentNode;

		if(o.tagName=='TD')
		{
			c=o.className.replace(/ cmscalovr/g, '');;
			c+=' cmscalovr';
			o.className=c;
		}
	}

	this.cellOut=function(e)
	{
		var o, c;
		e=(window.event?window.event:e);
		o=(window.event?e.srcElement:e.target);

		while(o.tagName!='TD' && o.tagName!='TABLE')
			o=o.parentNode;

		if(o.tagName=='TD')
		{
			c=o.className.replace(/ cmscalovr/g, '');;
			o.className=c;
		}
	}

	this.selectorChanged=function(e)
	{
		var t=new Date(this.yearSelector.value, this.monthSelector.value-1, this.daySelector.value);
		this.setDate(t);
	}

	var tO=this;
	//_addEventHandler(this.control, 'onfocus', function(){ tO.show();});
	//_addEventHandler(this.control, 'onblur', function(){ tO.blur();});
	switch(this.inputType)
	{
		case 1:
			_addEventHandler(this.comboDrop, 'onmousedown', function(){ tO.show(); _stopEventPropagation(arguments[0]); return false; });
			_addEventHandler(this.daySelector, 'onchange', function(){ tO.selectorChanged(); _stopEventPropagation(arguments[0]); return false; });
			_addEventHandler(this.monthSelector, 'onchange', function(){ tO.selectorChanged(); _stopEventPropagation(arguments[0]); return false; });
			break;
		case 0:
		default:
			_addEventHandler(this.container, 'onmousedown', function(){ tO.show(); _stopEventPropagation(arguments[0]); return false; });
			break;
	}

	this.initDate();
	this.updateFields();
	if(this.showYears) _zwDateYears(this.control);

}

function _zwCalendarNext(e)
{
	if(_zwContext.object && _zwContext.object.id=='_zwcalendareditor')
		_zwContext.parent.zwCalendar.next(e);
}

function _zwCalendarPrev(e)
{
	if(_zwContext.object && _zwContext.object.id=='_zwcalendareditor')
		_zwContext.parent.zwCalendar.prev(e);
}

function _zwCalendarRotateMode(e)
{
	if(_zwContext.object && _zwContext.object.id=='_zwcalendareditor')
		_zwContext.parent.zwCalendar.rotateMode();
}

function _zwCalendarYearMouseDown(e)
{
	if(_zwContext.object && _zwContext.object.id=='_zwcalendareditor')
		_zwContext.parent.zwCalendar.yearMouseDown(e);
}

function _zwCalendarMonthMouseDown(e)
{
	if(_zwContext.object && _zwContext.object.id=='_zwcalendareditor')
		_zwContext.parent.zwCalendar.monthMouseDown(e);
}

function _zwCalendarDayMouseDown(e)
{
	if(_zwContext.object && _zwContext.object.id=='_zwcalendareditor')
		_zwContext.parent.zwCalendar.dayMouseDown(e);
}

function _zwCalendarCellOver(e)
{
	if(_zwContext.object && _zwContext.object.id=='_zwcalendareditor')
		_zwContext.parent.zwCalendar.cellOver(e);
}

function _zwCalendarCellOut(e)
{
	if(_zwContext.object && _zwContext.object.id=='_zwcalendareditor')
		_zwContext.parent.zwCalendar.cellOut(e);
}