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("SCV80,SCV1436 |SCV842, SCV1447- Clicking Cancel Appointment display cancel appointment dialog, Display message if cancel reason is other than Cancelled by Clinic or Patient");
        test('SCV927,SCV1448 - Validate cancel appointment dialog, Validate the message for different cancel reason', function () {
            ok(true, "The tests are running");

            $(window).load(function () {
                asyncSeries(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 () {
                    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('Card')
                        .keyup();
                    $("#calendar-specialty li:nth-child(2) a")
                        .trigger('tap');

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

                    // Select Clinic
                    $("#calendar-clinic-menu label").eq(0)
                        .trigger('click');

                }, function () {
                    // Click View Availability to display the
                    // calendar
                    $("#appointment-button a").trigger('tap');
                    afterElementLoad($("#calendar"), function () {
                        ok($("#calendar")
                                .is(':visible'),
                            'Calendar is visible');
                    });


                }, function () {
                    //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 () {
                    //Go to patient tab
                    $('#patient-button').trigger('tap');
                    return 500;
                }, function () {
                    afterElementLoad($("#future-apointments-table"), function () {
                        ok($("#future-appointments-table").is(':visible'),
                            'Future Appointments table is visible');
                    });
                    //Select future appt list item
                    $("#future-appointments-table").find('tr').eq(1).trigger('tap');
                    return 500;
                }, function () {
                    equal($("#inner-content h3").is(':visible'), true, 'Future appointment details displayed');

                    //Click on Cancel Appointment button
                    $("#process-request").trigger('click');
                    return 500;
                }, function () {
                    afterElementLoad($("#book-appointment"), function () {
                        ok($("#book-appointment").is(':visible'),
                            'Cancel Appointment dialog is visible');
                    });

                    ok($("#book-appointment h3").eq(0).text(), 'Cancel appointment dialog');

                    //Validate the contents of the Book Appointment Dialog
                    //Patient Name, DoB, age, Gender and SSN
                    ok($("#book-appt-content h3").eq(0).text(), 'Patient Details section title');
                    ok($("#book-appt-content").find('li').eq(0).text(), 'FOUR, PATIENT Patient name');
                    ok($("#book-appt-content").find('li').eq(1).text(), '01/01/1900 (113) - MALE Patient DoB, age and Gender');
                    ok($("#book-appt-content").find('li').eq(2).text(), 'SSN 666-00-0004-Patient SSN');
                    //Appointment info - Clinic, Date and Appt time
                    ok($("#book-appt-content h3").eq(1).text(), 'Appointment Details');
                    ok($("#book-appt-content").find('ul').eq(1).find('li').eq(0), 'DIABETIC-Clinic name');
                    ok($("#book-appt-content").find('ul').eq(1).find('li').eq(0), '12/31/2013-Appt Date');
                    ok($("#book-appt-content").find('ul').eq(1).find('li').eq(0), '8:00 AM - 8:30 AM - 30 mins, Appt time and duration');

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

                    //Click the Purpose drop down and validate the list
                    $("#appt-purpose-button").trigger('click');
                    return 500;
                }, function () {
                    //number of Statuses including Select Status
                    equal($("#appt-purpose-menu").find('li').length, 6, '6 records including Select Status');
                    equal($("#appt-purpose-menu").find('li').eq(1).find('a').text(), 'No-Show', 'No-Show status');
                    equal($("#appt-purpose-menu").find('li').eq(2).find('a').text(), 'Cancelled by Clinic', 'Cancelled by Clinic status');
                    equal($("#appt-purpose-menu").find('li').eq(3).find('a').text(), 'Inpatient Appointment', 'Inpatient Appointment status');
                    equal($("#appt-purpose-menu").find('li').eq(4).find('a').text(), 'Cancelled by Patient', 'Cancelled by Patient status');
                    equal($("#appt-purpose-menu").find('li').eq(5).find('a').text(), 'No Action Taken', 'No Action Taken status');

                    //SCV-1436 - Validate the message when selecting status other than Cancelled by Clinic or Patient

                    //Select a purpose
                    //Status = No Show
                    $("#appt-purpose-menu").find('li').eq(1).trigger('click');
                    //message displayed
                    equal($("#book-appt-content").find('div').hasClass('rebook-warning hidden'), false, 'Message displayed');

                    //Status = Cancelled by Clinic
                    $("#appt-purpose-button").trigger('click');
                    $("#appt-purpose-menu").find('li').eq(2).trigger('click');
                    //message displayed
                    equal($("#book-appt-content").find('div').hasClass('rebook-warning hidden'), true, 'Message not displayed');

                    //Status = Inpatient Appointment
                    $("#appt-purpose-button").trigger('click');
                    $("#appt-purpose-menu").find('li').eq(3).trigger('click');
                    //message displayed
                    equal($("#book-appt-content").find('div').hasClass('rebook-warning hidden'), false, 'Message displayed');

                    //Status = Cancelled by Patient
                    $("#appt-purpose-button").trigger('click');
                    $("#appt-purpose-menu").find('li').eq(4).trigger('click');
                    //message displayed
                    equal($("#book-appt-content").find('div').hasClass('rebook-warning hidden'), true, 'Message not displayed');

                    //Status = No Action Taken
                    $("#appt-purpose-button").trigger('click');
                    $("#appt-purpose-menu").find('li').eq(5).trigger('click');
                    //message displayed
                    equal($("#book-appt-content").find('div').hasClass('rebook-warning hidden'), false, 'Message displayed');

                    //Message text
                    ok($("#book-appt-content p").text(),
                        '*NOTE: If you cancel with this status, attempting to rebook the exact same appointment will cause data errors.Characters Remaining: Error: ^ character is not allowed.');

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

                    //Click the Reason drop down and validate the list
                    $("#appt-type-button").trigger('click');
                    return 500;
                }, function () {
                    //number of Reasons including Select Reason
                    equal($("#appt-type-menu").find('li').length, 14, '14 records including Select Reason');
                    equal($("#appt-type-menu").find('li').eq(1).text(), 'APPOINTMENT NO LONGER REQUIRED', 'Appointment No Longer Required');
                    equal($("#appt-type-menu").find('li').eq(2).text(), 'CLINIC CANCELLED', 'Clinic Cancelled');
                    equal($("#appt-type-menu").find('li').eq(3).text(), 'CLINIC STAFFING', 'Clinic Staffing');
                    equal($("#appt-type-menu").find('li').eq(4).text(), 'DEATH IN FAMILY', 'Death In Family');
                    equal($("#appt-type-menu").find('li').eq(5).text(), 'INPATIENT STATUS', 'Inpatient Status');
                    equal($("#appt-type-menu").find('li').eq(6).text(), 'OTHER', 'Other reason');
                    equal($("#appt-type-menu").find('li').eq(7).text(), 'PATIENT DEATH', 'Patient Death');
                    equal($("#appt-type-menu").find('li').eq(8).text(), 'PATIENT NOT ELIGIBLE', 'Patient Not Eligible');
                    equal($("#appt-type-menu").find('li').eq(9).text(), 'SCHEDULING CONFLICT/ERROR', 'Scheduling Conflict/Error');
                    equal($("#appt-type-menu").find('li').eq(10).text(), 'TRANSFER OPT CARE TO OTHER VA', 'Transfer Opt Care to Other VA');
                    equal($("#appt-type-menu").find('li').eq(11).text(), 'TRAVEL DIFFICULTY', 'Travel Difficulty');
                    equal($("#appt-type-menu").find('li').eq(12).text(), 'UNABLE TO KEEP APPOINTMENT', 'Unable to Keep Appointment');
                    equal($("#appt-type-menu").find('li').eq(13).text(), 'WEATHER', 'Weather');

                    //Select a Reason
                    $("#appt-type-menu").find('li').eq(1).trigger('click');
                    return 500;
                }, function () {
                    //Validate that the selected reason is displayed
                    ok($("#appt-type-button").text(), 'Weather is displayed');

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

                    //Validate Notes text box
                    //Character count
                    equal($("#book-appt-content p.counter").eq(0).attr('data-balance'), 200, '200 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'), '200', 'Allows 200 characters');
                    //^ character not allowed
                    $("#booking-notes").val('Test ^').trigger('keyup');
                    //validate the character counter
                    equal($("#book-appt-content p.counter").attr('data-balance'), 194, '194 is the count after entering text');
                    return 400;
                }, function () {
                    ok($("#book-appt-content p.warning").text(), 'Error: ^ character is not allowed. -Error message text');

                    //Cancel Appt  button is disabled
                    ok($("#submit-btn").attr('data-disabled'), 'Cancel Appointment button disabled');
                    //Click Do Not Cancel Appt button to return back to Future Appt details
                    $("#cancel-btn").trigger('click');
                }, function () {
                    $('#main').animationComplete(function () {
                        equal($("#inner-content h3").is(':visible'), true, 'Future appointment details displayed');
                    });
                });
            });

        });
    });
