/**
* bookroom.js
*
* @author
* kkim@aegworldwide.com
* 04 - 20 - 2010
*
* @internal
* this script is for the module book a room, where it will enable the toggle function of selecting the hotel,
* the calendar pop-up for the check in and check out dates, and the dropdown for the number of guests and room.
*
*/

var bookModule = 
{
	/**
	* @internal
	* This is the initialize function that will set the options for the datepicker module, Class
	* and bind the handlers to the dropdowns and hotel toggle
	*
	* @param
	* empty.
	*	
	* @return
	* NONE.
	*
	*/
	"bookRoomInitialize":function( )
	{
		var options =
		{
			"dateFormat"		: "mm/dd/yy",
			"showOn"			: "focus"
			//"buttonImage"		: "../media/images/calendar.jpg",
			//"buttonImageOnly"	: true
		};	

		$( '.dateInput' ).datepicker( options );

		// this statement is used to remove the default value of the date input for guest and rooms, and insert the default if it is left blank
		$( 'form[name="bookRoom"]' ).find( 'input' ).bind( 'focusin', function( ) 
		{
			if( $( this ).val( ) == $( this ).attr( 'title' ) ){ $( this ).val( '' ); }
		})
		.bind( 'focusout', function( )
		{ 
			if( $( this ).val( ).length == 0 ){ $( this ).val( $( this ).attr( 'title' ) ); } 
		} );// end of date input focusin and out

		$( '#submitReservations' ).bind( 'click', function() { bookModule.validation( ); } );
	}, //end bookRoomInitialize.


	/**
	* @internal
	* This function validates the inputs, where if there are errors it will output the error, 
	* if ther are no errors it will submit the form.
	*
	* @param
	* empty.
	*
	* @return
	* empty.
	*
	*/
	"validation":function()
	{
		var fromdate = $( 'input[name="fromDate"]' ).val( );
		var todate = $( 'input[name="toDate"]' ).val( );
		var errorText = '';
		var error = 0;

		// Validates the Check-In input field for MM/DD/YY form, and if that passes checks if the input date is not in the past (less than current date)
		if( !/(\d){2}(\/)(\d){2}(\/)(\d){4}/.test( fromdate ) ){ errorText += 'Please enter a valid Check-In date in "mm/dd/yyyy" format.\n'; ++error; }

		else if( new Date( fromdate ) < new Date( ) ){ errorText += 'Please select a later date.\n'; ++error; }

		/** Validates the Check-Out input field for MM/DD/YY form, and if that passes checks if the input date is not in the past (less than current date).
		*	If all that passes, then Check-Out is compared to the Check-In date to see if the Check-Out date is greater than the Check-In date.
		**/
		if( !/(\d){2}(\/)(\d){2}(\/)(\d){4}/.test( todate ) ){ errorText += 'Please enter a valid Check-Out date in "mm/dd/yyyy" format.\n'; ++error; }

		else if( new Date( todate ) < new Date( ) ){ errorText += 'Check-Out date must be from today onwards.\n'; ++error; }

		else if( new Date( todate ) < new Date( fromdate ) ){ errorText += 'Your Check-Out date must be after your Check-In date.\n'; ++error; }

		// Reports error is there are any, else submits form
		if( error > 0 ){ alert( errorText ); }

		// Submits the form if there are no errors
		else if( error == 0 ){ $( 'form[name="bookRoom"]' ).submit(); }
	} // end validation.
}; // end bookModule.

