// ddDatePicker_ClientScripts
var ddDatePicker_DayNames=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var ddDatePicker_DayNamesShort=new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
function ddDatePicker(id) {
	this.Id=id;
	if (document.getElementById(this.Id+'_DayDropDown')) {
		this.DayDropDown=document.getElementById(this.Id+'_DayDropDown');
	} else {
		alert('ddDatePicker Error! Couldn\'t find day drop down control!');
	}
	if (document.getElementById(this.Id+'_MonthDropDown')) {
		this.MonthDropDown=document.getElementById(this.Id+'_MonthDropDown');
	} else {
		alert('ddDatePicker Error! Couldn\'t find month drop down control!');
	}
	if (document.getElementById(this.Id+'_YearDropDown')) {
		this.YearDropDown=document.getElementById(this.Id+'_YearDropDown');
	} else {
		alert('ddDatePicker Error! Couldn\'t find year drop down control!');
	}
	// arbitrary test for Firefox
	if (!document.selection) {
		this.Browser='Moz';
	}
}
ddDatePicker.prototype.Id = '';
ddDatePicker.prototype.DayDropDown;
ddDatePicker.prototype.MonthDropDown;
ddDatePicker.prototype.YearDropDown;
ddDatePicker.prototype.GetSelectedDay = ddDatePicker_GetSelectedDay;
ddDatePicker.prototype.GetSelectedMonth = ddDatePicker_GetSelectedMonth;
ddDatePicker.prototype.GetSelectedYear = ddDatePicker_GetSelectedYear;
ddDatePicker.prototype.GetSelectedDate = ddDatePicker_GetSelectedDate;
ddDatePicker.prototype.SetSelectedDay = ddDatePicker_SetSelectedDay;
ddDatePicker.prototype.SetSelectedMonth = ddDatePicker_SetSelectedMonth;
ddDatePicker.prototype.SetSelectedYear = ddDatePicker_SetSelectedYear;
ddDatePicker.prototype.SetSelectedDate = ddDatePicker_SetSelectedDate;
ddDatePicker.prototype.OnDayChange = ddDatePicker_OnDayChange;
ddDatePicker.prototype.OnMonthChange = ddDatePicker_OnMonthChange;
ddDatePicker.prototype.OnYearChange = ddDatePicker_OnYearChange;
ddDatePicker.prototype.OnChange = ddDatePicker_OnChange;
ddDatePicker.prototype.CustomOnChange=null;
ddDatePicker.prototype.RenderDaysDropDown = ddDatePicker_RenderDaysDropDown;
ddDatePicker.prototype.RenderYearsDropDown = ddDatePicker_RenderYearsDropDown;
ddDatePicker.prototype.RenderDayType;
ddDatePicker.prototype.RenderYearType;
ddDatePicker.prototype.MaxDate;
ddDatePicker.prototype.MinDate;
ddDatePicker.prototype.FirstYearToRender;
ddDatePicker.prototype.LastYearToRender;
ddDatePicker.prototype.Calendar = null;
ddDatePicker.prototype.ShowCalendar = ddDatePicker_ShowCalendar;
ddDatePicker.prototype.CalendarChanged = ddDatePicker_CalendarChanged;
ddDatePicker.prototype.DateIsValid = ddDatePicker_DateIsValid;
ddDatePicker.prototype.LastDate;

