<?php
/**
 * TAB: CONTACT — Contact form + support info
 *
 * - Formularz pre-filled danymi licencji klienta
 * - Podgląd wiadomości przed wysłaniem
 * - Po wysłaniu: pełna kopia tego co zostało wysłane
 * - Klient + my (office@) dostajemy kopię emailem
 *
 * @package ParcelSelector
 * @since   3.1.2
 */

if ( ! defined( 'ABSPATH' ) ) exit;

if ( ! function_exists( 'parcselect_tab_contact' ) ) {
function parcselect_tab_contact() {
    $t            = parcselect_admin_i18n();
    $current_user = wp_get_current_user();

    // Installation context attached to the support message.
    $install_id   = get_option( 'parcselect_installation_id', '' );
    $domain       = wp_parse_url( home_url(), PHP_URL_HOST );
    $site_name    = get_bloginfo( 'name' );
    ?>

    <!-- ═══ SZYBKI KONTAKT ═══ -->
    <div class="parcelselector-section">
        <h2>&#x1F4E7; <?php echo esc_html( $t['con_title'] ); ?></h2>
        <p class="description"><?php echo esc_html( $t['con_desc'] ); ?></p>
    </div>

    <div style="display:grid; grid-template-columns:repeat(auto-fill,minmax(280px,1fr)); gap:20px; margin-bottom:30px;">
        <div class="parcelselector-card" style="text-align:center; padding:30px;">
            <p style="font-size:40px; margin:0;">&#x1F4E7;</p>
            <h3 style="margin:10px 0 5px;"><?php echo esc_html( $t['con_email_title'] ); ?></h3>
            <p style="color:#666; font-size:13px; margin:0 0 12px;"><?php echo esc_html( $t['con_email_desc'] ); ?></p>
            <a href="mailto:office@parcelselector.com" class="parcelselector-btn parcelselector-btn-primary">
                office@parcelselector.com
            </a>
        </div>
        <div class="parcelselector-card" style="text-align:center; padding:30px;">
            <p style="font-size:40px; margin:0;">&#x1F310;</p>
            <h3 style="margin:10px 0 5px;"><?php echo esc_html( $t['con_web_title'] ); ?></h3>
            <p style="color:#666; font-size:13px; margin:0 0 12px;"><?php echo esc_html( $t['con_web_desc'] ); ?></p>
            <a href="https://www.parcelselector.com/" target="_blank" rel="noopener noreferrer" class="parcelselector-btn parcelselector-btn-primary">
                www.parcelselector.com &#x2192;
            </a>
        </div>
        <div class="parcelselector-card" style="text-align:center; padding:30px;">
            <p style="font-size:40px; margin:0;">&#x1F4AC;</p>
            <h3 style="margin:10px 0 5px;"><?php echo esc_html( $t['con_support_title'] ); ?></h3>
            <p style="color:#666; font-size:13px; margin:0 0 12px;"><?php echo esc_html( $t['con_support_desc'] ); ?></p>
            <a href="https://parcelselector.com/parcelselector-kontakt/" target="_blank" rel="noopener noreferrer" class="parcelselector-btn parcelselector-btn-secondary">
                <?php echo esc_html( $t['con_support_btn'] ); ?> &#x2192;
            </a>
        </div>
    </div>

    <!-- ═══ TWOJE DANE LICENCJI (kontekst który wyślemy razem z wiadomością) ═══ -->
    <div class="parcelselector-section">
        <h3>&#x1F512; <?php esc_html_e( 'Your installation details', 'parcel-selector' ); ?></h3>
        <div class="parcelselector-card" style="padding:18px 24px;">
            <p style="color:#666; font-size:13px; margin:0 0 14px;">
                <?php esc_html_e( 'This data will be automatically attached to your message — it helps us identify and resolve the issue faster.', 'parcel-selector' ); ?>
            </p>
            <div style="display:grid; grid-template-columns:repeat(auto-fill,minmax(220px,1fr)); gap:10px; font-size:13px;">
                <div style="background:#f8f9fa; padding:10px 14px; border-radius:6px;">
                    <div style="color:#888; font-size:11px; text-transform:uppercase; letter-spacing:.5px; margin-bottom:3px;"><?php esc_html_e( 'Domain', 'parcel-selector' ); ?></div>
                    <strong><?php echo esc_html( $domain ); ?></strong>
                </div>
                <?php if ( $install_id ) { ?>
                <div style="background:#f8f9fa; padding:10px 14px; border-radius:6px; grid-column:1/-1;">
                    <div style="color:#888; font-size:11px; text-transform:uppercase; letter-spacing:.5px; margin-bottom:3px;"><?php esc_html_e( 'Installation ID', 'parcel-selector' ); ?></div>
                    <code style="font-size:12px;"><?php echo esc_html( $install_id ); ?></code>
                </div>
                <?php } ?>
            </div>
        </div>
    </div>

    <!-- ═══ FORMULARZ KONTAKTOWY ═══ -->
    <div class="parcelselector-section" style="margin-top:10px;">
        <h3>&#x1F4DD; <?php echo esc_html( $t['con_form_title'] ?? __( 'Send a message', 'parcel-selector' ) ); ?></h3>
        <div class="parcelselector-card" style="padding:24px;">

            <!-- Wynik AJAX (sukces / błąd) -->
            <div id="contact-result" style="display:none; padding:12px 16px; margin-bottom:16px; border-radius:4px;"></div>

            <!-- Panel sukcesu — kopia wysłanej wiadomości -->
            <div id="contact-sent-copy" style="display:none; background:#f0fdf4; border:1px solid #22c55e; border-left:4px solid #22c55e; border-radius:6px; padding:20px; margin-bottom:20px;">
                <h4 style="margin:0 0 12px; color:#166534;">&#x2705; <?php esc_html_e( 'Message sent — here is a copy of what was sent:', 'parcel-selector' ); ?></h4>
                <div id="contact-sent-details" style="font-size:13px; background:#fff; border:1px solid #d1fae5; border-radius:4px; padding:16px; white-space:pre-wrap; font-family:monospace; line-height:1.7;"></div>
                <p style="margin:10px 0 0; color:#166534; font-size:13px;">
                    &#x1F4E7; <?php esc_html_e( 'We have sent a copy of this message to your email address. We will respond within 24–48h.', 'parcel-selector' ); ?>
                </p>
                <button type="button" id="btn-send-another" class="parcelselector-btn parcelselector-btn-secondary" style="margin-top:12px;">
                    &#x1F4DD; <?php esc_html_e( 'Send another message', 'parcel-selector' ); ?>
                </button>
            </div>

            <!-- Formularz -->
            <form id="parcelselector-contact-form" style="max-width:720px;">

                <div style="display:grid; grid-template-columns:1fr 1fr; gap:16px; margin-bottom:16px;">
                    <div>
                        <label for="contact_name" style="display:block; font-weight:600; margin-bottom:4px;">
                            <?php echo esc_html( $t['con_name'] ?? __( 'Your first and last name', 'parcel-selector' ) ); ?> <span style="color:#e74c3c;">*</span>
                        </label>
                        <input type="text" id="contact_name" name="sender_name" class="regular-text" style="width:100%;"
                               value="<?php echo esc_attr( $current_user->display_name ); ?>"
                               maxlength="200" required>
                    </div>
                    <div>
                        <label for="contact_email" style="display:block; font-weight:600; margin-bottom:4px;">
                            <?php echo esc_html( $t['con_email'] ); ?> <span style="color:#e74c3c;">*</span>
                        </label>
                        <input type="email" id="contact_email" name="sender_email" class="regular-text" style="width:100%;"
                               value="<?php echo esc_attr( $current_user->user_email ); ?>"
                               maxlength="255" required>
                    </div>
                </div>

                <div style="display:grid; grid-template-columns:1fr 1fr; gap:16px; margin-bottom:16px;">
                    <div>
                        <label for="contact_phone" style="display:block; font-weight:600; margin-bottom:4px;">
                            <?php echo esc_html( $t['con_phone'] ?? __( 'Phone', 'parcel-selector' ) ); ?>
                        </label>
                        <div style="display:flex;align-items:stretch;border:1px solid #d1d5db;border-radius:4px;overflow:hidden;background:#fff;">
                            <span style="background:#f1f5f9;padding:0 12px;display:flex;align-items:center;font-weight:700;color:#475569;font-size:13px;border-right:1px solid #d1d5db;flex-shrink:0;letter-spacing:0.3px;" aria-hidden="true">+48</span>
                            <input type="tel" id="contact_phone" name="sender_phone" style="border:0;outline:0;padding:6px 10px;flex:1;font-size:14px;background:transparent;min-width:0;"
                                   maxlength="11" pattern="[\d\s]{9,11}" inputmode="numeric" autocomplete="tel-national" placeholder="500 100 200">
                        </div>
                    </div>
                    <div>
                        <label for="contact_category" style="display:block; font-weight:600; margin-bottom:4px;">
                            <?php echo esc_html( $t['con_category'] ?? __( 'Category', 'parcel-selector' ) ); ?>
                        </label>
                        <select id="contact_category" name="category" style="width:100%; height:36px;">
                            <option value="general"><?php echo esc_html( $t['con_cat_general'] ?? __( 'General question', 'parcel-selector' ) ); ?></option>
                            <option value="bug"><?php echo esc_html( $t['con_cat_bug'] ?? __( 'Bug report', 'parcel-selector' ) ); ?></option>
                            <option value="feature"><?php echo esc_html( $t['con_cat_feature'] ?? __( 'Feature suggestion', 'parcel-selector' ) ); ?></option>
                            <option value="billing"><?php echo esc_html( $t['con_cat_billing'] ?? __( 'Payments and invoice', 'parcel-selector' ) ); ?></option>
                            <option value="technical"><?php echo esc_html( $t['con_cat_technical'] ?? __( 'Technical support', 'parcel-selector' ) ); ?></option>
                            <option value="other"><?php echo esc_html( $t['con_cat_other'] ?? __( 'Other', 'parcel-selector' ) ); ?></option>
                        </select>
                    </div>
                </div>

                <div style="margin-bottom:16px;">
                    <label for="contact_subject" style="display:block; font-weight:600; margin-bottom:4px;">
                        <?php echo esc_html( $t['con_subject'] ?? __( 'Subject', 'parcel-selector' ) ); ?>
                    </label>
                    <input type="text" id="contact_subject" name="subject" class="regular-text" style="width:100%;"
                           maxlength="300"
                           placeholder="<?php echo esc_attr( $t['con_subject_ph'] ?? __( 'A short summary of the problem or question', 'parcel-selector' ) ); ?>">
                </div>

                <div style="margin-bottom:20px;">
                    <label for="contact_message" style="display:block; font-weight:600; margin-bottom:4px;">
                        <?php echo esc_html( $t['con_message'] ); ?> <span style="color:#e74c3c;">*</span>
                    </label>
                    <textarea id="contact_message" name="message" rows="7"
                              style="width:100%; min-height:130px; resize:vertical;"
                              maxlength="5000" minlength="10" required
                              placeholder="<?php echo esc_attr( $t['con_placeholder'] ); ?>"></textarea>
                    <p style="margin:4px 0 0; color:#999; font-size:12px;">
                        <span id="contact-char-count">0</span> / 5000 <?php echo esc_html( $t['con_chars'] ?? 'characters' ); ?>
                    </p>
                </div>

                <!-- ── PODGLĄD WIADOMOŚCI ── -->
                <div id="contact-preview-section" style="display:none; margin-bottom:20px;">
                    <h4 style="margin:0 0 8px; font-size:14px; color:#444;">&#x1F441; <?php esc_html_e( 'Preview — what will be sent:', 'parcel-selector' ); ?></h4>
                    <div id="contact-preview-box" style="background:#f8f9fa; border:1px solid #ddd; border-radius:4px; padding:14px 16px; font-size:12px; font-family:monospace; white-space:pre-wrap; color:#333; line-height:1.7; max-height:300px; overflow-y:auto;"></div>
                </div>

                <!-- ── WYŚLIJ KOPIĘ DO MNIE ── -->
                <div style="background:#fff8f0; border:1px solid #fed7aa; border-radius:8px; padding:12px 16px; margin-bottom:18px; display:flex; align-items:center; gap:10px;">
                    <input type="checkbox" id="contact_send_copy" name="send_copy" value="yes"
                           checked style="width:18px; height:18px; cursor:pointer; accent-color:#FF6600; flex-shrink:0;">
                    <label for="contact_send_copy" style="cursor:pointer; font-size:14px; margin:0; font-weight:500; color:#333;">
                        <?php esc_html_e( 'Also send a copy of this message to me', 'parcel-selector' ); ?>
                        (<span id="copy-email-label" style="color:#FF6600; font-weight:600;"><?php echo esc_html( $current_user->user_email ); ?></span>)
                    </label>
                </div>

                <div style="display:flex; align-items:center; gap:12px; flex-wrap:wrap;">
                    <button type="submit" class="parcelselector-btn parcelselector-btn-primary" id="btn-send-contact"
                            style="font-size:15px; padding:10px 28px;">
                        &#x1F4E8; <?php echo esc_html( $t['con_btn_send'] ); ?>
                    </button>
                    <button type="button" id="btn-toggle-preview" class="parcelselector-btn parcelselector-btn-secondary" style="font-size:13px;">
                        &#x1F441; <?php esc_html_e( 'Show preview', 'parcel-selector' ); ?>
                    </button>
                    <span id="contact-spinner" style="display:none;">
                        <span class="spinner is-active" style="float:none; margin:0;"></span>
                    </span>
                </div>

            </form>
        </div>
    </div>


    <!-- ═══ CZĘSTO ZADAWANE PYTANIA (FAQ) ═══ -->
    <div class="parcelselector-section" style="margin-top:30px;">
        <h3>&#x2753; <?php esc_html_e( 'Frequently Asked Questions (FAQ)', 'parcel-selector' ); ?></h3>
        <div class="parcelselector-card" style="padding:10px 0;">
            
            <?php
            wp_add_inline_style( 'parcselect-admin-premium-css', '
                .ps-faq-item { border-bottom: 1px solid #f0f0f0; }
                .ps-faq-item:last-child { border-bottom: none; }
                .ps-faq-trigger { width: 100%; padding: 16px 24px; text-align: left; background: none; border: none; display: flex; justify-content: space-between; align-items: center; cursor: pointer; font-weight: 600; font-size: 14px; color: #333; transition: background 0.2s; }
                .ps-faq-trigger:hover { background: #fafafa; }
                .ps-faq-content { padding: 0 24px 16px; display: none; font-size: 13px; color: #666; line-height: 1.6; }
                .ps-faq-trigger.active { color: #FF6600; }
                .ps-faq-trigger .ps-icon { transition: transform 0.3s; }
                .ps-faq-trigger.active .ps-icon { transform: rotate(180deg); }
            ' );
            ?>

            <div class="ps-faq-item">
                <button type="button" class="ps-faq-trigger">
                    <span>&#x1F4B3; <?php esc_html_e( 'Are you planning to support cryptocurrency payments?', 'parcel-selector' ); ?></span>
                    <span class="ps-icon">&#x25BC;</span>
                </button>
                <div class="ps-faq-content" style="display:block;">
                    <p style="margin:0; font-weight:600; color:#2c3e50;">
                        &#x1F680; <?php esc_html_e( 'Yes! We are working on implementing cryptocurrency payments (BTC, ETH, USDT).', 'parcel-selector' ); ?>
                    </p>
                    <p style="margin:8px 0 0;">
                        <?php esc_html_e( 'This feature will appear soon in one of the upcoming updates. It will allow even faster and more anonymous order counter top-ups directly from your crypto wallet.', 'parcel-selector' ); ?>
                    </p>
                </div>
            </div>

            <div class="ps-faq-item">
                <button type="button" class="ps-faq-trigger">
                    <span>&#x1F4E6; <?php esc_html_e( 'Are there any order limits?', 'parcel-selector' ); ?></span>
                    <span class="ps-icon">&#x25BC;</span>
                </button>
                <div class="ps-faq-content">
                    <?php esc_html_e( 'The free version of ParcelSelector has no order limits — the map and all features work without restrictions.', 'parcel-selector' ); ?>
                </div>
            </div>

            <div class="ps-faq-item">
                <button type="button" class="ps-faq-trigger">
                    <span>&#x1F510; <?php esc_html_e( 'Is my license secure?', 'parcel-selector' ); ?></span>
                    <span class="ps-icon">&#x25BC;</span>
                </button>
                <div class="ps-faq-content">
                    <?php esc_html_e( 'Yes, all connections are encrypted. We do not store any personal data of your customers on our servers — that data is processed exclusively locally in your WooCommerce store.', 'parcel-selector' ); ?>
                </div>
            </div>

        </div>
    </div>

    <?php
    wp_add_inline_script( 'parcselect-admin-premium-js', 'jQuery(document).ready(function($) {
        $(".ps-faq-trigger").on("click", function() {
            var $content = $(this).next(".ps-faq-content");
            var $allContent = $(".ps-faq-content");
            var $allTriggers = $(".ps-faq-trigger");
            if ($content.is(":visible")) {
                $content.slideUp(200);
                $(this).removeClass("active");
            } else {
                $allContent.slideUp(200);
                $allTriggers.removeClass("active");
                $content.slideDown(200);
                $(this).addClass("active");
            }
        });
    });' );
    ?>

    <!-- ═══ DANE TECHNICZNE ═══ -->
    <div class="parcelselector-section" style="margin-top:30px;">
        <h3>&#x2139;&#xFE0F; <?php echo esc_html( $t['con_info_title'] ); ?></h3>
        <div class="parcelselector-card">
            <table class="form-table" role="presentation">
                <tr>
                    <th scope="row">Plugin</th>
                    <td>&#x1F4E6; ParcelSelector.com v<?php echo esc_html( PARCSELECT_VERSION ); ?></td>
                </tr>
                <?php if ( $install_id ) { ?>
                <tr>
                    <th scope="row"><?php esc_html_e( 'Installation ID', 'parcel-selector' ); ?></th>
                    <td><code style="font-size:11px;"><?php echo esc_html( $install_id ); ?></code></td>
                </tr>
                <?php } ?>
                <tr>
                    <th scope="row">WordPress</th>
                    <td><?php echo esc_html( get_bloginfo('version') ); ?></td>
                </tr>
                <tr>
                    <th scope="row">WooCommerce</th>
                    <td><?php echo class_exists('WooCommerce') ? esc_html( WC()->version ) : 'N/A'; ?></td>
                </tr>
                <tr>
                    <th scope="row">PHP</th>
                    <td><?php echo esc_html( phpversion() ); ?></td>
                </tr>
                <tr>
                    <th scope="row">Site URL</th>
                    <td><code><?php echo esc_html( home_url() ); ?></code></td>
                </tr>
            </table>
        </div>
    </div>

    <?php
    // Localize contact form data for JS
    wp_localize_script( 'parcselect-admin-premium-js', 'parcselectContactVars', array(
        'licenseContext' => array(
            'domain'      => $domain,
            'siteName'    => $site_name,
            'installId'   => $install_id,
            'pluginVer'   => PARCSELECT_VERSION,
            'wpVer'       => get_bloginfo( 'version' ),
            'wcVer'       => class_exists( 'WooCommerce' ) ? WC()->version : 'N/A',
            'phpVer'      => phpversion(),
        ),
        'nonce'          => wp_create_nonce( 'parcselect_contact' ),
        'defaultName'    => $current_user->display_name,
        'defaultEmail'   => $current_user->user_email,
        'i18n'           => array(
            'err_name'  => $t['con_err_name'] ?? 'Please enter your name.',
            'err_email' => $t['con_err_email'] ?? 'Please enter a valid email address.',
            'err_short' => $t['con_err_short'] ?? 'The message must be at least 10 characters.',
            'err_long'  => $t['con_err_long'] ?? 'The message cannot exceed 5000 characters.',
            'error'     => $t['error'] ?? 'Error',
        ),
    ) );

    wp_add_inline_script( 'parcselect-admin-premium-js', 'jQuery(document).ready(function($) {
        var $msg = $("#contact_message");
        var $counter = $("#contact-char-count");
        var licenseContext = parcselectContactVars.licenseContext;

        $msg.on("input", function() {
            $counter.text(this.value.length);
            $counter.css("color", this.value.length > 5000 ? "#e74c3c" : "#999");
            updatePreview();
        });

        function buildPreviewText() {
            var name     = $("#contact_name").val().trim() || "\u2014";
            var email    = $("#contact_email").val().trim() || "\u2014";
            var phone    = $("#contact_phone").val().trim();
            var cat      = $("#contact_category").val() || "general";
            var subject  = $("#contact_subject").val().trim() || "(no subject)";
            var message  = $msg.val().trim() || "(no content)";
            var lines = [];
            lines.push("=== ParcelSelector \u2014 Support message ===");
            lines.push("");
            lines.push("From          : " + name + " <" + email + ">");
            if (phone) lines.push("Phone         : " + phone);
            lines.push("Category      : " + cat);
            lines.push("Subject       : " + subject);
            lines.push("");
            lines.push("--- Content ---");
            lines.push(message);
            lines.push("");
            lines.push("--- Installation details ---");
            lines.push("Domain        : " + licenseContext.domain);
            lines.push("Site          : " + licenseContext.siteName);
            lines.push("Install ID    : " + (licenseContext.installId || "\u2014"));
            lines.push("Plugin ver    : " + licenseContext.pluginVer);
            lines.push("WP / WC / PHP : " + licenseContext.wpVer + " / " + licenseContext.wcVer + " / " + licenseContext.phpVer);
            lines.push("");
            lines.push("Copy to       : office@parcelselector.com + " + email);
            return lines.join("\\n");
        }

        function updatePreview() {
            if ($("#contact-preview-section").is(":visible")) {
                $("#contact-preview-box").text(buildPreviewText());
            }
        }

        $("#contact_name, #contact_email, #contact_phone, #contact_category, #contact_subject").on("input change", updatePreview);

        $("#contact_email").on("input", function() {
            $("#copy-email-label").text(this.value.trim() || "\u2014");
        });

        $("#btn-toggle-preview").on("click", function() {
            var $section = $("#contact-preview-section");
            if ($section.is(":visible")) {
                $section.slideUp(200);
                $(this).html("\uD83D\uDC41 Show preview");
            } else {
                $("#contact-preview-box").text(buildPreviewText());
                $section.slideDown(200);
                $(this).html("\u25B2 Hide preview");
            }
        });

        $("#parcelselector-contact-form").on("submit", function(e) {
            e.preventDefault();
            var $btn     = $("#btn-send-contact");
            var $spinner = $("#contact-spinner");
            var $result  = $("#contact-result");
            var name    = $("#contact_name").val().trim();
            var email   = $("#contact_email").val().trim();
            var message = $msg.val().trim();
            if (!name)                { showResult("error", parcselectContactVars.i18n.err_name); return; }
            if (!email)               { showResult("error", parcselectContactVars.i18n.err_email); return; }
            if (message.length < 10)  { showResult("error", parcselectContactVars.i18n.err_short); return; }
            if (message.length > 5000){ showResult("error", parcselectContactVars.i18n.err_long); return; }
            $btn.prop("disabled", true);
            $spinner.show();
            $result.hide();
            var sentPreview = buildPreviewText();
            $.ajax({
                url:  ajaxurl,
                type: "POST",
                data: {
                    action:       "parcselect_send_contact",
                    _wpnonce:     parcselectContactVars.nonce,
                    sender_name:  name,
                    sender_email: email,
                    sender_phone: $("#contact_phone").val().trim(),
                    subject:      $("#contact_subject").val().trim(),
                    message:      message,
                    category:     $("#contact_category").val(),
                    send_copy:    $("#contact_send_copy").is(":checked") ? "yes" : "no"
                },
                success: function(response) {
                    $btn.prop("disabled", false);
                    $spinner.hide();
                    if (response.success) {
                        var ticketId = (response.data && response.data.ticket_id) ? response.data.ticket_id : "";
                        $("#parcelselector-contact-form").slideUp(300);
                        $("#contact-preview-section").hide();
                        $("#contact-result").hide();
                        if (ticketId) { sentPreview = "Ticket ID: #" + ticketId + "\\n\\n" + sentPreview; }
                        $("#contact-sent-details").text(sentPreview);
                        $("#contact-sent-copy").slideDown(400);
                        $("html, body").animate({ scrollTop: $("#contact-sent-copy").offset().top - 80 }, 400);
                    } else {
                        showResult("error", (response.data && response.data.message) ? response.data.message : parcselectContactVars.i18n.error);
                    }
                },
                error: function(xhr, status, error) {
                    $btn.prop("disabled", false);
                    $spinner.hide();
                    showResult("error", "Connection error: " + error);
                }
            });
        });

        $("#btn-send-another").on("click", function() {
            $("#contact-sent-copy").slideUp(200, function() {
                $msg.val("");
                $("#contact_subject").val("");
                $counter.text("0");
                $("#parcelselector-contact-form")[0].reset();
                $("#contact_name").val(parcselectContactVars.defaultName);
                $("#contact_email").val(parcselectContactVars.defaultEmail);
                $("#parcelselector-contact-form").slideDown(300);
            });
        });

        function showResult(type, msg) {
            var $result = $("#contact-result");
            var bg     = type === "success" ? "#f0fdf4" : "#fff2f2";
            var border = type === "success" ? "#22c55e" : "#e74c3c";
            var color  = type === "success" ? "#166534" : "#c0392b";
            var icon   = type === "success" ? "\u2705" : "\u274C";
            $result.html(icon + " " + msg)
                   .css({ background: bg, border: "1px solid " + border, "border-left": "4px solid " + border, color: color })
                   .show();
            $("html, body").animate({ scrollTop: $result.offset().top - 100 }, 300);
        }
    });' );
}
}
