/**---------------------------------------------------------------------------------------------------------------------
 * Social Share
 * --------------------------------------------------------------------------------------------------------------------*/
jQuery(document).ready(function () {
  if (jQuery('.wl-posts a.share').length) {
    jQuery('.wl-posts a.share').on('click', function (e) {
      e.preventDefault();
      jQuery(this).toggleClass('active');
      jQuery('.wl-posts .share-print').toggleClass('active');
    })
  }
});


/**---------------------------------------------------------------------------------------------------------------------
 * Alignment of element heights
 * --------------------------------------------------------------------------------------------------------------------*/
jQuery(document).ready(function () {
  !function(i){i.fn.equalize=function(e){var n,t,h=!1,c=!1;return i.isPlainObject(e)?(n=e.equalize||"height",h=e.children||!1,c=e.reset||!1):n=e||"height",i.isFunction(i.fn[n])?(t=0<n.indexOf("eight")?"height":"width",this.each(function(){var e=h?i(this).find(h):i(this).children(),s=0;e.each(function(){var e=i(this);c&&e.css(t,""),e=e[n](),e>s&&(s=e)}),e.css(t,s+"px")})):!1}}(jQuery);

  function aligner() {
    jQuery('.single-blog-posts').equalize({ equalize: 'outerHeight', reset: true, children: '.single-blog-post-title' });
    jQuery('.wl-posts').equalize({ equalize: 'outerHeight', reset: true, children: '.wl-post-title' });
    jQuery('.wl-posts').equalize({ equalize: 'outerHeight', reset: true, children: '.wl-post-content' });
  }

  if (jQuery(window).outerWidth() > 767) {
    aligner();
  }

  jQuery(window).resize(function () {
    if (jQuery(window).outerWidth() > 767) {
      aligner();
    }
  });
});


/**---------------------------------------------------------------------------------------------------------------------
 *  Ol li more than 15
 * --------------------------------------------------------------------------------------------------------------------*/
jQuery(document).ready(function () {
  let singleOl = jQuery('.single-content-left-col ol');
  if (singleOl.length > 0) {
    singleOl.each(function (i, el) {
      let maxOl = jQuery(this);
      if (maxOl.children('li').length > 15) {
        maxOl.css('column-count', '2');
      }
    });
  }
});


/**---------------------------------------------------------------------------------------------------------------------
 * Rank Math Faq Accordion
 * --------------------------------------------------------------------------------------------------------------------*/
jQuery(document).ready(function () {
  let faqWrap = jQuery('.single-content-left-col #rank-math-faq .rank-math-list');

  if (faqWrap.length > 0) {
    faqWrap.find('.rank-math-list-item').each(function (i, el) {
      let faq = jQuery(this);
      let faqTitle = faq.find('.rank-math-question');
      let faqContent = faq.find('.rank-math-answer');
      if (i === 0) {
        faqContent.slideDown();
        faqTitle.addClass('active');
      } else {
        faqContent.slideUp();
        faqTitle.removeClass('active');
      }
      faq.click(function () {
        faqContent.slideDown(200);
        faqTitle.addClass('active');
        jQuery(this).siblings('.rank-math-list-item').find('.rank-math-answer').slideUp(200);
        jQuery(this).siblings('.rank-math-list-item').find('.rank-math-question').removeClass('active');
      });
    });
  }
});


/**---------------------------------------------------------------------------------------------------------------------
 * Wrap the iframe with a center tag
 * --------------------------------------------------------------------------------------------------------------------*/
jQuery(document).ready(function () {
  jQuery('.single-content-left-col iframe').each(function () {
    if (jQuery(this).parent('p')) {
      jQuery(this).wrap('<center></center>');
    }
  });
});


/**---------------------------------------------------------------------------------------------------------------------
 * Remove <p>&nbsp:</p> in single post
 * --------------------------------------------------------------------------------------------------------------------*/
jQuery('.single-content-left-col p').each( function(i,val) {
  if (jQuery(val).html() === "&nbsp;") {
    jQuery(this).remove();
  }
});

