/**
 * Cars Search form
 *
 * @author          Kate Bascombe
 * @modifiedby      $LastChangedBy: blundenr $
 * @copyright       Copyright Flight Centre Ltd. All rights reserved.
 * @version         $Revision: 6 $
 * @lastmodified    $Date: 2011-01-23 21:15:17 +1000 (Sun, 23 Jan 2011) $
 * @requires        FCL, FCL.UTIL, FCL.FORMS, TT.SEARCH
 */

;TT.CARS = $.extend(true, {}, TT.SEARCH,
{
    name: 'TT.CARS',
	
	init: function() 
	{
		var _this = this;
		this.$form = $('#carsForm');

		this.setupCarsForm();
		
		FCL.SIGNALS.listen('TT.UTIL', 'deserializeForm', function(vars)
		{
			if (vars[1].area1DestinationInverse) {
				_this.destinations[vars[1].area1] = vars[1].area1DestinationInverse;
			}
			if (vars[1].area2DestinationInverse) {
				_this.destinations[vars[1].area2] = vars[1].area2DestinationInverse;
			}
		});
	},
	
	setupCarsForm: function()
    {
		// Add datepickers
		this.departReturnDatePickers('cars_date1', 'cars_date2');
		
		this.setupCarsValidation();
		this.setup();
    },
	
	setupCarsValidation: function()
    {
		var _this = this;
		
		//Check that the drop off time is not before the pick up time on the same day
		$.validator.addMethod('timeCheck', function(value, element, params)
        {
			if($(this.currentForm).find('input[name="date1"]').val() === $(this.currentForm).find('input[name="date2"]').val()){
				return !(parseInt($(this.currentForm).find('select[name="time2"]').val()) < parseInt($(this.currentForm).find('select[name="time1"]').val()))
			}else{
				return true;
			};
        }, $.format('Your drop off time must be after your pick up time'));
		
		// Adjust field mappings
		this.fieldMappings.area1 = 'Pick Up Location';
		this.fieldMappings.date1 = 'Pick Up Date';
		this.fieldMappings.date2 = 'Drop Off Date';
		this.fieldMappings.time1 = 'Time';
		
		//Adjust rules
		this.validationRules.rules.area2.required = false;
		this.validationRules.rules.area2.area2Location = false;
		this.validationRules.rules.time1 = { timeCheck: true};
		
		
		// Adjust messages
		this.validationRules.messages.area1 = 'Please select a pick up location';
		this.validationRules.messages.date1 = 'Your pick up date must be in the format dd/mm/yyyy';
		this.validationRules.messages.date2 = 'Your return date must be in the format dd/mm/yyyy';
		
		this.setupValidation();
    }
});
