/**
* 2008-2024 Prestaworld
*
* NOTICE OF LICENSE
*
* The source code of this module is under a commercial license.
* Each license is unique and can be installed and used on only one website.
* Any reproduction or representation total or partial of the module, one or more of its components,
* by any means whatsoever, without express permission from us is prohibited.
*
* DISCLAIMER
*
* Do not alter or add/update to this file if you wish to upgrade this module to newer
* versions in the future.
*
* @author    prestaworld
* @copyright 2008-2024 Prestaworld
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
* International Registered Trademark & Property of prestaworld
*/

$(document).ready(function() {
    // Yes or No fetch dependant fields
    $(document).on('change', '.presta_yes_no input', function(){
        let currentInputVal = $(this).val();
        let fieldId = $(this).parents('.presta_yes_no').attr('data-id');
        let currentParentDiv = $(this).parents('.presta_field-dependant');
        getDependantField(currentParentDiv, currentInputVal, fieldId);
    });

    let currentInputVal = $('.presta_yes_no input:checked').val();
    let fieldId = $('.presta_yes_no input:checked').parents('.presta_yes_no').attr('data-id');
    let currentParentDiv = $('.presta_yes_no input:checked').parents('.presta_field-dependant');
    getDependantField(currentParentDiv, currentInputVal, fieldId);

    // End of code

    // Dropdown fetch dependant fields
    $(document).on('change', '.presta_dropDown_select select', function(){
        let currentInputVal = $(this).val();
        let fieldId = $(this).parents('.presta_dropDown_select').attr('data-id');
        let currentParentDiv = $(this).parents('.presta_field-dependant');
        getDependantField(currentParentDiv, currentInputVal, fieldId);
    });

    let currentSelectInputVal = $('.presta_dropDown_select select option:selected').val();
    let selectedFieldId = $('.presta_dropDown_select select option:selected').parents('.presta_dropDown_select').attr('data-id');
    let currentSelectParentDiv = $('.presta_dropDown_select select option:selected').parents('.presta_field-dependant');
    getDependantField(currentSelectParentDiv, currentSelectInputVal, selectedFieldId);

    // End of code

    // Radio button fetch dependant fields
    $(document).on('change', '.presta_radio_select input', function(){
        let currentInputVal = $(this).val();
        let fieldId = $(this).parents('.presta_radio_select').attr('data-id');
        let currentParentDiv = $(this).parents('.presta_field-dependant');
        getDependantField(currentParentDiv, currentInputVal, fieldId);
    });

    // let currentRadioInputVal = $('.presta_radio_select input:checked').val();
    // let radioFieldId = $('.presta_radio_select input:checked').parents('.presta_radio_select').attr('data-id');
    // let currentRadioParentDiv = $('.presta_radio_select input:checked').parents('.presta_field-dependant');
    // getDependantField(currentRadioInputVal, radioFieldId, currentRadioParentDiv);

    // End of code
    // if ($('#g-recaptchaa').length && typeof presta_v3_site !== 'undefined') {
    //     grecaptcha.ready(function() {
    //         grecaptcha.render(
    //             'g-recaptchaa',
    //             {
    //                 sitekey: presta_v3_site,
    //                 size: 'invisible',
    //                 theme: presta_v3_theme,
    //                 badge: presta_v3_pos
    //             }
    //         );
    //         grecaptcha.execute();
    //     });
    // }
});
function refreshCaptcha() {
    var img = document.images['presta_captcha_image'];
    img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;
}

function getDependantField(currentParentDiv, currentInputVal, fieldId)
{
    $.ajax({
        url: presta_reg_process_url,
        method: 'POST',
        dataType: 'json',
        cache: false,
        data : {
            ajax: true,
            action: 'getDependantField',
            currentInputVal: currentInputVal,
            fieldId: fieldId,
            front_controller:tf_front_controller
        },
        success: function(result) {
            if ($('.customIdField-'+fieldId).length) {
                $('.customIdField-'+fieldId).remove();
            }
            if (result.status == 'ok') {
                currentParentDiv.after(result.tpl);
                $('span.presta_custom_errors').each(function(key, obj) {
                    let errorMsg = $(this).attr('data-error-value');
                    let errorId = $(this).attr('data-error');
                    $('p[class="'+errorId+'"').remove();
                    $('<p class="presta_custom_error_msg '+errorId+'" style="color:red;">'+errorMsg+'</p>').insertAfter('div[data-custom="'+errorId+'"]');
                });
            }
        }
    });
}
