/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


var input = document.querySelector("#phone"), errorMsg = document.querySelector(".error-phone");
var errorMap = ["Número invalido", "Código de país no válido", "Demasiado corto", "Demasiado largo", "Número invalido"];
var country_code = 'CR', utilsScript = '';

$(document).ready(function () {
    var iti = window.intlTelInput(input, {
        //     allowDropdown: false,
        //     autoHideDialCode: false,
        //     autoPlaceholder: "off",
        dropdownContainer: document.body,
        //     excludeCountries: ["us"],
        //     formatOnDisplay: false,
        initialCountry: country_code,
        //    initialCountry: "auto",
        //    geoIpLookup: function(success) {
        //       $.get("http://ipinfo.io", function() {}, "jsonp").always(function(resp) {
        //            var countryCode = (resp && resp.country) ? resp.country : "{{$country_code}}";
        //            console.log('countryCode: '+countryCode);
        //       });
        //    },
        //     hiddenInput: "full_number",
        //     localizedCountries: { 'de': 'Deutschland' },
        //     nationalMode: false,
        //     onlyCountries: ['us', 'gb', 'ch', 'ca', 'do'],
        //     placeholderNumberType: "MOBILE",
        //     preferredCountries: ['cn', 'jp'],
        separateDialCode: true,
        utilsScript: utilsScript
    });

    $(window).keydown(function (e) {
        if (e.shiftKey && parseInt(e.keyCode) === 9) {   //  Shift + Tab
            if ($("#subscribe_form #phone").is(":focus")) {
                $("#subscribe_form input[name='email']").focus();
                e.preventDefault();
                return false;
            }
        } else if (e.which === 9) {                     //  Tab
            if ($("#subscribe_form input[name='email']").is(":focus")) {
                $('#subscribe_form #phone').focus();
                e.preventDefault();
                return false;
            }
        }
    });

    $('#subscribe_form').submit(function (e) {
        //        e.preventDefault();
        if (input.value.trim()) {
            if (iti.isValidNumber()) {
                input.classList.remove("error");
                errorMsg.innerHTML = '';
                var data = iti.getSelectedCountryData();
                $("input[name='phone']").val(((typeof data.dialCode !== 'undefined') ? data.dialCode.toString() : '') + $("#phone").val().toString());
            } else {
                e.preventDefault();
                input.classList.add("error");
                var errorCode = iti.getValidationError();
                var msg = errorMap[errorCode];
                errorMsg.innerHTML = ((typeof msg !== 'undefined') ? msg : 'Número invalido');
                $('.option-send-activation-code').hide();
                $('#btn_send_code_email').show();

            }
        }
    });
});