let conversation = [ { role: 'bot', message: "Hi there! I'm TioNat Assistant! May I know your name?" } ]; let userName = ''; // To store the user's name let isSubmitting = false; // Flag to prevent multiple submissions function addMessage(role, message) { const chatMessages = document.getElementById('chatMessages'); if (!chatMessages) { console.error('Chat messages container not found!'); return; } const messageDiv = document.createElement('div'); messageDiv.className = role === 'bot' ? 'bot-message' : 'user-message'; messageDiv.textContent = message; chatMessages.appendChild(messageDiv); chatMessages.scrollTop = chatMessages.scrollHeight; console.log(`Added ${role} message: ${message}`); } function sendMessage() { const userInput = document.getElementById('userInput'); if (!userInput) { console.error('User input field not found!'); return; } const message = userInput.value.trim(); if (message) { addMessage('user', message); conversation.push({ role: 'user', message: message }); userInput.value = ''; setTimeout(() => handleResponse(message), 500); } else { console.warn('Empty message!'); } } // Add Enter key event listener for the input field document.addEventListener('DOMContentLoaded', () => { const userInput = document.getElementById('userInput'); const clearButton = document.getElementById('clearButton'); if (userInput) { userInput.addEventListener('keydown', (event) => { if (event.key === 'Enter' && !event.shiftKey) { event.preventDefault(); sendMessage(); } }); } if (clearButton) { clearButton.addEventListener('click', clearChat); } }); function clearChat() { const chatMessages = document.getElementById('chatMessages'); chatMessages.innerHTML = '
Hi there! I\'m TioNat Assistant! May I know your name?
'; conversation = [{ role: 'bot', message: "Hi there! I'm TioNat Assistant! May I know your name?" }]; userName = ''; document.getElementById('userInput').value = ''; isSubmitting = false; // Reset submission flag on chat clear } function displayForm() { const chatMessages = document.getElementById('chatMessages'); if (!chatMessages) { console.error('Chat messages container not found for form!'); return; } // Prevent duplicate forms if (chatMessages.querySelector('.registration-form')) { console.log('Form already exists, skipping display.'); return; } const formDiv = document.createElement('div'); formDiv.className = 'bot-message'; formDiv.innerHTML = `

Please register here with these details. Our service agent will get back to you soon.




