// comboDate constructor
function ComboDate(intDay, intMonth, intYear){
	// Initialize
	this.day		= parseInt(intDay);
	this.month	= parseInt(intMonth);
	this.year		= parseInt(intYear);
	this.days		= 0;
	// Constants
	this.months = new Array("Ocak", "Şubat", "Mart", "Nisan", "Mayıs", "Haziran", "Temmuz", "Ağustos", "Eylül", "Ekim", "Kasım", "Aralık");
	this.years	= new Array("1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012");
	// Methods
	this.create				= calCreate;
	this.getDay				= calGetDay;
	this.getMonth			= calGetMonth;
	this.getYear			= calGetYear;
	this.getNext			= calGetNext;
	this.getPrev			= calGetPrev;
	this.setObjects		= calSetObjects
	this.update				= calUpdate;
}
function calSetObjects(strFormName, strDayCboName, strMonthCboName, strYearCboName){
	this.cboDay		= eval("document." + strFormName + "." + strDayCboName);
	this.cboMonth	= eval("document." + strFormName + "." + strMonthCboName);
	this.cboYear	= eval("document." + strFormName + "." + strYearCboName);
	
	this.cboDay.disabled	= false;
	this.cboMonth.disabled	= false;
	this.cboYear.disabled	= false;
}
function calCreate(){
	var myOption;

	this.cboYear.options[0] = null;
	for (var i = 0; i < this.years.length; i++) {
		myOption = new Option(this.years[i], this.years[i]);
		this.cboYear.options[this.cboYear.length] = myOption;
	}

	this.cboMonth.options[0] = null;
	for (var i = 0; i < this.months.length; i++) {
		myOption = new Option(this.months[i], i + 1);
		this.cboMonth.options[this.cboMonth.length] = myOption;
	}

	this.cboYear.options.selectedIndex	= this.year - this.years[0];
	this.cboMonth.options.selectedIndex = this.month - 1;
	this.update();
	this.cboDay.options.selectedIndex	= this.day - 1;
}
function calUpdate(){
	this.year		= parseInt(this.cboYear.options[this.cboYear.options.selectedIndex].value);
	this.month	= parseInt(this.cboMonth.options[this.cboMonth.options.selectedIndex].value);
	this.days		= GetDays(this.month, this.year);
	
		// Remove days
		for (var i = 0; i < 31; i++)
			this.cboDay.options[0] = null;

		// Add days	
		var myOption
		for (var i = 1; i <= this.days; i++) {
			myOption = new Option(i, i);
			this.cboDay.options[this.cboDay.length] = myOption;
		}

	this.cboDay.options.selectedIndex = 0;
}
function calGetDay(){
	return this.cboDay.options[this.cboDay.options.selectedIndex].value;
}
function calGetMonth(){
	return this.cboMonth.options[this.cboMonth.options.selectedIndex].value;
}
function calGetYear(){
	return this.cboYear.options[this.cboYear.options.selectedIndex].value;
}
function calGetNext(){
	this.day			= this.cboDay.options[this.cboDay.options.selectedIndex].value;
	this.month		= this.cboMonth.options[this.cboMonth.options.selectedIndex].value;
	this.year			= this.cboYear.options[this.cboYear.options.selectedIndex].value;
	var nextMonth	= false;
	var nextYear	= 0;
			
	if ((this.month == 1 || this.month == 3 || this.month == 5 || this.month == 7 || this.month == 8 || this.month == 10 || this.month == 12) && (this.day == 31)){
		this.day = 1;
		nextMonth = true;
	}else
		if ((this.month == 4 || this.month == 6 || this.month == 9 || this.month == 11) && (this.day == 30)){
			this.day = 1;
			nextMonth = true;
		}else
			if (this.month == 2)
				if (isLeapYear(this.year) && (this.day == 29)){
					this.day = 1;
					nextMonth = true;
				}else
					if (!isLeapYear(this.year) && this.day == 28){
						this.day = 1;
						nextMonth = true;
					}else
						this.day = parseInt(this.day) + 1;
			else
				this.day = parseInt(this.day) + 1;
						
	if (nextMonth && (this.month == 12)){
		this.year = parseInt(this.year) + 1;
		this.month = 1;
		if (this.year == parseInt(this.years[this.years.length - 1]) + 1)
			return;
	}else
		if (nextMonth)
			this.month = parseInt(this.month) + 1;
				
	this.cboYear.options.selectedIndex	= this.year - this.years[0];
	this.cboMonth.options.selectedIndex = this.month - 1;
	this.update();
	this.cboDay.options.selectedIndex	= this.day - 1;
}
function calGetPrev(){
	this.day			= this.cboDay.options[this.cboDay.options.selectedIndex].value;
	this.month		= this.cboMonth.options[this.cboMonth.options.selectedIndex].value;
	this.year			= this.cboYear.options[this.cboYear.options.selectedIndex].value;
	var prevMonth	= false;
	var prevYear	= 0;
		
	if ((this.month == 2 || this.month == 4 || this.month == 6  || this.month == 8 || this.month == 9 || this.month == 11) && (this.day == 1)){
		this.day = 31;
		prevMonth = true;
	}else
		if ((this.month == 1 || this.month == 5 || this.month == 7 || this.month == 10 || this.month == 12) && (this.day == 1)){
			this.day = 30;
			prevMonth = true;
		}else
			if (this.month == 3)
				if (isLeapYear(this.year) && (this.day == 1)){
					this.day = 29;
					prevMonth = true;
				}else
					if (this.day == 1){
						this.day = 28;
						prevMonth = true;
					}else
						this.day = parseInt(this.day) - 1;
			else
				this.day = parseInt(this.day) - 1;
					
	if (prevMonth && (this.month == 1)){
		this.year = parseInt(this.year) - 1;
		this.month = 12;
		if (this.year == this.years[0] - 1)
			return;
	}
	else
		if (prevMonth)
			this.month = parseInt(this.month) - 1;

	this.cboYear.options.selectedIndex	= this.year - this.years[0];
	this.cboMonth.options.selectedIndex = this.month - 1;
	this.update();
	this.cboDay.options.selectedIndex	= this.day - 1;
}
function GetDays(month, year){
	var days;

	if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
		days = 31;
	else
		if (month == 4 || month == 6 || month == 9 || month == 11)
			days = 30;
		else
			if (month == 2)  {
				if (isLeapYear(year))
					days = 29;
				else
					days = 28;
			}
	return days;
}
function isLeapYear(Year)
{
	if ((Year % 4 == 0) && (Year % 100 != 0) || (Year % 400 == 0))
		return true;
	return false;
}

