// JavaScript Document

function AppendList(List, Option)
{
	try
	{
		List.add(Option, null);
	}
	catch(e)
	{
		List.add(Option);
	}
}

DateSelector = function(Elem)
{
	this.Elem = Elem;
	
	//this.StartYear = new Date.getFullYear();
	this.StartYear = 2007;
	
	if(MonthNames != null)
	{
		this.Months = MonthNames;
		if(this.Months.length < 13)
			this.Months.unshift("");
	}
	else
		this.Months = new Array("", "January","February","March","April","May","June","July","August","September","October","November","December");
	
	this.Days = new Array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	
	this.Init = function()
							{
								this.Elem.style.display = "none";
								
								TmpVal = this.Elem.value.split("-");
								this.InitVal = new Array();
								this.InitVal["Year"] 	= (TmpVal[0] == null || TmpVal[0] == "") ? 0 : TmpVal[0];
								this.InitVal["Month"] = (TmpVal[1] == null || TmpVal[1] == "") ? 0 : TmpVal[1];
								this.InitVal["Day"] 	= (TmpVal[2] == null || TmpVal[2] == "") ? 0 : TmpVal[2];
								
								document.write("<SELECT CLASS='FormText DSYear' ID='" + this.Elem.id + "_Year'></SELECT>");
								document.write("<SELECT CLASS='FormText DSMonth' ID='" + this.Elem.id + "_Month'></SELECT>");
								document.write("<SELECT CLASS='FormText DSDay' ID='" + this.Elem.id + "_Day'></SELECT>");
								
								this.Year 	= document.getElementById(this.Elem.id + "_Year");
								this.Month 	= document.getElementById(this.Elem.id + "_Month");
								this.Day 		= document.getElementById(this.Elem.id + "_Day");
								
								this.GenerateYears();
								this.GenerateMonths();
								this.GenerateDays();
								
								this.Year.parent 	= this;
								this.Month.parent = this;
								this.Day.parent 	= this;
								
								this.Year.onchange 	= function() { this.parent.FixDays(); this.parent.GetValue(); }
								this.Month.onchange = function() { this.parent.FixDays(); this.parent.GetValue(); }
								this.Day.onchange 	= function() { this.parent.GetValue(); }
								
								this.Year.onblur 		= function() { this.parent.Elem.onblur(); }
								this.Month.onblur 	= function() { this.parent.Elem.onblur(); }
								this.Day.onblur 		= function() { this.parent.Elem.onblur(); }
								
								this.Year.onfocus 	= function() { this.parent.Elem.onfocus(); }
								this.Month.onfocus 	= function() { this.parent.Elem.onfocus(); }
								this.Day.onfocus 		= function() { this.parent.Elem.onfocus(); }
							}
	
	this.GenerateYears 	= function()
												{
													AppendList(this.Year, new Option("", "", false, false));
													for(i = this.StartYear; i <= (new Date).getFullYear(); i++)
													{
														Selected 	= (i == this.InitVal["Year"]);
														AppendList(this.Year, new Option(i, i, Selected, Selected));
														if(Selected)
															this.Year.selectedIndex = i - this.StartYear + 1;
													}
												}
	
	this.GenerateMonths = function()
												{
													for(i = 0; i < this.Months.length; i++)
													{
														Selected = (i == this.InitVal["Month"]);
														AppendList(this.Month, new Option(this.Months[i], i, Selected, Selected));
														if(Selected)
															this.Month.selectedIndex = i;
													}
												}
	
	this.GenerateDays 	= function()
												{
													for(i = 0; i <= this.Days[this.InitVal["Month"]]; i++)
													{
														Label = (i == 0) ? "" : i;
														Selected = (i == this.InitVal["Day"]);
														AppendList(this.Day, new Option(Label, i, Selected, Selected));
														if(Selected)
															this.Day.selectedIndex = i;
													}
												}
	
	this.FixDays				= function()
												{
													if(this.Year.options[this.Year.selectedIndex].value % 4 == 0)
														this.Days[2] = 29;
													else
														this.Days[2] = 28;
													
													NewDays = this.Days[this.Month.selectedIndex];
													if(this.Year.selectedIndex == 0)
														NewDays = 0;
													DayDiff = NewDays - (this.Day.options.length - 1);
													CurrDay = this.Day.selectedIndex;
													
													for(i = 0; i > DayDiff; i--)
														this.Day.remove(this.Day.options.length - 1);
													for(i = 0; i < DayDiff; i++)
													{
														NewIndex = this.Day.options.length;
														AppendList(this.Day, new Option(NewIndex, NewIndex, false, false));
													}
													
													if(CurrDay > this.Day.options.length - 1)
														CurrDay = this.Day.options.length - 1;
													this.Day.selectedIndex = CurrDay;
												}
	
	this.GetValue				= function()
												{
													YearVal 	= this.Year.options[this.Year.selectedIndex].value;
													MonthVal 	= this.Month.options[this.Month.selectedIndex].value;
													DayVal		= this.Day.options[this.Day.selectedIndex].value;
													
													YearVal		= (YearVal == 0) ? "" : YearVal;
													MonthVal	= (MonthVal == 0) ? "" : MonthVal;
													DayVal		= (DayVal == 0) ? "" : DayVal;
													
													if(YearVal != "" || MonthVal != "" || DayVal != "")
														Val = YearVal + "-" + MonthVal + "-" + DayVal;
													else
														Val = "";
													
													this.Elem.value = Val;
												}
}
