﻿var BookingForm;

$(document).ready(function () {

    BookingForm = {

        Dialog: $('#bookingDialog'),
        Form: $('form'),
        Panel: $('#bookingFormPanels'),
        HotelList: [],
        Buttons: {
            Back: $('.submit .back'),
            Book: $('.submit .book'),
            Next: $('.submit .next'),
            Pax: $('#bookingForm .pax')
        },

        Panels: {
            PersonalInfo: $('#personalInfo'),
            BookingInfo: $('#bookingInfo')
        },

        PersonalInfo: {
            FirstName: $('#personalInfo input:text:eq(0)'),
            Surname: $('#personalInfo input:text:eq(1)'),
            Email: $('#personalInfo input:text:eq(2)'),
            Phone: $('#personalInfo input:text:eq(3)'),
            Hotel: $('#personalInfo input:text:eq(4)'),
            Arrival: $('#personalInfo input:text:eq(5)'),
            ArrivalError: $('.errorDate'),
            AdultPax: $('#personalInfo input:text:eq(6)'),
            ChildPax: $('#personalInfo input:text:eq(7)')
        },

        BookingInfo:
        {
            AdultPax: $('#bookingInfo .adultPax'),
            ChildPax: $('#bookingInfo .childPax'),
            Departure: $('#bookingInfo .departureDate'),
            Details: $('#bookingInfo .details'),
            Excursion: $('#bookingInfo th :checkbox')
        },


        Init: function () {
            this.Dialog.dialog({
                autoOpen: false,
                draggable: true,
                height: 570,
                modal: true,
                open: function () {
                    User.Init();
                    $('#personalInfo table.dialog:first').show();
                    BookingForm.PersonalInfo.FirstName.val(User.FirstName);
                    BookingForm.PersonalInfo.Surname.val(User.Surname);
                    BookingForm.PersonalInfo.Email.val(User.Email);
                    BookingForm.Dialog.parent().appendTo(Master.Form);
                },
                beforeclose: function () {
                    $('#personalInfo table.dialog').hide();
                },
                resizable: false,
                width: 400
            });

            BookingForm.Dialog.parent().appendTo(Master.Form);

            this.Buttons.Back.click(function (e) {
                e.preventDefault();
                BookingForm.Panels.PersonalInfo.show();
                BookingForm.Panels.BookingInfo.hide();
            });

            this.Buttons.Next.click(function (e) {
                e.preventDefault()

                //get the date entered for comparison against today
                var regex = new RegExp('(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[012])/(20\\d\\d)');
                var match = regex.exec(BookingForm.PersonalInfo.Arrival.val());
                var arrival = new Date(match[3] + '/' + match[2] + '/' + match[1]);

                //validate the email address
                regex = new RegExp(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/);
                match = regex.test(BookingForm.PersonalInfo.Email.val());


                //if date is less than today show error
                if (arrival < new Date()) {
                    BookingForm.PersonalInfo.ArrivalError.show()
                }
                else {
                    BookingForm.PersonalInfo.ArrivalError.hide()
                }

                if (BookingForm.PersonalInfo.FirstName.val().length > 0 && BookingForm.PersonalInfo.Surname.val().length > 0 && BookingForm.PersonalInfo.Email.val().length > 0 && match && BookingForm.PersonalInfo.Hotel.val().length > 0 && BookingForm.PersonalInfo.Arrival.val().length > 0 && arrival > new Date()) {
                    BookingForm.Panels.BookingInfo.show();
                    BookingForm.Panels.PersonalInfo.hide();
                }
            });

            BookingForm.GetHotels();

            this.BookingInfo.Excursion.click(function () {
                if ($(this).is(':checked')) {
                    $(this).parent('th').next('td').find('.ctrl').attr('disabled', false);
                }
                else {
                    $(this).parent('th').next('td').find('.ctrl').attr('disabled', true);
                }
            });

            this.Buttons.Book.click(function (e) {
                if (!BookingForm.BookingInfo.Excursion.is(':checked')) {
                    e.preventDefault();
                }
            });
        },

        GetHotels: function () {
            $.ajax({
                type: 'POST',
                url: 'http://www.superjeep.is/Services/HotelService.svc/GetHotels',
                processData: false,
                contentType: "application/json; charset=utf-8",
                dataFilter: function (data) {
                    var msg;

                    if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function')
                        msg = JSON.parse(data);
                    else
                        msg = eval('(' + data + ')');

                    if (msg.hasOwnProperty('d'))
                        return msg.d;
                    else
                        return msg;
                },
                success: function (data) {
                    $.each(data, function (i, v) {
                        BookingForm.HotelList.push(v.Name);
                    });

                    BookingForm.PersonalInfo.Hotel.autocomplete({
                        source: BookingForm.HotelList
                    });
                }
            });
        }

    };

    BookingForm.Init();

});