`; chatMessages.appendChild(formDiv); chatMessages.scrollTop = chatMessages.scrollHeight; } function resetForm() { const mobileNumber = document.getElementById('mobileNumber'); const email = document.getElementById('email'); const issueDescription = document.getElementById('issueDescription'); const submitButton = document.getElementById('submitButton'); if (mobileNumber) mobileNumber.value = ''; if (email) email.value = ''; if (issueDescription) issueDescription.value = ''; if (submitButton) submitButton.disabled = false; isSubmitting = false; // Reset the flag after form reset console.log('Form reset after 4 seconds'); } function submitForm() { if (isSubmitting) { console.log('Submission already in progress, ignoring additional click'); return; } const submitButton = document.getElementById('submitButton'); if (!submitButton) { console.error('Submit button not found!'); return; } // Disable the button and set the flag isSubmitting = true; submitButton.disabled = true; console.log('Submit button clicked, starting submission process'); const mobileNumber = document.getElementById('mobileNumber').value.trim(); const email = document.getElementById('email').value.trim(); const issueDescription = document.getElementById('issueDescription').value.trim(); // Validate mobile number (must be 10 digits) const mobileRegex = /^\d{10}$/; if (!mobileNumber || !mobileRegex.test(mobileNumber)) { addMessage('bot', 'Please enter a valid 10-digit mobile number.'); isSubmitting = false; submitButton.disabled = false; console.log('Validation failed: Invalid mobile number'); return; } if (!email || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) { addMessage('bot', 'Please enter a valid email address.'); isSubmitting = false; submitButton.disabled = false; console.log('Validation failed: Invalid email'); return; } if (!issueDescription) { addMessage('bot', 'Please describe your issue.'); isSubmitting = false; submitButton.disabled = false; console.log('Validation failed: Issue description missing'); return; } const formData = { name: userName, mobileNumber: mobileNumber, email: email, issueDescription: issueDescription }; console.log('Sending form data to server:', formData); fetch('/submit-case', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(formData), }) .then(response => { console.log('Received response from server:', response); return response.json(); }) .then(data => { console.log('Parsed response data:', data); if (data.success) { addMessage('bot', 'Thank you! Your case has been submitted. Our service agent will get back to you soon.'); // Reset the form after 4 seconds setTimeout(() => { resetForm(); }, 4000); } else { addMessage('bot', 'There was an error submitting your case. Please try again later.'); isSubmitting = false; submitButton.disabled = false; } }) .catch(error => { console.error('Error submitting form:', error); addMessage('bot', 'There was an error submitting your case. Please try again later.'); isSubmitting = false; submitButton.disabled = false; }); } function displayOptions(options) { const chatMessages = document.getElementById('chatMessages'); if (!chatMessages) { console.error('Chat messages container not found for options!'); return; } options.forEach(opt => { const messageDiv = document.createElement('div'); messageDiv.className = 'bot-message'; const button = document.createElement('button'); button.textContent = opt.text; button.className = `option-button ${opt.class}`; button.onclick = () => { addMessage('user', opt.text); conversation.push({ role: 'user', message: opt.text }); setTimeout(() => handleResponse(opt.text), 500); }; messageDiv.appendChild(button); chatMessages.appendChild(messageDiv); }); chatMessages.scrollTop = chatMessages.scrollHeight; } function displayCategories() { const categories = [ { text: 'About TioNat and Account Setup', class: 'blue', questions: [ { text: 'What is TioNat?', class: 'blue' }, { text: 'How do I create an account on TioNat?', class: 'blue' }, { text: 'How can I track my order?', class: 'blue' }, { text: 'Do you ship internationally?', class: 'blue' }, { text: 'What payment methods do you accept?', class: 'blue' } ]}, { text: 'Nutritional Supplements', class: 'blue', questions: [ { text: 'What is the best supplement for boosting immunity?', class: 'blue' }, { text: 'Are your nutritional supplements vegan-friendly?', class: 'blue' }, { text: 'How do I know which supplement is right for me?', class: 'blue' }, { text: 'Are there any side effects of using your supplements?', class: 'blue' }, { text: 'Do you offer products for specific health conditions?', class: 'blue' } ]}, { text: 'Health Care Products', class: 'blue', questions: [ { text: 'How do I use your health care products?', class: 'blue' }, { text: 'Do your products come with a money-back guarantee?', class: 'blue' }, { text: 'Are your health care products FDA-approved?', class: 'blue' }, { text: 'Are your personal care products cruelty-free?', class: 'blue' }, { text: 'What ingredients do you use in your skincare products?', class: 'blue' } ]}, { text: 'Personal Care and Skincare', class: 'blue', questions: [ { text: 'Can I use your personal care products for sensitive skin?', class: 'blue' }, { text: 'How do I find the right skincare routine?', class: 'blue' }, { text: 'Can I modify or cancel my order after placing it?', class: 'blue' }, { text: 'How long does it take to receive my order?', class: 'blue' }, { text: 'Do you offer free shipping?', class: 'blue' } ]}, { text: 'Returns and Customer Support', class: 'blue', questions: [ { text: 'How do I return a product?', class: 'blue' }, { text: 'How can I contact customer service?', class: 'blue' }, { text: 'Can I track my return?', class: 'blue' }, { text: 'How can I get a product recommendation?', class: 'blue' }, { text: 'How do I reset my password?', class: 'blue' } // Moved here ]}, { text: 'Security and Account Management', class: 'blue', questions: [ { text: 'Is my payment information secure?', class: 'blue' }, { text: 'How do I update my account information?', class: 'blue' }, { text: 'Can I leave a review for a product?', class: 'blue' }, { text: 'How do I report a product issue?', class: 'blue' } ]}, { text: 'Other', class: 'green' } ]; const chatMessages = document.getElementById('chatMessages'); if (!chatMessages) { console.error('Chat messages container not found for categories!'); return; } categories.forEach(category => { const messageDiv = document.createElement('div'); messageDiv.className = 'bot-message'; const button = document.createElement('button'); button.textContent = category.text; button.className = `option-button ${category.class}`; button.onclick = () => { addMessage('user', category.text); conversation.push({ role: 'user', message: category.text }); if (category.text === 'Other') { addMessage('bot', "Please provide more details about your query so our service agent can assist you."); displayForm(); } else { displayOptions(category.questions); } }; messageDiv.appendChild(button); chatMessages.appendChild(messageDiv); }); chatMessages.scrollTop = chatMessages.scrollHeight; } function handleResponse(userInput) { const lastMessage = conversation[conversation.length - 1].message.toLowerCase(); let botResponse = ''; // Handle name input (first user response after bot's greeting) if (conversation.length === 2 && conversation[0].role === 'bot' && conversation[0].message.includes('May I know your name?')) { userName = userInput; botResponse = `Nice to meet you, ${userInput}! 😊 Please select a category to explore common questions or choose 'Other' to submit a custom query.`; addMessage('bot', botResponse); displayCategories(); return; } // Handle category selection and FAQ responses if (lastMessage.includes('about tionat and account setup')) { return; // Questions are already displayed via displayOptions } else if (lastMessage.includes('nutritional supplements')) { return; } else if (lastMessage.includes('health care products')) { return; } else if (lastMessage.includes('personal care and skincare')) { return; } else if (lastMessage.includes('returns and customer support')) { return; } else if (lastMessage.includes('security and account management')) { return; } else if (lastMessage.includes('other')) { addMessage('bot', "Please provide more details about your query so our service agent can assist you."); displayForm(); return; } // Handle FAQ responses if (lastMessage.includes('what is tionat?')) { botResponse = 'TioNat is an e-commerce platform offering a wide range of products in Nutritional Care, Health Care, and Personal Care categories. We aim to improve the quality of life with top-notch, reliable, and scientifically-backed products.'; } else if (lastMessage.includes('how do i create an account on tionat?')) { botResponse = 'To create an account on TioNat, simply click on the "Sign Up" button on the top-right corner of our website. Enter your name, email address, and a password. Once registered, you can start shopping!'; } else if (lastMessage.includes('how can i track my order?')) { botResponse = 'After placing your order, you will receive a tracking number via email. You can also track your order from your TioNat account under the "Order History" section.'; } else if (lastMessage.includes('do you ship internationally?')) { botResponse = 'Yes, we offer international shipping to select countries. You can check if your country is eligible for delivery during the checkout process.'; } else if (lastMessage.includes('what payment methods do you accept?')) { botResponse = 'We accept a wide variety of payment methods, including credit/debit cards (Visa, MasterCard, American Express), net banking, PayPal, and cash on delivery (where applicable).'; } else if (lastMessage.includes('what is the best supplement for boosting immunity?')) { botResponse = 'Our Immunity Booster supplements, like Vitamin C, Zinc, and Echinacea, are popular choices for improving immune system health. We recommend consulting with a healthcare provider to find the best fit for you.'; } else if (lastMessage.includes('are your nutritional supplements vegan-friendly?')) { botResponse = 'Yes, many of our nutritional supplements are vegan-friendly. Please check the product details for specific information on ingredients and certifications.'; } else if (lastMessage.includes('how do i know which supplement is right for me?')) { botResponse = 'We recommend consulting with a healthcare professional before choosing a supplement. You can also check our detailed product descriptions and consult with our customer service for recommendations.'; } else if (lastMessage.includes('are there any side effects of using your supplements?')) { botResponse = 'All our products are thoroughly tested for safety, but individual reactions may vary. We suggest reading the product description for possible side effects or consulting with a healthcare provider before using any product.'; } else if (lastMessage.includes('do you offer products for specific health conditions?')) { botResponse = 'Yes, we offer a variety of health care products for conditions such as joint pain, heart health, digestion issues, and more. Please browse through our Health Care category for detailed information.'; } else if (lastMessage.includes('how do i use your health care products?')) { botResponse = 'Each product comes with detailed instructions for use on the packaging. Additionally, you can find user guides and recommendations on our website under each product’s description.'; } else if (lastMessage.includes('do your products come with a money-back guarantee?')) { botResponse = 'Yes, we offer a 30-day money-back guarantee on most of our health care products. Please refer to our return policy for specific conditions and guidelines.'; } else if (lastMessage.includes('are your health care products fda-approved?')) { botResponse = 'Many of our health care products are manufactured following strict quality guidelines and may be FDA-approved where applicable. You can check the product details for certification information.'; } else if (lastMessage.includes('are your personal care products cruelty-free?')) { botResponse = 'Yes, all our personal care products are cruelty-free. We ensure that none of our products are tested on animals, and they are made with sustainable and ethical practices.'; } else if (lastMessage.includes('what ingredients do you use in your skincare products?')) { botResponse = 'Our skincare products are formulated with high-quality ingredients, such as natural oils, vitamins, and plant-based extracts. For specific ingredient lists, please refer to the product description on each item page.'; } else if (lastMessage.includes('can i use your personal care products for sensitive skin?')) { botResponse = 'We offer products specifically designed for sensitive skin. Check the product descriptions for details on ingredients and suitability for sensitive skin. We also recommend doing a patch test before full application.'; } else if (lastMessage.includes('how do i find the right skincare routine?')) { botResponse = 'We suggest starting with a basic routine: cleansing, toning, and moisturizing. For more tailored recommendations, please check out our personalized skincare guides, or reach out to our customer service for advice.'; } else if (lastMessage.includes('can i modify or cancel my order after placing it?')) { botResponse = 'Orders can be modified or canceled within 24 hours of placement. Please contact our customer support immediately for any changes.'; } else if (lastMessage.includes('how long does it take to receive my order?')) { botResponse = 'Orders typically take 3-7 business days to process and ship, depending on your location. You can check your tracking number for more accurate delivery details.'; } else if (lastMessage.includes('do you offer free shipping?')) { botResponse = 'Yes, we offer free shipping on orders above a certain amount. Check the shipping details at checkout to confirm eligibility.'; } else if (lastMessage.includes('how do i return a product?')) { botResponse = 'If you are not satisfied with your purchase, you can return most products within 30 days. Please visit our Returns & Exchange page for detailed instructions.'; } else if (lastMessage.includes('how can i contact customer service?')) { botResponse = 'You can reach our customer service team via email at support@tionat.com or call us at +1-800-123-4567. We\'re here to assist you with any queries.'; } else if (lastMessage.includes('can i track my return?')) { botResponse = 'Yes, once your return is processed, you will receive a tracking number to monitor the status of your return shipment.'; } else if (lastMessage.includes('how can i get a product recommendation?')) { botResponse = 'You can check out our product recommendations under each category. If you\'re unsure, feel free to ask our chatbot or reach out to our customer support for personalized advice.'; } else if (lastMessage.includes('how do i reset my password?')) { botResponse = 'If you\'ve forgotten your password, click on the "Forgot Password" link on the login page. You will receive an email with instructions on how to reset your password.'; } else if (lastMessage.includes('is my payment information secure?')) { botResponse = 'Yes, we use industry-standard encryption (SSL) to protect your payment information. All payments are processed securely through trusted payment gateways.'; } else if (lastMessage.includes('how do i update my account information?')) { botResponse = 'You can update your account information by logging into your account and navigating to the "Account Settings" page. From there, you can update your address, payment methods, and personal details.'; } else if (lastMessage.includes('can i leave a review for a product?')) { botResponse = 'Yes! After purchasing a product, you can leave a review directly on the product page. We value your feedback and it helps other customers make informed decisions.'; } else if (lastMessage.includes('how do i report a product issue?')) { botResponse = 'If you receive a defective or damaged product, please contact our customer service immediately for assistance with returns or exchanges.'; } else { botResponse = "I'm sorry, I don't understand your query. Please select a category to explore common questions or choose 'Other' to submit a custom query."; addMessage('bot', botResponse); if (!document.querySelector('.option-button')) { displayCategories(); } return; } addMessage('bot', botResponse); }