function ddDatePicker_OnDayChange() {
	this.OnChange();
}
function ddDatePicker_OnMonthChange() {
	this.RenderDaysDropDown();
	this.OnChange();
}
function ddDatePicker_OnYearChange() {
	this.RenderDaysDropDown();
	this.OnChange();
}
function ddDatePicker_OnChange() {
	if (this.LastDate != this.GetSelectedDate()) {
		if (this.CustomOnChange!=null) {
			this.CustomOnChange(this);
		}
		this.LastDate = this.GetSelectedDate();
	}
}
function ddDatePicker_GetSelectedDay() {
	return this.DayDropDown.options[this.DayDropDown.selectedIndex].value;
}
function ddDatePicker_GetSelectedMonth() {
	return this.MonthDropDown.options[this.MonthDropDown.selectedIndex].value;
}
function ddDatePicker_GetSelectedYear() {
	return this.YearDropDown.options[this.YearDropDown.selectedIndex].value;
}
function ddDatePicker_GetSelectedDate() {
	return new Date(this.GetSelectedYear(),this.GetSelectedMonth()-1,this.GetSelectedDay());
}
function ddDatePicker_SetSelectedDay(iDay, suppressOnChange) {
	if (iDay > 0 && iDay <= this.DayDropDown.options.length && this.DateIsValid(new Date(this.GetSelectedYear(),this.GetSelectedMonth()-1,iDay))     ) {
		this.DayDropDown.selectedIndex=iDay-1;
				if ((this.LastDate != this.GetSelectedDate()) && (!suppressOnChange)) {this.OnDayChange();}
	} else {
		alert('Failed to set day to '+iDay+': out of range');
	}
}
function ddDatePicker_SetSelectedMonth(iMonth, suppressOnChange) {
	if (iMonth > 0 && iMonth < 13 && this.DateIsValid(new Date(this.GetSelectedYear(),iMonth-1,this.GetSelectedDay()))) {
		this.MonthDropDown.selectedIndex=iMonth-1;
		if ((this.LastDate != this.GetSelectedDate()) && (!suppressOnChange)) {this.OnMonthChange();}
	} else {
		alert('Date is out of valid range');
	}
}
function ddDatePicker_SetSelectedYear(iYear, suppressOnChange) {
	if (!this.DateIsValid(new Date(iYear,this.GetSelectedMonth()-1,this.GetSelectedDay()))) {
		alert('Date is out of valid range!');
	} else {
		var n=0;
		var index=-1;
		if (iYear < this.FirstYearToRender) {
			this.FirstYearToRender = iYear;
			this.RenderYearsDropDown();
		}
		if (iYear > this.LastYearToRender) {
			this.LastYearToRender = iYear;
			this.RenderYearsDropDown();
		}
		for (n=0; n < this.YearDropDown.options.length; n++) {
			if (this.YearDropDown.options[n].value==iYear) {
				index = n;
			}
		}
		if (index > -1) {
			this.YearDropDown.selectedIndex=index;
			if ((this.LastDate != this.GetSelectedDate()) && (!suppressOnChange)) {this.OnYearChange();}
		} else {
			alert('Failed to set year to '+iYear+': out of range');
		}
	}
}
function ddDatePicker_SetSelectedDate(oDate, suppressOnChange) {
	if (this.DateIsValid(oDate)) {
		this.SetSelectedYear(oDate.getFullYear(),suppressOnChange);
		this.SetSelectedMonth(oDate.getMonth()+1,true);
		this.SetSelectedDay(oDate.getDate(),true);
	} else {
		alert('Date is out of valid range!');
	}
}
function ddDatePicker_RenderDaysDropDown() {
	var maxDays = 31;
	var isLeapYear = true;
	if ((this.GetSelectedYear()/4) != Math.floor(this.GetSelectedYear()/4)) {
		isLeapYear = false;
	} else {
		if ((this.GetSelectedYear()/100) != Math.floor(this.GetSelectedYear()/100)) {
			isLeapYear = true;
		} else {
			if ((this.GetSelectedYear()/400) != Math.floor(this.GetSelectedYear()/400)) {
				isLeapYear = false;
			}
		}
	}
	if (this.GetSelectedMonth()==9 || this.GetSelectedMonth()==4 || this.GetSelectedMonth()==6 || this.GetSelectedMonth()==11) {
		maxDays = 30;
	}
	if (this.GetSelectedMonth()==2) {
		if (isLeapYear == true) {
			maxDays = 29;
		} else {
			maxDays = 28;
		}
	}
	var n;
	var selectedDay = this.GetSelectedDay();
	for (n=this.DayDropDown.options.length; n >= 0; n--) {
		this.DayDropDown.options[n]=null;
	}
	for (n=1; n <= maxDays; n++) {
		switch (this.RenderDayType) {
			case 'Number' :
				this.DayDropDown.options[n-1]=new Option(n, n);
				break;
			case 'NumberAndAbbreviatedName' :
				var dDate = new Date(this.GetSelectedYear(),this.GetSelectedMonth()-1,n);
				var sDayName = ddDatePicker_DayNamesShort[dDate.getDay()];
				this.DayDropDown.options[n-1]=new Option(n+' ('+sDayName+')', n);
				break;
			case 'NumberAndFullName' :
				var dDate = new Date(this.GetSelectedYear(),this.GetSelectedMonth()-1,n);
				var sDayName = ddDatePicker_DayNames[dDate.getDay()];
				this.DayDropDown.options[n-1]=new Option(n+' ('+sDayName+')', n);
				break;
		}
	}
	if (maxDays < selectedDay) {
		this.SetSelectedDay(maxDays);
	} else {
		this.SetSelectedDay(selectedDay);
	}
}
function ddDatePicker_RenderYearsDropDown() {
	for (n=this.YearDropDown.options.length; n >= 0; n--) {
		this.YearDropDown.options[n]=null;
	}
	for (n=this.FirstYearToRender; n<=this.LastYearToRender; n++) {
		switch (this.RenderYearType) {
			case 'LastTwoDigits' :
				this.YearDropDown.options[n-this.FirstYearToRender]=new Option((''+n).substring(2,4), n);
				break;
			case 'Full' :
				this.YearDropDown.options[n-this.FirstYearToRender]=new Option(n, n);
				break;
		}
	}
}
function ddDatePicker_ShowCalendar() {
	if (this.Calendar.PopUpObjectShowing) {
	  this.Calendar.Hide();
	} else {
	  this.Calendar.SetSelectedDate(this.GetSelectedDate());
	  this.Calendar.Show();
	}
}
function ddDatePicker_CalendarChanged(oCalendar) {
	oCalendar.ddDatePicker.SetSelectedDate(oCalendar.GetSelectedDate());
}
function ddDatePicker_DateIsValid(oDate) {
	if (oDate <= this.MaxDate && oDate >= this.MinDate) {
		return true;
	}
	return false;
}
