
function DateListGroup(){
	if(arguments.length==2){
		this.prototype=DateListGroup2;
		this.prototype(arguments[0],arguments[1]);
	}else if(arguments.length==3){
	this.prototype=DateListGroup3;
		this.prototype(arguments[0],arguments[1],arguments[2]);
	}else{
		alert("invalid call to DateListGroup");
	}
}

//DateListGroup constructor
function DateListGroup2(dayList,monthYearList){

	var thisObj=this;
	this.dayList=typeof dayList=="string" ? document.getElementById(dayList) : dayList;
	this.dayList.set= function(dayOfMonth){
		this.selectedIndex=dayOfMonth-1;
	}
			
	this.monthYearList=typeof monthYearList=="string" ? document.getElementById(monthYearList) : monthYearList;
	this.monthYearList.set=function (monthYearValue){
		for(var i=0;i<this.options.length;i++){
			if(this.options[i].value == monthYearValue){
				this.selectedIndex=i;
				break;
			}
		}
	}
	
	this.setDate=function(d){
		this.setDayListOptions(d.getMonth()+1,d.getFullYear);
		this.dayList.set(d.getDate());
		this.monthYearList.set(d.toIso8601().substr(0,7));
	};

	this.setDate2=function(dayValue,monthYearValue){
		this.setDayListOptions(parseInt(monthYearValue.substr(5,2),10),parseInt(monthYearValue.substr(0,4),10));
		this.dayList.set(dayValue);
		this.monthYearList.set(monthYearValue);
	};		
	
	this.setDateIso8601=function(sDtIso8601){
		this.setDayListOptions(parseInt(sDtIso8601.substr(5,2),10),parseInt(sDtIso8601.substr(0,4),10));
		this.dayList.set(sDtIso8601.substr(8));
		this.monthYearList.set(sDtIso8601.substr(0,7));
	}
	
	this.getDate=function(){
		var d=new Date();
		d.parseFromIso8601(this.monthYearList.value+"-"+this.dayList.value);
		return d;
	};
	
	this.setDayListOptions=function(m,y){

		var month= m!=undefined ? m : parseInt(this.monthYearList.value.substr(5,2),10);
		var yearValue= y!=undefined ? y : parseInt(this.monthYearList.value.substr(0,4),10);
		
		var dys=daysInMonth( month, yearValue);
		
		var oldSelectedIndex=this.dayList.selectedIndex;
		this.dayList.options.length=0;
		for(var i=0;i<dys;i++){
			if(i==oldSelectedIndex)
				this.dayList.options[i]=new Option(i+1, paddedDigit(i+1), 1, true)
			else
				this.dayList.options[i]=new Option(i+1, paddedDigit(i+1), 1, false)
		}

	}
	this.monthYearList.onchange=function(){
		thisObj.setDayListOptions();
	}
	
	
}

function DateListGroup3(dayList,monthList,yearList){
	
	var thisObj=this;
	this.dayList=typeof dayList=="string" ? document.getElementById(dayList) : dayList;
	this.dayList.set= function(dayOfMonth){
		this.selectedIndex=dayOfMonth-1;
	}
			
	this.monthList=typeof monthList=="string" ? document.getElementById(monthList) : monthList;
	
	this.monthList.set=function (monthNumber){
		this.selectedIndex=monthNumber-1;
	}
	
	this.yearList=typeof yearList=="string" ? document.getElementById(yearList) : yearList;
	
	this.yearList.set=function (yearNumber){
		for(var i=0;i<this.options.length;i++){
			if(this.options[i].value == yearNumber){
				this.selectedIndex=i;
				break;
			}
		}
	}
		
	this.setDate=function(d){
		this.setDayListOptions(d.getMonth()+1,d.getFullYear);
		this.dayList.set(d.getDate());
		this.monthList.set(d.getMonth()+1);
		this.yearList.set(d.getFullYear);
	};

	this.setDate2=function(dayValue,monthValue,yearValue){
		this.setDayListOptions(monthValue,yearValue);
		this.dayList.set(dayValue);
		this.monthList.set(monthValue);
		this.yearList.set(yearValue);
	};
	
	this.setDateIso8601=function(sDtIso8601){
		this.setDayListOptions(parseInt(sDtIso8601.substr(5,2),10),parseInt(sDtIso8601.substr(0,4),10));
		this.dayList.set(sDtIso8601.substr(8));
		this.monthList.set(sDtIso8601.substr(5,2));
		this.yearList.set(sDtIso8601.substr(0,4));
	}
	
	this.getDate=function(){
		var d=new Date();
		d.parseFromIso8601(this.yearList.value+"-"+paddedDigit(this.monthList.value)+"-"+paddedDigit(this.dayList.value));
		return d;
	};
	
	this.setDayListOptions=function(m,y){

		var month= m!=undefined ? m : parseInt(this.monthYearList.value.substr(5,2),10);
		var yearValue= y!=undefined ? y : parseInt(this.monthYearList.value.substr(0,4),10);
		
		var dys=daysInMonth( month, yearValue);
		
		var oldSelectedIndex=this.dayList.selectedIndex;
		this.dayList.options.length=0;
		for(var i=0;i<dys;i++){
			if(i==oldSelectedIndex)
				this.dayList.options[i]=new Option(i+1, paddedDigit(i+1), 1, true)
			else
				this.dayList.options[i]=new Option(i+1, paddedDigit(i+1), 1, false)
		}

	}

	this.monthList.onchange=function(){
		thisObj.setDayListOptions(thisObj.getDate().getMonth()+1,thisObj.getDate().getFullYear());
	}
		
	
}
