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

        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("SCV-67 |SCV-17, SCV-20, SCV-470- Future Appointments listing,Default sort order, Future Appointment details");
        test('SCV-437 - a.Validate future appointments and default sort order', function () {
	        ok(true, "The 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 () {
                    $(window).load(function () {
                        ok($("#inner-content").find('p').text(),
                            'Please select a Service/Specialty and Clinic to view availability.');
                        // Search and select a patient
                        var e = $.Event("keypress");
                        e.which = 13; // # Some key
                        // code value
                        e.keyCode = 13;
                        $("#patient-search-input").val(
                            'ten, patient')
                            .trigger(e);
                        $('#patient-search-results li:nth-child(4) a')
                            .trigger('tap');
                        afterElementLoad($("#patient-button:not(.ui-disabled)"),
                            function () {
                                $('#patient-button').trigger('tap');
                                ok($("#future-appointments-table").is(':visible'),
                                    'Future Appointments table is visible');
                            });
                        //SCV-20:Validate default sort order
                        ok($("#future-appointments-table").find('th').eq(0).text(), 'Date', 'Date column is the default sort');
                        equal($("#future-appointments-table").find('th').eq(0).attr('aria-sort'),
                            'ascending', 'Default sort is ascending');
                        //validate the column headers
                        ok($("#future-appointments-table").find('th').eq(0).text(), 'Date', 'Date column-1st column');
                        ok($("#future-appointments-table").find('th').eq(1).text(), 'Provider Name', 'Provider Name-2nd column');
                        ok($("#future-appointments-table").find('th').eq(2).text(), 'Clinic / Specialty', 'Clinic / Specialty-3rd column');

                        //Records exist under Future Appointments table
                        equal($("#future-appointments .ui-btn-text").find('span').length > 0, true, 'Future Appointments exist');

                        //Validate that each column is sortable
                        //Date column - default is ascending, click to sort descending
                        $("#future-appointments-table").find('th').eq(0).trigger('click');
                        equal($("#future-appointments-table").find('th').eq(0).attr('aria-sort'),
                            'descending', 'Date column descending');
                        //Provider Name column - ascending
                        $("#future-appointments-table").find('th').eq(1).trigger('click');
                        equal($("#future-appointments-table").find('th').eq(01).attr('aria-sort'),
                            'ascending', 'Provide Name ascending');
                        //Provider Name column - descending
                        $("#future-appointments-table").find('th').eq(1).trigger('click');
                        equal($("#future-appointments-table").find('th').eq(1).attr('aria-sort'),
                            'descending', 'Provide Name descending');
                        //Service/Specialty column - ascending
                        $("#future-appointments-table").find('th').eq(2).trigger('click');
                        equal($("#future-appointments-table").find('th').eq(2).attr('aria-sort'),
                            'ascending', 'Service/Specialty ascending');
                        //Service/Specialty column - descending
                        $("#future-appointments-table").find('th').eq(2).trigger('click');
                        equal($("#future-appointments-table").find('th').eq(2).attr('aria-sort'),
                            'descending', 'Service/Specialty descending');

                        //Select future appointment meta data to display details
                        $("#future-appointments-table").find('tr').eq(1).trigger('tap');
                        return 500;

                    });
                },
                function () {
                    $('window').load(function () {

                        equal($("#inner-content h3").is(':visible'), true, 'Future appointment details displayed');
                        //Cancel Appointment button is disabled
                        equal($("#process-request").hasClass('ui-disabled'), false, 'Cancel Appointment button enabled');
                        //Details
                        equal($("#inner-content").find('li').eq(0).find('span').eq(0).text(), 'Clinic', 'Clinic displayed');
                        equal($("#inner-content").find('li').eq(1).find('span').eq(0).text(), 'Ask For Check In', 'Ask For Check In displayed');
                        equal($("#inner-content").find('ul').eq(1).find('li').eq(0).find('span').eq(0).text(), 'Current Status', 'Current Status displayed');
                        equal($("#inner-content").find('ul').eq(1).find('li').eq(1).find('span').eq(0).text(), 'Purpose', 'Purpose displayed');
                        equal($("#inner-content").find('ul').eq(1).find('li').eq(2).find('span').eq(0).text(), 'Date', 'Date displayed');
                        equal($("#inner-content").find('ul').eq(1).find('li').eq(3).find('span').eq(0).text(), 'Time', 'Time displayed');
                        equal($("#inner-content").find('ul').eq(1).find('li').eq(4).find('span').eq(0).text(), 'Length', 'Length displayed');
                        equal($("#inner-content").find('ul').eq(1).find('li').eq(5).find('span').eq(0).text(), 'Booking Notes', 'Booking Notes displayed');

                    });
                });
	        });
        });
    });