//Accepts ANY integers as date parameters
//Will clean date to nearest real value
//eg 29/02/2003 will be set to 28/02/2003
function DateTimeCleaner( year, month, day, hour, minute, second )
{	
	//Months are 0 based like the JavaScript Date() class
	var _year;
	var _month;
	var _day;
	var _hour;
	var _minute;
	var _second;
	var _daysInMonth = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
	var _arguments = arguments;
	
	initialize();
	
	//Expose public methods
	this.setYear = setYear;
	this.setMonth = setMonth;
	this.setDay = setDay;
	this.setHour = setHour;
	this.setMinute = setMinute;
	this.setSecond = setSecond;
	this.getCleanYear = getCleanYear;
	this.getCleanMonth = getCleanMonth;
	this.getCleanDay = getCleanDay;
	this.getCleanHour = getCleanHour;
	this.getCleanMinute = getCleanMinute;
	this.getCleanSecond = getCleanSecond;
	this.getCleanDate = getCleanDate;
	this.getCleanDateTime = getCleanDateTime;
	
	//Default to now
	function initialize()
	{
		var i;
		
		now = new Date();
		setYear(now.getFullYear());
		setMonth(now.getMonth());
		setDay(now.getDate());
		setHour(now.getHours());
		setMinute(now.getMinutes());
		setSecond(now.getSeconds());
		delete now;
		
		//Set the optional arguments
		for( i = 0; i < _arguments.length && i < 6; i++ )
		{
			switch( i )
			{
				case 0:
					setYear( _arguments[i] );
					break;
				case 1:
					setMonth( _arguments[i] );
					break;
				case 2:
					setDay( _arguments[i] );
					break;
				case 3:
					setHour( _arguments[i] );
					break;
				case 4:
					setMinute( _arguments[i] );
					break;
				case 5:
					setSecond( _arguments[i] );
					break;
			}
		}
	}
	
	function setYear( newValue )
	{
		newValue = Number( newValue );
		_year = newValue;
	}
	
	function setMonth( newValue )
	{
		newValue = Number( newValue );
		_month = newValue;	
	}
	
	function setDay( newValue )
	{
		newValue = Number( newValue );
		_day = newValue;	
	}
	
	function setHour( newValue )
	{
		newValue = Number( newValue );
		_hour = newValue;	
	}
	
	function setMinute( newValue )
	{
		newValue = Number( newValue );
		_minute = newValue;	
	}
	
	function setSecond( newValue )
	{
		newValue = Number( newValue );
		_second = newValue;	
	}
	
	function getCleanYear()
	{
		return _year;
	}
	
	function getCleanMonth()
	{
		if( _month < 0 ) return 0;
		else if( _month > 11 ) return 11;
		else return _month;
	}
	
	function getCleanDay()
	{
		var day = _day;
		var month = getCleanMonth();
		var year = getCleanYear();
		
		if( day < 1 ) 
		{
			day = 1;
		}
		else if( day > _daysInMonth[month])
		{
			day = _daysInMonth[month];
		}
		
		if( month == 1 && day == 29 && !isLeapYear( year ))
		{
			day = 28;
		}
		
		return day;
	}
	
	function isLeapYear( year )
	{ 
		if ((year % 4) == 0)
		{
			if ((year % 100) == 0)
			{
 				return ((year % 400) == 0); 
			}
			return true; 
		}
		return false; 
	} 
	
	function getCleanHour()
	{
		if( _hour < 0 ) return 0;
		else if( _hour > 23 ) return 23;
		else return _hour;
	}
	
	function getCleanMinute()
	{
		if( _minute < 0 ) return 0;
		else if( _minute > 59 ) return 59;
		else return _minute;
	}
	
	function getCleanSecond()
	{
		if( _second < 0 ) return 0;
		else if( _second > 59 ) return 59;
		else return _second;
	}
	
	function getCleanDate()
	{
		return new Date( getCleanYear(), getCleanMonth(), getCleanDay() );
	}
	
	function getCleanDateTime()
	{
		return new Date( getCleanYear(), getCleanMonth(), getCleanDay(), getCleanHour(), getCleanMinute(), getCleanSecond() );
	}
}

