﻿/*
    Handle the reservation form.  Heavily dependent on form's structure.
    Dependencies: 
        jquery.ui.datepicker.js
        Cyg.Framework.Web.DateTime.js
*/

ReservationForm = function() {

    // need to populate some hidden fields based on the new value
    ArrivalDate_change = function() {
        var arrival = new Date($("#ArrivalTextBox").val());
        var departure = new Date($("#DepartureTextBox").val());

        if (departure.getTime() <= arrival.getTime()) {
            var newDeparture = DateTime.addDays(arrival, 1);
            $("#DepartureTextBox").val(DateTime.shortDateFormat(newDeparture));
            departure = new Date($("#DepartureTextBox").val());
        }

        var days = DateTime.dateDiff(arrival, departure);
        $('#NightsTextBox').val(days);
    };

    var obj = {
        init: function() {
            $('input[type="text"].datetime').datepicker({
                minDate: (new Date()),
                dateFormat: 'm/d/yy',
                onShow: function() {
                    $('object[type="application/x-shockwave-flash"]').hide(); // Flash elements obstruct datepickers, so hide them.
                },
                onHide: function() {
                    $('object[type="application/x-shockwave-flash"]').show();
                }
            });

            // not valid XHTML Strict, do it like this so we validate
            $("#ReservationForm").attr('target', '_blank');

            // Initially hide the cancellation form, but show it if requested.
            $("#CancellationForm").hide();
            $("#ToggleResFormButton").click(function() {
                obj.toggle();
                return false;
            });

            // Event handlers
            $('#ArrivalTextBox,#DepartureTextBox').change(ArrivalDate_change);

            ArrivalDate_change();

            $('#ToggleResFormButton').show();
        },
        toggle: function() {
            var toggleButton = $("#ToggleResFormButton");

            if ($('#CancellationForm').is(':visible')) {
                $('#CancellationForm').hide();
                $('#BookingForm').fadeIn();
                toggleButton.text('View/Change a reservation');
                $('#ReservationForm')
                    .attr('action', 'https://res.windsurfercrs.com/bbe/page2.aspx')
                    .attr('method', 'get');
                $('#ReservationForm input:submit').val('Book a Room');
            }
            else {
                $('#BookingForm').hide();
                $('#CancellationForm').fadeIn();
                toggleButton.text('Book a reservation');
                $('#ReservationForm')
                    .attr('action', 'https://res.windsurfercrs.com/bbe/page5.aspx')
                    .attr('method', 'get');
                $('#ReservationForm input:submit').val('Find Reservation');
            }
        }
    }

    return obj;
} ();
