SF.QUICKTRIP = {

    FIELDNAMES : {
        startDate: 'Departure date',
        endDate: 'Return date',
        startCity: 'Departing from',
        endCity: 'Travelling to'
    },

    init: function() {
        this.$form = $('#quickTripSearchForm');
        this.setupForm();
        this.setupValidation();
    },

    setupForm: function() {
        var $tripType = this.$form.find('input[name="returnValue"]');

        this.$form.find('#domDepartureDate').val(SF.DATETIME.todaysDate());
        this.$form.find('#domReturnDate').val(SF.DATETIME.todaysDate());
        this.$form.find('#todaysDate').val(SF.DATETIME.todaysDate());
        this.$form.find('#domReturnDateHidden').val(SF.DATETIME.todaysDate());

        var strUrlDestination = SF.getQueryVariable('destination');
        
        if(strUrlDestination != '')
        {
            $('#alt_endCity option').each(function(){
                if($(this).text().toLowerCase()==strUrlDestination)
                {
                    $(this).attr('selected', 'selected');
                }
            });
        }

        var _this = this;

        $(document).ready(function(){
            SF.QUICKTRIP.$form.find('#domDepartureDate').datepicker({
                minDate: 'today',
                dateFormat: 'dd/mm/yy',
                onSelect: function() {
                    depDateVal = SF.DATETIME.stringToDate(_this.$form.find('#domDepartureDate').val());
                    retDateVal = SF.DATETIME.stringToDate(_this.$form.find('#domReturnDate').val());
                    if (retDateVal < depDateVal) {
                        _this.$form.find('#domReturnDate').val(_this.$form.find('#domDepartureDate').val())
                        return {minDate: depDateVal};
                    }
                }
            });

            SF.QUICKTRIP.$form.find('#domReturnDate').datepicker({
                minDate: 'today',
                dateFormat: 'dd/mm/yy',
                altField: '#domReturnDateHidden',
                altFormat: 'dd/mm/yy',
                beforeShow: function(){
                    var depDateVal = _this.$form.find('#domDepartureDate').val();
                    var newDateVal = SF.DATETIME.stringToDate(depDateVal);
                    return {minDate:newDateVal}
                }
            });
        });

        this.$form.find('#return').bind('click', function(){
            $tripType.val('R')
            SF.QUICKTRIP.$form.find('#domReturnDate').attr('disabled','');
        });

        this.$form.find('#domOneWay').bind('click', function(){
            $tripType.val('O');
            SF.QUICKTRIP.$form.find('#domReturnDate').attr('disabled','disabled');
        });

    },

    setupValidation: function() {
        
        var _this = this;

        // Check if return date is before depart date
        $.validator.addMethod('domDepartureArrivalDates', function()
        {
           var depDate = FCL.DATETIME.stringToDate(_this.$form.find('#domDepartureDate').val());
           var retDate = FCL.DATETIME.stringToDate(_this.$form.find('#domReturnDateHidden').val());

           return $('#domOneWay').is(':checked') || depDate  <= retDate ;


        }, $.format('Return date must be after departure date.'));

        // Check if depart date is 3 days from todays date NOTE - currently not being used
        $.validator.addMethod('domDepartureDateisValid', function() 
        {
            return FCL.DATETIME.stringToDate(_this.$form.find('#domDepartureDate').val()) > FCL.DATETIME.stringToDate(FCL.DATETIME.futureDateDays(3));
        }, $.format('Departure date must be at least three days from now.'));
            
        // Check that departure and destination locations are different
        $.validator.addMethod('domCheckDestination', function(value, element)
        {
            return _this.$form.find('#alt_startCity').val() != _this.$form.find('#alt_endCity').val();
        }, $.format('Your destination is the same as your departure city. Please revise your search.'));

        this.$form.validate({
            highlight: function(element, errorClass) {
                 $(element).css('background-color', 'LemonChiffon')
              },

            invalidHandler: function(form, validator) {
                var strError ='';
                var errorMessage = '';
                var intErrors = validator.numberOfInvalids();

                for (var formError in validator.errorMap) {
                    strError += SF.QUICKTRIP.FIELDNAMES[formError] + ': ' + validator.errorMap[formError] + '\n'
                }
                if (strError != '') {
                    alert(strError);
                };

            },

            rules: {
                startCity:{
                    required: true
                },
                startDate: {
                    domDepartureArrivalDates: true,
                    required: true
                },
                endDate: {
                    required: {
                        depends: function() {
                            return $('#quickTripSearchForm').find('#return').is(':checked');
                        }
                    }
                },
                endCity: {
                    domCheckDestination: true,
                    required: true
                }
            },
            submitHandler: function(form) {                
                form.submit();
            }

        });
    }
}

SF.QUICKTRIP.init();
