//-----
//	
//-----
function getDayCount( sIdStart, sIdEnd )
{
	nStart	= parseDate( $( sIdStart ).val() );
	nEnd	= parseDate( $( sIdEnd ).val() );
	
	return daydiff( nStart, nEnd );
}

//-----
//	
//-----
function parseDate( str ) 
{
    var mdy	= str.split( '/' )
alert( str );
    return new Date( mdy[2], mdy[0]-1, mdy[1] );
}

//-----
//	
//-----
function daydiff( first, second ) 
{
    return ( second - first ) / ( 1000*60*60*24 );
}



//-----
//	Convert International to US (or vice versa) date format.
//-----
function voodooSwapMonths( sDate )
{
	aDate	= sDate.split( "/" );
	
	return aDate[1] + "/" + aDate[0] + "/" + aDate[2];
}

//-----
//	Add days to a date.
//-----
function voodooAddDays( dtDate, nDays )
{
	var voodooDate = new Date( dtDate );
	
		voodooDate.setDate( voodooDate.getDate() + nDays );
		
	var vFormat				= "MM/dd/yyyy";
	var vDay				= voodooAddZero( voodooDate.getDate() ); 
	var vMonth				= voodooAddZero( voodooDate.getMonth() + 1 ); 
	var vYearLong			= voodooAddZero( voodooDate.getFullYear() ); 
	var vYearShort			= voodooAddZero( voodooDate.getFullYear().toString().substring( 3, 4 ) ); 
	var vYear				= ( vFormat.indexOf( "yyyy" ) >-1 ? vYearLong : vYearShort ) 
	var vHour				= voodooAddZero( voodooDate.getHours() ); 
	var vMinute				= voodooAddZero( voodooDate.getMinutes() ); 
	var vSecond				= voodooAddZero( voodooDate.getSeconds() ); 
	var voodooDateString	= vFormat.replace( /dd/g, vDay ).replace( /MM/g, vMonth ).replace( /y{1,4}/g, vYear ) 
	
		voodooDateString	= voodooDateString.replace( /hh/g, vHour ).replace( /mm/g, vMinute ).replace( /ss/g, vSecond ) 
	
	return voodooDateString;
}

//-----
//	Add a zero to numbers less than 10.
//-----
function voodooAddZero( vNumber )
{ 
	return ( ( vNumber < 10 ) ? "0" : "" ) + vNumber 
} 

//-----
//	Page script.
//-----
$( document ).ready
(
	function()
	{
		$( "#arrivalDate" ).datepicker( { dateFormat: 'dd/mm/yy', altFormat: 'yy-mm-dd' } );
		$( "#departureDate" ).datepicker( { dateFormat: 'dd/mm/yy', altFormat: 'yy-mm-dd' } );
		
		$( "#arrivalDate" ).change
		(
		 	function()
			{
				dtArrive		= voodooSwapMonths( $( "#arrivalDate" ).val() );
				
				$( "#departureDate" ).val( voodooSwapMonths( voodooAddDays( dtArrive, 7 ) ) );
			}
		)
		
		$( "#searchForm" ).validate
		(
			{
				rules: 
				{
					departureDate: { required: true, dateComparison: true },
					arrivalDate: { required:true, dateStart: true }
				}
			}
		);
		
		//	Minimum booking period.
		$.validator.addMethod
		(
			"minBookingPeriod",
			function( value, element )
			{
				var nCount	= getDayCount( '#arrivalDate', '#departureDate' )

				return nCount >= 3;
			}, 
			"Minimum booking period of 3 days is required."
		);		
		
		//	End date should be later than the start date.
		$.validator.addMethod
		(
			"dateComparison",
			function(value, element) 
			{
				var x	= voodooSwapMonths( $( "#arrivalDate" ).val() );
				var y	= voodooSwapMonths( $( "#departureDate" ).val() );
				
				return Date.parse( x ) < Date.parse( y );
			}, 
			"End Date should be greater than Start Date."
		);
		
		//	End date should be greater than or equal today.
		$.validator.addMethod
		(
		 	"dateStart",
			function( value, element )
			{
				var x	= voodooSwapMonths( $( "#arrivalDate" ).val() );
				var y	= new Date();

				return Date.parse( x ) >= Date.parse( ( y.getMonth() + 1 ) + "/" + y.getDate() + "/" + y.getFullYear() );
			},
			"Start Date, should be greater than or equal to today"
		);
	}
)
