/**
* Date Selector Input
*
* An intelligent set of 3 combo boxes for entering dates.
* The boxes update according to which day/month/year has been selected
* to account for days in each month (including leap years).
*
* @author Kevin Southworth <southwo8@msu.edu>
* @link http://kevin.tridubdesign.com
* @modified 2004-04-06
* @version 0.2
*/

/**
* The date selector constructor
*
* @access public
* @param string objName      Name of the object that you create
* @param string formName	Name of the form this object is in (OPTIONAL)
*/
function dsInput( objName, formName ) {
	
	/* Properties */
	this.objName 		= objName;
	this.today          = new Date();
	this.date           = this.today.getDate();			/* The Displayed Day */
	this.month          = this.today.getMonth()+1;		/* The Displayed Month */
	this.year           = this.today.getFullYear();		/* The Displayed Year */
	if(this.year < 2000) this.year += 1900; //for Netscape
	this.yearComboRange = 5;
	
	this.formName 		= arguments[1] ? arguments[1] : 'none';
	this.dayObj			= '';
	this.monthObj		= '';
	this.yearObj		= '';
	this.monthNames		= new Array( '','January','February','March','April','May','June','July','August','September','October','November','December' );
	
	this.startDateRange = null ;	/* If not set then there is no date limit. */
	this.endDateRange   = null ;
	
	/* Public Methods */
	this.setToToday			= ds_setToToday;
	this.adjustDaysInMonth 	= ds_adjustDaysInMonth;
	this.setDate			= ds_setDateYYYYMMDD;
	this.setDateDDMMYYYY	= ds_setDateDDMMYYYY;
	this.setDateParts		= ds_setDateParts;
	this.getSelYear			= ds_getYear;
	this.getSelMonth		= ds_getMonth;
	this.getSelDay			= ds_getDay;
	this.setDateRange    	= ds_setDateRange;

	/* Private Methods */
	this._initOptions		= ds_initOptions;
	this._writeYearOptions 	= ds_writeYearOptions;
	this._writeMonthOptions = ds_writeMonthOptions;
	this._writeMonthYearOptions = ds_writeMonthYearOptions;
	this._writeDayOptions 	= ds_writeDayOptions;
	this._getDaysInMonth	= ds_getDaysInMonth;
	
	/* Constructor Code */
	if( this.formName == 'none' ) 
		{
		this.srcObj  	  = eval("document.forms[0]." + this.objName );
		this.dayObj 	  = eval("document.forms[0]." + this.objName + "D");
		this.monthObj 	  = eval("document.forms[0]." + this.objName + "M");
		this.yearObj 	  = eval("document.forms[0]." + this.objName + "Y");
		this.monthyearObj = eval("document.forms[0]." + this.objName + "MY");
		} 
	else 
		{
		this.srcObj 	  = eval("document.forms['" + this.formName + "']." + this.objName );
		this.dayObj       = eval("document.forms['" + this.formName + "']." + this.objName + "D");
		this.monthObj     = eval("document.forms['" + this.formName + "']." + this.objName + "M");
		this.yearObj      = eval("document.forms['" + this.formName + "']." + this.objName + "Y");	
		this.monthyearObj = eval("document.forms['" + this.formName + "']." + this.objName + "MY");
		}	
	this._initOptions();
	
	// Set up the initial value
	if ( this.srcObj.value == null )
		{
		// Use todays date
		this.setToToday();
		}
	else
		{
		// Use the value
		this.setDateDDMMYYYY(this.srcObj.value);
		}
	
	this.adjustDaysInMonth();	
}

/* Class Methods */

/**
 * Set the date boxes to specific date
 * 
 * @param strStartDate Start date range for calendar (in yyyy-MM-dd format)
 * @param strEndDate End date range for calendar (in yyyy-MM-dd format)
 *
 */
function ds_setDateRange(strStartDate,strEndDate)
	{
	this.startDateRange = new Date();
	this.endDateRange   = new Date();

	var y, m, d;
	y = strStartDate.substr(0,4);
	m = strStartDate.substr(5,2);
	d = strStartDate.substr(8,2);
	this.startDateRange.setFullYear(new Number(y));	/* Not sure why but add on to year works */
	this.startDateRange.setMonth(new Number(m)-1);
	this.startDateRange.setDate(d);

	y = strEndDate.substr(0,4);
	m = strEndDate.substr(5,2);
	d = strEndDate.substr(8,2);
	this.endDateRange.setYear(new Number(y));	/* Not sure why but add on to year works */
	this.endDateRange.setMonth(new Number(m)-1);
	this.endDateRange.setDate(d);
	
	//alert ( "month" + strStartDate + "," + m  );
	
	this._initOptions();	
	this.adjustDaysInMonth();
	}

/**
 * Set the date boxes to specific date
 * 
 * @param dateStr Start date range for calendar (in yyyy-MM-dd format)
 *
 */
