/**
* Time Selector Input
*
* An intelligent set of 2 combo boxes for entering dates.
* The boxes update according to which Hour/Minute has been selected
* to account for days in each month (including leap years).
*
* @author Bruce Mundin
* @version 0.1
*/

/**
* The Time 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 tmInput( objName, formName ) {
	
	
	/* Properties */
	this.objName 		= objName;
	this.today          = new Date();
	this.hour           = this.today.getHours();		/* The Displayed Day */
	this.mins           = this.today.getMinutes();		/* The Displayed Minutes */
	this.minsStep		= 5;
	
	this.formName 		= arguments[1] ? arguments[1] : 'none';
	this.hourObj			= '';
	this.minsObj		= '';
	
	/* Public Methods */
	this.setToToday			= tm_setToToday;
	this.setTime			= tm_setTime;
	this.setTimeParts		= tm_setTimeParts;
	this.getSelHour			= tm_getHour;
	this.getSelMins			= tm_getMins;

	/* Private Methods */
	this._initOptions		= tm_initOptions;
	this._writeHourOptions 	= tm_writeHourOptions;
	this._writeMinsOptions 	= tm_writeMinOptions;
	
	/* Constructor Code */
	if( this.formName == 'none' ) 
		{
		this.srcObj  = eval("document.forms[0]." + this.objName );
		this.hourObj = eval("document.forms[0]." + this.objName + "H");
		this.minsObj = eval("document.forms[0]." + this.objName + "M");
		} 
	else 
		{
		this.srcObj = eval("document.forms['" + this.formName + "']." + this.objName );
		this.hourObj = eval("document.forms['" + this.formName + "']." + this.objName + "H");
		this.minsObj = eval("document.forms['" + this.formName + "']." + this.objName + "M");
		}
			
	this._initOptions();
	
	// Set up the initial value
	if ( this.srcObj.value == null )
		{
		// Use todays date
		this.setToToday();
		}
	else
		{
		// Use the value
		this.setTime(this.srcObj.value);
		}
}

/* Class Methods */


/*
 * Expects it in HH:MM style.
 *
 */
function tm_setTime( timeStr ) {

	//alert ( "tm_setTime=" + timeStr );

	var h, m;
	
	// Find the ":" seperator
	var sepPos = timeStr.indexOf(":");
	
	// Read the hours
	h = timeStr.substr(0,sepPos);
	
	// Read the minutes
	m = timeStr.substr(sepPos + 1,2);
	
	// Set the time parts
	this.setTimeParts( h, m );
}

/**
* Set the date boxes to today's date
*/
function tm_setToToday() {
	this.setTimeParts( this.hour, this.mins );
}

/**
* Set the date boxes to today's date
* @param integer year
* @param integer month
* @param integer day
*/
function tm_setTimeParts( hour, mins ) 
	{
	// Make sure we are comparing numbers
	hour = new Number(hour);
	
	//alert ( "tm_setTimeParts(" +hour+"," +mins+")" );
	// Set the day
	for( i=0; i < this.hourObj.length; i++ ) 
		{
		
		if ( this.hourObj[i].value == hour )
			{
			this.hourObj[i].selected = true;
			break;
			}
		}
	
	mins = new Number(mins);
	// Set the month year
	mins = mins - ( mins % this.minsStep);

	for( i=0; i < this.minsObj.length; i++ ) 
		{
		this.minsObj[i].selected = ( padZero(this.minsObj[i].value) == mins );
		}

}


function tm_getHour() {

	var selectedVal = this.hourObj[this.hourObj.selectedIndex].value;
	return selectedVal;
}

function tm_getMins() 
	{
	return this.minsObj[this.minsObj.selectedIndex].value;
	}


/* Private Methods */

function tm_initOptions() {
	this._writeHourOptions();
	this._writeMinsOptions();
}

function tm_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 tm_writeHourOptions() 
	{
	for( i= 0; 
		 i<= 23 ; 
		 i++ ) {
		var newOption = new Option( padZero(i) , i );
		var optionsColl = this.hourObj.options;
		optionsColl[optionsColl.length] = newOption;
	}
}

function tm_writeMinOptions() 
	{
	for( i= 0; 
		 i<= 59 ; 
		 i=i+this.minsStep ) {
		var newOption = new Option( padZero(i), i );
		var optionsColl = this.minsObj.options;
		optionsColl[optionsColl.length] = newOption;
	}
}


	// utility function
function padZero(num) {
	  return ((num <= 9) ? ("0" + num) : num);
	}
