/*
	Patch objet Date pour obtenir le numéro de la semaine
*/
Date.prototype.getWeek = function() 
{
	var onejan = new Date(this.getFullYear(),0,1);
	return Math.ceil((((this - onejan) / 86400000) + onejan.getDay())/7);
} 




function CDate( p_sDate )
{
	if ( typeof p_sDate != "undefined" )		this.setDate( p_sDate );
	else										this.setDate( new Date( ) );
}
CDate.prototype.oDate = new Date( );

CDate.prototype.setDate = function( p_mixDate )
{
	if ( typeof p_mixDate == "string" )
		this.oDate = this.importDate( p_mixDate );
	else
		this.oDate = p_mixDate;
}
CDate.prototype._importDate = function( p_sDate )
{
	var tmpDate = new Date( );
	var aDatetime = p_sDate.split( " " );
	var aDate = aDatetime[ 0 ].split( "/" );

	// On crée la date version "basique"
	tmpDate = new Date( aDate[2], (aDate[1]-1), aDate[0] );	

	// Heure présente ?
	if ( aDatetime.length > 1 )
	{
		var aTime = aDatetime[ 1 ].split( ":" );
		tmpDate.setHours( internalParseInt( aTime[0] ) )
		tmpDate.setMinutes( internalParseInt( aTime[1] ) )
		tmpDate.setSeconds( internalParseInt( aTime[2] ) )
	}
	return tmpDate;
}

/*
	RENVOIE LA DATE COURRANTE EN STRING

	Sauf :
		- Si p_oOptionnalDate est string, renvoie p_oOptionnalDate sous forme de Date() 
		- Si p_oOptionnalDate est Date( ), renvoie p_oOptionnalDate sous forme de string
	
*/
CDate.prototype.getDate = function( p_oOptionnalDate )
{
	if ( typeof p_oOptionnalDate != "undefined" && p_oOptionnalDate == null )			return "";
	if ( typeof p_oOptionnalDate == "string" )				return this._importDate( p_oOptionnalDate );
	else if ( typeof p_oOptionnalDate == "undefined" )		p_oOptionnalDate = this.oDate;
	if ( typeof p_oOptionnalDate == "undefined" )		return "";
	return ( p_oOptionnalDate.getDate() < 10 ? "0" + p_oOptionnalDate.getDate() : p_oOptionnalDate.getDate()) + "/" + (p_oOptionnalDate.getMonth() < 9 ? "0" + ( p_oOptionnalDate.getMonth( ) + 1 ) : ( p_oOptionnalDate.getMonth( ) + 1 ) ) + "/" + p_oOptionnalDate.getFullYear( );
}


CDate.prototype.getDateObject = function( )
{
	return CDate.getDate( this.getDate( ) );	// On force la création d'une nouvelle "instance" de l'objet date pour les rendres indépendants (sinon bonjour les bugs...)
}

CDate.prototype.getLastDayOfMonth = function( p_oDate )
{
	if ( typeof p_oDate == "string" )	p_oDate = CDate.getDate( p_oDate )

	var day;
	var year = p_oDate.getFullYear( );

	switch( p_oDate.getMonth( )+1 )
	{
		case 1 :
		case 3 :
		case 5 :
		case 7 :
		case 8 :
		case 10:
		case 12:
			day = 31;
			break;
		case 4 :
		case 6 :
		case 9 :
		case 11:
		   	day = 30;
			break;
		case 2 :
			if( ( (year % 4 == 0) && ( year % 100 != 0) ) 
                           || (year % 400 == 0) )
				day = 29;
			else
				day = 28;
			break;

	}

	return day;

}

// Permet de simuler le coté "static" et d'appeler direct CDate.getDate( ... ) ( _importDate etant lié, meme combat !)
CDate.getDate = CDate.prototype.getDate;
CDate.getLastDayOfMonth = CDate.prototype.getLastDayOfMonth;
CDate._importDate = CDate.prototype._importDate;

CDate.prototype.addYears = function( p_iYears )
{
	this.oDate.setYear( parseInt( this.oDate.getFullYear( ) ) + parseInt( p_iYears ) );
	return this.getDate( );
}