function ds_setDateYYYYMMDD( dateStr ) 
	{
	//alert ( dateStr );
	
	var y, m, d;
	y = dateStr.substr(0,4);
	m = dateStr.substr(5,2);
	d = dateStr.substr(8,2);
	this.setDateParts( y, m, d );
	}

/**
* Set the date boxes to today's date
*/
function ds_setToToday() {
	this.setDateParts( this.year, this.month, this.date );
}
/**
* Set the date boxes to specific date
* @param string dateStr 'DD-MM-YYYY'
*/
function ds_setDateDDMMYYYY( dateStr ) {
	var y, m, d;
	d = dateStr.substr(0,2);
	m = dateStr.substr(3,2);
	y = dateStr.substr(6,4);
	
	this.setDateParts( y, m, d );
}
/**
* Set the date boxes to today's date
* @param integer year
* @param integer month
* @param integer day
*/
function ds_setDateParts( year, month, date ) 
	{
	//alert ( "ds_setDateParts(" +year+"," +month+"," +date+ ")" );
	
	// Ensure the date, month & year are numbers
	year = new Number(year);
	month = new Number(month);
	date = new Number(date);
	
	// Set the day
	for( i=0; i < this.dayObj.length; i++ ) 
		{
		if( this.dayObj[i].value == date )
			this.dayObj[i].selected = true;
		}
	
	// Set the month year
	if ( this.monthyearObj == null )
		{
		this.monthObj[month-1].selected = true;
		for( i=0; i < this.yearObj.length; i++ ) {
			if( this.yearObj[i].value == year )
				this.yearObj[i].selected = true;
			}
		}
	else
		{
		// Find the correct value
		var targetVal = year + "-" + month ;
		//alert ( "ds_setDateParts : targetVal=" + targetVal + " len=" + this.monthyearObj.length );
		
		for( i=0; i < this.monthyearObj.length; i++ ) 
			{
			if( this.monthyearObj[i].value == targetVal )
				{
				//alert ( "ds_setDateParts: SELECT: val=" + this.monthyearObj[i].value + "idx:" + i );
				this.monthyearObj.selectedIndex = i ;
				//alert ( "ds_setDateParts: SELECTED:" + this.monthyearObj[this.monthyearObj.selectedIndex].value );
				}
			}
		
		}
	this.adjustDaysInMonth();
}


function ds_getYear() {

	if ( this.monthyearObj == null )
		{
		Year = this.yearObj[this.yearObj.selectedIndex].value;
		}
	else
		{
		var selectedVal = this.monthyearObj[this.monthyearObj.selectedIndex].value;
		Year  = selectedVal.substr(0,4);
		}

	return Year;
}

function ds_getMonth() {

	if ( this.monthyearObj == null )
		{
		Month = this.monthObj[this.monthObj.selectedIndex].value;
		}
	else
		{
		var selectedVal = this.monthyearObj[this.monthyearObj.selectedIndex].value;
		Month = selectedVal.substr(5,2);
		}

	return Month;
}

function ds_getDay() {
	return this.dayObj[this.dayObj.selectedIndex].value;;
}

/**
* Adjust the 'days' box according to the
* current month and year
*/
function ds_adjustDaysInMonth() 
{
	//alert ( "ds_adjustDaysInMonth" );
	
	var startDay = 1 ;
	
	Year  = this.getSelYear();
	Month = this.getSelMonth();

	//alert ( "ds_adjustDaysInMonth = " + Year + "-" + Month );

		
	DaysForThisSelection = this._getDaysInMonth(Month, Year);
	PrevDaysInSelection = this.dayObj.length;
	
	if ( ( this.startDateRange != null ) && ( this.endDateRange != null ) )
		{
		// Check to see if we need to trim the set further
		if ( ( Year == this.endDateRange.getFullYear() )
		     &&
		     ( Month == (this.endDateRange.getMonth()+1) ) )
			{
			DaysForThisSelection = this.endDateRange.getDate();
			}
			
		// See if we need to remove the start items
		if ( ( Year == this.startDateRange.getFullYear() )
		     &&
		     ( Month == (this.startDateRange.getMonth()+1) ) )
			{
			startDay =  this.startDateRange.getDate();
			}
			
		}
	
	var origSelected = new Number(this.getSelDay());
	
	//alert ( "ds_adjustDaysInMonth, origSelected=" + origSelected );
	
	var newSelected  = 0 ;
	if (origSelected < 0)
		origSelected = 0 ;

	// Remove the options		
	this.dayObj.options.length = 0 ;
	for( i = startDay ; i <= DaysForThisSelection; i++ ) 
		{
		var newOption = new Option( indentZero(i) , i );
		var optionsColl = this.dayObj.options;
		optionsColl[optionsColl.length] = newOption;
		
		if ( origSelected == i )
			{
			// Select this item
			newSelected = optionsColl.length - 1 ; /* zeo based index */
			}
			
		}

	//alert ( "ds_adjustDaysInMonth, newSelected=" + newSelected );

	this.dayObj.selectedIndex = newSelected;

	return ;	
}

