$(document).ready(function () {
    var serviceOptions = {
        'Essay writing services': ['Essay (any type)', 'Annotated Bibliography', 'Article Critique', 'Assignment', 'Book Review', 'Book Report', 'Business Plan', 'Case Study', 'Coursework', 'Critical Essay', 'Debate', 'Discussion Board Post', 'Excel Spreadsheet', 'Lab Report', 'Memo', 'Movie Review', 'Multiple Choice Questions', 'Outline', 'Poster', 'PowerPoint Presentation', 'Project', 'Reaction Paper', 'Reflection Paper', 'Report', 'Speech', 'Editing', 'Formatting', 'Proofreading', 'Other'],
        
        'Research paper writing': ['Research Paper', 'Research Proposal', 'Term Paper', 'Abstract', 'Annotated Bibliography', 'Capstone Project', 'Journal Article', 'Outline', 'Introduction Chapter', 'Literature Review Chapter', 'Methodology Chapter', 'Results Chapter', 'Discussion Chapter', 'Conclusion Chapter', 'Research Summary', 'PowerPoint Presentation', 'Editing', 'Formatting', 'Proofreading', 'Other'],
        
        'Dissertation/Thesis writing': ['Dissertation', 'Dissertation/Thesis Proposal', 'Outline', 'Abstract', 'Introduction Chapter', 'Literature Review Chapter', 'Methodology Chapter', 'Results Chapter', 'Discussion Chapter', 'Conclusion Chapter', 'PowerPoint Presentation', 'Editing', 'Formatting', 'Proofreading', 'Other'],
        
        'Admission services': ['Admission Essay', 'Personal Statement', 'Scholarship Essay', 'Cover Letter', 'Resume Writing', 'PowerPoint Presentation', 'Editing', 'Formatting', 'Proofreading'],
        
        'Copywriting services': ['Website Content', 'Website Review', 'Product Review', 'Press Release', 'News Article', 'Blogpost', 'SEO Article', 'PowerPoint Presentation', 'Editing', 'Formatting', 'Proofreading'],
            
        'Resume/CV services': ['Entry Level', 'Professional Level', 'Career Change', 'Executive', 'Military', 'Federal', 'PowerPoint Presentation', 'Editing', 'Formatting', 'Proofreading'],   
    };

    function updateTypeOfPaperOptions() {
        var selectedService = $('#typeOfService').val();
        var paperOptions = serviceOptions[selectedService] || [];
    
        $('#calc1').empty(); // Clear the current options
    
        $.each(paperOptions, function (index, option) {
            $('#calc1').append('<option value="' + option + '">' + option + '</option>');
        });
    
        // Set default options based on the selected service
        if (selectedService === 'Research paper writing') {
            $('#calc1').val('Research Paper'); // Set Research Paper as default
        } else if (selectedService === 'Essay writing services') {
            $('#calc1').val('Essay (any type)'); // Set Essay (any type) as default
        } else if (selectedService === 'Dissertation/Thesis writing') {
            $('#calc1').val('Dissertation'); // Set Dissertation as default
        } else if (selectedService === 'Admission services') {
            $('#calc1').val('Admission Essay'); // Set Admission Essay as default
        } else if (selectedService === 'Copywriting services') {
            $('#calc1').val('Website Content'); // Set Website Content as default
        } else if (selectedService === 'Resume/CV services') {
            $('#calc1').val('Entry Level'); // Set Entry Level as default
        }
    }
    
        
    

    function updateTypeOfServiceOptions() {
        var academicLevel = $('#calc2').val(); // Get selected academic level
        var typeOfService = $('#typeOfService');

        // Hide "Dissertation/Thesis writing" for HighSchool and Freshman
        if (academicLevel === 'High School' || academicLevel === 'Freshman' || academicLevel === 'Sophomore' || academicLevel === 'Junior') {
            typeOfService.find('option[value="Dissertation/Thesis writing"]').hide();

            // If "Dissertation/Thesis writing" was selected, reset to 'Essay writing services'
            if (typeOfService.val() === 'Dissertation/Thesis writing') {
                typeOfService.val('Essay writing services').trigger('change');
            }
        } else {
            // Show "Dissertation/Thesis writing" for higher academic levels
            typeOfService.find('option[value="Dissertation/Thesis writing"]').show();
        }
    }

    // Event delegation for academic level change
    $(document).on('change', '#calc2', function () {
        updateTypeOfServiceOptions(); // Update service options based on academic level
        $('#typeOfService').val('Essay writing services').trigger('change'); // Reset type of service to 'Essay writing services'

        // Force update type of paper options
        setTimeout(updateTypeOfPaperOptions, 0); 
    });

    // Event delegation for type of service change
    $(document).on('change', '#typeOfService', function () {
        updateTypeOfPaperOptions(); // Update paper options based on the selected service
    });

    // Initialize on document ready
    updateTypeOfServiceOptions(); // Ensure the correct options are displayed based on the initial academic level
    updateTypeOfPaperOptions();   // Initialize with default type of paper options
});
