crossays.com Open in urlscan Pro
2606:4700:3037::6815:487  Public Scan

Submitted URL: http://crossays.com/lander/libra-v2/js/form.js
Effective URL: https://crossays.com/lander/libra-v2/js/form.js
Submission: On November 03 via api from US — Scanned from GB

Form analysis 0 forms found in the DOM

Text Content

document.addEventListener('DOMContentLoaded', function () {
  
    // intlTelInput init
    const inputs = document.querySelectorAll('._phone');
    inputs.forEach((input) => {
      window.intlTelInput(input, {
        dropdownContainer: document.body,
        initialCountry: "auto",
        nationalMode: 1,
        autoPlaceholder: "aggressive",
        allowDropdown: 1,
        formatOnDisplay: 1,
        separateDialCode: 1,
        geoIpLookup: function(callback) {
            fetch('https://ipinfo.io/json', {
              cache: 'reload'}).then(response => {
              if ( response.ok ) {
                return response.json()
              }
              throw new Error('Failed: ' + response.status)
            }).then(ipjson => {
              console.log('IPINFO SUCCESS')
              callback(ipjson.country)
            }).catch(e => {
              console.log('IPINFO ERROR')
              callback('AZ')
            })
          },
        utilsScript: 'https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.8/js/utils.min.js',
      });
    })
    
    //Event listeners
    
    document.querySelectorAll('._form').forEach(form => {
      form.addEventListener('submit', FormSend);
    })
    
    document.querySelectorAll('._req').forEach(form => {
      form.addEventListener('focus', inputRemoveError);
    })
    
    /**
    * @param {Event} e - Form 'submit' event
    */
    async function FormSend(e) {
      e.preventDefault();
      
      let leadForm = this;
      let error = FormValidate(leadForm);
      let formData = new FormData(leadForm);
      let phoneInput = document.querySelector('#' + leadForm.id + ' ._phone');
      let sbmtBtn = document.querySelector('#' + leadForm.id + ' ._btn');
      let iti = window.intlTelInputGlobals.getInstance(phoneInput);
      sbmtBtn.disabled = true;
      let fullPhone = iti.getNumber();
      let countryData = iti.getSelectedCountryData();
      let dialCode = countryData.dialCode;
      formData.append('fullPhone', fullPhone);
      formData.append('dialCode', dialCode);
      let pixel = formData.get('p');
      
      if (error === 0) {
        let response = await fetch('mail.php', {
          method: 'POST',
          body: formData
        });
        if (response.ok) {
          leadForm.reset();
          window.location.href = `success.php?p=${pixel}`;
        }else{
          sbmtBtn.disabled = false;
          alert('Произошла ошибка, повторите попытку позже');
        }
      } else {
        sbmtBtn.disabled = false;
        return
      }
    }
    
    /**
    * @param {Object} form - Form that triggered 'submit' event
    */
    function FormValidate(form) {
      let error = 0;
      let formReq = form.querySelectorAll('._req');
      
      for (let index = 0; index < formReq.length; index++) {
        const input = formReq[index];
        formRemoveError(input);
        
        if (input.classList.contains('_name')) {
          if (nameTest(input)) {
            formAddError(input);
            error++;
          }
        } else if (input.classList.contains('_email')) {
          if (emailTest(input)) {
            formAddError(input);
            error++;
          }
        } else if (input.classList.contains('_phone')) {
          if (phoneTest(input)) {
            formAddError(input);
            error++;
          }
        }
      }
      return error;
    }
    
    // HELPERS
    
    function formAddError(input) {
      // input.parentElement.classList.add('_error');
      input.classList.add('_error');
    }
    
    function formRemoveError(input) {
      // input.parentElement.classList.remove('_error');
      input.classList.remove('_error');
    }
    
    function inputRemoveError(e) {
      let input = this;
      // input.parentElement.classList.remove('_error');
      input.classList.remove('_error');
    }
    
    function nameTest(input) {
      return !/^([a-zA-Z]{2,}(?: [a-zA-Z]+){0,2}|[а-яА-яё]{2,}(?: [а-яА-яё]+){0,2})$/.test(input.value.trim());
      
    }
    
    function emailTest(input) {
      return !/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,8})+$/.test(input.value.trim());
    }
    
    function phoneTest(input) {
      return !window.intlTelInputGlobals.getInstance(input).isValidNumber();
    }
    
  });