/**
* Author: Valery Buchinsky
* Last edit: 08.11.2006
*/

function DDDate(dayDDid, monthDDid, yearDDid)
{
	function DDDateToDateObject()
	{
		var date = new Date();
		_y = this.getYear();
		_m = this.getMonth();
		_d = this.getDate();
		date.setFullYear(_y,_m-1,_d);
		return date;
	}
	function DDDateFromDateObject(date)
	{
		if ( date.getDate()-1 < 0 || date.getDate()-1 > this.day.options.length-1 )
			return false;
		if ( date.getMonth() < 0 || date.getMonth() > this.month.options.length-1 )
			return false;
		this.day.selectedIndex = date.getDate()-1;
		this.month.selectedIndex = date.getMonth();
		for ( i = 0; i < this.year.options.length; i++ )
		{
			if ( this.year.options[i].value == date.getFullYear() )
			{
				this.year.selectedIndex = i;
				break;
			}
		}
		if ( this.year.selectedIndex != i )
			return false;
		return true;
	}
	function DDDateSetOnChangeEvent(event)
	{
		this.day.onChange = event;
		this.month.onChange = event;
		this.year.onChange = event;
		
		this.day.onchange = event;
		this.month.onchange = event;
		this.year.onchange = event;
	}
	function DDDateFixDays()
	{
		daysInMonth=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
		_current_date = this.toDate();
		_current_year = _current_date.getFullYear();
		if( ( (_current_year%4==0)&&(_current_year%100 !=0) ) || (_current_year%400==0) )
			daysInMonth[1]=29;
		_current_month = _current_date.getMonth();
		daysInMonth = daysInMonth[_current_month];
		// If we have the same number of options as the daysInMonth are
		if ( this.day.options.length == daysInMonth )
			return;
		// If we need to add options
		if ( this.day.options.length < daysInMonth )
		{
			while ( this.day.options.length < daysInMonth )
				this.day.options[this.day.options.length] = new Option(this.day.options.length+1, this.day.options.length+1);
		}
		// If we need to remove options
		else
		{
			this.day.options.length = daysInMonth;
		}
	}
	function DDDateFix()
	{
		OldMonth = this.getMonth()-1;
		this.fromDate(this.toDate());
		NewDate = this.toDate();
		while ( NewDate.getMonth() != OldMonth )
		{
			NewDate.setDate(NewDate.getDate()-1);
			this.fromDate(NewDate);
		}
		
		this.fixDays();
	}
	function DDDateUses(sId)
	{
		if ( this.dayID == sId )
			return 'd';
		if ( this.monthID == sId )
			return 'm';
		if ( this.yearID == sId )
			return 'y';
		return false;
	}
	function DDDateGetDate()
	{
		return this.day.options[this.day.selectedIndex].value;
	}
	function DDDateGetMonth()
	{
		return this.month.options[this.month.selectedIndex].value;
	}
	function DDDateGetYear()
	{
		return this.year.options[this.year.selectedIndex].value;
	}
	
	if ( !document.getElementById(dayDDid) ) return;
	if ( !document.getElementById(monthDDid) ) return;
	if ( !document.getElementById(yearDDid) ) return;
	
	this.dayID = dayDDid;
	this.monthID = monthDDid;
	this.yearID = yearDDid;
	
	this.day = document.getElementById(dayDDid);
	this.month = document.getElementById(monthDDid);
	this.year = document.getElementById(yearDDid);
	
	this.toDate = DDDateToDateObject;
	this.fromDate = DDDateFromDateObject;
	this.setOnChangeEvent = DDDateSetOnChangeEvent;
	this.fixDays = DDDateFixDays;
	this.fix = DDDateFix;
	this.uses = DDDateUses;
	this.getDate = DDDateGetDate;
	this.getMonth = DDDateGetMonth;
	this.getYear = DDDateGetYear;
	
	return this;
}
function DateDependency(date1, date2, difference)
{
	function DateDependencySynchronize()
	{
		if ( this.minAllowed && this.date1.toDate() < this.minAllowed )
			this.date1.fromDate(this.minAllowed);
		
		var Date1 = this.date1.toDate();
		var Date2 = this.date2.toDate();
		var Date2Temp = this.date2.toDate();
		if ( arguments[0] && arguments[0].id && this.date1.uses(arguments[0].id) )
		{
			Date2Temp.setDate(Date2.getDate()-this.difference);
		}
		
		if ( Date2Temp <= Date1 )
		{
			Date1.setDate(Date1.getDate()+this.difference);
			while ( !this.date2.fromDate(Date1) )
			{
				Date1.setDate(Date1.getDate()-1);
			}
		}
		this.date1.fix();
		this.date2.fix();
	}
	function DateDependencySetOnChangeEvent(event)
	{
		this.date1.setOnChangeEvent(event);
		this.date2.setOnChangeEvent(event);
	}
	
	this.date1 = date1;
	this.date2 = date2;
	this.difference = difference;
	this.minAllowed = new Date();
	
	this.Synchronize = DateDependencySynchronize;
	this.setOnChangeEvent = DateDependencySetOnChangeEvent;
	
	this.Synchronize();
	return this;
}