
    function tsSubmitGuestTicket() {

        var websiteInput = document.getElementById('ts-website-field');
        var websiteValue = websiteInput ? websiteInput.value.trim() : '';

        var name = document.getElementById('ts_guest_name') ? document.getElementById('ts_guest_name').value.trim() : '';
        var email = document.getElementById('ts_guest_email').value.trim();
        var phone = document.getElementById('ts_guest_phone').value.trim();
        var issue = document.getElementById('ts_guest_issue').value.trim();
        var attachmentInput = document.getElementById('ts_guest_attachment');
        var attachment = attachmentInput.files[0];
        var btn = document.getElementById('ts-guest-submit-btn');

        if(!email || !issue) { alert(tsWidgetData.i18n.fillEmailIssue); return; }

        if(attachment && attachment.size > 5 * 1024 * 1024) {
            alert(tsWidgetData.i18n.fileTooLarge);
            return;
        }

        btn.innerHTML = '...';
        btn.disabled = true;

        var formData = new FormData();
        formData.append('Website', websiteValue);
        formData.append('Name', name);
        formData.append('Email', email);
        formData.append('PhoneNumber', phone);
        formData.append('Issue', issue);
        formData.append('Language', tsWidgetData.apiLanguage);
        formData.append('Channel', 'Widget');
        
        if (attachment) {
            formData.append('attachment', attachment);
        }

        fetch(tsWidgetData.apiBase + '/tickets/CreateGuestTicket', {
            method: 'POST',
            headers: { 
                'X-Api-Key': tsWidgetData.apiKey,
                'X-Client-Domain': tsWidgetData.clientDomain,
                'X-Client-Language': tsWidgetData.apiLanguage
            },
            body: formData
        })
        .then(res => {
            return res.text().then(text => {
                let data = null;
                try { data = JSON.parse(text); } catch(e) {}
                return { ok: res.ok, data: data, status: res.status };
            });
        })
        .then(res => { 
            if(res.ok) { 
                document.getElementById('ts-widget-form-container').style.display='none'; 
                document.getElementById('ts-widget-success').style.display='block'; 
                if(res.data && res.data.ticketId) {
                    document.getElementById('ts-guest-created-id').innerText = '#' + res.data.ticketId;
                }
            } else { 
                var errMsg = 'Error ' + res.status;
                if (res.data) {
                    if (res.data.message) {
                        errMsg = res.data.message;
                    } else if (res.data.errors) {
                        errMsg = Object.values(res.data.errors).flat().join('\n');
                    }
                }
                alert(errMsg); 
                btn.disabled=false; 
                btn.innerHTML = tsWidgetData.i18n.sendMessage; 
            } 
        })
        .catch(err => { 
            console.error(err); 
            alert('Network Error. Please try again.'); 
            btn.disabled=false; 
            btn.innerHTML = tsWidgetData.i18n.sendMessage; 
        });
    }

