Object.extend(Date.prototype, {
	Clone : function() {
		return new Date(this);
	},

	AddDay : function(days) {
		this.setDate(this.getDate() + days);
		return this;
	},
	
	AddMonth : function(months) {
		this.setMonth(this.getMonth() + months);
		return this;
	},
	
	AddYear : function(years) {
		this.setFullYear(this.getFullYear() + years);
		return this;
	}, 
	
	GetDaysInMonth : function() {
		var firstDayDate = new Date(this.getFullYear(), this.getMonth(), 1);
		firstDayDate.AddMonth(1).AddDay(-1);
		return firstDayDate.getDate();
	}, 
	
	Equals : function(date) {
		if(date == null)
			return false;
		return this.getFullYear() == date.getFullYear() && this.getMonth() == date.getMonth() && this.getDate() == date.getDate();
	}
});

window.zen_DropDownCalendarArray = [];

if(typeof(Zen) == "undefined")
	Zen = { UI : {} };

Zen.UI.DropDownCalendar = function (id, ddDayId, ddMonthId, ddYearId, validFrom, validTo) {
	this.Id = id;
	window.zen_DropDownCalendarArray[this.Id] = this;
	this._validators = [];
	this.ddDay = document.getElementById(ddDayId);
	this.ddMonth = document.getElementById(ddMonthId);
	this.ddYear = document.getElementById(ddYearId);
	this.ValidFrom = new Date();
	this.ValidFrom.setYear(validFrom.substring(6,10));
	this.ValidFrom.setMonth(validFrom.substring(3,5)-1);
	this.ValidFrom.setDate(validFrom.substring(0,2));
	this.ValidFrom.setHours(0);
	this.ValidFrom.setMinutes(0);
	this.ValidFrom.setSeconds(0);
	this.ValidFrom.setMilliseconds(0);
	this.ValidTo = new Date();
	this.ValidTo.setYear(validTo.substring(6,10));
	this.ValidTo.setMonth(validTo.substring(3,5)-1);
	this.ValidTo.setDate(validTo.substring(0,2));
	this.ValidTo.setHours(0);
	this.ValidTo.setMinutes(0);
	this.ValidTo.setSeconds(0);
	this.ValidTo.setMilliseconds(0);
	this.OnChangeYearOrMonth();
}

function GetDropDownCalendar(id) {
	return window.zen_DropDownCalendarArray[id];
}

Zen.UI.DropDownCalendar.prototype = {
	OnChangeYearOrMonth : function () {
		var tmp = new Date(this.getSelValue(this.ddYear), this.getSelValue(this.ddMonth) - 1, 1);
		dayInMonth = tmp.GetDaysInMonth();

		while (this.ddDay.length > dayInMonth + 1)
			this.ddDay.remove(this.ddDay.length - 1);
			
		while (this.ddDay.length < dayInMonth + 1)
			this.addSelectOption(this.ddDay, this.ddDay.length);
			
		this.OnChange();
	},
	
	OnChange : function () {
		if(typeof(zen_DOMValidatorValidate) == "function" && this._validators.length > 0)
			for(var i = 0; i < this._validators.length; i++)
				if(document.getElementById(this._validators[i]))
					zen_DOMValidatorValidate(document.getElementById(this._validators[i]), null, true);
	},
	
	AddValidator : function(validatorId) {
		this._validators[this._validators.length] = validatorId;
	},
	
	getSelValue : function (sel) {
		return sel.options[sel.selectedIndex].value;
	},
	
	addSelectOption : function (sel, text, value) {
		var elOptNew = document.createElement('option');
		elOptNew.text = text;
		
		if (value)
			elOptNew.value = value;
		else 
			elOptNew.value = text;
		
		try {
			sel.add(elOptNew, null); // standards compliant; doesn't work in IE
		}
		catch(ex) {
			sel.add(elOptNew); // IE only
		}
	},
	
	ClearSelect : function (sel) {
		while (sel.length > 0)
			sel.remove(sel.length - 1);
	},
	
	DomValidate : function (validator, mandatory, validRangeDefined) {
		if(this.IsDatePartiallyDefined())
		{
			validator.errormessage = "DateUndefined";
			return false;
		}
		if(mandatory && !this.IsDateDefined())
		{
			validator.errormessage = "Date";
			return false;
		}
		if(this.IsDateDefined() && validRangeDefined && !this.IsDateWithinValidRange())
		{
			validator.errormessage = "DateRange";
			return false;
		}		
		return true;
	},
	
	IsDatePartiallyDefined : function () {
		return !this.IsDateDefined() && (this.ddDay.selectedIndex != 0 || this.ddMonth.selectedIndex != 0 || this.ddYear.selectedIndex != 0);
	},
	
	IsDateDefined : function () {
		return !(this.ddDay.selectedIndex == 0 || this.ddMonth.selectedIndex == 0 || this.ddYear.selectedIndex == 0);
	},
	
	IsDateWithinValidRange : function () {
		if (!this.IsDateDefined())
			return false;
		
		var date = new Date();
		date.setYear(this.getSelValue(this.ddYear));
		date.setMonth(this.getSelValue(this.ddMonth) - 1);
		date.setDate(this.getSelValue(this.ddDay));
		date.setHours(0);
		date.setMinutes(0);
		date.setSeconds(0);
		date.setMilliseconds(0);

		return this.ValidFrom <= date && date <= this.ValidTo;
	}
}