/* Private Methods */

function ds_initOptions() {
	if ( this.monthyearObj == null )
		{
		// Seperate month and year
		this._writeYearOptions();
		this._writeMonthOptions();
		}
	else
		{
		// Single month year
		this._writeMonthYearOptions();
		}
	this._writeDayOptions();
}

function ds_getDaysInMonth( m, y ) {
	monthdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
	if (m != 2) {
		return monthdays[m];
	} else {
		return ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0 ? 29 : 28);
	}
}


function ds_writeMonthYearOptions() 
	{
	//alert ( "ds_writeMonthYearOptions START:" + this.monthyearObj.options.length );
	var startYear = (this.year-this.yearComboRange);
	var endYear   = (this.year+this.yearComboRange);
	var startMonth = 1 ;
	var endMonth   = 12 ;
	
	if ( ( this.startDateRange != null ) && ( this.endDateRange != null ) )
		{
		// Use these date ranges
		startYear = this.startDateRange.getFullYear();
		endYear   = this.endDateRange.getFullYear();
		
		startMonth = this.startDateRange.getMonth()+1;
		endMonth   = this.endDateRange.getMonth()+1;
		}
	
	//alert ( "ds_writeMonthYearOptions=" + startYear + "-" + startMonth + "," + endYear + "-" + endMonth  );

	var selectedVal = "";
	
	if ( this.monthyearObj.selectedIndex >= 0 )
		selectedVal = this.monthyearObj[this.monthyearObj.selectedIndex].value;
	//alert ( "ds_writeMonthYearOptions selectedVal=" + selectedVal );
	
	
	// Clear any existing
	this.monthyearObj.options.length = 0 ;
	this.monthyearObj.selectedIndex	= 0 ;
	//alert ( "ds_writeMonthYearOptions CLEARED:" + this.monthyearObj.options.length );
	
	var optionsColl = this.monthyearObj.options;
	
	for( year= startYear; 
		 year<= endYear ; 
		 year++ ) 
		 {
		 var thisYearStartMonth = 1 ;
		 var thisYearEndMonth = 12 ;
		 
		 // Adjust the range
		 if ( year == startYear )  thisYearStartMonth = startMonth ;
		 if ( year == endYear	)  thisYearEndMonth   = endMonth ;
		 
		 //alert ( year + ": " + thisYearStartMonth + " to " + thisYearEndMonth );
		 
		 // Loop around all months for this year
		for( mon=thisYearStartMonth; mon <= thisYearEndMonth; mon++ ) 
			{
			var newValue = year + "-" + mon ;
			var newOption = new Option( this.monthNames[mon] + " " + year , newValue );
						  
			optionsColl[optionsColl.length] = newOption;
			
			if ( newValue == selectedVal )
				{
				this.monthyearObj.selectedIndex	= optionsColl.length - 1 ;
				//alert ( "ds_writeMonthYearOptions SELECT:" + this.monthyearObj[this.monthyearObj.selectedIndex].value );
				}
			}
		}
	
	if ( selectedVal == "" )	this.monthyearObj.selectedIndex = 0 ;
		
	//alert ( "ds_writeMonthYearOptions END:" + this.monthyearObj.options.length );
	//alert ( "ds_writeMonthYearOptions END:" + this.monthyearObj[this.monthyearObj.selectedIndex].value );
	
	}
	
function ds_writeYearOptions() 
	{
	var startYear = (this.year-this.yearComboRange);
	var endYear   = (this.year+this.yearComboRange);
	
	if ( ( this.startDateRange != null ) && ( this.endDateRange != null ) )
		{
		// Use these date ranges
		startYear = this.startDateRange.getFullYear();
		endYear   = this.endDateRange.getFullYear();
		}
	
	// Clear any existing
	this.yearObj.options.length = 0 ;
	
	for( i= startYear; 
		 i<= endYear ; 
		 i++ ) {
		var newOption = new Option( i, i );
		var optionsColl = this.yearObj.options;
		optionsColl[optionsColl.length] = newOption;
	}
}

function ds_writeMonthOptions() {
	for( i=1; i <= 12; i++ ) {
		var newOption = new Option( this.monthNames[i], i );
		var optionsColl = this.monthObj.options;
		optionsColl[optionsColl.length] = newOption;
	}
}

function ds_writeDayOptions() 
	{
	for( i=1; i <= 31; i++ ) 
		{
		var newOption = new Option( indentZero(i), i );
		var optionsColl = this.dayObj.options;
		optionsColl[optionsColl.length] = newOption;
		}
	}

	// utility function
function indentZero(num) {
	  return ((num <= 9) ? ("0" + num) : num);
	}