/**---------------------------------------------------------------------------------------------------------------------
 * Form Captcha
 * --------------------------------------------------------------------------------------------------------------------*/
// Remove all non-numeric characters from the beginning of the string
function removeNonNumericPrefix(inputString) {

  let numericString = inputString.replace(/^\D+/g, '');

  return numericString;
}

// Set Value Input Formstack New Standart
function setValueFormstackInpup( idForm, idInput, valie ) {
  const form = window.fsApi().getForm( removeNonNumericPrefix(idForm) );
  const dataField = form.getField( removeNonNumericPrefix(idInput) );

  if(form && dataField){
    dataField.setValue( valie );
  }
}
// END Set Value Input Formstack New Standart

jQuery(document).ready(function ($) {
  //Random 1-50
  function getRandomNumberWithout( excludedNumber ) {
    let randomNumber = Math.floor( Math.random() * 50 ) + 1;
    while ( randomNumber === excludedNumber ) {
      randomNumber = Math.floor( Math.random() * 50 ) + 1;
    }
    return randomNumber;
  }
  //Page URL in Forms
  function pageURLInForm( idFieldURL, idFieldFirstName, idFieldLastName, idCaptchaElement, captchaElementValue, idButton, idForm ) {
    let currentUrl = window.location.href;
    let urlField = document.getElementById( idFieldURL );
    const form = document.getElementById( idForm );
    const button = document.getElementById( idButton );
    const captchaElement = document.getElementById( idCaptchaElement );

    if ( currentUrl.includes( "?" ) || currentUrl.includes( "%" ) || currentUrl.includes( "~" ) || currentUrl.includes( "&" ) || currentUrl.includes( "yandex" ) || currentUrl.includes( "ru" ) || currentUrl.includes( "hs" ) ) {
      if ( button ) {
        button.disabled = true;
      }

      if ( captchaElement ) {
        captchaElementValue = captchaElementValue + getRandomNumberWithout( captchaElementValue );
        captchaElement.value = captchaElementValue;
      }
    }

    if ( urlField ) {
      urlField.value = currentUrl;
      setValueFormstackInpup( idForm, idFieldURL, currentUrl );
    }

    if(form) {
      form.addEventListener( "submit", ( event ) => {
        const fieldFirstName = document.getElementById( idFieldFirstName );
        const fieldLastName = document.getElementById( idFieldLastName );

        console.log( `fieldFirstName: ${ fieldFirstName.value }` );

        if ( fieldFirstName.value.includes( "?" ) || fieldFirstName.value.includes( "http" ) || fieldFirstName.value.includes( "yandex" ) || fieldFirstName.value.includes( "=" ) || fieldFirstName.value.includes( "ru" ) || fieldLastName.value.includes( "?" ) || fieldLastName.value.includes( "http" ) || fieldLastName.value.includes( "=" ) || fieldLastName.value.includes( "ru" ) || fieldLastName.value.includes( "yandex" ) ) {
          event.preventDefault();

          if ( button ) {
            button.disabled = true;
          }

          if ( idButton ) {
            const button = document.getElementById( idButton );
          }

          if ( idCaptchaElement && captchaElementValue ) {
            const captchaElement = document.getElementById( idCaptchaElement );
          }

          if ( captchaElement ) {
            captchaElementValue = captchaElementValue + getRandomNumberWithout( captchaElementValue );
            captchaElement.value = captchaElementValue;

            setValueFormstackInpup( idForm, idCaptchaElement, captchaElementValue );
          }
        }
      } );
    }
  }

  pageURLInForm( 'field154630240', 'field135581909', 'field135581910', 'field154630187', 55, 'fsSubmitButton5082181', 'fsForm5082181' );

  pageURLInForm( 'field154631272', 'field135581897', 'field135581900', 'field154631271', 55, 'fsSubmitButton5081031', 'fsForm5081031' );

  pageURLInForm( 'field154631541', 'field135536550', 'field135536551', 'field154630162', 55, 'fsSubmitButton5081024', 'fsForm5081024' );
  //End Page URL in Forms
});
