define(
    [ 'jQuery', 'asyncSeries', 'testUtilities', 'scvMockJaxCalls' ],
    function ($$, asyncSeries, utilities, scvMockJaxCalls) {

        function patientSelect(name) {
            // This method is used by patient viewer to search and select a
            // patient before proceeding
            if (window.location.href.indexOf('scheduling-manager')) {
                var e = $.Event("keypress"), $input = $("#patient-search-input");
                e.which = e.keyCode = 13;
                $input.val(name).trigger(e);

            }
        }

        module("SCV56, SCV597,SCV-78 |SCV589,SCV591, SCV-590,SCV743, SCV744, SCV-745- Clerk can see available appts, Book appts, Booking notes");
        test('SCV594 - Validate book appointment for a patient, Validate Booking notes',
            function () {
                ok(true, "Tests are running.");

                $(window.load, function () {
                    asyncSeries(function () {
                        afterElementLoad($("#calendar-specialty-input"), function () {
                            ok($('#left-navigation').is(':visible'),
                                "Header is visible");
                        });

                        // Then I can see the header info
                        ok($('#left-navigation:contains("Open Menu")').text(),
                            "Then I can see the header bar");
                        return 1500;
                    }, function () {
                        afterElementLoad($("#inner-content p:contains('Please select a Service/Specialty and Clinic to view availability.')"), function () {
                            ok($("#inner-content p:contains('Please select a Service/Specialty and Clinic to view availability.')")
                                    .text(),
                                'Select Service/Specialty and Clinic to display calendar');
                        });

                        // Select Service/Specialty and Clinic to
                        // show calendar
                        $("#calendar-specialty-input").val('General')
                            .keyup();
                        $("#calendar-specialty li:nth-child(4) a")
                            .trigger('tap');

                        $('#calendar-clinic-button')
                            .trigger('click');

                        // Select Clinic
                        $("#calendar-clinic-menu label").eq(0)
                            .trigger('click');
                        return 500;
                    }, function () {
                        // Click View Availability to display the
                        // calendar
                        $("#appointment-button a").trigger('tap');
                        afterElementLoad($("#calendar"), function () {
                            ok($("#calendar")
                                    .is(':visible'),
                                'Calendar is visible');
                        });

                    }, function () {
                        $('.fc-button-next').trigger('click');

                        ok($('td').hasClass('available'),
                            'Appointment slot available');


                        //Click the available slot and validate the book appointment dialog
                        $("#calendar").find('.available').eq(1).trigger('click');
                        return 500;
                    }, function () {
                        //Day view is displayed
                        equal($("#calendar").find('button').eq(5).hasClass('fc-state-active'), true, 'Day view');
                        //Search and select patient
                        var e = $.Event("keypress");
                        e.which = 13; // # Some key
                        // code value
                        e.keyCode = 13;
                        $("#patient-search-input").val(
                            '666000004')
                            .trigger(e);
                        $('#patient-search-results li:nth-child(2) a')
                            .trigger('tap');
                        return 1000;
                    }, function () {
                        equal($("#legend div:nth-child(3)").hasClass('legend patient-appt'), true, 'Patient has appt legend displayed');
                        //Day view is displayed
                        equal($("#calendar").find('button').eq(5).hasClass('fc-state-active'), true, 'Day view');

                        //select an appointment slot
                        $("#calendar .fc-view-agendaDay .fc-event:not(.patient-appt):not(.transparent)").eq(0).trigger('click');
                        return 500;
                    }, function () {
                        //Book Appointment dialog is displayed
                        ok($("#book-appointment h3").text(), 'Book Appointment', 'Book appointment dialog');

                        //Validate the contents of the Book Appointment Dialog
                        //Patient Name, DoB, age, Gender and SSN
                        ok($("#book-appt-content").find('li').eq(0).text(), 'FOUR, PATIENT');
                        ok($("#book-appt-content").find('li').eq(1).text(), '01/01/1900 (113) - MALE');
                        ok($("#book-appt-content").find('li').eq(2).text(), 'SSN 666-00-0004');
                        //Appointment info - Clinic, Date and Appt time
                        ok($("#book-appt-content").find('ul').eq(1).find('li').eq(0), 'CARDIOLOGY');
                        ok($("#book-appt-content").find('ul').eq(1).find('li').eq(0), '02/21/2014');
                        ok($("#book-appt-content").find('ul').eq(1).find('li').eq(0), '12:30 PM - 13:00 PM - 30 mins');

                        //Book Appointment button is disabled until you select Purpose and appt type
                        ok($("#submit-btn").attr('data-disabled'), 'true', 'Book Appointment button disabled');

                        //Click the Purpose drop down and validate the list
                        $("#appt-purpose-button").trigger('click');
                        return 500;
                    }, function () {
                        ok($("#appt-purpose-menu").find('li').eq(1).find('a'), 'COMPENSATION & PENSION');
                        ok($("#appt-purpose-menu").find('li').eq(1).find('a'), 'CLASS II DENTAL');
                        ok($("#appt-purpose-menu").find('li').eq(1).find('a'), 'ORGAN DONORS');
                        ok($("#appt-purpose-menu").find('li').eq(1).find('a'), 'EMPLOYEE');
                        ok($("#appt-purpose-menu").find('li').eq(1).find('a'), 'PRIMA FACIA');
                        ok($("#appt-purpose-menu").find('li').eq(1).find('a'), 'RESEARCH');
                        ok($("#appt-purpose-menu").find('li').eq(1).find('a'), 'COLLATERAL OF VET.');
                        ok($("#appt-purpose-menu").find('li').eq(1).find('a'), 'SHARING AGREEMENT');
                        ok($("#appt-purpose-menu").find('li').eq(1).find('a'), 'REGULAR');
                        ok($("#appt-purpose-menu").find('li').eq(1).find('a'), 'COMPUTER GENERATED');

                        //Select a purpose
                        $("#appt-purpose-menu").find('li').eq(2).trigger('click');
                        return 500;
                    }, function () {
                        //Validate that the selected purpose is displayed in the purpose field
                        ok($("#appt-purpose-button").text(), 'CLASS II DENTAL', 'Selected purpose displayed');

                        //Click the Appointment Type drop down and validate the list
                        $("#appt-type-button").trigger('click');
                        return 500;
                    }, function () {
                        ok($("#appt-type-menu").find('li').eq(1).text(), 'Next Available Appointment');
                        ok($("#appt-type-menu").find('li').eq(1).text(), 'Other (Clinician Req.)');
                        ok($("#appt-type-menu").find('li').eq(1).text(), 'Other (Patient Req.)');
                        ok($("#appt-type-menu").find('li').eq(1).text(), 'Walk-in');
                        ok($("#appt-type-menu").find('li').eq(1).text(), 'Multiple Appointment Booking');
                        ok($("#appt-type-menu").find('li').eq(1).text(), 'Auto Rebook');
                        ok($("#appt-type-menu").find('li').eq(1).text(), 'Other');

                        //Select an appointment type
                        $("#appt-type-menu").find('li').eq(1).trigger('click');
                        return 500;
                    }, function () {
                        //Validate that the selected appointment type is displayed
                        ok($("#appt-type-button").text(), 'Next Available Appointment', 'Selected appt type is displayed');

                        //Book Appointment button is enabled
                        ok($("#submit-btn").attr('data-disabled'), 'false', 'Book Appointment button enabled');

                        //Validate Notes text box
                        //Character count
                        equal($("#book-appt-content p").eq(0).attr('data-balance'), 160, '160 is the count allowed');
                        equal($("#booking-notes").hasClass('ui-input-text'), true, 'Booking Input text');
                        //Booking notes allows only 160 charcters
                        equal($("#booking-notes").attr('maxlength'), '160', 'Allows 160 characters');
                        //^ character not allowed
                        $("#booking-notes").val('Test ^').trigger('keyup');
                        //validate the character counter
                        equal($("#book-appt-content p").eq(0).attr('data-balance'), 154, '154 characters entered by the user');
                        return 400;
                    }, function () {
                        ok($("#book-appt-content p").text(), 'Error: ^ character is not allowed.', 'Error message text');

                        //Book Appt  button is disabled
                        ok($("#submit-btn").attr('data-disabled'), 'true', 'Book Appointment button disabled');
                        //Click Cancel Appt button to return back to the calendar (day view)
                        $("#cancel-btn").trigger('click');
                        return 500;
                    }, function () {
                        equal($("#calendar").find('button').eq(5).hasClass('fc-state-active'), true, 'Day view');


                    });

                });
            });
    });
