// source --> https://mandalomarket.com/wp-content/plugins/woo-phone-input-plugin/js/phone-validate.js?ver=2.0.1 
"use strict";

var $j = jQuery.noConflict();

$j(function() {

	var telInputs = $j(wipiValidationJsVars.wpiElements).toArray();

	for (var i = 0; i < telInputs.length; i++) {

		var telInput = $j(telInputs[i]);
	
		telInput.after('<span class="int-phone valid-msg hide">'+ wipiValidationJsVars.successMessage +'</span>');
		telInput.after('<span class="int-phone error-msg hide">'+ wipiValidationJsVars.failMessage +'</span>');

		telInput.blur(function() {
			wpisValidateIntPhone(this);
		});

		telInput.keydown(function() {
		  wpisHideValidationErrors(this);
		});

		if ( telInput.val().length > 0 ) {
			setTimeout(function(){
				wpisValidateIntPhone(telInput[0]);
			}, 3000);
		}        
	}

});

function wpisHideValidationErrors(telInputEl) {
  $j(telInputEl).removeClass("error");
  $j(telInputEl).parent().parent().find(".error-msg").addClass("hide");
  $j(telInputEl).parent().parent().find(".valid-msg").addClass("hide");
}

function wpisValidateIntPhone(telInputEl){

	var telInput = $j(telInputEl);
	
	var errorMsg = telInput.parent().parent().find(".error-msg"),
	validMsg = telInput.parent().parent().find(".valid-msg");

	wpisHideValidationErrors(telInputEl);

	if ($j.trim(telInput.val())) {
		if (telInput.intlTelInput("isValidNumber")) {
			validMsg.removeClass("hide");
			var nationalPhone = telInput.intlTelInput("getNumber");
			telInput.val(nationalPhone);
			// $j('.woocommerce #payment #place_order').prop( "disabled", false );
		} else {
			telInput.addClass("error");
			errorMsg.removeClass("hide");
			validMsg.addClass("hide");
			// $j('.woocommerce #payment #place_order').prop( "disabled", true );
		}
	}
};