! function (t) { t.closest = t.closest || function (t) { for (var e = this; e;) { if (e.matches(t)) return e; e = e.parentElement } return null } }(Element.prototype); var codes = [{ "mask": "+374 - XX - XXX - XXX", "countryCode": "AM", "countryName": "Armenia" }, { "mask": "+994 - XX - XXX - XX - XX", "countryCode": "AZ", "countryName": "Azerbaijan" }, { "mask": "+375(XX) XXX - XX - XX", "countryCode": "BY", "countryName": "Belarus" }, { "mask": "+372 - XXXX - XXXX", "countryCode": "EE", "countryName": "Estonia" }, { "mask": "+995(XXX) XXX - XXX", "countryCode": "GE", "countryName": "Georgia" }, { "mask": "+7(7XX) XXX - XX - XX", "countryCode": "KZ", "countryName": "Kazakhstan" }, { "mask": "+996(XXX) XXX - XXX", "countryCode": "KG", "countryName": "Kyrgyzstan" }, { "mask": "+371 - XX - XXX - XXX", "countryCode": "LV", "countryName": "Latvia" }, { "mask": "+370(XXX) XX - XXX", "countryCode": "LT", "countryName": "Lithuania" }, { "mask": "+373 - XXXX - XXXX", "countryCode": "MD", "countryName": "Moldova" }, { "mask": "+7(XXX) XXX - XX - XX", "countryCode": "RU", "countryName": "Russia" }, { "mask": "+992 - XX - XXX - XXXX", "countryCode": "TJ", "countryName": "Tajikistan" }, { "mask": "+993 - X - XXX - XXXX", "countryCode": "TM", "countryName": "Turkmenistan" }, // { // "mask": "+380(XX) XXX - XX - XX", // "countryCode": "UA", // "countryName": "Ukraine" // }, { "mask": "+998 - XX - XXX - XXXX", "countryCode": "UZ", "countryName": "Uzbekistan" }, ]; function createOptions(select, telInput, img) { var optionsString = ''; for (var item in codes) { var el = codes[item]; var codeSpace = el.mask.split(' ')[0]; var codeScobochka = el.mask.split('(')[0]; var code = codeSpace; if (codeScobochka.length < codeSpace.length) code = codeScobochka; optionsString += ""; } select.innerHTML = optionsString; cloneOptions(select, ['RU', 'KZ', 'BY', 'KG']); select.addEventListener('change', function () { setNumberMask(telInput, this.options[this.selectedIndex].value); setFlagImg(img, this.options[this.selectedIndex]); telInput.focus(); }); getCountryCodeByGeo(select, telInput, img) } function cloneOptions(select, codesArray) { var optionsCont = []; codesArray.forEach(function (el) { select.querySelectorAll('option[data-countrycode=' + el + ']').forEach(function (elIn) { optionsCont.push(elIn.cloneNode(true)); }) }); var optgroup = document.createElement('optgroup'); optgroup.label = ''; optgroup.disabled = true; select.insertBefore(optgroup, select.firstChild); optionsCont.reverse().forEach(function (elIn) { select.insertBefore(elIn, select.firstChild); }) } function setNumberMask(input, value) { input.value = ''; input.dataset.valid = 'false'; input.closest('form').classList.remove('form-active'); $(input).unmask(); $(input).mask(value, { autoclear: false, }); input.placeholder = value; } function setFlagImg(img, option, telInput) { img.src = 'public/img/flags/' + option.dataset.countrycode.toLowerCase() + '.svg'; img.alt = option.dataset.countrycode.toLowerCase(); } function selectByCode(select, telInput, img, code) { var nodes = Array.prototype.slice.call(select.children); var indexEl = nodes.indexOf(document.querySelector('option[data-countrycode="' + code + '"]')); document.querySelector('option[data-countrycode="' + code + '"]').selected = true; select.selectedIndex = indexEl; setNumberMask(telInput, select.options[indexEl].value); setFlagImg(img, select.options[indexEl]); } function getCountryCodeByGeo(select, telInput, img) { // var xhr = new XMLHttpRequest(); // xhr.open('GET', 'get_country.php'); var defCountry = 'RU'; // xhr.onload = function() { // if (xhr.status === 200) { // // var obj = JSON.parse(xhr.responseText); // if(document.querySelector('option[data-countrycode="'+xhr.responseText+'"]')){ // selectByCode(select,telInput,img,xhr.responseText); // }else{ // selectByCode(select,telInput,img,defCountry); // } // }else{ // selectByCode(select,telInput,img,defCountry); // } // }; // xhr.onerror = function() { // selectByCode(select,telInput,img,defCountry); // } // xhr.send(); selectByCode(select, telInput, img, defCountry); } createOptions(document.getElementById('phone_code'), document.getElementById('phone'), document.getElementById('flag_img'));